org-export: Improve `:genealogy' property in communication channel
[org-mode/org-mode-NeilSmithlineMods.git] / contrib / lisp / org-export.el
blobc635a2fd44eae87e1d797fc3831989c61627029e
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 export options or
32 ;; contextual data, all in a single place. The exhaustive list of
33 ;; 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)
97 ;;; Internal Variables
99 ;; Among internal variables, the most important is
100 ;; `org-export-option-alist'. This variable define the global export
101 ;; options, shared between every exporter, and how they are acquired.
103 (defconst org-export-max-depth 19
104 "Maximum nesting depth for headlines, counting from 0.")
106 (defconst org-export-option-alist
107 '((:author "AUTHOR" nil user-full-name t)
108 (:creator "CREATOR" nil org-export-creator-string)
109 (:date "DATE" nil nil t)
110 (:description "DESCRIPTION" nil nil newline)
111 (:email "EMAIL" nil user-mail-address t)
112 (:exclude-tags "EXPORT_EXCLUDE_TAGS" nil org-export-exclude-tags split)
113 (:headline-levels nil "H" org-export-headline-levels)
114 (:keywords "KEYWORDS" nil nil space)
115 (:language "LANGUAGE" nil org-export-default-language t)
116 (:preserve-breaks nil "\\n" org-export-preserve-breaks)
117 (:section-numbers nil "num" org-export-with-section-numbers)
118 (:select-tags "EXPORT_SELECT_TAGS" nil org-export-select-tags split)
119 (:time-stamp-file nil "timestamp" org-export-time-stamp-file)
120 (:title "TITLE" nil nil space)
121 (:with-archived-trees nil "arch" org-export-with-archived-trees)
122 (:with-author nil "author" org-export-with-author)
123 (:with-creator nil "creator" org-export-with-creator)
124 (:with-drawers nil "drawer" org-export-with-drawers)
125 (:with-email nil "email" org-export-with-email)
126 (:with-emphasize nil "*" org-export-with-emphasize)
127 (:with-entities nil "e" org-export-with-entities)
128 (:with-fixed-width nil ":" org-export-with-fixed-width)
129 (:with-footnotes nil "f" org-export-with-footnotes)
130 (:with-priority nil "pri" org-export-with-priority)
131 (:with-special-strings nil "-" org-export-with-special-strings)
132 (:with-sub-superscript nil "^" org-export-with-sub-superscripts)
133 (:with-toc nil "toc" org-export-with-toc)
134 (:with-tables nil "|" org-export-with-tables)
135 (:with-tags nil "tags" org-export-with-tags)
136 (:with-tasks nil "tasks" org-export-with-tasks)
137 (:with-timestamps nil "<" org-export-with-timestamps)
138 (:with-todo-keywords nil "todo" org-export-with-todo-keywords))
139 "Alist between export properties and ways to set them.
141 The car of the alist is the property name, and the cdr is a list
142 like \(KEYWORD OPTION DEFAULT BEHAVIOUR\) where:
144 KEYWORD is a string representing a buffer keyword, or nil.
145 OPTION is a string that could be found in an #+OPTIONS: line.
146 DEFAULT is the default value for the property.
147 BEHAVIOUR determine how Org should handle multiple keywords for
148 the same property. It is a symbol among:
149 nil Keep old value and discard the new one.
150 t Replace old value with the new one.
151 `space' Concatenate the values, separating them with a space.
152 `newline' Concatenate the values, separating them with
153 a newline.
154 `split' Split values at white spaces, and cons them to the
155 previous list.
157 KEYWORD and OPTION have precedence over DEFAULT.
159 All these properties should be back-end agnostic. For back-end
160 specific properties, define a similar variable named
161 `org-BACKEND-option-alist', replacing BACKEND with the name of
162 the appropriate back-end. You can also redefine properties
163 there, as they have precedence over these.")
165 (defconst org-export-special-keywords
166 '("SETUP_FILE" "OPTIONS" "MACRO")
167 "List of in-buffer keywords that require special treatment.
168 These keywords are not directly associated to a property. The
169 way they are handled must be hard-coded into
170 `org-export-get-inbuffer-options' function.")
174 ;;; User-configurable Variables
176 ;; Configuration for the masses.
178 ;; They should never be evaled directly, as their value is to be
179 ;; stored in a property list (cf. `org-export-option-alist').
181 (defgroup org-export nil
182 "Options for exporting Org mode files."
183 :tag "Org Export"
184 :group 'org)
186 (defgroup org-export-general nil
187 "General options for export engine."
188 :tag "Org Export General"
189 :group 'org-export)
191 (defcustom org-export-with-archived-trees 'headline
192 "Whether sub-trees with the ARCHIVE tag should be exported.
194 This can have three different values:
195 nil Do not export, pretend this tree is not present.
196 t Do export the entire tree.
197 `headline' Only export the headline, but skip the tree below it.
199 This option can also be set with the #+OPTIONS line,
200 e.g. \"arch:nil\"."
201 :group 'org-export-general
202 :type '(choice
203 (const :tag "Not at all" nil)
204 (const :tag "Headline only" 'headline)
205 (const :tag "Entirely" t)))
207 (defcustom org-export-with-author t
208 "Non-nil means insert author name into the exported file.
209 This option can also be set with the #+OPTIONS line,
210 e.g. \"author:nil\"."
211 :group 'org-export-general
212 :type 'boolean)
214 (defcustom org-export-with-creator 'comment
215 "Non-nil means the postamble should contain a creator sentence.
217 The sentence can be set in `org-export-creator-string' and
218 defaults to \"Generated by Org mode XX in Emacs XXX.\".
220 If the value is `comment' insert it as a comment."
221 :group 'org-export-general
222 :type '(choice
223 (const :tag "No creator sentence" nil)
224 (const :tag "Sentence as a comment" 'comment)
225 (const :tag "Insert the sentence" t)))
227 (defcustom org-export-creator-string
228 (format "Generated by Org mode %s in Emacs %s." org-version emacs-version)
229 "String to insert at the end of the generated document."
230 :group 'org-export-general
231 :type '(string :tag "Creator string"))
233 (defcustom org-export-with-drawers nil
234 "Non-nil means export with drawers like the property drawer.
235 When t, all drawers are exported. This may also be a list of
236 drawer names to export."
237 :group 'org-export-general
238 :type '(choice
239 (const :tag "All drawers" t)
240 (const :tag "None" nil)
241 (repeat :tag "Selected drawers"
242 (string :tag "Drawer name"))))
244 (defcustom org-export-with-email nil
245 "Non-nil means insert author email into the exported file.
246 This option can also be set with the #+OPTIONS line,
247 e.g. \"email:t\"."
248 :group 'org-export-general
249 :type 'boolean)
251 (defcustom org-export-with-emphasize t
252 "Non-nil means interpret *word*, /word/, and _word_ as emphasized text.
254 If the export target supports emphasizing text, the word will be
255 typeset in bold, italic, or underlined, respectively. Not all
256 export backends support this.
258 This option can also be set with the #+OPTIONS line, e.g. \"*:nil\"."
259 :group 'org-export-general
260 :type 'boolean)
262 (defcustom org-export-exclude-tags '("noexport")
263 "Tags that exclude a tree from export.
264 All trees carrying any of these tags will be excluded from
265 export. This is without condition, so even subtrees inside that
266 carry one of the `org-export-select-tags' will be removed."
267 :group 'org-export-general
268 :type '(repeat (string :tag "Tag")))
270 (defcustom org-export-with-fixed-width t
271 "Non-nil means lines starting with \":\" will be in fixed width font.
273 This can be used to have pre-formatted text, fragments of code
274 etc. For example:
275 : ;; Some Lisp examples
276 : (while (defc cnt)
277 : (ding))
278 will be looking just like this in also HTML. See also the QUOTE
279 keyword. Not all export backends support this.
281 This option can also be set with the #+OPTIONS line, e.g. \"::nil\"."
282 :group 'org-export-translation
283 :type 'boolean)
285 (defcustom org-export-with-footnotes t
286 "Non-nil means Org footnotes should be exported.
287 This option can also be set with the #+OPTIONS line,
288 e.g. \"f:nil\"."
289 :group 'org-export-general
290 :type 'boolean)
292 (defcustom org-export-headline-levels 3
293 "The last level which is still exported as a headline.
295 Inferior levels will produce itemize lists when exported. Note
296 that a numeric prefix argument to an exporter function overrides
297 this setting.
299 This option can also be set with the #+OPTIONS line, e.g. \"H:2\"."
300 :group 'org-export-general
301 :type 'integer)
303 (defcustom org-export-default-language "en"
304 "The default language for export and clocktable translations, as a string.
305 This may have an association in
306 `org-clock-clocktable-language-setup'."
307 :group 'org-export-general
308 :type '(string :tag "Language"))
310 (defcustom org-export-preserve-breaks nil
311 "Non-nil means preserve all line breaks when exporting.
313 Normally, in HTML output paragraphs will be reformatted.
315 This option can also be set with the #+OPTIONS line,
316 e.g. \"\\n:t\"."
317 :group 'org-export-general
318 :type 'boolean)
320 (defcustom org-export-with-entities t
321 "Non-nil means interpret entities when exporting.
323 For example, HTML export converts \\alpha to &alpha; and \\AA to
324 &Aring;.
326 For a list of supported names, see the constant `org-entities'
327 and the user option `org-entities-user'.
329 This option can also be set with the #+OPTIONS line,
330 e.g. \"e:nil\"."
331 :group 'org-export-general
332 :type 'boolean)
334 (defcustom org-export-with-priority nil
335 "Non-nil means include priority cookies in export.
336 When nil, remove priority cookies for export."
337 :group 'org-export-general
338 :type 'boolean)
340 (defcustom org-export-with-section-numbers t
341 "Non-nil means add section numbers to headlines when exporting.
343 This option can also be set with the #+OPTIONS line,
344 e.g. \"num:t\"."
345 :group 'org-export-general
346 :type 'boolean)
348 (defcustom org-export-select-tags '("export")
349 "Tags that select a tree for export.
350 If any such tag is found in a buffer, all trees that do not carry
351 one of these tags will be deleted before export. Inside trees
352 that are selected like this, you can still deselect a subtree by
353 tagging it with one of the `org-export-exclude-tags'."
354 :group 'org-export-general
355 :type '(repeat (string :tag "Tag")))
357 (defcustom org-export-with-special-strings t
358 "Non-nil means interpret \"\-\", \"--\" and \"---\" for export.
360 When this option is turned on, these strings will be exported as:
362 Org HTML LaTeX
363 -----+----------+--------
364 \\- &shy; \\-
365 -- &ndash; --
366 --- &mdash; ---
367 ... &hellip; \ldots
369 This option can also be set with the #+OPTIONS line,
370 e.g. \"-:nil\"."
371 :group 'org-export-general
372 :type 'boolean)
374 (defcustom org-export-with-sub-superscripts t
375 "Non-nil means interpret \"_\" and \"^\" for export.
377 When this option is turned on, you can use TeX-like syntax for
378 sub- and superscripts. Several characters after \"_\" or \"^\"
379 will be considered as a single item - so grouping with {} is
380 normally not needed. For example, the following things will be
381 parsed as single sub- or superscripts.
383 10^24 or 10^tau several digits will be considered 1 item.
384 10^-12 or 10^-tau a leading sign with digits or a word
385 x^2-y^3 will be read as x^2 - y^3, because items are
386 terminated by almost any nonword/nondigit char.
387 x_{i^2} or x^(2-i) braces or parenthesis do grouping.
389 Still, ambiguity is possible - so when in doubt use {} to enclose
390 the sub/superscript. If you set this variable to the symbol
391 `{}', the braces are *required* in order to trigger
392 interpretations as sub/superscript. This can be helpful in
393 documents that need \"_\" frequently in plain text.
395 This option can also be set with the #+OPTIONS line,
396 e.g. \"^:nil\"."
397 :group 'org-export-general
398 :type '(choice
399 (const :tag "Interpret them" t)
400 (const :tag "Curly brackets only" {})
401 (const :tag "Do not interpret them" nil)))
403 (defcustom org-export-with-toc t
404 "Non-nil means create a table of contents in exported files.
406 The TOC contains headlines with levels up
407 to`org-export-headline-levels'. When an integer, include levels
408 up to N in the toc, this may then be different from
409 `org-export-headline-levels', but it will not be allowed to be
410 larger than the number of headline levels. When nil, no table of
411 contents is made.
413 This option can also be set with the #+OPTIONS line,
414 e.g. \"toc:nil\" or \"toc:3\"."
415 :group 'org-export-general
416 :type '(choice
417 (const :tag "No Table of Contents" nil)
418 (const :tag "Full Table of Contents" t)
419 (integer :tag "TOC to level")))
421 (defcustom org-export-with-tables t
422 "If non-nil, lines starting with \"|\" define a table.
423 For example:
425 | Name | Address | Birthday |
426 |-------------+----------+-----------|
427 | Arthur Dent | England | 29.2.2100 |
429 This option can also be set with the #+OPTIONS line, e.g. \"|:nil\"."
430 :group 'org-export-general
431 :type 'boolean)
433 (defcustom org-export-with-tags t
434 "If nil, do not export tags, just remove them from headlines.
436 If this is the symbol `not-in-toc', tags will be removed from
437 table of contents entries, but still be shown in the headlines of
438 the document.
440 This option can also be set with the #+OPTIONS line,
441 e.g. \"tags:nil\"."
442 :group 'org-export-general
443 :type '(choice
444 (const :tag "Off" nil)
445 (const :tag "Not in TOC" not-in-toc)
446 (const :tag "On" t)))
448 (defcustom org-export-with-tasks t
449 "Non-nil means include TODO items for export.
450 This may have the following values:
451 t include tasks independent of state.
452 todo include only tasks that are not yet done.
453 done include only tasks that are already done.
454 nil remove all tasks before export
455 list of keywords keep only tasks with these keywords"
456 :group 'org-export-general
457 :type '(choice
458 (const :tag "All tasks" t)
459 (const :tag "No tasks" nil)
460 (const :tag "Not-done tasks" todo)
461 (const :tag "Only done tasks" done)
462 (repeat :tag "Specific TODO keywords"
463 (string :tag "Keyword"))))
465 (defcustom org-export-time-stamp-file t
466 "Non-nil means insert a time stamp into the exported file.
467 The time stamp shows when the file was created.
469 This option can also be set with the #+OPTIONS line,
470 e.g. \"timestamp:nil\"."
471 :group 'org-export-general
472 :type 'boolean)
474 (defcustom org-export-with-timestamps t
475 "If nil, do not export time stamps and associated keywords."
476 :group 'org-export-general
477 :type 'boolean)
479 (defcustom org-export-with-todo-keywords t
480 "Non-nil means include TODO keywords in export.
481 When nil, remove all these keywords from the export.")
483 (defcustom org-export-allow-BIND 'confirm
484 "Non-nil means allow #+BIND to define local variable values for export.
485 This is a potential security risk, which is why the user must
486 confirm the use of these lines."
487 :group 'org-export-general
488 :type '(choice
489 (const :tag "Never" nil)
490 (const :tag "Always" t)
491 (const :tag "Ask a confirmation for each file" confirm)))
493 (defcustom org-export-snippet-translation-alist nil
494 "Alist between export snippets back-ends and exporter back-ends.
496 This variable allows to provide shortcuts for export snippets.
498 For example, with a value of '\(\(\"h\" . \"html\"\)\), the HTML
499 back-end will recognize the contents of \"@h{<b>}\" as HTML code
500 while every other back-end will ignore it."
501 :group 'org-export-general
502 :type '(repeat
503 (cons
504 (string :tag "Shortcut")
505 (string :tag "Back-end"))))
507 (defcustom org-export-coding-system nil
508 "Coding system for the exported file."
509 :group 'org-export-general
510 :type 'coding-system)
512 (defcustom org-export-copy-to-kill-ring t
513 "Non-nil means exported stuff will also be pushed onto the kill ring."
514 :group 'org-export-general
515 :type 'boolean)
517 (defcustom org-export-initial-scope 'buffer
518 "The initial scope when exporting with `org-export-dispatch'.
519 This variable can be either set to `buffer' or `subtree'."
520 :group 'org-export-general
521 :type '(choice
522 (const :tag "Export current buffer" 'buffer)
523 (const :tag "Export current subtree" 'subtree)))
525 (defcustom org-export-show-temporary-export-buffer t
526 "Non-nil means show buffer after exporting to temp buffer.
527 When Org exports to a file, the buffer visiting that file is ever
528 shown, but remains buried. However, when exporting to a temporary
529 buffer, that buffer is popped up in a second window. When this variable
530 is nil, the buffer remains buried also in these cases."
531 :group 'org-export-general
532 :type 'boolean)
534 (defcustom org-export-dispatch-use-expert-ui nil
535 "Non-nil means using a non-intrusive `org-export-dispatch'.
536 In that case, no help buffer is displayed. Though, an indicator
537 for current export scope is added to the prompt \(i.e. \"b\" when
538 output is restricted to body only, \"s\" when it is restricted to
539 the current subtree and \"v\" when only visible elements are
540 considered for export\). Also, \[?] allows to switch back to
541 standard mode."
542 :group 'org-export-general
543 :type 'boolean)
547 ;;; The Communication Channel
549 ;; During export process, every function has access to a number of
550 ;; properties. They are of three types:
552 ;; 1. Export options are collected once at the very beginning of the
553 ;; process, out of the original buffer and environment. The task
554 ;; is handled by `org-export-collect-options' function.
556 ;; All export options are defined through the
557 ;; `org-export-option-alist' variable.
559 ;; 2. Persistent properties are stored in
560 ;; `org-export-persistent-properties' and available at every level
561 ;; of recursion. Their value is extracted directly from the parsed
562 ;; tree, and depends on export options (whole trees may be filtered
563 ;; out of the export process).
565 ;; Properties belonging to that type are defined in the
566 ;; `org-export-persistent-properties-list' variable.
568 ;; 3. Every other property is considered local, and available at
569 ;; a precise level of recursion and below.
571 ;; Managing properties during transcode process is mainly done with
572 ;; `org-export-update-info'. Even though they come from different
573 ;; sources, the function transparently concatenates them in a single
574 ;; property list passed as an argument to each transcode function.
575 ;; Thus, during export, all necessary information is available through
576 ;; that single property list, and the element or object itself.
577 ;; Though, modifying a property will still require some special care,
578 ;; and should be done with `org-export-set-property' instead of plain
579 ;; `plist-put'.
581 ;; Here is the full list of properties available during transcode
582 ;; process, with their category (option, persistent or local), their
583 ;; value type and the function updating them, when appropriate.
585 ;; + `author' :: Author's name.
586 ;; - category :: option
587 ;; - type :: string
589 ;; + `back-end' :: Current back-end used for transcoding.
590 ;; - category :: persistent
591 ;; - type :: symbol
593 ;; + `code-refs' :: Association list between reference name and real
594 ;; labels in source code. It is used to properly
595 ;; resolve links inside source blocks.
596 ;; - category :: persistent
597 ;; - type :: alist (INT-OR-STRING . STRING)
598 ;; - update :: `org-export-handle-code'
600 ;; + `creator' :: String to write as creation information.
601 ;; - category :: option
602 ;; - type :: string
604 ;; + `date' :: String to use as date.
605 ;; - category :: option
606 ;; - type :: string
608 ;; + `description' :: Description text for the current data.
609 ;; - category :: option
610 ;; - type :: string
612 ;; + `email' :: Author's email.
613 ;; - category :: option
614 ;; - type :: string
616 ;; + `exclude-tags' :: Tags for exclusion of subtrees from export
617 ;; process.
618 ;; - category :: option
619 ;; - type :: list of strings
621 ;; + `footnote-definition-alist' :: Alist between footnote labels and
622 ;; their definition, as parsed data. Only non-inlined footnotes
623 ;; are represented in this alist. Also, every definition isn't
624 ;; guaranteed to be referenced in the parse tree. The purpose of
625 ;; this property is to preserve definitions from oblivion
626 ;; (i.e. when the parse tree comes from a part of the original
627 ;; buffer), it isn't meant for direct use in a back-end. To
628 ;; retrieve a definition relative to a reference, use
629 ;; `org-export-get-footnote-definition' instead.
630 ;; - category :: option
631 ;; - type :: alist (STRING . LIST)
633 ;; + `footnote-seen-labels' :: List of already transcoded footnote
634 ;; labels. It is used to know when a reference appears for the
635 ;; first time. (cf. `org-export-footnote-first-reference-p').
636 ;; - category :: persistent
637 ;; - type :: list of strings
638 ;; - update :: `org-export-update-info'
640 ;; + `genealogy' :: Flat list of current object or element's parents
641 ;; from closest to farthest.
642 ;; - category :: local
643 ;; - type :: list of elements and objects
644 ;; - update :: `org-export-update-info'
646 ;; + `headline-alist' :: Alist between headlines raw name and their
647 ;; boundaries. It is used to resolve "fuzzy" links
648 ;; (cf. `org-export-resolve-fuzzy-link').
649 ;; - category :: persistent
650 ;; - type :: alist (STRING INTEGER INTEGER)
652 ;; + `headline-levels' :: Maximum level being exported as an
653 ;; headline. Comparison is done with the relative level of
654 ;; headlines in the parse tree, not necessarily with their
655 ;; actual level.
656 ;; - category :: option
657 ;; - type :: integer
659 ;; + `headline-offset' :: Difference between relative and real level
660 ;; of headlines in the parse tree. For example, a value of -1
661 ;; means a level 2 headline should be considered as level
662 ;; 1 (cf. `org-export-get-relative-level').
663 ;; - category :: persistent
664 ;; - type :: integer
666 ;; + `headline-numbering' :: Alist between headlines' beginning
667 ;; position and their numbering, as a list of numbers
668 ;; (cf. `org-export-get-headline-number').
669 ;; - category :: persistent
670 ;; - type :: alist (INTEGER . LIST)
672 ;; + `included-files' :: List of files, with full path, included in
673 ;; the current buffer, through the "#+include:" keyword. It is
674 ;; mainly used to verify that no infinite recursive inclusion
675 ;; happens.
676 ;; - category :: local
677 ;; - type :: list of strings
679 ;; + `keywords' :: List of keywords attached to data.
680 ;; - category :: option
681 ;; - type :: string
683 ;; + `language' :: Default language used for translations.
684 ;; - category :: option
685 ;; - type :: string
687 ;; + `parse-tree' :: Whole parse tree, available at any time during
688 ;; transcoding.
689 ;; - category :: global
690 ;; - type :: list (as returned by `org-element-parse-buffer')
692 ;; + `point-max' :: Last ending position in the parse tree.
693 ;; - category :: global
694 ;; - type :: integer
696 ;; + `preserve-breaks' :: Non-nil means transcoding should preserve
697 ;; all line breaks.
698 ;; - category :: option
699 ;; - type :: symbol (nil, t)
701 ;; + `previous-element' :: Previous element's type at the same
702 ;; level.
703 ;; - category :: local
704 ;; - type :: symbol
705 ;; - update :: `org-export-update-info'
707 ;; + `previous-object' :: Previous object type (or `plain-text') at
708 ;; the same level.
709 ;; - category :: local
710 ;; - type :: symbol
711 ;; - update :: `org-export-update-info'
713 ;; + `section-numbers' :: Non-nil means transcoding should add
714 ;; section numbers to headlines.
715 ;; - category :: option
716 ;; - type :: symbol (nil, t)
718 ;; + `select-tags' :: List of tags enforcing inclusion of sub-trees in
719 ;; transcoding. When such a tag is present,
720 ;; subtrees without it are de facto excluded from
721 ;; the process. See `use-select-tags'.
722 ;; - category :: option
723 ;; - type :: list of strings
725 ;; + `target-list' :: List of targets raw names encoutered in the
726 ;; parse tree. This is used to partly resolve
727 ;; "fuzzy" links
728 ;; (cf. `org-export-resolve-fuzzy-link').
729 ;; - category :: persistent
730 ;; - type :: list of strings
732 ;; + `time-stamp-file' :: Non-nil means transcoding should insert
733 ;; a time stamp in the output.
734 ;; - category :: option
735 ;; - type :: symbol (nil, t)
737 ;; + `total-loc' :: Contains total lines of code accumulated by source
738 ;; blocks with the "+n" option so far.
739 ;; - category :: persistent
740 ;; - type :: integer
741 ;; - update :: `org-export-handle-code'
743 ;; + `use-select-tags' :: When non-nil, a select tags has been found
744 ;; in the parse tree. Thus, any headline without one will be
745 ;; filtered out. See `select-tags'.
746 ;; - category :: persistent
747 ;; - type :: interger or nil
749 ;; + `with-archived-trees' :: Non-nil when archived subtrees should
750 ;; also be transcoded. If it is set to the `headline' symbol,
751 ;; only the archived headline's name is retained.
752 ;; - category :: option
753 ;; - type :: symbol (nil, t, `headline')
755 ;; + `with-author' :: Non-nil means author's name should be included
756 ;; in the output.
757 ;; - category :: option
758 ;; - type :: symbol (nil, t)
760 ;; + `with-creator' :: Non-nild means a creation sentence should be
761 ;; inserted at the end of the transcoded string. If the value
762 ;; is `comment', it should be commented.
763 ;; - category :: option
764 ;; - type :: symbol (`comment', nil, t)
766 ;; + `with-drawers' :: Non-nil means drawers should be exported. If
767 ;; its value is a list of names, only drawers with such names
768 ;; will be transcoded.
769 ;; - category :: option
770 ;; - type :: symbol (nil, t) or list of strings
772 ;; + `with-email' :: Non-nil means output should contain author's
773 ;; email.
774 ;; - category :: option
775 ;; - type :: symbol (nil, t)
777 ;; + `with-emphasize' :: Non-nil means emphasized text should be
778 ;; interpreted.
779 ;; - category :: option
780 ;; - type :: symbol (nil, t)
782 ;; + `with-fixed-width' :: Non-nil if transcoder should interpret
783 ;; strings starting with a colon as a fixed-with (verbatim)
784 ;; area.
785 ;; - category :: option
786 ;; - type :: symbol (nil, t)
788 ;; + `with-footnotes' :: Non-nil if transcoder should interpret
789 ;; footnotes.
790 ;; - category :: option
791 ;; - type :: symbol (nil, t)
793 ;; + `with-priority' :: Non-nil means transcoding should include
794 ;; priority cookies.
795 ;; - category :: option
796 ;; - type :: symbol (nil, t)
798 ;; + `with-special-strings' :: Non-nil means transcoding should
799 ;; interpret special strings in plain text.
800 ;; - category :: option
801 ;; - type :: symbol (nil, t)
803 ;; + `with-sub-superscript' :: Non-nil means transcoding should
804 ;; interpret subscript and superscript. With a value of "{}",
805 ;; only interpret those using curly brackets.
806 ;; - category :: option
807 ;; - type :: symbol (nil, {}, t)
809 ;; + `with-tables' :: Non-nil means transcoding should interpret
810 ;; tables.
811 ;; - category :: option
812 ;; - type :: symbol (nil, t)
814 ;; + `with-tags' :: Non-nil means transcoding should keep tags in
815 ;; headlines. A `not-in-toc' value will remove them
816 ;; from the table of contents, if any, nonetheless.
817 ;; - category :: option
818 ;; - type :: symbol (nil, t, `not-in-toc')
820 ;; + `with-tasks' :: Non-nil means transcoding should include
821 ;; headlines with a TODO keyword. A `todo' value
822 ;; will only include headlines with a todo type
823 ;; keyword while a `done' value will do the
824 ;; contrary. If a list of strings is provided, only
825 ;; tasks with keywords belonging to that list will
826 ;; be kept.
827 ;; - category :: option
828 ;; - type :: symbol (t, todo, done, nil) or list of strings
830 ;; + `with-timestamps' :: Non-nil means transcoding should include
831 ;; time stamps and associated keywords. Otherwise, completely
832 ;; remove them.
833 ;; - category :: option
834 ;; - type :: symbol: (t, nil)
836 ;; + `with-toc' :: Non-nil means that a table of contents has to be
837 ;; added to the output. An integer value limits its
838 ;; depth.
839 ;; - category :: option
840 ;; - type :: symbol (nil, t or integer)
842 ;; + `with-todo-keywords' :: Non-nil means transcoding should
843 ;; include TODO keywords.
844 ;; - category :: option
845 ;; - type :: symbol (nil, t)
847 ;;;; Export Options
849 ;; Export options come from five sources, in increasing precedence
850 ;; order:
852 ;; - Global variables,
853 ;; - External options provided at export time,
854 ;; - Options keyword symbols,
855 ;; - Buffer keywords,
856 ;; - Subtree properties.
858 ;; The central internal function with regards to export options is
859 ;; `org-export-collect-options'. It updates global variables with
860 ;; "#+BIND:" keywords, then retrieve and prioritize properties from
861 ;; the different sources.
863 ;; The internal functions doing the retrieval are:
864 ;; `org-export-parse-option-keyword' ,
865 ;; `org-export-get-subtree-options' ,
866 ;; `org-export-get-inbuffer-options' and
867 ;; `org-export-get-global-options'.
869 ;; Some properties do not rely on the previous sources but still
870 ;; depend on the original buffer are taken care of in
871 ;; `org-export-initial-options'.
873 ;; Also, `org-export-confirm-letbind' and `org-export-install-letbind'
874 ;; take care of the part relative to "#+BIND:" keywords.
876 (defun org-export-collect-options (backend subtreep ext-plist)
877 "Collect export options from the current buffer.
879 BACKEND is a symbol specifying the back-end to use.
881 When SUBTREEP is non-nil, assume the export is done against the
882 current sub-tree.
884 EXT-PLIST is a property list with external parameters overriding
885 org-mode's default settings, but still inferior to file-local
886 settings."
887 ;; First install #+BIND variables.
888 (org-export-install-letbind-maybe)
889 ;; Get and prioritize export options...
890 (let ((options (org-combine-plists
891 ;; ... from global variables...
892 (org-export-get-global-options backend)
893 ;; ... from an external property list...
894 ext-plist
895 ;; ... from in-buffer settings...
896 (org-export-get-inbuffer-options
897 (org-with-wide-buffer (buffer-string)) backend
898 (and buffer-file-name
899 (org-remove-double-quotes buffer-file-name)))
900 ;; ... and from subtree, when appropriate.
901 (and subtreep
902 (org-export-get-subtree-options)))))
903 ;; Add initial options.
904 (setq options (append (org-export-initial-options options)
905 options))
906 ;; Set a default title if none has been specified so far.
907 (unless (plist-get options :title)
908 (setq options (plist-put options :title
909 (or (and buffer-file-name
910 (file-name-sans-extension
911 (file-name-nondirectory
912 buffer-file-name)))
913 (buffer-name)))))
914 ;; Return plist.
915 options))
917 (defun org-export-parse-option-keyword (options backend)
918 "Parse an OPTIONS line and return values as a plist.
919 BACKEND is a symbol specifying the back-end to use."
920 (let* ((all (append org-export-option-alist
921 (let ((var (intern
922 (format "org-%s-option-alist" backend))))
923 (and (boundp var) (eval var)))))
924 ;; Build an alist between #+OPTION: item and property-name.
925 (alist (delq nil
926 (mapcar (lambda (e)
927 (when (nth 2 e) (cons (regexp-quote (nth 2 e))
928 (car e))))
929 all)))
930 plist)
931 (mapc (lambda (e)
932 (when (string-match (concat "\\(\\`\\|[ \t]\\)"
933 (car e)
934 ":\\(([^)\n]+)\\|[^ \t\n\r;,.]*\\)")
935 options)
936 (setq plist (plist-put plist
937 (cdr e)
938 (car (read-from-string
939 (match-string 2 options)))))))
940 alist)
941 plist))
943 (defun org-export-get-subtree-options ()
944 "Get export options in subtree at point.
946 Assume point is at subtree's beginning.
948 Return options as a plist."
949 (let (prop plist)
950 (when (setq prop (progn (looking-at org-todo-line-regexp)
951 (or (save-match-data
952 (org-entry-get (point) "EXPORT_TITLE"))
953 (org-match-string-no-properties 3))))
954 (setq plist (plist-put plist :title prop)))
955 (when (setq prop (org-entry-get (point) "EXPORT_TEXT"))
956 (setq plist (plist-put plist :text prop)))
957 (when (setq prop (org-entry-get (point) "EXPORT_AUTHOR"))
958 (setq plist (plist-put plist :author prop)))
959 (when (setq prop (org-entry-get (point) "EXPORT_DATE"))
960 (setq plist (plist-put plist :date prop)))
961 (when (setq prop (org-entry-get (point) "EXPORT_OPTIONS"))
962 (setq plist (org-export-add-options-to-plist plist prop)))
963 plist))
965 (defun org-export-get-inbuffer-options (buffer-string backend files)
966 "Return in-buffer options as a plist.
967 BUFFER-STRING is the string of the buffer. BACKEND is a symbol
968 specifying which back-end should be used."
969 (let ((case-fold-search t) plist)
970 ;; 1. Special keywords, as in `org-export-special-keywords'.
971 (let ((start 0)
972 (special-re (org-make-options-regexp org-export-special-keywords)))
973 (while (string-match special-re buffer-string start)
974 (setq start (match-end 0))
975 (let ((key (upcase (org-match-string-no-properties 1 buffer-string)))
976 ;; Special keywords do not have their value expanded.
977 (val (org-match-string-no-properties 2 buffer-string)))
978 (setq plist
979 (org-combine-plists
980 (cond
981 ((string= key "SETUP_FILE")
982 (let ((file (expand-file-name
983 (org-remove-double-quotes (org-trim val)))))
984 ;; Avoid circular dependencies.
985 (unless (member file files)
986 (org-export-get-inbuffer-options
987 (org-file-contents file 'noerror)
988 backend
989 (cons file files)))))
990 ((string= key "OPTIONS")
991 (org-export-parse-option-keyword val backend))
992 ((string= key "MACRO")
993 (string-match "^\\([-a-zA-Z0-9_]+\\)[ \t]+\\(.*?[ \t]*$\\)"
994 val)
995 (plist-put nil
996 (intern (concat ":macro-"
997 (downcase (match-string 1 val))))
998 (match-string 2 val))))
999 plist)))))
1000 ;; 2. Standard options, as in `org-export-option-alist'.
1001 (let* ((all (append org-export-option-alist
1002 (let ((var (intern
1003 (format "org-%s-option-alist" backend))))
1004 (and (boundp var) (eval var)))))
1005 ;; Build alist between keyword name and property name.
1006 (alist (delq nil (mapcar (lambda (e)
1007 (when (nth 1 e) (cons (nth 1 e) (car e))))
1008 all)))
1009 ;; Build regexp matching all keywords associated to export
1010 ;; options. Note: the search is case insensitive.
1011 (opt-re (org-make-options-regexp
1012 (delq nil (mapcar (lambda (e) (nth 1 e)) all))))
1013 (start 0))
1014 (while (string-match opt-re buffer-string start)
1015 (setq start (match-end 0))
1016 (let* ((key (upcase (org-match-string-no-properties 1 buffer-string)))
1017 ;; Expand value, applying restrictions for keywords.
1018 (val (org-match-string-no-properties 2 buffer-string))
1019 (prop (cdr (assoc key alist)))
1020 (behaviour (nth 4 (assq prop all))))
1021 (setq plist
1022 (plist-put
1023 plist prop
1024 ;; Handle value depending on specified BEHAVIOUR.
1025 (case behaviour
1026 (space (if (plist-get plist prop)
1027 (concat (plist-get plist prop) " " (org-trim val))
1028 (org-trim val)))
1029 (newline (org-trim
1030 (concat
1031 (plist-get plist prop) "\n" (org-trim val))))
1032 (split `(,@(plist-get plist prop) ,@(org-split-string val)))
1033 ('t val)
1034 (otherwise (plist-get plist prop)))))))
1035 ;; Parse keywords specified in `org-element-parsed-keywords'.
1036 (mapc
1037 (lambda (key)
1038 (let* ((prop (cdr (assoc (upcase key) alist)))
1039 (value (and prop (plist-get plist prop))))
1040 (when (stringp value)
1041 (setq plist
1042 (plist-put
1043 plist prop
1044 (org-element-parse-secondary-string
1045 value
1046 (cdr (assq 'keyword org-element-string-restrictions))))))))
1047 org-element-parsed-keywords))
1048 ;; Return final value.
1049 plist))
1051 (defun org-export-get-global-options (backend)
1052 "Return global export options as a plist.
1053 BACKEND is a symbol specifying which back-end should be used."
1054 (let ((all (append org-export-option-alist
1055 (let ((var (intern
1056 (format "org-%s-option-alist" backend))))
1057 (and (boundp var) (eval var)))))
1058 ;; Output value.
1059 plist)
1060 (mapc (lambda (cell)
1061 (setq plist
1062 (plist-put plist (car cell) (eval (nth 3 cell)))))
1063 all)
1064 ;; Return value.
1065 plist))
1067 (defun org-export-initial-options (options)
1068 "Return a plist with non-optional properties.
1069 OPTIONS is the export options plist computed so far."
1070 (list
1071 ;; `:macro-date', `:macro-time' and `:macro-property' could as well
1072 ;; be initialized as persistent properties, since they don't depend
1073 ;; on initial environment. Though, it may be more logical to keep
1074 ;; them close to other ":macro-" properties.
1075 :macro-date "(eval (format-time-string \"$1\"))"
1076 :macro-time "(eval (format-time-string \"$1\"))"
1077 :macro-property "(eval (org-entry-get nil \"$1\" 'selective))"
1078 :macro-modification-time
1079 (and (buffer-file-name)
1080 (file-exists-p (buffer-file-name))
1081 (concat "(eval (format-time-string \"$1\" '"
1082 (prin1-to-string (nth 5 (file-attributes (buffer-file-name))))
1083 "))"))
1084 :macro-input-file (and (buffer-file-name)
1085 (file-name-nondirectory (buffer-file-name)))
1086 ;; Footnotes definitions must be collected in the original buffer,
1087 ;; as there's no insurance that they will still be in the parse
1088 ;; tree, due to some narrowing.
1089 :footnote-definition-alist
1090 (let (alist)
1091 (org-with-wide-buffer
1092 (goto-char (point-min))
1093 (while (re-search-forward org-footnote-definition-re nil t)
1094 (let ((def (org-footnote-at-definition-p)))
1095 (when def
1096 (org-skip-whitespace)
1097 (push (cons (car def)
1098 (save-restriction
1099 (narrow-to-region (point) (nth 2 def))
1100 (org-element-parse-buffer)))
1101 alist))))
1102 alist))))
1104 (defvar org-export-allow-BIND-local nil)
1105 (defun org-export-confirm-letbind ()
1106 "Can we use #+BIND values during export?
1107 By default this will ask for confirmation by the user, to divert
1108 possible security risks."
1109 (cond
1110 ((not org-export-allow-BIND) nil)
1111 ((eq org-export-allow-BIND t) t)
1112 ((local-variable-p 'org-export-allow-BIND-local) org-export-allow-BIND-local)
1113 (t (org-set-local 'org-export-allow-BIND-local
1114 (yes-or-no-p "Allow BIND values in this buffer? ")))))
1116 (defun org-export-install-letbind-maybe ()
1117 "Install the values from #+BIND lines as local variables.
1118 Variables must be installed before in-buffer options are
1119 retrieved."
1120 (let (letbind pair)
1121 (org-with-wide-buffer
1122 (goto-char (point-min))
1123 (while (re-search-forward (org-make-options-regexp '("BIND")) nil t)
1124 (when (org-export-confirm-letbind)
1125 (push (read (concat "(" (org-match-string-no-properties 2) ")"))
1126 letbind))))
1127 (while (setq pair (pop letbind))
1128 (org-set-local (car pair) (nth 1 pair)))))
1131 ;;;; Persistent Properties
1133 ;; Persistent properties are declared in
1134 ;; `org-export-persistent-properties-list' variable. Most of them are
1135 ;; initialized at the beginning of the transcoding process by
1136 ;; `org-export-initialize-persistent-properties'. The others are
1137 ;; updated during that process.
1139 ;; Dedicated functions focus on computing the value of specific
1140 ;; persistent properties during initialization. Thus,
1141 ;; `org-export-use-select-tag-p' determines if an headline makes use
1142 ;; of an export tag enforcing inclusion. `org-export-get-min-level'
1143 ;; gets the minimal exportable level, used as a basis to compute
1144 ;; relative level for headlines. `org-export-get-point-max' returns
1145 ;; the maximum exportable ending position in the parse tree.
1146 ;; Eventually `org-export-collect-headline-numbering' builds an alist
1147 ;; between headlines' beginning position and their numbering.
1149 (defconst org-export-persistent-properties-list
1150 '(:back-end :code-refs :headline-alist :headline-numbering :headline-offset
1151 :parse-tree :point-max :footnote-seen-labels :target-list
1152 :total-loc :use-select-tags)
1153 "List of persistent properties.")
1155 (defconst org-export-persistent-properties nil
1156 "Used internally to store properties and values during transcoding.
1158 Only properties that should survive recursion are saved here.
1160 This variable is reset before each transcoding.")
1162 (defun org-export-initialize-persistent-properties (data options backend)
1163 "Initialize `org-export-persistent-properties'.
1165 DATA is the parse tree from which information is retrieved.
1166 OPTIONS is a list holding export options. BACKEND is the
1167 back-end called for transcoding, as a symbol.
1169 Following initial persistent properties are set:
1170 `:back-end' Back-end used for transcoding.
1172 `:headline-alist' Alist of all headlines' name as key and a list
1173 holding beginning and ending positions as
1174 value.
1176 `:headline-offset' Offset between true level of headlines and
1177 local level. An offset of -1 means an headline
1178 of level 2 should be considered as a level
1179 1 headline in the context.
1181 `:headline-numbering' Alist of all headlines' beginning position
1182 as key an the associated numbering as value.
1184 `:parse-tree' Whole parse tree.
1186 `:point-max' Last position in the parse tree
1188 `:target-list' List of all targets' raw name in the parse tree.
1190 `:use-select-tags' Non-nil when parsed tree use a special tag to
1191 enforce transcoding of the headline."
1192 ;; First delete any residual persistent property.
1193 (setq org-export-persistent-properties nil)
1194 ;; Immediately after, set `:use-select-tags' property, as it will be
1195 ;; required for further computations.
1196 (setq options
1197 (org-export-set-property
1198 options
1199 :use-select-tags
1200 (org-export-use-select-tags-p data options)))
1201 ;; Get the rest of the initial persistent properties, now
1202 ;; `:use-select-tags' is set...
1203 ;; 1. `:parse-tree' ...
1204 (setq options (org-export-set-property options :parse-tree data))
1205 ;; 2. `:headline-offset' ...
1206 (setq options
1207 (org-export-set-property
1208 options :headline-offset
1209 (- 1 (org-export-get-min-level data options))))
1210 ;; 3. `:point-max' ...
1211 (setq options (org-export-set-property
1212 options :point-max
1213 (org-export-get-point-max data options)))
1214 ;; 4. `:target-list'...
1215 (setq options (org-export-set-property
1216 options :target-list
1217 (org-element-map
1218 data 'target
1219 (lambda (target info)
1220 (org-element-get-property :raw-value target)))))
1221 ;; 5. `:headline-alist'
1222 (setq options (org-export-set-property
1223 options :headline-alist
1224 (org-element-map
1225 data 'headline
1226 (lambda (headline info)
1227 (list (org-element-get-property :raw-value headline)
1228 (org-element-get-property :begin headline)
1229 (org-element-get-property :end headline))))))
1230 ;; 6. `:headline-numbering'
1231 (setq options (org-export-set-property
1232 options :headline-numbering
1233 (org-export-collect-headline-numbering data options)))
1234 ;; 7. `:back-end'
1235 (setq options (org-export-set-property options :back-end backend)))
1237 (defun org-export-use-select-tags-p (data options)
1238 "Non-nil when data use a tag enforcing transcoding.
1239 DATA is parsed data as returned by `org-element-parse-buffer'.
1240 OPTIONS is a plist holding export options."
1241 (org-element-map
1242 data
1243 'headline
1244 (lambda (headline info)
1245 (let ((tags (org-element-get-property :with-tags headline)))
1246 (and tags (string-match
1247 (format ":%s:" (plist-get info :select-tags)) tags))))
1248 options
1249 'stop-at-first-match))
1251 (defun org-export-get-min-level (data options)
1252 "Return minimum exportable headline's level in DATA.
1253 DATA is parsed tree as returned by `org-element-parse-buffer'.
1254 OPTIONS is a plist holding export options."
1255 (catch 'exit
1256 (let ((min-level 10000))
1257 (mapc (lambda (blob)
1258 (when (and (eq (car blob) 'headline)
1259 (not (org-export-skip-p blob options)))
1260 (setq min-level
1261 (min (org-element-get-property :level blob) min-level)))
1262 (when (= min-level 1) (throw 'exit 1)))
1263 (org-element-get-contents data))
1264 ;; If no headline was found, for the sake of consistency, set
1265 ;; minimum level to 1 nonetheless.
1266 (if (= min-level 10000) 1 min-level))))
1268 (defun org-export-get-point-max (data options)
1269 "Return last exportable ending position in DATA.
1270 DATA is parsed tree as returned by `org-element-parse-buffer'.
1271 OPTIONS is a plist holding export options."
1272 (let ((pos-max 1))
1273 (mapc (lambda (blob)
1274 (unless (and (eq (car blob) 'headline)
1275 (org-export-skip-p blob options))
1276 (setq pos-max (org-element-get-property :end blob))))
1277 (org-element-get-contents data))
1278 pos-max))
1280 (defun org-export-collect-headline-numbering (data options)
1281 "Return numbering of all exportable headlines in a parse tree.
1283 DATA is the parse tree. OPTIONS is the plist holding export
1284 options.
1286 Return an alist whose key is headline's beginning position and
1287 value is its associated numbering (in the shape of a list of
1288 numbers)."
1289 (let ((numbering (make-vector org-export-max-depth 0)))
1290 (org-element-map
1291 data
1292 'headline
1293 (lambda (headline info)
1294 (let ((relative-level
1295 (1- (org-export-get-relative-level headline info))))
1296 (cons
1297 (org-element-get-property :begin headline)
1298 (loop for n across numbering
1299 for idx from 0 to org-export-max-depth
1300 when (< idx relative-level) collect n
1301 when (= idx relative-level) collect (aset numbering idx (1+ n))
1302 when (> idx relative-level) do (aset numbering idx 0)))))
1303 options)))
1306 ;;;; Properties Management
1308 ;; This is mostly done with the help of two functions. On the one
1309 ;; hand `org-export-update-info' is used to keep up-to-date local
1310 ;; information while walking the nested list representing the parsed
1311 ;; document. On the other end, `org-export-set-property' handles
1312 ;; properties modifications according to their type (persistent or
1313 ;; local).
1315 ;; As exceptions, `:code-refs' and `:total-loc' properties are updated
1316 ;; with `org-export-handle-code' function.
1318 (defun org-export-update-info (blob info recursep)
1319 "Update export options depending on context.
1321 BLOB is the element or object being parsed. INFO is the plist
1322 holding the export options.
1324 When RECURSEP is non-nil, assume the following element or object
1325 will be inside the current one.
1327 The following properties are updated:
1328 `footnote-seen-labels' List of already parsed footnote
1329 labels (string list)
1330 `genealogy' List of current element's parents
1331 (list of elements and objects).
1332 `previous-element' Previous element's type (symbol).
1333 `previous-object' Previous object's type (symbol).
1335 Return the property list."
1336 (let* ((type (and (not (stringp blob)) (car blob))))
1337 (cond
1338 ;; Case 1: We're moving into a recursive blob.
1339 (recursep
1340 (org-combine-plists
1341 info
1342 `(:genealogy ,(cons blob (plist-get info :genealogy)))
1343 org-export-persistent-properties))
1344 ;; Case 2: No recursion.
1346 ;; At a footnote reference: mark its label as seen, if not
1347 ;; already the case.
1348 (when (eq type 'footnote-reference)
1349 (let ((label (org-element-get-property :label blob))
1350 (seen-labels (plist-get org-export-persistent-properties
1351 :footnote-seen-labels)))
1352 ;; Store anonymous footnotes (nil label) without checking if
1353 ;; another anonymous footnote was seen before.
1354 (unless (and label (member label seen-labels))
1355 (setq info (org-export-set-property
1356 info :footnote-seen-labels (push label seen-labels))))))
1357 ;; Set `:previous-element' or `:previous-object' according to
1358 ;; BLOB.
1359 (setq info (cond ((not type)
1360 (org-export-set-property
1361 info :previous-object 'plain-text))
1362 ((memq type org-element-all-elements)
1363 (org-export-set-property info :previous-element type))
1364 (t (org-export-set-property info :previous-object type))))
1365 ;; Return updated value.
1366 info))))
1368 (defun org-export-set-property (info prop value)
1369 "Set property PROP to VALUE in plist INFO.
1370 Return the new plist."
1371 (when (memq prop org-export-persistent-properties-list)
1372 (setq org-export-persistent-properties
1373 (plist-put org-export-persistent-properties prop value)))
1374 (plist-put info prop value))
1378 ;;; The Transcoder
1380 ;; This function reads Org data (obtained with, i.e.
1381 ;; `org-element-parse-buffer') and transcodes it into a specified
1382 ;; back-end output. It takes care of updating local properties,
1383 ;; filtering out elements or objects according to export options and
1384 ;; organizing the output blank lines and white space are preserved.
1386 ;; Though, this function is inapropriate for secondary strings, which
1387 ;; require a fresh copy of the plist passed as INFO argument. Thus,
1388 ;; `org-export-secondary-string' is provided for that specific task.
1390 ;; Internally, three functions handle the filtering of objects and
1391 ;; elements during the export. More precisely, `org-export-skip-p'
1392 ;; determines if the considered object or element should be ignored
1393 ;; altogether, `org-export-interpret-p' tells which elements or
1394 ;; objects should be seen as real Org syntax and `org-export-expand'
1395 ;; transforms the others back into their original shape.
1397 (defun org-export-data (data backend info)
1398 "Convert DATA to a string into BACKEND format.
1400 DATA is a nested list as returned by `org-element-parse-buffer'.
1402 BACKEND is a symbol among supported exporters.
1404 INFO is a plist holding export options and also used as
1405 a communication channel between elements when walking the nested
1406 list. See `org-export-update-info' function for more
1407 details.
1409 Return transcoded string."
1410 (mapconcat
1411 ;; BLOB can be an element, an object, a string, or nil.
1412 (lambda (blob)
1413 (cond
1414 ((not blob) nil) ((equal blob "") nil)
1415 ;; BLOB is a string. Check if the optional transcoder for plain
1416 ;; text exists, and call it in that case. Otherwise, simply
1417 ;; return string. Also update INFO and call
1418 ;; `org-export-filter-plain-text-functions'.
1419 ((stringp blob)
1420 (setq info (org-export-update-info blob info nil))
1421 (let ((transcoder (intern (format "org-%s-plain-text" backend))))
1422 (org-export-filter-apply-functions
1423 org-export-filter-plain-text-functions
1424 (if (fboundp transcoder) (funcall transcoder blob info) blob)
1425 backend)))
1426 ;; BLOB is an element or an object.
1428 (let* ((type (if (stringp blob) 'plain-text (car blob)))
1429 ;; 1. Determine the appropriate TRANSCODER.
1430 (transcoder
1431 (cond
1432 ;; 1.0 A full Org document is inserted.
1433 ((eq type 'org-data) 'identity)
1434 ;; 1.1. BLOB should be ignored.
1435 ((org-export-skip-p blob info) nil)
1436 ;; 1.2. BLOB shouldn't be transcoded. Interpret it
1437 ;; back into Org syntax.
1438 ((not (org-export-interpret-p blob info))
1439 'org-export-expand)
1440 ;; 1.3. Else apply naming convention.
1441 (t (let ((trans (intern
1442 (format "org-%s-%s" backend type))))
1443 (and (fboundp trans) trans)))))
1444 ;; 2. Compute CONTENTS of BLOB.
1445 (contents
1446 (cond
1447 ;; Case 0. No transcoder defined: ignore BLOB.
1448 ((not transcoder) nil)
1449 ;; Case 1. Transparently export an Org document.
1450 ((eq type 'org-data)
1451 (org-export-data blob backend info))
1452 ;; Case 2. For a recursive object.
1453 ((memq type org-element-recursive-objects)
1454 (org-export-data
1455 blob backend (org-export-update-info blob info t)))
1456 ;; Case 3. For a recursive element.
1457 ((memq type org-element-greater-elements)
1458 ;; Ignore contents of an archived tree
1459 ;; when `:with-archived-trees' is `headline'.
1460 (unless (and
1461 (eq type 'headline)
1462 (eq (plist-get info :with-archived-trees) 'headline)
1463 (org-element-get-property :archivedp blob))
1464 (org-element-normalize-string
1465 (org-export-data
1466 blob backend (org-export-update-info blob info t)))))
1467 ;; Case 4. For a paragraph.
1468 ((eq type 'paragraph)
1469 (let ((paragraph
1470 (org-element-normalize-contents
1471 blob
1472 ;; When normalizing contents of an item or
1473 ;; a footnote definition, ignore first line's
1474 ;; indentation: there is none and it might be
1475 ;; misleading.
1476 (and (not (plist-get info :previous-element))
1477 (let ((parent (caar (plist-get info :genealogy))))
1478 (memq parent '(footnote-definition item)))))))
1479 (org-export-data
1480 paragraph
1481 backend
1482 (org-export-update-info blob info t))))))
1483 ;; 3. Transcode BLOB into RESULTS string.
1484 (results (cond
1485 ((not transcoder) nil)
1486 ((eq transcoder 'org-export-expand)
1487 (org-export-data
1488 `(org-data nil ,(funcall transcoder blob contents))
1489 backend info))
1490 (t (funcall transcoder blob contents info)))))
1491 ;; 4. Discard nil results. Otherwise, update INFO, append
1492 ;; the same white space between elements or objects as in
1493 ;; the original buffer, and call appropriate filters.
1494 (when results
1495 (setq info (org-export-update-info blob info nil))
1496 ;; No filter for a full document.
1497 (if (eq type 'org-data)
1498 results
1499 (org-export-filter-apply-functions
1500 (eval (intern (format "org-export-filter-%s-functions" type)))
1501 (if (memq type org-element-all-elements)
1502 (concat
1503 (org-element-normalize-string results)
1504 (make-string (org-element-get-property :post-blank blob) 10))
1505 (concat
1506 results
1507 (make-string
1508 (org-element-get-property :post-blank blob) 32)))
1509 backend)))))))
1510 (org-element-get-contents data) ""))
1512 (defun org-export-secondary-string (secondary backend info)
1513 "Convert SECONDARY string into BACKEND format.
1515 SECONDARY is a nested list as returned by
1516 `org-element-parse-secondary-string'.
1518 BACKEND is a symbol among supported exporters.
1520 INFO is a plist holding export options and also used as
1521 a communication channel between elements when walking the nested
1522 list. See `org-export-update-info' function for more
1523 details.
1525 Return transcoded string."
1526 ;; Make SECONDARY acceptable for `org-export-data'.
1527 (let ((s (if (listp secondary) secondary (list secondary))))
1528 (org-export-data `(org-data nil ,@s) backend (copy-sequence info))))
1530 (defun org-export-skip-p (blob info)
1531 "Non-nil when element or object BLOB should be skipped during export.
1532 INFO is the plist holding export options."
1533 ;; Check headline.
1534 (unless (stringp blob)
1535 (case (car blob)
1536 ('headline
1537 (let ((with-tasks (plist-get info :with-tasks))
1538 (todo (org-element-get-property :todo-keyword blob))
1539 (todo-type (org-element-get-property :todo-type blob))
1540 (archived (plist-get info :with-archived-trees))
1541 (tag-list (let ((tags (org-element-get-property :tags blob)))
1542 (and tags (org-split-string tags ":")))))
1544 ;; Ignore subtrees with an exclude tag.
1545 (loop for k in (plist-get info :exclude-tags)
1546 thereis (member k tag-list))
1547 ;; Ignore subtrees without a select tag, when such tag is found
1548 ;; in the buffer.
1549 (and (plist-get info :use-select-tags)
1550 (loop for k in (plist-get info :select-tags)
1551 never (member k tag-list)))
1552 ;; Ignore commented sub-trees.
1553 (org-element-get-property :commentedp blob)
1554 ;; Ignore archived subtrees if `:with-archived-trees' is nil.
1555 (and (not archived) (org-element-get-property :archivedp blob))
1556 ;; Ignore tasks, if specified by `:with-tasks' property.
1557 (and todo (not with-tasks))
1558 (and todo
1559 (memq with-tasks '(todo done))
1560 (not (eq todo-type with-tasks)))
1561 (and todo
1562 (consp with-tasks)
1563 (not (member todo with-tasks))))))
1564 ;; Check time-stamp.
1565 ('time-stamp (not (plist-get info :with-timestamps)))
1566 ;; Check drawer.
1567 ('drawer
1568 (or (not (plist-get info :with-drawers))
1569 (and (consp (plist-get info :with-drawers))
1570 (not (member (org-element-get-property :drawer-name blob)
1571 (plist-get info :with-drawers))))))
1572 ;; Check export snippet.
1573 ('export-snippet
1574 (let* ((raw-back-end (org-element-get-property :back-end blob))
1575 (true-back-end
1576 (or (cdr (assoc raw-back-end org-export-snippet-translation-alist))
1577 raw-back-end)))
1578 (not (string= (symbol-name (plist-get info :back-end))
1579 true-back-end)))))))
1581 (defun org-export-interpret-p (blob info)
1582 "Non-nil if element or object BLOB should be interpreted as Org syntax.
1583 Check is done according to export options INFO, stored as
1584 a plist."
1585 (case (car blob)
1586 ;; ... entities...
1587 (entity (plist-get info :with-entities))
1588 ;; ... emphasis...
1589 (emphasis (plist-get info :with-emphasize))
1590 ;; ... fixed-width areas.
1591 (fixed-width (plist-get info :with-fixed-width))
1592 ;; ... footnotes...
1593 ((footnote-definition footnote-reference)
1594 (plist-get info :with-footnotes))
1595 ;; ... sub/superscripts...
1596 ((subscript superscript)
1597 (let ((sub/super-p (plist-get info :with-sub-superscript)))
1598 (if (eq sub/super-p '{})
1599 (org-element-get-property :use-brackets-p blob)
1600 sub/super-p)))
1601 ;; ... tables...
1602 (table (plist-get info :with-tables))
1603 (otherwise t)))
1605 (defsubst org-export-expand (blob contents)
1606 "Expand a parsed element or object to its original state.
1607 BLOB is either an element or an object. CONTENTS is its
1608 contents, as a string or nil."
1609 (funcall
1610 (intern (format "org-element-%s-interpreter" (car blob))) blob contents))
1614 ;;; The Filter System
1616 ;; Filters allow end-users to tweak easily the transcoded output.
1617 ;; They are the functional counterpart of hooks, as every filter in
1618 ;; a set is applied to the return value of the previous one.
1620 ;; Every set is back-end agnostic. Although, a filter is always
1621 ;; called, in addition to the string it applies to, with the back-end
1622 ;; used as argument, so it's easy enough for the end-user to add
1623 ;; back-end specific filters in the set.
1625 ;; Filters sets are defined below. There are of four types:
1627 ;; - `org-export-filter-parse-tree-functions' applies directly on the
1628 ;; complete parsed tree. It's the only filters set that doesn't
1629 ;; apply to a string.
1630 ;; - `org-export-filter-final-output-functions' applies to the final
1631 ;; transcoded string.
1632 ;; - `org-export-filter-plain-text-functions' applies to any string
1633 ;; not recognized as Org syntax.
1634 ;; - `org-export-filter-TYPE-functions' applies on the string returned
1635 ;; after an element or object of type TYPE has been transcoded.
1637 ;; All filters sets are applied through
1638 ;; `org-export-filter-apply-functions' function. Filters in a set are
1639 ;; applied in reverse order, that is in the order of consing. It
1640 ;; allows developers to be reasonably sure that their filters will be
1641 ;; applied first.
1643 ;;;; Special Filters
1644 (defvar org-export-filter-parse-tree-functions nil
1645 "Filter, or list of filters, applied to the parsed tree.
1646 Each filter is called with two arguments: the parse tree, as
1647 returned by `org-element-parse-buffer', and the back-end as
1648 a symbol. It must return the modified parse tree to transcode.")
1650 (defvar org-export-filter-final-output-functions nil
1651 "Filter, or list of filters, applied to the transcoded string.
1652 Each filter is called with two arguments: the full transcoded
1653 string, and the back-end as a symbol. It must return a string
1654 that will be used as the final export output.")
1656 (defvar org-export-filter-plain-text-functions nil
1657 "Filter, or list of filters, applied to plain text.
1658 Each filter is called with two arguments: a string which contains
1659 no Org syntax, and the back-end as a symbol. It must return
1660 a string or nil.")
1663 ;;;; Elements Filters
1665 (defvar org-export-filter-center-block-functions nil
1666 "Filter, or list of filters, applied to a transcoded center block.
1667 Each filter is called with two arguments: the transcoded center
1668 block, as a string, and the back-end, as a symbol. It must
1669 return a string or nil.")
1671 (defvar org-export-filter-drawer-functions nil
1672 "Filter, or list of filters, applied to a transcoded drawer.
1673 Each filter is called with two arguments: the transcoded drawer,
1674 as a string, and the back-end, as a symbol. It must return
1675 a string or nil.")
1677 (defvar org-export-filter-dynamic-block-functions nil
1678 "Filter, or list of filters, applied to a transcoded dynamic-block.
1679 Each filter is called with two arguments: the transcoded
1680 dynamic-block, as a string, and the back-end, as a symbol. It
1681 must return a string or nil.")
1683 (defvar org-export-filter-headline-functions nil
1684 "Filter, or list of filters, applied to a transcoded headline.
1685 Each filter is called with two arguments: the transcoded
1686 headline, as a string, and the back-end, as a symbol. It must
1687 return a string or nil.")
1689 (defvar org-export-filter-inlinetask-functions nil
1690 "Filter, or list of filters, applied to a transcoded inlinetask.
1691 Each filter is called with two arguments: the transcoded
1692 inlinetask, as a string, and the back-end, as a symbol. It must
1693 return a string or nil.")
1695 (defvar org-export-filter-plain-list-functions nil
1696 "Filter, or list of filters, applied to a transcoded plain-list.
1697 Each filter is called with two arguments: the transcoded
1698 plain-list, as a string, and the back-end, as a symbol. It must
1699 return a string or nil.")
1701 (defvar org-export-filter-item-functions nil
1702 "Filter, or list of filters, applied to a transcoded item.
1703 Each filter is called with two arguments: the transcoded item, as
1704 a string, and the back-end, as a symbol. It must return a string
1705 or nil.")
1707 (defvar org-export-filter-comment-functions nil
1708 "Filter, or list of filters, applied to a transcoded comment.
1709 Each filter is called with two arguments: the transcoded comment,
1710 as a string, and the back-end, as a symbol. It must return
1711 a string or nil.")
1713 (defvar org-export-filter-comment-block-functions nil
1714 "Filter, or list of filters, applied to a transcoded comment-comment.
1715 Each filter is called with two arguments: the transcoded
1716 comment-block, as a string, and the back-end, as a symbol. It
1717 must return a string or nil.")
1719 (defvar org-export-filter-example-block-functions nil
1720 "Filter, or list of filters, applied to a transcoded example-block.
1721 Each filter is called with two arguments: the transcoded
1722 example-block, as a string, and the back-end, as a symbol. It
1723 must return a string or nil.")
1725 (defvar org-export-filter-export-block-functions nil
1726 "Filter, or list of filters, applied to a transcoded export-block.
1727 Each filter is called with two arguments: the transcoded
1728 export-block, as a string, and the back-end, as a symbol. It
1729 must return a string or nil.")
1731 (defvar org-export-filter-fixed-width-functions nil
1732 "Filter, or list of filters, applied to a transcoded fixed-width.
1733 Each filter is called with two arguments: the transcoded
1734 fixed-width, as a string, and the back-end, as a symbol. It must
1735 return a string or nil.")
1737 (defvar org-export-filter-footnote-definition-functions nil
1738 "Filter, or list of filters, applied to a transcoded footnote-definition.
1739 Each filter is called with two arguments: the transcoded
1740 footnote-definition, as a string, and the back-end, as a symbol.
1741 It must return a string or nil.")
1743 (defvar org-export-filter-horizontal-rule-functions nil
1744 "Filter, or list of filters, applied to a transcoded horizontal-rule.
1745 Each filter is called with two arguments: the transcoded
1746 horizontal-rule, as a string, and the back-end, as a symbol. It
1747 must return a string or nil.")
1749 (defvar org-export-filter-keyword-functions nil
1750 "Filter, or list of filters, applied to a transcoded keyword.
1751 Each filter is called with two arguments: the transcoded keyword,
1752 as a string, and the back-end, as a symbol. It must return
1753 a string or nil.")
1755 (defvar org-export-filter-latex-environment-functions nil
1756 "Filter, or list of filters, applied to a transcoded latex-environment.
1757 Each filter is called with two arguments: the transcoded
1758 latex-environment, as a string, and the back-end, as a symbol.
1759 It must return a string or nil.")
1761 (defvar org-export-filter-babel-call-functions nil
1762 "Filter, or list of filters, applied to a transcoded babel-call.
1763 Each filter is called with two arguments: the transcoded
1764 babel-call, as a string, and the back-end, as a symbol. It must
1765 return a string or nil.")
1767 (defvar org-export-filter-paragraph-functions nil
1768 "Filter, or list of filters, applied to a transcoded paragraph.
1769 Each filter is called with two arguments: the transcoded
1770 paragraph, as a string, and the back-end, as a symbol. It must
1771 return a string or nil.")
1773 (defvar org-export-filter-property-drawer-functions nil
1774 "Filter, or list of filters, applied to a transcoded property-drawer.
1775 Each filter is called with two arguments: the transcoded
1776 property-drawer, as a string, and the back-end, as a symbol. It
1777 must return a string or nil.")
1779 (defvar org-export-filter-quote-block-functions nil
1780 "Filter, or list of filters, applied to a transcoded quote block.
1781 Each filter is called with two arguments: the transcoded quote
1782 block, as a string, and the back-end, as a symbol. It must
1783 return a string or nil.")
1785 (defvar org-export-filter-quote-section-functions nil
1786 "Filter, or list of filters, applied to a transcoded quote-section.
1787 Each filter is called with two arguments: the transcoded
1788 quote-section, as a string, and the back-end, as a symbol. It
1789 must return a string or nil.")
1791 (defvar org-export-filter-special-block-functions nil
1792 "Filter, or list of filters, applied to a transcoded special block.
1793 Each filter is called with two arguments: the transcoded special
1794 block, as a string, and the back-end, as a symbol. It must
1795 return a string or nil.")
1797 (defvar org-export-filter-src-block-functions nil
1798 "Filter, or list of filters, applied to a transcoded src-block.
1799 Each filter is called with two arguments: the transcoded
1800 src-block, as a string, and the back-end, as a symbol. It must
1801 return a string or nil.")
1803 (defvar org-export-filter-table-functions nil
1804 "Filter, or list of filters, applied to a transcoded table.
1805 Each filter is called with two arguments: the transcoded table,
1806 as a string, and the back-end, as a symbol. It must return
1807 a string or nil.")
1809 (defvar org-export-filter-verse-block-functions nil
1810 "Filter, or list of filters, applied to a transcoded verse block.
1811 Each filter is called with two arguments: the transcoded verse
1812 block, as a string, and the back-end, as a symbol. It must
1813 return a string or nil.")
1816 ;;;; Objects Filters
1818 (defvar org-export-filter-emphasis-functions nil
1819 "Filter, or list of filters, applied to a transcoded emphasis.
1820 Each filter is called with two arguments: the transcoded
1821 emphasis, as a string, and the back-end, as a symbol. It must
1822 return a string or nil.")
1824 (defvar org-export-filter-entity-functions nil
1825 "Filter, or list of filters, applied to a transcoded entity.
1826 Each filter is called with two arguments: the transcoded entity,
1827 as a string, and the back-end, as a symbol. It must return
1828 a string or nil.")
1830 (defvar org-export-filter-export-snippet-functions nil
1831 "Filter, or list of filters, applied to a transcoded export-snippet.
1832 Each filter is called with two arguments: the transcoded
1833 export-snippet, as a string, and the back-end, as a symbol. It
1834 must return a string or nil.")
1836 (defvar org-export-filter-footnote-reference-functions nil
1837 "Filter, or list of filters, applied to a transcoded footnote-reference.
1838 Each filter is called with two arguments: the transcoded
1839 footnote-reference, as a string, and the back-end, as a symbol.
1840 It must return a string or nil.")
1842 (defvar org-export-filter-inline-babel-call-functions nil
1843 "Filter, or list of filters, applied to a transcoded inline-babel-call.
1844 Each filter is called with two arguments: the transcoded
1845 inline-babel-call, as a string, and the back-end, as a symbol. It
1846 must return a string or nil.")
1848 (defvar org-export-filter-inline-src-block-functions nil
1849 "Filter, or list of filters, applied to a transcoded inline-src-block.
1850 Each filter is called with two arguments: the transcoded
1851 inline-src-block, as a string, and the back-end, as a symbol. It
1852 must return a string or nil.")
1854 (defvar org-export-filter-latex-fragment-functions nil
1855 "Filter, or list of filters, applied to a transcoded latex-fragment.
1856 Each filter is called with two arguments: the transcoded
1857 latex-fragment, as a string, and the back-end, as a symbol. It
1858 must return a string or nil.")
1860 (defvar org-export-filter-line-break-functions nil
1861 "Filter, or list of filters, applied to a transcoded line-break.
1862 Each filter is called with two arguments: the transcoded
1863 line-break, as a string, and the back-end, as a symbol. It must
1864 return a string or nil.")
1866 (defvar org-export-filter-link-functions nil
1867 "Filter, or list of filters, applied to a transcoded link.
1868 Each filter is called with two arguments: the transcoded link, as
1869 a string, and the back-end, as a symbol. It must return a string
1870 or nil.")
1872 (defvar org-export-filter-macro-functions nil
1873 "Filter, or list of filters, applied to a transcoded macro.
1874 Each filter is called with two arguments: the transcoded macro,
1875 as a string, and the back-end, as a symbol. It must return
1876 a string or nil.")
1878 (defvar org-export-filter-radio-target-functions nil
1879 "Filter, or list of filters, applied to a transcoded radio-target.
1880 Each filter is called with two arguments: the transcoded
1881 radio-target, as a string, and the back-end, as a symbol. It
1882 must return a string or nil.")
1884 (defvar org-export-filter-statistics-cookie-functions nil
1885 "Filter, or list of filters, applied to a transcoded statistics-cookie.
1886 Each filter is called with two arguments: the transcoded
1887 statistics-cookie, as a string, and the back-end, as a symbol.
1888 It must return a string or nil.")
1890 (defvar org-export-filter-subscript-functions nil
1891 "Filter, or list of filters, applied to a transcoded subscript.
1892 Each filter is called with two arguments: the transcoded
1893 subscript, as a string, and the back-end, as a symbol. It must
1894 return a string or nil.")
1896 (defvar org-export-filter-superscript-functions nil
1897 "Filter, or list of filters, applied to a transcoded superscript.
1898 Each filter is called with two arguments: the transcoded
1899 superscript, as a string, and the back-end, as a symbol. It must
1900 return a string or nil.")
1902 (defvar org-export-filter-target-functions nil
1903 "Filter, or list of filters, applied to a transcoded target.
1904 Each filter is called with two arguments: the transcoded target,
1905 as a string, and the back-end, as a symbol. It must return
1906 a string or nil.")
1908 (defvar org-export-filter-time-stamp-functions nil
1909 "Filter, or list of filters, applied to a transcoded time-stamp.
1910 Each filter is called with two arguments: the transcoded
1911 time-stamp, as a string, and the back-end, as a symbol. It must
1912 return a string or nil.")
1914 (defvar org-export-filter-verbatim-functions nil
1915 "Filter, or list of filters, applied to a transcoded verbatim.
1916 Each filter is called with two arguments: the transcoded
1917 verbatim, as a string, and the back-end, as a symbol. It must
1918 return a string or nil.")
1920 (defun org-export-filter-apply-functions (filters value backend)
1921 "Call every function in FILTERS with arguments VALUE and BACKEND.
1922 Functions are called in reverse order, to be reasonably sure that
1923 developer-specified filters, if any, are called first."
1924 ;; Ensure FILTERS is a list.
1925 (let ((filters (if (listp filters) (reverse filters) (list filters))))
1926 (loop for filter in filters
1927 if (not value) return nil else
1928 do (setq value (funcall filter value backend))))
1929 value)
1933 ;;; Core functions
1935 ;; This is the room for the main function, `org-export-as', along with
1936 ;; its derivatives, `org-export-to-buffer' and `org-export-to-file'.
1937 ;; They differ only by the way they output the resulting code.
1939 ;; Note that `org-export-as' doesn't really parse the current buffer,
1940 ;; but a copy of it (with the same buffer-local variables and
1941 ;; visibility), where Babel blocks are executed, if appropriate.
1942 ;; `org-export-with-current-buffer-copy' macro prepares that copy.
1944 (defun org-export-as (backend
1945 &optional subtreep visible-only body-only ext-plist)
1946 "Transcode current Org buffer into BACKEND code.
1948 If narrowing is active in the current buffer, only transcode its
1949 narrowed part.
1951 If a region is active, transcode that region.
1953 When optional argument SUBTREEP is non-nil, transcode the
1954 sub-tree at point, extracting information from the headline
1955 properties first.
1957 When optional argument VISIBLE-ONLY is non-nil, don't export
1958 contents of hidden elements.
1960 When optional argument BODY-ONLY is non-nil, only return body
1961 code, without preamble nor postamble.
1963 EXT-PLIST, when provided, is a property list with external
1964 parameters overriding Org default settings, but still inferior to
1965 file-local settings.
1967 Return code as a string."
1968 (save-excursion
1969 (save-restriction
1970 ;; Narrow buffer to an appropriate region for parsing.
1971 (when (org-region-active-p)
1972 (narrow-to-region (region-beginning) (region-end))
1973 (goto-char (point-min)))
1974 (when (and subtreep (not (org-at-heading-p)))
1975 ;; Ensure point is at sub-tree's beginning.
1976 (org-with-limited-levels (org-back-to-heading (not visible-only))))
1977 ;; Retrieve export options (INFO) and parsed tree (RAW-DATA).
1978 ;; Buffer isn't parsed directly. Instead, a temporary copy is
1979 ;; created, where all code blocks are evaluated. RAW-DATA is
1980 ;; the parsed tree of the buffer resulting from that process.
1981 ;; Eventually call `org-export-filter-parse-tree-functions'.
1982 (let ((info (org-export-collect-options backend subtreep ext-plist))
1983 (raw-data (progn
1984 (when subtreep ; Only parse subtree contents.
1985 (let ((end (save-excursion (org-end-of-subtree t))))
1986 (narrow-to-region
1987 (progn (forward-line) (point)) end)))
1988 (org-export-filter-apply-functions
1989 org-export-filter-parse-tree-functions
1990 (org-export-with-current-buffer-copy
1991 (org-export-blocks-preprocess)
1992 (org-element-parse-buffer nil visible-only))
1993 backend))))
1994 ;; Initialize the communication system and combine it to INFO.
1995 (setq info
1996 (org-combine-plists
1997 info
1998 (org-export-initialize-persistent-properties
1999 raw-data info backend)))
2000 ;; Now transcode RAW-DATA. Also call
2001 ;; `org-export-filter-final-output-functions'.
2002 (let* ((body (org-element-normalize-string
2003 (org-export-data raw-data backend info)))
2004 (template (intern (format "org-%s-template" backend)))
2005 (output (org-export-filter-apply-functions
2006 org-export-filter-final-output-functions
2007 (if (or (not (fboundp template)) body-only) body
2008 (funcall template body info))
2009 backend)))
2010 ;; Maybe add final OUTPUT to kill ring before returning it.
2011 (when org-export-copy-to-kill-ring (org-kill-new output))
2012 output)))))
2014 (defun org-export-to-buffer (backend buffer &optional subtreep visible-only
2015 body-only ext-plist)
2016 "Call `org-export-as' with output to a specified buffer.
2018 BACKEND is the back-end used for transcoding, as a symbol.
2020 BUFFER is the output buffer. If it already exists, it will be
2021 erased first, otherwise, it will be created.
2023 Arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and EXT-PLIST are
2024 similar to those used in `org-export-as', which see.
2026 Return buffer."
2027 (let ((out (org-export-as backend subtreep visible-only body-only ext-plist))
2028 (buffer (get-buffer-create buffer)))
2029 (with-current-buffer buffer
2030 (erase-buffer)
2031 (insert out)
2032 (goto-char (point-min)))
2033 buffer))
2035 (defun org-export-to-file (backend &optional post-process subtreep visible-only
2036 body-only ext-plist pub-dir)
2037 "Call `org-export-as' with output to a specified file.
2039 BACKEND is the back-end used for transcoding, as a symbol.
2041 Optional argument POST-PROCESS, when non-nil, is a function
2042 applied to the output file. It expects one argument: the file
2043 name, as a string. It can be used to call shell commands on that
2044 file, display a specific buffer, etc.
2046 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and
2047 EXT-PLIST are similar to those used in `org-export-as', which
2048 see.
2050 When optional argument PUB-DIR is set, use it as the publishing
2051 directory.
2053 Return output file's name."
2054 ;; First get output directory and output file name.
2055 (let ((out-file
2056 (concat (file-name-as-directory (or pub-dir "."))
2057 ;; Output file name either comes from
2058 ;; EXPORT_FILE_NAME sub-tree property, assuming input
2059 ;; is narrowed to said sub-tree, or to the name of
2060 ;; buffer's associated file.
2061 (file-name-sans-extension
2062 (or (and subtreep
2063 (org-entry-get
2064 (save-excursion
2065 (ignore-errors
2066 (org-back-to-heading (not visible-only))
2067 (point)))
2068 "EXPORT_FILE_NAME" t))
2069 (file-name-nondirectory
2070 (or (buffer-file-name (buffer-base-buffer))
2071 (error "Output file's name undefined")))))
2072 ".tex")))
2073 ;; Checks for file and directory permissions.
2074 (cond
2075 ((not (file-exists-p out-file))
2076 (unless (file-writable-p (or pub-dir "."))
2077 (error "Output directory not writable")))
2078 ((not (file-writable-p out-file)) (error "Output file not writable")))
2079 ;; All checks passed: insert contents to a temporary buffer and
2080 ;; write it to the specified file.
2081 (let ((out (org-export-as
2082 backend subtreep visible-only body-only ext-plist)))
2083 (with-temp-buffer
2084 (insert out)
2085 (let ((coding-system-for-write org-export-coding-system))
2086 (write-file out-file))))
2087 (when post-process (funcall post-process out-file))
2088 ;; Return full path.
2089 out-file))
2091 (defmacro org-export-with-current-buffer-copy (&rest body)
2092 "Apply BODY in a copy of the current buffer.
2094 The copy preserves local variables and visibility of the original
2095 buffer.
2097 Point is at buffer's beginning when BODY is applied."
2098 (org-with-gensyms (original-buffer offset buffer-string overlays)
2099 `(let ((,original-buffer ,(current-buffer))
2100 (,offset ,(1- (point-min)))
2101 (,buffer-string ,(buffer-string))
2102 (,overlays (mapcar
2103 'copy-overlay (overlays-in (point-min) (point-max)))))
2104 (with-temp-buffer
2105 (let ((buffer-invisibility-spec nil))
2106 (org-clone-local-variables
2107 ,original-buffer "^\\(org-\\|orgtbl-\\|major-mode$\\)")
2108 (insert ,buffer-string)
2109 (mapc (lambda (ov)
2110 (move-overlay
2112 (- (overlay-start ov) ,offset)
2113 (- (overlay-end ov) ,offset)
2114 (current-buffer)))
2115 ,overlays)
2116 (goto-char (point-min))
2117 (progn ,@body))))))
2118 (def-edebug-spec org-export-with-current-buffer-copy (body))
2122 ;;; Tools For Back-Ends
2124 ;; A whole set of tools is available to help build new exporters. Any
2125 ;; function general enough to have its use across many back-ends
2126 ;; should be added here.
2128 ;; As of now, functions operating on footnotes, headlines, include
2129 ;; keywords, links, macros, references, src-blocks, tables and tables
2130 ;; of contents are implemented.
2132 ;;;; For Footnotes
2134 ;; `org-export-collect-footnote-definitions' is a tool to list
2135 ;; actually used footnotes definitions in the whole parse tree, or in
2136 ;; an headline, in order to add footnote listings throughout the
2137 ;; transcoded data.
2139 ;; `org-export-footnote-first-reference-p' is a predicate used by some
2140 ;; back-ends, when they need to attach the footnote definition only to
2141 ;; the first occurrence of the corresponding label.
2143 ;; `org-export-get-footnote-definition' and
2144 ;; `org-export-get-footnote-number' provide easier access to
2145 ;; additional information relative to a footnote reference.
2147 (defun org-export-collect-footnote-definitions (data info)
2148 "Return an alist between footnote numbers, labels and definitions.
2150 DATA is the parse tree from which definitions are collected.
2151 INFO is the plist used as a communication channel.
2153 Definitions are sorted by order of references. They either
2154 appear as Org data \(transcoded with `org-export-data'\) or as
2155 a secondary string for inlined footnotes \(transcoded with
2156 `org-export-secondary-string'\). Unreferenced definitions are
2157 ignored."
2158 (org-element-map
2159 data 'footnote-reference
2160 (lambda (footnote local)
2161 (when (org-export-footnote-first-reference-p footnote local)
2162 (list (org-export-get-footnote-number footnote local)
2163 (org-element-get-property :label footnote)
2164 (org-export-get-footnote-definition footnote local))))
2165 info))
2167 (defun org-export-footnote-first-reference-p (footnote-reference info)
2168 "Non-nil when a footnote reference is the first one for its label.
2170 FOOTNOTE-REFERENCE is the footnote reference being considered.
2171 INFO is the plist used as a communication channel."
2172 (let ((label (org-element-get-property :label footnote-reference)))
2173 (not (and label (member label (plist-get info :footnote-seen-labels))))))
2175 (defun org-export-get-footnote-definition (footnote-reference info)
2176 "Return definition of FOOTNOTE-REFERENCE as parsed data.
2177 INFO is the plist used as a communication channel."
2178 (let ((label (org-element-get-property :label footnote-reference)))
2179 (or (org-element-get-property :inline-definition footnote-reference)
2180 (cdr (assoc label (plist-get info :footnote-definition-alist))))))
2182 (defun org-export-get-footnote-number (footnote info)
2183 "Return number associated to a footnote.
2185 FOOTNOTE is either a footnote reference or a footnote definition.
2186 INFO is the plist used as a communication channel."
2187 (let ((label (org-element-get-property :label footnote)))
2188 (if (eq (car footnote) 'footnote-definition)
2189 ;; If a footnote definition was provided, first search for
2190 ;; a relative footnote reference, as only footnote references
2191 ;; can determine the associated ordinal.
2192 (org-element-map
2193 (plist-get info :parse-tree) 'footnote-reference
2194 (lambda (foot-ref local)
2195 (when (string= (org-element-get-property :label foot-ref) label)
2196 (let* ((all-seen (plist-get info :footnote-seen-labels))
2197 (seenp (and label (member label all-seen))))
2198 (if seenp (length seenp) (1+ (length all-seen))))))
2199 info 'first-match)
2200 (let* ((all-seen (plist-get info :footnote-seen-labels))
2201 ;; Anonymous footnotes are always new footnotes.
2202 (seenp (and label (member label all-seen))))
2203 (if seenp (length seenp) (1+ (length all-seen)))))))
2206 ;;;; For Headlines
2208 ;; `org-export-get-relative-level' is a shortcut to get headline
2209 ;; level, relatively to the lower headline level in the parsed tree.
2211 ;; `org-export-get-headline-number' returns the section number of an
2212 ;; headline, while `org-export-number-to-roman' allows to convert it
2213 ;; to roman numbers.
2215 ;; `org-export-first-sibling-p' and `org-export-last-sibling-p' are
2216 ;; two useful predicates when it comes to fulfill the
2217 ;; `:headline-levels' property.
2219 (defun org-export-get-relative-level (headline info)
2220 "Return HEADLINE relative level within current parsed tree.
2221 INFO is a plist holding contextual information."
2222 (+ (org-element-get-property :level headline)
2223 (or (plist-get info :headline-offset) 0)))
2225 (defun org-export-get-headline-number (headline info)
2226 "Return HEADLINE numbering as a list of numbers.
2227 INFO is a plist holding contextual information."
2228 (cdr (assq (org-element-get-property :begin headline)
2229 (plist-get info :headline-numbering))))
2231 (defun org-export-number-to-roman (n)
2232 "Convert integer N into a roman numeral."
2233 (let ((roman '((1000 . "M") (900 . "CM") (500 . "D") (400 . "CD")
2234 ( 100 . "C") ( 90 . "XC") ( 50 . "L") ( 40 . "XL")
2235 ( 10 . "X") ( 9 . "IX") ( 5 . "V") ( 4 . "IV")
2236 ( 1 . "I")))
2237 (res ""))
2238 (if (<= n 0)
2239 (number-to-string n)
2240 (while roman
2241 (if (>= n (caar roman))
2242 (setq n (- n (caar roman))
2243 res (concat res (cdar roman)))
2244 (pop roman)))
2245 res)))
2247 (defun org-export-first-sibling-p (headline info)
2248 "Non-nil when HEADLINE is the first sibling in its sub-tree.
2249 INFO is the plist used as a communication channel."
2250 (not (eq (plist-get info :previous-element) 'headline)))
2252 (defun org-export-last-sibling-p (headline info)
2253 "Non-nil when HEADLINE is the last sibling in its sub-tree.
2254 INFO is the plist used as a communication channel."
2255 (= (org-element-get-property :end headline)
2256 (or (org-element-get-property
2257 :end (org-export-get-parent-headline headline info))
2258 (plist-get info :point-max))))
2261 ;;;; For Include Keywords
2263 ;; This section provides a tool to properly handle insertion of files
2264 ;; during export: `org-export-included-files'. It recursively
2265 ;; transcodes a file specfied by an include keyword.
2267 ;; It uses two helper functions: `org-export-get-file-contents'
2268 ;; returns contents of a file according to parameters specified in the
2269 ;; keyword while `org-export-parse-included-file' parses the file
2270 ;; specified by it.
2272 (defun org-export-included-file (keyword backend info)
2273 "Transcode file specified with include KEYWORD.
2275 KEYWORD is the include keyword element transcoded. BACKEND is
2276 the language back-end used for transcoding. INFO is the plist
2277 used as a communication channel.
2279 This function updates `:included-files' and `:headline-offset'
2280 properties.
2282 Return the transcoded string."
2283 (let ((data (org-export-parse-included-file keyword info))
2284 (file (let ((value (org-element-get-property :value keyword)))
2285 (and (string-match "^\"\\(\\S-+\\)\"" value)
2286 (match-string 1 value)))))
2287 (org-element-normalize-string
2288 (org-export-data
2289 data backend
2290 (org-combine-plists
2291 info
2292 ;; Store full path of already included files to avoid
2293 ;; recursive file inclusion.
2294 `(:included-files
2295 ,(cons (expand-file-name file) (plist-get info :included-files))
2296 ;; Ensure that a top-level headline in the included
2297 ;; file becomes a direct child of the current headline
2298 ;; in the buffer.
2299 :headline-offset
2300 ,(- (+ (org-element-get-property
2301 :level (org-export-get-parent-headline keyword info))
2302 (plist-get info :headline-offset))
2303 (1- (org-export-get-min-level data info)))))))))
2305 (defun org-export-get-file-contents (file &optional lines)
2306 "Get the contents of FILE and return them as a string.
2307 When optional argument LINES is a string specifying a range of
2308 lines, include only those lines."
2309 (with-temp-buffer
2310 (insert-file-contents file)
2311 (when lines
2312 (let* ((lines (split-string lines "-"))
2313 (lbeg (string-to-number (car lines)))
2314 (lend (string-to-number (cadr lines)))
2315 (beg (if (zerop lbeg) (point-min)
2316 (goto-char (point-min))
2317 (forward-line (1- lbeg))
2318 (point)))
2319 (end (if (zerop lend) (point-max)
2320 (goto-char (point-min))
2321 (forward-line (1- lend))
2322 (point))))
2323 (narrow-to-region beg end)))
2324 (buffer-string)))
2326 (defun org-export-parse-included-file (keyword info)
2327 "Parse file specified by include KEYWORD.
2329 KEYWORD is the include keyword element transcoded. BACKEND is the
2330 language back-end used for transcoding. INFO is the plist used as
2331 a communication channel.
2333 Return the parsed tree."
2334 (let* ((value (org-element-get-property :value keyword))
2335 (file (and (string-match "^\"\\(\\S-+\\)\"" value)
2336 (prog1 (match-string 1 value)
2337 (setq value (replace-match "" nil nil value)))))
2338 (lines (and (string-match
2339 ":lines +\"\\(\\(?:[0-9]+\\)?-\\(?:[0-9]+\\)?\\)\"" value)
2340 (prog1 (match-string 1 value)
2341 (setq value (replace-match "" nil nil value)))))
2342 (env (cond ((string-match "\\<example\\>" value) "example")
2343 ((string-match "\\<src\\(?: +\\(.*\\)\\)?" value)
2344 (match-string 1 value)))))
2345 (cond
2346 ((or (not file)
2347 (not (file-exists-p file))
2348 (not (file-readable-p file)))
2349 (format "Cannot include file %s" file))
2350 ((and (not env)
2351 (member (expand-file-name file) (plist-get info :included-files)))
2352 (error "Recursive file inclusion: %S" file))
2353 (t (let ((raw (org-element-normalize-string
2354 (org-export-get-file-contents
2355 (expand-file-name file) lines))))
2356 ;; If environment isn't specified, Insert file in
2357 ;; a temporary buffer and parse it as Org syntax.
2358 ;; Otherwise, build the element representing the file.
2359 (cond
2360 ((not env)
2361 (with-temp-buffer
2362 (insert raw) (org-mode) (org-element-parse-buffer)))
2363 ((string= "example" env)
2364 `(org-data nil (example-block (:value ,raw :post-blank 0))))
2366 `(org-data
2368 (src-block (:value ,raw :language ,env :post-blank 0))))))))))
2371 ;;;; For Links
2373 ;; `org-export-solidify-link-text' turns a string into a safer version
2374 ;; for links, replacing most non-standard characters with hyphens.
2376 ;; `org-export-get-coderef-format' returns an appropriate format
2377 ;; string for coderefs.
2379 ;; `org-export-inline-image-p' returns a non-nil value when the link
2380 ;; provided should be considered as an inline image.
2382 ;; `org-export-resolve-fuzzy-link' searches destination of fuzzy links
2383 ;; (i.e. links with "fuzzy" as type) within the parsed tree, and
2384 ;; returns an appropriate unique identifier when found, or nil.
2386 (defun org-export-solidify-link-text (s)
2387 "Take link text and make a safe target out of it."
2388 (save-match-data
2389 (mapconcat 'identity (org-split-string s "[^a-zA-Z0-9_\\.-]+") "-")))
2391 (defun org-export-get-coderef-format (path desc)
2392 "Return format string for code reference link.
2393 PATH is the link path. DESC is its description."
2394 (save-match-data
2395 (cond ((string-match (regexp-quote (concat "(" path ")")) desc)
2396 (replace-match "%s" t t desc))
2397 ((string= desc "") "%s")
2398 (t desc))))
2400 (defun org-export-inline-image-p (link &optional extensions)
2401 "Non-nil if LINK object points to an inline image.
2403 When non-nil, optional argument EXTENSIONS is a list of valid
2404 extensions for image files, as strings. Otherwise, a default
2405 list is provided \(cf. `org-image-file-name-regexp'\)."
2406 (and (not (org-element-get-contents link))
2407 (string= (org-element-get-property :type link) "file")
2408 (org-file-image-p
2409 (expand-file-name (org-element-get-property :path link))
2410 extensions)))
2412 (defun org-export-resolve-fuzzy-link (link info)
2413 "Return an unique identifier for LINK destination.
2415 INFO is a plist holding contextual information.
2417 Return value can be a string, an buffer position, or nil:
2419 - If LINK path exactly matches any target, return its name as the
2420 identifier.
2422 - If LINK path exactly matches any headline name, return
2423 headline's beginning position as the identifier. If more than
2424 one headline share that name, priority will be given to the one
2425 with the closest common ancestor, if any, or the first one in
2426 the parse tree otherwise.
2428 - Otherwise, return nil.
2430 Assume LINK type is \"fuzzy\"."
2431 (let ((path (org-element-get-property :path link)))
2432 (if (member path (plist-get info :target-list))
2433 ;; Link points to a target: return its name as a string.
2434 path
2435 ;; Link either points to an headline or nothing. Try to find
2436 ;; the source, with priority given to headlines with the closest
2437 ;; common ancestor. If such candidate is found, return its
2438 ;; beginning position as an unique identifier, otherwise return
2439 ;; nil.
2440 (let* ((head-alist (plist-get info :headline-alist))
2441 (link-begin (org-element-get-property :begin link))
2442 (link-end (org-element-get-property :end link))
2443 ;; Store candidates as a list of cons cells holding their
2444 ;; beginning and ending position.
2445 (cands (loop for head in head-alist
2446 when (string= (car head) path)
2447 collect (cons (nth 1 head) (nth 2 head)))))
2448 (cond
2449 ;; No candidate: return nil.
2450 ((not cands) nil)
2451 ;; If one or more candidates share common ancestors with
2452 ;; LINK, return beginning position of the first one matching
2453 ;; the closer ancestor shared.
2454 ((let ((ancestors (loop for head in head-alist
2455 when (and (> link-begin (nth 1 head))
2456 (<= link-end (nth 2 head)))
2457 collect (cons (nth 1 head) (nth 2 head)))))
2458 (loop named main for ancestor in (nreverse ancestors) do
2459 (loop for candidate in cands
2460 when (and (>= (car candidate) (car ancestor))
2461 (<= (cdr candidate) (cdr ancestor)))
2462 do (return-from main (car candidate))))))
2463 ;; No candidate have a common ancestor with link: First match
2464 ;; will do. Return its beginning position.
2465 (t (caar cands)))))))
2468 ;;;; For Macros
2470 ;; `org-export-expand-macro' simply takes care of expanding macros.
2472 (defun org-export-expand-macro (macro info)
2473 "Expand MACRO and return it as a string.
2474 INFO is a plist holding export options."
2475 (let* ((key (org-element-get-property :key macro))
2476 (args (org-element-get-property :args macro))
2477 (value (plist-get info (intern (format ":macro-%s" key)))))
2478 ;; Replace arguments in VALUE.
2479 (let ((s 0) n)
2480 (while (string-match "\\$\\([0-9]+\\)" value s)
2481 (setq s (1+ (match-beginning 0))
2482 n (string-to-number (match-string 1 value)))
2483 (and (>= (length args) n)
2484 (setq value (replace-match (nth (1- n) args) t t value)))))
2485 ;; VALUE starts with "(eval": it is a s-exp, `eval' it.
2486 (when (string-match "\\`(eval\\>" value)
2487 (setq value (eval (read value))))
2488 ;; Return expanded string.
2489 (format "%s" value)))
2492 ;;;; For References
2494 ;; `org-export-get-ordinal' associates a sequence number to any object
2495 ;; or element.
2497 (defun org-export-get-ordinal (element info &optional within-section predicate)
2498 "Return ordinal number of an element or object.
2500 ELEMENT is the element or object considered. INFO is the plist
2501 used as a communication channel.
2503 When optional argument WITHIN-SECTION is non-nil, narrow counting
2504 to the section containing ELEMENT.
2506 Optional argument PREDICATE is a function returning a non-nil
2507 value if the current element or object should be counted in. It
2508 accepts one argument: the element or object being considered.
2509 This argument allows to count only a certain type of objects,
2510 like inline images, which are a subset of links \(in that case,
2511 `org-export-inline-image-p' might be an useful predicate\)."
2512 (let ((counter 0)
2513 (type (car element))
2514 ;; Determine if search should apply to current section, in
2515 ;; which case it should be retrieved first, or to full parse
2516 ;; tree. As a special case, an element or object without
2517 ;; a parent headline will also trigger a full search,
2518 ;; notwithstanding WITHIN-SECTION value.
2519 (data
2520 (if (not within-section) (plist-get info :parse-tree)
2521 (or (org-export-get-parent-headline element info)
2522 (plist-get info :parse-tree)))))
2523 ;; Increment counter until ELEMENT is found again.
2524 (org-element-map
2525 data type
2526 (lambda (el local)
2527 (cond
2528 ((and (functionp predicate) (funcall predicate el)))
2529 ((equal element el) (1+ counter))
2530 (t (incf counter) nil)))
2531 info 'first-match)))
2534 ;;;; For Src-Blocks
2536 ;; `org-export-handle-code' takes care of line numbering and reference
2537 ;; cleaning in source code, when appropriate. It also updates global
2538 ;; LOC count (`:total-loc' property) and code references alist
2539 ;; (`:code-refs' property).
2541 (defun org-export-handle-code (code switches info
2542 &optional language num-fmt ref-fmt)
2543 "Handle line numbers and code references in CODE.
2545 CODE is the string to process. SWITCHES is the option string
2546 determining which changes will be applied to CODE. INFO is the
2547 plist used as a communication channel during export.
2549 Optional argument LANGUAGE, when non-nil, is a string specifying
2550 code's language.
2552 If optional argument NUM-FMT is a string, it will be used as
2553 a format string for numbers at beginning of each line.
2555 If optional argument REF-FMT is a string, it will be used as
2556 a format string for each line of code containing a reference.
2558 Update the following INFO properties by side-effect: `:total-loc'
2559 and `:code-refs'.
2561 Return new code as a string."
2562 (let* ((switches (or switches ""))
2563 (numberp (string-match "[-+]n\\>" switches))
2564 (continuep (string-match "\\+n\\>" switches))
2565 (total-LOC (if (and numberp (not continuep))
2567 (or (plist-get info :total-loc) 0)))
2568 (preserve-indent-p (or org-src-preserve-indentation
2569 (string-match "-i\\>" switches)))
2570 (replace-labels (when (string-match "-r\\>" switches)
2571 (if (string-match "-k\\>" switches) 'keep t)))
2572 ;; Get code and clean it. Remove blank lines at its
2573 ;; beginning and end. Also remove protective commas.
2574 (code (let ((c (replace-regexp-in-string
2575 "\\`\\([ \t]*\n\\)+" ""
2576 (replace-regexp-in-string
2577 "\\(:?[ \t]*\n\\)*[ \t]*\\'" "\n" code))))
2578 ;; If appropriate, remove global indentation.
2579 (unless preserve-indent-p (setq c (org-remove-indentation c)))
2580 ;; Free up the protected lines. Note: Org blocks
2581 ;; have commas at the beginning or every line.
2582 (if (string= language "org")
2583 (replace-regexp-in-string "^," "" c)
2584 (replace-regexp-in-string
2585 "^\\(,\\)\\(:?\\*\\|[ \t]*#\\+\\)" "" c nil nil 1))))
2586 ;; Split code to process it line by line.
2587 (code-lines (org-split-string code "\n"))
2588 ;; Ensure line numbers will be correctly padded before
2589 ;; applying the format string.
2590 (num-fmt (format (if (stringp num-fmt) num-fmt "%s: ")
2591 (format "%%%ds"
2592 (length (number-to-string
2593 (+ (length code-lines)
2594 total-LOC))))))
2595 ;; Get format used for references.
2596 (label-fmt (or (and (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
2597 (match-string 1 switches))
2598 org-coderef-label-format))
2599 ;; Build a regexp matching a loc with a reference.
2600 (with-ref-re (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
2601 (replace-regexp-in-string
2602 "%s" "\\([-a-zA-Z0-9_ ]+\\)" label-fmt nil t)))
2603 coderefs)
2604 (org-element-normalize-string
2605 (mapconcat (lambda (loc)
2606 ;; Maybe add line number to current line of code
2607 ;; (LOC).
2608 (when numberp
2609 (setq loc (concat (format num-fmt (incf total-LOC)) loc)))
2610 ;; Take action if at a ref line.
2611 (when (string-match with-ref-re loc)
2612 (let ((ref (match-string 3 loc)))
2613 (setq loc
2614 (cond
2615 ;; Option "-k": don't remove labels. Use
2616 ;; numbers for references when lines are
2617 ;; numbered, use labels otherwise.
2618 ((eq replace-labels 'keep)
2619 (let ((full-ref (format "(%s)" ref)))
2620 (push (cons ref (if numberp total-LOC full-ref))
2621 coderefs)
2622 (replace-match full-ref nil nil loc 2))
2623 (replace-match (format "(%s)" ref) nil nil loc 2))
2624 ;; Option "-r" without "-k": remove labels.
2625 ;; Use numbers for references when lines are
2626 ;; numbered, use labels otherwise.
2627 (replace-labels
2628 (push (cons ref (if numberp total-LOC ref))
2629 coderefs)
2630 (replace-match "" nil nil loc 1))
2631 ;; Else: don't remove labels and don't use
2632 ;; numbers for references.
2634 (let ((full-ref (format "(%s)" ref)))
2635 (push (cons ref full-ref) coderefs)
2636 (replace-match full-ref nil nil loc 2)))))))
2637 ;; If REF-FMT is defined, apply it to current LOC.
2638 (when (stringp ref-fmt) (setq loc (format ref-fmt loc)))
2639 ;; Update by side-effect communication channel.
2640 ;; Return updated LOC.
2641 (setq info (org-export-set-property
2642 (org-export-set-property
2643 info :code-refs coderefs)
2644 :total-loc total-LOC))
2645 loc)
2646 code-lines "\n"))))
2649 ;;;; For Tables
2651 ;; `org-export-table-format-info' extracts formatting information
2652 ;; (alignment, column groups and presence of a special column) from
2653 ;; a raw table and returns it as a property list.
2655 ;; `org-export-clean-table' cleans the raw table from any Org
2656 ;; table-specific syntax.
2658 (defun org-export-table-format-info (table)
2659 "Extract info from TABLE.
2660 Return a plist whose properties and values are:
2661 `:alignment' vector of strings among \"r\", \"l\" and \"c\",
2662 `:column-groups' vector of symbols among `start', `end', `start-end',
2663 `:row-groups' list of integers representing row groups.
2664 `:special-column-p' non-nil if table has a special column.
2665 `:width' vector of integers representing desired width of
2666 current column, or nil."
2667 (with-temp-buffer
2668 (insert table)
2669 (goto-char 1)
2670 (org-table-align)
2671 (let ((align (vconcat (mapcar (lambda (c) (if c "r" "l"))
2672 org-table-last-alignment)))
2673 (width (make-vector (length org-table-last-alignment) nil))
2674 (colgroups (make-vector (length org-table-last-alignment) nil))
2675 (row-group 0)
2676 (rowgroups)
2677 (special-column-p 'empty))
2678 (mapc (lambda (row)
2679 (if (string-match "^[ \t]*|[-+]+|[ \t]*$" row)
2680 (incf row-group)
2681 (push row-group rowgroups)
2682 ;; Determine if a special column is present by looking
2683 ;; for special markers in the first column. More
2684 ;; accurately, the first column is considered special
2685 ;; if it only contains special markers and, maybe,
2686 ;; empty cells.
2687 (setq special-column-p
2688 (cond
2689 ((not special-column-p) nil)
2690 ((string-match "^[ \t]*| *\\\\?\\([\#!$*_^]\\) *|"
2691 row) 'special)
2692 ((string-match "^[ \t]*| +|" row) special-column-p))))
2693 (cond
2694 ;; Read forced alignment and width information, if any,
2695 ;; and determine final alignment for the table.
2696 ((org-table-cookie-line-p row)
2697 (let ((col 0))
2698 (mapc (lambda (field)
2699 (when (string-match "<\\([lrc]\\)\\([0-9]+\\)?>" field)
2700 (aset align col (match-string 1 field))
2701 (aset width col (let ((w (match-string 2 field)))
2702 (and w (string-to-number w)))))
2703 (incf col))
2704 (org-split-string row "[ \t]*|[ \t]*"))))
2705 ;; Read column groups information.
2706 ((org-table-colgroup-line-p row)
2707 (let ((col 0))
2708 (mapc (lambda (field)
2709 (aset colgroups col
2710 (cond ((string= "<" field) 'start)
2711 ((string= ">" field) 'end)
2712 ((string= "<>" field) 'start-end)))
2713 (incf col))
2714 (org-split-string row "[ \t]*|[ \t]*"))))))
2715 (org-split-string table "\n"))
2716 ;; Return plist.
2717 (list :alignment align
2718 :column-groups colgroups
2719 :row-groups (reverse rowgroups)
2720 :special-column-p (eq special-column-p 'special)
2721 :width width))))
2723 (defun org-export-clean-table (table specialp)
2724 "Clean string TABLE from its formatting elements.
2725 Remove any row containing column groups or formatting cookies and
2726 rows starting with a special marker. If SPECIALP is non-nil,
2727 assume the table contains a special formatting column and remove
2728 it also."
2729 (let ((rows (org-split-string table "\n")))
2730 (mapconcat 'identity
2731 (delq nil
2732 (mapcar
2733 (lambda (row)
2734 (cond
2735 ((org-table-colgroup-line-p row) nil)
2736 ((org-table-cookie-line-p row) nil)
2737 ;; Ignore rows starting with a special marker.
2738 ((string-match "^[ \t]*| *[!_^/] *|" row) nil)
2739 ;; Remove special column.
2740 ((and specialp
2741 (or (string-match "^\\([ \t]*\\)|-+\\+" row)
2742 (string-match "^\\([ \t]*\\)|[^|]*|" row)))
2743 (replace-match "\\1|" t nil row))
2744 (t row)))
2745 rows))
2746 "\n")))
2749 ;;;; For Tables Of Contents
2751 ;; `org-export-collect-headlines' builds a list of all exportable
2752 ;; headline elements, maybe limited to a certain depth. One can then
2753 ;; easily parse it and transcode it.
2755 ;; Building lists of tables, figures or listings is quite similar.
2756 ;; Once the generic function `org-export-collect-elements' is defined,
2757 ;; `org-export-collect-tables', `org-export-collect-figures' and
2758 ;; `org-export-collect-listings' can be derived from it.
2760 (defun org-export-collect-headlines (info &optional n)
2761 "Collect headlines in order to build a table of contents.
2763 When non-nil, optional argument N must be an integer. It
2764 specifies the depth of the table of contents.
2766 Return a list of all exportable headlines as parsed elements."
2767 (org-element-map
2768 (plist-get info :parse-tree)
2769 'headline
2770 (lambda (headline local)
2771 ;; Strip contents from HEADLINE.
2772 (let ((relative-level (org-export-get-relative-level headline local)))
2773 (unless (and n (> relative-level n)) headline)))
2774 info))
2776 (defun org-export-collect-elements (type backend info)
2777 "Collect named elements of type TYPE.
2779 Only elements with a caption or a name are collected.
2781 BACKEND is the back-end used to transcode their caption or name.
2782 INFO is a plist holding export options.
2784 Return an alist where key is entry's name and value an unique
2785 identifier that might be used for internal links."
2786 (org-element-map
2787 (plist-get info :parse-tree)
2788 type
2789 (lambda (element info)
2790 (let ((entry
2791 (cond
2792 ((org-element-get-property :caption element)
2793 (org-export-secondary-string
2794 (org-element-get-property :caption element) backend info))
2795 ((org-element-get-property :name element)
2796 (org-export-secondary-string
2797 (org-element-get-property :name element) backend info)))))
2798 ;; Skip elements with neither a caption nor a name.
2799 (when entry (cons entry (org-element-get-property :begin element)))))
2800 info))
2802 (defun org-export-collect-tables (backend info)
2803 "Build a list of tables.
2805 BACKEND is the back-end used to transcode table's name. INFO is
2806 a plist holding export options.
2808 Return an alist where key is the caption of the table and value
2809 an unique identifier that might be used for internal links."
2810 (org-export-collect-elements 'table backend info))
2812 (defun org-export-collect-figures (backend info)
2813 "Build a list of figures.
2815 A figure is a paragraph type element with a caption or a name.
2817 BACKEND is the back-end used to transcode headline's name. INFO
2818 is a plist holding export options.
2820 Return an alist where key is the caption of the figure and value
2821 an unique indentifier that might be used for internal links."
2822 (org-export-collect-elements 'paragraph backend info))
2824 (defun org-export-collect-listings (backend info)
2825 "Build a list of src blocks.
2827 BACKEND is the back-end used to transcode src block's name. INFO
2828 is a plist holding export options.
2830 Return an alist where key is the caption of the src block and
2831 value an unique indentifier that might be used for internal
2832 links."
2833 (org-export-collect-elements 'src-block backend info))
2836 ;;;; Misc. Tools
2838 (defun org-export-get-parent-headline (blob info)
2839 "Return BLOB's closest parent headline or nil."
2840 (catch 'exit
2841 (mapc
2842 (lambda (el) (when (eq (car el) headline) (throw 'exit el)))
2843 (plist-get info :genealogy))
2844 nil))
2848 ;;; The Dispatcher
2850 ;; `org-export-dispatch' is the standard interactive way to start an
2851 ;; export process. It uses `org-export-dispatch-ui' as a subroutine
2852 ;; for its interface. Most commons back-ends should have an entry in
2853 ;; it.
2855 (defun org-export-dispatch ()
2856 "Export dispatcher for Org mode.
2858 It provides an access to common export related tasks in a buffer.
2859 Its interface comes in two flavours: standard and expert. While
2860 both share the same set of bindings, only the former displays the
2861 valid keys associations. Set `org-export-dispatch-use-expert-ui'
2862 to switch to one or the other.
2864 Return an error if key pressed has no associated command."
2865 (interactive)
2866 (let* ((input (org-export-dispatch-ui
2867 (if (listp org-export-initial-scope) org-export-initial-scope
2868 (list org-export-initial-scope))
2869 org-export-dispatch-use-expert-ui))
2870 (raw-key (car input))
2871 (scope (cdr input)))
2872 ;; Translate "C-a", "C-b"... into "a", "b"... Then take action
2873 ;; depending on user's key pressed.
2874 (case (if (< raw-key 27) (+ raw-key 96) raw-key)
2875 ;; Export with `e-latex' back-end.
2876 (?L (let ((outbuf (org-export-to-buffer
2877 'e-latex "*Org E-latex Export*"
2878 (memq 'subtree scope)
2879 (memq 'visible scope)
2880 (memq 'body scope))))
2881 (with-current-buffer outbuf (latex-mode))
2882 (when org-export-show-temporary-export-buffer
2883 (switch-to-buffer-other-window outbuf))))
2884 ((?l ?p ?d)
2885 (org-export-to-file
2886 'e-latex
2887 (cond ((eq raw-key ?p) #'org-e-latex-compile)
2888 ((eq raw-key ?d)
2889 (lambda (file) (org-open-file (org-e-latex-compile file)))))
2890 (memq 'subtree scope)
2891 (memq 'visible scope)
2892 (memq 'body scope)))
2893 ;; Undefined command.
2894 (t (error "No command associated with key %s"
2895 (char-to-string raw-key))))))
2897 (defun org-export-dispatch-ui (scope expertp)
2898 "Handle interface for `org-export-dispatch'.
2900 SCOPE is a list containing current interactive options set for
2901 export. It can contain any of the following symbols:
2902 `body' toggles a body-only export
2903 `subtree' restricts export to current subtree
2904 `visible' restricts export to visible part of buffer.
2906 EXPERTP, when non-nil, triggers expert UI. In that case, no help
2907 buffer is provided, but indications about currently active
2908 options are given in the prompt. Moreover, \[?] allows to switch
2909 back to standard interface.
2911 Return value is a list with key pressed as car and a list of
2912 final interactive export options as cdr."
2913 (let ((help (format "------------------- General Options -------------------
2914 \[1] Body only: %s
2915 \[2] Export scope: %s
2916 \[3] Visible only: %s
2918 -------------------- LaTeX Export ---------------------
2919 \[l] to LaTeX file [L] to temporary buffer
2920 \[p] to PDF file [d] ... and open it"
2921 (if (memq 'body scope) "On" "Off")
2922 (if (memq 'subtree scope) "Subtree" "Buffer")
2923 (if (memq 'visible scope) "On" "Off")))
2924 (standard-prompt "Export command: ")
2925 (expert-prompt (format "Export command (%s%s%s): "
2926 (if (memq 'body scope) "b" "-")
2927 (if (memq 'subtree scope) "s" "-")
2928 (if (memq 'visible scope) "v" "-")))
2929 (handle-keypress
2930 (function
2931 ;; Read a character from command input, toggling interactive
2932 ;; options when applicable. PROMPT is the displayed prompt,
2933 ;; as a string.
2934 (lambda (prompt)
2935 (let ((key (read-char-exclusive prompt)))
2936 (cond
2937 ;; Ignore non-standard characters (i.e. "M-a").
2938 ((not (characterp key)) (org-export-dispatch-ui scope expertp))
2939 ;; Switch back to standard interface.
2940 ((and (eq key ??) expertp) (org-export-dispatch-ui scope nil))
2941 ((eq key ?1)
2942 (org-export-dispatch-ui
2943 (if (memq 'body scope) (remq 'body scope) (cons 'body scope))
2944 expertp))
2945 ((eq key ?2)
2946 (org-export-dispatch-ui
2947 (if (memq 'subtree scope) (remq 'subtree scope)
2948 (cons 'subtree scope))
2949 expertp))
2950 ((eq key ?3)
2951 (org-export-dispatch-ui
2952 (if (memq 'visible scope) (remq 'visible scope)
2953 (cons 'visible scope))
2954 expertp))
2955 (t (cons key scope))))))))
2956 ;; With expert UI, just read key with a fancy prompt. In standard
2957 ;; UI, display an intrusive help buffer.
2958 (if expertp (funcall handle-keypress expert-prompt)
2959 (save-window-excursion
2960 (delete-other-windows)
2961 (with-output-to-temp-buffer "*Org Export/Publishing Help*" (princ help))
2962 (org-fit-window-to-buffer
2963 (get-buffer-window "*Org Export/Publishing Help*"))
2964 (funcall handle-keypress standard-prompt)))))
2967 (provide 'org-export)
2968 ;;; org-export.el ends here