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-plannings nil
"p" org-export-with-planning
)
142 (:with-priority nil
"pri" org-export-with-priority
)
143 (:with-special-strings nil
"-" org-export-with-special-strings
)
144 (:with-sub-superscript nil
"^" org-export-with-sub-superscripts
)
145 (:with-toc nil
"toc" org-export-with-toc
)
146 (:with-tables nil
"|" org-export-with-tables
)
147 (:with-tags nil
"tags" org-export-with-tags
)
148 (:with-tasks nil
"tasks" org-export-with-tasks
)
149 (:with-timestamps nil
"<" org-export-with-timestamps
)
150 (:with-todo-keywords nil
"todo" org-export-with-todo-keywords
))
151 "Alist between export properties and ways to set them.
153 The CAR of the alist is the property name, and the CDR is a list
154 like (KEYWORD OPTION DEFAULT BEHAVIOUR) where:
156 KEYWORD is a string representing a buffer keyword, or nil. Each
157 property defined this way can also be set, during subtree
158 export, through an headline property named after the keyword
159 with the \"EXPORT_\" prefix (i.e. DATE keyword and EXPORT_DATE
161 OPTION is a string that could be found in an #+OPTIONS: line.
162 DEFAULT is the default value for the property.
163 BEHAVIOUR determine how Org should handle multiple keywords for
164 the same property. It is a symbol among:
165 nil Keep old value and discard the new one.
166 t Replace old value with the new one.
167 `space' Concatenate the values, separating them with a space.
168 `newline' Concatenate the values, separating them with
170 `split' Split values at white spaces, and cons them to the
173 KEYWORD and OPTION have precedence over DEFAULT.
175 All these properties should be back-end agnostic. For back-end
176 specific properties, define a similar variable named
177 `org-BACKEND-options-alist', replacing BACKEND with the name of
178 the appropriate back-end. You can also redefine properties
179 there, as they have precedence over these.")
181 (defconst org-export-special-keywords
182 '("SETUP_FILE" "OPTIONS" "MACRO")
183 "List of in-buffer keywords that require special treatment.
184 These keywords are not directly associated to a property. The
185 way they are handled must be hard-coded into
186 `org-export-get-inbuffer-options' function.")
188 (defconst org-export-filters-alist
189 '((:filter-bold . org-export-filter-bold-functions
)
190 (:filter-babel-call . org-export-filter-babel-call-functions
)
191 (:filter-center-block . org-export-filter-center-block-functions
)
192 (:filter-clock . org-export-filter-clock-functions
)
193 (:filter-code . org-export-filter-code-functions
)
194 (:filter-comment . org-export-filter-comment-functions
)
195 (:filter-comment-block . org-export-filter-comment-block-functions
)
196 (:filter-drawer . org-export-filter-drawer-functions
)
197 (:filter-dynamic-block . org-export-filter-dynamic-block-functions
)
198 (:filter-entity . org-export-filter-entity-functions
)
199 (:filter-example-block . org-export-filter-example-block-functions
)
200 (:filter-export-block . org-export-filter-export-block-functions
)
201 (:filter-export-snippet . org-export-filter-export-snippet-functions
)
202 (:filter-final-output . org-export-filter-final-output-functions
)
203 (:filter-fixed-width . org-export-filter-fixed-width-functions
)
204 (:filter-footnote-definition . org-export-filter-footnote-definition-functions
)
205 (:filter-footnote-reference . org-export-filter-footnote-reference-functions
)
206 (:filter-headline . org-export-filter-headline-functions
)
207 (:filter-horizontal-rule . org-export-filter-horizontal-rule-functions
)
208 (:filter-inline-babel-call . org-export-filter-inline-babel-call-functions
)
209 (:filter-inline-src-block . org-export-filter-inline-src-block-functions
)
210 (:filter-inlinetask . org-export-filter-inlinetask-functions
)
211 (:filter-italic . org-export-filter-italic-functions
)
212 (:filter-item . org-export-filter-item-functions
)
213 (:filter-keyword . org-export-filter-keyword-functions
)
214 (:filter-latex-environment . org-export-filter-latex-environment-functions
)
215 (:filter-latex-fragment . org-export-filter-latex-fragment-functions
)
216 (:filter-line-break . org-export-filter-line-break-functions
)
217 (:filter-link . org-export-filter-link-functions
)
218 (:filter-macro . org-export-filter-macro-functions
)
219 (:filter-paragraph . org-export-filter-paragraph-functions
)
220 (:filter-parse-tree . org-export-filter-parse-tree-functions
)
221 (:filter-plain-list . org-export-filter-plain-list-functions
)
222 (:filter-plain-text . org-export-filter-plain-text-functions
)
223 (:filter-planning . org-export-filter-planning-functions
)
224 (:filter-property-drawer . org-export-filter-property-drawer-functions
)
225 (:filter-quote-block . org-export-filter-quote-block-functions
)
226 (:filter-quote-section . org-export-filter-quote-section-functions
)
227 (:filter-radio-target . org-export-filter-radio-target-functions
)
228 (:filter-section . org-export-filter-section-functions
)
229 (:filter-special-block . org-export-filter-special-block-functions
)
230 (:filter-src-block . org-export-filter-src-block-functions
)
231 (:filter-statistics-cookie . org-export-filter-statistics-cookie-functions
)
232 (:filter-strike-through . org-export-filter-strike-through-functions
)
233 (:filter-subscript . org-export-filter-subscript-functions
)
234 (:filter-superscript . org-export-filter-superscript-functions
)
235 (:filter-table . org-export-filter-table-functions
)
236 (:filter-table-cell . org-export-filter-table-cell-functions
)
237 (:filter-table-row . org-export-filter-table-row-functions
)
238 (:filter-target . org-export-filter-target-functions
)
239 (:filter-timestamp . org-export-filter-timestamp-functions
)
240 (:filter-underline . org-export-filter-underline-functions
)
241 (:filter-verbatim . org-export-filter-verbatim-functions
)
242 (:filter-verse-block . org-export-filter-verse-block-functions
))
243 "Alist between filters properties and initial values.
245 The key of each association is a property name accessible through
246 the communication channel. Its value is a configurable global
247 variable defining initial filters.
249 This list is meant to install user specified filters. Back-end
250 developers may install their own filters using
251 `org-BACKEND-filters-alist', where BACKEND is the name of the
252 considered back-end. Filters defined there will always be
253 prepended to the current list, so they always get applied
256 (defconst org-export-default-inline-image-rule
260 '("png" "jpeg" "jpg" "gif" "tiff" "tif" "xbm"
261 "xpm" "pbm" "pgm" "ppm") t
))))
262 "Default rule for link matching an inline image.
263 This rule applies to links with no description. By default, it
264 will be considered as an inline image if it targets a local file
265 whose extension is either \"png\", \"jpeg\", \"jpg\", \"gif\",
266 \"tiff\", \"tif\", \"xbm\", \"xpm\", \"pbm\", \"pgm\" or \"ppm\".
267 See `org-export-inline-image-p' for more information about
272 ;;; User-configurable Variables
274 ;; Configuration for the masses.
276 ;; They should never be accessed directly, as their value is to be
277 ;; stored in a property list (cf. `org-export-options-alist').
278 ;; Back-ends will read their value from there instead.
280 (defgroup org-export nil
281 "Options for exporting Org mode files."
285 (defgroup org-export-general nil
286 "General options for export engine."
287 :tag
"Org Export General"
290 (defcustom org-export-with-archived-trees
'headline
291 "Whether sub-trees with the ARCHIVE tag should be exported.
293 This can have three different values:
294 nil Do not export, pretend this tree is not present.
295 t Do export the entire tree.
296 `headline' Only export the headline, but skip the tree below it.
298 This option can also be set with the #+OPTIONS line,
300 :group
'org-export-general
302 (const :tag
"Not at all" nil
)
303 (const :tag
"Headline only" 'headline
)
304 (const :tag
"Entirely" t
)))
306 (defcustom org-export-with-author t
307 "Non-nil means insert author name into the exported file.
308 This option can also be set with the #+OPTIONS line,
309 e.g. \"author:nil\"."
310 :group
'org-export-general
313 (defcustom org-export-with-clocks nil
314 "Non-nil means export CLOCK keywords.
315 This option can also be set with the #+OPTIONS line,
317 :group
'org-export-general
320 (defcustom org-export-with-creator
'comment
321 "Non-nil means the postamble should contain a creator sentence.
323 The sentence can be set in `org-export-creator-string' and
324 defaults to \"Generated by Org mode XX in Emacs XXX.\".
326 If the value is `comment' insert it as a comment."
327 :group
'org-export-general
329 (const :tag
"No creator sentence" nil
)
330 (const :tag
"Sentence as a comment" 'comment
)
331 (const :tag
"Insert the sentence" t
)))
333 (defcustom org-export-creator-string
334 (format "Generated by Org mode %s in Emacs %s."
335 (if (fboundp 'org-version
) (org-version) "(Unknown)")
337 "String to insert at the end of the generated document."
338 :group
'org-export-general
339 :type
'(string :tag
"Creator string"))
341 (defcustom org-export-with-drawers t
342 "Non-nil means export contents of standard drawers.
344 When t, all drawers are exported. This may also be a list of
345 drawer names to export. This variable doesn't apply to
348 This option can also be set with the #+OPTIONS line,
350 :group
'org-export-general
352 (const :tag
"All drawers" t
)
353 (const :tag
"None" nil
)
354 (repeat :tag
"Selected drawers"
355 (string :tag
"Drawer name"))))
357 (defcustom org-export-with-email nil
358 "Non-nil means insert author email into the exported file.
359 This option can also be set with the #+OPTIONS line,
361 :group
'org-export-general
364 (defcustom org-export-with-emphasize t
365 "Non-nil means interpret *word*, /word/, and _word_ as emphasized text.
367 If the export target supports emphasizing text, the word will be
368 typeset in bold, italic, or underlined, respectively. Not all
369 export backends support this.
371 This option can also be set with the #+OPTIONS line, e.g. \"*:nil\"."
372 :group
'org-export-general
375 (defcustom org-export-exclude-tags
'("noexport")
376 "Tags that exclude a tree from export.
378 All trees carrying any of these tags will be excluded from
379 export. This is without condition, so even subtrees inside that
380 carry one of the `org-export-select-tags' will be removed.
382 This option can also be set with the #+EXCLUDE_TAGS: keyword."
383 :group
'org-export-general
384 :type
'(repeat (string :tag
"Tag")))
386 (defcustom org-export-with-fixed-width t
387 "Non-nil means lines starting with \":\" will be in fixed width font.
389 This can be used to have pre-formatted text, fragments of code
391 : ;; Some Lisp examples
394 will be looking just like this in also HTML. See also the QUOTE
395 keyword. Not all export backends support this.
397 This option can also be set with the #+OPTIONS line, e.g. \"::nil\"."
398 :group
'org-export-translation
401 (defcustom org-export-with-footnotes t
402 "Non-nil means Org footnotes should be exported.
403 This option can also be set with the #+OPTIONS line,
405 :group
'org-export-general
408 (defcustom org-export-headline-levels
3
409 "The last level which is still exported as a headline.
411 Inferior levels will produce itemize lists when exported. Note
412 that a numeric prefix argument to an exporter function overrides
415 This option can also be set with the #+OPTIONS line, e.g. \"H:2\"."
416 :group
'org-export-general
419 (defcustom org-export-default-language
"en"
420 "The default language for export and clocktable translations, as a string.
421 This may have an association in
422 `org-clock-clocktable-language-setup'."
423 :group
'org-export-general
424 :type
'(string :tag
"Language"))
426 (defcustom org-export-preserve-breaks nil
427 "Non-nil means preserve all line breaks when exporting.
429 Normally, in HTML output paragraphs will be reformatted.
431 This option can also be set with the #+OPTIONS line,
433 :group
'org-export-general
436 (defcustom org-export-with-entities t
437 "Non-nil means interpret entities when exporting.
439 For example, HTML export converts \\alpha to α and \\AA to
442 For a list of supported names, see the constant `org-entities'
443 and the user option `org-entities-user'.
445 This option can also be set with the #+OPTIONS line,
447 :group
'org-export-general
450 (defcustom org-export-with-planning nil
451 "Non-nil means include planning info in export.
452 This option can also be set with the #+OPTIONS: line,
454 :group
'org-export-general
457 (defcustom org-export-with-priority nil
458 "Non-nil means include priority cookies in export.
460 When nil, remove priority cookies for 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.
752 Alist between back-end specific properties introduced in
753 communication channel and how their value are acquired. See
754 `org-export-options-alist' for more information about
755 structure of the values.
757 As an example, here is how the `e-ascii' back-end is defined:
759 \(org-export-define-backend e-ascii
760 \((babel-call . org-e-ascii-babel-call)
761 \(bold . org-e-ascii-bold)
762 \(center-block . org-e-ascii-center-block)
763 \(clock . org-e-ascii-clock)
764 \(code . org-e-ascii-code)
765 \(comment . org-e-ascii-comment)
766 \(comment-block . org-e-ascii-comment-block)
767 \(drawer . org-e-ascii-drawer)
768 \(dynamic-block . org-e-ascii-dynamic-block)
769 \(entity . org-e-ascii-entity)
770 \(example-block . org-e-ascii-example-block)
771 \(export-block . org-e-ascii-export-block)
772 \(export-snippet . org-e-ascii-export-snippet)
773 \(fixed-width . org-e-ascii-fixed-width)
774 \(footnote-definition . org-e-ascii-footnote-definition)
775 \(footnote-reference . org-e-ascii-footnote-reference)
776 \(headline . org-e-ascii-headline)
777 \(horizontal-rule . org-e-ascii-horizontal-rule)
778 \(inline-babel-call . org-e-ascii-inline-babel-call)
779 \(inline-src-block . org-e-ascii-inline-src-block)
780 \(inlinetask . org-e-ascii-inlinetask)
781 \(italic . org-e-ascii-italic)
782 \(item . org-e-ascii-item)
783 \(keyword . org-e-ascii-keyword)
784 \(latex-environment . org-e-ascii-latex-environment)
785 \(latex-fragment . org-e-ascii-latex-fragment)
786 \(line-break . org-e-ascii-line-break)
787 \(link . org-e-ascii-link)
788 \(macro . org-e-ascii-macro)
789 \(paragraph . org-e-ascii-paragraph)
790 \(plain-list . org-e-ascii-plain-list)
791 \(plain-text . org-e-ascii-plain-text)
792 \(planning . org-e-ascii-planning)
793 \(property-drawer . org-e-ascii-property-drawer)
794 \(quote-block . org-e-ascii-quote-block)
795 \(quote-section . org-e-ascii-quote-section)
796 \(radio-target . org-e-ascii-radio-target)
797 \(section . org-e-ascii-section)
798 \(special-block . org-e-ascii-special-block)
799 \(src-block . org-e-ascii-src-block)
800 \(statistics-cookie . org-e-ascii-statistics-cookie)
801 \(strike-through . org-e-ascii-strike-through)
802 \(subscript . org-e-ascii-subscript)
803 \(superscript . org-e-ascii-superscript)
804 \(table . org-e-ascii-table)
805 \(table-cell . org-e-ascii-table-cell)
806 \(table-row . org-e-ascii-table-row)
807 \(target . org-e-ascii-target)
808 \(template . org-e-ascii-template)
809 \(timestamp . org-e-ascii-timestamp)
810 \(underline . org-e-ascii-underline)
811 \(verbatim . org-e-ascii-verbatim)
812 \(verse-block . org-e-ascii-verse-block))
813 :export-block \"ASCII\"
814 :filters-alist ((:filter-headline . org-e-ascii-filter-headline-blank-lines)
815 \(:filter-section . org-e-ascii-filter-headline-blank-lines))
816 :options-alist ((:ascii-charset nil nil org-e-ascii-charset)))"
817 (declare (debug (&define name sexp
[&rest
[keywordp sexp
]] defbody
))
819 (let (filters options block-name
)
820 (while (keywordp (car body
))
822 (:export-block
(let ((names (pop body
)))
824 (if (consp names
) (mapcar 'upcase names
)
825 (list (upcase names
))))))
826 (:filters-alist
(setq filters
(pop body
)))
827 (:options-alist
(setq options
(pop body
)))
830 ;; Define translators.
831 (defvar ,(intern (format "org-%s-translate-alist" backend
)) ',translators
832 "Alist between element or object types and translators.")
835 `(defconst ,(intern (format "org-%s-options-alist" backend
)) ',options
836 ,(format "Alist between %s export properties and ways to set them.
837 See `org-export-options-alist' for more information on the
838 structure of the values."
842 `(defconst ,(intern (format "org-%s-filters-alist" backend
)) ',filters
843 "Alist between filters keywords and back-end specific filters.
844 See `org-export-filters-alist' for more information."))
845 ;; Tell parser to not parse EXPORT-BLOCK blocks.
849 (add-to-list 'org-element-block-name-alist
850 `(,name . org-element-export-block-parser
)))
852 ;; Splice in the body, if any.
855 (defmacro org-export-define-derived-backend
(child parent
&rest body
)
856 "Create a new back-end as a variant of an existing one.
858 CHILD is the name of the derived back-end. PARENT is the name of
861 BODY can start with pre-defined keyword arguments. The following
862 keywords are understood:
866 Alist of filters that will overwrite or complete filters
867 defined in PARENT back-end, if any.
871 Alist of buffer keywords or #+OPTIONS items that will
872 overwrite or complete those defined in PARENT back-end, if
877 Alist of element and object types and transcoders that will
878 overwrite or complete transcode table from PARENT back-end.
880 As an example, here is how one could define \"my-latex\" back-end
881 as a variant of `e-latex' back-end with a custom template
884 \(org-export-define-derived-backend my-latex e-latex
885 :translate-alist ((template . my-latex-template-fun)))
887 The back-end could then be called with, for example:
889 \(org-export-to-buffer 'my-latex \"*Test my-latex\")"
890 (declare (debug (&define name symbolp
[&rest keywordp sexp
] def-body
))
892 (let (filters options translate
)
893 (while (keywordp (car body
))
895 (:filters-alist
(setq filters
(pop body
)))
896 (:options-alist
(setq options
(pop body
)))
897 (:translate-alist
(setq translate
(pop body
)))
901 ,(let ((parent-filters (intern (format "org-%s-filters-alist" parent
))))
902 (when (or (boundp parent-filters
) filters
)
903 `(defconst ,(intern (format "org-%s-filters-alist" child
))
905 (and (boundp parent-filters
)
906 (copy-sequence (symbol-value parent-filters
))))
907 "Alist between filters keywords and back-end specific filters.
908 See `org-export-filters-alist' for more information.")))
910 ,(let ((parent-options (intern (format "org-%s-options-alist" parent
))))
911 (when (or (boundp parent-options
) options
)
912 `(defconst ,(intern (format "org-%s-options-alist" child
))
914 (and (boundp parent-options
)
915 (copy-sequence (symbol-value parent-options
))))
916 "Alist between LaTeX export properties and ways to set them.
917 See `org-export-options-alist' for more information on the
918 structure of the values.")))
919 ;; Define translators.
920 (defvar ,(intern (format "org-%s-translate-alist" child
))
924 (intern (format "org-%s-translate-alist" parent
)))))
925 "Alist between element or object types and translators.")
926 ;; Splice in the body, if any.
931 ;;; The Communication Channel
933 ;; During export process, every function has access to a number of
934 ;; properties. They are of two types:
936 ;; 1. Environment options are collected once at the very beginning of
937 ;; the process, out of the original buffer and configuration.
938 ;; Collecting them is handled by `org-export-get-environment'
941 ;; Most environment options are defined through the
942 ;; `org-export-options-alist' variable.
944 ;; 2. Tree properties are extracted directly from the parsed tree,
945 ;; just before export, by `org-export-collect-tree-properties'.
947 ;; Here is the full list of properties available during transcode
948 ;; process, with their category (option, tree or local) and their
951 ;; + `:author' :: Author's name.
952 ;; - category :: option
955 ;; + `:back-end' :: Current back-end used for transcoding.
956 ;; - category :: tree
959 ;; + `:creator' :: String to write as creation information.
960 ;; - category :: option
963 ;; + `:date' :: String to use as date.
964 ;; - category :: option
967 ;; + `:description' :: Description text for the current data.
968 ;; - category :: option
971 ;; + `:email' :: Author's email.
972 ;; - category :: option
975 ;; + `:exclude-tags' :: Tags for exclusion of subtrees from export
977 ;; - category :: option
978 ;; - type :: list of strings
980 ;; + `:footnote-definition-alist' :: Alist between footnote labels and
981 ;; their definition, as parsed data. Only non-inlined footnotes
982 ;; are represented in this alist. Also, every definition isn't
983 ;; guaranteed to be referenced in the parse tree. The purpose of
984 ;; this property is to preserve definitions from oblivion
985 ;; (i.e. when the parse tree comes from a part of the original
986 ;; buffer), it isn't meant for direct use in a back-end. To
987 ;; retrieve a definition relative to a reference, use
988 ;; `org-export-get-footnote-definition' instead.
989 ;; - category :: option
990 ;; - type :: alist (STRING . LIST)
992 ;; + `:headline-levels' :: Maximum level being exported as an
993 ;; headline. Comparison is done with the relative level of
994 ;; headlines in the parse tree, not necessarily with their
996 ;; - category :: option
999 ;; + `:headline-offset' :: Difference between relative and real level
1000 ;; of headlines in the parse tree. For example, a value of -1
1001 ;; means a level 2 headline should be considered as level
1002 ;; 1 (cf. `org-export-get-relative-level').
1003 ;; - category :: tree
1004 ;; - type :: integer
1006 ;; + `:headline-numbering' :: Alist between headlines and their
1007 ;; numbering, as a list of numbers
1008 ;; (cf. `org-export-get-headline-number').
1009 ;; - category :: tree
1010 ;; - type :: alist (INTEGER . LIST)
1012 ;; + `:id-alist' :: Alist between ID strings and destination file's
1013 ;; path, relative to current directory. It is used by
1014 ;; `org-export-resolve-id-link' to resolve ID links targeting an
1016 ;; - category :: option
1017 ;; - type :: alist (STRING . STRING)
1019 ;; + `:ignore-list' :: List of elements and objects that should be
1020 ;; ignored during export.
1021 ;; - category :: tree
1022 ;; - type :: list of elements and objects
1024 ;; + `:input-file' :: Full path to input file, if any.
1025 ;; - category :: option
1026 ;; - type :: string or nil
1028 ;; + `:keywords' :: List of keywords attached to data.
1029 ;; - category :: option
1032 ;; + `:language' :: Default language used for translations.
1033 ;; - category :: option
1036 ;; + `:parse-tree' :: Whole parse tree, available at any time during
1038 ;; - category :: option
1039 ;; - type :: list (as returned by `org-element-parse-buffer')
1041 ;; + `:preserve-breaks' :: Non-nil means transcoding should preserve
1043 ;; - category :: option
1044 ;; - type :: symbol (nil, t)
1046 ;; + `:section-numbers' :: Non-nil means transcoding should add
1047 ;; section numbers to headlines.
1048 ;; - category :: option
1049 ;; - type :: symbol (nil, t)
1051 ;; + `:select-tags' :: List of tags enforcing inclusion of sub-trees
1052 ;; in transcoding. When such a tag is present, subtrees without
1053 ;; it are de facto excluded from the process. See
1054 ;; `use-select-tags'.
1055 ;; - category :: option
1056 ;; - type :: list of strings
1058 ;; + `:target-list' :: List of targets encountered in the parse tree.
1059 ;; This is used to partly resolve "fuzzy" links
1060 ;; (cf. `org-export-resolve-fuzzy-link').
1061 ;; - category :: tree
1062 ;; - type :: list of strings
1064 ;; + `:time-stamp-file' :: Non-nil means transcoding should insert
1065 ;; a time stamp in the output.
1066 ;; - category :: option
1067 ;; - type :: symbol (nil, t)
1069 ;; + `:translate-alist' :: Alist between element and object types and
1070 ;; transcoding functions relative to the current back-end.
1071 ;; Special keys `template' and `plain-text' are also possible.
1072 ;; - category :: option
1073 ;; - type :: alist (SYMBOL . FUNCTION)
1075 ;; + `:with-archived-trees' :: Non-nil when archived subtrees should
1076 ;; also be transcoded. If it is set to the `headline' symbol,
1077 ;; only the archived headline's name is retained.
1078 ;; - category :: option
1079 ;; - type :: symbol (nil, t, `headline')
1081 ;; + `:with-author' :: Non-nil means author's name should be included
1083 ;; - category :: option
1084 ;; - type :: symbol (nil, t)
1086 ;; + `:with-clocks' :: Non-nild means clock keywords should be exported.
1087 ;; - category :: option
1088 ;; - type :: symbol (nil, t)
1090 ;; + `:with-creator' :: Non-nild means a creation sentence should be
1091 ;; inserted at the end of the transcoded string. If the value
1092 ;; is `comment', it should be commented.
1093 ;; - category :: option
1094 ;; - type :: symbol (`comment', nil, t)
1096 ;; + `:with-drawers' :: Non-nil means drawers should be exported. If
1097 ;; its value is a list of names, only drawers with such names
1098 ;; will be transcoded.
1099 ;; - category :: option
1100 ;; - type :: symbol (nil, t) or list of strings
1102 ;; + `:with-email' :: Non-nil means output should contain author's
1104 ;; - category :: option
1105 ;; - type :: symbol (nil, t)
1107 ;; + `:with-emphasize' :: Non-nil means emphasized text should be
1109 ;; - category :: option
1110 ;; - type :: symbol (nil, t)
1112 ;; + `:with-fixed-width' :: Non-nil if transcoder should interpret
1113 ;; strings starting with a colon as a fixed-with (verbatim) area.
1114 ;; - category :: option
1115 ;; - type :: symbol (nil, t)
1117 ;; + `:with-footnotes' :: Non-nil if transcoder should interpret
1119 ;; - category :: option
1120 ;; - type :: symbol (nil, t)
1122 ;; + `:with-plannings' :: Non-nil means transcoding should include
1124 ;; - category :: option
1125 ;; - type :: symbol (nil, t)
1127 ;; + `:with-priority' :: Non-nil means transcoding should include
1128 ;; priority cookies.
1129 ;; - category :: option
1130 ;; - type :: symbol (nil, t)
1132 ;; + `:with-special-strings' :: Non-nil means transcoding should
1133 ;; interpret special strings in plain text.
1134 ;; - category :: option
1135 ;; - type :: symbol (nil, t)
1137 ;; + `:with-sub-superscript' :: Non-nil means transcoding should
1138 ;; interpret subscript and superscript. With a value of "{}",
1139 ;; only interpret those using curly brackets.
1140 ;; - category :: option
1141 ;; - type :: symbol (nil, {}, t)
1143 ;; + `:with-tables' :: Non-nil means transcoding should interpret
1145 ;; - category :: option
1146 ;; - type :: symbol (nil, t)
1148 ;; + `:with-tags' :: Non-nil means transcoding should keep tags in
1149 ;; headlines. A `not-in-toc' value will remove them from the
1150 ;; table of contents, if any, nonetheless.
1151 ;; - category :: option
1152 ;; - type :: symbol (nil, t, `not-in-toc')
1154 ;; + `:with-tasks' :: Non-nil means transcoding should include
1155 ;; headlines with a TODO keyword. A `todo' value will only
1156 ;; include headlines with a todo type keyword while a `done'
1157 ;; value will do the contrary. If a list of strings is provided,
1158 ;; only tasks with keywords belonging to that list will be kept.
1159 ;; - category :: option
1160 ;; - type :: symbol (t, todo, done, nil) or list of strings
1162 ;; + `:with-timestamps' :: Non-nil means transcoding should include
1163 ;; time stamps. Special value `active' (resp. `inactive') ask to
1164 ;; export only active (resp. inactive) timestamps. Otherwise,
1165 ;; completely remove them.
1166 ;; - category :: option
1167 ;; - type :: symbol: (`active', `inactive', t, nil)
1169 ;; + `:with-toc' :: Non-nil means that a table of contents has to be
1170 ;; added to the output. An integer value limits its depth.
1171 ;; - category :: option
1172 ;; - type :: symbol (nil, t or integer)
1174 ;; + `:with-todo-keywords' :: Non-nil means transcoding should
1175 ;; include TODO keywords.
1176 ;; - category :: option
1177 ;; - type :: symbol (nil, t)
1180 ;;;; Environment Options
1182 ;; Environment options encompass all parameters defined outside the
1183 ;; scope of the parsed data. They come from five sources, in
1184 ;; increasing precedence order:
1186 ;; - Global variables,
1187 ;; - Buffer's attributes,
1188 ;; - Options keyword symbols,
1189 ;; - Buffer keywords,
1190 ;; - Subtree properties.
1192 ;; The central internal function with regards to environment options
1193 ;; is `org-export-get-environment'. It updates global variables with
1194 ;; "#+BIND:" keywords, then retrieve and prioritize properties from
1195 ;; the different sources.
1197 ;; The internal functions doing the retrieval are:
1198 ;; `org-export-get-global-options',
1199 ;; `org-export-get-buffer-attributes',
1200 ;; `org-export-parse-option-keyword',
1201 ;; `org-export-get-subtree-options' and
1202 ;; `org-export-get-inbuffer-options'
1204 ;; Also, `org-export-confirm-letbind' and `org-export-install-letbind'
1205 ;; take care of the part relative to "#+BIND:" keywords.
1207 (defun org-export-get-environment (&optional backend subtreep ext-plist
)
1208 "Collect export options from the current buffer.
1210 Optional argument BACKEND is a symbol specifying which back-end
1211 specific options to read, if any.
1213 When optional argument SUBTREEP is non-nil, assume the export is
1214 done against the current sub-tree.
1216 Third optional argument EXT-PLIST is a property list with
1217 external parameters overriding Org default settings, but still
1218 inferior to file-local settings."
1219 ;; First install #+BIND variables.
1220 (org-export-install-letbind-maybe)
1221 ;; Get and prioritize export options...
1223 ;; ... from global variables...
1224 (org-export-get-global-options backend
)
1225 ;; ... from buffer's attributes...
1226 (org-export-get-buffer-attributes)
1227 ;; ... from an external property list...
1229 ;; ... from in-buffer settings...
1230 (org-export-get-inbuffer-options
1232 (and buffer-file-name
(org-remove-double-quotes buffer-file-name
)))
1233 ;; ... and from subtree, when appropriate.
1234 (and subtreep
(org-export-get-subtree-options backend
))
1235 ;; Eventually install back-end symbol and its translation table.
1239 ,(let ((trans-alist (intern (format "org-%s-translate-alist" backend
))))
1240 (when (boundp trans-alist
) (symbol-value trans-alist
))))))
1242 (defun org-export-parse-option-keyword (options &optional backend
)
1243 "Parse an OPTIONS line and return values as a plist.
1244 Optional argument BACKEND is a symbol specifying which back-end
1245 specific items to read, if any."
1247 (append org-export-options-alist
1250 (format "org-%s-options-alist" backend
))))
1251 (and (boundp var
) (eval var
))))))
1252 ;; Build an alist between #+OPTION: item and property-name.
1255 (when (nth 2 e
) (cons (regexp-quote (nth 2 e
))
1260 (when (string-match (concat "\\(\\`\\|[ \t]\\)"
1262 ":\\(([^)\n]+)\\|[^ \t\n\r;,.]*\\)")
1264 (setq plist
(plist-put plist
1266 (car (read-from-string
1267 (match-string 2 options
)))))))
1271 (defun org-export-get-subtree-options (&optional backend
)
1272 "Get export options in subtree at point.
1273 Optional argument BACKEND is a symbol specifying back-end used
1274 for export. Return options as a plist."
1275 ;; For each buffer keyword, create an headline property setting the
1276 ;; same property in communication channel. The name for the property
1277 ;; is the keyword with "EXPORT_" appended to it.
1278 (org-with-wide-buffer
1280 ;; Make sure point is at an heading.
1281 (unless (org-at-heading-p) (org-back-to-heading t
))
1282 ;; Take care of EXPORT_TITLE. If it isn't defined, use headline's
1283 ;; title as its fallback value.
1284 (when (setq prop
(progn (looking-at org-todo-line-regexp
)
1285 (or (save-match-data
1286 (org-entry-get (point) "EXPORT_TITLE"))
1287 (org-match-string-no-properties 3))))
1291 (org-element-parse-secondary-string
1292 prop
(org-element-restriction 'keyword
)))))
1293 ;; EXPORT_OPTIONS are parsed in a non-standard way.
1294 (when (setq prop
(org-entry-get (point) "EXPORT_OPTIONS"))
1296 (nconc plist
(org-export-parse-option-keyword prop backend
))))
1297 ;; Handle other keywords.
1298 (let ((seen '("TITLE")))
1301 (let ((property (nth 1 option
)))
1302 (when (and property
(not (member property seen
)))
1303 (let* ((subtree-prop (concat "EXPORT_" property
))
1304 (value (org-entry-get (point) subtree-prop
)))
1305 (push property seen
)
1311 ;; Parse VALUE if required.
1312 (if (member property org-element-parsed-keywords
)
1313 (org-element-parse-secondary-string
1314 value
(org-element-restriction 'keyword
))
1316 ;; Also look for both general keywords and back-end specific
1317 ;; options if BACKEND is provided.
1318 (append (and backend
1320 (format "org-%s-options-alist" backend
))))
1321 (and (boundp var
) (symbol-value var
))))
1322 org-export-options-alist
)))
1326 (defun org-export-get-inbuffer-options (&optional backend files
)
1327 "Return current buffer export options, as a plist.
1329 Optional argument BACKEND, when non-nil, is a symbol specifying
1330 which back-end specific options should also be read in the
1333 Optional argument FILES is a list of setup files names read so
1334 far, used to avoid circular dependencies.
1336 Assume buffer is in Org mode. Narrowing, if any, is ignored."
1337 (org-with-wide-buffer
1338 (goto-char (point-min))
1339 (let ((case-fold-search t
) plist
)
1340 ;; 1. Special keywords, as in `org-export-special-keywords'.
1341 (let ((special-re (org-make-options-regexp org-export-special-keywords
)))
1342 (while (re-search-forward special-re nil t
)
1343 (let ((element (org-element-at-point)))
1344 (when (eq (org-element-type element
) 'keyword
)
1345 (let* ((key (org-element-property :key element
))
1346 (val (org-element-property :value element
))
1349 ((string= key
"SETUP_FILE")
1352 (org-remove-double-quotes (org-trim val
)))))
1353 ;; Avoid circular dependencies.
1354 (unless (member file files
)
1356 (insert (org-file-contents file
'noerror
))
1358 (org-export-get-inbuffer-options
1359 backend
(cons file files
))))))
1360 ((string= key
"OPTIONS")
1361 (org-export-parse-option-keyword val backend
))
1362 ((string= key
"MACRO")
1364 "^\\([-a-zA-Z0-9_]+\\)\\(?:[ \t]+\\(.*?\\)[ \t]*$\\)?"
1369 (downcase (match-string 1 val
)))))
1370 (value (org-match-string-no-properties 2 val
)))
1373 ;; Value will be evaled: do not parse it.
1374 ((string-match "\\`(eval\\>" value
)
1375 (list key
(list value
)))
1376 ;; Value has to be parsed for nested
1381 (let ((restr (org-element-restriction 'macro
)))
1382 (org-element-parse-secondary-string
1383 ;; If user explicitly asks for
1384 ;; a newline, be sure to preserve it
1385 ;; from further filling with
1386 ;; `hard-newline'. Also replace
1387 ;; "\\n" with "\n", "\\\n" with "\\n"
1389 (replace-regexp-in-string
1390 "\\(\\\\\\\\\\)n" "\\\\"
1391 (replace-regexp-in-string
1392 "\\(?:^\\|[^\\\\]\\)\\(\\\\n\\)"
1393 hard-newline value nil nil
1)
1396 (setq plist
(org-combine-plists plist prop
)))))))
1397 ;; 2. Standard options, as in `org-export-options-alist'.
1398 (let* ((all (append org-export-options-alist
1399 ;; Also look for back-end specific options
1400 ;; if BACKEND is defined.
1404 (format "org-%s-options-alist" backend
))))
1405 (and (boundp var
) (eval var
))))))
1406 ;; Build alist between keyword name and property name.
1409 (lambda (e) (when (nth 1 e
) (cons (nth 1 e
) (car e
))))
1411 ;; Build regexp matching all keywords associated to export
1412 ;; options. Note: the search is case insensitive.
1413 (opt-re (org-make-options-regexp
1414 (delq nil
(mapcar (lambda (e) (nth 1 e
)) all
)))))
1415 (goto-char (point-min))
1416 (while (re-search-forward opt-re nil t
)
1417 (let ((element (org-element-at-point)))
1418 (when (eq (org-element-type element
) 'keyword
)
1419 (let* ((key (org-element-property :key element
))
1420 (val (org-element-property :value element
))
1421 (prop (cdr (assoc key alist
)))
1422 (behaviour (nth 4 (assq prop all
))))
1426 ;; Handle value depending on specified BEHAVIOUR.
1429 (if (not (plist-get plist prop
)) (org-trim val
)
1430 (concat (plist-get plist prop
) " " (org-trim val
))))
1433 (concat (plist-get plist prop
) "\n" (org-trim val
))))
1435 `(,@(plist-get plist prop
) ,@(org-split-string val
)))
1437 (otherwise (if (not (plist-member plist prop
)) val
1438 (plist-get plist prop
))))))))))
1439 ;; Parse keywords specified in `org-element-parsed-keywords'.
1442 (let* ((prop (cdr (assoc key alist
)))
1443 (value (and prop
(plist-get plist prop
))))
1444 (when (stringp value
)
1448 (org-element-parse-secondary-string
1449 value
(org-element-restriction 'keyword
)))))))
1450 org-element-parsed-keywords
))
1451 ;; 3. Return final value.
1454 (defun org-export-get-buffer-attributes ()
1455 "Return properties related to buffer attributes, as a plist."
1456 (let ((visited-file (buffer-file-name (buffer-base-buffer))))
1458 ;; Store full path of input file name, or nil. For internal use.
1459 :input-file visited-file
1460 :title
(or (and visited-file
1461 (file-name-sans-extension
1462 (file-name-nondirectory visited-file
)))
1463 (buffer-name (buffer-base-buffer)))
1464 :footnote-definition-alist
1465 ;; Footnotes definitions must be collected in the original
1466 ;; buffer, as there's no insurance that they will still be in the
1467 ;; parse tree, due to possible narrowing.
1469 (org-with-wide-buffer
1470 (goto-char (point-min))
1471 (while (re-search-forward org-footnote-definition-re nil t
)
1472 (let ((def (org-footnote-at-definition-p)))
1474 (org-skip-whitespace)
1475 (push (cons (car def
)
1477 (narrow-to-region (point) (nth 2 def
))
1478 ;; Like `org-element-parse-buffer', but
1479 ;; makes sure the definition doesn't start
1480 ;; with a section element.
1481 (org-element--parse-elements
1482 (point-min) (point-max) nil nil nil nil
1483 (list 'org-data nil
))))
1487 ;; Collect id references.
1489 (org-with-wide-buffer
1490 (goto-char (point-min))
1491 (while (re-search-forward
1492 "\\[\\[id:\\(\\S-+?\\)\\]\\(?:\\[.*?\\]\\)?\\]" nil t
)
1493 (let* ((id (org-match-string-no-properties 1))
1494 (file (org-id-find-id-file id
)))
1495 (when file
(push (cons id
(file-relative-name file
)) alist
)))))
1497 :macro-modification-time
1499 (file-exists-p visited-file
)
1500 (concat "(eval (format-time-string \"$1\" '"
1501 (prin1-to-string (nth 5 (file-attributes visited-file
)))
1503 ;; Store input file name as a macro.
1504 :macro-input-file
(and visited-file
(file-name-nondirectory visited-file
))
1505 ;; `:macro-date', `:macro-time' and `:macro-property' could as
1506 ;; well be initialized as tree properties, since they don't
1507 ;; depend on buffer properties. Though, it may be more logical
1508 ;; to keep them close to other ":macro-" properties.
1509 :macro-date
"(eval (format-time-string \"$1\"))"
1510 :macro-time
"(eval (format-time-string \"$1\"))"
1511 :macro-property
"(eval (org-entry-get nil \"$1\" 'selective))")))
1513 (defun org-export-get-global-options (&optional backend
)
1514 "Return global export options as a plist.
1516 Optional argument BACKEND, if non-nil, is a symbol specifying
1517 which back-end specific export options should also be read in the
1519 (let ((all (append org-export-options-alist
1522 (format "org-%s-options-alist" backend
))))
1523 (and (boundp var
) (symbol-value var
))))))
1532 ;; Eval default value provided. If keyword is a member
1533 ;; of `org-element-parsed-keywords', parse it as
1534 ;; a secondary string before storing it.
1535 (let ((value (eval (nth 3 cell
))))
1536 (if (not (stringp value
)) value
1537 (let ((keyword (nth 1 cell
)))
1538 (if (not (member keyword org-element-parsed-keywords
)) value
1539 (org-element-parse-secondary-string
1540 value
(org-element-restriction 'keyword
)))))))))
1545 (defvar org-export-allow-BIND-local nil
)
1546 (defun org-export-confirm-letbind ()
1547 "Can we use #+BIND values during export?
1548 By default this will ask for confirmation by the user, to divert
1549 possible security risks."
1551 ((not org-export-allow-BIND
) nil
)
1552 ((eq org-export-allow-BIND t
) t
)
1553 ((local-variable-p 'org-export-allow-BIND-local
) org-export-allow-BIND-local
)
1554 (t (org-set-local 'org-export-allow-BIND-local
1555 (yes-or-no-p "Allow BIND values in this buffer? ")))))
1557 (defun org-export-install-letbind-maybe ()
1558 "Install the values from #+BIND lines as local variables.
1559 Variables must be installed before in-buffer options are
1562 (org-with-wide-buffer
1563 (goto-char (point-min))
1564 (while (re-search-forward (org-make-options-regexp '("BIND")) nil t
)
1565 (when (org-export-confirm-letbind)
1566 (push (read (concat "(" (org-match-string-no-properties 2) ")"))
1568 (while (setq pair
(pop letbind
))
1569 (org-set-local (car pair
) (nth 1 pair
)))))
1572 ;;;; Tree Properties
1574 ;; Tree properties are infromation extracted from parse tree. They
1575 ;; are initialized at the beginning of the transcoding process by
1576 ;; `org-export-collect-tree-properties'.
1578 ;; Dedicated functions focus on computing the value of specific tree
1579 ;; properties during initialization. Thus,
1580 ;; `org-export-populate-ignore-list' lists elements and objects that
1581 ;; should be skipped during export, `org-export-get-min-level' gets
1582 ;; the minimal exportable level, used as a basis to compute relative
1583 ;; level for headlines. Eventually
1584 ;; `org-export-collect-headline-numbering' builds an alist between
1585 ;; headlines and their numbering.
1587 (defun org-export-collect-tree-properties (data info
)
1588 "Extract tree properties from parse tree.
1590 DATA is the parse tree from which information is retrieved. INFO
1591 is a list holding export options.
1593 Following tree properties are set or updated:
1594 `:footnote-definition-alist' List of footnotes definitions in
1595 original buffer and current parse tree.
1597 `:headline-offset' Offset between true level of headlines and
1598 local level. An offset of -1 means an headline
1599 of level 2 should be considered as a level
1600 1 headline in the context.
1602 `:headline-numbering' Alist of all headlines as key an the
1603 associated numbering as value.
1605 `:ignore-list' List of elements that should be ignored during
1608 `:target-list' List of all targets in the parse tree.
1610 Return updated plist."
1611 ;; Install the parse tree in the communication channel, in order to
1612 ;; use `org-export-get-genealogy' and al.
1613 (setq info
(plist-put info
:parse-tree data
))
1614 ;; Get the list of elements and objects to ignore, and put it into
1615 ;; `:ignore-list'. Do not overwrite any user ignore that might have
1616 ;; been done during parse tree filtering.
1620 (append (org-export-populate-ignore-list data info
)
1621 (plist-get info
:ignore-list
))))
1622 ;; Compute `:headline-offset' in order to be able to use
1623 ;; `org-export-get-relative-level'.
1626 :headline-offset
(- 1 (org-export-get-min-level data info
))))
1627 ;; Update footnotes definitions list with definitions in parse tree.
1628 ;; This is required since buffer expansion might have modified
1629 ;; boundaries of footnote definitions contained in the parse tree.
1630 ;; This way, definitions in `footnote-definition-alist' are bound to
1631 ;; match those in the parse tree.
1632 (let ((defs (plist-get info
:footnote-definition-alist
)))
1634 data
'footnote-definition
1636 (push (cons (org-element-property :label fn
)
1637 `(org-data nil
,@(org-element-contents fn
)))
1639 (setq info
(plist-put info
:footnote-definition-alist defs
)))
1640 ;; Properties order doesn't matter: get the rest of the tree
1645 data
'(keyword target
)
1647 (when (or (eq (org-element-type blob
) 'target
)
1648 (string= (org-element-property :key blob
) "TARGET"))
1650 :headline-numbering
,(org-export-collect-headline-numbering data info
))
1653 (defun org-export-get-min-level (data options
)
1654 "Return minimum exportable headline's level in DATA.
1655 DATA is parsed tree as returned by `org-element-parse-buffer'.
1656 OPTIONS is a plist holding export options."
1658 (let ((min-level 10000))
1661 (when (and (eq (org-element-type blob
) 'headline
)
1662 (not (memq blob
(plist-get options
:ignore-list
))))
1664 (min (org-element-property :level blob
) min-level
)))
1665 (when (= min-level
1) (throw 'exit
1)))
1666 (org-element-contents data
))
1667 ;; If no headline was found, for the sake of consistency, set
1668 ;; minimum level to 1 nonetheless.
1669 (if (= min-level
10000) 1 min-level
))))
1671 (defun org-export-collect-headline-numbering (data options
)
1672 "Return numbering of all exportable headlines in a parse tree.
1674 DATA is the parse tree. OPTIONS is the plist holding export
1677 Return an alist whose key is an headline and value is its
1678 associated numbering \(in the shape of a list of numbers\)."
1679 (let ((numbering (make-vector org-export-max-depth
0)))
1684 (let ((relative-level
1685 (1- (org-export-get-relative-level headline options
))))
1688 (loop for n across numbering
1689 for idx from
0 to org-export-max-depth
1690 when
(< idx relative-level
) collect n
1691 when
(= idx relative-level
) collect
(aset numbering idx
(1+ n
))
1692 when
(> idx relative-level
) do
(aset numbering idx
0)))))
1695 (defun org-export-populate-ignore-list (data options
)
1696 "Return list of elements and objects to ignore during export.
1697 DATA is the parse tree to traverse. OPTIONS is the plist holding
1700 walk-data
; for byte-compiler.
1703 (lambda (data options selected
)
1704 ;; Collect ignored elements or objects into IGNORE-LIST.
1707 (if (org-export--skip-p el options selected
) (push el ignore
)
1708 (let ((type (org-element-type el
)))
1709 (if (and (eq (plist-get options
:with-archived-trees
)
1711 (eq (org-element-type el
) 'headline
)
1712 (org-element-property :archivedp el
))
1713 ;; If headline is archived but tree below has
1714 ;; to be skipped, add it to ignore list.
1715 (mapc (lambda (e) (push e ignore
))
1716 (org-element-contents el
))
1717 ;; Move into recursive objects/elements.
1718 (when (org-element-contents el
)
1719 (funcall walk-data el options selected
))))))
1720 (org-element-contents data
))))))
1721 ;; Main call. First find trees containing a select tag, if any.
1722 (funcall walk-data data options
(org-export--selected-trees data options
))
1726 (defun org-export--selected-trees (data info
)
1727 "Return list of headlines containing a select tag in their tree.
1728 DATA is parsed data as returned by `org-element-parse-buffer'.
1729 INFO is a plist holding export options."
1730 (let* (selected-trees
1731 walk-data
; for byte-compiler.
1734 (lambda (data genealogy
)
1735 (case (org-element-type data
)
1736 (org-data (mapc (lambda (el) (funcall walk-data el genealogy
))
1737 (org-element-contents data
)))
1739 (let ((tags (org-element-property :tags data
)))
1740 (if (loop for tag in
(plist-get info
:select-tags
)
1741 thereis
(member tag tags
))
1742 ;; When a select tag is found, mark full
1743 ;; genealogy and every headline within the tree
1745 (setq selected-trees
1748 (org-element-map data
'headline
'identity
)
1750 ;; Else, continue searching in tree, recursively.
1752 (lambda (el) (funcall walk-data el
(cons data genealogy
)))
1753 (org-element-contents data
))))))))))
1754 (funcall walk-data data nil
) selected-trees
))
1756 (defun org-export--skip-p (blob options selected
)
1757 "Non-nil when element or object BLOB should be skipped during export.
1758 OPTIONS is the plist holding export options. SELECTED, when
1759 non-nil, is a list of headlines belonging to a tree with a select
1761 (case (org-element-type blob
)
1764 (let ((with-tasks (plist-get options
:with-tasks
))
1765 (todo (org-element-property :todo-keyword blob
))
1766 (todo-type (org-element-property :todo-type blob
))
1767 (archived (plist-get options
:with-archived-trees
))
1768 (tags (org-element-property :tags blob
)))
1770 ;; Ignore subtrees with an exclude tag.
1771 (loop for k in
(plist-get options
:exclude-tags
)
1772 thereis
(member k tags
))
1773 ;; When a select tag is present in the buffer, ignore any tree
1775 (and selected
(not (memq blob selected
)))
1776 ;; Ignore commented sub-trees.
1777 (org-element-property :commentedp blob
)
1778 ;; Ignore archived subtrees if `:with-archived-trees' is nil.
1779 (and (not archived
) (org-element-property :archivedp blob
))
1780 ;; Ignore tasks, if specified by `:with-tasks' property.
1782 (or (not with-tasks
)
1783 (and (memq with-tasks
'(todo done
))
1784 (not (eq todo-type with-tasks
)))
1785 (and (consp with-tasks
) (not (member todo with-tasks
))))))))
1788 (case (plist-get options
:with-timestamps
)
1789 ;; No timestamp allowed.
1791 ;; Only active timestamps allowed and the current one isn't
1794 (not (memq (org-element-property :type blob
)
1795 '(active active-range
))))
1796 ;; Only inactive timestamps allowed and the current one isn't
1799 (not (memq (org-element-property :type blob
)
1800 '(inactive inactive-range
))))))
1803 (or (not (plist-get options
:with-drawers
))
1804 (and (consp (plist-get options
:with-drawers
))
1805 (not (member (org-element-property :drawer-name blob
)
1806 (plist-get options
:with-drawers
))))))
1808 (table-row (org-export-table-row-is-special-p blob options
))
1809 ;; Check table-cell.
1811 (and (org-export-table-has-special-column-p
1812 (org-export-get-parent-table blob
))
1813 (not (org-export-get-previous-element blob
))))
1815 (clock (not (plist-get options
:with-clocks
)))
1817 (planning (not (plist-get options
:with-plannings
)))))
1823 ;; `org-export-data' reads a parse tree (obtained with, i.e.
1824 ;; `org-element-parse-buffer') and transcodes it into a specified
1825 ;; back-end output. It takes care of filtering out elements or
1826 ;; objects according to export options and organizing the output blank
1827 ;; lines and white space are preserved.
1829 ;; Internally, three functions handle the filtering of objects and
1830 ;; elements during the export. In particular,
1831 ;; `org-export-ignore-element' marks an element or object so future
1832 ;; parse tree traversals skip it, `org-export-interpret-p' tells which
1833 ;; elements or objects should be seen as real Org syntax and
1834 ;; `org-export-expand' transforms the others back into their original
1837 ;; `org-export-transcoder' is an accessor returning appropriate
1838 ;; translator function for a given element or object.
1840 (defun org-export-transcoder (blob info
)
1841 "Return appropriate transcoder for BLOB.
1842 INFO is a plist containing export directives."
1843 (let ((type (org-element-type blob
)))
1844 ;; Return contents only for complete parse trees.
1845 (if (eq type
'org-data
) (lambda (blob contents info
) contents
)
1846 (let ((transcoder (cdr (assq type
(plist-get info
:translate-alist
)))))
1847 (and (functionp transcoder
) transcoder
)))))
1849 (defun org-export-data (data info
)
1850 "Convert DATA into current back-end format.
1852 DATA is a parse tree, an element or an object or a secondary
1853 string. INFO is a plist holding export options.
1855 Return transcoded string."
1856 (let* ((type (org-element-type data
))
1859 ;; Ignored element/object.
1860 ((memq data
(plist-get info
:ignore-list
)) nil
)
1862 ((eq type
'plain-text
)
1863 (org-export-filter-apply-functions
1864 (plist-get info
:filter-plain-text
)
1865 (let ((transcoder (org-export-transcoder data info
)))
1866 (if transcoder
(funcall transcoder data info
) data
))
1868 ;; Uninterpreted element/object: change it back to Org
1869 ;; syntax and export again resulting raw string.
1870 ((not (org-export-interpret-p data info
))
1874 (mapconcat (lambda (blob) (org-export-data blob info
))
1875 (org-element-contents data
)
1878 ;; Secondary string.
1880 (mapconcat (lambda (obj) (org-export-data obj info
)) data
""))
1881 ;; Element/Object without contents or, as a special case,
1882 ;; headline with archive tag and archived trees restricted
1884 ((or (not (org-element-contents data
))
1885 (and (eq type
'headline
)
1886 (eq (plist-get info
:with-archived-trees
) 'headline
)
1887 (org-element-property :archivedp data
)))
1888 (let ((transcoder (org-export-transcoder data info
)))
1889 (and (functionp transcoder
) (funcall transcoder data nil info
))))
1890 ;; Element/Object with contents.
1892 (let ((transcoder (org-export-transcoder data info
)))
1894 (let* ((greaterp (memq type org-element-greater-elements
))
1895 (objectp (and (not greaterp
)
1896 (memq type org-element-recursive-objects
)))
1899 (lambda (element) (org-export-data element info
))
1900 (org-element-contents
1901 (if (or greaterp objectp
) data
1902 ;; Elements directly containing objects
1903 ;; must have their indentation normalized
1905 (org-element-normalize-contents
1907 ;; When normalizing contents of the first
1908 ;; paragraph in an item or a footnote
1909 ;; definition, ignore first line's
1910 ;; indentation: there is none and it
1911 ;; might be misleading.
1912 (when (eq type
'paragraph
)
1913 (let ((parent (org-export-get-parent data
)))
1914 (and (eq (car (org-element-contents parent
))
1916 (memq (org-element-type parent
)
1917 '(footnote-definition item
))))))))
1919 (funcall transcoder data
1920 (if greaterp
(org-element-normalize-string contents
)
1925 ((memq type
'(org-data plain-text nil
)) results
)
1926 ;; Append the same white space between elements or objects as in
1927 ;; the original buffer, and call appropriate filters.
1930 (org-export-filter-apply-functions
1931 (plist-get info
(intern (format ":filter-%s" type
)))
1932 (let ((post-blank (or (org-element-property :post-blank data
) 0)))
1933 (if (memq type org-element-all-elements
)
1934 (concat (org-element-normalize-string results
)
1935 (make-string post-blank ?
\n))
1936 (concat results
(make-string post-blank ?
))))
1938 ;; Eventually return string.
1941 (defun org-export-interpret-p (blob info
)
1942 "Non-nil if element or object BLOB should be interpreted as Org syntax.
1943 Check is done according to export options INFO, stored as
1945 (case (org-element-type blob
)
1947 (entity (plist-get info
:with-entities
))
1949 (emphasis (plist-get info
:with-emphasize
))
1950 ;; ... fixed-width areas.
1951 (fixed-width (plist-get info
:with-fixed-width
))
1953 ((footnote-definition footnote-reference
)
1954 (plist-get info
:with-footnotes
))
1955 ;; ... sub/superscripts...
1956 ((subscript superscript
)
1957 (let ((sub/super-p
(plist-get info
:with-sub-superscript
)))
1958 (if (eq sub
/super-p
'{})
1959 (org-element-property :use-brackets-p blob
)
1962 (table (plist-get info
:with-tables
))
1965 (defun org-export-expand (blob contents
)
1966 "Expand a parsed element or object to its original state.
1967 BLOB is either an element or an object. CONTENTS is its
1968 contents, as a string or nil."
1970 (intern (format "org-element-%s-interpreter" (org-element-type blob
)))
1973 (defun org-export-ignore-element (element info
)
1974 "Add ELEMENT to `:ignore-list' in INFO.
1976 Any element in `:ignore-list' will be skipped when using
1977 `org-element-map'. INFO is modified by side effects."
1978 (plist-put info
:ignore-list
(cons element
(plist-get info
:ignore-list
))))
1982 ;;; The Filter System
1984 ;; Filters allow end-users to tweak easily the transcoded output.
1985 ;; They are the functional counterpart of hooks, as every filter in
1986 ;; a set is applied to the return value of the previous one.
1988 ;; Every set is back-end agnostic. Although, a filter is always
1989 ;; called, in addition to the string it applies to, with the back-end
1990 ;; used as argument, so it's easy for the end-user to add back-end
1991 ;; specific filters in the set. The communication channel, as
1992 ;; a plist, is required as the third argument.
1994 ;; From the developer side, filters sets can be installed in the
1995 ;; process with the help of `org-BACKEND-filters-alist' variable.
1996 ;; Each association has a key among the following symbols and
1997 ;; a function or a list of functions as value.
1999 ;; - `:filter-parse-tree' applies directly on the complete parsed
2000 ;; tree. It's the only filters set that doesn't apply to a string.
2001 ;; Users can set it through `org-export-filter-parse-tree-functions'
2004 ;; - `:filter-final-output' applies to the final transcoded string.
2005 ;; Users can set it with `org-export-filter-final-output-functions'
2008 ;; - `:filter-plain-text' applies to any string not recognized as Org
2009 ;; syntax. `org-export-filter-plain-text-functions' allows users to
2012 ;; - `:filter-TYPE' applies on the string returned after an element or
2013 ;; object of type TYPE has been transcoded. An user can modify
2014 ;; `org-export-filter-TYPE-functions'
2016 ;; All filters sets are applied with
2017 ;; `org-export-filter-apply-functions' function. Filters in a set are
2018 ;; applied in a LIFO fashion. It allows developers to be sure that
2019 ;; their filters will be applied first.
2021 ;; Filters properties are installed in communication channel with
2022 ;; `org-export-install-filters' function.
2024 ;; Eventually, a hook (`org-export-before-parsing-hook') is run just
2025 ;; before parsing to allow for heavy structure modifications.
2028 ;;;; Before Parsing Hook
2030 (defvar org-export-before-parsing-hook nil
2031 "Hook run before parsing an export buffer.
2032 This is run after include keywords have been expanded and Babel
2033 code executed, on a copy of original buffer's area being
2034 exported. Visibility is the same as in the original one. Point
2035 is left at the beginning of the new one.")
2038 ;;;; Special Filters
2040 (defvar org-export-filter-parse-tree-functions nil
2041 "List of functions applied to the parsed tree.
2042 Each filter is called with three arguments: the parse tree, as
2043 returned by `org-element-parse-buffer', the back-end, as
2044 a symbol, and the communication channel, as a plist. It must
2045 return the modified parse tree to transcode.")
2047 (defvar org-export-filter-final-output-functions nil
2048 "List of functions applied to the transcoded string.
2049 Each filter is called with three arguments: the full transcoded
2050 string, the back-end, as a symbol, and the communication channel,
2051 as a plist. It must return a string that will be used as the
2052 final export output.")
2054 (defvar org-export-filter-plain-text-functions nil
2055 "List of functions applied to plain text.
2056 Each filter is called with three arguments: a string which
2057 contains no Org syntax, the back-end, as a symbol, and the
2058 communication channel, as a plist. It must return a string or
2062 ;;;; Elements Filters
2064 (defvar org-export-filter-center-block-functions nil
2065 "List of functions applied to a transcoded center block.
2066 Each filter is called with three arguments: the transcoded data,
2067 as a string, the back-end, as a symbol, and the communication
2068 channel, as a plist. It must return a string or nil.")
2070 (defvar org-export-filter-clock-functions nil
2071 "List of functions applied to a transcoded clock.
2072 Each filter is called with three arguments: the transcoded data,
2073 as a string, the back-end, as a symbol, and the communication
2074 channel, as a plist. It must return a string or nil.")
2076 (defvar org-export-filter-drawer-functions nil
2077 "List of functions applied to a transcoded drawer.
2078 Each filter is called with three arguments: the transcoded data,
2079 as a string, the back-end, as a symbol, and the communication
2080 channel, as a plist. It must return a string or nil.")
2082 (defvar org-export-filter-dynamic-block-functions nil
2083 "List of functions applied to a transcoded dynamic-block.
2084 Each filter is called with three arguments: the transcoded data,
2085 as a string, the back-end, as a symbol, and the communication
2086 channel, as a plist. It must return a string or nil.")
2088 (defvar org-export-filter-headline-functions nil
2089 "List of functions applied to a transcoded headline.
2090 Each filter is called with three arguments: the transcoded data,
2091 as a string, the back-end, as a symbol, and the communication
2092 channel, as a plist. It must return a string or nil.")
2094 (defvar org-export-filter-inlinetask-functions nil
2095 "List of functions applied to a transcoded inlinetask.
2096 Each filter is called with three arguments: the transcoded data,
2097 as a string, the back-end, as a symbol, and the communication
2098 channel, as a plist. It must return a string or nil.")
2100 (defvar org-export-filter-plain-list-functions nil
2101 "List of functions applied to a transcoded plain-list.
2102 Each filter is called with three arguments: the transcoded data,
2103 as a string, the back-end, as a symbol, and the communication
2104 channel, as a plist. It must return a string or nil.")
2106 (defvar org-export-filter-item-functions nil
2107 "List of functions applied to a transcoded item.
2108 Each filter is called with three arguments: the transcoded data,
2109 as a string, the back-end, as a symbol, and the communication
2110 channel, as a plist. It must return a string or nil.")
2112 (defvar org-export-filter-comment-functions nil
2113 "List of functions applied to a transcoded comment.
2114 Each filter is called with three arguments: the transcoded data,
2115 as a string, the back-end, as a symbol, and the communication
2116 channel, as a plist. It must return a string or nil.")
2118 (defvar org-export-filter-comment-block-functions nil
2119 "List of functions applied to a transcoded comment-comment.
2120 Each filter is called with three arguments: the transcoded data,
2121 as a string, the back-end, as a symbol, and the communication
2122 channel, as a plist. It must return a string or nil.")
2124 (defvar org-export-filter-example-block-functions nil
2125 "List of functions applied to a transcoded example-block.
2126 Each filter is called with three arguments: the transcoded data,
2127 as a string, the back-end, as a symbol, and the communication
2128 channel, as a plist. It must return a string or nil.")
2130 (defvar org-export-filter-export-block-functions nil
2131 "List of functions applied to a transcoded export-block.
2132 Each filter is called with three arguments: the transcoded data,
2133 as a string, the back-end, as a symbol, and the communication
2134 channel, as a plist. It must return a string or nil.")
2136 (defvar org-export-filter-fixed-width-functions nil
2137 "List of functions applied to a transcoded fixed-width.
2138 Each filter is called with three arguments: the transcoded data,
2139 as a string, the back-end, as a symbol, and the communication
2140 channel, as a plist. It must return a string or nil.")
2142 (defvar org-export-filter-footnote-definition-functions nil
2143 "List of functions applied to a transcoded footnote-definition.
2144 Each filter is called with three arguments: the transcoded data,
2145 as a string, the back-end, as a symbol, and the communication
2146 channel, as a plist. It must return a string or nil.")
2148 (defvar org-export-filter-horizontal-rule-functions nil
2149 "List of functions applied to a transcoded horizontal-rule.
2150 Each filter is called with three arguments: the transcoded data,
2151 as a string, the back-end, as a symbol, and the communication
2152 channel, as a plist. It must return a string or nil.")
2154 (defvar org-export-filter-keyword-functions nil
2155 "List of functions applied to a transcoded keyword.
2156 Each filter is called with three arguments: the transcoded data,
2157 as a string, the back-end, as a symbol, and the communication
2158 channel, as a plist. It must return a string or nil.")
2160 (defvar org-export-filter-latex-environment-functions nil
2161 "List of functions applied to a transcoded latex-environment.
2162 Each filter is called with three arguments: the transcoded data,
2163 as a string, the back-end, as a symbol, and the communication
2164 channel, as a plist. It must return a string or nil.")
2166 (defvar org-export-filter-babel-call-functions nil
2167 "List of functions applied to a transcoded babel-call.
2168 Each filter is called with three arguments: the transcoded data,
2169 as a string, the back-end, as a symbol, and the communication
2170 channel, as a plist. It must return a string or nil.")
2172 (defvar org-export-filter-paragraph-functions nil
2173 "List of functions applied to a transcoded paragraph.
2174 Each filter is called with three arguments: the transcoded data,
2175 as a string, the back-end, as a symbol, and the communication
2176 channel, as a plist. It must return a string or nil.")
2178 (defvar org-export-filter-planning-functions nil
2179 "List of functions applied to a transcoded planning.
2180 Each filter is called with three arguments: the transcoded data,
2181 as a string, the back-end, as a symbol, and the communication
2182 channel, as a plist. It must return a string or nil.")
2184 (defvar org-export-filter-property-drawer-functions nil
2185 "List of functions applied to a transcoded property-drawer.
2186 Each filter is called with three arguments: the transcoded data,
2187 as a string, the back-end, as a symbol, and the communication
2188 channel, as a plist. It must return a string or nil.")
2190 (defvar org-export-filter-quote-block-functions nil
2191 "List of functions applied to a transcoded quote block.
2192 Each filter is called with three arguments: the transcoded quote
2193 data, as a string, the back-end, as a symbol, and the
2194 communication channel, as a plist. It must return a string or
2197 (defvar org-export-filter-quote-section-functions nil
2198 "List of functions applied to a transcoded quote-section.
2199 Each filter is called with three arguments: the transcoded data,
2200 as a string, the back-end, as a symbol, and the communication
2201 channel, as a plist. It must return a string or nil.")
2203 (defvar org-export-filter-section-functions nil
2204 "List of functions applied to a transcoded section.
2205 Each filter is called with three arguments: the transcoded data,
2206 as a string, the back-end, as a symbol, and the communication
2207 channel, as a plist. It must return a string or nil.")
2209 (defvar org-export-filter-special-block-functions nil
2210 "List of functions applied to a transcoded special block.
2211 Each filter is called with three arguments: the transcoded data,
2212 as a string, the back-end, as a symbol, and the communication
2213 channel, as a plist. It must return a string or nil.")
2215 (defvar org-export-filter-src-block-functions nil
2216 "List of functions applied to a transcoded src-block.
2217 Each filter is called with three arguments: the transcoded data,
2218 as a string, the back-end, as a symbol, and the communication
2219 channel, as a plist. It must return a string or nil.")
2221 (defvar org-export-filter-table-functions nil
2222 "List of functions applied to a transcoded table.
2223 Each filter is called with three arguments: the transcoded data,
2224 as a string, the back-end, as a symbol, and the communication
2225 channel, as a plist. It must return a string or nil.")
2227 (defvar org-export-filter-table-cell-functions nil
2228 "List of functions applied to a transcoded table-cell.
2229 Each filter is called with three arguments: the transcoded data,
2230 as a string, the back-end, as a symbol, and the communication
2231 channel, as a plist. It must return a string or nil.")
2233 (defvar org-export-filter-table-row-functions nil
2234 "List of functions applied to a transcoded table-row.
2235 Each filter is called with three arguments: the transcoded data,
2236 as a string, the back-end, as a symbol, and the communication
2237 channel, as a plist. It must return a string or nil.")
2239 (defvar org-export-filter-verse-block-functions nil
2240 "List of functions applied to a transcoded verse block.
2241 Each filter is called with three arguments: the transcoded data,
2242 as a string, the back-end, as a symbol, and the communication
2243 channel, as a plist. It must return a string or nil.")
2246 ;;;; Objects Filters
2248 (defvar org-export-filter-bold-functions nil
2249 "List of functions applied to transcoded bold text.
2250 Each filter is called with three arguments: the transcoded data,
2251 as a string, the back-end, as a symbol, and the communication
2252 channel, as a plist. It must return a string or nil.")
2254 (defvar org-export-filter-code-functions nil
2255 "List of functions applied to transcoded code text.
2256 Each filter is called with three arguments: the transcoded data,
2257 as a string, the back-end, as a symbol, and the communication
2258 channel, as a plist. It must return a string or nil.")
2260 (defvar org-export-filter-entity-functions nil
2261 "List of functions applied to a transcoded entity.
2262 Each filter is called with three arguments: the transcoded data,
2263 as a string, the back-end, as a symbol, and the communication
2264 channel, as a plist. It must return a string or nil.")
2266 (defvar org-export-filter-export-snippet-functions nil
2267 "List of functions applied to a transcoded export-snippet.
2268 Each filter is called with three arguments: the transcoded data,
2269 as a string, the back-end, as a symbol, and the communication
2270 channel, as a plist. It must return a string or nil.")
2272 (defvar org-export-filter-footnote-reference-functions nil
2273 "List of functions applied to a transcoded footnote-reference.
2274 Each filter is called with three arguments: the transcoded data,
2275 as a string, the back-end, as a symbol, and the communication
2276 channel, as a plist. It must return a string or nil.")
2278 (defvar org-export-filter-inline-babel-call-functions nil
2279 "List of functions applied to a transcoded inline-babel-call.
2280 Each filter is called with three arguments: the transcoded data,
2281 as a string, the back-end, as a symbol, and the communication
2282 channel, as a plist. It must return a string or nil.")
2284 (defvar org-export-filter-inline-src-block-functions nil
2285 "List of functions applied to a transcoded inline-src-block.
2286 Each filter is called with three arguments: the transcoded data,
2287 as a string, the back-end, as a symbol, and the communication
2288 channel, as a plist. It must return a string or nil.")
2290 (defvar org-export-filter-italic-functions nil
2291 "List of functions applied to transcoded italic text.
2292 Each filter is called with three arguments: the transcoded data,
2293 as a string, the back-end, as a symbol, and the communication
2294 channel, as a plist. It must return a string or nil.")
2296 (defvar org-export-filter-latex-fragment-functions nil
2297 "List of functions applied to a transcoded latex-fragment.
2298 Each filter is called with three arguments: the transcoded data,
2299 as a string, the back-end, as a symbol, and the communication
2300 channel, as a plist. It must return a string or nil.")
2302 (defvar org-export-filter-line-break-functions nil
2303 "List of functions applied to a transcoded line-break.
2304 Each filter is called with three arguments: the transcoded data,
2305 as a string, the back-end, as a symbol, and the communication
2306 channel, as a plist. It must return a string or nil.")
2308 (defvar org-export-filter-link-functions nil
2309 "List of functions applied to a transcoded link.
2310 Each filter is called with three arguments: the transcoded data,
2311 as a string, the back-end, as a symbol, and the communication
2312 channel, as a plist. It must return a string or nil.")
2314 (defvar org-export-filter-macro-functions nil
2315 "List of functions applied to a transcoded macro.
2316 Each filter is called with three arguments: the transcoded data,
2317 as a string, the back-end, as a symbol, and the communication
2318 channel, as a plist. It must return a string or nil.")
2320 (defvar org-export-filter-radio-target-functions nil
2321 "List of functions applied to a transcoded radio-target.
2322 Each filter is called with three arguments: the transcoded data,
2323 as a string, the back-end, as a symbol, and the communication
2324 channel, as a plist. It must return a string or nil.")
2326 (defvar org-export-filter-statistics-cookie-functions nil
2327 "List of functions applied to a transcoded statistics-cookie.
2328 Each filter is called with three arguments: the transcoded data,
2329 as a string, the back-end, as a symbol, and the communication
2330 channel, as a plist. It must return a string or nil.")
2332 (defvar org-export-filter-strike-through-functions nil
2333 "List of functions applied to transcoded strike-through text.
2334 Each filter is called with three arguments: the transcoded data,
2335 as a string, the back-end, as a symbol, and the communication
2336 channel, as a plist. It must return a string or nil.")
2338 (defvar org-export-filter-subscript-functions nil
2339 "List of functions applied to a transcoded subscript.
2340 Each filter is called with three arguments: the transcoded data,
2341 as a string, the back-end, as a symbol, and the communication
2342 channel, as a plist. It must return a string or nil.")
2344 (defvar org-export-filter-superscript-functions nil
2345 "List of functions applied to a transcoded superscript.
2346 Each filter is called with three arguments: the transcoded data,
2347 as a string, the back-end, as a symbol, and the communication
2348 channel, as a plist. It must return a string or nil.")
2350 (defvar org-export-filter-target-functions nil
2351 "List of functions applied to a transcoded target.
2352 Each filter is called with three arguments: the transcoded data,
2353 as a string, the back-end, as a symbol, and the communication
2354 channel, as a plist. It must return a string or nil.")
2356 (defvar org-export-filter-timestamp-functions nil
2357 "List of functions applied to a transcoded timestamp.
2358 Each filter is called with three arguments: the transcoded data,
2359 as a string, the back-end, as a symbol, and the communication
2360 channel, as a plist. It must return a string or nil.")
2362 (defvar org-export-filter-underline-functions nil
2363 "List of functions applied to transcoded underline text.
2364 Each filter is called with three arguments: the transcoded data,
2365 as a string, the back-end, as a symbol, and the communication
2366 channel, as a plist. It must return a string or nil.")
2368 (defvar org-export-filter-verbatim-functions nil
2369 "List of functions applied to transcoded verbatim text.
2370 Each filter is called with three arguments: the transcoded data,
2371 as a string, the back-end, as a symbol, and the communication
2372 channel, as a plist. It must return a string or nil.")
2377 ;; Internal function `org-export-install-filters' installs filters
2378 ;; hard-coded in back-ends (developer filters) and filters from global
2379 ;; variables (user filters) in the communication channel.
2381 ;; Internal function `org-export-filter-apply-functions' takes care
2382 ;; about applying each filter in order to a given data. It stops
2383 ;; whenever a filter returns a nil value.
2385 (defun org-export-filter-apply-functions (filters value info
)
2386 "Call every function in FILTERS.
2387 Functions are called with arguments VALUE, current export
2388 back-end and INFO. Call is done in a LIFO fashion, to be sure
2389 that developer specified filters, if any, are called first."
2390 (loop for filter in filters
2391 if
(not value
) return nil else
2392 do
(setq value
(funcall filter value
(plist-get info
:back-end
) info
)))
2395 (defun org-export-install-filters (info)
2396 "Install filters properties in communication channel.
2398 INFO is a plist containing the current communication channel.
2400 Return the updated communication channel."
2402 ;; Install user defined filters with `org-export-filters-alist'.
2404 (setq plist
(plist-put plist
(car p
) (eval (cdr p
)))))
2405 org-export-filters-alist
)
2406 ;; Prepend back-end specific filters to that list.
2407 (let ((back-end-filters (intern (format "org-%s-filters-alist"
2408 (plist-get info
:back-end
)))))
2409 (when (boundp back-end-filters
)
2411 ;; Single values get consed, lists are prepended.
2412 (let ((key (car p
)) (value (cdr p
)))
2417 (if (atom value
) (cons value
(plist-get plist key
))
2418 (append value
(plist-get plist key
))))))))
2419 (eval back-end-filters
))))
2420 ;; Return new communication channel.
2421 (org-combine-plists info plist
)))
2427 ;; This is the room for the main function, `org-export-as', along with
2428 ;; its derivatives, `org-export-to-buffer' and `org-export-to-file'.
2429 ;; They differ only by the way they output the resulting code.
2431 ;; `org-export-output-file-name' is an auxiliary function meant to be
2432 ;; used with `org-export-to-file'. With a given extension, it tries
2433 ;; to provide a canonical file name to write export output to.
2435 ;; Note that `org-export-as' doesn't really parse the current buffer,
2436 ;; but a copy of it (with the same buffer-local variables and
2437 ;; visibility), where include keywords are expanded and Babel blocks
2438 ;; are executed, if appropriate.
2439 ;; `org-export-with-current-buffer-copy' macro prepares that copy.
2441 ;; File inclusion is taken care of by
2442 ;; `org-export-expand-include-keyword' and
2443 ;; `org-export-prepare-file-contents'. Structure wise, including
2444 ;; a whole Org file in a buffer often makes little sense. For
2445 ;; example, if the file contains an headline and the include keyword
2446 ;; was within an item, the item should contain the headline. That's
2447 ;; why file inclusion should be done before any structure can be
2448 ;; associated to the file, that is before parsing.
2450 (defvar org-current-export-file
) ; Dynamically scoped
2451 (defvar org-export-current-backend
) ; Dynamically scoped
2452 (defun org-export-as
2453 (backend &optional subtreep visible-only body-only ext-plist noexpand
)
2454 "Transcode current Org buffer into BACKEND code.
2456 If narrowing is active in the current buffer, only transcode its
2459 If a region is active, transcode that region.
2461 When optional argument SUBTREEP is non-nil, transcode the
2462 sub-tree at point, extracting information from the headline
2465 When optional argument VISIBLE-ONLY is non-nil, don't export
2466 contents of hidden elements.
2468 When optional argument BODY-ONLY is non-nil, only return body
2469 code, without preamble nor postamble.
2471 Optional argument EXT-PLIST, when provided, is a property list
2472 with external parameters overriding Org default settings, but
2473 still inferior to file-local settings.
2475 Optional argument NOEXPAND, when non-nil, prevents included files
2476 to be expanded and Babel code to be executed.
2478 Return code as a string."
2481 ;; Narrow buffer to an appropriate region or subtree for
2482 ;; parsing. If parsing subtree, be sure to remove main headline
2484 (cond ((org-region-active-p)
2485 (narrow-to-region (region-beginning) (region-end)))
2487 (org-narrow-to-subtree)
2488 (goto-char (point-min))
2490 (narrow-to-region (point) (point-max))))
2491 ;; 1. Get export environment from original buffer. Also install
2492 ;; user's and developer's filters.
2493 (let ((info (org-export-install-filters
2494 (org-export-get-environment backend subtreep ext-plist
)))
2495 ;; 2. Get parse tree. Buffer isn't parsed directly.
2496 ;; Instead, a temporary copy is created, where include
2497 ;; keywords are expanded and code blocks are evaluated.
2498 (tree (let ((buf (or (buffer-file-name (buffer-base-buffer))
2500 (org-export-with-current-buffer-copy
2502 (org-export-expand-include-keyword)
2503 ;; Setting `org-current-export-file' is
2504 ;; required by Org Babel to properly resolve
2505 ;; noweb references.
2506 (let ((org-current-export-file buf
))
2507 (org-export-blocks-preprocess)))
2508 (goto-char (point-min))
2509 ;; Run hook with `org-export-current-backend' set
2511 (let ((org-export-current-backend backend
))
2512 (run-hooks 'org-export-before-parsing-hook
))
2513 ;; Eventually parse buffer.
2514 (org-element-parse-buffer nil visible-only
)))))
2515 ;; 3. Call parse-tree filters to get the final tree.
2517 (org-export-filter-apply-functions
2518 (plist-get info
:filter-parse-tree
) tree info
))
2519 ;; 4. Now tree is complete, compute its properties and add
2520 ;; them to communication channel.
2523 info
(org-export-collect-tree-properties tree info
)))
2524 ;; 5. Eventually transcode TREE. Wrap the resulting string
2525 ;; into a template, if required. Eventually call
2526 ;; final-output filter.
2527 (let* ((body (org-element-normalize-string (org-export-data tree info
)))
2528 (template (cdr (assq 'template
2529 (plist-get info
:translate-alist
))))
2530 (output (org-export-filter-apply-functions
2531 (plist-get info
:filter-final-output
)
2532 (if (or (not (functionp template
)) body-only
) body
2533 (funcall template body info
))
2535 ;; Maybe add final OUTPUT to kill ring, then return it.
2536 (when org-export-copy-to-kill-ring
(org-kill-new output
))
2539 (defun org-export-to-buffer
2540 (backend buffer
&optional subtreep visible-only body-only ext-plist noexpand
)
2541 "Call `org-export-as' with output to a specified buffer.
2543 BACKEND is the back-end used for transcoding, as a symbol.
2545 BUFFER is the output buffer. If it already exists, it will be
2546 erased first, otherwise, it will be created.
2548 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY, EXT-PLIST
2549 and NOEXPAND are similar to those used in `org-export-as', which
2553 (let ((out (org-export-as
2554 backend subtreep visible-only body-only ext-plist noexpand
))
2555 (buffer (get-buffer-create buffer
)))
2556 (with-current-buffer buffer
2559 (goto-char (point-min)))
2562 (defun org-export-to-file
2563 (backend file
&optional subtreep visible-only body-only ext-plist noexpand
)
2564 "Call `org-export-as' with output to a specified file.
2566 BACKEND is the back-end used for transcoding, as a symbol. FILE
2567 is the name of the output file, as a string.
2569 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY, EXT-PLIST
2570 and NOEXPAND are similar to those used in `org-export-as', which
2573 Return output file's name."
2574 ;; Checks for FILE permissions. `write-file' would do the same, but
2575 ;; we'd rather avoid needless transcoding of parse tree.
2576 (unless (file-writable-p file
) (error "Output file not writable"))
2577 ;; Insert contents to a temporary buffer and write it to FILE.
2578 (let ((out (org-export-as
2579 backend subtreep visible-only body-only ext-plist noexpand
)))
2582 (let ((coding-system-for-write org-export-coding-system
))
2583 (write-file file
))))
2584 ;; Return full path.
2587 (defun org-export-output-file-name (extension &optional subtreep pub-dir
)
2588 "Return output file's name according to buffer specifications.
2590 EXTENSION is a string representing the output file extension,
2591 with the leading dot.
2593 With a non-nil optional argument SUBTREEP, try to determine
2594 output file's name by looking for \"EXPORT_FILE_NAME\" property
2595 of subtree at point.
2597 When optional argument PUB-DIR is set, use it as the publishing
2600 When optional argument VISIBLE-ONLY is non-nil, don't export
2601 contents of hidden elements.
2603 Return file name as a string, or nil if it couldn't be
2606 ;; File name may come from EXPORT_FILE_NAME subtree property,
2607 ;; assuming point is at beginning of said sub-tree.
2608 (file-name-sans-extension
2612 (ignore-errors (org-back-to-heading) (point)))
2613 "EXPORT_FILE_NAME" t
))
2614 ;; File name may be extracted from buffer's associated
2616 (buffer-file-name (buffer-base-buffer))
2617 ;; Can't determine file name on our own: Ask user.
2618 (let ((read-file-name-function
2619 (and org-completion-use-ido
'ido-read-file-name
)))
2621 "Output file: " pub-dir nil nil nil
2623 (string= (file-name-extension name t
) extension
))))))))
2624 ;; Build file name. Enforce EXTENSION over whatever user may have
2625 ;; come up with. PUB-DIR, if defined, always has precedence over
2626 ;; any provided path.
2629 (concat (file-name-as-directory pub-dir
)
2630 (file-name-nondirectory base-name
)
2632 ((string= (file-name-nondirectory base-name
) base-name
)
2633 (concat (file-name-as-directory ".") base-name extension
))
2634 (t (concat base-name extension
)))))
2636 (defmacro org-export-with-current-buffer-copy
(&rest body
)
2637 "Apply BODY in a copy of the current buffer.
2639 The copy preserves local variables and visibility of the original
2642 Point is at buffer's beginning when BODY is applied."
2643 (org-with-gensyms (original-buffer offset buffer-string overlays
)
2644 `(let ((,original-buffer
(current-buffer))
2645 (,offset
(1- (point-min)))
2646 (,buffer-string
(buffer-string))
2648 'copy-overlay
(overlays-in (point-min) (point-max)))))
2650 (let ((buffer-invisibility-spec nil
))
2651 (org-clone-local-variables
2653 "^\\(org-\\|orgtbl-\\|major-mode$\\|outline-\\(regexp\\|level\\)$\\)")
2654 (insert ,buffer-string
)
2658 (- (overlay-start ov
) ,offset
)
2659 (- (overlay-end ov
) ,offset
)
2662 (goto-char (point-min))
2664 (def-edebug-spec org-export-with-current-buffer-copy
(body))
2666 (defun org-export-expand-include-keyword (&optional included dir
)
2667 "Expand every include keyword in buffer.
2668 Optional argument INCLUDED is a list of included file names along
2669 with their line restriction, when appropriate. It is used to
2670 avoid infinite recursion. Optional argument DIR is the current
2671 working directory. It is used to properly resolve relative
2673 (let ((case-fold-search t
))
2674 (goto-char (point-min))
2675 (while (re-search-forward "^[ \t]*#\\+INCLUDE: \\(.*\\)" nil t
)
2676 (when (eq (org-element-type (save-match-data (org-element-at-point)))
2679 ;; Extract arguments from keyword's value.
2680 (let* ((value (match-string 1))
2681 (ind (org-get-indentation))
2682 (file (and (string-match "^\"\\(\\S-+\\)\"" value
)
2683 (prog1 (expand-file-name (match-string 1 value
) dir
)
2684 (setq value
(replace-match "" nil nil value
)))))
2687 ":lines +\"\\(\\(?:[0-9]+\\)?-\\(?:[0-9]+\\)?\\)\"" value
)
2688 (prog1 (match-string 1 value
)
2689 (setq value
(replace-match "" nil nil value
)))))
2690 (env (cond ((string-match "\\<example\\>" value
) 'example
)
2691 ((string-match "\\<src\\(?: +\\(.*\\)\\)?" value
)
2692 (match-string 1 value
))))
2693 ;; Minimal level of included file defaults to the child
2694 ;; level of the current headline, if any, or one. It
2695 ;; only applies is the file is meant to be included as
2699 (if (string-match ":minlevel +\\([0-9]+\\)" value
)
2700 (prog1 (string-to-number (match-string 1 value
))
2701 (setq value
(replace-match "" nil nil value
)))
2702 (let ((cur (org-current-level)))
2703 (if cur
(1+ (org-reduced-level cur
)) 1))))))
2705 (delete-region (point) (progn (forward-line) (point)))
2707 ((not (file-readable-p file
)) (error "Cannot include file %s" file
))
2708 ;; Check if files has already been parsed. Look after
2709 ;; inclusion lines too, as different parts of the same file
2710 ;; can be included too.
2711 ((member (list file lines
) included
)
2712 (error "Recursive file inclusion: %s" file
))
2717 (let ((ind-str (make-string ind ?
))
2719 ;; Protect sensitive contents with commas.
2720 (replace-regexp-in-string
2721 "\\(^\\)\\([*]\\|[ \t]*#\\+\\)" ","
2722 (org-export-prepare-file-contents file lines
)
2724 (format "%s#+BEGIN_EXAMPLE\n%s%s#+END_EXAMPLE\n"
2725 ind-str contents ind-str
))))
2728 (let ((ind-str (make-string ind ?
))
2730 ;; Protect sensitive contents with commas.
2731 (replace-regexp-in-string
2732 (if (string= env
"org") "\\(^\\)\\(.\\)"
2733 "\\(^\\)\\([*]\\|[ \t]*#\\+\\)") ","
2734 (org-export-prepare-file-contents file lines
)
2736 (format "%s#+BEGIN_SRC %s\n%s%s#+END_SRC\n"
2737 ind-str env contents ind-str
))))
2743 (org-export-prepare-file-contents file lines ind minlevel
))
2744 (org-export-expand-include-keyword
2745 (cons (list file lines
) included
)
2746 (file-name-directory file
))
2747 (buffer-string))))))))))))
2749 (defun org-export-prepare-file-contents (file &optional lines ind minlevel
)
2750 "Prepare the contents of FILE for inclusion and return them as a string.
2752 When optional argument LINES is a string specifying a range of
2753 lines, include only those lines.
2755 Optional argument IND, when non-nil, is an integer specifying the
2756 global indentation of returned contents. Since its purpose is to
2757 allow an included file to stay in the same environment it was
2758 created \(i.e. a list item), it doesn't apply past the first
2759 headline encountered.
2761 Optional argument MINLEVEL, when non-nil, is an integer
2762 specifying the level that any top-level headline in the included
2765 (insert-file-contents file
)
2767 (let* ((lines (split-string lines
"-"))
2768 (lbeg (string-to-number (car lines
)))
2769 (lend (string-to-number (cadr lines
)))
2770 (beg (if (zerop lbeg
) (point-min)
2771 (goto-char (point-min))
2772 (forward-line (1- lbeg
))
2774 (end (if (zerop lend
) (point-max)
2775 (goto-char (point-min))
2776 (forward-line (1- lend
))
2778 (narrow-to-region beg end
)))
2779 ;; Remove blank lines at beginning and end of contents. The logic
2780 ;; behind that removal is that blank lines around include keyword
2781 ;; override blank lines in included file.
2782 (goto-char (point-min))
2783 (org-skip-whitespace)
2785 (delete-region (point-min) (point))
2786 (goto-char (point-max))
2787 (skip-chars-backward " \r\t\n")
2789 (delete-region (point) (point-max))
2790 ;; If IND is set, preserve indentation of include keyword until
2791 ;; the first headline encountered.
2793 (unless (eq major-mode
'org-mode
) (org-mode))
2794 (goto-char (point-min))
2795 (let ((ind-str (make-string ind ?
)))
2796 (while (not (or (eobp) (looking-at org-outline-regexp-bol
)))
2797 ;; Do not move footnote definitions out of column 0.
2798 (unless (and (looking-at org-footnote-definition-re
)
2799 (eq (org-element-type (org-element-at-point))
2800 'footnote-definition
))
2803 ;; When MINLEVEL is specified, compute minimal level for headlines
2804 ;; in the file (CUR-MIN), and remove stars to each headline so
2805 ;; that headlines with minimal level have a level of MINLEVEL.
2807 (unless (eq major-mode
'org-mode
) (org-mode))
2808 (let ((levels (org-map-entries
2809 (lambda () (org-reduced-level (org-current-level))))))
2811 (let ((offset (- minlevel
(apply 'min levels
))))
2812 (unless (zerop offset
)
2813 (when org-odd-levels-only
(setq offset
(* offset
2)))
2814 ;; Only change stars, don't bother moving whole
2817 (lambda () (if (< offset
0) (delete-char (abs offset
))
2818 (insert (make-string offset ?
*))))))))))
2822 ;;; Tools For Back-Ends
2824 ;; A whole set of tools is available to help build new exporters. Any
2825 ;; function general enough to have its use across many back-ends
2826 ;; should be added here.
2828 ;; As of now, functions operating on footnotes, headlines, links,
2829 ;; macros, references, src-blocks, tables and tables of contents are
2832 ;;;; For Affiliated Keywords
2834 ;; `org-export-read-attribute' reads a property from a given element
2835 ;; as a plist. It can be used to normalize affiliated keywords'
2838 (defun org-export-read-attribute (attribute element
&optional property
)
2839 "Turn ATTRIBUTE property from ELEMENT into a plist.
2841 When optional argument PROPERTY is non-nil, return the value of
2842 that property within attributes.
2844 This function assumes attributes are defined as \":keyword
2845 value\" pairs. It is appropriate for `:attr_html' like
2848 (let ((value (org-element-property attribute element
)))
2850 (read (format "(%s)" (mapconcat 'identity value
" ")))))))
2851 (if property
(plist-get attributes property
) attributes
)))
2854 ;;;; For Export Snippets
2856 ;; Every export snippet is transmitted to the back-end. Though, the
2857 ;; latter will only retain one type of export-snippet, ignoring
2858 ;; others, based on the former's target back-end. The function
2859 ;; `org-export-snippet-backend' returns that back-end for a given
2862 (defun org-export-snippet-backend (export-snippet)
2863 "Return EXPORT-SNIPPET targeted back-end as a symbol.
2864 Translation, with `org-export-snippet-translation-alist', is
2866 (let ((back-end (org-element-property :back-end export-snippet
)))
2868 (or (cdr (assoc back-end org-export-snippet-translation-alist
))
2874 ;; `org-export-collect-footnote-definitions' is a tool to list
2875 ;; actually used footnotes definitions in the whole parse tree, or in
2876 ;; an headline, in order to add footnote listings throughout the
2879 ;; `org-export-footnote-first-reference-p' is a predicate used by some
2880 ;; back-ends, when they need to attach the footnote definition only to
2881 ;; the first occurrence of the corresponding label.
2883 ;; `org-export-get-footnote-definition' and
2884 ;; `org-export-get-footnote-number' provide easier access to
2885 ;; additional information relative to a footnote reference.
2887 (defun org-export-collect-footnote-definitions (data info
)
2888 "Return an alist between footnote numbers, labels and definitions.
2890 DATA is the parse tree from which definitions are collected.
2891 INFO is the plist used as a communication channel.
2893 Definitions are sorted by order of references. They either
2894 appear as Org data or as a secondary string for inlined
2895 footnotes. Unreferenced definitions are ignored."
2897 collect-fn
; for byte-compiler.
2901 ;; Collect footnote number, label and definition in DATA.
2903 data
'footnote-reference
2905 (when (org-export-footnote-first-reference-p fn info
)
2906 (let ((def (org-export-get-footnote-definition fn info
)))
2908 (list (org-export-get-footnote-number fn info
)
2909 (org-element-property :label fn
)
2912 ;; Also search in definition for nested footnotes.
2913 (when (eq (org-element-property :type fn
) 'standard
)
2914 (funcall collect-fn def
)))))
2915 ;; Don't enter footnote definitions since it will happen
2916 ;; when their first reference is found.
2917 info nil
'footnote-definition
)))))
2918 (funcall collect-fn
(plist-get info
:parse-tree
))
2919 (reverse num-alist
)))
2921 (defun org-export-footnote-first-reference-p (footnote-reference info
)
2922 "Non-nil when a footnote reference is the first one for its label.
2924 FOOTNOTE-REFERENCE is the footnote reference being considered.
2925 INFO is the plist used as a communication channel."
2926 (let ((label (org-element-property :label footnote-reference
)))
2927 ;; Anonymous footnotes are always a first reference.
2929 ;; Otherwise, return the first footnote with the same LABEL and
2930 ;; test if it is equal to FOOTNOTE-REFERENCE.
2931 (let* (search-refs ; for byte-compiler.
2936 data
'footnote-reference
2939 ((string= (org-element-property :label fn
) label
)
2941 ;; If FN isn't inlined, be sure to traverse its
2942 ;; definition before resuming search. See
2943 ;; comments in `org-export-get-footnote-number'
2944 ;; for more information.
2945 ((eq (org-element-property :type fn
) 'standard
)
2946 (funcall search-refs
2947 (org-export-get-footnote-definition fn info
)))))
2948 ;; Don't enter footnote definitions since it will
2949 ;; happen when their first reference is found.
2950 info
'first-match
'footnote-definition
)))))
2951 (eq (catch 'exit
(funcall search-refs
(plist-get info
:parse-tree
)))
2952 footnote-reference
)))))
2954 (defun org-export-get-footnote-definition (footnote-reference info
)
2955 "Return definition of FOOTNOTE-REFERENCE as parsed data.
2956 INFO is the plist used as a communication channel."
2957 (let ((label (org-element-property :label footnote-reference
)))
2958 (or (org-element-property :inline-definition footnote-reference
)
2959 (cdr (assoc label
(plist-get info
:footnote-definition-alist
))))))
2961 (defun org-export-get-footnote-number (footnote info
)
2962 "Return number associated to a footnote.
2964 FOOTNOTE is either a footnote reference or a footnote definition.
2965 INFO is the plist used as a communication channel."
2966 (let* ((label (org-element-property :label footnote
))
2968 search-ref
; for byte-compiler.
2972 ;; Search footnote references through DATA, filling
2973 ;; SEEN-REFS along the way.
2975 data
'footnote-reference
2977 (let ((fn-lbl (org-element-property :label fn
)))
2979 ;; Anonymous footnote match: return number.
2980 ((and (not fn-lbl
) (eq fn footnote
))
2981 (throw 'exit
(1+ (length seen-refs
))))
2982 ;; Labels match: return number.
2983 ((and label
(string= label fn-lbl
))
2984 (throw 'exit
(1+ (length seen-refs
))))
2985 ;; Anonymous footnote: it's always a new one. Also,
2986 ;; be sure to return nil from the `cond' so
2987 ;; `first-match' doesn't get us out of the loop.
2988 ((not fn-lbl
) (push 'inline seen-refs
) nil
)
2989 ;; Label not seen so far: add it so SEEN-REFS.
2991 ;; Also search for subsequent references in footnote
2992 ;; definition so numbering following reading logic.
2993 ;; Note that we don't have to care about inline
2994 ;; definitions, since `org-element-map' already
2995 ;; traverse them at the right time.
2997 ;; Once again, return nil to stay in the loop.
2998 ((not (member fn-lbl seen-refs
))
2999 (push fn-lbl seen-refs
)
3001 (org-export-get-footnote-definition fn info
))
3003 ;; Don't enter footnote definitions since it will happen
3004 ;; when their first reference is found.
3005 info
'first-match
'footnote-definition
)))))
3006 (catch 'exit
(funcall search-ref
(plist-get info
:parse-tree
)))))
3011 ;; `org-export-get-relative-level' is a shortcut to get headline
3012 ;; level, relatively to the lower headline level in the parsed tree.
3014 ;; `org-export-get-headline-number' returns the section number of an
3015 ;; headline, while `org-export-number-to-roman' allows to convert it
3016 ;; to roman numbers.
3018 ;; `org-export-low-level-p', `org-export-first-sibling-p' and
3019 ;; `org-export-last-sibling-p' are three useful predicates when it
3020 ;; comes to fulfill the `:headline-levels' property.
3022 (defun org-export-get-relative-level (headline info
)
3023 "Return HEADLINE relative level within current parsed tree.
3024 INFO is a plist holding contextual information."
3025 (+ (org-element-property :level headline
)
3026 (or (plist-get info
:headline-offset
) 0)))
3028 (defun org-export-low-level-p (headline info
)
3029 "Non-nil when HEADLINE is considered as low level.
3031 INFO is a plist used as a communication channel.
3033 A low level headlines has a relative level greater than
3034 `:headline-levels' property value.
3036 Return value is the difference between HEADLINE relative level
3037 and the last level being considered as high enough, or nil."
3038 (let ((limit (plist-get info
:headline-levels
)))
3039 (when (wholenump limit
)
3040 (let ((level (org-export-get-relative-level headline info
)))
3041 (and (> level limit
) (- level limit
))))))
3043 (defun org-export-get-headline-number (headline info
)
3044 "Return HEADLINE numbering as a list of numbers.
3045 INFO is a plist holding contextual information."
3046 (cdr (assoc headline
(plist-get info
:headline-numbering
))))
3048 (defun org-export-numbered-headline-p (headline info
)
3049 "Return a non-nil value if HEADLINE element should be numbered.
3050 INFO is a plist used as a communication channel."
3051 (let ((sec-num (plist-get info
:section-numbers
))
3052 (level (org-export-get-relative-level headline info
)))
3053 (if (wholenump sec-num
) (<= level sec-num
) sec-num
)))
3055 (defun org-export-number-to-roman (n)
3056 "Convert integer N into a roman numeral."
3057 (let ((roman '((1000 .
"M") (900 .
"CM") (500 .
"D") (400 .
"CD")
3058 ( 100 .
"C") ( 90 .
"XC") ( 50 .
"L") ( 40 .
"XL")
3059 ( 10 .
"X") ( 9 .
"IX") ( 5 .
"V") ( 4 .
"IV")
3063 (number-to-string n
)
3065 (if (>= n
(caar roman
))
3066 (setq n
(- n
(caar roman
))
3067 res
(concat res
(cdar roman
)))
3071 (defun org-export-get-tags (element info
&optional tags
)
3072 "Return list of tags associated to ELEMENT.
3074 ELEMENT has either an `headline' or an `inlinetask' type. INFO
3075 is a plist used as a communication channel.
3077 Select tags (see `org-export-select-tags') and exclude tags (see
3078 `org-export-exclude-tags') are removed from the list.
3080 When non-nil, optional argument TAGS should be a list of strings.
3081 Any tag belonging to this list will also be removed."
3082 (org-remove-if (lambda (tag) (or (member tag
(plist-get info
:select-tags
))
3083 (member tag
(plist-get info
:exclude-tags
))
3085 (org-element-property :tags element
)))
3087 (defun org-export-first-sibling-p (headline)
3088 "Non-nil when HEADLINE is the first sibling in its sub-tree."
3089 (not (eq (org-element-type (org-export-get-previous-element headline
))
3092 (defun org-export-last-sibling-p (headline)
3093 "Non-nil when HEADLINE is the last sibling in its sub-tree."
3094 (not (org-export-get-next-element headline
)))
3099 ;; `org-export-solidify-link-text' turns a string into a safer version
3100 ;; for links, replacing most non-standard characters with hyphens.
3102 ;; `org-export-get-coderef-format' returns an appropriate format
3103 ;; string for coderefs.
3105 ;; `org-export-inline-image-p' returns a non-nil value when the link
3106 ;; provided should be considered as an inline image.
3108 ;; `org-export-resolve-fuzzy-link' searches destination of fuzzy links
3109 ;; (i.e. links with "fuzzy" as type) within the parsed tree, and
3110 ;; returns an appropriate unique identifier when found, or nil.
3112 ;; `org-export-resolve-id-link' returns the first headline with
3113 ;; specified id or custom-id in parse tree, the path to the external
3114 ;; file with the id or nil when neither was found.
3116 ;; `org-export-resolve-coderef' associates a reference to a line
3117 ;; number in the element it belongs, or returns the reference itself
3118 ;; when the element isn't numbered.
3120 (defun org-export-solidify-link-text (s)
3121 "Take link text S and make a safe target out of it."
3123 (mapconcat 'identity
(org-split-string s
"[^a-zA-Z0-9_.-]+") "-")))
3125 (defun org-export-get-coderef-format (path desc
)
3126 "Return format string for code reference link.
3127 PATH is the link path. DESC is its description."
3129 (cond ((not desc
) "%s")
3130 ((string-match (regexp-quote (concat "(" path
")")) desc
)
3131 (replace-match "%s" t t desc
))
3134 (defun org-export-inline-image-p (link &optional rules
)
3135 "Non-nil if LINK object points to an inline image.
3137 Optional argument is a set of RULES defining inline images. It
3138 is an alist where associations have the following shape:
3142 Applying a rule means apply REGEXP against LINK's path when its
3143 type is TYPE. The function will return a non-nil value if any of
3144 the provided rules is non-nil. The default rule is
3145 `org-export-default-inline-image-rule'.
3147 This only applies to links without a description."
3148 (and (not (org-element-contents link
))
3149 (let ((case-fold-search t
)
3150 (rules (or rules org-export-default-inline-image-rule
)))
3154 (and (string= (org-element-property :type link
) (car rule
))
3155 (string-match (cdr rule
)
3156 (org-element-property :path link
))
3159 ;; Return nil if no rule matched.
3162 (defun org-export-resolve-coderef (ref info
)
3163 "Resolve a code reference REF.
3165 INFO is a plist used as a communication channel.
3167 Return associated line number in source code, or REF itself,
3168 depending on src-block or example element's switches."
3170 (plist-get info
:parse-tree
) '(example-block src-block
)
3173 (insert (org-trim (org-element-property :value el
)))
3174 (let* ((label-fmt (regexp-quote
3175 (or (org-element-property :label-fmt el
)
3176 org-coderef-label-format
)))
3178 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
3179 (replace-regexp-in-string "%s" ref label-fmt nil t
))))
3180 ;; Element containing REF is found. Resolve it to either
3181 ;; a label or a line number, as needed.
3182 (when (re-search-backward ref-re nil t
)
3184 ((org-element-property :use-labels el
) ref
)
3185 ((eq (org-element-property :number-lines el
) 'continued
)
3186 (+ (org-export-get-loc el info
) (line-number-at-pos)))
3187 (t (line-number-at-pos)))))))
3190 (defun org-export-resolve-fuzzy-link (link info
)
3191 "Return LINK destination.
3193 INFO is a plist holding contextual information.
3195 Return value can be an object, an element, or nil:
3197 - If LINK path matches a target object (i.e. <<path>>) or
3198 element (i.e. \"#+TARGET: path\"), return it.
3200 - If LINK path exactly matches the name affiliated keyword
3201 \(i.e. #+NAME: path) of an element, return that element.
3203 - If LINK path exactly matches any headline name, return that
3204 element. If more than one headline share that name, priority
3205 will be given to the one with the closest common ancestor, if
3206 any, or the first one in the parse tree otherwise.
3208 - Otherwise, return nil.
3210 Assume LINK type is \"fuzzy\"."
3211 (let* ((path (org-element-property :path link
))
3212 (match-title-p (eq (aref path
0) ?
*)))
3214 ;; First try to find a matching "<<path>>" unless user specified
3215 ;; he was looking for an headline (path starts with a *
3217 ((and (not match-title-p
)
3218 (loop for target in
(plist-get info
:target-list
)
3219 when
(string= (org-element-property :value target
) path
)
3221 ;; Then try to find an element with a matching "#+NAME: path"
3222 ;; affiliated keyword.
3223 ((and (not match-title-p
)
3225 (plist-get info
:parse-tree
) org-element-all-elements
3227 (when (string= (org-element-property :name el
) path
) el
))
3228 info
'first-match
)))
3229 ;; Last case: link either points to an headline or to
3230 ;; nothingness. Try to find the source, with priority given to
3231 ;; headlines with the closest common ancestor. If such candidate
3232 ;; is found, return it, otherwise return nil.
3234 (let ((find-headline
3236 ;; Return first headline whose `:raw-value' property
3237 ;; is NAME in parse tree DATA, or nil.
3243 (org-element-property :raw-value headline
)
3246 info
'first-match
)))))
3247 ;; Search among headlines sharing an ancestor with link,
3248 ;; from closest to farthest.
3252 (when (eq (org-element-type parent
) 'headline
)
3253 (let ((foundp (funcall find-headline path parent
)))
3254 (when foundp
(throw 'exit foundp
)))))
3255 (org-export-get-genealogy link
)) nil
)
3256 ;; No match with a common ancestor: try the full parse-tree.
3257 (funcall find-headline
3258 (if match-title-p
(substring path
1) path
)
3259 (plist-get info
:parse-tree
))))))))
3261 (defun org-export-resolve-id-link (link info
)
3262 "Return headline referenced as LINK destination.
3264 INFO is a plist used as a communication channel.
3266 Return value can be the headline element matched in current parse
3267 tree, a file name or nil. Assume LINK type is either \"id\" or
3269 (let ((id (org-element-property :path link
)))
3270 ;; First check if id is within the current parse tree.
3271 (or (org-element-map
3272 (plist-get info
:parse-tree
) 'headline
3274 (when (or (string= (org-element-property :id headline
) id
)
3275 (string= (org-element-property :custom-id headline
) id
))
3278 ;; Otherwise, look for external files.
3279 (cdr (assoc id
(plist-get info
:id-alist
))))))
3281 (defun org-export-resolve-radio-link (link info
)
3282 "Return radio-target object referenced as LINK destination.
3284 INFO is a plist used as a communication channel.
3286 Return value can be a radio-target object or nil. Assume LINK
3287 has type \"radio\"."
3288 (let ((path (org-element-property :path link
)))
3290 (plist-get info
:parse-tree
) 'radio-target
3292 (when (equal (org-element-property :value radio
) path
) radio
))
3293 info
'first-match
)))
3298 ;; `org-export-expand-macro' simply takes care of expanding macros.
3300 (defun org-export-expand-macro (macro info
)
3301 "Expand MACRO and return it as a string.
3302 INFO is a plist holding export options."
3303 (let* ((key (org-element-property :key macro
))
3304 (args (org-element-property :args macro
))
3305 ;; User's macros are stored in the communication channel with
3306 ;; a ":macro-" prefix. Replace arguments in VALUE. Also
3307 ;; expand recursively macros within.
3308 (value (org-export-data
3311 (if (not (stringp obj
)) (org-export-data obj info
)
3312 (replace-regexp-in-string
3315 (nth (1- (string-to-number (substring arg
1))) args
))
3317 (plist-get info
(intern (format ":macro-%s" key
))))
3319 ;; VALUE starts with "(eval": it is a s-exp, `eval' it.
3320 (when (string-match "\\`(eval\\>" value
) (setq value
(eval (read value
))))
3322 (format "%s" (or value
""))))
3327 ;; `org-export-get-ordinal' associates a sequence number to any object
3330 (defun org-export-get-ordinal (element info
&optional types predicate
)
3331 "Return ordinal number of an element or object.
3333 ELEMENT is the element or object considered. INFO is the plist
3334 used as a communication channel.
3336 Optional argument TYPES, when non-nil, is a list of element or
3337 object types, as symbols, that should also be counted in.
3338 Otherwise, only provided element's type is considered.
3340 Optional argument PREDICATE is a function returning a non-nil
3341 value if the current element or object should be counted in. It
3342 accepts two arguments: the element or object being considered and
3343 the plist used as a communication channel. This allows to count
3344 only a certain type of objects (i.e. inline images).
3346 Return value is a list of numbers if ELEMENT is an headline or an
3347 item. It is nil for keywords. It represents the footnote number
3348 for footnote definitions and footnote references. If ELEMENT is
3349 a target, return the same value as if ELEMENT was the closest
3350 table, item or headline containing the target. In any other
3351 case, return the sequence number of ELEMENT among elements or
3352 objects of the same type."
3353 ;; A target keyword, representing an invisible target, never has
3354 ;; a sequence number.
3355 (unless (eq (org-element-type element
) 'keyword
)
3356 ;; Ordinal of a target object refer to the ordinal of the closest
3357 ;; table, item, or headline containing the object.
3358 (when (eq (org-element-type element
) 'target
)
3360 (loop for parent in
(org-export-get-genealogy element
)
3363 (org-element-type parent
)
3364 '(footnote-definition footnote-reference headline item
3367 (case (org-element-type element
)
3368 ;; Special case 1: An headline returns its number as a list.
3369 (headline (org-export-get-headline-number element info
))
3370 ;; Special case 2: An item returns its number as a list.
3371 (item (let ((struct (org-element-property :structure element
)))
3372 (org-list-get-item-number
3373 (org-element-property :begin element
)
3375 (org-list-prevs-alist struct
)
3376 (org-list-parents-alist struct
))))
3377 ((footnote-definition footnote-reference
)
3378 (org-export-get-footnote-number element info
))
3381 ;; Increment counter until ELEMENT is found again.
3383 (plist-get info
:parse-tree
) (or types
(org-element-type element
))
3386 ((eq element el
) (1+ counter
))
3387 ((not predicate
) (incf counter
) nil
)
3388 ((funcall predicate el info
) (incf counter
) nil
)))
3389 info
'first-match
))))))
3394 ;; `org-export-get-loc' counts number of code lines accumulated in
3395 ;; src-block or example-block elements with a "+n" switch until
3396 ;; a given element, excluded. Note: "-n" switches reset that count.
3398 ;; `org-export-unravel-code' extracts source code (along with a code
3399 ;; references alist) from an `element-block' or `src-block' type
3402 ;; `org-export-format-code' applies a formatting function to each line
3403 ;; of code, providing relative line number and code reference when
3404 ;; appropriate. Since it doesn't access the original element from
3405 ;; which the source code is coming, it expects from the code calling
3406 ;; it to know if lines should be numbered and if code references
3409 ;; Eventually, `org-export-format-code-default' is a higher-level
3410 ;; function (it makes use of the two previous functions) which handles
3411 ;; line numbering and code references inclusion, and returns source
3412 ;; code in a format suitable for plain text or verbatim output.
3414 (defun org-export-get-loc (element info
)
3415 "Return accumulated lines of code up to ELEMENT.
3417 INFO is the plist used as a communication channel.
3419 ELEMENT is excluded from count."
3422 (plist-get info
:parse-tree
)
3423 `(src-block example-block
,(org-element-type element
))
3426 ;; ELEMENT is reached: Quit the loop.
3428 ;; Only count lines from src-block and example-block elements
3429 ;; with a "+n" or "-n" switch. A "-n" switch resets counter.
3430 ((not (memq (org-element-type el
) '(src-block example-block
))) nil
)
3431 ((let ((linums (org-element-property :number-lines el
)))
3433 ;; Accumulate locs or reset them.
3434 (let ((lines (org-count-lines
3435 (org-trim (org-element-property :value el
)))))
3436 (setq loc
(if (eq linums
'new
) lines
(+ loc lines
))))))
3437 ;; Return nil to stay in the loop.
3443 (defun org-export-unravel-code (element)
3444 "Clean source code and extract references out of it.
3446 ELEMENT has either a `src-block' an `example-block' type.
3448 Return a cons cell whose CAR is the source code, cleaned from any
3449 reference and protective comma and CDR is an alist between
3450 relative line number (integer) and name of code reference on that
3452 (let* ((line 0) refs
3453 ;; Get code and clean it. Remove blank lines at its
3454 ;; beginning and end. Also remove protective commas.
3455 (code (let ((c (replace-regexp-in-string
3456 "\\`\\([ \t]*\n\\)+" ""
3457 (replace-regexp-in-string
3458 "\\(:?[ \t]*\n\\)*[ \t]*\\'" "\n"
3459 (org-element-property :value element
)))))
3460 ;; If appropriate, remove global indentation.
3461 (unless (or org-src-preserve-indentation
3462 (org-element-property :preserve-indent element
))
3463 (setq c
(org-remove-indentation c
)))
3464 ;; Free up the protected lines. Note: Org blocks
3465 ;; have commas at the beginning or every line.
3466 (if (string= (org-element-property :language element
) "org")
3467 (replace-regexp-in-string "^," "" c
)
3468 (replace-regexp-in-string
3469 "^\\(,\\)\\(:?\\*\\|[ \t]*#\\+\\)" "" c nil nil
1))))
3470 ;; Get format used for references.
3471 (label-fmt (regexp-quote
3472 (or (org-element-property :label-fmt element
)
3473 org-coderef-label-format
)))
3474 ;; Build a regexp matching a loc with a reference.
3476 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)[ \t]*\\)$"
3477 (replace-regexp-in-string
3478 "%s" "\\([-a-zA-Z0-9_ ]+\\)" label-fmt nil t
))))
3481 ;; Code with references removed.
3482 (org-element-normalize-string
3486 (if (not (string-match with-ref-re loc
)) loc
3487 ;; Ref line: remove ref, and signal its position in REFS.
3488 (push (cons line
(match-string 3 loc
)) refs
)
3489 (replace-match "" nil nil loc
1)))
3490 (org-split-string code
"\n") "\n"))
3494 (defun org-export-format-code (code fun
&optional num-lines ref-alist
)
3495 "Format CODE by applying FUN line-wise and return it.
3497 CODE is a string representing the code to format. FUN is
3498 a function. It must accept three arguments: a line of
3499 code (string), the current line number (integer) or nil and the
3500 reference associated to the current line (string) or nil.
3502 Optional argument NUM-LINES can be an integer representing the
3503 number of code lines accumulated until the current code. Line
3504 numbers passed to FUN will take it into account. If it is nil,
3505 FUN's second argument will always be nil. This number can be
3506 obtained with `org-export-get-loc' function.
3508 Optional argument REF-ALIST can be an alist between relative line
3509 number (i.e. ignoring NUM-LINES) and the name of the code
3510 reference on it. If it is nil, FUN's third argument will always
3511 be nil. It can be obtained through the use of
3512 `org-export-unravel-code' function."
3513 (let ((--locs (org-split-string code
"\n"))
3515 (org-element-normalize-string
3519 (let ((--ref (cdr (assq --line ref-alist
))))
3520 (funcall fun --loc
(and num-lines
(+ num-lines --line
)) --ref
)))
3523 (defun org-export-format-code-default (element info
)
3524 "Return source code from ELEMENT, formatted in a standard way.
3526 ELEMENT is either a `src-block' or `example-block' element. INFO
3527 is a plist used as a communication channel.
3529 This function takes care of line numbering and code references
3530 inclusion. Line numbers, when applicable, appear at the
3531 beginning of the line, separated from the code by two white
3532 spaces. Code references, on the other hand, appear flushed to
3533 the right, separated by six white spaces from the widest line of
3535 ;; Extract code and references.
3536 (let* ((code-info (org-export-unravel-code element
))
3537 (code (car code-info
))
3538 (code-lines (org-split-string code
"\n"))
3539 (refs (and (org-element-property :retain-labels element
)
3541 ;; Handle line numbering.
3542 (num-start (case (org-element-property :number-lines element
)
3543 (continued (org-export-get-loc element info
))
3548 (length (number-to-string
3549 (+ (length code-lines
) num-start
))))))
3550 ;; Prepare references display, if required. Any reference
3551 ;; should start six columns after the widest line of code,
3552 ;; wrapped with parenthesis.
3554 (+ (apply 'max
(mapcar 'length code-lines
))
3555 (if (not num-start
) 0 (length (format num-fmt num-start
))))))
3556 (org-export-format-code
3558 (lambda (loc line-num ref
)
3559 (let ((number-str (and num-fmt
(format num-fmt line-num
))))
3564 (concat (make-string
3566 (+ (length loc
) (length number-str
))) ?
)
3567 (format "(%s)" ref
))))))
3573 ;; `org-export-table-has-special-column-p' and and
3574 ;; `org-export-table-row-is-special-p' are predicates used to look for
3575 ;; meta-information about the table structure.
3577 ;; `org-table-has-header-p' tells when the rows before the first rule
3578 ;; should be considered as table's header.
3580 ;; `org-export-table-cell-width', `org-export-table-cell-alignment'
3581 ;; and `org-export-table-cell-borders' extract information from
3582 ;; a table-cell element.
3584 ;; `org-export-table-dimensions' gives the number on rows and columns
3585 ;; in the table, ignoring horizontal rules and special columns.
3586 ;; `org-export-table-cell-address', given a table-cell object, returns
3587 ;; the absolute address of a cell. On the other hand,
3588 ;; `org-export-get-table-cell-at' does the contrary.
3590 ;; `org-export-table-cell-starts-colgroup-p',
3591 ;; `org-export-table-cell-ends-colgroup-p',
3592 ;; `org-export-table-row-starts-rowgroup-p',
3593 ;; `org-export-table-row-ends-rowgroup-p',
3594 ;; `org-export-table-row-starts-header-p' and
3595 ;; `org-export-table-row-ends-header-p' indicate position of current
3596 ;; row or cell within the table.
3598 (defun org-export-table-has-special-column-p (table)
3599 "Non-nil when TABLE has a special column.
3600 All special columns will be ignored during export."
3601 ;; The table has a special column when every first cell of every row
3602 ;; has an empty value or contains a symbol among "/", "#", "!", "$",
3603 ;; "*" "_" and "^". Though, do not consider a first row containing
3604 ;; only empty cells as special.
3605 (let ((special-column-p 'empty
))
3609 (when (eq (org-element-property :type row
) 'standard
)
3610 (let ((value (org-element-contents
3611 (car (org-element-contents row
)))))
3612 (cond ((member value
'(("/") ("#") ("!") ("$") ("*") ("_") ("^")))
3613 (setq special-column-p
'special
))
3615 (t (throw 'exit nil
))))))
3616 (org-element-contents table
))
3617 (eq special-column-p
'special
))))
3619 (defun org-export-table-has-header-p (table info
)
3620 "Non-nil when TABLE has an header.
3622 INFO is a plist used as a communication channel.
3624 A table has an header when it contains at least two row groups."
3625 (let ((rowgroup 1) row-flag
)
3631 ((and row-flag
(eq (org-element-property :type row
) 'rule
))
3632 (incf rowgroup
) (setq row-flag nil
))
3633 ((and (not row-flag
) (eq (org-element-property :type row
) 'standard
))
3634 (setq row-flag t
) nil
)))
3637 (defun org-export-table-row-is-special-p (table-row info
)
3638 "Non-nil if TABLE-ROW is considered special.
3640 INFO is a plist used as the communication channel.
3642 All special rows will be ignored during export."
3643 (when (eq (org-element-property :type table-row
) 'standard
)
3644 (let ((first-cell (org-element-contents
3645 (car (org-element-contents table-row
)))))
3646 ;; A row is special either when...
3648 ;; ... it starts with a field only containing "/",
3649 (equal first-cell
'("/"))
3650 ;; ... the table contains a special column and the row start
3651 ;; with a marking character among, "^", "_", "$" or "!",
3652 (and (org-export-table-has-special-column-p
3653 (org-export-get-parent table-row
))
3654 (member first-cell
'(("^") ("_") ("$") ("!"))))
3655 ;; ... it contains only alignment cookies and empty cells.
3656 (let ((special-row-p 'empty
))
3660 (let ((value (org-element-contents cell
)))
3661 ;; Since VALUE is a secondary string, the following
3662 ;; checks avoid expanding it with `org-export-data'.
3664 ((and (not (cdr value
))
3665 (stringp (car value
))
3666 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'"
3668 (setq special-row-p
'cookie
))
3669 (t (throw 'exit nil
)))))
3670 (org-element-contents table-row
))
3671 (eq special-row-p
'cookie
)))))))
3673 (defun org-export-table-row-group (table-row info
)
3674 "Return TABLE-ROW's group.
3676 INFO is a plist used as the communication channel.
3678 Return value is the group number, as an integer, or nil special
3679 rows and table rules. Group 1 is also table's header."
3680 (unless (or (eq (org-element-property :type table-row
) 'rule
)
3681 (org-export-table-row-is-special-p table-row info
))
3682 (let ((group 0) row-flag
)
3687 ((and (eq (org-element-property :type row
) 'standard
)
3688 (not (org-export-table-row-is-special-p row info
)))
3689 (unless row-flag
(incf group
) (setq row-flag t
)))
3690 ((eq (org-element-property :type row
) 'rule
)
3691 (setq row-flag nil
)))
3692 (when (eq table-row row
) (throw 'found group
)))
3693 (org-element-contents (org-export-get-parent table-row
)))))))
3695 (defun org-export-table-cell-width (table-cell info
)
3696 "Return TABLE-CELL contents width.
3698 INFO is a plist used as the communication channel.
3700 Return value is the width given by the last width cookie in the
3701 same column as TABLE-CELL, or nil."
3702 (let* ((row (org-export-get-parent table-cell
))
3703 (column (let ((cells (org-element-contents row
)))
3704 (- (length cells
) (length (memq table-cell cells
)))))
3705 (table (org-export-get-parent-table table-cell
))
3710 ;; In a special row, try to find a width cookie at COLUMN.
3711 ((org-export-table-row-is-special-p row info
)
3712 (let ((value (org-element-contents
3713 (elt (org-element-contents row
) column
))))
3714 ;; The following checks avoid expanding unnecessarily the
3715 ;; cell with `org-export-data'
3718 (stringp (car value
))
3719 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'" (car value
))
3720 (match-string 1 (car value
)))
3722 (string-to-number (match-string 1 (car value
)))))))
3723 ;; Ignore table rules.
3724 ((eq (org-element-property :type row
) 'rule
))))
3725 (org-element-contents table
))
3729 (defun org-export-table-cell-alignment (table-cell info
)
3730 "Return TABLE-CELL contents alignment.
3732 INFO is a plist used as the communication channel.
3734 Return alignment as specified by the last alignment cookie in the
3735 same column as TABLE-CELL. If no such cookie is found, a default
3736 alignment value will be deduced from fraction of numbers in the
3737 column (see `org-table-number-fraction' for more information).
3738 Possible values are `left', `right' and `center'."
3739 (let* ((row (org-export-get-parent table-cell
))
3740 (column (let ((cells (org-element-contents row
)))
3741 (- (length cells
) (length (memq table-cell cells
)))))
3742 (table (org-export-get-parent-table table-cell
))
3749 ;; In a special row, try to find an alignment cookie at
3751 ((org-export-table-row-is-special-p row info
)
3752 (let ((value (org-element-contents
3753 (elt (org-element-contents row
) column
))))
3754 ;; Since VALUE is a secondary string, the following checks
3755 ;; avoid useless expansion through `org-export-data'.
3758 (stringp (car value
))
3759 (string-match "\\`<\\([lrc]\\)?\\([0-9]+\\)?>\\'"
3761 (match-string 1 (car value
)))
3762 (setq cookie-align
(match-string 1 (car value
))))))
3763 ;; Ignore table rules.
3764 ((eq (org-element-property :type row
) 'rule
))
3765 ;; In a standard row, check if cell's contents are expressing
3766 ;; some kind of number. Increase NUMBER-CELLS accordingly.
3767 ;; Though, don't bother if an alignment cookie has already
3768 ;; defined cell's alignment.
3770 (let ((value (org-export-data
3771 (org-element-contents
3772 (elt (org-element-contents row
) column
))
3775 (when (string-match org-table-number-regexp value
)
3776 (incf number-cells
))))))
3777 (org-element-contents table
))
3778 ;; Return value. Alignment specified by cookies has precedence
3779 ;; over alignment deduced from cells contents.
3780 (cond ((equal cookie-align
"l") 'left
)
3781 ((equal cookie-align
"r") 'right
)
3782 ((equal cookie-align
"c") 'center
)
3783 ((>= (/ (float number-cells
) total-cells
) org-table-number-fraction
)
3787 (defun org-export-table-cell-borders (table-cell info
)
3788 "Return TABLE-CELL borders.
3790 INFO is a plist used as a communication channel.
3792 Return value is a list of symbols, or nil. Possible values are:
3793 `top', `bottom', `above', `below', `left' and `right'. Note:
3794 `top' (resp. `bottom') only happen for a cell in the first
3795 row (resp. last row) of the table, ignoring table rules, if any.
3797 Returned borders ignore special rows."
3798 (let* ((row (org-export-get-parent table-cell
))
3799 (table (org-export-get-parent-table table-cell
))
3801 ;; Top/above border? TABLE-CELL has a border above when a rule
3802 ;; used to demarcate row groups can be found above. Hence,
3803 ;; finding a rule isn't sufficient to push `above' in BORDERS:
3804 ;; another regular row has to be found above that rule.
3808 (cond ((eq (org-element-property :type row
) 'rule
)
3810 ((not (org-export-table-row-is-special-p row info
))
3811 (if rule-flag
(throw 'exit
(push 'above borders
))
3812 (throw 'exit nil
)))))
3813 ;; Look at every row before the current one.
3814 (cdr (memq row
(reverse (org-element-contents table
)))))
3815 ;; No rule above, or rule found starts the table (ignoring any
3816 ;; special row): TABLE-CELL is at the top of the table.
3817 (when rule-flag
(push 'above borders
))
3818 (push 'top borders
)))
3819 ;; Bottom/below border? TABLE-CELL has a border below when next
3820 ;; non-regular row below is a rule.
3824 (cond ((eq (org-element-property :type row
) 'rule
)
3826 ((not (org-export-table-row-is-special-p row info
))
3827 (if rule-flag
(throw 'exit
(push 'below borders
))
3828 (throw 'exit nil
)))))
3829 ;; Look at every row after the current one.
3830 (cdr (memq row
(org-element-contents table
))))
3831 ;; No rule below, or rule found ends the table (modulo some
3832 ;; special row): TABLE-CELL is at the bottom of the table.
3833 (when rule-flag
(push 'below borders
))
3834 (push 'bottom borders
)))
3835 ;; Right/left borders? They can only be specified by column
3836 ;; groups. Column groups are defined in a row starting with "/".
3837 ;; Also a column groups row only contains "<", "<>", ">" or blank
3840 (let ((column (let ((cells (org-element-contents row
)))
3841 (- (length cells
) (length (memq table-cell cells
))))))
3844 (unless (eq (org-element-property :type row
) 'rule
)
3845 (when (equal (org-element-contents
3846 (car (org-element-contents row
)))
3848 (let ((column-groups
3851 (let ((value (org-element-contents cell
)))
3852 (when (member value
'(("<") ("<>") (">") nil
))
3854 (org-element-contents row
))))
3855 ;; There's a left border when previous cell, if
3856 ;; any, ends a group, or current one starts one.
3857 (when (or (and (not (zerop column
))
3858 (member (elt column-groups
(1- column
))
3860 (member (elt column-groups column
) '("<" "<>")))
3861 (push 'left borders
))
3862 ;; There's a right border when next cell, if any,
3863 ;; starts a group, or current one ends one.
3864 (when (or (and (/= (1+ column
) (length column-groups
))
3865 (member (elt column-groups
(1+ column
))
3867 (member (elt column-groups column
) '(">" "<>")))
3868 (push 'right borders
))
3869 (throw 'exit nil
)))))
3870 ;; Table rows are read in reverse order so last column groups
3871 ;; row has precedence over any previous one.
3872 (reverse (org-element-contents table
)))))
3876 (defun org-export-table-cell-starts-colgroup-p (table-cell info
)
3877 "Non-nil when TABLE-CELL is at the beginning of a row group.
3878 INFO is a plist used as a communication channel."
3879 ;; A cell starts a column group either when it is at the beginning
3880 ;; of a row (or after the special column, if any) or when it has
3882 (or (eq (org-element-map
3883 (org-export-get-parent table-cell
)
3884 'table-cell
'identity info
'first-match
)
3886 (memq 'left
(org-export-table-cell-borders table-cell info
))))
3888 (defun org-export-table-cell-ends-colgroup-p (table-cell info
)
3889 "Non-nil when TABLE-CELL is at the end of a row group.
3890 INFO is a plist used as a communication channel."
3891 ;; A cell ends a column group either when it is at the end of a row
3892 ;; or when it has a right border.
3893 (or (eq (car (last (org-element-contents
3894 (org-export-get-parent table-cell
))))
3896 (memq 'right
(org-export-table-cell-borders table-cell info
))))
3898 (defun org-export-table-row-starts-rowgroup-p (table-row info
)
3899 "Non-nil when TABLE-ROW is at the beginning of a column group.
3900 INFO is a plist used as a communication channel."
3901 (unless (or (eq (org-element-property :type table-row
) 'rule
)
3902 (org-export-table-row-is-special-p table-row info
))
3903 (let ((borders (org-export-table-cell-borders
3904 (car (org-element-contents table-row
)) info
)))
3905 (or (memq 'top borders
) (memq 'above borders
)))))
3907 (defun org-export-table-row-ends-rowgroup-p (table-row info
)
3908 "Non-nil when TABLE-ROW is at the end of a column group.
3909 INFO is a plist used as a communication channel."
3910 (unless (or (eq (org-element-property :type table-row
) 'rule
)
3911 (org-export-table-row-is-special-p table-row info
))
3912 (let ((borders (org-export-table-cell-borders
3913 (car (org-element-contents table-row
)) info
)))
3914 (or (memq 'bottom borders
) (memq 'below borders
)))))
3916 (defun org-export-table-row-starts-header-p (table-row info
)
3917 "Non-nil when TABLE-ROW is the first table header's row.
3918 INFO is a plist used as a communication channel."
3919 (and (org-export-table-has-header-p
3920 (org-export-get-parent-table table-row
) info
)
3921 (org-export-table-row-starts-rowgroup-p table-row info
)
3922 (= (org-export-table-row-group table-row info
) 1)))
3924 (defun org-export-table-row-ends-header-p (table-row info
)
3925 "Non-nil when TABLE-ROW is the last table header's row.
3926 INFO is a plist used as a communication channel."
3927 (and (org-export-table-has-header-p
3928 (org-export-get-parent-table table-row
) info
)
3929 (org-export-table-row-ends-rowgroup-p table-row info
)
3930 (= (org-export-table-row-group table-row info
) 1)))
3932 (defun org-export-table-dimensions (table info
)
3933 "Return TABLE dimensions.
3935 INFO is a plist used as a communication channel.
3937 Return value is a CONS like (ROWS . COLUMNS) where
3938 ROWS (resp. COLUMNS) is the number of exportable
3939 rows (resp. columns)."
3940 (let (first-row (columns 0) (rows 0))
3941 ;; Set number of rows, and extract first one.
3945 (when (eq (org-element-property :type row
) 'standard
)
3947 (unless first-row
(setq first-row row
)))) info
)
3948 ;; Set number of columns.
3949 (org-element-map first-row
'table-cell
(lambda (cell) (incf columns
)) info
)
3951 (cons rows columns
)))
3953 (defun org-export-table-cell-address (table-cell info
)
3954 "Return address of a regular TABLE-CELL object.
3956 TABLE-CELL is the cell considered. INFO is a plist used as
3957 a communication channel.
3959 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
3960 zero-based index. Only exportable cells are considered. The
3961 function returns nil for other cells."
3962 (let* ((table-row (org-export-get-parent table-cell
))
3963 (table (org-export-get-parent-table table-cell
)))
3964 ;; Ignore cells in special rows or in special column.
3965 (unless (or (org-export-table-row-is-special-p table-row info
)
3966 (and (org-export-table-has-special-column-p table
)
3967 (eq (car (org-element-contents table-row
)) table-cell
)))
3970 (let ((row-count 0))
3974 (cond ((eq (org-element-property :type row
) 'rule
) nil
)
3975 ((eq row table-row
) row-count
)
3976 (t (incf row-count
) nil
)))
3979 (let ((col-count 0))
3981 table-row
'table-cell
3983 (if (eq cell table-cell
) col-count
(incf col-count
) nil
))
3984 info
'first-match
))))))
3986 (defun org-export-get-table-cell-at (address table info
)
3987 "Return regular table-cell object at ADDRESS in TABLE.
3989 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
3990 zero-based index. TABLE is a table type element. INFO is
3991 a plist used as a communication channel.
3993 If no table-cell, among exportable cells, is found at ADDRESS,
3995 (let ((column-pos (cdr address
)) (column-count 0))
3997 ;; Row at (car address) or nil.
3998 (let ((row-pos (car address
)) (row-count 0))
4002 (cond ((eq (org-element-property :type row
) 'rule
) nil
)
4003 ((= row-count row-pos
) row
)
4004 (t (incf row-count
) nil
)))
4008 (if (= column-count column-pos
) cell
4009 (incf column-count
) nil
))
4010 info
'first-match
)))
4013 ;;;; For Tables Of Contents
4015 ;; `org-export-collect-headlines' builds a list of all exportable
4016 ;; headline elements, maybe limited to a certain depth. One can then
4017 ;; easily parse it and transcode it.
4019 ;; Building lists of tables, figures or listings is quite similar.
4020 ;; Once the generic function `org-export-collect-elements' is defined,
4021 ;; `org-export-collect-tables', `org-export-collect-figures' and
4022 ;; `org-export-collect-listings' can be derived from it.
4024 (defun org-export-collect-headlines (info &optional n
)
4025 "Collect headlines in order to build a table of contents.
4027 INFO is a plist used as a communication channel.
4029 When non-nil, optional argument N must be an integer. It
4030 specifies the depth of the table of contents.
4032 Return a list of all exportable headlines as parsed elements."
4034 (plist-get info
:parse-tree
)
4037 ;; Strip contents from HEADLINE.
4038 (let ((relative-level (org-export-get-relative-level headline info
)))
4039 (unless (and n
(> relative-level n
)) headline
)))
4042 (defun org-export-collect-elements (type info
&optional predicate
)
4043 "Collect referenceable elements of a determined type.
4045 TYPE can be a symbol or a list of symbols specifying element
4046 types to search. Only elements with a caption are collected.
4048 INFO is a plist used as a communication channel.
4050 When non-nil, optional argument PREDICATE is a function accepting
4051 one argument, an element of type TYPE. It returns a non-nil
4052 value when that element should be collected.
4054 Return a list of all elements found, in order of appearance."
4056 (plist-get info
:parse-tree
) type
4058 (and (org-element-property :caption element
)
4059 (or (not predicate
) (funcall predicate element
))
4063 (defun org-export-collect-tables (info)
4064 "Build a list of tables.
4065 INFO is a plist used as a communication channel.
4067 Return a list of table elements with a caption."
4068 (org-export-collect-elements 'table info
))
4070 (defun org-export-collect-figures (info predicate
)
4071 "Build a list of figures.
4073 INFO is a plist used as a communication channel. PREDICATE is
4074 a function which accepts one argument: a paragraph element and
4075 whose return value is non-nil when that element should be
4078 A figure is a paragraph type element, with a caption, verifying
4079 PREDICATE. The latter has to be provided since a \"figure\" is
4080 a vague concept that may depend on back-end.
4082 Return a list of elements recognized as figures."
4083 (org-export-collect-elements 'paragraph info predicate
))
4085 (defun org-export-collect-listings (info)
4086 "Build a list of src blocks.
4088 INFO is a plist used as a communication channel.
4090 Return a list of src-block elements with a caption."
4091 (org-export-collect-elements 'src-block info
))
4096 ;; Here are various functions to retrieve information about the
4097 ;; neighbourhood of a given element or object. Neighbours of interest
4098 ;; are direct parent (`org-export-get-parent'), parent headline
4099 ;; (`org-export-get-parent-headline'), first element containing an
4100 ;; object, (`org-export-get-parent-element'), parent table
4101 ;; (`org-export-get-parent-table'), previous element or object
4102 ;; (`org-export-get-previous-element') and next element or object
4103 ;; (`org-export-get-next-element').
4105 ;; `org-export-get-genealogy' returns the full genealogy of a given
4106 ;; element or object, from closest parent to full parse tree.
4108 (defun org-export-get-parent (blob)
4109 "Return BLOB parent or nil.
4110 BLOB is the element or object considered."
4111 (org-element-property :parent blob
))
4113 (defun org-export-get-genealogy (blob)
4114 "Return full genealogy relative to a given element or object.
4116 BLOB is the element or object being considered.
4118 Ancestors are returned from closest to farthest, the last one
4119 being the full parse tree."
4120 (let (genealogy (parent blob
))
4121 (while (setq parent
(org-element-property :parent parent
))
4122 (push parent genealogy
))
4123 (nreverse genealogy
)))
4125 (defun org-export-get-parent-headline (blob)
4126 "Return BLOB parent headline or nil.
4127 BLOB is the element or object being considered."
4128 (let ((parent blob
))
4129 (while (and (setq parent
(org-element-property :parent parent
))
4130 (not (eq (org-element-type parent
) 'headline
))))
4133 (defun org-export-get-parent-element (object)
4134 "Return first element containing OBJECT or nil.
4135 OBJECT is the object to consider."
4136 (let ((parent object
))
4137 (while (and (setq parent
(org-element-property :parent parent
))
4138 (memq (org-element-type parent
) org-element-all-objects
)))
4141 (defun org-export-get-parent-table (object)
4142 "Return OBJECT parent table or nil.
4143 OBJECT is either a `table-cell' or `table-element' type object."
4144 (let ((parent object
))
4145 (while (and (setq parent
(org-element-property :parent parent
))
4146 (not (eq (org-element-type parent
) 'table
))))
4149 (defun org-export-get-previous-element (blob)
4150 "Return previous element or object.
4151 BLOB is an element or object. Return previous element or object,
4153 (let ((parent (org-export-get-parent blob
)))
4154 (cadr (memq blob
(reverse (org-element-contents parent
))))))
4156 (defun org-export-get-next-element (blob)
4157 "Return next element or object.
4158 BLOB is an element or object. Return next element or object,
4160 (let ((parent (org-export-get-parent blob
)))
4161 (cadr (memq blob
(org-element-contents parent
)))))
4166 ;; `org-export-translate' translates a string according to language
4167 ;; specified by LANGUAGE keyword or `org-export-language-setup'
4168 ;; variable and a specified charset. `org-export-dictionary' contains
4169 ;; the dictionary used for the translation.
4171 (defconst org-export-dictionary
4186 :ascii
"Notes de bas de page"
4187 :latin1
"Notes de bas de page"
4188 :utf-8
"Notes de bas de page"))
4191 :ascii
"Liste des programmes"
4192 :latin1
"Liste des programmes"
4193 :utf-8
"Liste des programmes"))
4196 :ascii
"Liste des tableaux"
4197 :latin1
"Liste des tableaux"
4198 :utf-8
"Liste des tableaux"))
4201 :ascii
"Programme %d :"
4202 :latin1
"Programme %d :"
4203 :utf-8
"Programme nº %d :"))
4206 :ascii
"Programme %d : %s"
4207 :latin1
"Programme %d : %s"
4208 :utf-8
"Programme nº %d : %s"))
4211 :ascii
"cf. section %s"
4212 :latin1
"cf. section %s"
4213 :utf-8
"cf. section %s"))
4216 :ascii
"Tableau %d :"
4217 :latin1
"Tableau %d :"
4218 :utf-8
"Tableau nº %d :"))
4221 :ascii
"Tableau %d : %s"
4222 :latin1
"Tableau %d : %s"
4223 :utf-8
"Tableau nº %d : %s"))
4224 ("Table of Contents"
4227 :latin1
"Table des matières"
4228 :utf-8
"Table des matières"))
4229 ("Unknown reference"
4231 :ascii
"Destination inconnue"
4232 :latin1
"Référence inconnue"
4233 :utf-8
"Référence inconnue")))
4234 "Dictionary for export engine.
4236 Alist whose CAR is the string to translate and CDR is an alist
4237 whose CAR is the language string and CDR is a plist whose
4238 properties are possible charsets and values translated terms.
4240 It is used as a database for `org-export-translate'. Since this
4241 function returns the string as-is if no translation was found,
4242 the variable only needs to record values different from the
4245 (defun org-export-translate (s encoding info
)
4246 "Translate string S according to language specification.
4248 ENCODING is a symbol among `:ascii', `:html', `:latex', `:latin1'
4249 and `:utf8'. INFO is a plist used as a communication channel.
4251 Translation depends on `:language' property. Return the
4252 translated string. If no translation is found return S."
4253 (let ((lang (plist-get info
:language
))
4254 (translations (cdr (assoc s org-export-dictionary
))))
4255 (or (plist-get (cdr (assoc lang translations
)) encoding
) s
)))
4261 ;; `org-export-dispatch' is the standard interactive way to start an
4262 ;; export process. It uses `org-export-dispatch-ui' as a subroutine
4263 ;; for its interface. Most commons back-ends should have an entry in
4266 (defun org-export-dispatch ()
4267 "Export dispatcher for Org mode.
4269 It provides an access to common export related tasks in a buffer.
4270 Its interface comes in two flavours: standard and expert. While
4271 both share the same set of bindings, only the former displays the
4272 valid keys associations. Set `org-export-dispatch-use-expert-ui'
4273 to switch to one or the other.
4275 Return an error if key pressed has no associated command."
4277 (let* ((input (org-export-dispatch-ui
4278 (if (listp org-export-initial-scope
) org-export-initial-scope
4279 (list org-export-initial-scope
))
4280 org-export-dispatch-use-expert-ui
))
4281 (raw-key (car input
))
4282 (optns (cdr input
)))
4283 ;; Translate "C-a", "C-b"... into "a", "b"... Then take action
4284 ;; depending on user's key pressed.
4285 (case (if (< raw-key
27) (+ raw-key
96) raw-key
)
4286 ;; Allow to quit with "q" key.
4288 ;; Export with `e-ascii' back-end.
4290 (org-e-ascii-export-as-ascii
4291 (memq 'subtree optns
) (memq 'visible optns
) (memq 'body optns
)
4292 `(:ascii-charset
,(case raw-key
(?A
'ascii
) (?N
'latin1
) (t 'utf-8
)))))
4294 (org-e-ascii-export-to-ascii
4295 (memq 'subtree optns
) (memq 'visible optns
) (memq 'body optns
)
4296 `(:ascii-charset
,(case raw-key
(?a
'ascii
) (?n
'latin1
) (t 'utf-8
)))))
4297 ;; Export with `e-latex' back-end.
4298 (?L
(org-e-latex-export-as-latex
4299 (memq 'subtree optns
) (memq 'visible optns
) (memq 'body optns
)))
4301 (org-e-latex-export-to-latex
4302 (memq 'subtree optns
) (memq 'visible optns
) (memq 'body optns
)))
4304 (org-e-latex-export-to-pdf
4305 (memq 'subtree optns
) (memq 'visible optns
) (memq 'body optns
)))
4308 (org-e-latex-export-to-pdf
4309 (memq 'subtree optns
) (memq 'visible optns
) (memq 'body optns
))))
4310 ;; Export with `e-html' back-end.
4312 (org-e-html-export-as-html
4313 (memq 'subtree optns
) (memq 'visible optns
) (memq 'body optns
)))
4315 (org-e-html-export-to-html
4316 (memq 'subtree optns
) (memq 'visible optns
) (memq 'body optns
)))
4319 (org-e-html-export-to-html
4320 (memq 'subtree optns
) (memq 'visible optns
) (memq 'body optns
))))
4321 ;; Export with `e-odt' back-end.
4323 (org-e-odt-export-to-odt
4324 (memq 'subtree optns
) (memq 'visible optns
) (memq 'body optns
)))
4327 (org-e-odt-export-to-odt
4328 (memq 'subtree optns
) (memq 'visible optns
) (memq 'body optns
))))
4329 ;; Publishing facilities
4331 (org-e-publish-current-file (memq 'force optns
)))
4333 (org-e-publish-current-project (memq 'force optns
)))
4336 (assoc (org-icompleting-read
4337 "Publish project: " org-e-publish-project-alist nil t
)
4338 org-e-publish-project-alist
)))
4339 (org-e-publish project
(memq 'force optns
))))
4341 (org-e-publish-all (memq 'force optns
)))
4342 ;; Undefined command.
4343 (t (error "No command associated with key %s"
4344 (char-to-string raw-key
))))))
4346 (defun org-export-dispatch-ui (options expertp
)
4347 "Handle interface for `org-export-dispatch'.
4349 OPTIONS is a list containing current interactive options set for
4350 export. It can contain any of the following symbols:
4351 `body' toggles a body-only export
4352 `subtree' restricts export to current subtree
4353 `visible' restricts export to visible part of buffer.
4354 `force' force publishing files.
4356 EXPERTP, when non-nil, triggers expert UI. In that case, no help
4357 buffer is provided, but indications about currently active
4358 options are given in the prompt. Moreover, \[?] allows to switch
4359 back to standard interface.
4361 Return value is a list with key pressed as CAR and a list of
4362 final interactive export options as CDR."
4364 (format "---- (Options) -------------------------------------------
4366 \[1] Body only: %s [2] Export scope: %s
4367 \[3] Visible only: %s [4] Force publishing: %s
4370 --- (ASCII/Latin-1/UTF-8 Export) -------------------------
4372 \[a/n/u] to TXT file [A/N/U] to temporary buffer
4374 --- (HTML Export) ----------------------------------------
4376 \[h] to HTML file [b] ... and open it
4377 \[H] to temporary buffer
4379 --- (LaTeX Export) ---------------------------------------
4381 \[l] to TEX file [L] to temporary buffer
4382 \[p] to PDF file [d] ... and open it
4384 --- (ODF Export) -----------------------------------------
4386 \[o] to ODT file [O] ... and open it
4388 --- (Publish) --------------------------------------------
4390 \[F] current file [P] current project
4391 \[X] a project [E] every project"
4392 (if (memq 'body options
) "On " "Off")
4393 (if (memq 'subtree options
) "Subtree" "Buffer ")
4394 (if (memq 'visible options
) "On " "Off")
4395 (if (memq 'force options
) "On " "Off")))
4396 (standard-prompt "Export command: ")
4397 (expert-prompt (format "Export command (%s%s%s%s): "
4398 (if (memq 'body options
) "b" "-")
4399 (if (memq 'subtree options
) "s" "-")
4400 (if (memq 'visible options
) "v" "-")
4401 (if (memq 'force options
) "f" "-")))
4404 ;; Read a character from command input, toggling interactive
4405 ;; options when applicable. PROMPT is the displayed prompt,
4408 (let ((key (read-char-exclusive prompt
)))
4410 ;; Ignore non-standard characters (i.e. "M-a").
4411 ((not (characterp key
)) (org-export-dispatch-ui options expertp
))
4412 ;; Help key: Switch back to standard interface if
4413 ;; expert UI was active.
4414 ((eq key ??
) (org-export-dispatch-ui options nil
))
4415 ;; Toggle export options.
4416 ((memq key
'(?
1 ?
2 ?
3 ?
4))
4417 (org-export-dispatch-ui
4418 (let ((option (case key
(?
1 'body
) (?
2 'subtree
) (?
3 'visible
)
4420 (if (memq option options
) (remq option options
)
4421 (cons option options
)))
4423 ;; Action selected: Send key and options back to
4424 ;; `org-export-dispatch'.
4425 (t (cons key options
))))))))
4426 ;; With expert UI, just read key with a fancy prompt. In standard
4427 ;; UI, display an intrusive help buffer.
4428 (if expertp
(funcall handle-keypress expert-prompt
)
4429 (save-window-excursion
4430 (delete-other-windows)
4431 (with-output-to-temp-buffer "*Org Export/Publishing Help*" (princ help
))
4432 (org-fit-window-to-buffer
4433 (get-buffer-window "*Org Export/Publishing Help*"))
4434 (funcall handle-keypress standard-prompt
)))))
4437 (provide 'org-export
)
4438 ;;; org-export.el ends here