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-ascii-export-as-ascii
"org-e-ascii"
79 (&optional subtreep visible-only body-only ext-plist
))
80 (declare-function org-e-ascii-export-to-ascii
"org-e-ascii"
81 (&optional subtreep visible-only body-only ext-plist pub-dir
))
82 (declare-function org-e-html-export-as-html
"org-e-html"
83 (&optional subtreep visible-only body-only ext-plist
))
84 (declare-function org-e-html-export-to-html
"org-e-html"
85 (&optional subtreep visible-only body-only ext-plist pub-dir
))
86 (declare-function org-e-latex-export-as-latex
"org-e-latex"
87 (&optional subtreep visible-only body-only ext-plist
))
88 (declare-function org-e-latex-export-to-latex
"org-e-latex"
89 (&optional subtreep visible-only body-only ext-plist pub-dir
))
90 (declare-function org-e-latex-export-to-pdf
"org-e-latex"
91 (&optional subtreep visible-only body-only ext-plist pub-dir
))
92 (declare-function org-e-odt-export-to-odt
"org-e-odt"
93 (&optional subtreep visible-only body-only ext-plist pub-dir
))
94 (declare-function org-e-publish
"org-e-publish" (project &optional force
))
95 (declare-function org-e-publish-all
"org-e-publish" (&optional force
))
96 (declare-function org-e-publish-current-file
"org-e-publish" (&optional force
))
97 (declare-function org-e-publish-current-project
"org-e-publish"
99 (declare-function org-export-blocks-preprocess
"org-exp-blocks")
101 (defvar org-e-publish-project-alist
)
102 (defvar org-table-number-fraction
)
103 (defvar org-table-number-regexp
)
107 ;;; Internal Variables
109 ;; Among internal variables, the most important is
110 ;; `org-export-options-alist'. This variable define the global export
111 ;; options, shared between every exporter, and how they are acquired.
113 (defconst org-export-max-depth
19
114 "Maximum nesting depth for headlines, counting from 0.")
116 (defconst org-export-options-alist
117 '((:author
"AUTHOR" nil user-full-name t
)
118 (:creator
"CREATOR" nil org-export-creator-string
)
119 (:date
"DATE" nil nil t
)
120 (:description
"DESCRIPTION" nil nil newline
)
121 (:email
"EMAIL" nil user-mail-address t
)
122 (:exclude-tags
"EXCLUDE_TAGS" nil org-export-exclude-tags split
)
123 (:headline-levels nil
"H" org-export-headline-levels
)
124 (:keywords
"KEYWORDS" nil nil space
)
125 (:language
"LANGUAGE" nil org-export-default-language t
)
126 (:preserve-breaks nil
"\\n" org-export-preserve-breaks
)
127 (:section-numbers nil
"num" org-export-with-section-numbers
)
128 (:select-tags
"SELECT_TAGS" nil org-export-select-tags split
)
129 (:time-stamp-file nil
"timestamp" org-export-time-stamp-file
)
130 (:title
"TITLE" nil nil space
)
131 (:with-archived-trees nil
"arch" org-export-with-archived-trees
)
132 (:with-author nil
"author" org-export-with-author
)
133 (:with-clocks nil
"c" org-export-with-clocks
)
134 (:with-creator nil
"creator" org-export-with-creator
)
135 (:with-drawers nil
"d" org-export-with-drawers
)
136 (:with-email nil
"email" org-export-with-email
)
137 (:with-emphasize nil
"*" org-export-with-emphasize
)
138 (:with-entities nil
"e" org-export-with-entities
)
139 (:with-fixed-width nil
":" org-export-with-fixed-width
)
140 (:with-footnotes nil
"f" org-export-with-footnotes
)
141 (:with-inlinetasks nil
"inline" org-export-with-inlinetasks
)
142 (:with-plannings nil
"p" org-export-with-planning
)
143 (:with-priority nil
"pri" org-export-with-priority
)
144 (:with-special-strings nil
"-" org-export-with-special-strings
)
145 (:with-sub-superscript nil
"^" org-export-with-sub-superscripts
)
146 (:with-toc nil
"toc" org-export-with-toc
)
147 (:with-tables nil
"|" org-export-with-tables
)
148 (:with-tags nil
"tags" org-export-with-tags
)
149 (:with-tasks nil
"tasks" org-export-with-tasks
)
150 (:with-timestamps nil
"<" org-export-with-timestamps
)
151 (:with-todo-keywords nil
"todo" org-export-with-todo-keywords
))
152 "Alist between export properties and ways to set them.
154 The CAR of the alist is the property name, and the CDR is a list
155 like (KEYWORD OPTION DEFAULT BEHAVIOUR) where:
157 KEYWORD is a string representing a buffer keyword, or nil. Each
158 property defined this way can also be set, during subtree
159 export, through an headline property named after the keyword
160 with the \"EXPORT_\" prefix (i.e. DATE keyword and EXPORT_DATE
162 OPTION is a string that could be found in an #+OPTIONS: line.
163 DEFAULT is the default value for the property.
164 BEHAVIOUR determine how Org should handle multiple keywords for
165 the same property. It is a symbol among:
166 nil Keep old value and discard the new one.
167 t Replace old value with the new one.
168 `space' Concatenate the values, separating them with a space.
169 `newline' Concatenate the values, separating them with
171 `split' Split values at white spaces, and cons them to the
174 KEYWORD and OPTION have precedence over DEFAULT.
176 All these properties should be back-end agnostic. Back-end
177 specific properties are set through `org-export-define-backend'.
178 Properties redefined there have precedence over these.")
180 (defconst org-export-special-keywords
181 '("SETUP_FILE" "OPTIONS" "MACRO")
182 "List of in-buffer keywords that require special treatment.
183 These keywords are not directly associated to a property. The
184 way they are handled must be hard-coded into
185 `org-export--get-inbuffer-options' function.")
187 (defconst org-export-filters-alist
188 '((:filter-bold . org-export-filter-bold-functions
)
189 (:filter-babel-call . org-export-filter-babel-call-functions
)
190 (:filter-center-block . org-export-filter-center-block-functions
)
191 (:filter-clock . org-export-filter-clock-functions
)
192 (:filter-code . org-export-filter-code-functions
)
193 (:filter-comment . org-export-filter-comment-functions
)
194 (:filter-comment-block . org-export-filter-comment-block-functions
)
195 (:filter-drawer . org-export-filter-drawer-functions
)
196 (:filter-dynamic-block . org-export-filter-dynamic-block-functions
)
197 (:filter-entity . org-export-filter-entity-functions
)
198 (:filter-example-block . org-export-filter-example-block-functions
)
199 (:filter-export-block . org-export-filter-export-block-functions
)
200 (:filter-export-snippet . org-export-filter-export-snippet-functions
)
201 (:filter-final-output . org-export-filter-final-output-functions
)
202 (:filter-fixed-width . org-export-filter-fixed-width-functions
)
203 (:filter-footnote-definition . org-export-filter-footnote-definition-functions
)
204 (:filter-footnote-reference . org-export-filter-footnote-reference-functions
)
205 (:filter-headline . org-export-filter-headline-functions
)
206 (:filter-horizontal-rule . org-export-filter-horizontal-rule-functions
)
207 (:filter-inline-babel-call . org-export-filter-inline-babel-call-functions
)
208 (:filter-inline-src-block . org-export-filter-inline-src-block-functions
)
209 (:filter-inlinetask . org-export-filter-inlinetask-functions
)
210 (:filter-italic . org-export-filter-italic-functions
)
211 (:filter-item . org-export-filter-item-functions
)
212 (:filter-keyword . org-export-filter-keyword-functions
)
213 (:filter-latex-environment . org-export-filter-latex-environment-functions
)
214 (:filter-latex-fragment . org-export-filter-latex-fragment-functions
)
215 (:filter-line-break . org-export-filter-line-break-functions
)
216 (:filter-link . org-export-filter-link-functions
)
217 (:filter-macro . org-export-filter-macro-functions
)
218 (:filter-paragraph . org-export-filter-paragraph-functions
)
219 (:filter-parse-tree . org-export-filter-parse-tree-functions
)
220 (:filter-plain-list . org-export-filter-plain-list-functions
)
221 (:filter-plain-text . org-export-filter-plain-text-functions
)
222 (:filter-planning . org-export-filter-planning-functions
)
223 (:filter-property-drawer . org-export-filter-property-drawer-functions
)
224 (:filter-quote-block . org-export-filter-quote-block-functions
)
225 (:filter-quote-section . org-export-filter-quote-section-functions
)
226 (:filter-radio-target . org-export-filter-radio-target-functions
)
227 (:filter-section . org-export-filter-section-functions
)
228 (:filter-special-block . org-export-filter-special-block-functions
)
229 (:filter-src-block . org-export-filter-src-block-functions
)
230 (:filter-statistics-cookie . org-export-filter-statistics-cookie-functions
)
231 (:filter-strike-through . org-export-filter-strike-through-functions
)
232 (:filter-subscript . org-export-filter-subscript-functions
)
233 (:filter-superscript . org-export-filter-superscript-functions
)
234 (:filter-table . org-export-filter-table-functions
)
235 (:filter-table-cell . org-export-filter-table-cell-functions
)
236 (:filter-table-row . org-export-filter-table-row-functions
)
237 (:filter-target . org-export-filter-target-functions
)
238 (:filter-timestamp . org-export-filter-timestamp-functions
)
239 (:filter-underline . org-export-filter-underline-functions
)
240 (:filter-verbatim . org-export-filter-verbatim-functions
)
241 (:filter-verse-block . org-export-filter-verse-block-functions
))
242 "Alist between filters properties and initial values.
244 The key of each association is a property name accessible through
245 the communication channel. Its value is a configurable global
246 variable defining initial filters.
248 This list is meant to install user specified filters. Back-end
249 developers may install their own filters using
250 `org-export-define-backend'. Filters defined there will always
251 be prepended to the current list, so they always get applied
254 (defconst org-export-default-inline-image-rule
258 '("png" "jpeg" "jpg" "gif" "tiff" "tif" "xbm"
259 "xpm" "pbm" "pgm" "ppm") t
))))
260 "Default rule for link matching an inline image.
261 This rule applies to links with no description. By default, it
262 will be considered as an inline image if it targets a local file
263 whose extension is either \"png\", \"jpeg\", \"jpg\", \"gif\",
264 \"tiff\", \"tif\", \"xbm\", \"xpm\", \"pbm\", \"pgm\" or \"ppm\".
265 See `org-export-inline-image-p' for more information about
270 ;;; User-configurable Variables
272 ;; Configuration for the masses.
274 ;; They should never be accessed directly, as their value is to be
275 ;; stored in a property list (cf. `org-export-options-alist').
276 ;; Back-ends will read their value from there instead.
278 (defgroup org-export nil
279 "Options for exporting Org mode files."
283 (defgroup org-export-general nil
284 "General options for export engine."
285 :tag
"Org Export General"
288 (defcustom org-export-with-archived-trees
'headline
289 "Whether sub-trees with the ARCHIVE tag should be exported.
291 This can have three different values:
292 nil Do not export, pretend this tree is not present.
293 t Do export the entire tree.
294 `headline' Only export the headline, but skip the tree below it.
296 This option can also be set with the #+OPTIONS line,
298 :group
'org-export-general
300 (const :tag
"Not at all" nil
)
301 (const :tag
"Headline only" 'headline
)
302 (const :tag
"Entirely" t
)))
304 (defcustom org-export-with-author t
305 "Non-nil means insert author name into the exported file.
306 This option can also be set with the #+OPTIONS line,
307 e.g. \"author:nil\"."
308 :group
'org-export-general
311 (defcustom org-export-with-clocks nil
312 "Non-nil means export CLOCK keywords.
313 This option can also be set with the #+OPTIONS line,
315 :group
'org-export-general
318 (defcustom org-export-with-creator
'comment
319 "Non-nil means the postamble should contain a creator sentence.
321 The sentence can be set in `org-export-creator-string' and
322 defaults to \"Generated by Org mode XX in Emacs XXX.\".
324 If the value is `comment' insert it as a comment."
325 :group
'org-export-general
327 (const :tag
"No creator sentence" nil
)
328 (const :tag
"Sentence as a comment" 'comment
)
329 (const :tag
"Insert the sentence" t
)))
331 (defcustom org-export-creator-string
332 (format "Generated by Org mode %s in Emacs %s."
333 (if (fboundp 'org-version
) (org-version) "(Unknown)")
335 "String to insert at the end of the generated document."
336 :group
'org-export-general
337 :type
'(string :tag
"Creator string"))
339 (defcustom org-export-with-drawers t
340 "Non-nil means export contents of standard drawers.
342 When t, all drawers are exported. This may also be a list of
343 drawer names to export. This variable doesn't apply to
346 This option can also be set with the #+OPTIONS line,
348 :group
'org-export-general
350 (const :tag
"All drawers" t
)
351 (const :tag
"None" nil
)
352 (repeat :tag
"Selected drawers"
353 (string :tag
"Drawer name"))))
355 (defcustom org-export-with-email nil
356 "Non-nil means insert author email into the exported file.
357 This option can also be set with the #+OPTIONS line,
359 :group
'org-export-general
362 (defcustom org-export-with-emphasize t
363 "Non-nil means interpret *word*, /word/, and _word_ as emphasized text.
365 If the export target supports emphasizing text, the word will be
366 typeset in bold, italic, or underlined, respectively. Not all
367 export backends support this.
369 This option can also be set with the #+OPTIONS line, e.g. \"*:nil\"."
370 :group
'org-export-general
373 (defcustom org-export-exclude-tags
'("noexport")
374 "Tags that exclude a tree from export.
376 All trees carrying any of these tags will be excluded from
377 export. This is without condition, so even subtrees inside that
378 carry one of the `org-export-select-tags' will be removed.
380 This option can also be set with the #+EXCLUDE_TAGS: keyword."
381 :group
'org-export-general
382 :type
'(repeat (string :tag
"Tag")))
384 (defcustom org-export-with-fixed-width t
385 "Non-nil means lines starting with \":\" will be in fixed width font.
387 This can be used to have pre-formatted text, fragments of code
389 : ;; Some Lisp examples
392 will be looking just like this in also HTML. See also the QUOTE
393 keyword. Not all export backends support this.
395 This option can also be set with the #+OPTIONS line, e.g. \"::nil\"."
396 :group
'org-export-translation
399 (defcustom org-export-with-footnotes t
400 "Non-nil means Org footnotes should be exported.
401 This option can also be set with the #+OPTIONS line,
403 :group
'org-export-general
406 (defcustom org-export-headline-levels
3
407 "The last level which is still exported as a headline.
409 Inferior levels will produce itemize lists when exported.
411 This option can also be set with the #+OPTIONS line, e.g. \"H:2\"."
412 :group
'org-export-general
415 (defcustom org-export-default-language
"en"
416 "The default language for export and clocktable translations, as a string.
417 This may have an association in
418 `org-clock-clocktable-language-setup'."
419 :group
'org-export-general
420 :type
'(string :tag
"Language"))
422 (defcustom org-export-preserve-breaks nil
423 "Non-nil means preserve all line breaks when exporting.
425 Normally, in HTML output paragraphs will be reformatted.
427 This option can also be set with the #+OPTIONS line,
429 :group
'org-export-general
432 (defcustom org-export-with-entities t
433 "Non-nil means interpret entities when exporting.
435 For example, HTML export converts \\alpha to α and \\AA to
438 For a list of supported names, see the constant `org-entities'
439 and the user option `org-entities-user'.
441 This option can also be set with the #+OPTIONS line,
443 :group
'org-export-general
446 (defcustom org-export-with-inlinetasks t
447 "Non-nil means inlinetasks should be exported.
448 This option can also be set with the #+OPTIONS line,
449 e.g. \"inline:nil\"."
450 :group
'org-export-general
453 (defcustom org-export-with-planning nil
454 "Non-nil means include planning info in export.
455 This option can also be set with the #+OPTIONS: line,
457 :group
'org-export-general
460 (defcustom org-export-with-priority nil
461 "Non-nil means include priority cookies in export.
462 This option can also be set with the #+OPTIONS line,
464 :group
'org-export-general
467 (defcustom org-export-with-section-numbers t
468 "Non-nil means add section numbers to headlines when exporting.
470 When set to an integer n, numbering will only happen for
471 headlines whose relative level is higher or equal to n.
473 This option can also be set with the #+OPTIONS line,
475 :group
'org-export-general
478 (defcustom org-export-select-tags
'("export")
479 "Tags that select a tree for export.
481 If any such tag is found in a buffer, all trees that do not carry
482 one of these tags will be ignored during export. Inside trees
483 that are selected like this, you can still deselect a subtree by
484 tagging it with one of the `org-export-exclude-tags'.
486 This option can also be set with the #+SELECT_TAGS: keyword."
487 :group
'org-export-general
488 :type
'(repeat (string :tag
"Tag")))
490 (defcustom org-export-with-special-strings t
491 "Non-nil means interpret \"\-\", \"--\" and \"---\" for export.
493 When this option is turned on, these strings will be exported as:
496 -----+----------+--------
502 This option can also be set with the #+OPTIONS line,
504 :group
'org-export-general
507 (defcustom org-export-with-sub-superscripts t
508 "Non-nil means interpret \"_\" and \"^\" for export.
510 When this option is turned on, you can use TeX-like syntax for
511 sub- and superscripts. Several characters after \"_\" or \"^\"
512 will be considered as a single item - so grouping with {} is
513 normally not needed. For example, the following things will be
514 parsed as single sub- or superscripts.
516 10^24 or 10^tau several digits will be considered 1 item.
517 10^-12 or 10^-tau a leading sign with digits or a word
518 x^2-y^3 will be read as x^2 - y^3, because items are
519 terminated by almost any nonword/nondigit char.
520 x_{i^2} or x^(2-i) braces or parenthesis do grouping.
522 Still, ambiguity is possible - so when in doubt use {} to enclose
523 the sub/superscript. If you set this variable to the symbol
524 `{}', the braces are *required* in order to trigger
525 interpretations as sub/superscript. This can be helpful in
526 documents that need \"_\" frequently in plain text.
528 This option can also be set with the #+OPTIONS line,
530 :group
'org-export-general
532 (const :tag
"Interpret them" t
)
533 (const :tag
"Curly brackets only" {})
534 (const :tag
"Do not interpret them" nil
)))
536 (defcustom org-export-with-toc t
537 "Non-nil means create a table of contents in exported files.
539 The TOC contains headlines with levels up
540 to`org-export-headline-levels'. When an integer, include levels
541 up to N in the toc, this may then be different from
542 `org-export-headline-levels', but it will not be allowed to be
543 larger than the number of headline levels. When nil, no table of
546 This option can also be set with the #+OPTIONS line,
547 e.g. \"toc:nil\" or \"toc:3\"."
548 :group
'org-export-general
550 (const :tag
"No Table of Contents" nil
)
551 (const :tag
"Full Table of Contents" t
)
552 (integer :tag
"TOC to level")))
554 (defcustom org-export-with-tables t
555 "If non-nil, lines starting with \"|\" define a table.
558 | Name | Address | Birthday |
559 |-------------+----------+-----------|
560 | Arthur Dent | England | 29.2.2100 |
562 This option can also be set with the #+OPTIONS line, e.g. \"|:nil\"."
563 :group
'org-export-general
566 (defcustom org-export-with-tags t
567 "If nil, do not export tags, just remove them from headlines.
569 If this is the symbol `not-in-toc', tags will be removed from
570 table of contents entries, but still be shown in the headlines of
573 This option can also be set with the #+OPTIONS line,
575 :group
'org-export-general
577 (const :tag
"Off" nil
)
578 (const :tag
"Not in TOC" not-in-toc
)
579 (const :tag
"On" t
)))
581 (defcustom org-export-with-tasks t
582 "Non-nil means include TODO items for export.
583 This may have the following values:
584 t include tasks independent of state.
585 todo include only tasks that are not yet done.
586 done include only tasks that are already done.
587 nil remove all tasks before export
588 list of keywords keep only tasks with these keywords"
589 :group
'org-export-general
591 (const :tag
"All tasks" t
)
592 (const :tag
"No tasks" nil
)
593 (const :tag
"Not-done tasks" todo
)
594 (const :tag
"Only done tasks" done
)
595 (repeat :tag
"Specific TODO keywords"
596 (string :tag
"Keyword"))))
598 (defcustom org-export-time-stamp-file t
599 "Non-nil means insert a time stamp into the exported file.
600 The time stamp shows when the file was created.
602 This option can also be set with the #+OPTIONS line,
603 e.g. \"timestamp:nil\"."
604 :group
'org-export-general
607 (defcustom org-export-with-timestamps t
608 "Non nil means allow timestamps in export.
610 It can be set to `active', `inactive', t or nil, in order to
611 export, respectively, only active timestamps, only inactive ones,
614 This option can also be set with the #+OPTIONS line, e.g.
616 :group
'org-export-general
618 (const :tag
"All timestamps" t
)
619 (const :tag
"Only active timestamps" active
)
620 (const :tag
"Only inactive timestamps" inactive
)
621 (const :tag
"No timestamp" nil
)))
623 (defcustom org-export-with-todo-keywords t
624 "Non-nil means include TODO keywords in export.
625 When nil, remove all these keywords from the export."
626 :group
'org-export-general
629 (defcustom org-export-allow-BIND
'confirm
630 "Non-nil means allow #+BIND to define local variable values for export.
631 This is a potential security risk, which is why the user must
632 confirm the use of these lines."
633 :group
'org-export-general
635 (const :tag
"Never" nil
)
636 (const :tag
"Always" t
)
637 (const :tag
"Ask a confirmation for each file" confirm
)))
639 (defcustom org-export-snippet-translation-alist nil
640 "Alist between export snippets back-ends and exporter back-ends.
642 This variable allows to provide shortcuts for export snippets.
644 For example, with a value of '\(\(\"h\" . \"e-html\"\)\), the
645 HTML back-end will recognize the contents of \"@@h:<b>@@\" as
646 HTML code while every other back-end will ignore it."
647 :group
'org-export-general
650 (string :tag
"Shortcut")
651 (string :tag
"Back-end"))))
653 (defcustom org-export-coding-system nil
654 "Coding system for the exported file."
655 :group
'org-export-general
656 :type
'coding-system
)
658 (defcustom org-export-copy-to-kill-ring t
659 "Non-nil means exported stuff will also be pushed onto the kill ring."
660 :group
'org-export-general
663 (defcustom org-export-initial-scope
'buffer
664 "The initial scope when exporting with `org-export-dispatch'.
665 This variable can be either set to `buffer' or `subtree'."
666 :group
'org-export-general
668 (const :tag
"Export current buffer" 'buffer
)
669 (const :tag
"Export current subtree" 'subtree
)))
671 (defcustom org-export-show-temporary-export-buffer t
672 "Non-nil means show buffer after exporting to temp buffer.
673 When Org exports to a file, the buffer visiting that file is ever
674 shown, but remains buried. However, when exporting to
675 a temporary buffer, that buffer is popped up in a second window.
676 When this variable is nil, the buffer remains buried also in
678 :group
'org-export-general
681 (defcustom org-export-dispatch-use-expert-ui nil
682 "Non-nil means using a non-intrusive `org-export-dispatch'.
683 In that case, no help buffer is displayed. Though, an indicator
684 for current export scope is added to the prompt \(i.e. \"b\" when
685 output is restricted to body only, \"s\" when it is restricted to
686 the current subtree and \"v\" when only visible elements are
687 considered for export\). Also, \[?] allows to switch back to
689 :group
'org-export-general
694 ;;; Defining New Back-ends
696 (defmacro org-export-define-backend
(backend translators
&rest body
)
697 "Define a new back-end BACKEND.
699 TRANSLATORS is an alist between object or element types and
700 functions handling them.
702 These functions should return a string without any trailing
703 space, or nil. They must accept three arguments: the object or
704 element itself, its contents or nil when it isn't recursive and
705 the property list used as a communication channel.
707 Contents, when not nil, are stripped from any global indentation
708 \(although the relative one is preserved). They also always end
709 with a single newline character.
711 If, for a given type, no function is found, that element or
712 object type will simply be ignored, along with any blank line or
713 white space at its end. The same will happen if the function
714 returns the nil value. If that function returns the empty
715 string, the type will be ignored, but the blank lines or white
718 In addition to element and object types, one function can be
719 associated to the `template' symbol and another one to the
722 The former returns the final transcoded string, and can be used
723 to add a preamble and a postamble to document's body. It must
724 accept two arguments: the transcoded string and the property list
725 containing export options.
727 The latter, when defined, is to be called on every text not
728 recognized as an element or an object. It must accept two
729 arguments: the text string and the information channel. It is an
730 appropriate place to protect special chars relative to the
733 BODY can start with pre-defined keyword arguments. The following
734 keywords are understood:
738 String, or list of strings, representing block names that
739 will not be parsed. This is used to specify blocks that will
740 contain raw code specific to the back-end. These blocks
741 still have to be handled by the relative `export-block' type
746 Alist between filters and function, or list of functions,
747 specific to the back-end. See `org-export-filters-alist' for
748 a list of all allowed filters. Filters defined here
749 shouldn't make a back-end test, as it may prevent back-ends
750 derived from this one to behave properly.
754 Alist between back-end specific properties introduced in
755 communication channel and how their value are acquired. See
756 `org-export-options-alist' for more information about
757 structure of the values.
759 As an example, here is how the `e-ascii' back-end is defined:
761 \(org-export-define-backend e-ascii
762 \((bold . org-e-ascii-bold)
763 \(center-block . org-e-ascii-center-block)
764 \(clock . org-e-ascii-clock)
765 \(code . org-e-ascii-code)
766 \(drawer . org-e-ascii-drawer)
767 \(dynamic-block . org-e-ascii-dynamic-block)
768 \(entity . org-e-ascii-entity)
769 \(example-block . org-e-ascii-example-block)
770 \(export-block . org-e-ascii-export-block)
771 \(export-snippet . org-e-ascii-export-snippet)
772 \(fixed-width . org-e-ascii-fixed-width)
773 \(footnote-definition . org-e-ascii-footnote-definition)
774 \(footnote-reference . org-e-ascii-footnote-reference)
775 \(headline . org-e-ascii-headline)
776 \(horizontal-rule . org-e-ascii-horizontal-rule)
777 \(inline-src-block . org-e-ascii-inline-src-block)
778 \(inlinetask . org-e-ascii-inlinetask)
779 \(italic . org-e-ascii-italic)
780 \(item . org-e-ascii-item)
781 \(keyword . org-e-ascii-keyword)
782 \(latex-environment . org-e-ascii-latex-environment)
783 \(latex-fragment . org-e-ascii-latex-fragment)
784 \(line-break . org-e-ascii-line-break)
785 \(link . org-e-ascii-link)
786 \(macro . org-e-ascii-macro)
787 \(paragraph . org-e-ascii-paragraph)
788 \(plain-list . org-e-ascii-plain-list)
789 \(plain-text . org-e-ascii-plain-text)
790 \(planning . org-e-ascii-planning)
791 \(property-drawer . org-e-ascii-property-drawer)
792 \(quote-block . org-e-ascii-quote-block)
793 \(quote-section . org-e-ascii-quote-section)
794 \(radio-target . org-e-ascii-radio-target)
795 \(section . org-e-ascii-section)
796 \(special-block . org-e-ascii-special-block)
797 \(src-block . org-e-ascii-src-block)
798 \(statistics-cookie . org-e-ascii-statistics-cookie)
799 \(strike-through . org-e-ascii-strike-through)
800 \(subscript . org-e-ascii-subscript)
801 \(superscript . org-e-ascii-superscript)
802 \(table . org-e-ascii-table)
803 \(table-cell . org-e-ascii-table-cell)
804 \(table-row . org-e-ascii-table-row)
805 \(target . org-e-ascii-target)
806 \(template . org-e-ascii-template)
807 \(timestamp . org-e-ascii-timestamp)
808 \(underline . org-e-ascii-underline)
809 \(verbatim . org-e-ascii-verbatim)
810 \(verse-block . org-e-ascii-verse-block))
811 :export-block \"ASCII\"
812 :filters-alist ((:filter-headline . org-e-ascii-filter-headline-blank-lines)
813 \(:filter-section . org-e-ascii-filter-headline-blank-lines))
814 :options-alist ((:ascii-charset nil nil org-e-ascii-charset)))"
815 (declare (debug (&define name sexp
[&rest
[keywordp sexp
]] defbody
))
817 (let (filters options export-block
)
818 (while (keywordp (car body
))
820 (:export-block
(let ((names (pop body
)))
822 (if (consp names
) (mapcar 'upcase names
)
823 (list (upcase names
))))))
824 (:filters-alist
(setq filters
(pop body
)))
825 (:options-alist
(setq options
(pop body
)))
828 ;; Define translators.
829 (defvar ,(intern (format "org-%s-translate-alist" backend
)) ',translators
830 "Alist between element or object types and translators.")
833 `(defconst ,(intern (format "org-%s-options-alist" backend
)) ',options
834 ,(format "Alist between %s export properties and ways to set them.
835 See `org-export-options-alist' for more information on the
836 structure of the values."
840 `(defconst ,(intern (format "org-%s-filters-alist" backend
)) ',filters
841 "Alist between filters keywords and back-end specific filters.
842 See `org-export-filters-alist' for more information."))
843 ;; Tell parser to not parse EXPORT-BLOCK blocks.
847 (add-to-list 'org-element-block-name-alist
848 `(,name . org-element-export-block-parser
)))
850 ;; Splice in the body, if any.
853 (defmacro org-export-define-derived-backend
(child parent
&rest body
)
854 "Create a new back-end as a variant of an existing one.
856 CHILD is the name of the derived back-end. PARENT is the name of
859 BODY can start with pre-defined keyword arguments. The following
860 keywords are understood:
864 String, or list of strings, representing block names that
865 will not be parsed. This is used to specify blocks that will
866 contain raw code specific to the back-end. These blocks
867 still have to be handled by the relative `export-block' type
872 Alist of filters that will overwrite or complete filters
873 defined in PARENT back-end. See `org-export-filters-alist'
874 for more a list of allowed filters.
878 Alist of back-end specific properties that will overwrite or
879 complete those defined in PARENT back-end. Refer to
880 `org-export-options-alist' for more information about
881 structure of the values.
885 Alist of element and object types and transcoders that will
886 overwrite or complete transcode table from PARENT back-end.
887 Refer to `org-export-define-backend' for detailed information
890 As an example, here is how one could define \"my-latex\" back-end
891 as a variant of `e-latex' back-end with a custom template
894 \(org-export-define-derived-backend my-latex e-latex
895 :translate-alist ((template . my-latex-template-fun)))
897 The back-end could then be called with, for example:
899 \(org-export-to-buffer 'my-latex \"*Test my-latex*\")"
900 (declare (debug (&define name sexp
[&rest
[keywordp sexp
]] def-body
))
902 (let (filters options translate export-block
)
903 (while (keywordp (car body
))
905 (:export-block
(let ((names (pop body
)))
907 (if (consp names
) (mapcar 'upcase names
)
908 (list (upcase names
))))))
909 (:filters-alist
(setq filters
(pop body
)))
910 (:options-alist
(setq options
(pop body
)))
911 (:translate-alist
(setq translate
(pop body
)))
914 ;; Tell parser to not parse EXPORT-BLOCK blocks.
918 (add-to-list 'org-element-block-name-alist
919 `(,name . org-element-export-block-parser
)))
922 ,(let ((parent-filters (intern (format "org-%s-filters-alist" parent
))))
923 (when (or (boundp parent-filters
) filters
)
924 `(defconst ,(intern (format "org-%s-filters-alist" child
))
926 (and (boundp parent-filters
)
927 (copy-sequence (symbol-value parent-filters
))))
928 "Alist between filters keywords and back-end specific filters.
929 See `org-export-filters-alist' for more information.")))
931 ,(let ((parent-options (intern (format "org-%s-options-alist" parent
))))
932 (when (or (boundp parent-options
) options
)
933 `(defconst ,(intern (format "org-%s-options-alist" child
))
935 (and (boundp parent-options
)
936 (copy-sequence (symbol-value parent-options
))))
937 ,(format "Alist between %s export properties and ways to set them.
938 See `org-export-options-alist' for more information on the
939 structure of the values."
941 ;; Define translators.
942 (defvar ,(intern (format "org-%s-translate-alist" child
))
946 (intern (format "org-%s-translate-alist" parent
)))))
947 "Alist between element or object types and translators.")
948 ;; Splice in the body, if any.
953 ;;; The Communication Channel
955 ;; During export process, every function has access to a number of
956 ;; properties. They are of two types:
958 ;; 1. Environment options are collected once at the very beginning of
959 ;; the process, out of the original buffer and configuration.
960 ;; Collecting them is handled by `org-export-get-environment'
963 ;; Most environment options are defined through the
964 ;; `org-export-options-alist' variable.
966 ;; 2. Tree properties are extracted directly from the parsed tree,
967 ;; just before export, by `org-export-collect-tree-properties'.
969 ;; Here is the full list of properties available during transcode
970 ;; process, with their category (option, tree or local) and their
973 ;; + `:author' :: Author's name.
974 ;; - category :: option
977 ;; + `:back-end' :: Current back-end used for transcoding.
978 ;; - category :: tree
981 ;; + `:creator' :: String to write as creation information.
982 ;; - category :: option
985 ;; + `:date' :: String to use as date.
986 ;; - category :: option
989 ;; + `:description' :: Description text for the current data.
990 ;; - category :: option
993 ;; + `:email' :: Author's email.
994 ;; - category :: option
997 ;; + `:exclude-tags' :: Tags for exclusion of subtrees from export
999 ;; - category :: option
1000 ;; - type :: list of strings
1002 ;; + `:exported-data' :: Hash table used for memoizing
1003 ;; `org-export-data'.
1004 ;; - category :: tree
1005 ;; - type :: hash table
1007 ;; + `:footnote-definition-alist' :: Alist between footnote labels and
1008 ;; their definition, as parsed data. Only non-inlined footnotes
1009 ;; are represented in this alist. Also, every definition isn't
1010 ;; guaranteed to be referenced in the parse tree. The purpose of
1011 ;; this property is to preserve definitions from oblivion
1012 ;; (i.e. when the parse tree comes from a part of the original
1013 ;; buffer), it isn't meant for direct use in a back-end. To
1014 ;; retrieve a definition relative to a reference, use
1015 ;; `org-export-get-footnote-definition' instead.
1016 ;; - category :: option
1017 ;; - type :: alist (STRING . LIST)
1019 ;; + `:headline-levels' :: Maximum level being exported as an
1020 ;; headline. Comparison is done with the relative level of
1021 ;; headlines in the parse tree, not necessarily with their
1023 ;; - category :: option
1024 ;; - type :: integer
1026 ;; + `:headline-offset' :: Difference between relative and real level
1027 ;; of headlines in the parse tree. For example, a value of -1
1028 ;; means a level 2 headline should be considered as level
1029 ;; 1 (cf. `org-export-get-relative-level').
1030 ;; - category :: tree
1031 ;; - type :: integer
1033 ;; + `:headline-numbering' :: Alist between headlines and their
1034 ;; numbering, as a list of numbers
1035 ;; (cf. `org-export-get-headline-number').
1036 ;; - category :: tree
1037 ;; - type :: alist (INTEGER . LIST)
1039 ;; + `:id-alist' :: Alist between ID strings and destination file's
1040 ;; path, relative to current directory. It is used by
1041 ;; `org-export-resolve-id-link' to resolve ID links targeting an
1043 ;; - category :: option
1044 ;; - type :: alist (STRING . STRING)
1046 ;; + `:ignore-list' :: List of elements and objects that should be
1047 ;; ignored during export.
1048 ;; - category :: tree
1049 ;; - type :: list of elements and objects
1051 ;; + `:input-file' :: Full path to input file, if any.
1052 ;; - category :: option
1053 ;; - type :: string or nil
1055 ;; + `:keywords' :: List of keywords attached to data.
1056 ;; - category :: option
1059 ;; + `:language' :: Default language used for translations.
1060 ;; - category :: option
1063 ;; + `:parse-tree' :: Whole parse tree, available at any time during
1065 ;; - category :: option
1066 ;; - type :: list (as returned by `org-element-parse-buffer')
1068 ;; + `:preserve-breaks' :: Non-nil means transcoding should preserve
1070 ;; - category :: option
1071 ;; - type :: symbol (nil, t)
1073 ;; + `:section-numbers' :: Non-nil means transcoding should add
1074 ;; section numbers to headlines.
1075 ;; - category :: option
1076 ;; - type :: symbol (nil, t)
1078 ;; + `:select-tags' :: List of tags enforcing inclusion of sub-trees
1079 ;; in transcoding. When such a tag is present, subtrees without
1080 ;; it are de facto excluded from the process. See
1081 ;; `use-select-tags'.
1082 ;; - category :: option
1083 ;; - type :: list of strings
1085 ;; + `:target-list' :: List of targets encountered in the parse tree.
1086 ;; This is used to partly resolve "fuzzy" links
1087 ;; (cf. `org-export-resolve-fuzzy-link').
1088 ;; - category :: tree
1089 ;; - type :: list of strings
1091 ;; + `:time-stamp-file' :: Non-nil means transcoding should insert
1092 ;; a time stamp in the output.
1093 ;; - category :: option
1094 ;; - type :: symbol (nil, t)
1096 ;; + `:translate-alist' :: Alist between element and object types and
1097 ;; transcoding functions relative to the current back-end.
1098 ;; Special keys `template' and `plain-text' are also possible.
1099 ;; - category :: option
1100 ;; - type :: alist (SYMBOL . FUNCTION)
1102 ;; + `:with-archived-trees' :: Non-nil when archived subtrees should
1103 ;; also be transcoded. If it is set to the `headline' symbol,
1104 ;; only the archived headline's name is retained.
1105 ;; - category :: option
1106 ;; - type :: symbol (nil, t, `headline')
1108 ;; + `:with-author' :: Non-nil means author's name should be included
1110 ;; - category :: option
1111 ;; - type :: symbol (nil, t)
1113 ;; + `:with-clocks' :: Non-nild means clock keywords should be exported.
1114 ;; - category :: option
1115 ;; - type :: symbol (nil, t)
1117 ;; + `:with-creator' :: Non-nild means a creation sentence should be
1118 ;; inserted at the end of the transcoded string. If the value
1119 ;; is `comment', it should be commented.
1120 ;; - category :: option
1121 ;; - type :: symbol (`comment', nil, t)
1123 ;; + `:with-drawers' :: Non-nil means drawers should be exported. If
1124 ;; its value is a list of names, only drawers with such names
1125 ;; will be transcoded.
1126 ;; - category :: option
1127 ;; - type :: symbol (nil, t) or list of strings
1129 ;; + `:with-email' :: Non-nil means output should contain author's
1131 ;; - category :: option
1132 ;; - type :: symbol (nil, t)
1134 ;; + `:with-emphasize' :: Non-nil means emphasized text should be
1136 ;; - category :: option
1137 ;; - type :: symbol (nil, t)
1139 ;; + `:with-fixed-width' :: Non-nil if transcoder should interpret
1140 ;; strings starting with a colon as a fixed-with (verbatim) area.
1141 ;; - category :: option
1142 ;; - type :: symbol (nil, t)
1144 ;; + `:with-footnotes' :: Non-nil if transcoder should interpret
1146 ;; - category :: option
1147 ;; - type :: symbol (nil, t)
1149 ;; + `:with-plannings' :: Non-nil means transcoding should include
1151 ;; - category :: option
1152 ;; - type :: symbol (nil, t)
1154 ;; + `:with-priority' :: Non-nil means transcoding should include
1155 ;; priority cookies.
1156 ;; - category :: option
1157 ;; - type :: symbol (nil, t)
1159 ;; + `:with-special-strings' :: Non-nil means transcoding should
1160 ;; interpret special strings in plain text.
1161 ;; - category :: option
1162 ;; - type :: symbol (nil, t)
1164 ;; + `:with-sub-superscript' :: Non-nil means transcoding should
1165 ;; interpret subscript and superscript. With a value of "{}",
1166 ;; only interpret those using curly brackets.
1167 ;; - category :: option
1168 ;; - type :: symbol (nil, {}, t)
1170 ;; + `:with-tables' :: Non-nil means transcoding should interpret
1172 ;; - category :: option
1173 ;; - type :: symbol (nil, t)
1175 ;; + `:with-tags' :: Non-nil means transcoding should keep tags in
1176 ;; headlines. A `not-in-toc' value will remove them from the
1177 ;; table of contents, if any, nonetheless.
1178 ;; - category :: option
1179 ;; - type :: symbol (nil, t, `not-in-toc')
1181 ;; + `:with-tasks' :: Non-nil means transcoding should include
1182 ;; headlines with a TODO keyword. A `todo' value will only
1183 ;; include headlines with a todo type keyword while a `done'
1184 ;; value will do the contrary. If a list of strings is provided,
1185 ;; only tasks with keywords belonging to that list will be kept.
1186 ;; - category :: option
1187 ;; - type :: symbol (t, todo, done, nil) or list of strings
1189 ;; + `:with-timestamps' :: Non-nil means transcoding should include
1190 ;; time stamps. Special value `active' (resp. `inactive') ask to
1191 ;; export only active (resp. inactive) timestamps. Otherwise,
1192 ;; completely remove them.
1193 ;; - category :: option
1194 ;; - type :: symbol: (`active', `inactive', t, nil)
1196 ;; + `:with-toc' :: Non-nil means that a table of contents has to be
1197 ;; added to the output. An integer value limits its depth.
1198 ;; - category :: option
1199 ;; - type :: symbol (nil, t or integer)
1201 ;; + `:with-todo-keywords' :: Non-nil means transcoding should
1202 ;; include TODO keywords.
1203 ;; - category :: option
1204 ;; - type :: symbol (nil, t)
1207 ;;;; Environment Options
1209 ;; Environment options encompass all parameters defined outside the
1210 ;; scope of the parsed data. They come from five sources, in
1211 ;; increasing precedence order:
1213 ;; - Global variables,
1214 ;; - Buffer's attributes,
1215 ;; - Options keyword symbols,
1216 ;; - Buffer keywords,
1217 ;; - Subtree properties.
1219 ;; The central internal function with regards to environment options
1220 ;; is `org-export-get-environment'. It updates global variables with
1221 ;; "#+BIND:" keywords, then retrieve and prioritize properties from
1222 ;; the different sources.
1224 ;; The internal functions doing the retrieval are:
1225 ;; `org-export--get-global-options',
1226 ;; `org-export--get-buffer-attributes',
1227 ;; `org-export--parse-option-keyword',
1228 ;; `org-export--get-subtree-options' and
1229 ;; `org-export--get-inbuffer-options'
1231 ;; Also, `org-export--confirm-letbind' and `org-export--install-letbind'
1232 ;; take care of the part relative to "#+BIND:" keywords.
1234 (defun org-export-get-environment (&optional backend subtreep ext-plist
)
1235 "Collect export options from the current buffer.
1237 Optional argument BACKEND is a symbol specifying which back-end
1238 specific options to read, if any.
1240 When optional argument SUBTREEP is non-nil, assume the export is
1241 done against the current sub-tree.
1243 Third optional argument EXT-PLIST is a property list with
1244 external parameters overriding Org default settings, but still
1245 inferior to file-local settings."
1246 ;; First install #+BIND variables.
1247 (org-export--install-letbind-maybe)
1248 ;; Get and prioritize export options...
1250 ;; ... from global variables...
1251 (org-export--get-global-options backend
)
1252 ;; ... from buffer's attributes...
1253 (org-export--get-buffer-attributes)
1254 ;; ... from an external property list...
1256 ;; ... from in-buffer settings...
1257 (org-export--get-inbuffer-options
1259 (and buffer-file-name
(org-remove-double-quotes buffer-file-name
)))
1260 ;; ... and from subtree, when appropriate.
1261 (and subtreep
(org-export--get-subtree-options backend
))
1262 ;; Eventually install back-end symbol and its translation table.
1266 ,(let ((trans-alist (intern (format "org-%s-translate-alist" backend
))))
1267 (when (boundp trans-alist
) (symbol-value trans-alist
))))))
1269 (defun org-export--parse-option-keyword (options &optional backend
)
1270 "Parse an OPTIONS line and return values as a plist.
1271 Optional argument BACKEND is a symbol specifying which back-end
1272 specific items to read, if any."
1274 (append org-export-options-alist
1277 (format "org-%s-options-alist" backend
))))
1278 (and (boundp var
) (eval var
))))))
1279 ;; Build an alist between #+OPTION: item and property-name.
1282 (when (nth 2 e
) (cons (regexp-quote (nth 2 e
))
1287 (when (string-match (concat "\\(\\`\\|[ \t]\\)"
1289 ":\\(([^)\n]+)\\|[^ \t\n\r;,.]*\\)")
1291 (setq plist
(plist-put plist
1293 (car (read-from-string
1294 (match-string 2 options
)))))))
1298 (defun org-export--get-subtree-options (&optional backend
)
1299 "Get export options in subtree at point.
1300 Optional argument BACKEND is a symbol specifying back-end used
1301 for export. Return options as a plist."
1302 ;; For each buffer keyword, create an headline property setting the
1303 ;; same property in communication channel. The name for the property
1304 ;; is the keyword with "EXPORT_" appended to it.
1305 (org-with-wide-buffer
1307 ;; Make sure point is at an heading.
1308 (unless (org-at-heading-p) (org-back-to-heading t
))
1309 ;; Take care of EXPORT_TITLE. If it isn't defined, use headline's
1310 ;; title as its fallback value.
1311 (when (setq prop
(progn (looking-at org-todo-line-regexp
)
1312 (or (save-match-data
1313 (org-entry-get (point) "EXPORT_TITLE"))
1314 (org-match-string-no-properties 3))))
1318 (org-element-parse-secondary-string
1319 prop
(org-element-restriction 'keyword
)))))
1320 ;; EXPORT_OPTIONS are parsed in a non-standard way.
1321 (when (setq prop
(org-entry-get (point) "EXPORT_OPTIONS"))
1323 (nconc plist
(org-export--parse-option-keyword prop backend
))))
1324 ;; Handle other keywords.
1325 (let ((seen '("TITLE")))
1328 (let ((property (nth 1 option
)))
1329 (when (and property
(not (member property seen
)))
1330 (let* ((subtree-prop (concat "EXPORT_" property
))
1331 ;; Export properties are not case-sensitive.
1332 (value (let ((case-fold-search t
))
1333 (org-entry-get (point) subtree-prop
))))
1334 (push property seen
)
1340 ;; Parse VALUE if required.
1341 (if (member property org-element-parsed-keywords
)
1342 (org-element-parse-secondary-string
1343 value
(org-element-restriction 'keyword
))
1345 ;; Also look for both general keywords and back-end specific
1346 ;; options if BACKEND is provided.
1347 (append (and backend
1349 (format "org-%s-options-alist" backend
))))
1350 (and (boundp var
) (symbol-value var
))))
1351 org-export-options-alist
)))
1355 (defun org-export--get-inbuffer-options (&optional backend files
)
1356 "Return current buffer export options, as a plist.
1358 Optional argument BACKEND, when non-nil, is a symbol specifying
1359 which back-end specific options should also be read in the
1362 Optional argument FILES is a list of setup files names read so
1363 far, used to avoid circular dependencies.
1365 Assume buffer is in Org mode. Narrowing, if any, is ignored."
1366 (org-with-wide-buffer
1367 (goto-char (point-min))
1368 (let ((case-fold-search t
) plist
)
1369 ;; 1. Special keywords, as in `org-export-special-keywords'.
1370 (let ((special-re (org-make-options-regexp org-export-special-keywords
)))
1371 (while (re-search-forward special-re nil t
)
1372 (let ((element (org-element-at-point)))
1373 (when (eq (org-element-type element
) 'keyword
)
1374 (let* ((key (org-element-property :key element
))
1375 (val (org-element-property :value element
))
1378 ((string= key
"SETUP_FILE")
1381 (org-remove-double-quotes (org-trim val
)))))
1382 ;; Avoid circular dependencies.
1383 (unless (member file files
)
1385 (insert (org-file-contents file
'noerror
))
1387 (org-export--get-inbuffer-options
1388 backend
(cons file files
))))))
1389 ((string= key
"OPTIONS")
1390 (org-export--parse-option-keyword val backend
))
1391 ((string= key
"MACRO")
1393 "^\\([-a-zA-Z0-9_]+\\)\\(?:[ \t]+\\(.*?\\)[ \t]*$\\)?"
1398 (downcase (match-string 1 val
)))))
1399 (value (org-match-string-no-properties 2 val
)))
1402 ;; Value will be evaled: do not parse it.
1403 ((string-match "\\`(eval\\>" value
)
1404 (list key
(list value
)))
1405 ;; Value has to be parsed for nested
1410 (let ((restr (org-element-restriction 'macro
)))
1411 (org-element-parse-secondary-string
1412 ;; If user explicitly asks for
1413 ;; a newline, be sure to preserve it
1414 ;; from further filling with
1415 ;; `hard-newline'. Also replace
1416 ;; "\\n" with "\n", "\\\n" with "\\n"
1418 (replace-regexp-in-string
1419 "\\(\\\\\\\\\\)n" "\\\\"
1420 (replace-regexp-in-string
1421 "\\(?:^\\|[^\\\\]\\)\\(\\\\n\\)"
1422 hard-newline value nil nil
1)
1425 (setq plist
(org-combine-plists plist prop
)))))))
1426 ;; 2. Standard options, as in `org-export-options-alist'.
1427 (let* ((all (append org-export-options-alist
1428 ;; Also look for back-end specific options
1429 ;; if BACKEND is defined.
1433 (format "org-%s-options-alist" backend
))))
1434 (and (boundp var
) (eval var
))))))
1435 ;; Build alist between keyword name and property name.
1438 (lambda (e) (when (nth 1 e
) (cons (nth 1 e
) (car e
))))
1440 ;; Build regexp matching all keywords associated to export
1441 ;; options. Note: the search is case insensitive.
1442 (opt-re (org-make-options-regexp
1443 (delq nil
(mapcar (lambda (e) (nth 1 e
)) all
)))))
1444 (goto-char (point-min))
1445 (while (re-search-forward opt-re nil t
)
1446 (let ((element (org-element-at-point)))
1447 (when (eq (org-element-type element
) 'keyword
)
1448 (let* ((key (org-element-property :key element
))
1449 (val (org-element-property :value element
))
1450 (prop (cdr (assoc key alist
)))
1451 (behaviour (nth 4 (assq prop all
))))
1455 ;; Handle value depending on specified BEHAVIOUR.
1458 (if (not (plist-get plist prop
)) (org-trim val
)
1459 (concat (plist-get plist prop
) " " (org-trim val
))))
1462 (concat (plist-get plist prop
) "\n" (org-trim val
))))
1464 `(,@(plist-get plist prop
) ,@(org-split-string val
)))
1466 (otherwise (if (not (plist-member plist prop
)) val
1467 (plist-get plist prop
))))))))))
1468 ;; Parse keywords specified in `org-element-parsed-keywords'.
1471 (let* ((prop (cdr (assoc key alist
)))
1472 (value (and prop
(plist-get plist prop
))))
1473 (when (stringp value
)
1477 (org-element-parse-secondary-string
1478 value
(org-element-restriction 'keyword
)))))))
1479 org-element-parsed-keywords
))
1480 ;; 3. Return final value.
1483 (defun org-export--get-buffer-attributes ()
1484 "Return properties related to buffer attributes, as a plist."
1485 (let ((visited-file (buffer-file-name (buffer-base-buffer))))
1487 ;; Store full path of input file name, or nil. For internal use.
1488 :input-file visited-file
1489 :title
(or (and visited-file
1490 (file-name-sans-extension
1491 (file-name-nondirectory visited-file
)))
1492 (buffer-name (buffer-base-buffer)))
1493 :footnote-definition-alist
1494 ;; Footnotes definitions must be collected in the original
1495 ;; buffer, as there's no insurance that they will still be in the
1496 ;; parse tree, due to possible narrowing.
1498 (org-with-wide-buffer
1499 (goto-char (point-min))
1500 (while (re-search-forward org-footnote-definition-re nil t
)
1501 (let ((def (org-footnote-at-definition-p)))
1503 (org-skip-whitespace)
1504 (push (cons (car def
)
1506 (narrow-to-region (point) (nth 2 def
))
1507 ;; Like `org-element-parse-buffer', but
1508 ;; makes sure the definition doesn't start
1509 ;; with a section element.
1510 (org-element--parse-elements
1511 (point-min) (point-max) nil nil nil nil
1512 (list 'org-data nil
))))
1516 ;; Collect id references.
1518 (org-with-wide-buffer
1519 (goto-char (point-min))
1520 (while (re-search-forward
1521 "\\[\\[id:\\(\\S-+?\\)\\]\\(?:\\[.*?\\]\\)?\\]" nil t
)
1522 (let* ((id (org-match-string-no-properties 1))
1523 (file (org-id-find-id-file id
)))
1524 (when file
(push (cons id
(file-relative-name file
)) alist
)))))
1526 :macro-modification-time
1528 (file-exists-p visited-file
)
1529 (concat "(eval (format-time-string \"$1\" '"
1530 (prin1-to-string (nth 5 (file-attributes visited-file
)))
1532 ;; Store input file name as a macro.
1533 :macro-input-file
(and visited-file
(file-name-nondirectory visited-file
))
1534 ;; `:macro-date', `:macro-time' and `:macro-property' could as
1535 ;; well be initialized as tree properties, since they don't
1536 ;; depend on buffer properties. Though, it may be more logical
1537 ;; to keep them close to other ":macro-" properties.
1538 :macro-date
"(eval (format-time-string \"$1\"))"
1539 :macro-time
"(eval (format-time-string \"$1\"))"
1540 :macro-property
"(eval (org-entry-get nil \"$1\" 'selective))")))
1542 (defun org-export--get-global-options (&optional backend
)
1543 "Return global export options as a plist.
1545 Optional argument BACKEND, if non-nil, is a symbol specifying
1546 which back-end specific export options should also be read in the
1548 (let ((all (append org-export-options-alist
1551 (format "org-%s-options-alist" backend
))))
1552 (and (boundp var
) (symbol-value var
))))))
1561 ;; Eval default value provided. If keyword is a member
1562 ;; of `org-element-parsed-keywords', parse it as
1563 ;; a secondary string before storing it.
1564 (let ((value (eval (nth 3 cell
))))
1565 (if (not (stringp value
)) value
1566 (let ((keyword (nth 1 cell
)))
1567 (if (not (member keyword org-element-parsed-keywords
)) value
1568 (org-element-parse-secondary-string
1569 value
(org-element-restriction 'keyword
)))))))))
1574 (defvar org-export--allow-BIND-local nil
)
1575 (defun org-export--confirm-letbind ()
1576 "Can we use #+BIND values during export?
1577 By default this will ask for confirmation by the user, to divert
1578 possible security risks."
1580 ((not org-export-allow-BIND
) nil
)
1581 ((eq org-export-allow-BIND t
) t
)
1582 ((local-variable-p 'org-export--allow-BIND-local
)
1583 org-export--allow-BIND-local
)
1584 (t (org-set-local 'org-export--allow-BIND-local
1585 (yes-or-no-p "Allow BIND values in this buffer? ")))))
1587 (defun org-export--install-letbind-maybe ()
1588 "Install the values from #+BIND lines as local variables.
1589 Variables must be installed before in-buffer options are
1592 (org-with-wide-buffer
1593 (goto-char (point-min))
1594 (while (re-search-forward (org-make-options-regexp '("BIND")) nil t
)
1595 (when (org-export-confirm-letbind)
1596 (push (read (concat "(" (org-match-string-no-properties 2) ")"))
1598 (while (setq pair
(pop letbind
))
1599 (org-set-local (car pair
) (nth 1 pair
)))))
1602 ;;;; Tree Properties
1604 ;; Tree properties are infromation extracted from parse tree. They
1605 ;; are initialized at the beginning of the transcoding process by
1606 ;; `org-export-collect-tree-properties'.
1608 ;; Dedicated functions focus on computing the value of specific tree
1609 ;; properties during initialization. Thus,
1610 ;; `org-export--populate-ignore-list' lists elements and objects that
1611 ;; should be skipped during export, `org-export--get-min-level' gets
1612 ;; the minimal exportable level, used as a basis to compute relative
1613 ;; level for headlines. Eventually
1614 ;; `org-export--collect-headline-numbering' builds an alist between
1615 ;; headlines and their numbering.
1617 (defun org-export-collect-tree-properties (data info
)
1618 "Extract tree properties from parse tree.
1620 DATA is the parse tree from which information is retrieved. INFO
1621 is a list holding export options.
1623 Following tree properties are set or updated:
1625 `:exported-data' Hash table used to memoize results from
1628 `:footnote-definition-alist' List of footnotes definitions in
1629 original buffer and current parse tree.
1631 `:headline-offset' Offset between true level of headlines and
1632 local level. An offset of -1 means an headline
1633 of level 2 should be considered as a level
1634 1 headline in the context.
1636 `:headline-numbering' Alist of all headlines as key an the
1637 associated numbering as value.
1639 `:ignore-list' List of elements that should be ignored during
1642 `:target-list' List of all targets in the parse tree.
1644 Return updated plist."
1645 ;; Install the parse tree in the communication channel, in order to
1646 ;; use `org-export-get-genealogy' and al.
1647 (setq info
(plist-put info
:parse-tree data
))
1648 ;; Get the list of elements and objects to ignore, and put it into
1649 ;; `:ignore-list'. Do not overwrite any user ignore that might have
1650 ;; been done during parse tree filtering.
1654 (append (org-export--populate-ignore-list data info
)
1655 (plist-get info
:ignore-list
))))
1656 ;; Compute `:headline-offset' in order to be able to use
1657 ;; `org-export-get-relative-level'.
1661 (- 1 (org-export--get-min-level data info
))))
1662 ;; Update footnotes definitions list with definitions in parse tree.
1663 ;; This is required since buffer expansion might have modified
1664 ;; boundaries of footnote definitions contained in the parse tree.
1665 ;; This way, definitions in `footnote-definition-alist' are bound to
1666 ;; match those in the parse tree.
1667 (let ((defs (plist-get info
:footnote-definition-alist
)))
1669 data
'footnote-definition
1671 (push (cons (org-element-property :label fn
)
1672 `(org-data nil
,@(org-element-contents fn
)))
1674 (setq info
(plist-put info
:footnote-definition-alist defs
)))
1675 ;; Properties order doesn't matter: get the rest of the tree
1680 data
'(keyword target
)
1682 (when (or (eq (org-element-type blob
) 'target
)
1683 (string= (org-element-property :key blob
) "TARGET"))
1685 :headline-numbering
,(org-export--collect-headline-numbering data info
)
1686 :exported-data
,(make-hash-table :test
'eq
:size
4001))
1689 (defun org-export--get-min-level (data options
)
1690 "Return minimum exportable headline's level in DATA.
1691 DATA is parsed tree as returned by `org-element-parse-buffer'.
1692 OPTIONS is a plist holding export options."
1694 (let ((min-level 10000))
1697 (when (and (eq (org-element-type blob
) 'headline
)
1698 (not (memq blob
(plist-get options
:ignore-list
))))
1700 (min (org-element-property :level blob
) min-level
)))
1701 (when (= min-level
1) (throw 'exit
1)))
1702 (org-element-contents data
))
1703 ;; If no headline was found, for the sake of consistency, set
1704 ;; minimum level to 1 nonetheless.
1705 (if (= min-level
10000) 1 min-level
))))
1707 (defun org-export--collect-headline-numbering (data options
)
1708 "Return numbering of all exportable headlines in a parse tree.
1710 DATA is the parse tree. OPTIONS is the plist holding export
1713 Return an alist whose key is an headline and value is its
1714 associated numbering \(in the shape of a list of numbers\)."
1715 (let ((numbering (make-vector org-export-max-depth
0)))
1720 (let ((relative-level
1721 (1- (org-export-get-relative-level headline options
))))
1724 (loop for n across numbering
1725 for idx from
0 to org-export-max-depth
1726 when
(< idx relative-level
) collect n
1727 when
(= idx relative-level
) collect
(aset numbering idx
(1+ n
))
1728 when
(> idx relative-level
) do
(aset numbering idx
0)))))
1731 (defun org-export--populate-ignore-list (data options
)
1732 "Return list of elements and objects to ignore during export.
1733 DATA is the parse tree to traverse. OPTIONS is the plist holding
1736 walk-data
; for byte-compiler.
1739 (lambda (data options selected
)
1740 ;; Collect ignored elements or objects into IGNORE-LIST.
1743 (if (org-export--skip-p el options selected
) (push el ignore
)
1744 (let ((type (org-element-type el
)))
1745 (if (and (eq (plist-get options
:with-archived-trees
)
1747 (eq (org-element-type el
) 'headline
)
1748 (org-element-property :archivedp el
))
1749 ;; If headline is archived but tree below has
1750 ;; to be skipped, add it to ignore list.
1751 (mapc (lambda (e) (push e ignore
))
1752 (org-element-contents el
))
1753 ;; Move into recursive objects/elements.
1754 (when (org-element-contents el
)
1755 (funcall walk-data el options selected
))))))
1756 (org-element-contents data
))))))
1757 ;; Main call. First find trees containing a select tag, if any.
1758 (funcall walk-data data options
(org-export--selected-trees data options
))
1762 (defun org-export--selected-trees (data info
)
1763 "Return list of headlines containing a select tag in their tree.
1764 DATA is parsed data as returned by `org-element-parse-buffer'.
1765 INFO is a plist holding export options."
1766 (let* (selected-trees
1767 walk-data
; for byte-compiler.
1770 (lambda (data genealogy
)
1771 (case (org-element-type data
)
1772 (org-data (mapc (lambda (el) (funcall walk-data el genealogy
))
1773 (org-element-contents data
)))
1775 (let ((tags (org-element-property :tags data
)))
1776 (if (loop for tag in
(plist-get info
:select-tags
)
1777 thereis
(member tag tags
))
1778 ;; When a select tag is found, mark full
1779 ;; genealogy and every headline within the tree
1781 (setq selected-trees
1784 (org-element-map data
'headline
'identity
)
1786 ;; Else, continue searching in tree, recursively.
1788 (lambda (el) (funcall walk-data el
(cons data genealogy
)))
1789 (org-element-contents data
))))))))))
1790 (funcall walk-data data nil
) selected-trees
))
1792 (defun org-export--skip-p (blob options selected
)
1793 "Non-nil when element or object BLOB should be skipped during export.
1794 OPTIONS is the plist holding export options. SELECTED, when
1795 non-nil, is a list of headlines belonging to a tree with a select
1797 (case (org-element-type blob
)
1800 (let ((with-tasks (plist-get options
:with-tasks
))
1801 (todo (org-element-property :todo-keyword blob
))
1802 (todo-type (org-element-property :todo-type blob
))
1803 (archived (plist-get options
:with-archived-trees
))
1804 (tags (org-element-property :tags blob
)))
1806 ;; Ignore subtrees with an exclude tag.
1807 (loop for k in
(plist-get options
:exclude-tags
)
1808 thereis
(member k tags
))
1809 ;; When a select tag is present in the buffer, ignore any tree
1811 (and selected
(not (memq blob selected
)))
1812 ;; Ignore commented sub-trees.
1813 (org-element-property :commentedp blob
)
1814 ;; Ignore archived subtrees if `:with-archived-trees' is nil.
1815 (and (not archived
) (org-element-property :archivedp blob
))
1816 ;; Ignore tasks, if specified by `:with-tasks' property.
1818 (or (not with-tasks
)
1819 (and (memq with-tasks
'(todo done
))
1820 (not (eq todo-type with-tasks
)))
1821 (and (consp with-tasks
) (not (member todo with-tasks
))))))))
1822 ;; Check inlinetask.
1823 (inlinetask (not (plist-get options
:with-inlinetasks
)))
1826 (case (plist-get options
:with-timestamps
)
1827 ;; No timestamp allowed.
1829 ;; Only active timestamps allowed and the current one isn't
1832 (not (memq (org-element-property :type blob
)
1833 '(active active-range
))))
1834 ;; Only inactive timestamps allowed and the current one isn't
1837 (not (memq (org-element-property :type blob
)
1838 '(inactive inactive-range
))))))
1841 (or (not (plist-get options
:with-drawers
))
1842 (and (consp (plist-get options
:with-drawers
))
1843 (not (member (org-element-property :drawer-name blob
)
1844 (plist-get options
:with-drawers
))))))
1846 (table-row (org-export-table-row-is-special-p blob options
))
1847 ;; Check table-cell.
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
))))
1853 (clock (not (plist-get options
:with-clocks
)))
1855 (planning (not (plist-get options
:with-plannings
)))))
1861 ;; `org-export-data' reads a parse tree (obtained with, i.e.
1862 ;; `org-element-parse-buffer') and transcodes it into a specified
1863 ;; back-end output. It takes care of filtering out elements or
1864 ;; objects according to export options and organizing the output blank
1865 ;; lines and white space are preserved. The function memoizes its
1866 ;; results, so it is cheap to call it within translators.
1868 ;; Internally, three functions handle the filtering of objects and
1869 ;; elements during the export. In particular,
1870 ;; `org-export-ignore-element' marks an element or object so future
1871 ;; parse tree traversals skip it, `org-export--interpret-p' tells which
1872 ;; elements or objects should be seen as real Org syntax and
1873 ;; `org-export-expand' transforms the others back into their original
1876 ;; `org-export-transcoder' is an accessor returning appropriate
1877 ;; translator function for a given element or object.
1879 (defun org-export-transcoder (blob info
)
1880 "Return appropriate transcoder for BLOB.
1881 INFO is a plist containing export directives."
1882 (let ((type (org-element-type blob
)))
1883 ;; Return contents only for complete parse trees.
1884 (if (eq type
'org-data
) (lambda (blob contents info
) contents
)
1885 (let ((transcoder (cdr (assq type
(plist-get info
:translate-alist
)))))
1886 (and (functionp transcoder
) transcoder
)))))
1888 (defun org-export-data (data info
)
1889 "Convert DATA into current back-end format.
1891 DATA is a parse tree, an element or an object or a secondary
1892 string. INFO is a plist holding export options.
1894 Return transcoded string."
1895 (let ((memo (gethash data
(plist-get info
:exported-data
) 'no-memo
)))
1896 (if (not (eq memo
'no-memo
)) memo
1897 (let* ((type (org-element-type data
))
1900 ;; Ignored element/object.
1901 ((memq data
(plist-get info
:ignore-list
)) nil
)
1903 ((eq type
'plain-text
)
1904 (org-export-filter-apply-functions
1905 (plist-get info
:filter-plain-text
)
1906 (let ((transcoder (org-export-transcoder data info
)))
1907 (if transcoder
(funcall transcoder data info
) data
))
1909 ;; Uninterpreted element/object: change it back to Org
1910 ;; syntax and export again resulting raw string.
1911 ((not (org-export--interpret-p data info
))
1915 (mapconcat (lambda (blob) (org-export-data blob info
))
1916 (org-element-contents data
)
1919 ;; Secondary string.
1921 (mapconcat (lambda (obj) (org-export-data obj info
)) data
""))
1922 ;; Element/Object without contents or, as a special case,
1923 ;; headline with archive tag and archived trees restricted
1925 ((or (not (org-element-contents data
))
1926 (and (eq type
'headline
)
1927 (eq (plist-get info
:with-archived-trees
) 'headline
)
1928 (org-element-property :archivedp data
)))
1929 (let ((transcoder (org-export-transcoder data info
)))
1930 (and (functionp transcoder
)
1931 (funcall transcoder data nil info
))))
1932 ;; Element/Object with contents.
1934 (let ((transcoder (org-export-transcoder data info
)))
1936 (let* ((greaterp (memq type org-element-greater-elements
))
1939 (memq type org-element-recursive-objects
)))
1942 (lambda (element) (org-export-data element info
))
1943 (org-element-contents
1944 (if (or greaterp objectp
) data
1945 ;; Elements directly containing objects
1946 ;; must have their indentation normalized
1948 (org-element-normalize-contents
1950 ;; When normalizing contents of the first
1951 ;; paragraph in an item or a footnote
1952 ;; definition, ignore first line's
1953 ;; indentation: there is none and it
1954 ;; might be misleading.
1955 (when (eq type
'paragraph
)
1956 (let ((parent (org-export-get-parent data
)))
1958 (eq (car (org-element-contents parent
))
1960 (memq (org-element-type parent
)
1961 '(footnote-definition item
))))))))
1963 (funcall transcoder data
1964 (if (not greaterp
) contents
1965 (org-element-normalize-string contents
))
1967 ;; Final result will be memoized before being returned.
1972 ((memq type
'(org-data plain-text nil
)) results
)
1973 ;; Append the same white space between elements or objects as in
1974 ;; the original buffer, and call appropriate filters.
1977 (org-export-filter-apply-functions
1978 (plist-get info
(intern (format ":filter-%s" type
)))
1979 (let ((post-blank (or (org-element-property :post-blank data
)
1981 (if (memq type org-element-all-elements
)
1982 (concat (org-element-normalize-string results
)
1983 (make-string post-blank ?
\n))
1984 (concat results
(make-string post-blank ?
))))
1987 (plist-get info
:exported-data
))))))
1989 (defun org-export--interpret-p (blob info
)
1990 "Non-nil if element or object BLOB should be interpreted as Org syntax.
1991 Check is done according to export options INFO, stored as
1993 (case (org-element-type blob
)
1995 (entity (plist-get info
:with-entities
))
1997 (emphasis (plist-get info
:with-emphasize
))
1998 ;; ... fixed-width areas.
1999 (fixed-width (plist-get info
:with-fixed-width
))
2001 ((footnote-definition footnote-reference
)
2002 (plist-get info
:with-footnotes
))
2003 ;; ... sub/superscripts...
2004 ((subscript superscript
)
2005 (let ((sub/super-p
(plist-get info
:with-sub-superscript
)))
2006 (if (eq sub
/super-p
'{})
2007 (org-element-property :use-brackets-p blob
)
2010 (table (plist-get info
:with-tables
))
2013 (defun org-export-expand (blob contents
)
2014 "Expand a parsed element or object to its original state.
2015 BLOB is either an element or an object. CONTENTS is its
2016 contents, as a string or nil."
2018 (intern (format "org-element-%s-interpreter" (org-element-type blob
)))
2021 (defun org-export-ignore-element (element info
)
2022 "Add ELEMENT to `:ignore-list' in INFO.
2024 Any element in `:ignore-list' will be skipped when using
2025 `org-element-map'. INFO is modified by side effects."
2026 (plist-put info
:ignore-list
(cons element
(plist-get info
:ignore-list
))))
2030 ;;; The Filter System
2032 ;; Filters allow end-users to tweak easily the transcoded output.
2033 ;; They are the functional counterpart of hooks, as every filter in
2034 ;; a set is applied to the return value of the previous one.
2036 ;; Every set is back-end agnostic. Although, a filter is always
2037 ;; called, in addition to the string it applies to, with the back-end
2038 ;; used as argument, so it's easy for the end-user to add back-end
2039 ;; specific filters in the set. The communication channel, as
2040 ;; a plist, is required as the third argument.
2042 ;; From the developer side, filters sets can be installed in the
2043 ;; process with the help of `org-export-define-backend', which
2044 ;; internally sets `org-BACKEND-filters-alist' variable. Each
2045 ;; association has a key among the following symbols and a function or
2046 ;; a list of functions as value.
2048 ;; - `:filter-parse-tree' applies directly on the complete parsed
2049 ;; tree. It's the only filters set that doesn't apply to a string.
2050 ;; Users can set it through `org-export-filter-parse-tree-functions'
2053 ;; - `:filter-final-output' applies to the final transcoded string.
2054 ;; Users can set it with `org-export-filter-final-output-functions'
2057 ;; - `:filter-plain-text' applies to any string not recognized as Org
2058 ;; syntax. `org-export-filter-plain-text-functions' allows users to
2061 ;; - `:filter-TYPE' applies on the string returned after an element or
2062 ;; object of type TYPE has been transcoded. An user can modify
2063 ;; `org-export-filter-TYPE-functions'
2065 ;; All filters sets are applied with
2066 ;; `org-export-filter-apply-functions' function. Filters in a set are
2067 ;; applied in a LIFO fashion. It allows developers to be sure that
2068 ;; their filters will be applied first.
2070 ;; Filters properties are installed in communication channel with
2071 ;; `org-export-install-filters' function.
2073 ;; Eventually, a hook (`org-export-before-parsing-hook') is run just
2074 ;; before parsing to allow for heavy structure modifications.
2077 ;;;; Before Parsing Hook
2079 (defvar org-export-before-parsing-hook nil
2080 "Hook run before parsing an export buffer.
2082 This is run after include keywords have been expanded and Babel
2083 code executed, on a copy of original buffer's area being
2084 exported. Visibility is the same as in the original one. Point
2085 is left at the beginning of the new one.
2087 Every function in this hook will be called with one argument: the
2088 back-end currently used, as a symbol.")
2091 ;;;; Special Filters
2093 (defvar org-export-filter-parse-tree-functions nil
2094 "List of functions applied to the parsed tree.
2095 Each filter is called with three arguments: the parse tree, as
2096 returned by `org-element-parse-buffer', the back-end, as
2097 a symbol, and the communication channel, as a plist. It must
2098 return the modified parse tree to transcode.")
2100 (defvar org-export-filter-final-output-functions nil
2101 "List of functions applied to the transcoded string.
2102 Each filter is called with three arguments: the full transcoded
2103 string, the back-end, as a symbol, and the communication channel,
2104 as a plist. It must return a string that will be used as the
2105 final export output.")
2107 (defvar org-export-filter-plain-text-functions nil
2108 "List of functions applied to plain text.
2109 Each filter is called with three arguments: a string which
2110 contains no Org syntax, the back-end, as a symbol, and the
2111 communication channel, as a plist. It must return a string or
2115 ;;;; Elements Filters
2117 (defvar org-export-filter-center-block-functions nil
2118 "List of functions applied to a transcoded center block.
2119 Each filter is called with three arguments: the transcoded data,
2120 as a string, the back-end, as a symbol, and the communication
2121 channel, as a plist. It must return a string or nil.")
2123 (defvar org-export-filter-clock-functions nil
2124 "List of functions applied to a transcoded clock.
2125 Each filter is called with three arguments: the transcoded data,
2126 as a string, the back-end, as a symbol, and the communication
2127 channel, as a plist. It must return a string or nil.")
2129 (defvar org-export-filter-drawer-functions nil
2130 "List of functions applied to a transcoded drawer.
2131 Each filter is called with three arguments: the transcoded data,
2132 as a string, the back-end, as a symbol, and the communication
2133 channel, as a plist. It must return a string or nil.")
2135 (defvar org-export-filter-dynamic-block-functions nil
2136 "List of functions applied to a transcoded dynamic-block.
2137 Each filter is called with three arguments: the transcoded data,
2138 as a string, the back-end, as a symbol, and the communication
2139 channel, as a plist. It must return a string or nil.")
2141 (defvar org-export-filter-headline-functions nil
2142 "List of functions applied to a transcoded headline.
2143 Each filter is called with three arguments: the transcoded data,
2144 as a string, the back-end, as a symbol, and the communication
2145 channel, as a plist. It must return a string or nil.")
2147 (defvar org-export-filter-inlinetask-functions nil
2148 "List of functions applied to a transcoded inlinetask.
2149 Each filter is called with three arguments: the transcoded data,
2150 as a string, the back-end, as a symbol, and the communication
2151 channel, as a plist. It must return a string or nil.")
2153 (defvar org-export-filter-plain-list-functions nil
2154 "List of functions applied to a transcoded plain-list.
2155 Each filter is called with three arguments: the transcoded data,
2156 as a string, the back-end, as a symbol, and the communication
2157 channel, as a plist. It must return a string or nil.")
2159 (defvar org-export-filter-item-functions nil
2160 "List of functions applied to a transcoded item.
2161 Each filter is called with three arguments: the transcoded data,
2162 as a string, the back-end, as a symbol, and the communication
2163 channel, as a plist. It must return a string or nil.")
2165 (defvar org-export-filter-comment-functions nil
2166 "List of functions applied to a transcoded comment.
2167 Each filter is called with three arguments: the transcoded data,
2168 as a string, the back-end, as a symbol, and the communication
2169 channel, as a plist. It must return a string or nil.")
2171 (defvar org-export-filter-comment-block-functions nil
2172 "List of functions applied to a transcoded comment-comment.
2173 Each filter is called with three arguments: the transcoded data,
2174 as a string, the back-end, as a symbol, and the communication
2175 channel, as a plist. It must return a string or nil.")
2177 (defvar org-export-filter-example-block-functions nil
2178 "List of functions applied to a transcoded example-block.
2179 Each filter is called with three arguments: the transcoded data,
2180 as a string, the back-end, as a symbol, and the communication
2181 channel, as a plist. It must return a string or nil.")
2183 (defvar org-export-filter-export-block-functions nil
2184 "List of functions applied to a transcoded export-block.
2185 Each filter is called with three arguments: the transcoded data,
2186 as a string, the back-end, as a symbol, and the communication
2187 channel, as a plist. It must return a string or nil.")
2189 (defvar org-export-filter-fixed-width-functions nil
2190 "List of functions applied to a transcoded fixed-width.
2191 Each filter is called with three arguments: the transcoded data,
2192 as a string, the back-end, as a symbol, and the communication
2193 channel, as a plist. It must return a string or nil.")
2195 (defvar org-export-filter-footnote-definition-functions nil
2196 "List of functions applied to a transcoded footnote-definition.
2197 Each filter is called with three arguments: the transcoded data,
2198 as a string, the back-end, as a symbol, and the communication
2199 channel, as a plist. It must return a string or nil.")
2201 (defvar org-export-filter-horizontal-rule-functions nil
2202 "List of functions applied to a transcoded horizontal-rule.
2203 Each filter is called with three arguments: the transcoded data,
2204 as a string, the back-end, as a symbol, and the communication
2205 channel, as a plist. It must return a string or nil.")
2207 (defvar org-export-filter-keyword-functions nil
2208 "List of functions applied to a transcoded keyword.
2209 Each filter is called with three arguments: the transcoded data,
2210 as a string, the back-end, as a symbol, and the communication
2211 channel, as a plist. It must return a string or nil.")
2213 (defvar org-export-filter-latex-environment-functions nil
2214 "List of functions applied to a transcoded latex-environment.
2215 Each filter is called with three arguments: the transcoded data,
2216 as a string, the back-end, as a symbol, and the communication
2217 channel, as a plist. It must return a string or nil.")
2219 (defvar org-export-filter-babel-call-functions nil
2220 "List of functions applied to a transcoded babel-call.
2221 Each filter is called with three arguments: the transcoded data,
2222 as a string, the back-end, as a symbol, and the communication
2223 channel, as a plist. It must return a string or nil.")
2225 (defvar org-export-filter-paragraph-functions nil
2226 "List of functions applied to a transcoded paragraph.
2227 Each filter is called with three arguments: the transcoded data,
2228 as a string, the back-end, as a symbol, and the communication
2229 channel, as a plist. It must return a string or nil.")
2231 (defvar org-export-filter-planning-functions nil
2232 "List of functions applied to a transcoded planning.
2233 Each filter is called with three arguments: the transcoded data,
2234 as a string, the back-end, as a symbol, and the communication
2235 channel, as a plist. It must return a string or nil.")
2237 (defvar org-export-filter-property-drawer-functions nil
2238 "List of functions applied to a transcoded property-drawer.
2239 Each filter is called with three arguments: the transcoded data,
2240 as a string, the back-end, as a symbol, and the communication
2241 channel, as a plist. It must return a string or nil.")
2243 (defvar org-export-filter-quote-block-functions nil
2244 "List of functions applied to a transcoded quote block.
2245 Each filter is called with three arguments: the transcoded quote
2246 data, as a string, the back-end, as a symbol, and the
2247 communication channel, as a plist. It must return a string or
2250 (defvar org-export-filter-quote-section-functions nil
2251 "List of functions applied to a transcoded quote-section.
2252 Each filter is called with three arguments: the transcoded data,
2253 as a string, the back-end, as a symbol, and the communication
2254 channel, as a plist. It must return a string or nil.")
2256 (defvar org-export-filter-section-functions nil
2257 "List of functions applied to a transcoded section.
2258 Each filter is called with three arguments: the transcoded data,
2259 as a string, the back-end, as a symbol, and the communication
2260 channel, as a plist. It must return a string or nil.")
2262 (defvar org-export-filter-special-block-functions nil
2263 "List of functions applied to a transcoded special block.
2264 Each filter is called with three arguments: the transcoded data,
2265 as a string, the back-end, as a symbol, and the communication
2266 channel, as a plist. It must return a string or nil.")
2268 (defvar org-export-filter-src-block-functions nil
2269 "List of functions applied to a transcoded src-block.
2270 Each filter is called with three arguments: the transcoded data,
2271 as a string, the back-end, as a symbol, and the communication
2272 channel, as a plist. It must return a string or nil.")
2274 (defvar org-export-filter-table-functions nil
2275 "List of functions applied to a transcoded table.
2276 Each filter is called with three arguments: the transcoded data,
2277 as a string, the back-end, as a symbol, and the communication
2278 channel, as a plist. It must return a string or nil.")
2280 (defvar org-export-filter-table-cell-functions nil
2281 "List of functions applied to a transcoded table-cell.
2282 Each filter is called with three arguments: the transcoded data,
2283 as a string, the back-end, as a symbol, and the communication
2284 channel, as a plist. It must return a string or nil.")
2286 (defvar org-export-filter-table-row-functions nil
2287 "List of functions applied to a transcoded table-row.
2288 Each filter is called with three arguments: the transcoded data,
2289 as a string, the back-end, as a symbol, and the communication
2290 channel, as a plist. It must return a string or nil.")
2292 (defvar org-export-filter-verse-block-functions nil
2293 "List of functions applied to a transcoded verse block.
2294 Each filter is called with three arguments: the transcoded data,
2295 as a string, the back-end, as a symbol, and the communication
2296 channel, as a plist. It must return a string or nil.")
2299 ;;;; Objects Filters
2301 (defvar org-export-filter-bold-functions nil
2302 "List of functions applied to transcoded bold text.
2303 Each filter is called with three arguments: the transcoded data,
2304 as a string, the back-end, as a symbol, and the communication
2305 channel, as a plist. It must return a string or nil.")
2307 (defvar org-export-filter-code-functions nil
2308 "List of functions applied to transcoded code text.
2309 Each filter is called with three arguments: the transcoded data,
2310 as a string, the back-end, as a symbol, and the communication
2311 channel, as a plist. It must return a string or nil.")
2313 (defvar org-export-filter-entity-functions nil
2314 "List of functions applied to a transcoded entity.
2315 Each filter is called with three arguments: the transcoded data,
2316 as a string, the back-end, as a symbol, and the communication
2317 channel, as a plist. It must return a string or nil.")
2319 (defvar org-export-filter-export-snippet-functions nil
2320 "List of functions applied to a transcoded export-snippet.
2321 Each filter is called with three arguments: the transcoded data,
2322 as a string, the back-end, as a symbol, and the communication
2323 channel, as a plist. It must return a string or nil.")
2325 (defvar org-export-filter-footnote-reference-functions nil
2326 "List of functions applied to a transcoded footnote-reference.
2327 Each filter is called with three arguments: the transcoded data,
2328 as a string, the back-end, as a symbol, and the communication
2329 channel, as a plist. It must return a string or nil.")
2331 (defvar org-export-filter-inline-babel-call-functions nil
2332 "List of functions applied to a transcoded inline-babel-call.
2333 Each filter is called with three arguments: the transcoded data,
2334 as a string, the back-end, as a symbol, and the communication
2335 channel, as a plist. It must return a string or nil.")
2337 (defvar org-export-filter-inline-src-block-functions nil
2338 "List of functions applied to a transcoded inline-src-block.
2339 Each filter is called with three arguments: the transcoded data,
2340 as a string, the back-end, as a symbol, and the communication
2341 channel, as a plist. It must return a string or nil.")
2343 (defvar org-export-filter-italic-functions nil
2344 "List of functions applied to transcoded italic text.
2345 Each filter is called with three arguments: the transcoded data,
2346 as a string, the back-end, as a symbol, and the communication
2347 channel, as a plist. It must return a string or nil.")
2349 (defvar org-export-filter-latex-fragment-functions nil
2350 "List of functions applied to a transcoded latex-fragment.
2351 Each filter is called with three arguments: the transcoded data,
2352 as a string, the back-end, as a symbol, and the communication
2353 channel, as a plist. It must return a string or nil.")
2355 (defvar org-export-filter-line-break-functions nil
2356 "List of functions applied to a transcoded line-break.
2357 Each filter is called with three arguments: the transcoded data,
2358 as a string, the back-end, as a symbol, and the communication
2359 channel, as a plist. It must return a string or nil.")
2361 (defvar org-export-filter-link-functions nil
2362 "List of functions applied to a transcoded link.
2363 Each filter is called with three arguments: the transcoded data,
2364 as a string, the back-end, as a symbol, and the communication
2365 channel, as a plist. It must return a string or nil.")
2367 (defvar org-export-filter-macro-functions nil
2368 "List of functions applied to a transcoded macro.
2369 Each filter is called with three arguments: the transcoded data,
2370 as a string, the back-end, as a symbol, and the communication
2371 channel, as a plist. It must return a string or nil.")
2373 (defvar org-export-filter-radio-target-functions nil
2374 "List of functions applied to a transcoded radio-target.
2375 Each filter is called with three arguments: the transcoded data,
2376 as a string, the back-end, as a symbol, and the communication
2377 channel, as a plist. It must return a string or nil.")
2379 (defvar org-export-filter-statistics-cookie-functions nil
2380 "List of functions applied to a transcoded statistics-cookie.
2381 Each filter is called with three arguments: the transcoded data,
2382 as a string, the back-end, as a symbol, and the communication
2383 channel, as a plist. It must return a string or nil.")
2385 (defvar org-export-filter-strike-through-functions nil
2386 "List of functions applied to transcoded strike-through text.
2387 Each filter is called with three arguments: the transcoded data,
2388 as a string, the back-end, as a symbol, and the communication
2389 channel, as a plist. It must return a string or nil.")
2391 (defvar org-export-filter-subscript-functions nil
2392 "List of functions applied to a transcoded subscript.
2393 Each filter is called with three arguments: the transcoded data,
2394 as a string, the back-end, as a symbol, and the communication
2395 channel, as a plist. It must return a string or nil.")
2397 (defvar org-export-filter-superscript-functions nil
2398 "List of functions applied to a transcoded superscript.
2399 Each filter is called with three arguments: the transcoded data,
2400 as a string, the back-end, as a symbol, and the communication
2401 channel, as a plist. It must return a string or nil.")
2403 (defvar org-export-filter-target-functions nil
2404 "List of functions applied to a transcoded target.
2405 Each filter is called with three arguments: the transcoded data,
2406 as a string, the back-end, as a symbol, and the communication
2407 channel, as a plist. It must return a string or nil.")
2409 (defvar org-export-filter-timestamp-functions nil
2410 "List of functions applied to a transcoded timestamp.
2411 Each filter is called with three arguments: the transcoded data,
2412 as a string, the back-end, as a symbol, and the communication
2413 channel, as a plist. It must return a string or nil.")
2415 (defvar org-export-filter-underline-functions nil
2416 "List of functions applied to transcoded underline text.
2417 Each filter is called with three arguments: the transcoded data,
2418 as a string, the back-end, as a symbol, and the communication
2419 channel, as a plist. It must return a string or nil.")
2421 (defvar org-export-filter-verbatim-functions nil
2422 "List of functions applied to transcoded verbatim text.
2423 Each filter is called with three arguments: the transcoded data,
2424 as a string, the back-end, as a symbol, and the communication
2425 channel, as a plist. It must return a string or nil.")
2430 ;; Internal function `org-export-install-filters' installs filters
2431 ;; hard-coded in back-ends (developer filters) and filters from global
2432 ;; variables (user filters) in the communication channel.
2434 ;; Internal function `org-export-filter-apply-functions' takes care
2435 ;; about applying each filter in order to a given data. It ignores
2436 ;; filters returning a nil value but stops whenever a filter returns
2439 (defun org-export-filter-apply-functions (filters value info
)
2440 "Call every function in FILTERS.
2442 Functions are called with arguments VALUE, current export
2443 back-end and INFO. A function returning a nil value will be
2444 skipped. If it returns the empty string, the process ends and
2447 Call is done in a LIFO fashion, to be sure that developer
2448 specified filters, if any, are called first."
2450 (dolist (filter filters value
)
2451 (let ((result (funcall filter value
(plist-get info
:back-end
) info
)))
2453 ((equal value
"") (throw 'exit nil
))
2454 (t (setq value result
)))))))
2456 (defun org-export-install-filters (info)
2457 "Install filters properties in communication channel.
2459 INFO is a plist containing the current communication channel.
2461 Return the updated communication channel."
2463 ;; Install user defined filters with `org-export-filters-alist'.
2465 (setq plist
(plist-put plist
(car p
) (eval (cdr p
)))))
2466 org-export-filters-alist
)
2467 ;; Prepend back-end specific filters to that list.
2468 (let ((back-end-filters (intern (format "org-%s-filters-alist"
2469 (plist-get info
:back-end
)))))
2470 (when (boundp back-end-filters
)
2472 ;; Single values get consed, lists are prepended.
2473 (let ((key (car p
)) (value (cdr p
)))
2478 (if (atom value
) (cons value
(plist-get plist key
))
2479 (append value
(plist-get plist key
))))))))
2480 (eval back-end-filters
))))
2481 ;; Return new communication channel.
2482 (org-combine-plists info plist
)))
2488 ;; This is the room for the main function, `org-export-as', along with
2489 ;; its derivatives, `org-export-to-buffer' and `org-export-to-file'.
2490 ;; They differ only by the way they output the resulting code.
2492 ;; `org-export-output-file-name' is an auxiliary function meant to be
2493 ;; used with `org-export-to-file'. With a given extension, it tries
2494 ;; to provide a canonical file name to write export output to.
2496 ;; Note that `org-export-as' doesn't really parse the current buffer,
2497 ;; but a copy of it (with the same buffer-local variables and
2498 ;; visibility), where include keywords are expanded and Babel blocks
2499 ;; are executed, if appropriate.
2500 ;; `org-export-with-current-buffer-copy' macro prepares that copy.
2502 ;; File inclusion is taken care of by
2503 ;; `org-export-expand-include-keyword' and
2504 ;; `org-export--prepare-file-contents'. Structure wise, including
2505 ;; a whole Org file in a buffer often makes little sense. For
2506 ;; example, if the file contains an headline and the include keyword
2507 ;; was within an item, the item should contain the headline. That's
2508 ;; why file inclusion should be done before any structure can be
2509 ;; associated to the file, that is before parsing.
2511 (defun org-export-as
2512 (backend &optional subtreep visible-only body-only ext-plist noexpand
)
2513 "Transcode current Org buffer into BACKEND code.
2515 If narrowing is active in the current buffer, only transcode its
2518 If a region is active, transcode that region.
2520 When optional argument SUBTREEP is non-nil, transcode the
2521 sub-tree at point, extracting information from the headline
2524 When optional argument VISIBLE-ONLY is non-nil, don't export
2525 contents of hidden elements.
2527 When optional argument BODY-ONLY is non-nil, only return body
2528 code, without preamble nor postamble.
2530 Optional argument EXT-PLIST, when provided, is a property list
2531 with external parameters overriding Org default settings, but
2532 still inferior to file-local settings.
2534 Optional argument NOEXPAND, when non-nil, prevents included files
2535 to be expanded and Babel code to be executed.
2537 Return code as a string."
2540 ;; Narrow buffer to an appropriate region or subtree for
2541 ;; parsing. If parsing subtree, be sure to remove main headline
2543 (cond ((org-region-active-p)
2544 (narrow-to-region (region-beginning) (region-end)))
2546 (org-narrow-to-subtree)
2547 (goto-char (point-min))
2549 (narrow-to-region (point) (point-max))))
2550 ;; 1. Get export environment from original buffer. Also install
2551 ;; user's and developer's filters.
2552 (let ((info (org-export-install-filters
2553 (org-export-get-environment backend subtreep ext-plist
)))
2554 ;; 2. Get parse tree. Buffer isn't parsed directly.
2555 ;; Instead, a temporary copy is created, where include
2556 ;; keywords are expanded and code blocks are evaluated.
2557 (tree (let ((buf (or (buffer-file-name (buffer-base-buffer))
2559 (org-export-with-current-buffer-copy
2561 (org-export-expand-include-keyword)
2562 ;; TODO: Setting `org-current-export-file' is
2563 ;; required by Org Babel to properly resolve
2564 ;; noweb references. Once "org-exp.el" is
2566 ;; `org-export-blocks-preprocess' so it accepts
2567 ;; the value as an argument instead.
2568 (let ((org-current-export-file buf
))
2569 (org-export-blocks-preprocess)))
2570 (goto-char (point-min))
2572 ;; `org-export-before-parsing-hook'. with current
2573 ;; back-end as argument.
2575 'org-export-before-parsing-hook backend
)
2576 ;; Eventually parse buffer.
2577 (org-element-parse-buffer nil visible-only
)))))
2578 ;; 3. Call parse-tree filters to get the final tree.
2580 (org-export-filter-apply-functions
2581 (plist-get info
:filter-parse-tree
) tree info
))
2582 ;; 4. Now tree is complete, compute its properties and add
2583 ;; them to communication channel.
2586 info
(org-export-collect-tree-properties tree info
)))
2587 ;; 5. Eventually transcode TREE. Wrap the resulting string
2588 ;; into a template, if required. Eventually call
2589 ;; final-output filter.
2590 (let* ((body (org-element-normalize-string (org-export-data tree info
)))
2591 (template (cdr (assq 'template
2592 (plist-get info
:translate-alist
))))
2593 (output (org-export-filter-apply-functions
2594 (plist-get info
:filter-final-output
)
2595 (if (or (not (functionp template
)) body-only
) body
2596 (funcall template body info
))
2598 ;; Maybe add final OUTPUT to kill ring, then return it.
2599 (when org-export-copy-to-kill-ring
(org-kill-new output
))
2602 (defun org-export-to-buffer
2603 (backend buffer
&optional subtreep visible-only body-only ext-plist noexpand
)
2604 "Call `org-export-as' with output to a specified buffer.
2606 BACKEND is the back-end used for transcoding, as a symbol.
2608 BUFFER is the output buffer. If it already exists, it will be
2609 erased first, otherwise, it will be created.
2611 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY, EXT-PLIST
2612 and NOEXPAND are similar to those used in `org-export-as', which
2616 (let ((out (org-export-as
2617 backend subtreep visible-only body-only ext-plist noexpand
))
2618 (buffer (get-buffer-create buffer
)))
2619 (with-current-buffer buffer
2622 (goto-char (point-min)))
2625 (defun org-export-to-file
2626 (backend file
&optional subtreep visible-only body-only ext-plist noexpand
)
2627 "Call `org-export-as' with output to a specified file.
2629 BACKEND is the back-end used for transcoding, as a symbol. FILE
2630 is the name of the output file, as a string.
2632 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY, EXT-PLIST
2633 and NOEXPAND are similar to those used in `org-export-as', which
2636 Return output file's name."
2637 ;; Checks for FILE permissions. `write-file' would do the same, but
2638 ;; we'd rather avoid needless transcoding of parse tree.
2639 (unless (file-writable-p file
) (error "Output file not writable"))
2640 ;; Insert contents to a temporary buffer and write it to FILE.
2641 (let ((out (org-export-as
2642 backend subtreep visible-only body-only ext-plist noexpand
)))
2645 (let ((coding-system-for-write org-export-coding-system
))
2646 (write-file file
))))
2647 ;; Return full path.
2650 (defun org-export-output-file-name (extension &optional subtreep pub-dir
)
2651 "Return output file's name according to buffer specifications.
2653 EXTENSION is a string representing the output file extension,
2654 with the leading dot.
2656 With a non-nil optional argument SUBTREEP, try to determine
2657 output file's name by looking for \"EXPORT_FILE_NAME\" property
2658 of subtree at point.
2660 When optional argument PUB-DIR is set, use it as the publishing
2663 When optional argument VISIBLE-ONLY is non-nil, don't export
2664 contents of hidden elements.
2666 Return file name as a string, or nil if it couldn't be
2669 ;; File name may come from EXPORT_FILE_NAME subtree property,
2670 ;; assuming point is at beginning of said sub-tree.
2671 (file-name-sans-extension
2675 (ignore-errors (org-back-to-heading) (point)))
2676 "EXPORT_FILE_NAME" t
))
2677 ;; File name may be extracted from buffer's associated
2679 (buffer-file-name (buffer-base-buffer))
2680 ;; Can't determine file name on our own: Ask user.
2681 (let ((read-file-name-function
2682 (and org-completion-use-ido
'ido-read-file-name
)))
2684 "Output file: " pub-dir nil nil nil
2686 (string= (file-name-extension name t
) extension
))))))))
2687 ;; Build file name. Enforce EXTENSION over whatever user may have
2688 ;; come up with. PUB-DIR, if defined, always has precedence over
2689 ;; any provided path.
2692 (concat (file-name-as-directory pub-dir
)
2693 (file-name-nondirectory base-name
)
2695 ((string= (file-name-nondirectory base-name
) base-name
)
2696 (concat (file-name-as-directory ".") base-name extension
))
2697 (t (concat base-name extension
)))))
2699 (defmacro org-export-with-current-buffer-copy
(&rest body
)
2700 "Apply BODY in a copy of the current buffer.
2702 The copy preserves local variables and visibility of the original
2705 Point is at buffer's beginning when BODY is applied."
2706 (org-with-gensyms (original-buffer offset buffer-string overlays
)
2707 `(let ((,original-buffer
(current-buffer))
2708 (,offset
(1- (point-min)))
2709 (,buffer-string
(buffer-string))
2711 'copy-overlay
(overlays-in (point-min) (point-max)))))
2713 (let ((buffer-invisibility-spec nil
))
2714 (org-clone-local-variables
2716 "^\\(org-\\|orgtbl-\\|major-mode$\\|outline-\\(regexp\\|level\\)$\\)")
2717 (insert ,buffer-string
)
2721 (- (overlay-start ov
) ,offset
)
2722 (- (overlay-end ov
) ,offset
)
2725 (goto-char (point-min))
2727 (def-edebug-spec org-export-with-current-buffer-copy
(body))
2729 (defun org-export-expand-include-keyword (&optional included dir
)
2730 "Expand every include keyword in buffer.
2731 Optional argument INCLUDED is a list of included file names along
2732 with their line restriction, when appropriate. It is used to
2733 avoid infinite recursion. Optional argument DIR is the current
2734 working directory. It is used to properly resolve relative
2736 (let ((case-fold-search t
))
2737 (goto-char (point-min))
2738 (while (re-search-forward "^[ \t]*#\\+INCLUDE: \\(.*\\)" nil t
)
2739 (when (eq (org-element-type (save-match-data (org-element-at-point)))
2742 ;; Extract arguments from keyword's value.
2743 (let* ((value (match-string 1))
2744 (ind (org-get-indentation))
2745 (file (and (string-match "^\"\\(\\S-+\\)\"" value
)
2746 (prog1 (expand-file-name (match-string 1 value
) dir
)
2747 (setq value
(replace-match "" nil nil value
)))))
2750 ":lines +\"\\(\\(?:[0-9]+\\)?-\\(?:[0-9]+\\)?\\)\"" value
)
2751 (prog1 (match-string 1 value
)
2752 (setq value
(replace-match "" nil nil value
)))))
2753 (env (cond ((string-match "\\<example\\>" value
) 'example
)
2754 ((string-match "\\<src\\(?: +\\(.*\\)\\)?" value
)
2755 (match-string 1 value
))))
2756 ;; Minimal level of included file defaults to the child
2757 ;; level of the current headline, if any, or one. It
2758 ;; only applies is the file is meant to be included as
2762 (if (string-match ":minlevel +\\([0-9]+\\)" value
)
2763 (prog1 (string-to-number (match-string 1 value
))
2764 (setq value
(replace-match "" nil nil value
)))
2765 (let ((cur (org-current-level)))
2766 (if cur
(1+ (org-reduced-level cur
)) 1))))))
2768 (delete-region (point) (progn (forward-line) (point)))
2770 ((not (file-readable-p file
)) (error "Cannot include file %s" file
))
2771 ;; Check if files has already been parsed. Look after
2772 ;; inclusion lines too, as different parts of the same file
2773 ;; can be included too.
2774 ((member (list file lines
) included
)
2775 (error "Recursive file inclusion: %s" file
))
2780 (let ((ind-str (make-string ind ?
))
2782 ;; Protect sensitive contents with commas.
2783 (replace-regexp-in-string
2784 "\\(^\\)\\([*]\\|[ \t]*#\\+\\)" ","
2785 (org-export--prepare-file-contents file lines
)
2787 (format "%s#+BEGIN_EXAMPLE\n%s%s#+END_EXAMPLE\n"
2788 ind-str contents ind-str
))))
2791 (let ((ind-str (make-string ind ?
))
2793 ;; Protect sensitive contents with commas.
2794 (replace-regexp-in-string
2795 (if (string= env
"org") "\\(^\\)\\(.\\)"
2796 "\\(^\\)\\([*]\\|[ \t]*#\\+\\)") ","
2797 (org-export--prepare-file-contents file lines
)
2799 (format "%s#+BEGIN_SRC %s\n%s%s#+END_SRC\n"
2800 ind-str env contents ind-str
))))
2806 (org-export--prepare-file-contents file lines ind minlevel
))
2807 (org-export-expand-include-keyword
2808 (cons (list file lines
) included
)
2809 (file-name-directory file
))
2810 (buffer-string))))))))))))
2812 (defun org-export--prepare-file-contents (file &optional lines ind minlevel
)
2813 "Prepare the contents of FILE for inclusion and return them as a string.
2815 When optional argument LINES is a string specifying a range of
2816 lines, include only those lines.
2818 Optional argument IND, when non-nil, is an integer specifying the
2819 global indentation of returned contents. Since its purpose is to
2820 allow an included file to stay in the same environment it was
2821 created \(i.e. a list item), it doesn't apply past the first
2822 headline encountered.
2824 Optional argument MINLEVEL, when non-nil, is an integer
2825 specifying the level that any top-level headline in the included
2828 (insert-file-contents file
)
2830 (let* ((lines (split-string lines
"-"))
2831 (lbeg (string-to-number (car lines
)))
2832 (lend (string-to-number (cadr lines
)))
2833 (beg (if (zerop lbeg
) (point-min)
2834 (goto-char (point-min))
2835 (forward-line (1- lbeg
))
2837 (end (if (zerop lend
) (point-max)
2838 (goto-char (point-min))
2839 (forward-line (1- lend
))
2841 (narrow-to-region beg end
)))
2842 ;; Remove blank lines at beginning and end of contents. The logic
2843 ;; behind that removal is that blank lines around include keyword
2844 ;; override blank lines in included file.
2845 (goto-char (point-min))
2846 (org-skip-whitespace)
2848 (delete-region (point-min) (point))
2849 (goto-char (point-max))
2850 (skip-chars-backward " \r\t\n")
2852 (delete-region (point) (point-max))
2853 ;; If IND is set, preserve indentation of include keyword until
2854 ;; the first headline encountered.
2856 (unless (eq major-mode
'org-mode
) (org-mode))
2857 (goto-char (point-min))
2858 (let ((ind-str (make-string ind ?
)))
2859 (while (not (or (eobp) (looking-at org-outline-regexp-bol
)))
2860 ;; Do not move footnote definitions out of column 0.
2861 (unless (and (looking-at org-footnote-definition-re
)
2862 (eq (org-element-type (org-element-at-point))
2863 'footnote-definition
))
2866 ;; When MINLEVEL is specified, compute minimal level for headlines
2867 ;; in the file (CUR-MIN), and remove stars to each headline so
2868 ;; that headlines with minimal level have a level of MINLEVEL.
2870 (unless (eq major-mode
'org-mode
) (org-mode))
2871 (let ((levels (org-map-entries
2872 (lambda () (org-reduced-level (org-current-level))))))
2874 (let ((offset (- minlevel
(apply 'min levels
))))
2875 (unless (zerop offset
)
2876 (when org-odd-levels-only
(setq offset
(* offset
2)))
2877 ;; Only change stars, don't bother moving whole
2880 (lambda () (if (< offset
0) (delete-char (abs offset
))
2881 (insert (make-string offset ?
*))))))))))
2885 ;;; Tools For Back-Ends
2887 ;; A whole set of tools is available to help build new exporters. Any
2888 ;; function general enough to have its use across many back-ends
2889 ;; should be added here.
2891 ;; As of now, functions operating on footnotes, headlines, links,
2892 ;; macros, references, src-blocks, tables and tables of contents are
2895 ;;;; For Affiliated Keywords
2897 ;; `org-export-read-attribute' reads a property from a given element
2898 ;; as a plist. It can be used to normalize affiliated keywords'
2901 (defun org-export-read-attribute (attribute element
&optional property
)
2902 "Turn ATTRIBUTE property from ELEMENT into a plist.
2904 When optional argument PROPERTY is non-nil, return the value of
2905 that property within attributes.
2907 This function assumes attributes are defined as \":keyword
2908 value\" pairs. It is appropriate for `:attr_html' like
2911 (let ((value (org-element-property attribute element
)))
2913 (read (format "(%s)" (mapconcat 'identity value
" ")))))))
2914 (if property
(plist-get attributes property
) attributes
)))
2917 ;;;; For Export Snippets
2919 ;; Every export snippet is transmitted to the back-end. Though, the
2920 ;; latter will only retain one type of export-snippet, ignoring
2921 ;; others, based on the former's target back-end. The function
2922 ;; `org-export-snippet-backend' returns that back-end for a given
2925 (defun org-export-snippet-backend (export-snippet)
2926 "Return EXPORT-SNIPPET targeted back-end as a symbol.
2927 Translation, with `org-export-snippet-translation-alist', is
2929 (let ((back-end (org-element-property :back-end export-snippet
)))
2931 (or (cdr (assoc back-end org-export-snippet-translation-alist
))
2937 ;; `org-export-collect-footnote-definitions' is a tool to list
2938 ;; actually used footnotes definitions in the whole parse tree, or in
2939 ;; an headline, in order to add footnote listings throughout the
2942 ;; `org-export-footnote-first-reference-p' is a predicate used by some
2943 ;; back-ends, when they need to attach the footnote definition only to
2944 ;; the first occurrence of the corresponding label.
2946 ;; `org-export-get-footnote-definition' and
2947 ;; `org-export-get-footnote-number' provide easier access to
2948 ;; additional information relative to a footnote reference.
2950 (defun org-export-collect-footnote-definitions (data info
)
2951 "Return an alist between footnote numbers, labels and definitions.
2953 DATA is the parse tree from which definitions are collected.
2954 INFO is the plist used as a communication channel.
2956 Definitions are sorted by order of references. They either
2957 appear as Org data or as a secondary string for inlined
2958 footnotes. Unreferenced definitions are ignored."
2960 collect-fn
; for byte-compiler.
2964 ;; Collect footnote number, label and definition in DATA.
2966 data
'footnote-reference
2968 (when (org-export-footnote-first-reference-p fn info
)
2969 (let ((def (org-export-get-footnote-definition fn info
)))
2971 (list (org-export-get-footnote-number fn info
)
2972 (org-element-property :label fn
)
2975 ;; Also search in definition for nested footnotes.
2976 (when (eq (org-element-property :type fn
) 'standard
)
2977 (funcall collect-fn def
)))))
2978 ;; Don't enter footnote definitions since it will happen
2979 ;; when their first reference is found.
2980 info nil
'footnote-definition
)))))
2981 (funcall collect-fn
(plist-get info
:parse-tree
))
2982 (reverse num-alist
)))
2984 (defun org-export-footnote-first-reference-p (footnote-reference info
)
2985 "Non-nil when a footnote reference is the first one for its label.
2987 FOOTNOTE-REFERENCE is the footnote reference being considered.
2988 INFO is the plist used as a communication channel."
2989 (let ((label (org-element-property :label footnote-reference
)))
2990 ;; Anonymous footnotes are always a first reference.
2992 ;; Otherwise, return the first footnote with the same LABEL and
2993 ;; test if it is equal to FOOTNOTE-REFERENCE.
2994 (let* (search-refs ; for byte-compiler.
2999 data
'footnote-reference
3002 ((string= (org-element-property :label fn
) label
)
3004 ;; If FN isn't inlined, be sure to traverse its
3005 ;; definition before resuming search. See
3006 ;; comments in `org-export-get-footnote-number'
3007 ;; for more information.
3008 ((eq (org-element-property :type fn
) 'standard
)
3009 (funcall search-refs
3010 (org-export-get-footnote-definition fn info
)))))
3011 ;; Don't enter footnote definitions since it will
3012 ;; happen when their first reference is found.
3013 info
'first-match
'footnote-definition
)))))
3014 (eq (catch 'exit
(funcall search-refs
(plist-get info
:parse-tree
)))
3015 footnote-reference
)))))
3017 (defun org-export-get-footnote-definition (footnote-reference info
)
3018 "Return definition of FOOTNOTE-REFERENCE as parsed data.
3019 INFO is the plist used as a communication channel."
3020 (let ((label (org-element-property :label footnote-reference
)))
3021 (or (org-element-property :inline-definition footnote-reference
)
3022 (cdr (assoc label
(plist-get info
:footnote-definition-alist
))))))
3024 (defun org-export-get-footnote-number (footnote info
)
3025 "Return number associated to a footnote.
3027 FOOTNOTE is either a footnote reference or a footnote definition.
3028 INFO is the plist used as a communication channel."
3029 (let* ((label (org-element-property :label footnote
))
3031 search-ref
; For byte-compiler.
3035 ;; Search footnote references through DATA, filling
3036 ;; SEEN-REFS along the way.
3038 data
'footnote-reference
3040 (let ((fn-lbl (org-element-property :label fn
)))
3042 ;; Anonymous footnote match: return number.
3043 ((and (not fn-lbl
) (eq fn footnote
))
3044 (throw 'exit
(1+ (length seen-refs
))))
3045 ;; Labels match: return number.
3046 ((and label
(string= label fn-lbl
))
3047 (throw 'exit
(1+ (length seen-refs
))))
3048 ;; Anonymous footnote: it's always a new one. Also,
3049 ;; be sure to return nil from the `cond' so
3050 ;; `first-match' doesn't get us out of the loop.
3051 ((not fn-lbl
) (push 'inline seen-refs
) nil
)
3052 ;; Label not seen so far: add it so SEEN-REFS.
3054 ;; Also search for subsequent references in
3055 ;; footnote definition so numbering follows reading
3056 ;; logic. Note that we don't have to care about
3057 ;; inline definitions, since `org-element-map'
3058 ;; already traverses them at the right time.
3060 ;; Once again, return nil to stay in the loop.
3061 ((not (member fn-lbl seen-refs
))
3062 (push fn-lbl seen-refs
)
3064 (org-export-get-footnote-definition fn info
))
3066 ;; Don't enter footnote definitions since it will happen
3067 ;; when their first reference is found.
3068 info
'first-match
'footnote-definition
)))))
3069 (catch 'exit
(funcall search-ref
(plist-get info
:parse-tree
)))))
3074 ;; `org-export-get-relative-level' is a shortcut to get headline
3075 ;; level, relatively to the lower headline level in the parsed tree.
3077 ;; `org-export-get-headline-number' returns the section number of an
3078 ;; headline, while `org-export-number-to-roman' allows to convert it
3079 ;; to roman numbers.
3081 ;; `org-export-low-level-p', `org-export-first-sibling-p' and
3082 ;; `org-export-last-sibling-p' are three useful predicates when it
3083 ;; comes to fulfill the `:headline-levels' property.
3085 (defun org-export-get-relative-level (headline info
)
3086 "Return HEADLINE relative level within current parsed tree.
3087 INFO is a plist holding contextual information."
3088 (+ (org-element-property :level headline
)
3089 (or (plist-get info
:headline-offset
) 0)))
3091 (defun org-export-low-level-p (headline info
)
3092 "Non-nil when HEADLINE is considered as low level.
3094 INFO is a plist used as a communication channel.
3096 A low level headlines has a relative level greater than
3097 `:headline-levels' property value.
3099 Return value is the difference between HEADLINE relative level
3100 and the last level being considered as high enough, or nil."
3101 (let ((limit (plist-get info
:headline-levels
)))
3102 (when (wholenump limit
)
3103 (let ((level (org-export-get-relative-level headline info
)))
3104 (and (> level limit
) (- level limit
))))))
3106 (defun org-export-get-headline-number (headline info
)
3107 "Return HEADLINE numbering as a list of numbers.
3108 INFO is a plist holding contextual information."
3109 (cdr (assoc headline
(plist-get info
:headline-numbering
))))
3111 (defun org-export-numbered-headline-p (headline info
)
3112 "Return a non-nil value if HEADLINE element should be numbered.
3113 INFO is a plist used as a communication channel."
3114 (let ((sec-num (plist-get info
:section-numbers
))
3115 (level (org-export-get-relative-level headline info
)))
3116 (if (wholenump sec-num
) (<= level sec-num
) sec-num
)))
3118 (defun org-export-number-to-roman (n)
3119 "Convert integer N into a roman numeral."
3120 (let ((roman '((1000 .
"M") (900 .
"CM") (500 .
"D") (400 .
"CD")
3121 ( 100 .
"C") ( 90 .
"XC") ( 50 .
"L") ( 40 .
"XL")
3122 ( 10 .
"X") ( 9 .
"IX") ( 5 .
"V") ( 4 .
"IV")
3126 (number-to-string n
)
3128 (if (>= n
(caar roman
))
3129 (setq n
(- n
(caar roman
))
3130 res
(concat res
(cdar roman
)))
3134 (defun org-export-get-tags (element info
&optional tags
)
3135 "Return list of tags associated to ELEMENT.
3137 ELEMENT has either an `headline' or an `inlinetask' type. INFO
3138 is a plist used as a communication channel.
3140 Select tags (see `org-export-select-tags') and exclude tags (see
3141 `org-export-exclude-tags') are removed from the list.
3143 When non-nil, optional argument TAGS should be a list of strings.
3144 Any tag belonging to this list will also be removed."
3145 (org-remove-if (lambda (tag) (or (member tag
(plist-get info
:select-tags
))
3146 (member tag
(plist-get info
:exclude-tags
))
3148 (org-element-property :tags element
)))
3150 (defun org-export-first-sibling-p (headline info
)
3151 "Non-nil when HEADLINE is the first sibling in its sub-tree.
3152 INFO is a plist used as a communication channel."
3153 (not (eq (org-element-type (org-export-get-previous-element headline info
))
3156 (defun org-export-last-sibling-p (headline info
)
3157 "Non-nil when HEADLINE is the last sibling in its sub-tree.
3158 INFO is a plist used as a communication channel."
3159 (not (org-export-get-next-element headline info
)))
3164 ;; `org-export-solidify-link-text' turns a string into a safer version
3165 ;; for links, replacing most non-standard characters with hyphens.
3167 ;; `org-export-get-coderef-format' returns an appropriate format
3168 ;; string for coderefs.
3170 ;; `org-export-inline-image-p' returns a non-nil value when the link
3171 ;; provided should be considered as an inline image.
3173 ;; `org-export-resolve-fuzzy-link' searches destination of fuzzy links
3174 ;; (i.e. links with "fuzzy" as type) within the parsed tree, and
3175 ;; returns an appropriate unique identifier when found, or nil.
3177 ;; `org-export-resolve-id-link' returns the first headline with
3178 ;; specified id or custom-id in parse tree, the path to the external
3179 ;; file with the id or nil when neither was found.
3181 ;; `org-export-resolve-coderef' associates a reference to a line
3182 ;; number in the element it belongs, or returns the reference itself
3183 ;; when the element isn't numbered.
3185 (defun org-export-solidify-link-text (s)
3186 "Take link text S and make a safe target out of it."
3188 (mapconcat 'identity
(org-split-string s
"[^a-zA-Z0-9_.-]+") "-")))
3190 (defun org-export-get-coderef-format (path desc
)
3191 "Return format string for code reference link.
3192 PATH is the link path. DESC is its description."
3194 (cond ((not desc
) "%s")
3195 ((string-match (regexp-quote (concat "(" path
")")) desc
)
3196 (replace-match "%s" t t desc
))
3199 (defun org-export-inline-image-p (link &optional rules
)
3200 "Non-nil if LINK object points to an inline image.
3202 Optional argument is a set of RULES defining inline images. It
3203 is an alist where associations have the following shape:
3207 Applying a rule means apply REGEXP against LINK's path when its
3208 type is TYPE. The function will return a non-nil value if any of
3209 the provided rules is non-nil. The default rule is
3210 `org-export-default-inline-image-rule'.
3212 This only applies to links without a description."
3213 (and (not (org-element-contents link
))
3214 (let ((case-fold-search t
)
3215 (rules (or rules org-export-default-inline-image-rule
)))
3219 (and (string= (org-element-property :type link
) (car rule
))
3220 (string-match (cdr rule
)
3221 (org-element-property :path link
))
3224 ;; Return nil if no rule matched.
3227 (defun org-export-resolve-coderef (ref info
)
3228 "Resolve a code reference REF.
3230 INFO is a plist used as a communication channel.
3232 Return associated line number in source code, or REF itself,
3233 depending on src-block or example element's switches."
3235 (plist-get info
:parse-tree
) '(example-block src-block
)
3238 (insert (org-trim (org-element-property :value el
)))
3239 (let* ((label-fmt (regexp-quote
3240 (or (org-element-property :label-fmt el
)
3241 org-coderef-label-format
)))
3243 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
3244 (replace-regexp-in-string "%s" ref label-fmt nil t
))))
3245 ;; Element containing REF is found. Resolve it to either
3246 ;; a label or a line number, as needed.
3247 (when (re-search-backward ref-re nil t
)
3249 ((org-element-property :use-labels el
) ref
)
3250 ((eq (org-element-property :number-lines el
) 'continued
)
3251 (+ (org-export-get-loc el info
) (line-number-at-pos)))
3252 (t (line-number-at-pos)))))))
3255 (defun org-export-resolve-fuzzy-link (link info
)
3256 "Return LINK destination.
3258 INFO is a plist holding contextual information.
3260 Return value can be an object, an element, or nil:
3262 - If LINK path matches a target object (i.e. <<path>>) or
3263 element (i.e. \"#+TARGET: path\"), return it.
3265 - If LINK path exactly matches the name affiliated keyword
3266 \(i.e. #+NAME: path) of an element, return that element.
3268 - If LINK path exactly matches any headline name, return that
3269 element. If more than one headline share that name, priority
3270 will be given to the one with the closest common ancestor, if
3271 any, or the first one in the parse tree otherwise.
3273 - Otherwise, return nil.
3275 Assume LINK type is \"fuzzy\"."
3276 (let* ((path (org-element-property :path link
))
3277 (match-title-p (eq (aref path
0) ?
*)))
3279 ;; First try to find a matching "<<path>>" unless user specified
3280 ;; he was looking for an headline (path starts with a *
3282 ((and (not match-title-p
)
3283 (loop for target in
(plist-get info
:target-list
)
3284 when
(string= (org-element-property :value target
) path
)
3286 ;; Then try to find an element with a matching "#+NAME: path"
3287 ;; affiliated keyword.
3288 ((and (not match-title-p
)
3290 (plist-get info
:parse-tree
) org-element-all-elements
3292 (when (string= (org-element-property :name el
) path
) el
))
3293 info
'first-match
)))
3294 ;; Last case: link either points to an headline or to
3295 ;; nothingness. Try to find the source, with priority given to
3296 ;; headlines with the closest common ancestor. If such candidate
3297 ;; is found, return it, otherwise return nil.
3299 (let ((find-headline
3301 ;; Return first headline whose `:raw-value' property
3302 ;; is NAME in parse tree DATA, or nil.
3308 (org-element-property :raw-value headline
)
3311 info
'first-match
)))))
3312 ;; Search among headlines sharing an ancestor with link,
3313 ;; from closest to farthest.
3317 (when (eq (org-element-type parent
) 'headline
)
3318 (let ((foundp (funcall find-headline path parent
)))
3319 (when foundp
(throw 'exit foundp
)))))
3320 (org-export-get-genealogy link
)) nil
)
3321 ;; No match with a common ancestor: try the full parse-tree.
3322 (funcall find-headline
3323 (if match-title-p
(substring path
1) path
)
3324 (plist-get info
:parse-tree
))))))))
3326 (defun org-export-resolve-id-link (link info
)
3327 "Return headline referenced as LINK destination.
3329 INFO is a plist used as a communication channel.
3331 Return value can be the headline element matched in current parse
3332 tree, a file name or nil. Assume LINK type is either \"id\" or
3334 (let ((id (org-element-property :path link
)))
3335 ;; First check if id is within the current parse tree.
3336 (or (org-element-map
3337 (plist-get info
:parse-tree
) 'headline
3339 (when (or (string= (org-element-property :id headline
) id
)
3340 (string= (org-element-property :custom-id headline
) id
))
3343 ;; Otherwise, look for external files.
3344 (cdr (assoc id
(plist-get info
:id-alist
))))))
3346 (defun org-export-resolve-radio-link (link info
)
3347 "Return radio-target object referenced as LINK destination.
3349 INFO is a plist used as a communication channel.
3351 Return value can be a radio-target object or nil. Assume LINK
3352 has type \"radio\"."
3353 (let ((path (org-element-property :path link
)))
3355 (plist-get info
:parse-tree
) 'radio-target
3357 (when (equal (org-element-property :value radio
) path
) radio
))
3358 info
'first-match
)))
3363 ;; `org-export-expand-macro' simply takes care of expanding macros.
3365 (defun org-export-expand-macro (macro info
)
3366 "Expand MACRO and return it as a string.
3367 INFO is a plist holding export options."
3368 (let* ((key (org-element-property :key macro
))
3369 (args (org-element-property :args macro
))
3370 ;; User's macros are stored in the communication channel with
3371 ;; a ":macro-" prefix. Replace arguments in VALUE. Also
3372 ;; expand recursively macros within.
3373 (value (org-export-data
3376 (if (not (stringp obj
)) (org-export-data obj info
)
3377 (replace-regexp-in-string
3380 (nth (1- (string-to-number (substring arg
1))) args
))
3382 (plist-get info
(intern (format ":macro-%s" key
))))
3384 ;; VALUE starts with "(eval": it is a s-exp, `eval' it.
3385 (when (string-match "\\`(eval\\>" value
) (setq value
(eval (read value
))))
3387 (format "%s" (or value
""))))
3392 ;; `org-export-get-ordinal' associates a sequence number to any object
3395 (defun org-export-get-ordinal (element info
&optional types predicate
)
3396 "Return ordinal number of an element or object.
3398 ELEMENT is the element or object considered. INFO is the plist
3399 used as a communication channel.
3401 Optional argument TYPES, when non-nil, is a list of element or
3402 object types, as symbols, that should also be counted in.
3403 Otherwise, only provided element's type is considered.
3405 Optional argument PREDICATE is a function returning a non-nil
3406 value if the current element or object should be counted in. It
3407 accepts two arguments: the element or object being considered and
3408 the plist used as a communication channel. This allows to count
3409 only a certain type of objects (i.e. inline images).
3411 Return value is a list of numbers if ELEMENT is an headline or an
3412 item. It is nil for keywords. It represents the footnote number
3413 for footnote definitions and footnote references. If ELEMENT is
3414 a target, return the same value as if ELEMENT was the closest
3415 table, item or headline containing the target. In any other
3416 case, return the sequence number of ELEMENT among elements or
3417 objects of the same type."
3418 ;; A target keyword, representing an invisible target, never has
3419 ;; a sequence number.
3420 (unless (eq (org-element-type element
) 'keyword
)
3421 ;; Ordinal of a target object refer to the ordinal of the closest
3422 ;; table, item, or headline containing the object.
3423 (when (eq (org-element-type element
) 'target
)
3425 (loop for parent in
(org-export-get-genealogy element
)
3428 (org-element-type parent
)
3429 '(footnote-definition footnote-reference headline item
3432 (case (org-element-type element
)
3433 ;; Special case 1: An headline returns its number as a list.
3434 (headline (org-export-get-headline-number element info
))
3435 ;; Special case 2: An item returns its number as a list.
3436 (item (let ((struct (org-element-property :structure element
)))
3437 (org-list-get-item-number
3438 (org-element-property :begin element
)
3440 (org-list-prevs-alist struct
)
3441 (org-list-parents-alist struct
))))
3442 ((footnote-definition footnote-reference
)
3443 (org-export-get-footnote-number element info
))
3446 ;; Increment counter until ELEMENT is found again.
3448 (plist-get info
:parse-tree
) (or types
(org-element-type element
))
3451 ((eq element el
) (1+ counter
))
3452 ((not predicate
) (incf counter
) nil
)
3453 ((funcall predicate el info
) (incf counter
) nil
)))
3454 info
'first-match
))))))
3459 ;; `org-export-get-loc' counts number of code lines accumulated in
3460 ;; src-block or example-block elements with a "+n" switch until
3461 ;; a given element, excluded. Note: "-n" switches reset that count.
3463 ;; `org-export-unravel-code' extracts source code (along with a code
3464 ;; references alist) from an `element-block' or `src-block' type
3467 ;; `org-export-format-code' applies a formatting function to each line
3468 ;; of code, providing relative line number and code reference when
3469 ;; appropriate. Since it doesn't access the original element from
3470 ;; which the source code is coming, it expects from the code calling
3471 ;; it to know if lines should be numbered and if code references
3474 ;; Eventually, `org-export-format-code-default' is a higher-level
3475 ;; function (it makes use of the two previous functions) which handles
3476 ;; line numbering and code references inclusion, and returns source
3477 ;; code in a format suitable for plain text or verbatim output.
3479 (defun org-export-get-loc (element info
)
3480 "Return accumulated lines of code up to ELEMENT.
3482 INFO is the plist used as a communication channel.
3484 ELEMENT is excluded from count."
3487 (plist-get info
:parse-tree
)
3488 `(src-block example-block
,(org-element-type element
))
3491 ;; ELEMENT is reached: Quit the loop.
3493 ;; Only count lines from src-block and example-block elements
3494 ;; with a "+n" or "-n" switch. A "-n" switch resets counter.
3495 ((not (memq (org-element-type el
) '(src-block example-block
))) nil
)
3496 ((let ((linums (org-element-property :number-lines el
)))
3498 ;; Accumulate locs or reset them.
3499 (let ((lines (org-count-lines
3500 (org-trim (org-element-property :value el
)))))
3501 (setq loc
(if (eq linums
'new
) lines
(+ loc lines
))))))
3502 ;; Return nil to stay in the loop.
3508 (defun org-export-unravel-code (element)
3509 "Clean source code and extract references out of it.
3511 ELEMENT has either a `src-block' an `example-block' type.
3513 Return a cons cell whose CAR is the source code, cleaned from any
3514 reference and protective comma and CDR is an alist between
3515 relative line number (integer) and name of code reference on that
3517 (let* ((line 0) refs
3518 ;; Get code and clean it. Remove blank lines at its
3519 ;; beginning and end.
3520 (code (let ((c (replace-regexp-in-string
3521 "\\`\\([ \t]*\n\\)+" ""
3522 (replace-regexp-in-string
3523 "\\(:?[ \t]*\n\\)*[ \t]*\\'" "\n"
3524 (org-element-property :value element
)))))
3525 ;; If appropriate, remove global indentation.
3526 (if (or org-src-preserve-indentation
3527 (org-element-property :preserve-indent element
))
3529 (org-remove-indentation c
))))
3530 ;; Get format used for references.
3531 (label-fmt (regexp-quote
3532 (or (org-element-property :label-fmt element
)
3533 org-coderef-label-format
)))
3534 ;; Build a regexp matching a loc with a reference.
3536 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)[ \t]*\\)$"
3537 (replace-regexp-in-string
3538 "%s" "\\([-a-zA-Z0-9_ ]+\\)" label-fmt nil t
))))
3541 ;; Code with references removed.
3542 (org-element-normalize-string
3546 (if (not (string-match with-ref-re loc
)) loc
3547 ;; Ref line: remove ref, and signal its position in REFS.
3548 (push (cons line
(match-string 3 loc
)) refs
)
3549 (replace-match "" nil nil loc
1)))
3550 (org-split-string code
"\n") "\n"))
3554 (defun org-export-format-code (code fun
&optional num-lines ref-alist
)
3555 "Format CODE by applying FUN line-wise and return it.
3557 CODE is a string representing the code to format. FUN is
3558 a function. It must accept three arguments: a line of
3559 code (string), the current line number (integer) or nil and the
3560 reference associated to the current line (string) or nil.
3562 Optional argument NUM-LINES can be an integer representing the
3563 number of code lines accumulated until the current code. Line
3564 numbers passed to FUN will take it into account. If it is nil,
3565 FUN's second argument will always be nil. This number can be
3566 obtained with `org-export-get-loc' function.
3568 Optional argument REF-ALIST can be an alist between relative line
3569 number (i.e. ignoring NUM-LINES) and the name of the code
3570 reference on it. If it is nil, FUN's third argument will always
3571 be nil. It can be obtained through the use of
3572 `org-export-unravel-code' function."
3573 (let ((--locs (org-split-string code
"\n"))
3575 (org-element-normalize-string
3579 (let ((--ref (cdr (assq --line ref-alist
))))
3580 (funcall fun --loc
(and num-lines
(+ num-lines --line
)) --ref
)))
3583 (defun org-export-format-code-default (element info
)
3584 "Return source code from ELEMENT, formatted in a standard way.
3586 ELEMENT is either a `src-block' or `example-block' element. INFO
3587 is a plist used as a communication channel.
3589 This function takes care of line numbering and code references
3590 inclusion. Line numbers, when applicable, appear at the
3591 beginning of the line, separated from the code by two white
3592 spaces. Code references, on the other hand, appear flushed to
3593 the right, separated by six white spaces from the widest line of
3595 ;; Extract code and references.
3596 (let* ((code-info (org-export-unravel-code element
))
3597 (code (car code-info
))
3598 (code-lines (org-split-string code
"\n"))
3599 (refs (and (org-element-property :retain-labels element
)
3601 ;; Handle line numbering.
3602 (num-start (case (org-element-property :number-lines element
)
3603 (continued (org-export-get-loc element info
))
3608 (length (number-to-string
3609 (+ (length code-lines
) num-start
))))))
3610 ;; Prepare references display, if required. Any reference
3611 ;; should start six columns after the widest line of code,
3612 ;; wrapped with parenthesis.
3614 (+ (apply 'max
(mapcar 'length code-lines
))
3615 (if (not num-start
) 0 (length (format num-fmt num-start
))))))
3616 (org-export-format-code
3618 (lambda (loc line-num ref
)
3619 (let ((number-str (and num-fmt
(format num-fmt line-num
))))
3624 (concat (make-string
3626 (+ (length loc
) (length number-str
))) ?
)
3627 (format "(%s)" ref
))))))
3633 ;; `org-export-table-has-special-column-p' and and
3634 ;; `org-export-table-row-is-special-p' are predicates used to look for
3635 ;; meta-information about the table structure.
3637 ;; `org-table-has-header-p' tells when the rows before the first rule
3638 ;; should be considered as table's header.
3640 ;; `org-export-table-cell-width', `org-export-table-cell-alignment'
3641 ;; and `org-export-table-cell-borders' extract information from
3642 ;; a table-cell element.
3644 ;; `org-export-table-dimensions' gives the number on rows and columns
3645 ;; in the table, ignoring horizontal rules and special columns.
3646 ;; `org-export-table-cell-address', given a table-cell object, returns
3647 ;; the absolute address of a cell. On the other hand,
3648 ;; `org-export-get-table-cell-at' does the contrary.
3650 ;; `org-export-table-cell-starts-colgroup-p',
3651 ;; `org-export-table-cell-ends-colgroup-p',
3652 ;; `org-export-table-row-starts-rowgroup-p',
3653 ;; `org-export-table-row-ends-rowgroup-p',
3654 ;; `org-export-table-row-starts-header-p' and
3655 ;; `org-export-table-row-ends-header-p' indicate position of current
3656 ;; row or cell within the table.
3658 (defun org-export-table-has-special-column-p (table)
3659 "Non-nil when TABLE has a special column.
3660 All special columns will be ignored during export."
3661 ;; The table has a special column when every first cell of every row
3662 ;; has an empty value or contains a symbol among "/", "#", "!", "$",
3663 ;; "*" "_" and "^". Though, do not consider a first row containing
3664 ;; only empty cells as special.
3665 (let ((special-column-p 'empty
))
3669 (when (eq (org-element-property :type row
) 'standard
)
3670 (let ((value (org-element-contents
3671 (car (org-element-contents row
)))))
3672 (cond ((member value
'(("/") ("#") ("!") ("$") ("*") ("_") ("^")))
3673 (setq special-column-p
'special
))
3675 (t (throw 'exit nil
))))))
3676 (org-element-contents table
))
3677 (eq special-column-p
'special
))))
3679 (defun org-export-table-has-header-p (table info
)
3680 "Non-nil when TABLE has an header.
3682 INFO is a plist used as a communication channel.
3684 A table has an header when it contains at least two row groups."
3685 (let ((rowgroup 1) row-flag
)
3691 ((and row-flag
(eq (org-element-property :type row
) 'rule
))
3692 (incf rowgroup
) (setq row-flag nil
))
3693 ((and (not row-flag
) (eq (org-element-property :type row
) 'standard
))
3694 (setq row-flag t
) nil
)))
3697 (defun org-export-table-row-is-special-p (table-row info
)
3698 "Non-nil if TABLE-ROW is considered special.
3700 INFO is a plist used as the communication channel.
3702 All special rows will be ignored during export."
3703 (when (eq (org-element-property :type table-row
) 'standard
)
3704 (let ((first-cell (org-element-contents
3705 (car (org-element-contents table-row
)))))
3706 ;; A row is special either when...
3708 ;; ... it starts with a field only containing "/",
3709 (equal first-cell
'("/"))
3710 ;; ... the table contains a special column and the row start
3711 ;; with a marking character among, "^", "_", "$" or "!",
3712 (and (org-export-table-has-special-column-p
3713 (org-export-get-parent table-row
))
3714 (member first-cell
'(("^") ("_") ("$") ("!"))))
3715 ;; ... it contains only alignment cookies and empty cells.
3716 (let ((special-row-p 'empty
))
3720 (let ((value (org-element-contents cell
)))
3721 ;; Since VALUE is a secondary string, the following
3722 ;; checks avoid expanding it with `org-export-data'.
3724 ((and (not (cdr value
))
3725 (stringp (car value
))
3726 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'"
3728 (setq special-row-p
'cookie
))
3729 (t (throw 'exit nil
)))))
3730 (org-element-contents table-row
))
3731 (eq special-row-p
'cookie
)))))))
3733 (defun org-export-table-row-group (table-row info
)
3734 "Return TABLE-ROW's group.
3736 INFO is a plist used as the communication channel.
3738 Return value is the group number, as an integer, or nil special
3739 rows and table rules. Group 1 is also table's header."
3740 (unless (or (eq (org-element-property :type table-row
) 'rule
)
3741 (org-export-table-row-is-special-p table-row info
))
3742 (let ((group 0) row-flag
)
3747 ((and (eq (org-element-property :type row
) 'standard
)
3748 (not (org-export-table-row-is-special-p row info
)))
3749 (unless row-flag
(incf group
) (setq row-flag t
)))
3750 ((eq (org-element-property :type row
) 'rule
)
3751 (setq row-flag nil
)))
3752 (when (eq table-row row
) (throw 'found group
)))
3753 (org-element-contents (org-export-get-parent table-row
)))))))
3755 (defun org-export-table-cell-width (table-cell info
)
3756 "Return TABLE-CELL contents width.
3758 INFO is a plist used as the communication channel.
3760 Return value is the width given by the last width cookie in the
3761 same column as TABLE-CELL, or nil."
3762 (let* ((row (org-export-get-parent table-cell
))
3763 (column (let ((cells (org-element-contents row
)))
3764 (- (length cells
) (length (memq table-cell cells
)))))
3765 (table (org-export-get-parent-table table-cell
))
3770 ;; In a special row, try to find a width cookie at COLUMN.
3771 ((org-export-table-row-is-special-p row info
)
3772 (let ((value (org-element-contents
3773 (elt (org-element-contents row
) column
))))
3774 ;; The following checks avoid expanding unnecessarily the
3775 ;; cell with `org-export-data'
3778 (stringp (car value
))
3779 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'" (car value
))
3780 (match-string 1 (car value
)))
3782 (string-to-number (match-string 1 (car value
)))))))
3783 ;; Ignore table rules.
3784 ((eq (org-element-property :type row
) 'rule
))))
3785 (org-element-contents table
))
3789 (defun org-export-table-cell-alignment (table-cell info
)
3790 "Return TABLE-CELL contents alignment.
3792 INFO is a plist used as the communication channel.
3794 Return alignment as specified by the last alignment cookie in the
3795 same column as TABLE-CELL. If no such cookie is found, a default
3796 alignment value will be deduced from fraction of numbers in the
3797 column (see `org-table-number-fraction' for more information).
3798 Possible values are `left', `right' and `center'."
3799 (let* ((row (org-export-get-parent table-cell
))
3800 (column (let ((cells (org-element-contents row
)))
3801 (- (length cells
) (length (memq table-cell cells
)))))
3802 (table (org-export-get-parent-table table-cell
))
3809 ;; In a special row, try to find an alignment cookie at
3811 ((org-export-table-row-is-special-p row info
)
3812 (let ((value (org-element-contents
3813 (elt (org-element-contents row
) column
))))
3814 ;; Since VALUE is a secondary string, the following checks
3815 ;; avoid useless expansion through `org-export-data'.
3818 (stringp (car value
))
3819 (string-match "\\`<\\([lrc]\\)?\\([0-9]+\\)?>\\'"
3821 (match-string 1 (car value
)))
3822 (setq cookie-align
(match-string 1 (car value
))))))
3823 ;; Ignore table rules.
3824 ((eq (org-element-property :type row
) 'rule
))
3825 ;; In a standard row, check if cell's contents are expressing
3826 ;; some kind of number. Increase NUMBER-CELLS accordingly.
3827 ;; Though, don't bother if an alignment cookie has already
3828 ;; defined cell's alignment.
3830 (let ((value (org-export-data
3831 (org-element-contents
3832 (elt (org-element-contents row
) column
))
3835 (when (string-match org-table-number-regexp value
)
3836 (incf number-cells
))))))
3837 (org-element-contents table
))
3838 ;; Return value. Alignment specified by cookies has precedence
3839 ;; over alignment deduced from cells contents.
3840 (cond ((equal cookie-align
"l") 'left
)
3841 ((equal cookie-align
"r") 'right
)
3842 ((equal cookie-align
"c") 'center
)
3843 ((>= (/ (float number-cells
) total-cells
) org-table-number-fraction
)
3847 (defun org-export-table-cell-borders (table-cell info
)
3848 "Return TABLE-CELL borders.
3850 INFO is a plist used as a communication channel.
3852 Return value is a list of symbols, or nil. Possible values are:
3853 `top', `bottom', `above', `below', `left' and `right'. Note:
3854 `top' (resp. `bottom') only happen for a cell in the first
3855 row (resp. last row) of the table, ignoring table rules, if any.
3857 Returned borders ignore special rows."
3858 (let* ((row (org-export-get-parent table-cell
))
3859 (table (org-export-get-parent-table table-cell
))
3861 ;; Top/above border? TABLE-CELL has a border above when a rule
3862 ;; used to demarcate row groups can be found above. Hence,
3863 ;; finding a rule isn't sufficient to push `above' in BORDERS:
3864 ;; another regular row has to be found above that rule.
3868 (cond ((eq (org-element-property :type row
) 'rule
)
3870 ((not (org-export-table-row-is-special-p row info
))
3871 (if rule-flag
(throw 'exit
(push 'above borders
))
3872 (throw 'exit nil
)))))
3873 ;; Look at every row before the current one.
3874 (cdr (memq row
(reverse (org-element-contents table
)))))
3875 ;; No rule above, or rule found starts the table (ignoring any
3876 ;; special row): TABLE-CELL is at the top of the table.
3877 (when rule-flag
(push 'above borders
))
3878 (push 'top borders
)))
3879 ;; Bottom/below border? TABLE-CELL has a border below when next
3880 ;; non-regular row below is a rule.
3884 (cond ((eq (org-element-property :type row
) 'rule
)
3886 ((not (org-export-table-row-is-special-p row info
))
3887 (if rule-flag
(throw 'exit
(push 'below borders
))
3888 (throw 'exit nil
)))))
3889 ;; Look at every row after the current one.
3890 (cdr (memq row
(org-element-contents table
))))
3891 ;; No rule below, or rule found ends the table (modulo some
3892 ;; special row): TABLE-CELL is at the bottom of the table.
3893 (when rule-flag
(push 'below borders
))
3894 (push 'bottom borders
)))
3895 ;; Right/left borders? They can only be specified by column
3896 ;; groups. Column groups are defined in a row starting with "/".
3897 ;; Also a column groups row only contains "<", "<>", ">" or blank
3900 (let ((column (let ((cells (org-element-contents row
)))
3901 (- (length cells
) (length (memq table-cell cells
))))))
3904 (unless (eq (org-element-property :type row
) 'rule
)
3905 (when (equal (org-element-contents
3906 (car (org-element-contents row
)))
3908 (let ((column-groups
3911 (let ((value (org-element-contents cell
)))
3912 (when (member value
'(("<") ("<>") (">") nil
))
3914 (org-element-contents row
))))
3915 ;; There's a left border when previous cell, if
3916 ;; any, ends a group, or current one starts one.
3917 (when (or (and (not (zerop column
))
3918 (member (elt column-groups
(1- column
))
3920 (member (elt column-groups column
) '("<" "<>")))
3921 (push 'left borders
))
3922 ;; There's a right border when next cell, if any,
3923 ;; starts a group, or current one ends one.
3924 (when (or (and (/= (1+ column
) (length column-groups
))
3925 (member (elt column-groups
(1+ column
))
3927 (member (elt column-groups column
) '(">" "<>")))
3928 (push 'right borders
))
3929 (throw 'exit nil
)))))
3930 ;; Table rows are read in reverse order so last column groups
3931 ;; row has precedence over any previous one.
3932 (reverse (org-element-contents table
)))))
3936 (defun org-export-table-cell-starts-colgroup-p (table-cell info
)
3937 "Non-nil when TABLE-CELL is at the beginning of a row group.
3938 INFO is a plist used as a communication channel."
3939 ;; A cell starts a column group either when it is at the beginning
3940 ;; of a row (or after the special column, if any) or when it has
3942 (or (eq (org-element-map
3943 (org-export-get-parent table-cell
)
3944 'table-cell
'identity info
'first-match
)
3946 (memq 'left
(org-export-table-cell-borders table-cell info
))))
3948 (defun org-export-table-cell-ends-colgroup-p (table-cell info
)
3949 "Non-nil when TABLE-CELL is at the end of a row group.
3950 INFO is a plist used as a communication channel."
3951 ;; A cell ends a column group either when it is at the end of a row
3952 ;; or when it has a right border.
3953 (or (eq (car (last (org-element-contents
3954 (org-export-get-parent table-cell
))))
3956 (memq 'right
(org-export-table-cell-borders table-cell info
))))
3958 (defun org-export-table-row-starts-rowgroup-p (table-row info
)
3959 "Non-nil when TABLE-ROW is at the beginning of a column group.
3960 INFO is a plist used as a communication channel."
3961 (unless (or (eq (org-element-property :type table-row
) 'rule
)
3962 (org-export-table-row-is-special-p table-row info
))
3963 (let ((borders (org-export-table-cell-borders
3964 (car (org-element-contents table-row
)) info
)))
3965 (or (memq 'top borders
) (memq 'above borders
)))))
3967 (defun org-export-table-row-ends-rowgroup-p (table-row info
)
3968 "Non-nil when TABLE-ROW is at the end of a column group.
3969 INFO is a plist used as a communication channel."
3970 (unless (or (eq (org-element-property :type table-row
) 'rule
)
3971 (org-export-table-row-is-special-p table-row info
))
3972 (let ((borders (org-export-table-cell-borders
3973 (car (org-element-contents table-row
)) info
)))
3974 (or (memq 'bottom borders
) (memq 'below borders
)))))
3976 (defun org-export-table-row-starts-header-p (table-row info
)
3977 "Non-nil when TABLE-ROW is the first table header's row.
3978 INFO is a plist used as a communication channel."
3979 (and (org-export-table-has-header-p
3980 (org-export-get-parent-table table-row
) info
)
3981 (org-export-table-row-starts-rowgroup-p table-row info
)
3982 (= (org-export-table-row-group table-row info
) 1)))
3984 (defun org-export-table-row-ends-header-p (table-row info
)
3985 "Non-nil when TABLE-ROW is the last table header's row.
3986 INFO is a plist used as a communication channel."
3987 (and (org-export-table-has-header-p
3988 (org-export-get-parent-table table-row
) info
)
3989 (org-export-table-row-ends-rowgroup-p table-row info
)
3990 (= (org-export-table-row-group table-row info
) 1)))
3992 (defun org-export-table-dimensions (table info
)
3993 "Return TABLE dimensions.
3995 INFO is a plist used as a communication channel.
3997 Return value is a CONS like (ROWS . COLUMNS) where
3998 ROWS (resp. COLUMNS) is the number of exportable
3999 rows (resp. columns)."
4000 (let (first-row (columns 0) (rows 0))
4001 ;; Set number of rows, and extract first one.
4005 (when (eq (org-element-property :type row
) 'standard
)
4007 (unless first-row
(setq first-row row
)))) info
)
4008 ;; Set number of columns.
4009 (org-element-map first-row
'table-cell
(lambda (cell) (incf columns
)) info
)
4011 (cons rows columns
)))
4013 (defun org-export-table-cell-address (table-cell info
)
4014 "Return address of a regular TABLE-CELL object.
4016 TABLE-CELL is the cell considered. INFO is a plist used as
4017 a communication channel.
4019 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
4020 zero-based index. Only exportable cells are considered. The
4021 function returns nil for other cells."
4022 (let* ((table-row (org-export-get-parent table-cell
))
4023 (table (org-export-get-parent-table table-cell
)))
4024 ;; Ignore cells in special rows or in special column.
4025 (unless (or (org-export-table-row-is-special-p table-row info
)
4026 (and (org-export-table-has-special-column-p table
)
4027 (eq (car (org-element-contents table-row
)) table-cell
)))
4030 (let ((row-count 0))
4034 (cond ((eq (org-element-property :type row
) 'rule
) nil
)
4035 ((eq row table-row
) row-count
)
4036 (t (incf row-count
) nil
)))
4039 (let ((col-count 0))
4041 table-row
'table-cell
4043 (if (eq cell table-cell
) col-count
(incf col-count
) nil
))
4044 info
'first-match
))))))
4046 (defun org-export-get-table-cell-at (address table info
)
4047 "Return regular table-cell object at ADDRESS in TABLE.
4049 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
4050 zero-based index. TABLE is a table type element. INFO is
4051 a plist used as a communication channel.
4053 If no table-cell, among exportable cells, is found at ADDRESS,
4055 (let ((column-pos (cdr address
)) (column-count 0))
4057 ;; Row at (car address) or nil.
4058 (let ((row-pos (car address
)) (row-count 0))
4062 (cond ((eq (org-element-property :type row
) 'rule
) nil
)
4063 ((= row-count row-pos
) row
)
4064 (t (incf row-count
) nil
)))
4068 (if (= column-count column-pos
) cell
4069 (incf column-count
) nil
))
4070 info
'first-match
)))
4073 ;;;; For Tables Of Contents
4075 ;; `org-export-collect-headlines' builds a list of all exportable
4076 ;; headline elements, maybe limited to a certain depth. One can then
4077 ;; easily parse it and transcode it.
4079 ;; Building lists of tables, figures or listings is quite similar.
4080 ;; Once the generic function `org-export-collect-elements' is defined,
4081 ;; `org-export-collect-tables', `org-export-collect-figures' and
4082 ;; `org-export-collect-listings' can be derived from it.
4084 (defun org-export-collect-headlines (info &optional n
)
4085 "Collect headlines in order to build a table of contents.
4087 INFO is a plist used as a communication channel.
4089 When optional argument N is an integer, it specifies the depth of
4090 the table of contents. Otherwise, it is set to the value of the
4091 last headline level. See `org-export-headline-levels' for more
4094 Return a list of all exportable headlines as parsed elements."
4095 (unless (wholenump n
) (setq n
(plist-get info
:headline-levels
)))
4097 (plist-get info
:parse-tree
)
4100 ;; Strip contents from HEADLINE.
4101 (let ((relative-level (org-export-get-relative-level headline info
)))
4102 (unless (> relative-level n
) headline
)))
4105 (defun org-export-collect-elements (type info
&optional predicate
)
4106 "Collect referenceable elements of a determined type.
4108 TYPE can be a symbol or a list of symbols specifying element
4109 types to search. Only elements with a caption are collected.
4111 INFO is a plist used as a communication channel.
4113 When non-nil, optional argument PREDICATE is a function accepting
4114 one argument, an element of type TYPE. It returns a non-nil
4115 value when that element should be collected.
4117 Return a list of all elements found, in order of appearance."
4119 (plist-get info
:parse-tree
) type
4121 (and (org-element-property :caption element
)
4122 (or (not predicate
) (funcall predicate element
))
4126 (defun org-export-collect-tables (info)
4127 "Build a list of tables.
4128 INFO is a plist used as a communication channel.
4130 Return a list of table elements with a caption."
4131 (org-export-collect-elements 'table info
))
4133 (defun org-export-collect-figures (info predicate
)
4134 "Build a list of figures.
4136 INFO is a plist used as a communication channel. PREDICATE is
4137 a function which accepts one argument: a paragraph element and
4138 whose return value is non-nil when that element should be
4141 A figure is a paragraph type element, with a caption, verifying
4142 PREDICATE. The latter has to be provided since a \"figure\" is
4143 a vague concept that may depend on back-end.
4145 Return a list of elements recognized as figures."
4146 (org-export-collect-elements 'paragraph info predicate
))
4148 (defun org-export-collect-listings (info)
4149 "Build a list of src blocks.
4151 INFO is a plist used as a communication channel.
4153 Return a list of src-block elements with a caption."
4154 (org-export-collect-elements 'src-block info
))
4159 ;; Here are various functions to retrieve information about the
4160 ;; neighbourhood of a given element or object. Neighbours of interest
4161 ;; are direct parent (`org-export-get-parent'), parent headline
4162 ;; (`org-export-get-parent-headline'), first element containing an
4163 ;; object, (`org-export-get-parent-element'), parent table
4164 ;; (`org-export-get-parent-table'), previous element or object
4165 ;; (`org-export-get-previous-element') and next element or object
4166 ;; (`org-export-get-next-element').
4168 ;; `org-export-get-genealogy' returns the full genealogy of a given
4169 ;; element or object, from closest parent to full parse tree.
4171 (defun org-export-get-parent (blob)
4172 "Return BLOB parent or nil.
4173 BLOB is the element or object considered."
4174 (org-element-property :parent blob
))
4176 (defun org-export-get-genealogy (blob)
4177 "Return full genealogy relative to a given element or object.
4179 BLOB is the element or object being considered.
4181 Ancestors are returned from closest to farthest, the last one
4182 being the full parse tree."
4183 (let (genealogy (parent blob
))
4184 (while (setq parent
(org-element-property :parent parent
))
4185 (push parent genealogy
))
4186 (nreverse genealogy
)))
4188 (defun org-export-get-parent-headline (blob)
4189 "Return BLOB parent headline or nil.
4190 BLOB is the element or object being considered."
4191 (let ((parent blob
))
4192 (while (and (setq parent
(org-element-property :parent parent
))
4193 (not (eq (org-element-type parent
) 'headline
))))
4196 (defun org-export-get-parent-element (object)
4197 "Return first element containing OBJECT or nil.
4198 OBJECT is the object to consider."
4199 (let ((parent object
))
4200 (while (and (setq parent
(org-element-property :parent parent
))
4201 (memq (org-element-type parent
) org-element-all-objects
)))
4204 (defun org-export-get-parent-table (object)
4205 "Return OBJECT parent table or nil.
4206 OBJECT is either a `table-cell' or `table-element' type object."
4207 (let ((parent object
))
4208 (while (and (setq parent
(org-element-property :parent parent
))
4209 (not (eq (org-element-type parent
) 'table
))))
4212 (defun org-export-get-previous-element (blob info
)
4213 "Return previous element or object.
4214 BLOB is an element or object. INFO is a plist used as
4215 a communication channel. Return previous exportable element or
4216 object, a string, or nil."
4220 (cond ((eq obj blob
) (throw 'exit prev
))
4221 ((memq obj
(plist-get info
:ignore-list
)))
4222 (t (setq prev obj
))))
4223 (org-element-contents (org-export-get-parent blob
))))))
4225 (defun org-export-get-next-element (blob info
)
4226 "Return next element or object.
4227 BLOB is an element or object. INFO is a plist used as
4228 a communication channel. Return next exportable element or
4229 object, a string, or nil."
4232 (unless (memq obj
(plist-get info
:ignore-list
))
4233 (throw 'found obj
)))
4234 (cdr (memq blob
(org-element-contents (org-export-get-parent blob
)))))
4240 ;; `org-export-translate' translates a string according to language
4241 ;; specified by LANGUAGE keyword or `org-export-language-setup'
4242 ;; variable and a specified charset. `org-export-dictionary' contains
4243 ;; the dictionary used for the translation.
4245 (defconst org-export-dictionary
4260 :ascii
"Notes de bas de page"
4261 :latin1
"Notes de bas de page"
4262 :utf-8
"Notes de bas de page"))
4265 :ascii
"Liste des programmes"
4266 :latin1
"Liste des programmes"
4267 :utf-8
"Liste des programmes"))
4270 :ascii
"Liste des tableaux"
4271 :latin1
"Liste des tableaux"
4272 :utf-8
"Liste des tableaux"))
4275 :ascii
"Programme %d :"
4276 :latin1
"Programme %d :"
4277 :utf-8
"Programme nº %d :"))
4280 :ascii
"Programme %d : %s"
4281 :latin1
"Programme %d : %s"
4282 :utf-8
"Programme nº %d : %s"))
4285 :ascii
"cf. section %s"
4286 :latin1
"cf. section %s"
4287 :utf-8
"cf. section %s"))
4290 :ascii
"Tableau %d :"
4291 :latin1
"Tableau %d :"
4292 :utf-8
"Tableau nº %d :"))
4295 :ascii
"Tableau %d : %s"
4296 :latin1
"Tableau %d : %s"
4297 :utf-8
"Tableau nº %d : %s"))
4298 ("Table of Contents"
4301 :latin1
"Table des matières"
4302 :utf-8
"Table des matières"))
4303 ("Unknown reference"
4305 :ascii
"Destination inconnue"
4306 :latin1
"Référence inconnue"
4307 :utf-8
"Référence inconnue")))
4308 "Dictionary for export engine.
4310 Alist whose CAR is the string to translate and CDR is an alist
4311 whose CAR is the language string and CDR is a plist whose
4312 properties are possible charsets and values translated terms.
4314 It is used as a database for `org-export-translate'. Since this
4315 function returns the string as-is if no translation was found,
4316 the variable only needs to record values different from the
4319 (defun org-export-translate (s encoding info
)
4320 "Translate string S according to language specification.
4322 ENCODING is a symbol among `:ascii', `:html', `:latex', `:latin1'
4323 and `:utf-8'. INFO is a plist used as a communication channel.
4325 Translation depends on `:language' property. Return the
4326 translated string. If no translation is found return S."
4327 (let ((lang (plist-get info
:language
))
4328 (translations (cdr (assoc s org-export-dictionary
))))
4329 (or (plist-get (cdr (assoc lang translations
)) encoding
) s
)))
4335 ;; `org-export-dispatch' is the standard interactive way to start an
4336 ;; export process. It uses `org-export-dispatch-ui' as a subroutine
4337 ;; for its interface. Most commons back-ends should have an entry in
4341 (defun org-export-dispatch ()
4342 "Export dispatcher for Org mode.
4344 It provides an access to common export related tasks in a buffer.
4345 Its interface comes in two flavours: standard and expert. While
4346 both share the same set of bindings, only the former displays the
4347 valid keys associations. Set `org-export-dispatch-use-expert-ui'
4348 to switch to one or the other.
4350 Return an error if key pressed has no associated command."
4352 (let* ((input (org-export-dispatch-ui
4353 (if (listp org-export-initial-scope
) org-export-initial-scope
4354 (list org-export-initial-scope
))
4355 org-export-dispatch-use-expert-ui
))
4356 (raw-key (car input
))
4357 (optns (cdr input
)))
4358 ;; Translate "C-a", "C-b"... into "a", "b"... Then take action
4359 ;; depending on user's key pressed.
4360 (case (if (< raw-key
27) (+ raw-key
96) raw-key
)
4361 ;; Allow to quit with "q" key.
4363 ;; Export with `e-ascii' back-end.
4365 (org-e-ascii-export-as-ascii
4366 (memq 'subtree optns
) (memq 'visible optns
) (memq 'body optns
)
4367 `(:ascii-charset
,(case raw-key
(?A
'ascii
) (?N
'latin1
) (t 'utf-8
)))))
4369 (org-e-ascii-export-to-ascii
4370 (memq 'subtree optns
) (memq 'visible optns
) (memq 'body optns
)
4371 `(:ascii-charset
,(case raw-key
(?a
'ascii
) (?n
'latin1
) (t 'utf-8
)))))
4372 ;; Export with `e-latex' back-end.
4373 (?L
(org-e-latex-export-as-latex
4374 (memq 'subtree optns
) (memq 'visible optns
) (memq 'body optns
)))
4376 (org-e-latex-export-to-latex
4377 (memq 'subtree optns
) (memq 'visible optns
) (memq 'body optns
)))
4379 (org-e-latex-export-to-pdf
4380 (memq 'subtree optns
) (memq 'visible optns
) (memq 'body optns
)))
4383 (org-e-latex-export-to-pdf
4384 (memq 'subtree optns
) (memq 'visible optns
) (memq 'body optns
))))
4385 ;; Export with `e-html' back-end.
4387 (org-e-html-export-as-html
4388 (memq 'subtree optns
) (memq 'visible optns
) (memq 'body optns
)))
4390 (org-e-html-export-to-html
4391 (memq 'subtree optns
) (memq 'visible optns
) (memq 'body optns
)))
4394 (org-e-html-export-to-html
4395 (memq 'subtree optns
) (memq 'visible optns
) (memq 'body optns
))))
4396 ;; Export with `e-odt' back-end.
4398 (org-e-odt-export-to-odt
4399 (memq 'subtree optns
) (memq 'visible optns
) (memq 'body optns
)))
4402 (org-e-odt-export-to-odt
4403 (memq 'subtree optns
) (memq 'visible optns
) (memq 'body optns
))))
4404 ;; Publishing facilities
4406 (org-e-publish-current-file (memq 'force optns
)))
4408 (org-e-publish-current-project (memq 'force optns
)))
4411 (assoc (org-icompleting-read
4412 "Publish project: " org-e-publish-project-alist nil t
)
4413 org-e-publish-project-alist
)))
4414 (org-e-publish project
(memq 'force optns
))))
4416 (org-e-publish-all (memq 'force optns
)))
4417 ;; Undefined command.
4418 (t (error "No command associated with key %s"
4419 (char-to-string raw-key
))))))
4421 (defun org-export-dispatch-ui (options expertp
)
4422 "Handle interface for `org-export-dispatch'.
4424 OPTIONS is a list containing current interactive options set for
4425 export. It can contain any of the following symbols:
4426 `body' toggles a body-only export
4427 `subtree' restricts export to current subtree
4428 `visible' restricts export to visible part of buffer.
4429 `force' force publishing files.
4431 EXPERTP, when non-nil, triggers expert UI. In that case, no help
4432 buffer is provided, but indications about currently active
4433 options are given in the prompt. Moreover, \[?] allows to switch
4434 back to standard interface.
4436 Return value is a list with key pressed as CAR and a list of
4437 final interactive export options as CDR."
4439 (format "---- (Options) -------------------------------------------
4441 \[1] Body only: %s [2] Export scope: %s
4442 \[3] Visible only: %s [4] Force publishing: %s
4445 --- (ASCII/Latin-1/UTF-8 Export) -------------------------
4447 \[a/n/u] to TXT file [A/N/U] to temporary buffer
4449 --- (HTML Export) ----------------------------------------
4451 \[h] to HTML file [b] ... and open it
4452 \[H] to temporary buffer
4454 --- (LaTeX Export) ---------------------------------------
4456 \[l] to TEX file [L] to temporary buffer
4457 \[p] to PDF file [d] ... and open it
4459 --- (ODF Export) -----------------------------------------
4461 \[o] to ODT file [O] ... and open it
4463 --- (Publish) --------------------------------------------
4465 \[F] current file [P] current project
4466 \[X] a project [E] every project"
4467 (if (memq 'body options
) "On " "Off")
4468 (if (memq 'subtree options
) "Subtree" "Buffer ")
4469 (if (memq 'visible options
) "On " "Off")
4470 (if (memq 'force options
) "On " "Off")))
4471 (standard-prompt "Export command: ")
4472 (expert-prompt (format "Export command (%s%s%s%s): "
4473 (if (memq 'body options
) "b" "-")
4474 (if (memq 'subtree options
) "s" "-")
4475 (if (memq 'visible options
) "v" "-")
4476 (if (memq 'force options
) "f" "-")))
4479 ;; Read a character from command input, toggling interactive
4480 ;; options when applicable. PROMPT is the displayed prompt,
4483 (let ((key (read-char-exclusive prompt
)))
4485 ;; Ignore non-standard characters (i.e. "M-a").
4486 ((not (characterp key
)) (org-export-dispatch-ui options expertp
))
4487 ;; Help key: Switch back to standard interface if
4488 ;; expert UI was active.
4489 ((eq key ??
) (org-export-dispatch-ui options nil
))
4490 ;; Toggle export options.
4491 ((memq key
'(?
1 ?
2 ?
3 ?
4))
4492 (org-export-dispatch-ui
4493 (let ((option (case key
(?
1 'body
) (?
2 'subtree
) (?
3 'visible
)
4495 (if (memq option options
) (remq option options
)
4496 (cons option options
)))
4498 ;; Action selected: Send key and options back to
4499 ;; `org-export-dispatch'.
4500 (t (cons key options
))))))))
4501 ;; With expert UI, just read key with a fancy prompt. In standard
4502 ;; UI, display an intrusive help buffer.
4503 (if expertp
(funcall handle-keypress expert-prompt
)
4504 (save-window-excursion
4505 (delete-other-windows)
4506 (with-output-to-temp-buffer "*Org Export/Publishing Help*" (princ help
))
4507 (org-fit-window-to-buffer
4508 (get-buffer-window "*Org Export/Publishing Help*"))
4509 (funcall handle-keypress standard-prompt
)))))
4512 (provide 'org-export
)
4513 ;;; org-export.el ends here