org-export: Remove call to a phantom function
[org-mode.git] / contrib / lisp / org-export.el
blobc4e95e725c6fcc979e5f12fa4c482197ffaf2781
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/>.
21 ;;; Commentary:
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
27 ;; parts:
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
34 ;; this file.
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.
46 ;; The core function is `org-export-as'. It returns the transcoded
47 ;; buffer as a string.
49 ;; In order to derive an exporter out of this generic implementation,
50 ;; one can define a transcode function for each element or object.
51 ;; Such function should return a string for the corresponding element,
52 ;; without any trailing space, or nil. It must accept three
53 ;; arguments:
54 ;; 1. the element or object itself,
55 ;; 2. its contents, or nil when it isn't recursive,
56 ;; 3. the property list used as a communication channel.
58 ;; If no such function is found, that element or object type will
59 ;; simply be ignored, along with any separating blank line. The same
60 ;; will happen if the function returns the nil value. If that
61 ;; function returns the empty string, the type will be ignored, but
62 ;; the blank lines will be kept.
64 ;; Contents, when not nil, are stripped from any global indentation
65 ;; (although the relative one is preserved). They also always end
66 ;; with a single newline character.
68 ;; These functions must follow a strict naming convention:
69 ;; `org-BACKEND-TYPE' where, obviously, BACKEND is the name of the
70 ;; export back-end and TYPE the type of the element or object handled.
72 ;; Moreover, two additional functions can be defined. On the one
73 ;; hand, `org-BACKEND-template' returns the final transcoded string,
74 ;; and can be used to add a preamble and a postamble to document's
75 ;; body. It must accept two arguments: the transcoded string and the
76 ;; property list containing export options. On the other hand,
77 ;; `org-BACKEND-plain-text', when defined, is to be called on every
78 ;; text not recognized as an element or an object. It must accept two
79 ;; arguments: the text string and the information channel.
81 ;; Any back-end can define its own variables. Among them, those
82 ;; customizables should belong to the `org-export-BACKEND' group.
83 ;; Also, a special variable, `org-BACKEND-option-alist', allows to
84 ;; define buffer keywords and "#+options:" items specific to that
85 ;; back-end. See `org-export-option-alist' for supported defaults and
86 ;; syntax.
88 ;; Tools for common tasks across back-ends are implemented in the
89 ;; penultimate part of this file. A dispatcher for standard back-ends
90 ;; is provided in the last one.
92 ;;; Code:
93 (eval-when-compile (require 'cl))
94 (require 'org-element)
95 ;; Require major back-ends and publishing tools
96 (require 'org-e-ascii "../../EXPERIMENTAL/org-e-ascii.el")
97 (require 'org-e-html "../../EXPERIMENTAL/org-e-html.el")
98 (require 'org-e-latex "../../EXPERIMENTAL/org-e-latex.el")
99 (require 'org-e-publish "../../EXPERIMENTAL/org-e-publish.el")
102 ;;; Internal Variables
104 ;; Among internal variables, the most important is
105 ;; `org-export-option-alist'. This variable define the global export
106 ;; options, shared between every exporter, and how they are acquired.
108 (defconst org-export-max-depth 19
109 "Maximum nesting depth for headlines, counting from 0.")
111 (defconst org-export-option-alist
112 '((:author "AUTHOR" nil user-full-name t)
113 (:creator "CREATOR" nil org-export-creator-string)
114 (:date "DATE" nil nil t)
115 (:description "DESCRIPTION" nil nil newline)
116 (:email "EMAIL" nil user-mail-address t)
117 (:exclude-tags "EXPORT_EXCLUDE_TAGS" nil org-export-exclude-tags split)
118 (:headline-levels nil "H" org-export-headline-levels)
119 (:keywords "KEYWORDS" nil nil space)
120 (:language "LANGUAGE" nil org-export-default-language t)
121 (:preserve-breaks nil "\\n" org-export-preserve-breaks)
122 (:section-numbers nil "num" org-export-with-section-numbers)
123 (:select-tags "EXPORT_SELECT_TAGS" nil org-export-select-tags split)
124 (:time-stamp-file nil "timestamp" org-export-time-stamp-file)
125 (:title "TITLE" nil nil space)
126 (:with-archived-trees nil "arch" org-export-with-archived-trees)
127 (:with-author nil "author" org-export-with-author)
128 (:with-creator nil "creator" org-export-with-creator)
129 (:with-drawers nil "d" org-export-with-drawers)
130 (:with-email nil "email" org-export-with-email)
131 (:with-emphasize nil "*" org-export-with-emphasize)
132 (:with-entities nil "e" org-export-with-entities)
133 (:with-fixed-width nil ":" org-export-with-fixed-width)
134 (:with-footnotes nil "f" org-export-with-footnotes)
135 (:with-priority nil "pri" org-export-with-priority)
136 (:with-special-strings nil "-" org-export-with-special-strings)
137 (:with-sub-superscript nil "^" org-export-with-sub-superscripts)
138 (:with-toc nil "toc" org-export-with-toc)
139 (:with-tables nil "|" org-export-with-tables)
140 (:with-tags nil "tags" org-export-with-tags)
141 (:with-tasks nil "tasks" org-export-with-tasks)
142 (:with-timestamps nil "<" org-export-with-timestamps)
143 (:with-todo-keywords nil "todo" org-export-with-todo-keywords))
144 "Alist between export properties and ways to set them.
146 The car of the alist is the property name, and the cdr is a list
147 like \(KEYWORD OPTION DEFAULT BEHAVIOUR\) where:
149 KEYWORD is a string representing a buffer keyword, or nil.
150 OPTION is a string that could be found in an #+OPTIONS: line.
151 DEFAULT is the default value for the property.
152 BEHAVIOUR determine how Org should handle multiple keywords for
153 the same property. It is a symbol among:
154 nil Keep old value and discard the new one.
155 t Replace old value with the new one.
156 `space' Concatenate the values, separating them with a space.
157 `newline' Concatenate the values, separating them with
158 a newline.
159 `split' Split values at white spaces, and cons them to the
160 previous list.
162 KEYWORD and OPTION have precedence over DEFAULT.
164 All these properties should be back-end agnostic. For back-end
165 specific properties, define a similar variable named
166 `org-BACKEND-option-alist', replacing BACKEND with the name of
167 the appropriate back-end. You can also redefine properties
168 there, as they have precedence over these.")
170 (defconst org-export-special-keywords
171 '("SETUP_FILE" "OPTIONS" "MACRO")
172 "List of in-buffer keywords that require special treatment.
173 These keywords are not directly associated to a property. The
174 way they are handled must be hard-coded into
175 `org-export-get-inbuffer-options' function.")
177 (defconst org-export-filters-alist
178 '((:filter-babel-call . org-export-filter-babel-call-functions)
179 (:filter-center-block . org-export-filter-center-block-functions)
180 (:filter-comment . org-export-filter-comment-functions)
181 (:filter-comment-block . org-export-filter-comment-block-functions)
182 (:filter-drawer . org-export-filter-drawer-functions)
183 (:filter-dynamic-block . org-export-filter-dynamic-block-functions)
184 (:filter-emphasis . org-export-filter-emphasis-functions)
185 (:filter-entity . org-export-filter-entity-functions)
186 (:filter-example-block . org-export-filter-example-block-functions)
187 (:filter-export-block . org-export-filter-export-block-functions)
188 (:filter-export-snippet . org-export-filter-export-snippet-functions)
189 (:filter-final-output . org-export-filter-final-output-functions)
190 (:filter-fixed-width . org-export-filter-fixed-width-functions)
191 (:filter-footnote-definition . org-export-filter-footnote-definition-functions)
192 (:filter-footnote-reference . org-export-filter-footnote-reference-functions)
193 (:filter-headline . org-export-filter-headline-functions)
194 (:filter-horizontal-rule . org-export-filter-horizontal-rule-functions)
195 (:filter-inline-babel-call . org-export-filter-inline-babel-call-functions)
196 (:filter-inline-src-block . org-export-filter-inline-src-block-functions)
197 (:filter-inlinetask . org-export-filter-inlinetask-functions)
198 (:filter-item . org-export-filter-item-functions)
199 (:filter-keyword . org-export-filter-keyword-functions)
200 (:filter-latex-environment . org-export-filter-latex-environment-functions)
201 (:filter-latex-fragment . org-export-filter-latex-fragment-functions)
202 (:filter-line-break . org-export-filter-line-break-functions)
203 (:filter-link . org-export-filter-link-functions)
204 (:filter-macro . org-export-filter-macro-functions)
205 (:filter-paragraph . org-export-filter-paragraph-functions)
206 (:filter-parse-tree . org-export-filter-parse-tree-functions)
207 (:filter-plain-list . org-export-filter-plain-list-functions)
208 (:filter-plain-text . org-export-filter-plain-text-functions)
209 (:filter-property-drawer . org-export-filter-property-drawer-functions)
210 (:filter-quote-block . org-export-filter-quote-block-functions)
211 (:filter-quote-section . org-export-filter-quote-section-functions)
212 (:filter-radio-target . org-export-filter-radio-target-functions)
213 (:filter-section . org-export-filter-section-functions)
214 (:filter-special-block . org-export-filter-special-block-functions)
215 (:filter-src-block . org-export-filter-src-block-functions)
216 (:filter-statistics-cookie . org-export-filter-statistics-cookie-functions)
217 (:filter-subscript . org-export-filter-subscript-functions)
218 (:filter-superscript . org-export-filter-superscript-functions)
219 (:filter-table . org-export-filter-table-functions)
220 (:filter-target . org-export-filter-target-functions)
221 (:filter-time-stamp . org-export-filter-time-stamp-functions)
222 (:filter-verbatim . org-export-filter-verbatim-functions)
223 (:filter-verse-block . org-export-filter-verse-block-functions))
224 "Alist between filters properties and initial values.
226 The key of each association is a property name accessible through
227 the communication channel its value is a configurable global
228 variable defining initial filters.
230 This list is meant to install user specified filters. Back-end
231 developers may install their own filters using
232 `org-BACKEND-filters-alist', where BACKEND is the name of the
233 considered back-end. Filters defined there will always be
234 prepended to the current list, so they always get applied
235 first.")
237 (defconst org-export-default-inline-image-rule
238 `(("file" .
239 ,(format "\\.%s\\'"
240 (regexp-opt
241 '("png" "jpeg" "jpg" "gif" "tiff" "tif" "xbm"
242 "xpm" "pbm" "pgm" "ppm") t))))
243 "Default rule for link matching an inline image.
244 This rule applies to links with no description. By default, it
245 will be considered as an inline image if it targets a local file
246 whose extension is either \"png\", \"jpeg\", \"jpg\", \"gif\",
247 \"tiff\", \"tif\", \"xbm\", \"xpm\", \"pbm\", \"pgm\" or \"ppm\".
248 See `org-export-inline-image-p' for more information about
249 rules.")
253 ;;; User-configurable Variables
255 ;; Configuration for the masses.
257 ;; They should never be evaled directly, as their value is to be
258 ;; stored in a property list (cf. `org-export-option-alist').
260 (defgroup org-export nil
261 "Options for exporting Org mode files."
262 :tag "Org Export"
263 :group 'org)
265 (defgroup org-export-general nil
266 "General options for export engine."
267 :tag "Org Export General"
268 :group 'org-export)
270 (defcustom org-export-with-archived-trees 'headline
271 "Whether sub-trees with the ARCHIVE tag should be exported.
273 This can have three different values:
274 nil Do not export, pretend this tree is not present.
275 t Do export the entire tree.
276 `headline' Only export the headline, but skip the tree below it.
278 This option can also be set with the #+OPTIONS line,
279 e.g. \"arch:nil\"."
280 :group 'org-export-general
281 :type '(choice
282 (const :tag "Not at all" nil)
283 (const :tag "Headline only" 'headline)
284 (const :tag "Entirely" t)))
286 (defcustom org-export-with-author t
287 "Non-nil means insert author name into the exported file.
288 This option can also be set with the #+OPTIONS line,
289 e.g. \"author:nil\"."
290 :group 'org-export-general
291 :type 'boolean)
293 (defcustom org-export-with-creator 'comment
294 "Non-nil means the postamble should contain a creator sentence.
296 The sentence can be set in `org-export-creator-string' and
297 defaults to \"Generated by Org mode XX in Emacs XXX.\".
299 If the value is `comment' insert it as a comment."
300 :group 'org-export-general
301 :type '(choice
302 (const :tag "No creator sentence" nil)
303 (const :tag "Sentence as a comment" 'comment)
304 (const :tag "Insert the sentence" t)))
306 (defcustom org-export-creator-string
307 (format "Generated by Org mode %s in Emacs %s." org-version emacs-version)
308 "String to insert at the end of the generated document."
309 :group 'org-export-general
310 :type '(string :tag "Creator string"))
312 (defcustom org-export-with-drawers t
313 "Non-nil means export contents of standard drawers.
315 When t, all drawers are exported. This may also be a list of
316 drawer names to export. This variable doesn't apply to
317 properties drawers.
319 This option can also be set with the #+OPTIONS line,
320 e.g. \"d:nil\"."
321 :group 'org-export-general
322 :type '(choice
323 (const :tag "All drawers" t)
324 (const :tag "None" nil)
325 (repeat :tag "Selected drawers"
326 (string :tag "Drawer name"))))
328 (defcustom org-export-with-email nil
329 "Non-nil means insert author email into the exported file.
330 This option can also be set with the #+OPTIONS line,
331 e.g. \"email:t\"."
332 :group 'org-export-general
333 :type 'boolean)
335 (defcustom org-export-with-emphasize t
336 "Non-nil means interpret *word*, /word/, and _word_ as emphasized text.
338 If the export target supports emphasizing text, the word will be
339 typeset in bold, italic, or underlined, respectively. Not all
340 export backends support this.
342 This option can also be set with the #+OPTIONS line, e.g. \"*:nil\"."
343 :group 'org-export-general
344 :type 'boolean)
346 (defcustom org-export-exclude-tags '("noexport")
347 "Tags that exclude a tree from export.
348 All trees carrying any of these tags will be excluded from
349 export. This is without condition, so even subtrees inside that
350 carry one of the `org-export-select-tags' will be removed."
351 :group 'org-export-general
352 :type '(repeat (string :tag "Tag")))
354 (defcustom org-export-with-fixed-width t
355 "Non-nil means lines starting with \":\" will be in fixed width font.
357 This can be used to have pre-formatted text, fragments of code
358 etc. For example:
359 : ;; Some Lisp examples
360 : (while (defc cnt)
361 : (ding))
362 will be looking just like this in also HTML. See also the QUOTE
363 keyword. Not all export backends support this.
365 This option can also be set with the #+OPTIONS line, e.g. \"::nil\"."
366 :group 'org-export-translation
367 :type 'boolean)
369 (defcustom org-export-with-footnotes t
370 "Non-nil means Org footnotes should be exported.
371 This option can also be set with the #+OPTIONS line,
372 e.g. \"f:nil\"."
373 :group 'org-export-general
374 :type 'boolean)
376 (defcustom org-export-headline-levels 3
377 "The last level which is still exported as a headline.
379 Inferior levels will produce itemize lists when exported. Note
380 that a numeric prefix argument to an exporter function overrides
381 this setting.
383 This option can also be set with the #+OPTIONS line, e.g. \"H:2\"."
384 :group 'org-export-general
385 :type 'integer)
387 (defcustom org-export-default-language "en"
388 "The default language for export and clocktable translations, as a string.
389 This may have an association in
390 `org-clock-clocktable-language-setup'."
391 :group 'org-export-general
392 :type '(string :tag "Language"))
394 (defcustom org-export-preserve-breaks nil
395 "Non-nil means preserve all line breaks when exporting.
397 Normally, in HTML output paragraphs will be reformatted.
399 This option can also be set with the #+OPTIONS line,
400 e.g. \"\\n:t\"."
401 :group 'org-export-general
402 :type 'boolean)
404 (defcustom org-export-with-entities t
405 "Non-nil means interpret entities when exporting.
407 For example, HTML export converts \\alpha to &alpha; and \\AA to
408 &Aring;.
410 For a list of supported names, see the constant `org-entities'
411 and the user option `org-entities-user'.
413 This option can also be set with the #+OPTIONS line,
414 e.g. \"e:nil\"."
415 :group 'org-export-general
416 :type 'boolean)
418 (defcustom org-export-with-priority nil
419 "Non-nil means include priority cookies in export.
420 When nil, remove priority cookies for export."
421 :group 'org-export-general
422 :type 'boolean)
424 (defcustom org-export-with-section-numbers t
425 "Non-nil means add section numbers to headlines when exporting.
427 When set to an integer n, numbering will only happen for
428 headlines whose relative level is higher or equal to n.
430 This option can also be set with the #+OPTIONS line,
431 e.g. \"num:t\"."
432 :group 'org-export-general
433 :type 'boolean)
435 (defcustom org-export-select-tags '("export")
436 "Tags that select a tree for export.
437 If any such tag is found in a buffer, all trees that do not carry
438 one of these tags will be deleted before export. Inside trees
439 that are selected like this, you can still deselect a subtree by
440 tagging it with one of the `org-export-exclude-tags'."
441 :group 'org-export-general
442 :type '(repeat (string :tag "Tag")))
444 (defcustom org-export-with-special-strings t
445 "Non-nil means interpret \"\-\", \"--\" and \"---\" for export.
447 When this option is turned on, these strings will be exported as:
449 Org HTML LaTeX
450 -----+----------+--------
451 \\- &shy; \\-
452 -- &ndash; --
453 --- &mdash; ---
454 ... &hellip; \ldots
456 This option can also be set with the #+OPTIONS line,
457 e.g. \"-:nil\"."
458 :group 'org-export-general
459 :type 'boolean)
461 (defcustom org-export-with-sub-superscripts t
462 "Non-nil means interpret \"_\" and \"^\" for export.
464 When this option is turned on, you can use TeX-like syntax for
465 sub- and superscripts. Several characters after \"_\" or \"^\"
466 will be considered as a single item - so grouping with {} is
467 normally not needed. For example, the following things will be
468 parsed as single sub- or superscripts.
470 10^24 or 10^tau several digits will be considered 1 item.
471 10^-12 or 10^-tau a leading sign with digits or a word
472 x^2-y^3 will be read as x^2 - y^3, because items are
473 terminated by almost any nonword/nondigit char.
474 x_{i^2} or x^(2-i) braces or parenthesis do grouping.
476 Still, ambiguity is possible - so when in doubt use {} to enclose
477 the sub/superscript. If you set this variable to the symbol
478 `{}', the braces are *required* in order to trigger
479 interpretations as sub/superscript. This can be helpful in
480 documents that need \"_\" frequently in plain text.
482 This option can also be set with the #+OPTIONS line,
483 e.g. \"^:nil\"."
484 :group 'org-export-general
485 :type '(choice
486 (const :tag "Interpret them" t)
487 (const :tag "Curly brackets only" {})
488 (const :tag "Do not interpret them" nil)))
490 (defcustom org-export-with-toc t
491 "Non-nil means create a table of contents in exported files.
493 The TOC contains headlines with levels up
494 to`org-export-headline-levels'. When an integer, include levels
495 up to N in the toc, this may then be different from
496 `org-export-headline-levels', but it will not be allowed to be
497 larger than the number of headline levels. When nil, no table of
498 contents is made.
500 This option can also be set with the #+OPTIONS line,
501 e.g. \"toc:nil\" or \"toc:3\"."
502 :group 'org-export-general
503 :type '(choice
504 (const :tag "No Table of Contents" nil)
505 (const :tag "Full Table of Contents" t)
506 (integer :tag "TOC to level")))
508 (defcustom org-export-with-tables t
509 "If non-nil, lines starting with \"|\" define a table.
510 For example:
512 | Name | Address | Birthday |
513 |-------------+----------+-----------|
514 | Arthur Dent | England | 29.2.2100 |
516 This option can also be set with the #+OPTIONS line, e.g. \"|:nil\"."
517 :group 'org-export-general
518 :type 'boolean)
520 (defcustom org-export-with-tags t
521 "If nil, do not export tags, just remove them from headlines.
523 If this is the symbol `not-in-toc', tags will be removed from
524 table of contents entries, but still be shown in the headlines of
525 the document.
527 This option can also be set with the #+OPTIONS line,
528 e.g. \"tags:nil\"."
529 :group 'org-export-general
530 :type '(choice
531 (const :tag "Off" nil)
532 (const :tag "Not in TOC" not-in-toc)
533 (const :tag "On" t)))
535 (defcustom org-export-with-tasks t
536 "Non-nil means include TODO items for export.
537 This may have the following values:
538 t include tasks independent of state.
539 todo include only tasks that are not yet done.
540 done include only tasks that are already done.
541 nil remove all tasks before export
542 list of keywords keep only tasks with these keywords"
543 :group 'org-export-general
544 :type '(choice
545 (const :tag "All tasks" t)
546 (const :tag "No tasks" nil)
547 (const :tag "Not-done tasks" todo)
548 (const :tag "Only done tasks" done)
549 (repeat :tag "Specific TODO keywords"
550 (string :tag "Keyword"))))
552 (defcustom org-export-time-stamp-file t
553 "Non-nil means insert a time stamp into the exported file.
554 The time stamp shows when the file was created.
556 This option can also be set with the #+OPTIONS line,
557 e.g. \"timestamp:nil\"."
558 :group 'org-export-general
559 :type 'boolean)
561 (defcustom org-export-with-timestamps t
562 "If nil, do not export time stamps and associated keywords."
563 :group 'org-export-general
564 :type 'boolean)
566 (defcustom org-export-with-todo-keywords t
567 "Non-nil means include TODO keywords in export.
568 When nil, remove all these keywords from the export.")
570 (defcustom org-export-allow-BIND 'confirm
571 "Non-nil means allow #+BIND to define local variable values for export.
572 This is a potential security risk, which is why the user must
573 confirm the use of these lines."
574 :group 'org-export-general
575 :type '(choice
576 (const :tag "Never" nil)
577 (const :tag "Always" t)
578 (const :tag "Ask a confirmation for each file" confirm)))
580 (defcustom org-export-snippet-translation-alist nil
581 "Alist between export snippets back-ends and exporter back-ends.
583 This variable allows to provide shortcuts for export snippets.
585 For example, with a value of '\(\(\"h\" . \"html\"\)\), the HTML
586 back-end will recognize the contents of \"@h{<b>}\" as HTML code
587 while every other back-end will ignore it."
588 :group 'org-export-general
589 :type '(repeat
590 (cons
591 (string :tag "Shortcut")
592 (string :tag "Back-end"))))
594 (defcustom org-export-coding-system nil
595 "Coding system for the exported file."
596 :group 'org-export-general
597 :type 'coding-system)
599 (defcustom org-export-copy-to-kill-ring t
600 "Non-nil means exported stuff will also be pushed onto the kill ring."
601 :group 'org-export-general
602 :type 'boolean)
604 (defcustom org-export-initial-scope 'buffer
605 "The initial scope when exporting with `org-export-dispatch'.
606 This variable can be either set to `buffer' or `subtree'."
607 :group 'org-export-general
608 :type '(choice
609 (const :tag "Export current buffer" 'buffer)
610 (const :tag "Export current subtree" 'subtree)))
612 (defcustom org-export-show-temporary-export-buffer t
613 "Non-nil means show buffer after exporting to temp buffer.
614 When Org exports to a file, the buffer visiting that file is ever
615 shown, but remains buried. However, when exporting to a temporary
616 buffer, that buffer is popped up in a second window. When this variable
617 is nil, the buffer remains buried also in these cases."
618 :group 'org-export-general
619 :type 'boolean)
621 (defcustom org-export-dispatch-use-expert-ui nil
622 "Non-nil means using a non-intrusive `org-export-dispatch'.
623 In that case, no help buffer is displayed. Though, an indicator
624 for current export scope is added to the prompt \(i.e. \"b\" when
625 output is restricted to body only, \"s\" when it is restricted to
626 the current subtree and \"v\" when only visible elements are
627 considered for export\). Also, \[?] allows to switch back to
628 standard mode."
629 :group 'org-export-general
630 :type 'boolean)
634 ;;; The Communication Channel
636 ;; During export process, every function has access to a number of
637 ;; properties. They are of three types:
639 ;; 1. Environment options are collected once at the very beginning of
640 ;; the process, out of the original buffer and configuration.
641 ;; Associated to the parse tree, they make an Org closure.
642 ;; Collecting them is handled by `org-export-get-environment'
643 ;; function.
645 ;; Most environment options are defined through the
646 ;; `org-export-option-alist' variable.
648 ;; 2. Tree properties are extracted directly from the parsed tree,
649 ;; just before export, by `org-export-collect-tree-properties'.
651 ;; 3. Local options are updated during parsing, and their value
652 ;; depends on the level of recursion. For now, only `:ignore-list'
653 ;; belongs to that category.
655 ;; Here is the full list of properties available during transcode
656 ;; process, with their category (option, tree or local) and their
657 ;; value type.
659 ;; + `:author' :: Author's name.
660 ;; - category :: option
661 ;; - type :: string
663 ;; + `:back-end' :: Current back-end used for transcoding.
664 ;; - category :: tree
665 ;; - type :: symbol
667 ;; + `:creator' :: String to write as creation information.
668 ;; - category :: option
669 ;; - type :: string
671 ;; + `:date' :: String to use as date.
672 ;; - category :: option
673 ;; - type :: string
675 ;; + `:description' :: Description text for the current data.
676 ;; - category :: option
677 ;; - type :: string
679 ;; + `:email' :: Author's email.
680 ;; - category :: option
681 ;; - type :: string
683 ;; + `:exclude-tags' :: Tags for exclusion of subtrees from export
684 ;; process.
685 ;; - category :: option
686 ;; - type :: list of strings
688 ;; + `:footnote-definition-alist' :: Alist between footnote labels and
689 ;; their definition, as parsed data. Only non-inlined footnotes
690 ;; are represented in this alist. Also, every definition isn't
691 ;; guaranteed to be referenced in the parse tree. The purpose of
692 ;; this property is to preserve definitions from oblivion
693 ;; (i.e. when the parse tree comes from a part of the original
694 ;; buffer), it isn't meant for direct use in a back-end. To
695 ;; retrieve a definition relative to a reference, use
696 ;; `org-export-get-footnote-definition' instead.
697 ;; - category :: option
698 ;; - type :: alist (STRING . LIST)
700 ;; + `:headline-levels' :: Maximum level being exported as an
701 ;; headline. Comparison is done with the relative level of
702 ;; headlines in the parse tree, not necessarily with their
703 ;; actual level.
704 ;; - category :: option
705 ;; - type :: integer
707 ;; + `:headline-offset' :: Difference between relative and real level
708 ;; of headlines in the parse tree. For example, a value of -1
709 ;; means a level 2 headline should be considered as level
710 ;; 1 (cf. `org-export-get-relative-level').
711 ;; - category :: tree
712 ;; - type :: integer
714 ;; + `:headline-numbering' :: Alist between headlines and their
715 ;; numbering, as a list of numbers
716 ;; (cf. `org-export-get-headline-number').
717 ;; - category :: tree
718 ;; - type :: alist (INTEGER . LIST)
720 ;; + `:ignore-list' :: List of elements and objects that should be
721 ;; ignored during export.
722 ;; - category :: local
723 ;; - type :: list of elements and objects
725 ;; + `:input-file' :: Full path to input file, if any.
726 ;; - category :: option
727 ;; - type :: string or nil
729 ;; + `:keywords' :: List of keywords attached to data.
730 ;; - category :: option
731 ;; - type :: string
733 ;; + `:language' :: Default language used for translations.
734 ;; - category :: option
735 ;; - type :: string
737 ;; + `:macro-input-file' :: Macro returning file name of input file,
738 ;; or nil.
739 ;; - category :: option
740 ;; - type :: string or nil
742 ;; + `:parse-tree' :: Whole parse tree, available at any time during
743 ;; transcoding.
744 ;; - category :: global
745 ;; - type :: list (as returned by `org-element-parse-buffer')
747 ;; + `:preserve-breaks' :: Non-nil means transcoding should preserve
748 ;; all line breaks.
749 ;; - category :: option
750 ;; - type :: symbol (nil, t)
752 ;; + `:section-numbers' :: Non-nil means transcoding should add
753 ;; section numbers to headlines.
754 ;; - category :: option
755 ;; - type :: symbol (nil, t)
757 ;; + `:select-tags' :: List of tags enforcing inclusion of sub-trees
758 ;; in transcoding. When such a tag is present,
759 ;; subtrees without it are de facto excluded from
760 ;; the process. See `use-select-tags'.
761 ;; - category :: option
762 ;; - type :: list of strings
764 ;; + `:target-list' :: List of targets encountered in the parse tree.
765 ;; This is used to partly resolve "fuzzy" links
766 ;; (cf. `org-export-resolve-fuzzy-link').
767 ;; - category :: tree
768 ;; - type :: list of strings
770 ;; + `:time-stamp-file' :: Non-nil means transcoding should insert
771 ;; a time stamp in the output.
772 ;; - category :: option
773 ;; - type :: symbol (nil, t)
775 ;; + `:use-select-tags' :: When non-nil, a select tags has been found
776 ;; in the parse tree. Thus, any headline without one will be
777 ;; filtered out. See `select-tags'.
778 ;; - category :: tree
779 ;; - type :: interger or nil
781 ;; + `:with-archived-trees' :: Non-nil when archived subtrees should
782 ;; also be transcoded. If it is set to the `headline' symbol,
783 ;; only the archived headline's name is retained.
784 ;; - category :: option
785 ;; - type :: symbol (nil, t, `headline')
787 ;; + `:with-author' :: Non-nil means author's name should be included
788 ;; in the output.
789 ;; - category :: option
790 ;; - type :: symbol (nil, t)
792 ;; + `:with-creator' :: Non-nild means a creation sentence should be
793 ;; inserted at the end of the transcoded string. If the value
794 ;; is `comment', it should be commented.
795 ;; - category :: option
796 ;; - type :: symbol (`comment', nil, t)
798 ;; + `:with-drawers' :: Non-nil means drawers should be exported. If
799 ;; its value is a list of names, only drawers with such names
800 ;; will be transcoded.
801 ;; - category :: option
802 ;; - type :: symbol (nil, t) or list of strings
804 ;; + `:with-email' :: Non-nil means output should contain author's
805 ;; email.
806 ;; - category :: option
807 ;; - type :: symbol (nil, t)
809 ;; + `:with-emphasize' :: Non-nil means emphasized text should be
810 ;; interpreted.
811 ;; - category :: option
812 ;; - type :: symbol (nil, t)
814 ;; + `:with-fixed-width' :: Non-nil if transcoder should interpret
815 ;; strings starting with a colon as a fixed-with (verbatim) area.
816 ;; - category :: option
817 ;; - type :: symbol (nil, t)
819 ;; + `:with-footnotes' :: Non-nil if transcoder should interpret
820 ;; footnotes.
821 ;; - category :: option
822 ;; - type :: symbol (nil, t)
824 ;; + `:with-priority' :: Non-nil means transcoding should include
825 ;; priority cookies.
826 ;; - category :: option
827 ;; - type :: symbol (nil, t)
829 ;; + `:with-special-strings' :: Non-nil means transcoding should
830 ;; interpret special strings in plain text.
831 ;; - category :: option
832 ;; - type :: symbol (nil, t)
834 ;; + `:with-sub-superscript' :: Non-nil means transcoding should
835 ;; interpret subscript and superscript. With a value of "{}",
836 ;; only interpret those using curly brackets.
837 ;; - category :: option
838 ;; - type :: symbol (nil, {}, t)
840 ;; + `:with-tables' :: Non-nil means transcoding should interpret
841 ;; tables.
842 ;; - category :: option
843 ;; - type :: symbol (nil, t)
845 ;; + `:with-tags' :: Non-nil means transcoding should keep tags in
846 ;; headlines. A `not-in-toc' value will remove them
847 ;; from the table of contents, if any, nonetheless.
848 ;; - category :: option
849 ;; - type :: symbol (nil, t, `not-in-toc')
851 ;; + `:with-tasks' :: Non-nil means transcoding should include
852 ;; headlines with a TODO keyword. A `todo' value
853 ;; will only include headlines with a todo type
854 ;; keyword while a `done' value will do the
855 ;; contrary. If a list of strings is provided, only
856 ;; tasks with keywords belonging to that list will
857 ;; be kept.
858 ;; - category :: option
859 ;; - type :: symbol (t, todo, done, nil) or list of strings
861 ;; + `:with-timestamps' :: Non-nil means transcoding should include
862 ;; time stamps and associated keywords. Otherwise, completely
863 ;; remove them.
864 ;; - category :: option
865 ;; - type :: symbol: (t, nil)
867 ;; + `:with-toc' :: Non-nil means that a table of contents has to be
868 ;; added to the output. An integer value limits its
869 ;; depth.
870 ;; - category :: option
871 ;; - type :: symbol (nil, t or integer)
873 ;; + `:with-todo-keywords' :: Non-nil means transcoding should
874 ;; include TODO keywords.
875 ;; - category :: option
876 ;; - type :: symbol (nil, t)
879 ;;;; Environment Options
881 ;; Environment options encompass all parameters defined outside the
882 ;; scope of the parsed data. They come from five sources, in
883 ;; increasing precedence order:
885 ;; - Global variables,
886 ;; - Options keyword symbols,
887 ;; - Buffer keywords,
888 ;; - Subtree properties.
890 ;; The central internal function with regards to environment options
891 ;; is `org-export-get-environment'. It updates global variables with
892 ;; "#+BIND:" keywords, then retrieve and prioritize properties from
893 ;; the different sources.
895 ;; The internal functions doing the retrieval are:
896 ;; `org-export-parse-option-keyword' ,
897 ;; `org-export-get-subtree-options' ,
898 ;; `org-export-get-inbuffer-options' and
899 ;; `org-export-get-global-options'.
901 ;; Some properties, which do not rely on the previous sources but
902 ;; still depend on the original buffer, are taken care of with
903 ;; `org-export-initial-options'.
905 ;; Also, `org-export-confirm-letbind' and `org-export-install-letbind'
906 ;; take care of the part relative to "#+BIND:" keywords.
908 (defun org-export-get-environment (&optional backend subtreep ext-plist)
909 "Collect export options from the current buffer.
911 Optional argument BACKEND is a symbol specifying which back-end
912 specific options to read, if any.
914 When optional argument SUBTREEP is non-nil, assume the export is
915 done against the current sub-tree.
917 Third optional argument EXT-PLIST is a property list with
918 external parameters overriding Org default settings, but still
919 inferior to file-local settings."
920 ;; First install #+BIND variables.
921 (org-export-install-letbind-maybe)
922 ;; Get and prioritize export options...
923 (let ((options (org-combine-plists
924 ;; ... from global variables...
925 (org-export-get-global-options backend)
926 ;; ... from buffer's name (default title)...
927 `(:title
928 ,(or (let ((file (buffer-file-name (buffer-base-buffer))))
929 (and file
930 (file-name-sans-extension
931 (file-name-nondirectory file))))
932 (buffer-name (buffer-base-buffer))))
933 ;; ... from an external property list...
934 ext-plist
935 ;; ... from in-buffer settings...
936 (org-export-get-inbuffer-options
937 backend
938 (and buffer-file-name
939 (org-remove-double-quotes buffer-file-name)))
940 ;; ... and from subtree, when appropriate.
941 (and subtreep (org-export-get-subtree-options)))))
942 ;; Add initial options.
943 (setq options (append (org-export-initial-options) options))
944 ;; Return plist.
945 options))
947 (defun org-export-parse-option-keyword (options &optional backend)
948 "Parse an OPTIONS line and return values as a plist.
949 Optional argument BACKEND is a symbol specifying which back-end
950 specific items to read, if any."
951 (let* ((all
952 (append org-export-option-alist
953 (and backend
954 (let ((var (intern
955 (format "org-%s-option-alist" backend))))
956 (and (boundp var) (eval var))))))
957 ;; Build an alist between #+OPTION: item and property-name.
958 (alist (delq nil
959 (mapcar (lambda (e)
960 (when (nth 2 e) (cons (regexp-quote (nth 2 e))
961 (car e))))
962 all)))
963 plist)
964 (mapc (lambda (e)
965 (when (string-match (concat "\\(\\`\\|[ \t]\\)"
966 (car e)
967 ":\\(([^)\n]+)\\|[^ \t\n\r;,.]*\\)")
968 options)
969 (setq plist (plist-put plist
970 (cdr e)
971 (car (read-from-string
972 (match-string 2 options)))))))
973 alist)
974 plist))
976 (defun org-export-get-subtree-options ()
977 "Get export options in subtree at point.
979 Assume point is at subtree's beginning.
981 Return options as a plist."
982 (let (prop plist)
983 (when (setq prop (progn (looking-at org-todo-line-regexp)
984 (or (save-match-data
985 (org-entry-get (point) "EXPORT_TITLE"))
986 (org-match-string-no-properties 3))))
987 (setq plist
988 (plist-put
989 plist :title
990 (org-element-parse-secondary-string
991 prop
992 (cdr (assq 'keyword org-element-string-restrictions))))))
993 (when (setq prop (org-entry-get (point) "EXPORT_TEXT"))
994 (setq plist (plist-put plist :text prop)))
995 (when (setq prop (org-entry-get (point) "EXPORT_AUTHOR"))
996 (setq plist (plist-put plist :author prop)))
997 (when (setq prop (org-entry-get (point) "EXPORT_DATE"))
998 (setq plist (plist-put plist :date prop)))
999 (when (setq prop (org-entry-get (point) "EXPORT_OPTIONS"))
1000 (setq plist (org-export-add-options-to-plist plist prop)))
1001 plist))
1003 (defun org-export-get-inbuffer-options (&optional backend files)
1004 "Return current buffer export options, as a plist.
1006 Optional argument BACKEND, when non-nil, is a symbol specifying
1007 which back-end specific options should also be read in the
1008 process.
1010 Optional argument FILES is a list of setup files names read so
1011 far, used to avoid circular dependencies.
1013 Assume buffer is in Org mode. Narrowing, if any, is ignored."
1014 (org-with-wide-buffer
1015 (goto-char (point-min))
1016 (let ((case-fold-search t) plist)
1017 ;; 1. Special keywords, as in `org-export-special-keywords'.
1018 (let ((special-re (org-make-options-regexp org-export-special-keywords)))
1019 (while (re-search-forward special-re nil t)
1020 (let ((element (org-element-at-point)))
1021 (when (eq (org-element-type element) 'keyword)
1022 (let* ((key (upcase (org-element-property :key element)))
1023 (val (org-element-property :value element))
1024 (prop
1025 (cond
1026 ((string= key "SETUP_FILE")
1027 (let ((file
1028 (expand-file-name
1029 (org-remove-double-quotes (org-trim val)))))
1030 ;; Avoid circular dependencies.
1031 (unless (member file files)
1032 (with-temp-buffer
1033 (insert (org-file-contents file 'noerror))
1034 (org-mode)
1035 (org-export-get-inbuffer-options
1036 backend (cons file files))))))
1037 ((string= key "OPTIONS")
1038 (org-export-parse-option-keyword val backend))
1039 ((string= key "MACRO")
1040 (when (string-match
1041 "^\\([-a-zA-Z0-9_]+\\)\\(?:[ \t]+\\(.*?\\)[ \t]*$\\)?"
1042 val)
1043 (let ((key
1044 (intern
1045 (concat ":macro-"
1046 (downcase (match-string 1 val)))))
1047 (value (org-match-string-no-properties 2 val)))
1048 (cond
1049 ((not value) nil)
1050 ;; Value will be evaled. Leave it as-is.
1051 ((string-match "\\`(eval\\>" value)
1052 (list key value))
1053 ;; Value has to be parsed for nested
1054 ;; macros.
1056 (list
1058 (let ((restr
1059 (cdr
1060 (assq 'macro
1061 org-element-object-restrictions))))
1062 (org-element-parse-secondary-string
1063 ;; If user explicitly asks for
1064 ;; a newline, be sure to preserve it
1065 ;; from further filling with
1066 ;; `hard-newline'. Also replace
1067 ;; "\\n" with "\n", "\\\n" with "\\n"
1068 ;; and so on...
1069 (replace-regexp-in-string
1070 "\\(\\\\\\\\\\)n" "\\\\"
1071 (replace-regexp-in-string
1072 "\\(?:^\\|[^\\\\]\\)\\(\\\\n\\)"
1073 hard-newline value nil nil 1)
1074 nil nil 1)
1075 restr)))))))))))
1076 (setq plist (org-combine-plists plist prop)))))))
1077 ;; 2. Standard options, as in `org-export-option-alist'.
1078 (let* ((all (append org-export-option-alist
1079 ;; Also look for back-end specific options
1080 ;; if BACKEND is defined.
1081 (and backend
1082 (let ((var
1083 (intern
1084 (format "org-%s-option-alist" backend))))
1085 (and (boundp var) (eval var))))))
1086 ;; Build alist between keyword name and property name.
1087 (alist
1088 (delq nil (mapcar
1089 (lambda (e) (when (nth 1 e) (cons (nth 1 e) (car e))))
1090 all)))
1091 ;; Build regexp matching all keywords associated to export
1092 ;; options. Note: the search is case insensitive.
1093 (opt-re (org-make-options-regexp
1094 (delq nil (mapcar (lambda (e) (nth 1 e)) all)))))
1095 (goto-char (point-min))
1096 (while (re-search-forward opt-re nil t)
1097 (let ((element (org-element-at-point)))
1098 (when (eq (org-element-type element) 'keyword)
1099 (let* ((key (upcase (org-element-property :key element)))
1100 (val (org-element-property :value element))
1101 (prop (cdr (assoc key alist)))
1102 (behaviour (nth 4 (assq prop all))))
1103 (setq plist
1104 (plist-put
1105 plist prop
1106 ;; Handle value depending on specified BEHAVIOUR.
1107 (case behaviour
1108 (space
1109 (if (not (plist-get plist prop)) (org-trim val)
1110 (concat (plist-get plist prop) " " (org-trim val))))
1111 (newline
1112 (org-trim
1113 (concat (plist-get plist prop) "\n" (org-trim val))))
1114 (split
1115 `(,@(plist-get plist prop) ,@(org-split-string val)))
1116 ('t val)
1117 (otherwise (if (not (plist-member plist prop)) val
1118 (plist-get plist prop))))))))))
1119 ;; Parse keywords specified in `org-element-parsed-keywords'.
1120 (mapc
1121 (lambda (key)
1122 (let* ((prop (cdr (assoc key alist)))
1123 (value (and prop (plist-get plist prop))))
1124 (when (stringp value)
1125 (setq plist
1126 (plist-put
1127 plist prop
1128 (org-element-parse-secondary-string
1129 value
1130 (cdr (assq 'keyword org-element-string-restrictions))))))))
1131 org-element-parsed-keywords))
1132 ;; 3. Return final value.
1133 plist)))
1135 (defun org-export-get-global-options (&optional backend)
1136 "Return global export options as a plist.
1138 Optional argument BACKEND, if non-nil, is a symbol specifying
1139 which back-end specific export options should also be read in the
1140 process."
1141 (let ((all (append org-export-option-alist
1142 (and backend
1143 (let ((var (intern
1144 (format "org-%s-option-alist" backend))))
1145 (and (boundp var) (eval var))))))
1146 ;; Output value.
1147 plist)
1148 (mapc (lambda (cell)
1149 (setq plist (plist-put plist (car cell) (eval (nth 3 cell)))))
1150 all)
1151 ;; Return value.
1152 plist))
1154 (defun org-export-initial-options ()
1155 "Return a plist with properties related to input buffer."
1156 (let ((visited-file (buffer-file-name (buffer-base-buffer))))
1157 (list
1158 ;; Store full path of input file name, or nil. For internal use.
1159 :input-file visited-file
1160 ;; `:macro-date', `:macro-time' and `:macro-property' could as well
1161 ;; be initialized as tree properties, since they don't depend on
1162 ;; initial environment. Though, it may be more logical to keep
1163 ;; them close to other ":macro-" properties.
1164 :macro-date "(eval (format-time-string \"$1\"))"
1165 :macro-time "(eval (format-time-string \"$1\"))"
1166 :macro-property "(eval (org-entry-get nil \"$1\" 'selective))"
1167 :macro-modification-time
1168 (and visited-file
1169 (file-exists-p visited-file)
1170 (concat "(eval (format-time-string \"$1\" '"
1171 (prin1-to-string (nth 5 (file-attributes visited-file)))
1172 "))"))
1173 ;; Store input file name as a macro.
1174 :macro-input-file (and visited-file (file-name-nondirectory visited-file))
1175 ;; Footnotes definitions must be collected in the original buffer,
1176 ;; as there's no insurance that they will still be in the parse
1177 ;; tree, due to some narrowing.
1178 :footnote-definition-alist
1179 (let (alist)
1180 (org-with-wide-buffer
1181 (goto-char (point-min))
1182 (while (re-search-forward org-footnote-definition-re nil t)
1183 (let ((def (org-footnote-at-definition-p)))
1184 (when def
1185 (org-skip-whitespace)
1186 (push (cons (car def)
1187 (save-restriction
1188 (narrow-to-region (point) (nth 2 def))
1189 ;; Like `org-element-parse-buffer', but
1190 ;; makes sure the definition doesn't start
1191 ;; with a section element.
1192 (nconc
1193 (list 'org-data nil)
1194 (org-element-parse-elements
1195 (point-min) (point-max) nil nil nil nil nil))))
1196 alist))))
1197 alist)))))
1199 (defvar org-export-allow-BIND-local nil)
1200 (defun org-export-confirm-letbind ()
1201 "Can we use #+BIND values during export?
1202 By default this will ask for confirmation by the user, to divert
1203 possible security risks."
1204 (cond
1205 ((not org-export-allow-BIND) nil)
1206 ((eq org-export-allow-BIND t) t)
1207 ((local-variable-p 'org-export-allow-BIND-local) org-export-allow-BIND-local)
1208 (t (org-set-local 'org-export-allow-BIND-local
1209 (yes-or-no-p "Allow BIND values in this buffer? ")))))
1211 (defun org-export-install-letbind-maybe ()
1212 "Install the values from #+BIND lines as local variables.
1213 Variables must be installed before in-buffer options are
1214 retrieved."
1215 (let (letbind pair)
1216 (org-with-wide-buffer
1217 (goto-char (point-min))
1218 (while (re-search-forward (org-make-options-regexp '("BIND")) nil t)
1219 (when (org-export-confirm-letbind)
1220 (push (read (concat "(" (org-match-string-no-properties 2) ")"))
1221 letbind))))
1222 (while (setq pair (pop letbind))
1223 (org-set-local (car pair) (nth 1 pair)))))
1226 ;;;; Tree Properties
1228 ;; Tree properties are infromation extracted from parse tree. They
1229 ;; are initialized at the beginning of the transcoding process by
1230 ;; `org-export-collect-tree-properties'.
1232 ;; Dedicated functions focus on computing the value of specific tree
1233 ;; properties during initialization. Thus,
1234 ;; `org-export-use-select-tag-p' determines if an headline makes use
1235 ;; of an export tag enforcing inclusion. `org-export-get-ignore-list'
1236 ;; marks collect elements and objects that should be skipped during
1237 ;; export, `org-export-get-min-level' gets the minimal exportable
1238 ;; level, used as a basis to compute relative level for headlines.
1239 ;; Eventually `org-export-collect-headline-numbering' builds an alist
1240 ;; between headlines and their numbering.
1242 (defun org-export-collect-tree-properties (data info backend)
1243 "Extract tree properties from parse tree.
1245 DATA is the parse tree from which information is retrieved. INFO
1246 is a list holding export options. BACKEND is the back-end called
1247 for transcoding, as a symbol.
1249 Following tree properties are set:
1250 `:back-end' Back-end used for transcoding.
1252 `:headline-offset' Offset between true level of headlines and
1253 local level. An offset of -1 means an headline
1254 of level 2 should be considered as a level
1255 1 headline in the context.
1257 `:headline-numbering' Alist of all headlines' beginning position
1258 as key an the associated numbering as value.
1260 `:ignore-list' List of elements that should be ignored during export.
1262 `:parse-tree' Whole parse tree.
1264 `:target-list' List of all targets in the parse tree.
1266 `:use-select-tags' Non-nil when parsed tree use a special tag to
1267 enforce transcoding of the headline."
1268 ;; First, set `:use-select-tags' property, as it will be required
1269 ;; for further computations.
1270 (setq info
1271 (plist-put info
1272 :use-select-tags (org-export-use-select-tags-p data info)))
1273 ;; Then get the list of elements and objects to ignore, and put it
1274 ;; into `:ignore-list'.
1275 (setq info
1276 (plist-put info :ignore-list (org-export-get-ignore-list data info)))
1277 ;; Finally get `:headline-offset' in order to be able to use
1278 ;; `org-export-get-relative-level'.
1279 (setq info
1280 (plist-put info
1281 :headline-offset (- 1 (org-export-get-min-level data info))))
1282 ;; Now, properties order doesn't matter: get the rest of the tree
1283 ;; properties.
1284 (nconc
1285 `(:parse-tree
1286 ,data
1287 :target-list
1288 ,(org-element-map data 'target (lambda (target local) target) info)
1289 :headline-numbering ,(org-export-collect-headline-numbering data info)
1290 :back-end ,backend)
1291 info))
1293 (defun org-export-use-select-tags-p (data options)
1294 "Non-nil when data use a tag enforcing transcoding.
1295 DATA is parsed data as returned by `org-element-parse-buffer'.
1296 OPTIONS is a plist holding export options."
1297 (org-element-map
1298 data
1299 'headline
1300 (lambda (headline info)
1301 (let ((tags (org-element-property :tags headline)))
1302 (and tags
1303 (loop for tag in (plist-get info :select-tags)
1304 thereis (string-match (format ":%s:" tag) tags)))))
1305 options 'first-match))
1307 (defun org-export-get-min-level (data options)
1308 "Return minimum exportable headline's level in DATA.
1309 DATA is parsed tree as returned by `org-element-parse-buffer'.
1310 OPTIONS is a plist holding export options."
1311 (catch 'exit
1312 (let ((min-level 10000))
1313 (mapc
1314 (lambda (blob)
1315 (when (and (eq (org-element-type blob) 'headline)
1316 (not (member blob (plist-get options :ignore-list))))
1317 (setq min-level
1318 (min (org-element-property :level blob) min-level)))
1319 (when (= min-level 1) (throw 'exit 1)))
1320 (org-element-contents data))
1321 ;; If no headline was found, for the sake of consistency, set
1322 ;; minimum level to 1 nonetheless.
1323 (if (= min-level 10000) 1 min-level))))
1325 (defun org-export-collect-headline-numbering (data options)
1326 "Return numbering of all exportable headlines in a parse tree.
1328 DATA is the parse tree. OPTIONS is the plist holding export
1329 options.
1331 Return an alist whose key is an headline and value is its
1332 associated numbering \(in the shape of a list of numbers\)."
1333 (let ((numbering (make-vector org-export-max-depth 0)))
1334 (org-element-map
1335 data
1336 'headline
1337 (lambda (headline info)
1338 (let ((relative-level
1339 (1- (org-export-get-relative-level headline info))))
1340 (cons
1341 headline
1342 (loop for n across numbering
1343 for idx from 0 to org-export-max-depth
1344 when (< idx relative-level) collect n
1345 when (= idx relative-level) collect (aset numbering idx (1+ n))
1346 when (> idx relative-level) do (aset numbering idx 0)))))
1347 options)))
1349 (defun org-export--skip-p (blob options)
1350 "Non-nil when element or object BLOB should be skipped during export.
1351 OPTIONS is the plist holding export options."
1352 (case (org-element-type blob)
1353 ;; Check headline.
1354 (headline
1355 (let ((with-tasks (plist-get options :with-tasks))
1356 (todo (org-element-property :todo-keyword blob))
1357 (todo-type (org-element-property :todo-type blob))
1358 (archived (plist-get options :with-archived-trees))
1359 (tag-list (let ((tags (org-element-property :tags blob)))
1360 (and tags (org-split-string tags ":")))))
1362 ;; Ignore subtrees with an exclude tag.
1363 (loop for k in (plist-get options :exclude-tags)
1364 thereis (member k tag-list))
1365 ;; Ignore subtrees without a select tag, when such tag is found
1366 ;; in the buffer.
1367 (and (plist-get options :use-select-tags)
1368 (loop for k in (plist-get options :select-tags)
1369 never (member k tag-list)))
1370 ;; Ignore commented sub-trees.
1371 (org-element-property :commentedp blob)
1372 ;; Ignore archived subtrees if `:with-archived-trees' is nil.
1373 (and (not archived) (org-element-property :archivedp blob))
1374 ;; Ignore tasks, if specified by `:with-tasks' property.
1375 (and todo (not with-tasks))
1376 (and todo
1377 (memq with-tasks '(todo done))
1378 (not (eq todo-type with-tasks)))
1379 (and todo
1380 (consp with-tasks)
1381 (not (member todo with-tasks))))))
1382 ;; Check time-stamp.
1383 (time-stamp (not (plist-get options :with-timestamps)))
1384 ;; Check drawer.
1385 (drawer
1386 (or (not (plist-get options :with-drawers))
1387 (and (consp (plist-get options :with-drawers))
1388 (not (member (org-element-property :drawer-name blob)
1389 (plist-get options :with-drawers))))))
1390 ;; Check export snippet.
1391 (export-snippet
1392 (let* ((raw-back-end (org-element-property :back-end blob))
1393 (true-back-end
1394 (or (cdr (assoc raw-back-end org-export-snippet-translation-alist))
1395 raw-back-end)))
1396 (not (string= (symbol-name (plist-get options :back-end))
1397 true-back-end))))))
1399 (defun org-export-get-ignore-list (data options)
1400 "Return list of elements and objects to ignore during export.
1402 DATA is the parse tree to traverse. OPTIONS is the plist holding
1403 export options.
1405 Return elements or objects to ignore as a list."
1406 (let (ignore-list
1407 (walk-data
1408 (function
1409 (lambda (data options)
1410 ;; Collect ignored elements or objects into IGNORE-LIST.
1411 (mapc
1412 (lambda (el)
1413 (if (org-export--skip-p el options) (push el ignore-list)
1414 (let ((type (org-element-type el)))
1415 (when (or (eq type 'org-data)
1416 (memq type org-element-greater-elements)
1417 (memq type org-element-recursive-objects)
1418 (eq type 'paragraph))
1419 (funcall walk-data el options)))))
1420 (org-element-contents data))))))
1421 (funcall walk-data data options)
1422 ;; Return value.
1423 ignore-list))
1427 ;;; The Transcoder
1429 ;; This function reads Org data (obtained with, i.e.
1430 ;; `org-element-parse-buffer') and transcodes it into a specified
1431 ;; back-end output. It takes care of updating local properties,
1432 ;; filtering out elements or objects according to export options and
1433 ;; organizing the output blank lines and white space are preserved.
1435 ;; Though, this function is inapropriate for secondary strings, which
1436 ;; require a fresh copy of the plist passed as INFO argument. Thus,
1437 ;; `org-export-secondary-string' is provided for that specific task.
1439 ;; Internally, three functions handle the filtering of objects and
1440 ;; elements during the export. In particular,
1441 ;; `org-export-ignore-element' mark an element or object so future
1442 ;; parse tree traversals skip it, `org-export-interpret-p' tells which
1443 ;; elements or objects should be seen as real Org syntax and
1444 ;; `org-export-expand' transforms the others back into their original
1445 ;; shape.
1447 (defun org-export-data (data backend info)
1448 "Convert DATA to a string into BACKEND format.
1450 DATA is a nested list as returned by `org-element-parse-buffer'.
1452 BACKEND is a symbol among supported exporters.
1454 INFO is a plist holding export options and also used as
1455 a communication channel between elements when walking the nested
1456 list. See `org-export-update-info' function for more
1457 details.
1459 Return transcoded string."
1460 (mapconcat
1461 ;; BLOB can be an element, an object, a string, or nil.
1462 (lambda (blob)
1463 (cond
1464 ((not blob) nil)
1465 ;; BLOB is a string. Check if the optional transcoder for plain
1466 ;; text exists, and call it in that case. Otherwise, simply
1467 ;; return string. Also update INFO and call
1468 ;; `org-export-filter-plain-text-functions'.
1469 ((stringp blob)
1470 (let ((transcoder (intern (format "org-%s-plain-text" backend))))
1471 (org-export-filter-apply-functions
1472 (plist-get info :filter-plain-text)
1473 (if (fboundp transcoder) (funcall transcoder blob info) blob)
1474 backend info)))
1475 ;; BLOB is an element or an object.
1477 (let* ((type (org-element-type blob))
1478 ;; 1. Determine the appropriate TRANSCODER.
1479 (transcoder
1480 (cond
1481 ;; 1.0 A full Org document is inserted.
1482 ((eq type 'org-data) 'identity)
1483 ;; 1.1. BLOB should be ignored.
1484 ((member blob (plist-get info :ignore-list)) nil)
1485 ;; 1.2. BLOB shouldn't be transcoded. Interpret it
1486 ;; back into Org syntax.
1487 ((not (org-export-interpret-p blob info)) 'org-export-expand)
1488 ;; 1.3. Else apply naming convention.
1489 (t (let ((trans (intern (format "org-%s-%s" backend type))))
1490 (and (fboundp trans) trans)))))
1491 ;; 2. Compute CONTENTS of BLOB.
1492 (contents
1493 (cond
1494 ;; Case 0. No transcoder defined: ignore BLOB.
1495 ((not transcoder) nil)
1496 ;; Case 1. Transparently export an Org document.
1497 ((eq type 'org-data) (org-export-data blob backend info))
1498 ;; Case 2. For a recursive object.
1499 ((memq type org-element-recursive-objects)
1500 (org-export-data blob backend info))
1501 ;; Case 3. For a recursive element.
1502 ((memq type org-element-greater-elements)
1503 ;; Ignore contents of an archived tree
1504 ;; when `:with-archived-trees' is `headline'.
1505 (unless (and
1506 (eq type 'headline)
1507 (eq (plist-get info :with-archived-trees) 'headline)
1508 (org-element-property :archivedp blob))
1509 (org-element-normalize-string
1510 (org-export-data blob backend info))))
1511 ;; Case 4. For a paragraph.
1512 ((eq type 'paragraph)
1513 (let ((paragraph
1514 (org-element-normalize-contents
1515 blob
1516 ;; When normalizing contents of an item or
1517 ;; a footnote definition, ignore first line's
1518 ;; indentation: there is none and it might be
1519 ;; misleading.
1520 (and (not (org-export-get-previous-element blob info))
1521 (let ((parent (org-export-get-parent blob info)))
1522 (memq (org-element-type parent)
1523 '(footnote-definition item)))))))
1524 (org-export-data paragraph backend info)))))
1525 ;; 3. Transcode BLOB into RESULTS string.
1526 (results (cond
1527 ((not transcoder) nil)
1528 ((eq transcoder 'org-export-expand)
1529 (org-export-data
1530 `(org-data nil ,(funcall transcoder blob contents))
1531 backend info))
1532 (t (funcall transcoder blob contents info)))))
1533 ;; 4. Return results.
1534 (cond
1535 ;; Discard nil results. Also ignore BLOB from further
1536 ;; traversals in parse tree.
1537 ((not results) (org-export-ignore-element blob info) nil)
1538 ;; No filter for a full document.
1539 ((eq type 'org-data) results)
1540 ;; Otherwise, update INFO, append the same white space
1541 ;; between elements or objects as in the original buffer,
1542 ;; and call appropriate filters.
1544 (let ((results
1545 (org-export-filter-apply-functions
1546 (plist-get info (intern (format ":filter-%s" type)))
1547 (let ((post-blank (org-element-property :post-blank blob)))
1548 (if (memq type org-element-all-elements)
1549 (concat (org-element-normalize-string results)
1550 (make-string post-blank ?\n))
1551 (concat results (make-string post-blank ? ))))
1552 backend info)))
1553 ;; If BLOB was transcoded into an empty string, ignore it
1554 ;; from subsequent traversals.
1555 (unless (org-string-nw-p results)
1556 (org-export-ignore-element blob info))
1557 ;; Eventually return string.
1558 results)))))))
1559 (org-element-contents data) ""))
1561 (defun org-export-secondary-string (secondary backend info)
1562 "Convert SECONDARY string into BACKEND format.
1564 SECONDARY is a nested list as returned by
1565 `org-element-parse-secondary-string'.
1567 BACKEND is a symbol among supported exporters. INFO is a plist
1568 used as a communication channel.
1570 Return transcoded string."
1571 ;; Make SECONDARY acceptable for `org-export-data'.
1572 (let ((s (if (listp secondary) secondary (list secondary))))
1573 (org-export-data `(org-data nil ,@s) backend (copy-sequence info))))
1575 (defun org-export-interpret-p (blob info)
1576 "Non-nil if element or object BLOB should be interpreted as Org syntax.
1577 Check is done according to export options INFO, stored as
1578 a plist."
1579 (case (org-element-type blob)
1580 ;; ... entities...
1581 (entity (plist-get info :with-entities))
1582 ;; ... emphasis...
1583 (emphasis (plist-get info :with-emphasize))
1584 ;; ... fixed-width areas.
1585 (fixed-width (plist-get info :with-fixed-width))
1586 ;; ... footnotes...
1587 ((footnote-definition footnote-reference)
1588 (plist-get info :with-footnotes))
1589 ;; ... sub/superscripts...
1590 ((subscript superscript)
1591 (let ((sub/super-p (plist-get info :with-sub-superscript)))
1592 (if (eq sub/super-p '{})
1593 (org-element-property :use-brackets-p blob)
1594 sub/super-p)))
1595 ;; ... tables...
1596 (table (plist-get info :with-tables))
1597 (otherwise t)))
1599 (defsubst org-export-expand (blob contents)
1600 "Expand a parsed element or object to its original state.
1601 BLOB is either an element or an object. CONTENTS is its
1602 contents, as a string or nil."
1603 (funcall (intern (format "org-element-%s-interpreter" (org-element-type blob)))
1604 blob contents))
1606 (defun org-export-ignore-element (element info)
1607 "Add ELEMENT to `:ignore-list' in INFO.
1609 Any element in `:ignore-list' will be skipped when using
1610 `org-element-map'. INFO is modified by side effects."
1611 (plist-put info :ignore-list (cons element (plist-get info :ignore-list))))
1615 ;;; The Filter System
1617 ;; Filters allow end-users to tweak easily the transcoded output.
1618 ;; They are the functional counterpart of hooks, as every filter in
1619 ;; a set is applied to the return value of the previous one.
1621 ;; Every set is back-end agnostic. Although, a filter is always
1622 ;; called, in addition to the string it applies to, with the back-end
1623 ;; used as argument, so it's easy enough for the end-user to add
1624 ;; back-end specific filters in the set. The communication channel,
1625 ;; as a plist, is required as the third argument.
1627 ;; Filters sets are defined below. There are of four types:
1629 ;; - `org-export-filter-parse-tree-functions' applies directly on the
1630 ;; complete parsed tree. It's the only filters set that doesn't
1631 ;; apply to a string.
1632 ;; - `org-export-filter-final-output-functions' applies to the final
1633 ;; transcoded string.
1634 ;; - `org-export-filter-plain-text-functions' applies to any string
1635 ;; not recognized as Org syntax.
1636 ;; - `org-export-filter-TYPE-functions' applies on the string returned
1637 ;; after an element or object of type TYPE has been transcoded.
1639 ;; All filters sets are applied through
1640 ;; `org-export-filter-apply-functions' function. Filters in a set are
1641 ;; applied in a LIFO fashion. It allows developers to be sure that
1642 ;; their filters will be applied first.
1644 ;; Filters properties are installed in communication channel just
1645 ;; before parsing, with `org-export-install-filters' function.
1647 ;;;; Special Filters
1648 (defvar org-export-filter-parse-tree-functions nil
1649 "Filter, or list of filters, applied to the parsed tree.
1650 Each filter is called with three arguments: the parse tree, as
1651 returned by `org-element-parse-buffer', the back-end, as
1652 a symbol, and the communication channel, as a plist. It must
1653 return the modified parse tree to transcode.")
1655 (defvar org-export-filter-final-output-functions nil
1656 "Filter, or list of filters, applied to the transcoded string.
1657 Each filter is called with three arguments: the full transcoded
1658 string, the back-end, as a symbol, and the communication channel,
1659 as a plist. It must return a string that will be used as the
1660 final export output.")
1662 (defvar org-export-filter-plain-text-functions nil
1663 "Filter, or list of filters, applied to plain text.
1664 Each filter is called with three arguments: a string which
1665 contains no Org syntax, the back-end, as a symbol, and the
1666 communication channel, as a plist. It must return a string or
1667 nil.")
1670 ;;;; Elements Filters
1672 (defvar org-export-filter-center-block-functions nil
1673 "List of functions applied to a transcoded center block.
1674 Each filter is called with three arguments: the transcoded center
1675 block, as a string, the back-end, as a symbol, and the
1676 communication channel, as a plist. It must return a string or
1677 nil.")
1679 (defvar org-export-filter-drawer-functions nil
1680 "List of functions applied to a transcoded drawer.
1681 Each filter is called with three arguments: the transcoded
1682 drawer, as a string, the back-end, as a symbol, and the
1683 communication channel, as a plist. It must return a string or
1684 nil.")
1686 (defvar org-export-filter-dynamic-block-functions nil
1687 "List of functions applied to a transcoded dynamic-block.
1688 Each filter is called with three arguments: the transcoded
1689 dynamic-block, as a string, the back-end, as a symbol, and the
1690 communication channel, as a plist. It must return a string or
1691 nil.")
1693 (defvar org-export-filter-headline-functions nil
1694 "List of functions applied to a transcoded headline.
1695 Each filter is called with three arguments: the transcoded
1696 headline, as a string, the back-end, as a symbol, and the
1697 communication channel, as a plist. It must return a string or
1698 nil.")
1700 (defvar org-export-filter-inlinetask-functions nil
1701 "List of functions applied to a transcoded inlinetask.
1702 Each filter is called with three arguments: the transcoded
1703 inlinetask, as a string, the back-end, as a symbol, and the
1704 communication channel, as a plist. It must return a string or
1705 nil.")
1707 (defvar org-export-filter-plain-list-functions nil
1708 "List of functions applied to a transcoded plain-list.
1709 Each filter is called with three arguments: the transcoded
1710 plain-list, as a string, the back-end, as a symbol, and the
1711 communication channel, as a plist. It must return a string or
1712 nil.")
1714 (defvar org-export-filter-item-functions nil
1715 "List of functions applied to a transcoded item.
1716 Each filter is called with three arguments: the transcoded item,
1717 as a string, the back-end, as a symbol, and the communication
1718 channel, as a plist. It must return a string or nil.")
1720 (defvar org-export-filter-comment-functions nil
1721 "List of functions applied to a transcoded comment.
1722 Each filter is called with three arguments: the transcoded
1723 comment, as a string, the back-end, as a symbol, and the
1724 communication channel, as a plist. It must return a string or
1725 nil.")
1727 (defvar org-export-filter-comment-block-functions nil
1728 "List of functions applied to a transcoded comment-comment.
1729 Each filter is called with three arguments: the transcoded
1730 comment-block, as a string, the back-end, as a symbol, and the
1731 communication channel, as a plist. It must return a string or
1732 nil.")
1734 (defvar org-export-filter-example-block-functions nil
1735 "List of functions applied to a transcoded example-block.
1736 Each filter is called with three arguments: the transcoded
1737 example-block, as a string, the back-end, as a symbol, and the
1738 communication channel, as a plist. It must return a string or
1739 nil.")
1741 (defvar org-export-filter-export-block-functions nil
1742 "List of functions applied to a transcoded export-block.
1743 Each filter is called with three arguments: the transcoded
1744 export-block, as a string, the back-end, as a symbol, and the
1745 communication channel, as a plist. It must return a string or
1746 nil.")
1748 (defvar org-export-filter-fixed-width-functions nil
1749 "List of functions applied to a transcoded fixed-width.
1750 Each filter is called with three arguments: the transcoded
1751 fixed-width, as a string, the back-end, as a symbol, and the
1752 communication channel, as a plist. It must return a string or
1753 nil.")
1755 (defvar org-export-filter-footnote-definition-functions nil
1756 "List of functions applied to a transcoded footnote-definition.
1757 Each filter is called with three arguments: the transcoded
1758 footnote-definition, as a string, the back-end, as a symbol, and
1759 the communication channel, as a plist. It must return a string
1760 or nil.")
1762 (defvar org-export-filter-horizontal-rule-functions nil
1763 "List of functions applied to a transcoded horizontal-rule.
1764 Each filter is called with three arguments: the transcoded
1765 horizontal-rule, as a string, the back-end, as a symbol, and the
1766 communication channel, as a plist. It must return a string or
1767 nil.")
1769 (defvar org-export-filter-keyword-functions nil
1770 "List of functions applied to a transcoded keyword.
1771 Each filter is called with three arguments: the transcoded
1772 keyword, as a string, the back-end, as a symbol, and the
1773 communication channel, as a plist. It must return a string or
1774 nil.")
1776 (defvar org-export-filter-latex-environment-functions nil
1777 "List of functions applied to a transcoded latex-environment.
1778 Each filter is called with three arguments: the transcoded
1779 latex-environment, as a string, the back-end, as a symbol, and
1780 the communication channel, as a plist. It must return a string
1781 or nil.")
1783 (defvar org-export-filter-babel-call-functions nil
1784 "List of functions applied to a transcoded babel-call.
1785 Each filter is called with three arguments: the transcoded
1786 babel-call, as a string, the back-end, as a symbol, and the
1787 communication channel, as a plist. It must return a string or
1788 nil.")
1790 (defvar org-export-filter-paragraph-functions nil
1791 "List of functions applied to a transcoded paragraph.
1792 Each filter is called with three arguments: the transcoded
1793 paragraph, as a string, the back-end, as a symbol, and the
1794 communication channel, as a plist. It must return a string or
1795 nil.")
1797 (defvar org-export-filter-property-drawer-functions nil
1798 "List of functions applied to a transcoded property-drawer.
1799 Each filter is called with three arguments: the transcoded
1800 property-drawer, as a string, the back-end, as a symbol, and the
1801 communication channel, as a plist. It must return a string or
1802 nil.")
1804 (defvar org-export-filter-quote-block-functions nil
1805 "List of functions applied to a transcoded quote block.
1806 Each filter is called with three arguments: the transcoded quote
1807 block, as a string, the back-end, as a symbol, and the
1808 communication channel, as a plist. It must return a string or
1809 nil.")
1811 (defvar org-export-filter-quote-section-functions nil
1812 "List of functions applied to a transcoded quote-section.
1813 Each filter is called with three arguments: the transcoded
1814 quote-section, as a string, the back-end, as a symbol, and the
1815 communication channel, as a plist. It must return a string or
1816 nil.")
1818 (defvar org-export-filter-section-functions nil
1819 "List of functions applied to a transcoded section.
1820 Each filter is called with three arguments: the transcoded
1821 section, as a string, the back-end, as a symbol, and the
1822 communication channel, as a plist. It must return a string or
1823 nil.")
1825 (defvar org-export-filter-special-block-functions nil
1826 "List of functions applied to a transcoded special block.
1827 Each filter is called with three arguments: the transcoded
1828 special block, as a string, the back-end, as a symbol, and the
1829 communication channel, as a plist. It must return a string or
1830 nil.")
1832 (defvar org-export-filter-src-block-functions nil
1833 "List of functions applied to a transcoded src-block.
1834 Each filter is called with three arguments: the transcoded
1835 src-block, as a string, the back-end, as a symbol, and the
1836 communication channel, as a plist. It must return a string or
1837 nil.")
1839 (defvar org-export-filter-table-functions nil
1840 "List of functions applied to a transcoded table.
1841 Each filter is called with three arguments: the transcoded table,
1842 as a string, the back-end, as a symbol, and the communication
1843 channel, as a plist. It must return a string or nil.")
1845 (defvar org-export-filter-verse-block-functions nil
1846 "List of functions applied to a transcoded verse block.
1847 Each filter is called with three arguments: the transcoded verse
1848 block, as a string, the back-end, as a symbol, and the
1849 communication channel, as a plist. It must return a string or
1850 nil.")
1853 ;;;; Objects Filters
1855 (defvar org-export-filter-emphasis-functions nil
1856 "List of functions applied to a transcoded emphasis.
1857 Each filter is called with three arguments: the transcoded
1858 emphasis, as a string, the back-end, as a symbol, and the
1859 communication channel, as a plist. It must return a string or
1860 nil.")
1862 (defvar org-export-filter-entity-functions nil
1863 "List of functions applied to a transcoded entity.
1864 Each filter is called with three arguments: the transcoded
1865 entity, as a string, the back-end, as a symbol, and the
1866 communication channel, as a plist. It must return a string or
1867 nil.")
1869 (defvar org-export-filter-export-snippet-functions nil
1870 "List of functions applied to a transcoded export-snippet.
1871 Each filter is called with three arguments: the transcoded
1872 export-snippet, as a string, the back-end, as a symbol, and the
1873 communication channel, as a plist. It must return a string or
1874 nil.")
1876 (defvar org-export-filter-footnote-reference-functions nil
1877 "List of functions applied to a transcoded footnote-reference.
1878 Each filter is called with three arguments: the transcoded
1879 footnote-reference, as a string, the back-end, as a symbol, and
1880 the communication channel, as a plist. It must return a string
1881 or nil.")
1883 (defvar org-export-filter-inline-babel-call-functions nil
1884 "List of functions applied to a transcoded inline-babel-call.
1885 Each filter is called with three arguments: the transcoded
1886 inline-babel-call, as a string, the back-end, as a symbol, and
1887 the communication channel, as a plist. It must return a string
1888 or nil.")
1890 (defvar org-export-filter-inline-src-block-functions nil
1891 "List of functions applied to a transcoded inline-src-block.
1892 Each filter is called with three arguments: the transcoded
1893 inline-src-block, as a string, the back-end, as a symbol, and the
1894 communication channel, as a plist. It must return a string or
1895 nil.")
1897 (defvar org-export-filter-latex-fragment-functions nil
1898 "List of functions applied to a transcoded latex-fragment.
1899 Each filter is called with three arguments: the transcoded
1900 latex-fragment, as a string, the back-end, as a symbol, and the
1901 communication channel, as a plist. It must return a string or
1902 nil.")
1904 (defvar org-export-filter-line-break-functions nil
1905 "List of functions applied to a transcoded line-break.
1906 Each filter is called with three arguments: the transcoded
1907 line-break, as a string, the back-end, as a symbol, and the
1908 communication channel, as a plist. It must return a string or
1909 nil.")
1911 (defvar org-export-filter-link-functions nil
1912 "List of functions applied to a transcoded link.
1913 Each filter is called with three arguments: the transcoded link,
1914 as a string, the back-end, as a symbol, and the communication
1915 channel, as a plist. It must return a string or nil.")
1917 (defvar org-export-filter-macro-functions nil
1918 "List of functions applied to a transcoded macro.
1919 Each filter is called with three arguments: the transcoded macro,
1920 as a string, the back-end, as a symbol, and the communication
1921 channel, as a plist. It must return a string or nil.")
1923 (defvar org-export-filter-radio-target-functions nil
1924 "List of functions applied to a transcoded radio-target.
1925 Each filter is called with three arguments: the transcoded
1926 radio-target, as a string, the back-end, as a symbol, and the
1927 communication channel, as a plist. It must return a string or
1928 nil.")
1930 (defvar org-export-filter-statistics-cookie-functions nil
1931 "List of functions applied to a transcoded statistics-cookie.
1932 Each filter is called with three arguments: the transcoded
1933 statistics-cookie, as a string, the back-end, as a symbol, and
1934 the communication channel, as a plist. It must return a string
1935 or nil.")
1937 (defvar org-export-filter-subscript-functions nil
1938 "List of functions applied to a transcoded subscript.
1939 Each filter is called with three arguments: the transcoded
1940 subscript, as a string, the back-end, as a symbol, and the
1941 communication channel, as a plist. It must return a string or
1942 nil.")
1944 (defvar org-export-filter-superscript-functions nil
1945 "List of functions applied to a transcoded superscript.
1946 Each filter is called with three arguments: the transcoded
1947 superscript, as a string, the back-end, as a symbol, and the
1948 communication channel, as a plist. It must return a string or
1949 nil.")
1951 (defvar org-export-filter-target-functions nil
1952 "List of functions applied to a transcoded target.
1953 Each filter is called with three arguments: the transcoded
1954 target, as a string, the back-end, as a symbol, and the
1955 communication channel, as a plist. It must return a string or
1956 nil.")
1958 (defvar org-export-filter-time-stamp-functions nil
1959 "List of functions applied to a transcoded time-stamp.
1960 Each filter is called with three arguments: the transcoded
1961 time-stamp, as a string, the back-end, as a symbol, and the
1962 communication channel, as a plist. It must return a string or
1963 nil.")
1965 (defvar org-export-filter-verbatim-functions nil
1966 "List of functions applied to a transcoded verbatim.
1967 Each filter is called with three arguments: the transcoded
1968 verbatim, as a string, the back-end, as a symbol, and the
1969 communication channel, as a plist. It must return a string or
1970 nil.")
1972 (defun org-export-filter-apply-functions (filters value backend info)
1973 "Call every function in FILTERS with arguments VALUE, BACKEND and INFO.
1974 Functions are called in a LIFO fashion, to be sure that developer
1975 specified filters, if any, are called first."
1976 (loop for filter in filters
1977 if (not value) return nil else
1978 do (setq value (funcall filter value backend info)))
1979 value)
1981 (defun org-export-install-filters (backend info)
1982 "Install filters properties in communication channel.
1984 BACKEND is a symbol specifying which back-end specific filters to
1985 install, if any. INFO is a plist containing the current
1986 communication channel.
1988 Return the updated communication channel."
1989 (let (plist)
1990 ;; Install user defined filters with `org-export-filters-alist'.
1991 (mapc (lambda (p)
1992 (setq plist (plist-put plist (car p) (eval (cdr p)))))
1993 org-export-filters-alist)
1994 ;; Prepend back-end specific filters to that list.
1995 (let ((back-end-filters (intern (format "org-%s-filters-alist" backend))))
1996 (when (boundp back-end-filters)
1997 (mapc (lambda (p)
1998 ;; Single values get consed, lists are prepended.
1999 (let ((key (car p)) (value (cdr p)))
2000 (when value
2001 (setq plist
2002 (plist-put
2003 plist key
2004 (if (atom value) (cons value (plist-get plist key))
2005 (append value (plist-get plist key))))))))
2006 (eval back-end-filters))))
2007 ;; Return new communication channel.
2008 (org-combine-plists info plist)))
2012 ;;; Core functions
2014 ;; This is the room for the main function, `org-export-as', along with
2015 ;; its derivatives, `org-export-to-buffer' and `org-export-to-file'.
2016 ;; They differ only by the way they output the resulting code.
2018 ;; `org-export-output-file-name' is an auxiliary function meant to be
2019 ;; used with `org-export-to-file'. With a given extension, it tries
2020 ;; to provide a canonical file name to write export output to.
2022 ;; Note that `org-export-as' doesn't really parse the current buffer,
2023 ;; but a copy of it (with the same buffer-local variables and
2024 ;; visibility), where include keywords are expanded and Babel blocks
2025 ;; are executed, if appropriate.
2026 ;; `org-export-with-current-buffer-copy' macro prepares that copy.
2028 ;; File inclusion is taken care of by
2029 ;; `org-export-expand-include-keyword' and
2030 ;; `org-export-prepare-file-contents'. Structure wise, including
2031 ;; a whole Org file in a buffer often makes little sense. For
2032 ;; example, if the file contains an headline and the include keyword
2033 ;; was within an item, the item should contain the headline. That's
2034 ;; why file inclusion should be done before any structure can be
2035 ;; associated to the file, that is before parsing.
2037 (defun org-export-as
2038 (backend &optional subtreep visible-only body-only ext-plist noexpand)
2039 "Transcode current Org buffer into BACKEND code.
2041 If narrowing is active in the current buffer, only transcode its
2042 narrowed part.
2044 If a region is active, transcode that region.
2046 When optional argument SUBTREEP is non-nil, transcode the
2047 sub-tree at point, extracting information from the headline
2048 properties first.
2050 When optional argument VISIBLE-ONLY is non-nil, don't export
2051 contents of hidden elements.
2053 When optional argument BODY-ONLY is non-nil, only return body
2054 code, without preamble nor postamble.
2056 Optional argument EXT-PLIST, when provided, is a property list
2057 with external parameters overriding Org default settings, but
2058 still inferior to file-local settings.
2060 Optional argument NOEXPAND, when non-nil, prevents included files
2061 to be expanded and Babel code to be executed.
2063 Return code as a string."
2064 (save-excursion
2065 (save-restriction
2066 ;; Narrow buffer to an appropriate region for parsing.
2067 (cond ((org-region-active-p)
2068 (narrow-to-region (region-beginning) (region-end)))
2069 (subtreep (org-narrow-to-subtree)))
2070 ;; Retrieve export options (INFO) and parsed tree (RAW-DATA),
2071 ;; Then options can be completed with tree properties. Note:
2072 ;; Buffer isn't parsed directly. Instead, a temporary copy is
2073 ;; created, where include keywords are expanded and code blocks
2074 ;; are evaluated. RAW-DATA is the parsed tree of the buffer
2075 ;; resulting from that process. Eventually call
2076 ;; `org-export-filter-parse-tree-functions'.
2077 (goto-char (point-min))
2078 (let ((info (org-export-get-environment backend subtreep ext-plist)))
2079 ;; Remove subtree's headline from contents if subtree mode is
2080 ;; activated.
2081 (when subtreep (forward-line) (narrow-to-region (point) (point-max)))
2082 ;; Install filters in communication channel.
2083 (setq info (org-export-install-filters backend info))
2084 (let ((raw-data
2085 (org-export-filter-apply-functions
2086 (plist-get info :filter-parse-tree)
2087 ;; If NOEXPAND is non-nil, simply parse current
2088 ;; visible part of buffer.
2089 (if noexpand (org-element-parse-buffer nil visible-only)
2090 (org-export-with-current-buffer-copy
2091 (org-export-expand-include-keyword nil)
2092 (let ((org-current-export-file (current-buffer)))
2093 (org-export-blocks-preprocess))
2094 (org-element-parse-buffer nil visible-only)))
2095 backend info)))
2096 ;; Complete communication channel with tree properties.
2097 (setq info
2098 (org-combine-plists
2099 info
2100 (org-export-collect-tree-properties raw-data info backend)))
2101 ;; Transcode RAW-DATA. Also call
2102 ;; `org-export-filter-final-output-functions'.
2103 (let* ((body (org-element-normalize-string
2104 (org-export-data raw-data backend info)))
2105 (template (intern (format "org-%s-template" backend)))
2106 (output (org-export-filter-apply-functions
2107 (plist-get info :filter-final-output)
2108 (if (or (not (fboundp template)) body-only) body
2109 (funcall template body info))
2110 backend info)))
2111 ;; Maybe add final OUTPUT to kill ring before returning it.
2112 (when org-export-copy-to-kill-ring (org-kill-new output))
2113 output))))))
2115 (defun org-export-to-buffer
2116 (backend buffer &optional subtreep visible-only body-only ext-plist noexpand)
2117 "Call `org-export-as' with output to a specified buffer.
2119 BACKEND is the back-end used for transcoding, as a symbol.
2121 BUFFER is the output buffer. If it already exists, it will be
2122 erased first, otherwise, it will be created.
2124 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY, EXT-PLIST
2125 and NOEXPAND are similar to those used in `org-export-as', which
2126 see.
2128 Return buffer."
2129 (let ((out (org-export-as
2130 backend subtreep visible-only body-only ext-plist noexpand))
2131 (buffer (get-buffer-create buffer)))
2132 (with-current-buffer buffer
2133 (erase-buffer)
2134 (insert out)
2135 (goto-char (point-min)))
2136 buffer))
2138 (defun org-export-to-file
2139 (backend file &optional subtreep visible-only body-only ext-plist noexpand)
2140 "Call `org-export-as' with output to a specified file.
2142 BACKEND is the back-end used for transcoding, as a symbol. FILE
2143 is the name of the output file, as a string.
2145 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY, EXT-PLIST
2146 and NOEXPAND are similar to those used in `org-export-as', which
2147 see.
2149 Return output file's name."
2150 ;; Checks for FILE permissions. `write-file' would do the same, but
2151 ;; we'd rather avoid needless transcoding of parse tree.
2152 (unless (file-writable-p file) (error "Output file not writable"))
2153 ;; Insert contents to a temporary buffer and write it to FILE.
2154 (let ((out (org-export-as
2155 backend subtreep visible-only body-only ext-plist noexpand)))
2156 (with-temp-buffer
2157 (insert out)
2158 (let ((coding-system-for-write org-export-coding-system))
2159 (write-file file))))
2160 ;; Return full path.
2161 file)
2163 (defun org-export-output-file-name (extension &optional subtreep pub-dir)
2164 "Return output file's name according to buffer specifications.
2166 EXTENSION is a string representing the output file extension,
2167 with the leading dot.
2169 With a non-nil optional argument SUBTREEP, try to determine
2170 output file's name by looking for \"EXPORT_FILE_NAME\" property
2171 of subtree at point.
2173 When optional argument PUB-DIR is set, use it as the publishing
2174 directory.
2176 Return file name as a string, or nil if it couldn't be
2177 determined."
2178 (let ((base-name
2179 ;; File name may come from EXPORT_FILE_NAME subtree property,
2180 ;; assuming point is at beginning of said sub-tree.
2181 (file-name-sans-extension
2182 (or (and subtreep
2183 (org-entry-get
2184 (save-excursion
2185 (ignore-errors
2186 (org-back-to-heading (not visible-only)) (point)))
2187 "EXPORT_FILE_NAME" t))
2188 ;; File name may be extracted from buffer's associated
2189 ;; file, if any.
2190 (buffer-file-name (buffer-base-buffer))
2191 ;; Can't determine file name on our own: Ask user.
2192 (let ((read-file-name-function
2193 (and org-completion-use-ido 'ido-read-file-name)))
2194 (read-file-name
2195 "Output file: " pub-dir nil nil nil
2196 (lambda (name)
2197 (string= (file-name-extension name t) extension))))))))
2198 ;; Build file name. Enforce EXTENSION over whatever user may have
2199 ;; come up with. PUB-DIR, if defined, always has precedence over
2200 ;; any provided path.
2201 (cond
2202 (pub-dir
2203 (concat (file-name-as-directory pub-dir)
2204 (file-name-nondirectory base-name)
2205 extension))
2206 ((string= (file-name-nondirectory base-name) base-name)
2207 (concat (file-name-as-directory ".") base-name extension))
2208 (t (concat base-name extension)))))
2210 (defmacro org-export-with-current-buffer-copy (&rest body)
2211 "Apply BODY in a copy of the current buffer.
2213 The copy preserves local variables and visibility of the original
2214 buffer.
2216 Point is at buffer's beginning when BODY is applied."
2217 (org-with-gensyms (original-buffer offset buffer-string overlays)
2218 `(let ((,original-buffer ,(current-buffer))
2219 (,offset ,(1- (point-min)))
2220 (,buffer-string ,(buffer-string))
2221 (,overlays (mapcar
2222 'copy-overlay (overlays-in (point-min) (point-max)))))
2223 (with-temp-buffer
2224 (let ((buffer-invisibility-spec nil))
2225 (org-clone-local-variables
2226 ,original-buffer
2227 "^\\(org-\\|orgtbl-\\|major-mode$\\|outline-\\(regexp\\|level\\)$\\)")
2228 (insert ,buffer-string)
2229 (mapc (lambda (ov)
2230 (move-overlay
2232 (- (overlay-start ov) ,offset)
2233 (- (overlay-end ov) ,offset)
2234 (current-buffer)))
2235 ,overlays)
2236 (goto-char (point-min))
2237 (progn ,@body))))))
2238 (def-edebug-spec org-export-with-current-buffer-copy (body))
2240 (defun org-export-expand-include-keyword (included)
2241 "Expand every include keyword in buffer.
2242 INCLUDED is a list of included file names along with their line
2243 restriction, when appropriate. It is used to avoid infinite
2244 recursion."
2245 (let ((case-fold-search nil))
2246 (goto-char (point-min))
2247 (while (re-search-forward "^[ \t]*#\\+include: \\(.*\\)" nil t)
2248 (when (eq (org-element-type (save-match-data (org-element-at-point)))
2249 'keyword)
2250 (beginning-of-line)
2251 ;; Extract arguments from keyword's value.
2252 (let* ((value (match-string 1))
2253 (ind (org-get-indentation))
2254 (file (and (string-match "^\"\\(\\S-+\\)\"" value)
2255 (prog1 (expand-file-name (match-string 1 value))
2256 (setq value (replace-match "" nil nil value)))))
2257 (lines
2258 (and (string-match
2259 ":lines +\"\\(\\(?:[0-9]+\\)?-\\(?:[0-9]+\\)?\\)\"" value)
2260 (prog1 (match-string 1 value)
2261 (setq value (replace-match "" nil nil value)))))
2262 (env (cond ((string-match "\\<example\\>" value) 'example)
2263 ((string-match "\\<src\\(?: +\\(.*\\)\\)?" value)
2264 (match-string 1 value))))
2265 ;; Minimal level of included file defaults to the child
2266 ;; level of the current headline, if any, or one. It
2267 ;; only applies is the file is meant to be included as
2268 ;; an Org one.
2269 (minlevel
2270 (and (not env)
2271 (if (string-match ":minlevel +\\([0-9]+\\)" value)
2272 (prog1 (string-to-number (match-string 1 value))
2273 (setq value (replace-match "" nil nil value)))
2274 (let ((cur (org-current-level)))
2275 (if cur (1+ (org-reduced-level cur)) 1))))))
2276 ;; Remove keyword.
2277 (delete-region (point) (progn (forward-line) (point)))
2278 (cond
2279 ((not (file-readable-p file)) (error "Cannot include file %s" file))
2280 ;; Check if files has already been parsed. Look after
2281 ;; inclusion lines too, as different parts of the same file
2282 ;; can be included too.
2283 ((member (list file lines) included)
2284 (error "Recursive file inclusion: %s" file))
2286 (cond
2287 ((eq env 'example)
2288 (insert
2289 (let ((ind-str (make-string ind ? ))
2290 (contents
2291 ;; Protect sensitive contents with commas.
2292 (replace-regexp-in-string
2293 "\\(^\\)\\([*]\\|[ \t]*#\\+\\)" ","
2294 (org-export-prepare-file-contents file lines)
2295 nil nil 1)))
2296 (format "%s#+begin_example\n%s%s#+end_example\n"
2297 ind-str contents ind-str))))
2298 ((stringp env)
2299 (insert
2300 (let ((ind-str (make-string ind ? ))
2301 (contents
2302 ;; Protect sensitive contents with commas.
2303 (replace-regexp-in-string
2304 (if (string= env "org") "\\(^\\)\\(.\\)"
2305 "\\(^\\)\\([*]\\|[ \t]*#\\+\\)") ","
2306 (org-export-prepare-file-contents file lines)
2307 nil nil 1)))
2308 (format "%s#+begin_src %s\n%s%s#+end_src\n"
2309 ind-str env contents ind-str))))
2311 (insert
2312 (with-temp-buffer
2313 (org-mode)
2314 (insert
2315 (org-export-prepare-file-contents file lines ind minlevel))
2316 (org-export-expand-include-keyword
2317 (cons (list file lines) included))
2318 (buffer-string))))))))))))
2320 (defun org-export-prepare-file-contents (file &optional lines ind minlevel)
2321 "Prepare the contents of FILE for inclusion and return them as a string.
2323 When optional argument LINES is a string specifying a range of
2324 lines, include only those lines.
2326 Optional argument IND, when non-nil, is an integer specifying the
2327 global indentation of returned contents. Since its purpose is to
2328 allow an included file to stay in the same environment it was
2329 created \(i.e. a list item), it doesn't apply past the first
2330 headline encountered.
2332 Optional argument MINLEVEL, when non-nil, is an integer
2333 specifying the level that any top-level headline in the included
2334 file should have."
2335 (with-temp-buffer
2336 (insert-file-contents file)
2337 (when lines
2338 (let* ((lines (split-string lines "-"))
2339 (lbeg (string-to-number (car lines)))
2340 (lend (string-to-number (cadr lines)))
2341 (beg (if (zerop lbeg) (point-min)
2342 (goto-char (point-min))
2343 (forward-line (1- lbeg))
2344 (point)))
2345 (end (if (zerop lend) (point-max)
2346 (goto-char (point-min))
2347 (forward-line (1- lend))
2348 (point))))
2349 (narrow-to-region beg end)))
2350 ;; Remove blank lines at beginning and end of contents. The logic
2351 ;; behind that removal is that blank lines around include keyword
2352 ;; override blank lines in included file.
2353 (goto-char (point-min))
2354 (org-skip-whitespace)
2355 (beginning-of-line)
2356 (delete-region (point-min) (point))
2357 (goto-char (point-max))
2358 (skip-chars-backward " \r\t\n")
2359 (forward-line)
2360 (delete-region (point) (point-max))
2361 ;; If IND is set, preserve indentation of include keyword until
2362 ;; the first headline encountered.
2363 (when ind
2364 (unless (eq major-mode 'org-mode) (org-mode))
2365 (goto-char (point-min))
2366 (let ((ind-str (make-string ind ? )))
2367 (while (not (or (eobp) (looking-at org-outline-regexp-bol)))
2368 ;; Do not move footnote definitions out of column 0.
2369 (unless (and (looking-at org-footnote-definition-re)
2370 (eq (org-element-type (org-element-at-point))
2371 'footnote-definition))
2372 (insert ind-str))
2373 (forward-line))))
2374 ;; When MINLEVEL is specified, compute minimal level for headlines
2375 ;; in the file (CUR-MIN), and remove stars to each headline so
2376 ;; that headlines with minimal level have a level of MINLEVEL.
2377 (when minlevel
2378 (unless (eq major-mode 'org-mode) (org-mode))
2379 (let ((levels (org-map-entries
2380 (lambda () (org-reduced-level (org-current-level))))))
2381 (when levels
2382 (let ((offset (- minlevel (apply 'min levels))))
2383 (unless (zerop offset)
2384 (when org-odd-levels-only (setq offset (* offset 2)))
2385 ;; Only change stars, don't bother moving whole
2386 ;; sections.
2387 (org-map-entries
2388 (lambda () (if (< offset 0) (delete-char (abs offset))
2389 (insert (make-string offset ?*))))))))))
2390 (buffer-string)))
2393 ;;; Tools For Back-Ends
2395 ;; A whole set of tools is available to help build new exporters. Any
2396 ;; function general enough to have its use across many back-ends
2397 ;; should be added here.
2399 ;; As of now, functions operating on footnotes, headlines, links,
2400 ;; macros, references, src-blocks, tables and tables of contents are
2401 ;; implemented.
2403 ;;;; For Footnotes
2405 ;; `org-export-collect-footnote-definitions' is a tool to list
2406 ;; actually used footnotes definitions in the whole parse tree, or in
2407 ;; an headline, in order to add footnote listings throughout the
2408 ;; transcoded data.
2410 ;; `org-export-footnote-first-reference-p' is a predicate used by some
2411 ;; back-ends, when they need to attach the footnote definition only to
2412 ;; the first occurrence of the corresponding label.
2414 ;; `org-export-get-footnote-definition' and
2415 ;; `org-export-get-footnote-number' provide easier access to
2416 ;; additional information relative to a footnote reference.
2418 (defun org-export-collect-footnote-definitions (data info)
2419 "Return an alist between footnote numbers, labels and definitions.
2421 DATA is the parse tree from which definitions are collected.
2422 INFO is the plist used as a communication channel.
2424 Definitions are sorted by order of references. They either
2425 appear as Org data \(transcoded with `org-export-data'\) or as
2426 a secondary string for inlined footnotes \(transcoded with
2427 `org-export-secondary-string'\). Unreferenced definitions are
2428 ignored."
2429 (let (refs)
2430 ;; Collect seen references in REFS.
2431 (org-element-map
2432 data 'footnote-reference
2433 (lambda (footnote local)
2434 (when (org-export-footnote-first-reference-p footnote local)
2435 (list (org-export-get-footnote-number footnote local)
2436 (org-element-property :label footnote)
2437 (org-export-get-footnote-definition footnote local))))
2438 info)))
2440 (defun org-export-footnote-first-reference-p (footnote-reference info)
2441 "Non-nil when a footnote reference is the first one for its label.
2443 FOOTNOTE-REFERENCE is the footnote reference being considered.
2444 INFO is the plist used as a communication channel."
2445 (let ((label (org-element-property :label footnote-reference)))
2446 (or (not label)
2447 (equal
2448 footnote-reference
2449 (org-element-map
2450 (plist-get info :parse-tree) 'footnote-reference
2451 (lambda (footnote local)
2452 (when (string= (org-element-property :label footnote) label)
2453 footnote))
2454 info 'first-match)))))
2456 (defun org-export-get-footnote-definition (footnote-reference info)
2457 "Return definition of FOOTNOTE-REFERENCE as parsed data.
2458 INFO is the plist used as a communication channel."
2459 (let ((label (org-element-property :label footnote-reference)))
2460 (or (org-element-property :inline-definition footnote-reference)
2461 (cdr (assoc label (plist-get info :footnote-definition-alist))))))
2463 (defun org-export-get-footnote-number (footnote info)
2464 "Return number associated to a footnote.
2466 FOOTNOTE is either a footnote reference or a footnote definition.
2467 INFO is the plist used as a communication channel."
2468 (let ((label (org-element-property :label footnote)) seen-refs)
2469 (org-element-map
2470 (plist-get info :parse-tree) 'footnote-reference
2471 (lambda (fn local)
2472 (let ((fn-lbl (org-element-property :label fn)))
2473 (cond
2474 ((and (not fn-lbl) (equal fn footnote)) (1+ (length seen-refs)))
2475 ((and label (string= label fn-lbl)) (1+ (length seen-refs)))
2476 ;; Anonymous footnote: it's always a new one. Also, be sure
2477 ;; to return nil from the `cond' so `first-match' doesn't
2478 ;; get us out of the loop.
2479 ((not fn-lbl) (push 'inline seen-refs) nil)
2480 ;; Label not seen so far: add it so SEEN-REFS. Again,
2481 ;; return nil to stay in the loop.
2482 ((not (member fn-lbl seen-refs)) (push fn-lbl seen-refs) nil))))
2483 info 'first-match)))
2486 ;;;; For Headlines
2488 ;; `org-export-get-relative-level' is a shortcut to get headline
2489 ;; level, relatively to the lower headline level in the parsed tree.
2491 ;; `org-export-get-headline-number' returns the section number of an
2492 ;; headline, while `org-export-number-to-roman' allows to convert it
2493 ;; to roman numbers.
2495 ;; `org-export-low-level-p', `org-export-first-sibling-p' and
2496 ;; `org-export-last-sibling-p' are three useful predicates when it
2497 ;; comes to fulfill the `:headline-levels' property.
2499 (defun org-export-get-relative-level (headline info)
2500 "Return HEADLINE relative level within current parsed tree.
2501 INFO is a plist holding contextual information."
2502 (+ (org-element-property :level headline)
2503 (or (plist-get info :headline-offset) 0)))
2505 (defun org-export-low-level-p (headline info)
2506 "Non-nil when HEADLINE is considered as low level.
2508 INFO is a plist used as a communication channel.
2510 A low level headlines has a relative level greater than
2511 `:headline-levels' property value.
2513 Return value is the difference between HEADLINE relative level
2514 and the last level being considered as high enough, or nil."
2515 (let ((limit (plist-get info :headline-levels)))
2516 (when (wholenump limit)
2517 (let ((level (org-export-get-relative-level headline info)))
2518 (and (> level limit) (- level limit))))))
2520 (defun org-export-get-headline-number (headline info)
2521 "Return HEADLINE numbering as a list of numbers.
2522 INFO is a plist holding contextual information."
2523 (cdr (assoc headline (plist-get info :headline-numbering))))
2525 (defun org-export-number-to-roman (n)
2526 "Convert integer N into a roman numeral."
2527 (let ((roman '((1000 . "M") (900 . "CM") (500 . "D") (400 . "CD")
2528 ( 100 . "C") ( 90 . "XC") ( 50 . "L") ( 40 . "XL")
2529 ( 10 . "X") ( 9 . "IX") ( 5 . "V") ( 4 . "IV")
2530 ( 1 . "I")))
2531 (res ""))
2532 (if (<= n 0)
2533 (number-to-string n)
2534 (while roman
2535 (if (>= n (caar roman))
2536 (setq n (- n (caar roman))
2537 res (concat res (cdar roman)))
2538 (pop roman)))
2539 res)))
2541 (defun org-export-first-sibling-p (headline info)
2542 "Non-nil when HEADLINE is the first sibling in its sub-tree.
2543 INFO is the plist used as a communication channel."
2544 (not (eq (org-element-type (org-export-get-previous-element headline info))
2545 'headline)))
2547 (defun org-export-last-sibling-p (headline info)
2548 "Non-nil when HEADLINE is the last sibling in its sub-tree.
2549 INFO is the plist used as a communication channel."
2550 (not (org-export-get-next-element headline info)))
2553 ;;;; For Links
2555 ;; `org-export-solidify-link-text' turns a string into a safer version
2556 ;; for links, replacing most non-standard characters with hyphens.
2558 ;; `org-export-get-coderef-format' returns an appropriate format
2559 ;; string for coderefs.
2561 ;; `org-export-inline-image-p' returns a non-nil value when the link
2562 ;; provided should be considered as an inline image.
2564 ;; `org-export-resolve-fuzzy-link' searches destination of fuzzy links
2565 ;; (i.e. links with "fuzzy" as type) within the parsed tree, and
2566 ;; returns an appropriate unique identifier when found, or nil.
2568 ;; `org-export-resolve-id-link' returns the first headline with
2569 ;; specified id or custom-id in parse tree, or nil when none was
2570 ;; found.
2572 ;; `org-export-resolve-coderef' associates a reference to a line
2573 ;; number in the element it belongs, or returns the reference itself
2574 ;; when the element isn't numbered.
2576 (defun org-export-solidify-link-text (s)
2577 "Take link text S and make a safe target out of it."
2578 (save-match-data
2579 (mapconcat 'identity (org-split-string s "[^a-zA-Z0-9_.-]+") "-")))
2581 (defun org-export-get-coderef-format (path desc)
2582 "Return format string for code reference link.
2583 PATH is the link path. DESC is its description."
2584 (save-match-data
2585 (cond ((string-match (regexp-quote (concat "(" path ")")) desc)
2586 (replace-match "%s" t t desc))
2587 ((string= desc "") "%s")
2588 (t desc))))
2590 (defun org-export-inline-image-p (link &optional rules)
2591 "Non-nil if LINK object points to an inline image.
2593 Optional argument is a set of RULES defining inline images. It
2594 is an alist where associations have the following shape:
2596 \(TYPE . REGEXP)
2598 Applying a rule means apply REGEXP against LINK's path when its
2599 type is TYPE. The function will return a non-nil value if any of
2600 the provided rules is non-nil. The default rule is
2601 `org-export-default-inline-image-rule'.
2603 This only applies to links without a description."
2604 (and (not (org-element-contents link))
2605 (let ((case-fold-search t)
2606 (rules (or rules org-export-default-inline-image-rule)))
2607 (some
2608 (lambda (rule)
2609 (and (string= (org-element-property :type link) (car rule))
2610 (string-match (cdr rule)
2611 (org-element-property :path link))))
2612 rules))))
2614 (defun org-export-resolve-fuzzy-link (link info)
2615 "Return LINK destination.
2617 INFO is a plist holding contextual information.
2619 Return value can be an object, an element, or nil:
2621 - If LINK path exactly matches any target, return the target
2622 object.
2624 - If LINK path exactly matches any headline name, return that
2625 element. If more than one headline share that name, priority
2626 will be given to the one with the closest common ancestor, if
2627 any, or the first one in the parse tree otherwise.
2629 - Otherwise, return nil.
2631 Assume LINK type is \"fuzzy\"."
2632 (let ((path (org-element-property :path link)))
2633 ;; Link points to a target: return it.
2634 (or (loop for target in (plist-get info :target-list)
2635 when (string= (org-element-property :raw-value target) path)
2636 return target)
2637 ;; Link either points to an headline or nothing. Try to find
2638 ;; the source, with priority given to headlines with the closest
2639 ;; common ancestor. If such candidate is found, return its
2640 ;; beginning position as an unique identifier, otherwise return
2641 ;; nil.
2642 (let ((find-headline
2643 (function
2644 ;; Return first headline whose `:raw-value' property
2645 ;; is NAME in parse tree DATA, or nil.
2646 (lambda (name data)
2647 (org-element-map
2648 data 'headline
2649 (lambda (headline local)
2650 (when (string=
2651 (org-element-property :raw-value headline)
2652 name)
2653 headline))
2654 info 'first-match)))))
2655 ;; Search among headlines sharing an ancestor with link,
2656 ;; from closest to farthest.
2657 (or (catch 'exit
2658 (mapc
2659 (lambda (parent)
2660 (when (eq (org-element-type parent) 'headline)
2661 (let ((foundp (funcall find-headline path parent)))
2662 (when foundp (throw 'exit foundp)))))
2663 (org-export-get-genealogy link info)) nil)
2664 ;; No match with a common ancestor: try the full parse-tree.
2665 (funcall find-headline path (plist-get info :parse-tree)))))))
2667 (defun org-export-resolve-id-link (link info)
2668 "Return headline referenced as LINK destination.
2670 INFO is a plist used as a communication channel.
2672 Return value can be an headline element or nil. Assume LINK type
2673 is either \"id\" or \"custom-id\"."
2674 (let ((id (org-element-property :path link)))
2675 (org-element-map
2676 (plist-get info :parse-tree) 'headline
2677 (lambda (headline local)
2678 (when (or (string= (org-element-property :id headline) id)
2679 (string= (org-element-property :custom-id headline) id))
2680 headline))
2681 info 'first-match)))
2683 (defun org-export-resolve-ref-link (link info)
2684 "Return element referenced as LINK destination.
2686 INFO is a plist used as a communication channel.
2688 Assume LINK type is \"ref\" and. Return value is the first
2689 element whose `:name' property matches LINK's `:path', or nil."
2690 (let ((name (org-element-property :path link)))
2691 (org-element-map
2692 (plist-get info :parse-tree) org-element-all-elements
2693 (lambda (el local)
2694 (when (string= (org-element-property :name el) name) el))
2695 info 'first-match)))
2697 (defun org-export-resolve-coderef (ref info)
2698 "Resolve a code reference REF.
2700 INFO is a plist used as a communication channel.
2702 Return associated line number in source code, or REF itself,
2703 depending on src-block or example element's switches."
2704 (org-element-map
2705 (plist-get info :parse-tree) '(src-block example)
2706 (lambda (el local)
2707 (let ((switches (or (org-element-property :switches el) "")))
2708 (with-temp-buffer
2709 (insert (org-trim (org-element-property :value el)))
2710 ;; Build reference regexp.
2711 (let* ((label
2712 (or (and (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
2713 (match-string 1 switches))
2714 org-coderef-label-format))
2715 (ref-re
2716 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
2717 (replace-regexp-in-string "%s" ref label nil t))))
2718 ;; Element containing REF is found. Only associate REF to
2719 ;; a line number if element has "+n" or "-n" and "-k" or
2720 ;; "-r" as switches. When it has "+n", count accumulated
2721 ;; locs before, too.
2722 (when (re-search-backward ref-re nil t)
2723 (cond
2724 ((not (string-match "-[kr]\\>" switches)) ref)
2725 ((string-match "-n\\>" switches) (line-number-at-pos))
2726 ((string-match "\\+n\\>" switches)
2727 (+ (org-export-get-loc el local) (line-number-at-pos)))
2728 (t ref)))))))
2729 info 'first-match))
2732 ;;;; For Macros
2734 ;; `org-export-expand-macro' simply takes care of expanding macros.
2736 (defun org-export-expand-macro (macro info)
2737 "Expand MACRO and return it as a string.
2738 INFO is a plist holding export options."
2739 (let* ((key (org-element-property :key macro))
2740 (args (org-element-property :args macro))
2741 ;; User's macros are stored in the communication channel with
2742 ;; a ":macro-" prefix. If it's a string leave it as-is.
2743 ;; Otherwise, it's a secondary string that needs to be
2744 ;; expanded recursively.
2745 (value
2746 (let ((val (plist-get info (intern (format ":macro-%s" key)))))
2747 (if (stringp val) val
2748 (org-export-secondary-string
2749 val (plist-get info :back-end) info)))))
2750 ;; Replace arguments in VALUE.
2751 (let ((s 0) n)
2752 (while (string-match "\\$\\([0-9]+\\)" value s)
2753 (setq s (1+ (match-beginning 0))
2754 n (string-to-number (match-string 1 value)))
2755 (and (>= (length args) n)
2756 (setq value (replace-match (nth (1- n) args) t t value)))))
2757 ;; VALUE starts with "(eval": it is a s-exp, `eval' it.
2758 (when (string-match "\\`(eval\\>" value)
2759 (setq value (eval (read value))))
2760 ;; Return string.
2761 (format "%s" (or value ""))))
2764 ;;;; For References
2766 ;; `org-export-get-ordinal' associates a sequence number to any object
2767 ;; or element.
2769 (defun org-export-get-ordinal
2770 (element info &optional types within-section predicate)
2771 "Return ordinal number of an element or object.
2773 ELEMENT is the element or object considered. INFO is the plist
2774 used as a communication channel.
2776 Optional argument TYPES, when non-nil, is a list of element or
2777 object types, as symbols, that should also be counted in.
2778 Otherwise, only provided element's type is considered.
2780 When optional argument WITHIN-SECTION is non-nil, narrow counting
2781 to the section containing ELEMENT.
2783 Optional argument PREDICATE is a function returning a non-nil
2784 value if the current element or object should be counted in. It
2785 accepts one argument: the element or object being considered.
2786 This argument allows to count only a certain type of objects,
2787 like inline images, which are a subset of links \(in that case,
2788 `org-export-inline-image-p' might be an useful predicate\)."
2789 (let ((counter 0)
2790 ;; Determine if search should apply to current section, in
2791 ;; which case it should be retrieved first, or to full parse
2792 ;; tree. As a special case, an element or object without
2793 ;; a parent headline will also trigger a full search,
2794 ;; notwithstanding WITHIN-SECTION value.
2795 (data
2796 (if (not within-section) (plist-get info :parse-tree)
2797 (or (org-export-get-parent-headline element info)
2798 (plist-get info :parse-tree)))))
2799 ;; Increment counter until ELEMENT is found again.
2800 (org-element-map
2801 data (or types (org-element-type element))
2802 (lambda (el local)
2803 (cond
2804 ((equal element el) (1+ counter))
2805 ((not predicate) (incf counter) nil)
2806 ((funcall predicate el) (incf counter) nil)))
2807 info 'first-match)))
2810 ;;;; For Src-Blocks
2812 ;; `org-export-get-loc' counts number of code lines accumulated in
2813 ;; src-block or example-block elements with a "+n" switch until
2814 ;; a given element, excluded. Note: "-n" switches reset that count.
2816 ;; `org-export-handle-code' takes care of line numbering and reference
2817 ;; cleaning in source code, when appropriate.
2819 (defun org-export-get-loc (element info)
2820 "Return accumulated lines of code up to ELEMENT.
2822 INFO is the plist used as a communication channel.
2824 ELEMENT is excluded from count."
2825 (let ((loc 0))
2826 (org-element-map
2827 (plist-get info :parse-tree)
2828 `(src-block example-block ,(org-element-type element))
2829 (lambda (el local)
2830 (cond
2831 ;; ELEMENT is reached: Quit the loop.
2832 ((equal el element) t)
2833 ;; Only count lines from src-block and example-block elements
2834 ;; with a "+n" or "-n" switch. A "-n" switch resets counter.
2835 ((not (memq (org-element-type el) '(src-block example-block))) nil)
2836 ((let ((switches (org-element-property :switches el)))
2837 (when (and switches (string-match "\\([-+]\\)n\\>" switches))
2838 ;; Accumulate locs or reset them.
2839 (let ((accumulatep (string= (match-string 1 switches) "-"))
2840 (lines (org-count-lines
2841 (org-trim (org-element-property :value el)))))
2842 (setq loc (if accumulatep lines (+ loc lines))))))
2843 ;; Return nil to stay in the loop.
2844 nil)))
2845 info 'first-match)
2846 ;; Return value.
2847 loc))
2849 (defun org-export-handle-code (element info &optional num-fmt ref-fmt delayed)
2850 "Handle line numbers and code references in ELEMENT.
2852 ELEMENT has either a `src-block' an `example-block' type. INFO
2853 is a plist used as a communication channel.
2855 If optional argument NUM-FMT is a string, it will be used as
2856 a format string for numbers at beginning of each line.
2858 If optional argument REF-FMT is a string, it will be used as
2859 a format string for each line of code containing a reference.
2861 When optional argument DELAYED is non-nil, `org-loc' and
2862 `org-coderef' properties, set to an adequate value, are applied
2863 to, respectively, numbered lines and lines with a reference. No
2864 line numbering is done and all references are stripped from the
2865 resulting string. Both NUM-FMT and REF-FMT arguments are ignored
2866 in that situation.
2868 Return new code as a string."
2869 (let* ((switches (or (org-element-property :switches element) ""))
2870 (code (org-element-property :value element))
2871 (numberp (string-match "[-+]n\\>" switches))
2872 (accumulatep (string-match "\\+n\\>" switches))
2873 ;; Initialize loc counter when any kind of numbering is
2874 ;; active.
2875 (total-LOC (cond
2876 (accumulatep (org-export-get-loc element info))
2877 (numberp 0)))
2878 ;; Get code and clean it. Remove blank lines at its
2879 ;; beginning and end. Also remove protective commas.
2880 (preserve-indent-p (or org-src-preserve-indentation
2881 (string-match "-i\\>" switches)))
2882 (replace-labels (when (string-match "-r\\>" switches)
2883 (if (string-match "-k\\>" switches) 'keep t)))
2884 (code (let ((c (replace-regexp-in-string
2885 "\\`\\([ \t]*\n\\)+" ""
2886 (replace-regexp-in-string
2887 "\\(:?[ \t]*\n\\)*[ \t]*\\'" "\n" code))))
2888 ;; If appropriate, remove global indentation.
2889 (unless preserve-indent-p (setq c (org-remove-indentation c)))
2890 ;; Free up the protected lines. Note: Org blocks
2891 ;; have commas at the beginning or every line.
2892 (if (string=
2893 (or (org-element-property :language element) "")
2894 "org")
2895 (replace-regexp-in-string "^," "" c)
2896 (replace-regexp-in-string
2897 "^\\(,\\)\\(:?\\*\\|[ \t]*#\\+\\)" "" c nil nil 1))))
2898 ;; Split code to process it line by line.
2899 (code-lines (org-split-string code "\n"))
2900 ;; If numbering is active, ensure line numbers will be
2901 ;; correctly padded before applying the format string.
2902 (num-fmt
2903 (when (and (not delayed) numberp)
2904 (format (if (stringp num-fmt) num-fmt "%s: ")
2905 (format "%%%ds"
2906 (length (number-to-string
2907 (+ (length code-lines) total-LOC)))))))
2908 ;; Get format used for references.
2909 (label-fmt (or (and (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
2910 (match-string 1 switches))
2911 org-coderef-label-format))
2912 ;; Build a regexp matching a loc with a reference.
2913 (with-ref-re (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
2914 (replace-regexp-in-string
2915 "%s" "\\([-a-zA-Z0-9_ ]+\\)" label-fmt nil t))))
2916 (org-element-normalize-string
2917 (mapconcat
2918 (lambda (loc)
2919 ;; Maybe add line number to current line of code (LOC).
2920 (when numberp
2921 (incf total-LOC)
2922 (setq loc (if delayed (org-add-props loc nil 'org-loc total-LOC)
2923 (concat (format num-fmt total-LOC) loc))))
2924 ;; Take action if at a ref line.
2925 (when (string-match with-ref-re loc)
2926 (let ((ref (match-string 3 loc)))
2927 (setq loc
2928 ;; Option "-r" without "-k" removes labels.
2929 ;; A non-nil DELAYED removes labels unconditionally.
2930 (if (or delayed
2931 (and replace-labels (not (eq replace-labels 'keep))))
2932 (replace-match "" nil nil loc 1)
2933 (replace-match (format "(%s)" ref) nil nil loc 2)))
2934 ;; Store REF in `org-coderef' property if DELAYED asks to.
2935 (cond (delayed (setq loc (org-add-props loc nil 'org-coderef ref)))
2936 ;; If REF-FMT is defined, apply it to current LOC.
2937 ((stringp ref-fmt) (setq loc (format ref-fmt loc))))))
2938 ;; Return updated LOC for concatenation.
2939 loc)
2940 code-lines "\n"))))
2943 ;;;; For Tables
2945 ;; `org-export-table-format-info' extracts formatting information
2946 ;; (alignment, column groups and presence of a special column) from
2947 ;; a raw table and returns it as a property list.
2949 ;; `org-export-clean-table' cleans the raw table from any Org
2950 ;; table-specific syntax.
2952 (defun org-export-table-format-info (table)
2953 "Extract info from TABLE.
2954 Return a plist whose properties and values are:
2955 `:alignment' vector of strings among \"r\", \"l\" and \"c\",
2956 `:column-groups' vector of symbols among `start', `end', `start-end',
2957 `:row-groups' list of integers representing row groups.
2958 `:special-column-p' non-nil if table has a special column.
2959 `:width' vector of integers representing desired width of
2960 current column, or nil."
2961 (with-temp-buffer
2962 (insert table)
2963 (goto-char 1)
2964 (org-table-align)
2965 (let ((align (vconcat (mapcar (lambda (c) (if c "r" "l"))
2966 org-table-last-alignment)))
2967 (width (make-vector (length org-table-last-alignment) nil))
2968 (colgroups (make-vector (length org-table-last-alignment) nil))
2969 (row-group 0)
2970 (rowgroups)
2971 (special-column-p 'empty))
2972 (mapc (lambda (row)
2973 (if (string-match "^[ \t]*|[-+]+|[ \t]*$" row)
2974 (incf row-group)
2975 ;; Determine if a special column is present by looking
2976 ;; for special markers in the first column. More
2977 ;; accurately, the first column is considered special
2978 ;; if it only contains special markers and, maybe,
2979 ;; empty cells.
2980 (setq special-column-p
2981 (cond
2982 ((not special-column-p) nil)
2983 ((string-match "^[ \t]*| *\\\\?\\([/#!$*_^]\\) *|" row)
2984 'special)
2985 ((string-match "^[ \t]*| +|" row) special-column-p))))
2986 (cond
2987 ;; Read forced alignment and width information, if any,
2988 ;; and determine final alignment for the table.
2989 ((org-table-cookie-line-p row)
2990 (let ((col 0))
2991 (mapc (lambda (field)
2992 (when (string-match
2993 "<\\([lrc]\\)?\\([0-9]+\\)?>" field)
2994 (let ((align-data (match-string 1 field)))
2995 (when align-data (aset align col align-data)))
2996 (let ((w-data (match-string 2 field)))
2997 (when w-data
2998 (aset width col (string-to-number w-data)))))
2999 (incf col))
3000 (org-split-string row "[ \t]*|[ \t]*"))))
3001 ;; Read column groups information.
3002 ((org-table-colgroup-line-p row)
3003 (let ((col 0))
3004 (mapc (lambda (field)
3005 (aset colgroups col
3006 (cond ((string= "<" field) 'start)
3007 ((string= ">" field) 'end)
3008 ((string= "<>" field) 'start-end)))
3009 (incf col))
3010 (org-split-string row "[ \t]*|[ \t]*"))))
3011 ;; Contents line.
3012 (t (push row-group rowgroups))))
3013 (org-split-string table "\n"))
3014 ;; Return plist.
3015 (list :alignment align
3016 :column-groups colgroups
3017 :row-groups (reverse rowgroups)
3018 :special-column-p (eq special-column-p 'special)
3019 :width width))))
3021 (defun org-export-clean-table (table specialp)
3022 "Clean string TABLE from its formatting elements.
3023 Remove any row containing column groups or formatting cookies and
3024 rows starting with a special marker. If SPECIALP is non-nil,
3025 assume the table contains a special formatting column and remove
3026 it also."
3027 (let ((rows (org-split-string table "\n")))
3028 (mapconcat 'identity
3029 (delq nil
3030 (mapcar
3031 (lambda (row)
3032 (cond
3033 ((org-table-colgroup-line-p row) nil)
3034 ((org-table-cookie-line-p row) nil)
3035 ;; Ignore rows starting with a special marker.
3036 ((string-match "^[ \t]*| *[!_^/$] *|" row) nil)
3037 ;; Remove special column.
3038 ((and specialp
3039 (or (string-match "^\\([ \t]*\\)|-+\\+" row)
3040 (string-match "^\\([ \t]*\\)|[^|]*|" row)))
3041 (replace-match "\\1|" t nil row))
3042 (t row)))
3043 rows))
3044 "\n")))
3047 ;;;; For Tables Of Contents
3049 ;; `org-export-collect-headlines' builds a list of all exportable
3050 ;; headline elements, maybe limited to a certain depth. One can then
3051 ;; easily parse it and transcode it.
3053 ;; Building lists of tables, figures or listings is quite similar.
3054 ;; Once the generic function `org-export-collect-elements' is defined,
3055 ;; `org-export-collect-tables', `org-export-collect-figures' and
3056 ;; `org-export-collect-listings' can be derived from it.
3058 (defun org-export-collect-headlines (info &optional n)
3059 "Collect headlines in order to build a table of contents.
3061 INFO is a plist used as a communication channel.
3063 When non-nil, optional argument N must be an integer. It
3064 specifies the depth of the table of contents.
3066 Return a list of all exportable headlines as parsed elements."
3067 (org-element-map
3068 (plist-get info :parse-tree)
3069 'headline
3070 (lambda (headline local)
3071 ;; Strip contents from HEADLINE.
3072 (let ((relative-level (org-export-get-relative-level headline local)))
3073 (unless (and n (> relative-level n)) headline)))
3074 info))
3076 (defun org-export-collect-elements (type info &optional predicate)
3077 "Collect referenceable elements of a determined type.
3079 TYPE can be a symbol or a list of symbols specifying element
3080 types to search. Only elements with a caption or a name are
3081 collected.
3083 INFO is a plist used as a communication channel.
3085 When non-nil, optional argument PREDICATE is a function accepting
3086 one argument, an element of type TYPE. It returns a non-nil
3087 value when that element should be collected.
3089 Return a list of all elements found, in order of appearance."
3090 (org-element-map
3091 (plist-get info :parse-tree) type
3092 (lambda (element local)
3093 (and (or (org-element-property :caption element)
3094 (org-element-property :name element))
3095 (or (not predicate) (funcall predicate element))
3096 element)) info))
3098 (defun org-export-collect-tables (info)
3099 "Build a list of tables.
3101 INFO is a plist used as a communication channel.
3103 Return a list of table elements with a caption or a name
3104 affiliated keyword."
3105 (org-export-collect-elements 'table info))
3107 (defun org-export-collect-figures (info predicate)
3108 "Build a list of figures.
3110 INFO is a plist used as a communication channel. PREDICATE is
3111 a function which accepts one argument: a paragraph element and
3112 whose return value is non-nil when that element should be
3113 collected.
3115 A figure is a paragraph type element, with a caption or a name,
3116 verifying PREDICATE. The latter has to be provided since
3117 a \"figure\" is a vague concept that may depend on back-end.
3119 Return a list of elements recognized as figures."
3120 (org-export-collect-elements 'paragraph info predicate))
3122 (defun org-export-collect-listings (info)
3123 "Build a list of src blocks.
3125 INFO is a plist used as a communication channel.
3127 Return a list of src-block elements with a caption or a name
3128 affiliated keyword."
3129 (org-export-collect-elements 'src-block info))
3132 ;;;; Topology
3134 ;; Here are various functions to retrieve information about the
3135 ;; neighbourhood of a given element or object. Neighbours of interest
3136 ;; are direct parent (`org-export-get-parent'), parent headline
3137 ;; (`org-export-get-parent-headline'), parent paragraph
3138 ;; (`org-export-get-parent-paragraph'), previous element or object
3139 ;; (`org-export-get-previous-element') and next element or object
3140 ;; (`org-export-get-next-element').
3142 ;; All of these functions are just a specific use of the more generic
3143 ;; `org-export-get-genealogy', which returns the genealogy relative to
3144 ;; the element or object.
3146 (defun org-export-get-genealogy (blob info)
3147 "Return genealogy relative to a given element or object.
3148 BLOB is the element or object being considered. INFO is a plist
3149 used as a communication channel."
3150 (let* ((end (org-element-property :end blob))
3151 (walk-data
3152 (lambda (data genealogy)
3153 (mapc
3154 (lambda (el)
3155 (cond
3156 ((stringp el))
3157 ((equal el blob) (throw 'exit genealogy))
3158 ((>= (org-element-property :end el) end)
3159 (funcall walk-data el (cons el genealogy)))))
3160 (org-element-contents data)))))
3161 (catch 'exit (funcall walk-data (plist-get info :parse-tree) nil) nil)))
3163 (defun org-export-get-parent (blob info)
3164 "Return BLOB parent or nil.
3165 BLOB is the element or object considered. INFO is a plist used
3166 as a communication channel."
3167 (car (org-export-get-genealogy blob info)))
3169 (defun org-export-get-parent-headline (blob info)
3170 "Return closest parent headline or nil.
3172 BLOB is the element or object being considered. INFO is a plist
3173 used as a communication channel."
3174 (catch 'exit
3175 (mapc
3176 (lambda (el) (when (eq (org-element-type el) 'headline) (throw 'exit el)))
3177 (org-export-get-genealogy blob info))
3178 nil))
3180 (defun org-export-get-parent-paragraph (object info)
3181 "Return parent paragraph or nil.
3183 INFO is a plist used as a communication channel.
3185 Optional argument OBJECT, when provided, is the object to consider.
3186 Otherwise, return the paragraph containing current object.
3188 This is useful for objects, which share attributes with the
3189 paragraph containing them."
3190 (catch 'exit
3191 (mapc
3192 (lambda (el) (when (eq (org-element-type el) 'paragraph) (throw 'exit el)))
3193 (org-export-get-genealogy object info))
3194 nil))
3196 (defun org-export-get-previous-element (blob info)
3197 "Return previous element or object.
3199 BLOB is an element or object. INFO is a plist used as
3200 a communication channel.
3202 Return previous element or object, a string, or nil."
3203 (let ((parent (org-export-get-parent blob info)))
3204 (cadr (member blob (reverse (org-element-contents parent))))))
3206 (defun org-export-get-next-element (blob info)
3207 "Return next element or object.
3209 BLOB is an element or object. INFO is a plist used as
3210 a communication channel.
3212 Return next element or object, a string, or nil."
3213 (let ((parent (org-export-get-parent blob info)))
3214 (cadr (member blob (org-element-contents parent)))))
3218 ;;; The Dispatcher
3220 ;; `org-export-dispatch' is the standard interactive way to start an
3221 ;; export process. It uses `org-export-dispatch-ui' as a subroutine
3222 ;; for its interface. Most commons back-ends should have an entry in
3223 ;; it.
3225 (defun org-export-dispatch ()
3226 "Export dispatcher for Org mode.
3228 It provides an access to common export related tasks in a buffer.
3229 Its interface comes in two flavours: standard and expert. While
3230 both share the same set of bindings, only the former displays the
3231 valid keys associations. Set `org-export-dispatch-use-expert-ui'
3232 to switch to one or the other.
3234 Return an error if key pressed has no associated command."
3235 (interactive)
3236 (let* ((input (org-export-dispatch-ui
3237 (if (listp org-export-initial-scope) org-export-initial-scope
3238 (list org-export-initial-scope))
3239 org-export-dispatch-use-expert-ui))
3240 (raw-key (car input))
3241 (optns (cdr input)))
3242 ;; Translate "C-a", "C-b"... into "a", "b"... Then take action
3243 ;; depending on user's key pressed.
3244 (case (if (< raw-key 27) (+ raw-key 96) raw-key)
3245 ;; Allow to quit with "q" key.
3246 (?q nil)
3247 ;; Export with `e-ascii' back-end.
3248 ((?A ?N ?U)
3249 (let ((outbuf
3250 (org-export-to-buffer
3251 'e-ascii "*Org E-ASCII Export*"
3252 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)
3253 `(:ascii-charset
3254 ,(case raw-key (?A 'ascii) (?N 'latin1) (t 'utf-8))))))
3255 (with-current-buffer outbuf (text-mode))
3256 (when org-export-show-temporary-export-buffer
3257 (switch-to-buffer-other-window outbuf))))
3258 ((?a ?n ?u)
3259 (org-e-ascii-export-to-ascii
3260 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)
3261 `(:ascii-charset ,(case raw-key (?a 'ascii) (?n 'latin1) (t 'utf-8)))))
3262 ;; Export with `e-latex' back-end.
3264 (let ((outbuf
3265 (org-export-to-buffer
3266 'e-latex "*Org E-LaTeX Export*"
3267 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))))
3268 (with-current-buffer outbuf (latex-mode))
3269 (when org-export-show-temporary-export-buffer
3270 (switch-to-buffer-other-window outbuf))))
3271 (?l (org-e-latex-export-to-latex
3272 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
3273 (?p (org-e-latex-export-to-pdf
3274 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
3275 (?d (org-open-file
3276 (org-e-latex-export-to-pdf
3277 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))))
3278 ;; Export with `e-html' back-end.
3280 (let ((outbuf
3281 (org-export-to-buffer
3282 'e-html "*Org E-HTML Export*"
3283 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))))
3284 ;; set major mode
3285 (with-current-buffer outbuf
3286 (if (featurep 'nxhtml-mode) (nxhtml-mode) (nxml-mode)))
3287 (when org-export-show-temporary-export-buffer
3288 (switch-to-buffer-other-window outbuf))))
3289 (?h (org-e-html-export-to-html
3290 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
3291 (?b (org-open-file
3292 (org-e-html-export-to-html
3293 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))))
3294 ;; Export with `e-odt' back-end.
3295 (?o (org-e-odt-export-to-odt
3296 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
3297 (?O (org-open-file
3298 (org-e-odt-export-to-odt
3299 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))))
3300 ;; Publishing facilities
3301 (?F (org-e-publish-current-file (memq 'force optns)))
3302 (?P (org-e-publish-current-project (memq 'force optns)))
3303 (?X (let ((project
3304 (assoc (org-icompleting-read
3305 "Publish project: " org-e-publish-project-alist nil t)
3306 org-e-publish-project-alist)))
3307 (org-e-publish project (memq 'force optns))))
3308 (?E (org-e-publish-all (memq 'force optns)))
3309 ;; Undefined command.
3310 (t (error "No command associated with key %s"
3311 (char-to-string raw-key))))))
3313 (defun org-export-dispatch-ui (options expertp)
3314 "Handle interface for `org-export-dispatch'.
3316 OPTIONS is a list containing current interactive options set for
3317 export. It can contain any of the following symbols:
3318 `body' toggles a body-only export
3319 `subtree' restricts export to current subtree
3320 `visible' restricts export to visible part of buffer.
3321 `force' force publishing files.
3323 EXPERTP, when non-nil, triggers expert UI. In that case, no help
3324 buffer is provided, but indications about currently active
3325 options are given in the prompt. Moreover, \[?] allows to switch
3326 back to standard interface.
3328 Return value is a list with key pressed as CAR and a list of
3329 final interactive export options as CDR."
3330 (let ((help
3331 (format "---- (Options) -------------------------------------------
3333 \[1] Body only: %s [2] Export scope: %s
3334 \[3] Visible only: %s [4] Force publishing: %s
3337 ----(ASCII/Latin-1/UTF-8 Export)--------------------------
3339 \[a/n/u] to TXT file [A/N/U] to temporary buffer
3341 ----(HTML Export)-----------------------------------------
3343 \[h] to HTML file [b] ... and open it
3344 \[H] to temporary buffer
3346 ----(LaTeX Export)----------------------------------------
3348 \[l] to TEX file [L] to temporary buffer
3349 \[p] to PDF file [d] ... and open it
3351 ----(ODF Export)------------------------------------------
3353 \[o] to ODT file [O] ... and open it
3355 ----(Publish)---------------------------------------------
3357 \[F] current file [P] current project
3358 \[X] a project [E] every project"
3359 (if (memq 'body options) "On " "Off")
3360 (if (memq 'subtree options) "Subtree" "Buffer ")
3361 (if (memq 'visible options) "On " "Off")
3362 (if (memq 'force options) "On " "Off")))
3363 (standard-prompt "Export command: ")
3364 (expert-prompt (format "Export command (%s%s%s%s): "
3365 (if (memq 'body options) "b" "-")
3366 (if (memq 'subtree options) "s" "-")
3367 (if (memq 'visible options) "v" "-")
3368 (if (memq 'force options) "f" "-")))
3369 (handle-keypress
3370 (function
3371 ;; Read a character from command input, toggling interactive
3372 ;; options when applicable. PROMPT is the displayed prompt,
3373 ;; as a string.
3374 (lambda (prompt)
3375 (let ((key (read-char-exclusive prompt)))
3376 (cond
3377 ;; Ignore non-standard characters (i.e. "M-a").
3378 ((not (characterp key)) (org-export-dispatch-ui options expertp))
3379 ;; Help key: Switch back to standard interface if
3380 ;; expert UI was active.
3381 ((eq key ??) (org-export-dispatch-ui options nil))
3382 ;; Toggle export options.
3383 ((memq key '(?1 ?2 ?3 ?4))
3384 (org-export-dispatch-ui
3385 (let ((option (case key (?1 'body) (?2 'subtree) (?3 'visible)
3386 (?4 'force))))
3387 (if (memq option options) (remq option options)
3388 (cons option options)))
3389 expertp))
3390 ;; Action selected: Send key and options back to
3391 ;; `org-export-dispatch'.
3392 (t (cons key options))))))))
3393 ;; With expert UI, just read key with a fancy prompt. In standard
3394 ;; UI, display an intrusive help buffer.
3395 (if expertp (funcall handle-keypress expert-prompt)
3396 (save-window-excursion
3397 (delete-other-windows)
3398 (with-output-to-temp-buffer "*Org Export/Publishing Help*" (princ help))
3399 (org-fit-window-to-buffer
3400 (get-buffer-window "*Org Export/Publishing Help*"))
3401 (funcall handle-keypress standard-prompt)))))
3404 (provide 'org-export)
3405 ;;; org-export.el ends here