contrib/lisp/org-export: Fix visibility influence on list parsing
[org-mode.git] / contrib / lisp / org-export.el
blob3d65ed5d0e9e63a7f985493d8dfdc4c665db4578
1 ;;; org-export.el --- Generic Export Engine For Org
3 ;; Copyright (C) 2011 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 last
89 ;; part of this file.
91 ;;; Code:
92 (eval-when-compile (require 'cl))
93 (require 'org-element)
96 ;;; Internal Variables
98 ;; Among internal variables, the most important is
99 ;; `org-export-option-alist'. This variable define the global export
100 ;; options, shared between every exporter, and how they are acquired.
102 (defconst org-export-max-depth 19
103 "Maximum nesting depth for headlines, counting from 0.")
105 (defconst org-export-option-alist
106 '((:author "AUTHOR" nil user-full-name t)
107 (:creator "CREATOR" nil org-export-creator-string)
108 (:date "DATE" nil nil t)
109 (:description "DESCRIPTION" nil nil newline)
110 (:email "EMAIL" nil user-mail-address t)
111 (:exclude-tags "EXPORT_EXCLUDE_TAGS" nil org-export-exclude-tags split)
112 (:headline-levels nil "H" org-export-headline-levels)
113 (:keywords "KEYWORDS" nil nil space)
114 (:language "LANGUAGE" nil org-export-default-language t)
115 (:preserve-breaks nil "\\n" org-export-preserve-breaks)
116 (:section-numbers nil "num" org-export-with-section-numbers)
117 (:select-tags "EXPORT_SELECT_TAGS" nil org-export-select-tags split)
118 (:time-stamp-file nil "timestamp" org-export-time-stamp-file)
119 (:title "TITLE" nil nil space)
120 (:with-archived-trees nil "arch" org-export-with-archived-trees)
121 (:with-author nil "author" org-export-with-author)
122 (:with-creator nil "creator" org-export-with-creator)
123 (:with-drawers nil "drawer" org-export-with-drawers)
124 (:with-email nil "email" org-export-with-email)
125 (:with-emphasize nil "*" org-export-with-emphasize)
126 (:with-entities nil "e" org-export-with-entities)
127 (:with-fixed-width nil ":" org-export-with-fixed-width)
128 (:with-footnotes nil "f" org-export-with-footnotes)
129 (:with-priority nil "pri" org-export-with-priority)
130 (:with-special-strings nil "-" org-export-with-special-strings)
131 (:with-sub-superscript nil "^" org-export-with-sub-superscripts)
132 (:with-toc nil "toc" org-export-with-toc)
133 (:with-tables nil "|" org-export-with-tables)
134 (:with-tags nil "tags" org-export-with-tags)
135 (:with-tasks nil "tasks" org-export-with-tasks)
136 (:with-timestamps nil "<" org-export-with-timestamps)
137 (:with-todo-keywords nil "todo" org-export-with-todo-keywords))
138 "Alist between export properties and ways to set them.
140 The car of the alist is the property name, and the cdr is a list
141 like \(KEYWORD OPTION DEFAULT BEHAVIOUR\) where:
143 KEYWORD is a string representing a buffer keyword, or nil.
144 OPTION is a string that could be found in an #+OPTIONS: line.
145 DEFAULT is the default value for the property.
146 BEHAVIOUR determine how Org should handle multiple keywords for
147 the same property. It is a symbol among:
148 nil Keep old value and discard the new one.
149 t Replace old value with the new one.
150 `space' Concatenate the values, separating them with a space.
151 `newline' Concatenate the values, separating them with
152 a newline.
153 `split' Split values at white spaces, and cons them to the
154 previous list.
156 KEYWORD and OPTION have precedence over DEFAULT.
158 All these properties should be back-end agnostic. For back-end
159 specific properties, define a similar variable named
160 `org-BACKEND-option-alist', replacing BACKEND with the name of
161 the appropriate back-end. You can also redefine properties
162 there, as they have precedence over these.")
164 (defconst org-export-special-keywords
165 '("SETUP_FILE" "OPTIONS" "MACRO")
166 "List of in-buffer keywords that require special treatment.
167 These keywords are not directly associated to a property. The
168 way they are handled must be hard-coded into
169 `org-export-get-inbuffer-options' function.")
173 ;;; User-configurable Variables
175 ;; Configuration for the masses.
177 ;; They should never be evaled directly, as their value is to be
178 ;; stored in a property list (cf. `org-export-option-alist').
180 (defgroup org-export nil
181 "Options for exporting Org mode files."
182 :tag "Org Export"
183 :group 'org)
185 (defgroup org-export-general nil
186 "General options for export engine."
187 :tag "Org Export General"
188 :group 'org-export)
190 (defcustom org-export-with-archived-trees 'headline
191 "Whether sub-trees with the ARCHIVE tag should be exported.
193 This can have three different values:
194 nil Do not export, pretend this tree is not present.
195 t Do export the entire tree.
196 `headline' Only export the headline, but skip the tree below it.
198 This option can also be set with the #+OPTIONS line,
199 e.g. \"arch:nil\"."
200 :group 'org-export-general
201 :type '(choice
202 (const :tag "Not at all" nil)
203 (const :tag "Headline only" 'headline)
204 (const :tag "Entirely" t)))
206 (defcustom org-export-with-author t
207 "Non-nil means insert author name into the exported file.
208 This option can also be set with the #+OPTIONS line,
209 e.g. \"author:nil\"."
210 :group 'org-export-general
211 :type 'boolean)
213 (defcustom org-export-with-creator 'comment
214 "Non-nil means the postamble should contain a creator sentence.
216 The sentence can be set in `org-export-creator-string' and
217 defaults to \"Generated by Org mode XX in Emacs XXX.\".
219 If the value is `comment' insert it as a comment."
220 :group 'org-export-general
221 :type '(choice
222 (const :tag "No creator sentence" nil)
223 (const :tag "Sentence as a comment" 'comment)
224 (const :tag "Insert the sentence" t)))
226 (defcustom org-export-creator-string
227 (format "Generated by Org mode %s in Emacs %s." org-version emacs-version)
228 "String to insert at the end of the generated document."
229 :group 'org-export-general
230 :type '(string :tag "Creator string"))
232 (defcustom org-export-with-drawers nil
233 "Non-nil means export with drawers like the property drawer.
234 When t, all drawers are exported. This may also be a list of
235 drawer names to export."
236 :group 'org-export-general
237 :type '(choice
238 (const :tag "All drawers" t)
239 (const :tag "None" nil)
240 (repeat :tag "Selected drawers"
241 (string :tag "Drawer name"))))
243 (defcustom org-export-with-email nil
244 "Non-nil means insert author email into the exported file.
245 This option can also be set with the #+OPTIONS line,
246 e.g. \"email:t\"."
247 :group 'org-export-general
248 :type 'boolean)
250 (defcustom org-export-with-emphasize t
251 "Non-nil means interpret *word*, /word/, and _word_ as emphasized text.
253 If the export target supports emphasizing text, the word will be
254 typeset in bold, italic, or underlined, respectively. Not all
255 export backends support this.
257 This option can also be set with the #+OPTIONS line, e.g. \"*:nil\"."
258 :group 'org-export-general
259 :type 'boolean)
261 (defcustom org-export-exclude-tags '("noexport")
262 "Tags that exclude a tree from export.
263 All trees carrying any of these tags will be excluded from
264 export. This is without condition, so even subtrees inside that
265 carry one of the `org-export-select-tags' will be removed."
266 :group 'org-export-general
267 :type '(repeat (string :tag "Tag")))
269 (defcustom org-export-with-fixed-width t
270 "Non-nil means lines starting with \":\" will be in fixed width font.
272 This can be used to have pre-formatted text, fragments of code
273 etc. For example:
274 : ;; Some Lisp examples
275 : (while (defc cnt)
276 : (ding))
277 will be looking just like this in also HTML. See also the QUOTE
278 keyword. Not all export backends support this.
280 This option can also be set with the #+OPTIONS line, e.g. \"::nil\"."
281 :group 'org-export-translation
282 :type 'boolean)
284 (defcustom org-export-with-footnotes t
285 "Non-nil means Org footnotes should be exported.
286 This option can also be set with the #+OPTIONS line,
287 e.g. \"f:nil\"."
288 :group 'org-export-general
289 :type 'boolean)
291 (defcustom org-export-headline-levels 3
292 "The last level which is still exported as a headline.
294 Inferior levels will produce itemize lists when exported. Note
295 that a numeric prefix argument to an exporter function overrides
296 this setting.
298 This option can also be set with the #+OPTIONS line, e.g. \"H:2\"."
299 :group 'org-export-general
300 :type 'integer)
302 (defcustom org-export-default-language "en"
303 "The default language for export and clocktable translations, as a string.
304 This may have an association in
305 `org-clock-clocktable-language-setup'."
306 :group 'org-export-general
307 :type '(string :tag "Language"))
309 (defcustom org-export-preserve-breaks nil
310 "Non-nil means preserve all line breaks when exporting.
312 Normally, in HTML output paragraphs will be reformatted.
314 This option can also be set with the #+OPTIONS line,
315 e.g. \"\\n:t\"."
316 :group 'org-export-general
317 :type 'boolean)
319 (defcustom org-export-with-entities t
320 "Non-nil means interpret entities when exporting.
322 For example, HTML export converts \\alpha to &alpha; and \\AA to
323 &Aring;.
325 For a list of supported names, see the constant `org-entities'
326 and the user option `org-entities-user'.
328 This option can also be set with the #+OPTIONS line,
329 e.g. \"e:nil\"."
330 :group 'org-export-general
331 :type 'boolean)
333 (defcustom org-export-with-priority nil
334 "Non-nil means include priority cookies in export.
335 When nil, remove priority cookies for export."
336 :group 'org-export-general
337 :type 'boolean)
339 (defcustom org-export-with-section-numbers t
340 "Non-nil means add section numbers to headlines when exporting.
342 This option can also be set with the #+OPTIONS line,
343 e.g. \"num:t\"."
344 :group 'org-export-general
345 :type 'boolean)
347 (defcustom org-export-select-tags '("export")
348 "Tags that select a tree for export.
349 If any such tag is found in a buffer, all trees that do not carry
350 one of these tags will be deleted before export. Inside trees
351 that are selected like this, you can still deselect a subtree by
352 tagging it with one of the `org-export-exclude-tags'."
353 :group 'org-export-general
354 :type '(repeat (string :tag "Tag")))
356 (defcustom org-export-with-special-strings t
357 "Non-nil means interpret \"\-\", \"--\" and \"---\" for export.
359 When this option is turned on, these strings will be exported as:
361 Org HTML LaTeX
362 -----+----------+--------
363 \\- &shy; \\-
364 -- &ndash; --
365 --- &mdash; ---
366 ... &hellip; \ldots
368 This option can also be set with the #+OPTIONS line,
369 e.g. \"-:nil\"."
370 :group 'org-export-general
371 :type 'boolean)
373 (defcustom org-export-with-sub-superscripts t
374 "Non-nil means interpret \"_\" and \"^\" for export.
376 When this option is turned on, you can use TeX-like syntax for
377 sub- and superscripts. Several characters after \"_\" or \"^\"
378 will be considered as a single item - so grouping with {} is
379 normally not needed. For example, the following things will be
380 parsed as single sub- or superscripts.
382 10^24 or 10^tau several digits will be considered 1 item.
383 10^-12 or 10^-tau a leading sign with digits or a word
384 x^2-y^3 will be read as x^2 - y^3, because items are
385 terminated by almost any nonword/nondigit char.
386 x_{i^2} or x^(2-i) braces or parenthesis do grouping.
388 Still, ambiguity is possible - so when in doubt use {} to enclose
389 the sub/superscript. If you set this variable to the symbol
390 `{}', the braces are *required* in order to trigger
391 interpretations as sub/superscript. This can be helpful in
392 documents that need \"_\" frequently in plain text.
394 This option can also be set with the #+OPTIONS line,
395 e.g. \"^:nil\"."
396 :group 'org-export-general
397 :type '(choice
398 (const :tag "Interpret them" t)
399 (const :tag "Curly brackets only" {})
400 (const :tag "Do not interpret them" nil)))
402 (defcustom org-export-with-toc t
403 "Non-nil means create a table of contents in exported files.
405 The TOC contains headlines with levels up
406 to`org-export-headline-levels'. When an integer, include levels
407 up to N in the toc, this may then be different from
408 `org-export-headline-levels', but it will not be allowed to be
409 larger than the number of headline levels. When nil, no table of
410 contents is made.
412 This option can also be set with the #+OPTIONS line,
413 e.g. \"toc:nil\" or \"toc:3\"."
414 :group 'org-export-general
415 :type '(choice
416 (const :tag "No Table of Contents" nil)
417 (const :tag "Full Table of Contents" t)
418 (integer :tag "TOC to level")))
420 (defcustom org-export-with-tables t
421 "If non-nil, lines starting with \"|\" define a table.
422 For example:
424 | Name | Address | Birthday |
425 |-------------+----------+-----------|
426 | Arthur Dent | England | 29.2.2100 |
428 This option can also be set with the #+OPTIONS line, e.g. \"|:nil\"."
429 :group 'org-export-general
430 :type 'boolean)
432 (defcustom org-export-with-tags t
433 "If nil, do not export tags, just remove them from headlines.
435 If this is the symbol `not-in-toc', tags will be removed from
436 table of contents entries, but still be shown in the headlines of
437 the document.
439 This option can also be set with the #+OPTIONS line,
440 e.g. \"tags:nil\"."
441 :group 'org-export-general
442 :type '(choice
443 (const :tag "Off" nil)
444 (const :tag "Not in TOC" not-in-toc)
445 (const :tag "On" t)))
447 (defcustom org-export-with-tasks t
448 "Non-nil means include TODO items for export.
449 This may have the following values:
450 t include tasks independent of state.
451 todo include only tasks that are not yet done.
452 done include only tasks that are already done.
453 nil remove all tasks before export
454 list of keywords keep only tasks with these keywords"
455 :group 'org-export-general
456 :type '(choice
457 (const :tag "All tasks" t)
458 (const :tag "No tasks" nil)
459 (const :tag "Not-done tasks" todo)
460 (const :tag "Only done tasks" done)
461 (repeat :tag "Specific TODO keywords"
462 (string :tag "Keyword"))))
464 (defcustom org-export-time-stamp-file t
465 "Non-nil means insert a time stamp into the exported file.
466 The time stamp shows when the file was created.
468 This option can also be set with the #+OPTIONS line,
469 e.g. \"timestamp:nil\"."
470 :group 'org-export-general
471 :type 'boolean)
473 (defcustom org-export-with-timestamps t
474 "If nil, do not export time stamps and associated keywords."
475 :group 'org-export-general
476 :type 'boolean)
478 (defcustom org-export-with-todo-keywords t
479 "Non-nil means include TODO keywords in export.
480 When nil, remove all these keywords from the export.")
482 (defcustom org-export-allow-BIND 'confirm
483 "Non-nil means allow #+BIND to define local variable values for export.
484 This is a potential security risk, which is why the user must
485 confirm the use of these lines."
486 :group 'org-export-general
487 :type '(choice
488 (const :tag "Never" nil)
489 (const :tag "Always" t)
490 (const :tag "Ask a confirmation for each file" confirm)))
492 (defcustom org-export-snippet-translation-alist nil
493 "Alist between export snippets back-ends and exporter back-ends.
495 This variable allows to provide shortcuts for export snippets.
497 For example, with a value of '\(\(\"h\" . \"html\"\)\), the HTML
498 back-end will recognize the contents of \"@h{<b>}\" as HTML code
499 while every other back-end will ignore it."
500 :group 'org-export-general
501 :type '(repeat
502 (cons
503 (string :tag "Shortcut")
504 (string :tag "Back-end"))))
508 ;;; The Communication Channel
510 ;; During export process, every function has access to a number of
511 ;; properties. They are of three types:
513 ;; 1. Export options are collected once at the very beginning of the
514 ;; process, out of the original buffer and environment. The task
515 ;; is handled by `org-export-collect-options' function.
517 ;; All export options are defined through the
518 ;; `org-export-option-alist' variable.
520 ;; 2. Persistent properties are stored in
521 ;; `org-export-persistent-properties' and available at every level
522 ;; of recursion. Their value is extracted directly from the parsed
523 ;; tree, and depends on export options (whole trees may be filtered
524 ;; out of the export process).
526 ;; Properties belonging to that type are defined in the
527 ;; `org-export-persistent-properties-list' variable.
529 ;; 3. Every other property is considered local, and available at
530 ;; a precise level of recursion and below.
532 ;; Managing properties during transcode process is mainly done with
533 ;; `org-export-update-info'. Even though they come from different
534 ;; sources, the function transparently concatenates them in a single
535 ;; property list passed as an argument to each transcode function.
536 ;; Thus, during export, all necessary information is available through
537 ;; that single property list, and the element or object itself.
538 ;; Though, modifying a property will still require some special care,
539 ;; and should be done with `org-export-set-property' instead of plain
540 ;; `plist-put'.
542 ;; Here is the full list of properties available during transcode
543 ;; process, with their category (option, persistent or local), their
544 ;; value type and the function updating them, when appropriate.
546 ;; + `author' :: Author's name.
547 ;; - category :: option
548 ;; - type :: string
550 ;; + `back-end' :: Current back-end used for transcoding.
551 ;; - category :: persistent
552 ;; - type :: symbol
554 ;; + `code-refs' :: Association list between reference name and real
555 ;; labels in source code. It is used to properly
556 ;; resolve links inside source blocks.
557 ;; - category :: persistent
558 ;; - type :: alist (INT-OR-STRING . STRING)
559 ;; - update :: `org-export-handle-code'
561 ;; + `creator' :: String to write as creation information.
562 ;; - category :: option
563 ;; - type :: string
565 ;; + `date' :: String to use as date.
566 ;; - category :: option
567 ;; - type :: string
569 ;; + `description' :: Description text for the current data.
570 ;; - category :: option
571 ;; - type :: string
573 ;; + `email' :: Author's email.
574 ;; - category :: option
575 ;; - type :: string
577 ;; + `exclude-tags' :: Tags for exclusion of subtrees from export
578 ;; process.
579 ;; - category :: option
580 ;; - type :: list of strings
582 ;; + `footnotes-labels-alist' :: Alist between footnote labels and
583 ;; their definition, as parsed data. Once retrieved, the
584 ;; definition should be exported with `org-export-data'.
585 ;; - category :: option
586 ;; - type :: alist (STRING . LIST)
588 ;; + `genealogy' :: List of current element's parents types.
589 ;; - category :: local
590 ;; - type :: list of symbols
591 ;; - update :: `org-export-update-info'
593 ;; + `headline-alist' :: Alist between headlines raw name and their
594 ;; boundaries. It is used to resolve "fuzzy" links
595 ;; (cf. `org-export-resolve-fuzzy-link').
596 ;; - category :: persistent
597 ;; - type :: alist (STRING INTEGER INTEGER)
599 ;; + `headline-levels' :: Maximum level being exported as an
600 ;; headline. Comparison is done with the relative level of
601 ;; headlines in the parse tree, not necessarily with their
602 ;; actual level.
603 ;; - category :: option
604 ;; - type :: integer
606 ;; + `headline-offset' :: Difference between relative and real level
607 ;; of headlines in the parse tree. For example, a value of -1
608 ;; means a level 2 headline should be considered as level
609 ;; 1 (cf. `org-export-get-relative-level').
610 ;; - category :: persistent
611 ;; - type :: integer
613 ;; + `included-files' :: List of files, with full path, included in
614 ;; the current buffer, through the "#+include:" keyword. It is
615 ;; mainly used to verify that no infinite recursive inclusion
616 ;; happens.
617 ;; - category :: persistent
618 ;; - type :: list of strings
620 ;; + `inherited-properties' :: Properties of the headline ancestors
621 ;; of the current element or object. Those from the closest
622 ;; headline have precedence over the others.
623 ;; - category :: local
624 ;; - type :: plist
626 ;; + `keywords' :: List of keywords attached to data.
627 ;; - category :: option
628 ;; - type :: string
630 ;; + `language' :: Default language used for translations.
631 ;; - category :: option
632 ;; - type :: string
634 ;; + `parent-properties' :: Properties of the parent element.
635 ;; - category :: local
636 ;; - type :: plist
637 ;; - update :: `org-export-update-info'
639 ;; + `parse-tree' :: Whole parse tree, available at any time during
640 ;; transcoding.
641 ;; - category :: global
642 ;; - type :: list (as returned by `org-element-parse-buffer')
644 ;; + `point-max' :: Last ending position in the parse tree.
645 ;; - category :: global
646 ;; - type :: integer
648 ;; + `preserve-breaks' :: Non-nil means transcoding should preserve
649 ;; all line breaks.
650 ;; - category :: option
651 ;; - type :: symbol (nil, t)
653 ;; + `previous-element' :: Previous element's type at the same
654 ;; level.
655 ;; - category :: local
656 ;; - type :: symbol
657 ;; - update :: `org-export-update-info'
659 ;; + `previous-object' :: Previous object type (or `plain-text') at
660 ;; the same level.
661 ;; - category :: local
662 ;; - type :: symbol
663 ;; - update :: `org-export-update-info'
665 ;; + `previous-section-number' :: Numbering of the previous
666 ;; headline. As it might not be practical for direct use, the
667 ;; function `org-export-get-headline-level' is provided
668 ;; to extract useful information out of it.
669 ;; - category :: local
670 ;; - type :: vector
672 ;; + `section-numbers' :: Non-nil means transcoding should add
673 ;; section numbers to headlines.
674 ;; - category :: option
675 ;; - type :: symbol (nil, t)
677 ;; + `seen-footnote-labels' :: List of already transcoded footnote
678 ;; labels.
679 ;; - category :: persistent
680 ;; - type :: list of strings
681 ;; - update :: `org-export-update-info'
683 ;; + `select-tags' :: List of tags enforcing inclusion of sub-trees in
684 ;; transcoding. When such a tag is present,
685 ;; subtrees without it are de facto excluded from
686 ;; the process. See `use-select-tags'.
687 ;; - category :: option
688 ;; - type :: list of strings
690 ;; + `target-list' :: List of targets raw names encoutered in the
691 ;; parse tree. This is used to partly resolve
692 ;; "fuzzy" links
693 ;; (cf. `org-export-resolve-fuzzy-link').
694 ;; - category :: persistent
695 ;; - type :: list of strings
697 ;; + `time-stamp-file' :: Non-nil means transcoding should insert
698 ;; a time stamp in the output.
699 ;; - category :: option
700 ;; - type :: symbol (nil, t)
702 ;; + `total-loc' :: Contains total lines of code accumulated by source
703 ;; blocks with the "+n" option so far.
704 ;; - category :: option
705 ;; - type :: integer
706 ;; - update :: `org-export-handle-code'
708 ;; + `use-select-tags' :: When non-nil, a select tags has been found
709 ;; in the parse tree. Thus, any headline without one will be
710 ;; filtered out. See `select-tags'.
711 ;; - category :: persistent
712 ;; - type :: interger or nil
714 ;; + `with-archived-trees' :: Non-nil when archived subtrees should
715 ;; also be transcoded. If it is set to the `headline' symbol,
716 ;; only the archived headline's name is retained.
717 ;; - category :: option
718 ;; - type :: symbol (nil, t, `headline')
720 ;; + `with-author' :: Non-nil means author's name should be included
721 ;; in the output.
722 ;; - category :: option
723 ;; - type :: symbol (nil, t)
725 ;; + `with-creator' :: Non-nild means a creation sentence should be
726 ;; inserted at the end of the transcoded string. If the value
727 ;; is `comment', it should be commented.
728 ;; - category :: option
729 ;; - type :: symbol (`comment', nil, t)
731 ;; + `with-drawers' :: Non-nil means drawers should be exported. If
732 ;; its value is a list of names, only drawers with such names
733 ;; will be transcoded.
734 ;; - category :: option
735 ;; - type :: symbol (nil, t) or list of strings
737 ;; + `with-email' :: Non-nil means output should contain author's
738 ;; email.
739 ;; - category :: option
740 ;; - type :: symbol (nil, t)
742 ;; + `with-emphasize' :: Non-nil means emphasized text should be
743 ;; interpreted.
744 ;; - category :: option
745 ;; - type :: symbol (nil, t)
747 ;; + `with-fixed-width' :: Non-nil if transcoder should interpret
748 ;; strings starting with a colon as a fixed-with (verbatim)
749 ;; area.
750 ;; - category :: option
751 ;; - type :: symbol (nil, t)
753 ;; + `with-footnotes' :: Non-nil if transcoder should interpret
754 ;; footnotes.
755 ;; - category :: option
756 ;; - type :: symbol (nil, t)
758 ;; + `with-priority' :: Non-nil means transcoding should include
759 ;; priority cookies.
760 ;; - category :: option
761 ;; - type :: symbol (nil, t)
763 ;; + `with-special-strings' :: Non-nil means transcoding should
764 ;; interpret special strings in plain text.
765 ;; - category :: option
766 ;; - type :: symbol (nil, t)
768 ;; + `with-sub-superscript' :: Non-nil means transcoding should
769 ;; interpret subscript and superscript. With a value of "{}",
770 ;; only interpret those using curly brackets.
771 ;; - category :: option
772 ;; - type :: symbol (nil, {}, t)
774 ;; + `with-tables' :: Non-nil means transcoding should interpret
775 ;; tables.
776 ;; - category :: option
777 ;; - type :: symbol (nil, t)
779 ;; + `with-tags' :: Non-nil means transcoding should keep tags in
780 ;; headlines. A `not-in-toc' value will remove them
781 ;; from the table of contents, if any, nonetheless.
782 ;; - category :: option
783 ;; - type :: symbol (nil, t, `not-in-toc')
785 ;; + `with-tasks' :: Non-nil means transcoding should include
786 ;; headlines with a TODO keyword. A `todo' value
787 ;; will only include headlines with a todo type
788 ;; keyword while a `done' value will do the
789 ;; contrary. If a list of strings is provided, only
790 ;; tasks with keywords belonging to that list will
791 ;; be kept.
792 ;; - category :: option
793 ;; - type :: symbol (t, todo, done, nil) or list of strings
795 ;; + `with-timestamps' :: Non-nil means transcoding should include
796 ;; time stamps and associated keywords. Otherwise, completely
797 ;; remove them.
798 ;; - category :: option
799 ;; - type :: symbol: (t, nil)
801 ;; + `with-toc' :: Non-nil means that a table of contents has to be
802 ;; added to the output. An integer value limits its
803 ;; depth.
804 ;; - category :: option
805 ;; - type :: symbol (nil, t or integer)
807 ;; + `with-todo-keywords' :: Non-nil means transcoding should
808 ;; include TODO keywords.
809 ;; - category :: option
810 ;; - type :: symbol (nil, t)
812 ;;;; Export Options
814 ;; Export options come from five sources, in increasing precedence
815 ;; order:
817 ;; - Global variables,
818 ;; - External options provided at export time,
819 ;; - Options keyword symbols,
820 ;; - Buffer keywords,
821 ;; - Subtree properties.
823 ;; The central internal function with regards to export options is
824 ;; `org-export-collect-options'. It updates global variables with
825 ;; "#+BIND:" keywords, then retrieve and prioritize properties from
826 ;; the different sources.
828 ;; The internal functions doing the retrieval are:
829 ;; `org-export-parse-option-keyword' ,
830 ;; `org-export-get-subtree-options' ,
831 ;; `org-export-get-inbuffer-options' and
832 ;; `org-export-get-global-options'.
834 ;; Some properties do not rely on the previous sources but still
835 ;; depend on the original buffer are taken care of in
836 ;; `org-export-initial-options'.
838 ;; Also, `org-export-confirm-letbind' and `org-export-install-letbind'
839 ;; take care of the part relative to "#+BIND:" keywords.
841 (defun org-export-collect-options (backend subtreep ext-plist)
842 "Collect export options from the current buffer.
844 BACKEND is a symbol specifying the back-end to use.
846 When SUBTREEP is non-nil, assume the export is done against the
847 current sub-tree.
849 EXT-PLIST is a property list with external parameters overriding
850 org-mode's default settings, but still inferior to file-local
851 settings."
852 ;; First install #+BIND variables.
853 (org-export-install-letbind-maybe)
854 ;; Get and prioritize export options...
855 (let ((options (org-combine-plists
856 ;; ... from global variables...
857 (org-export-get-global-options backend)
858 ;; ... from an external property list...
859 ext-plist
860 ;; ... from in-buffer settings...
861 (org-export-get-inbuffer-options
862 (org-with-wide-buffer (buffer-string)) backend
863 (and buffer-file-name
864 (org-remove-double-quotes buffer-file-name)))
865 ;; ... and from subtree, when appropriate.
866 (and subtreep
867 (org-export-get-subtree-options)))))
868 ;; Add initial options.
869 (setq options (append (org-export-initial-options options)
870 options))
871 ;; Set a default title if none has been specified so far.
872 (unless (plist-get options :title)
873 (setq options (plist-put options :title
874 (or (and buffer-file-name
875 (file-name-sans-extension
876 (file-name-nondirectory
877 buffer-file-name)))
878 (buffer-name)))))
879 ;; Return plist.
880 options))
882 (defun org-export-parse-option-keyword (options backend)
883 "Parse an OPTIONS line and return values as a plist.
884 BACKEND is a symbol specifying the back-end to use."
885 (let* ((all (append org-export-option-alist
886 (let ((var (intern
887 (format "org-%s-option-alist" backend))))
888 (and (boundp var) (eval var)))))
889 ;; Build an alist between #+OPTION: item and property-name.
890 (alist (delq nil
891 (mapcar (lambda (e)
892 (when (nth 2 e) (cons (regexp-quote (nth 2 e))
893 (car e))))
894 all)))
895 plist)
896 (mapc (lambda (e)
897 (when (string-match (concat "\\(\\`\\|[ \t]\\)"
898 (car e)
899 ":\\(([^)\n]+)\\|[^ \t\n\r;,.]*\\)")
900 options)
901 (setq plist (plist-put plist
902 (cdr e)
903 (car (read-from-string
904 (match-string 2 options)))))))
905 alist)
906 plist))
908 (defun org-export-get-subtree-options ()
909 "Get export options in subtree at point.
910 Return the options as a plist."
911 (org-with-wide-buffer
912 (when (ignore-errors (org-back-to-heading t))
913 (let (prop plist)
914 (when (setq prop (progn (looking-at org-todo-line-regexp)
915 (or (org-entry-get (point) "EXPORT_TITLE")
916 (org-match-string-no-properties 3))))
917 (setq plist (plist-put plist :title prop)))
918 (when (setq prop (org-entry-get (point) "EXPORT_TEXT"))
919 (setq plist (plist-put plist :text prop)))
920 (when (setq prop (org-entry-get (point) "EXPORT_AUTHOR"))
921 (setq plist (plist-put plist :author prop)))
922 (when (setq prop (org-entry-get (point) "EXPORT_DATE"))
923 (setq plist (plist-put plist :date prop)))
924 (when (setq prop (org-entry-get (point) "EXPORT_OPTIONS"))
925 (setq plist (org-export-add-options-to-plist plist prop)))
926 plist))))
928 (defun org-export-get-inbuffer-options (buffer-string backend files)
929 "Return in-buffer options as a plist.
930 BUFFER-STRING is the string of the buffer. BACKEND is a symbol
931 specifying which back-end should be used."
932 (let ((case-fold-search t) plist)
933 ;; 1. Special keywords, as in `org-export-special-keywords'.
934 (let ((start 0)
935 (special-re (org-make-options-regexp org-export-special-keywords)))
936 (while (string-match special-re buffer-string start)
937 (setq start (match-end 0))
938 (let ((key (upcase (org-match-string-no-properties 1 buffer-string)))
939 ;; Special keywords do not have their value expanded.
940 (val (org-match-string-no-properties 2 buffer-string)))
941 (setq plist
942 (org-combine-plists
943 (cond
944 ((string= key "SETUP_FILE")
945 (let ((file (expand-file-name
946 (org-remove-double-quotes (org-trim val)))))
947 ;; Avoid circular dependencies.
948 (unless (member file files)
949 (org-export-get-inbuffer-options
950 (org-file-contents file 'noerror)
951 backend
952 (cons file files)))))
953 ((string= key "OPTIONS")
954 (org-export-parse-option-keyword val backend))
955 ((string= key "MACRO")
956 (string-match "^\\([-a-zA-Z0-9_]+\\)[ \t]+\\(.*?[ \t]*$\\)"
957 val)
958 (plist-put nil
959 (intern (concat ":macro-"
960 (downcase (match-string 1 val))))
961 (match-string 2 val))))
962 plist)))))
963 ;; 2. Standard options, as in `org-export-option-alist'.
964 (let* ((all (append org-export-option-alist
965 (let ((var (intern
966 (format "org-%s-option-alist" backend))))
967 (and (boundp var) (eval var)))))
968 ;; Build alist between keyword name and property name.
969 (alist (delq nil (mapcar (lambda (e)
970 (when (nth 1 e) (cons (nth 1 e) (car e))))
971 all)))
972 ;; Build regexp matching all keywords associated to export
973 ;; options. Note: the search is case insensitive.
974 (opt-re (org-make-options-regexp
975 (delq nil (mapcar (lambda (e) (nth 1 e)) all))))
976 (start 0))
977 (while (string-match opt-re buffer-string start)
978 (setq start (match-end 0))
979 (let* ((key (upcase (org-match-string-no-properties 1 buffer-string)))
980 ;; Expand value, applying restrictions for keywords.
981 (val (org-match-string-no-properties 2 buffer-string))
982 (prop (cdr (assoc key alist)))
983 (behaviour (nth 4 (assq prop all))))
984 (setq plist
985 (plist-put
986 plist prop
987 ;; Handle value depending on specified BEHAVIOUR.
988 (case behaviour
989 (space (if (plist-get plist prop)
990 (concat (plist-get plist prop) " " (org-trim val))
991 (org-trim val)))
992 (newline (org-trim
993 (concat
994 (plist-get plist prop) "\n" (org-trim val))))
995 (split `(,@(plist-get plist prop) ,@(org-split-string val)))
996 ('t val)
997 (otherwise (plist-get plist prop)))))))
998 ;; Parse keywords specified in `org-element-parsed-keywords'.
999 (mapc
1000 (lambda (key)
1001 (let* ((prop (cdr (assoc (upcase key) alist)))
1002 (value (and prop (plist-get plist prop))))
1003 (when (stringp value)
1004 (setq plist
1005 (plist-put
1006 plist prop
1007 (org-element-parse-secondary-string
1008 value
1009 (cdr (assq 'keyword org-element-string-restrictions))))))))
1010 org-element-parsed-keywords))
1011 ;; Return final value.
1012 plist))
1014 (defun org-export-get-global-options (backend)
1015 "Return global export options as a plist.
1016 BACKEND is a symbol specifying which back-end should be used."
1017 (let ((all (append org-export-option-alist
1018 (let ((var (intern
1019 (format "org-%s-option-alist" backend))))
1020 (and (boundp var) (eval var)))))
1021 ;; Output value.
1022 plist)
1023 (mapc (lambda (cell)
1024 (setq plist
1025 (plist-put plist (car cell) (eval (nth 3 cell)))))
1026 all)
1027 ;; Return value.
1028 plist))
1030 (defun org-export-initial-options (options)
1031 "Return a plist with non-optional properties.
1032 OPTIONS is the export options plist computed so far."
1033 (list
1034 :macro-date "(eval (format-time-string \"$1\"))"
1035 :macro-time "(eval (format-time-string \"$1\"))"
1036 :macro-property "(eval (org-entry-get nil \"$1\" 'selective))"
1037 :macro-modification-time
1038 (and (buffer-file-name)
1039 (file-exists-p (buffer-file-name))
1040 (concat "(eval (format-time-string \"$1\" '"
1041 (prin1-to-string (nth 5 (file-attributes (buffer-file-name))))
1042 "))"))
1043 :macro-input-file (and (buffer-file-name)
1044 (file-name-nondirectory (buffer-file-name)))
1045 :footnotes-labels-alist
1046 (let (alist)
1047 (org-with-wide-buffer
1048 (goto-char (point-min))
1049 (while (re-search-forward org-footnote-definition-re nil t)
1050 (let ((def (org-footnote-at-definition-p)))
1051 (org-skip-whitespace)
1052 (push (cons (car def)
1053 (save-restriction
1054 (narrow-to-region (point) (nth 2 def))
1055 (org-element-parse-buffer)))
1056 alist)))
1057 alist))))
1059 (defvar org-export-allow-BIND-local nil)
1060 (defun org-export-confirm-letbind ()
1061 "Can we use #+BIND values during export?
1062 By default this will ask for confirmation by the user, to divert
1063 possible security risks."
1064 (cond
1065 ((not org-export-allow-BIND) nil)
1066 ((eq org-export-allow-BIND t) t)
1067 ((local-variable-p 'org-export-allow-BIND-local) org-export-allow-BIND-local)
1068 (t (org-set-local 'org-export-allow-BIND-local
1069 (yes-or-no-p "Allow BIND values in this buffer? ")))))
1071 (defun org-export-install-letbind-maybe ()
1072 "Install the values from #+BIND lines as local variables.
1073 Variables must be installed before in-buffer options are
1074 retrieved."
1075 (let (letbind pair)
1076 (org-with-wide-buffer
1077 (goto-char (point-min))
1078 (while (re-search-forward (org-make-options-regexp '("BIND")) nil t)
1079 (when (org-export-confirm-letbind)
1080 (push (read (concat "(" (org-match-string-no-properties 2) ")"))
1081 letbind))))
1082 (while (setq pair (pop letbind))
1083 (org-set-local (car pair) (nth 1 pair)))))
1086 ;;;; Persistent Properties
1088 ;; Persistent properties are declared in
1089 ;; `org-export-persistent-properties-list' variable. Most of them are
1090 ;; initialized at the beginning of the transcoding process by
1091 ;; `org-export-initialize-persistent-properties'. The others are
1092 ;; updated during that process.
1094 ;; Dedicated functions focus on computing the value of specific
1095 ;; persistent properties during initialization. Thus,
1096 ;; `org-export-use-select-tag-p' determines if an headline makes use
1097 ;; of an export tag enforcing inclusion. `org-export-get-min-level'
1098 ;; gets the minimal exportable level, used as a basis to compute
1099 ;; relative level for headlines. Eventually,
1100 ;; `org-export-get-point-max' returns the maximum exportable ending
1101 ;; position in the parse tree.
1103 (defconst org-export-persistent-properties-list
1104 '(:babel-data-alist :babel-info-alist :babel-results-alist :code-refs
1105 :headline-offset :parse-tree :point-max
1106 :seen-footnote-labels :total-loc :use-select-tags)
1107 "List of persistent properties.")
1109 (defconst org-export-persistent-properties nil
1110 "Used internally to store properties and values during transcoding.
1112 Only properties that should survive recursion are saved here.
1114 This variable is reset before each transcoding.")
1116 (defun org-export-initialize-persistent-properties (data options backend)
1117 "Initialize `org-export-persistent-properties'.
1119 DATA is the parse tree from which information is retrieved.
1120 OPTIONS is a list holding export options. BACKEND is the
1121 back-end called for transcoding, as a symbol.
1123 Following initial persistent properties are set:
1124 `:back-end' Back-end used for transcoding.
1126 `:headline-alist' Alist of all headlines' name as key and a list
1127 holding beginning and ending positions as
1128 value.
1130 `:headline-offset' Offset between true level of headlines and
1131 local level. An offset of -1 means an headline
1132 of level 2 should be considered as a level
1133 1 headline in the context.
1135 `:parse-tree' Whole parse tree.
1137 `:point-max' Last position in the parse tree
1139 `:target-list' List of all targets' raw name in the parse tree.
1141 `:use-select-tags' Non-nil when parsed tree use a special tag to
1142 enforce transcoding of the headline."
1143 ;; First delete any residual persistent property.
1144 (setq org-export-persistent-properties nil)
1145 ;; Immediately after, set `:use-select-tags' property, as it will be
1146 ;; required for further computations.
1147 (setq options
1148 (org-export-set-property
1149 options
1150 :use-select-tags
1151 (org-export-use-select-tags-p data options)))
1152 ;; Get the rest of the initial persistent properties, now
1153 ;; `:use-select-tags' is set...
1154 ;; 1. `:parse-tree' ...
1155 (setq options (org-export-set-property options :parse-tree data))
1156 ;; 2. `:headline-offset' ...
1157 (setq options
1158 (org-export-set-property
1159 options :headline-offset
1160 (- 1 (org-export-get-min-level data options))))
1161 ;; 3. `:point-max' ...
1162 (setq options (org-export-set-property
1163 options :point-max
1164 (org-export-get-point-max data options)))
1165 ;; 4. `:target-list'...
1166 (setq options (org-export-set-property
1167 options :target-list
1168 (org-element-map
1169 data 'target
1170 (lambda (target info)
1171 (org-element-get-property :raw-value target)))))
1172 ;; 5. `:headline-alist'
1173 (setq options (org-export-set-property
1174 options :headline-alist
1175 (org-element-map
1176 data 'headline
1177 (lambda (headline info)
1178 (list (org-element-get-property :raw-value headline)
1179 (org-element-get-property :begin headline)
1180 (org-element-get-property :end headline))))))
1181 ;; 6. `:back-end'
1182 (setq options (org-export-set-property options :back-end backend)))
1184 (defun org-export-use-select-tags-p (data options)
1185 "Non-nil when data use a tag enforcing transcoding.
1186 DATA is parsed data as returned by `org-element-parse-buffer'.
1187 OPTIONS is a plist holding export options."
1188 (org-element-map
1189 data
1190 'headline
1191 (lambda (headline info)
1192 (let ((tags (org-element-get-property :with-tags headline)))
1193 (and tags (string-match
1194 (format ":%s:" (plist-get info :select-tags)) tags))))
1195 options
1196 'stop-at-first-match))
1198 (defun org-export-get-min-level (data options)
1199 "Return minimum exportable headline's level in DATA.
1200 DATA is parsed tree as returned by `org-element-parse-buffer'.
1201 OPTIONS is a plist holding export options."
1202 (catch 'exit
1203 (let ((min-level 10000))
1204 (mapc (lambda (blob)
1205 (when (and (eq (car blob) 'headline)
1206 (not (org-export-skip-p blob options)))
1207 (setq min-level
1208 (min (org-element-get-property :level blob) min-level)))
1209 (when (= min-level 1) (throw 'exit 1)))
1210 (org-element-get-contents data))
1211 ;; If no headline was found, for the sake of consistency, set
1212 ;; minimum level to 1 nonetheless.
1213 (if (= min-level 10000) 1 min-level))))
1215 (defun org-export-get-point-max (data options)
1216 "Return last exportable ending position in DATA.
1217 DATA is parsed tree as returned by `org-element-parse-buffer'.
1218 OPTIONS is a plist holding export options."
1219 (let ((pos-max 1))
1220 (mapc (lambda (blob)
1221 (unless (and (eq (car blob) 'headline)
1222 (org-export-skip-p blob options))
1223 (setq pos-max (org-element-get-property :end blob))))
1224 (org-element-get-contents data))
1225 pos-max))
1228 ;;;; Properties Management
1230 ;; This is mostly done with the help of two functions. On the one
1231 ;; hand `org-export-update-info' is used to keep up-to-date local
1232 ;; information while walking the nested list representing the parsed
1233 ;; document. On the other end, `org-export-set-property' handles
1234 ;; properties modifications according to their type (persistent or
1235 ;; local).
1237 ;; As exceptions, `:code-refs' and `:total-loc' properties are updated
1238 ;; with `org-export-handle-code' function.
1240 (defun org-export-update-info (blob info recursep)
1241 "Update export options depending on context.
1243 BLOB is the element or object being parsed. INFO is the plist
1244 holding the export options.
1246 When RECURSEP is non-nil, assume the following element or object
1247 will be inside the current one.
1249 The following properties are updated:
1250 `genealogy' List of current element's parents
1251 (symbol list).
1252 `inherited-properties' List of inherited properties from
1253 parent headlines (plist).
1254 `parent-properties' List of last element's properties
1255 (plist).
1256 `previous-element' Previous element's type (symbol).
1257 `previous-object' Previous object's type (symbol).
1258 `previous-section-number' Numbering of the previous headline
1259 (vector).
1260 `seen-footnote-labels' List of already parsed footnote
1261 labels (string list)
1263 Return the property list."
1264 (let* ((type (and (not (stringp blob)) (car blob)))
1265 (relative-level (and (eq type 'headline)
1266 (org-export-get-relative-level blob info)))
1267 (current-num (and (eq type 'headline)
1268 (or (plist-get info :previous-section-number)
1269 (make-vector org-export-max-depth 0)))))
1270 (cond
1271 ;; Case 1: We're moving into a recursive blob.
1272 (recursep
1273 (org-combine-plists
1274 info
1275 `(:genealogy ,(cons type (plist-get info :genealogy))
1276 :previous-element nil
1277 :previous-object nil
1278 :parent-properties ,(if (memq type org-element-all-elements)
1279 (nth 1 blob)
1280 (plist-get info :parent-properties))
1281 :inherited-properties
1282 ,(if (eq type 'headline)
1283 (org-combine-plists
1284 (plist-get info :inherited-properties) (nth 1 blob))
1285 (plist-get info :inherited-properties))
1286 :previous-section-number
1287 ,(let ((current-num (copy-sequence current-num)))
1288 (if (not (eq type 'headline))
1289 current-num
1290 (progn (incf (aref current-num (1- relative-level)))
1291 current-num))))
1292 ;; Add persistent properties.
1293 org-export-persistent-properties))
1294 ;; Case 2: No recursion.
1296 ;; At a footnote reference: mark its label as seen, if not
1297 ;; already the case.
1298 (when (eq type 'footnote-reference)
1299 (let ((label (org-element-get-property :label blob))
1300 (seen-labels (plist-get org-export-persistent-properties
1301 :seen-footnote-labels)))
1302 ;; Store anonymous footnotes (nil label) without checking if
1303 ;; another anonymous footnote was seen before.
1304 (unless (and label (member label seen-labels))
1305 (setq info (org-export-set-property
1306 info :seen-footnote-labels (push label seen-labels))))))
1307 ;; At an headline: update section number.
1308 (when (eq type 'headline)
1309 (setq info (org-export-set-property
1310 info :previous-section-number
1311 (progn (incf (aref current-num (1- relative-level)))
1312 current-num))))
1313 ;; Set `:previous-element' or `:previous-object' according to
1314 ;; BLOB.
1315 (setq info (cond ((not type)
1316 (org-export-set-property
1317 info :previous-object 'plain-text))
1318 ((memq type org-element-all-elements)
1319 (org-export-set-property info :previous-element type))
1320 (t (org-export-set-property info :previous-object type))))
1321 ;; Return updated value.
1322 info))))
1324 (defun org-export-set-property (info prop value)
1325 "Set property PROP to VALUE in plist INFO.
1326 Return the new plist."
1327 (when (memq prop org-export-persistent-properties-list)
1328 (setq org-export-persistent-properties
1329 (plist-put org-export-persistent-properties prop value)))
1330 (plist-put info prop value))
1334 ;;; The Transcoder
1336 ;; This function reads Org data (obtained with, i.e.
1337 ;; `org-element-parse-buffer') and transcodes it into a specified
1338 ;; back-end output. It takes care of updating local properties,
1339 ;; filtering out elements or objects according to export options and
1340 ;; organizing the output blank lines and white space are preserved.
1342 ;; Though, this function is inapropriate for secondary strings, which
1343 ;; require a fresh copy of the plist passed as INFO argument. Thus,
1344 ;; `org-export-secondary-string' is provided for that specific task.
1346 ;; Internally, three functions handle the filtering of objects and
1347 ;; elements during the export. More precisely, `org-export-skip-p'
1348 ;; determines if the considered object or element should be ignored
1349 ;; altogether, `org-export-interpret-p' tells which elements or
1350 ;; objects should be seen as real Org syntax and `org-export-expand'
1351 ;; transforms the others back into their original shape.
1353 (defun org-export-data (data backend info)
1354 "Convert DATA to a string into BACKEND format.
1356 DATA is a nested list as returned by `org-element-parse-buffer'.
1358 BACKEND is a symbol among supported exporters.
1360 INFO is a plist holding export options and also used as
1361 a communication channel between elements when walking the nested
1362 list. See `org-export-update-info' function for more
1363 details.
1365 Return transcoded string."
1366 (mapconcat
1367 ;; BLOB can be an element, an object, a string, or nil.
1368 (lambda (blob)
1369 (cond
1370 ((not blob) nil) ((equal blob "") nil)
1371 ;; BLOB is a string. Check if the optional transcoder for plain
1372 ;; text exists, and call it in that case. Otherwise, simply
1373 ;; return string. Also update INFO and call
1374 ;; `org-export-filter-plain-text-functions'.
1375 ((stringp blob)
1376 (setq info (org-export-update-info blob info nil))
1377 (let ((transcoder (intern (format "org-%s-plain-text" backend))))
1378 (org-export-filter-apply-functions
1379 org-export-filter-plain-text-functions
1380 (if (fboundp transcoder) (funcall transcoder blob info) blob)
1381 backend)))
1382 ;; BLOB is an element or an object.
1384 (let* ((type (if (stringp blob) 'plain-text (car blob)))
1385 ;; 1. Determine the appropriate TRANSCODER.
1386 (transcoder
1387 (cond
1388 ;; 1.0 A full Org document is inserted.
1389 ((eq type 'org-data) 'identity)
1390 ;; 1.1. BLOB should be ignored.
1391 ((org-export-skip-p blob info) nil)
1392 ;; 1.2. BLOB shouldn't be transcoded. Interpret it
1393 ;; back into Org syntax.
1394 ((not (org-export-interpret-p blob info))
1395 'org-export-expand)
1396 ;; 1.3. Else apply naming convention.
1397 (t (let ((trans (intern
1398 (format "org-%s-%s" backend type))))
1399 (and (fboundp trans) trans)))))
1400 ;; 2. Compute CONTENTS of BLOB.
1401 (contents
1402 (cond
1403 ;; Case 0. No transcoder defined: ignore BLOB.
1404 ((not transcoder) nil)
1405 ;; Case 1. Transparently export an Org document.
1406 ((eq type 'org-data)
1407 (org-export-data blob backend info))
1408 ;; Case 2. For a recursive object.
1409 ((memq type org-element-recursive-objects)
1410 (org-export-data
1411 blob backend (org-export-update-info blob info t)))
1412 ;; Case 3. For a recursive element.
1413 ((memq type org-element-greater-elements)
1414 ;; Ignore contents of an archived tree
1415 ;; when `:with-archived-trees' is `headline'.
1416 (unless (and
1417 (eq type 'headline)
1418 (eq (plist-get info :with-archived-trees) 'headline)
1419 (org-element-get-property :archivedp blob))
1420 (org-element-normalize-string
1421 (org-export-data
1422 blob backend (org-export-update-info blob info t)))))
1423 ;; Case 4. For a paragraph.
1424 ((eq type 'paragraph)
1425 (let ((paragraph
1426 (org-element-normalize-contents
1427 blob
1428 ;; When normalizing contents of an item or
1429 ;; a footnote definition, ignore first line's
1430 ;; indentation: there is none and it might be
1431 ;; misleading.
1432 (and (not (plist-get info :previous-element))
1433 (let ((parent (car (plist-get info :genealogy))))
1434 (memq parent '(footnote-definition item)))))))
1435 (org-export-data
1436 paragraph
1437 backend
1438 (org-export-update-info blob info t))))))
1439 ;; 3. Transcode BLOB into RESULTS string.
1440 (results (cond
1441 ((not transcoder) nil)
1442 ((eq transcoder 'org-export-expand)
1443 (org-export-data
1444 `(org-data nil ,(funcall transcoder blob contents))
1445 backend info))
1446 (t (funcall transcoder blob contents info)))))
1447 ;; 4. Discard nil results. Otherwise, update INFO, append
1448 ;; the same white space between elements or objects as in
1449 ;; the original buffer, and call appropriate filters.
1450 (when results
1451 (setq info (org-export-update-info blob info nil))
1452 ;; No filter for a full document.
1453 (if (eq type 'org-data)
1454 results
1455 (org-export-filter-apply-functions
1456 (eval (intern (format "org-export-filter-%s-functions" type)))
1457 (if (memq type org-element-all-elements)
1458 (concat
1459 (org-element-normalize-string results)
1460 (make-string (org-element-get-property :post-blank blob) 10))
1461 (concat
1462 results
1463 (make-string
1464 (org-element-get-property :post-blank blob) 32)))
1465 backend)))))))
1466 (org-element-get-contents data) ""))
1468 (defun org-export-secondary-string (secondary backend info)
1469 "Convert SECONDARY string into BACKEND format.
1471 SECONDARY is a nested list as returned by
1472 `org-element-parse-secondary-string'.
1474 BACKEND is a symbol among supported exporters.
1476 INFO is a plist holding export options and also used as
1477 a communication channel between elements when walking the nested
1478 list. See `org-export-update-info' function for more
1479 details.
1481 Return transcoded string."
1482 ;; Make SECONDARY acceptable for `org-export-data'.
1483 (let ((s (if (listp secondary) secondary (list secondary))))
1484 (org-export-data `(org-data nil ,@s) backend (copy-sequence info))))
1486 (defun org-export-skip-p (blob info)
1487 "Non-nil when element or object BLOB should be skipped during export.
1488 INFO is the plist holding export options."
1489 ;; Check headline.
1490 (unless (stringp blob)
1491 (case (car blob)
1492 ('headline
1493 (let ((with-tasks (plist-get info :with-tasks))
1494 (todo (org-element-get-property :todo-keyword blob))
1495 (todo-type (org-element-get-property :todo-type blob))
1496 (archived (plist-get info :with-archived-trees))
1497 (tag-list (let ((tags (org-element-get-property :tags blob)))
1498 (and tags (org-split-string tags ":")))))
1500 ;; Ignore subtrees with an exclude tag.
1501 (loop for k in (plist-get info :exclude-tags)
1502 thereis (member k tag-list))
1503 ;; Ignore subtrees without a select tag, when such tag is found
1504 ;; in the buffer.
1505 (and (plist-get info :use-select-tags)
1506 (loop for k in (plist-get info :select-tags)
1507 never (member k tag-list)))
1508 ;; Ignore commented sub-trees.
1509 (org-element-get-property :commentedp blob)
1510 ;; Ignore archived subtrees if `:with-archived-trees' is nil.
1511 (and (not archived) (org-element-get-property :archivedp blob))
1512 ;; Ignore tasks, if specified by `:with-tasks' property.
1513 (and todo (not with-tasks))
1514 (and todo
1515 (memq with-tasks '(todo done))
1516 (not (eq todo-type with-tasks)))
1517 (and todo
1518 (consp with-tasks)
1519 (not (member todo with-tasks))))))
1520 ;; Check time-stamp.
1521 ('time-stamp (not (plist-get info :with-timestamps)))
1522 ;; Check drawer.
1523 ('drawer
1524 (or (not (plist-get info :with-drawers))
1525 (and (consp (plist-get info :with-drawers))
1526 (not (member (org-element-get-property :drawer-name blob)
1527 (plist-get info :with-drawers))))))
1528 ;; Check export snippet.
1529 ('export-snippet
1530 (let* ((raw-back-end (org-element-get-property :back-end blob))
1531 (true-back-end
1532 (or (cdr (assoc raw-back-end org-export-snippet-translation-alist))
1533 raw-back-end)))
1534 (not (string= (symbol-name (plist-get info :back-end))
1535 true-back-end)))))))
1537 (defun org-export-interpret-p (blob info)
1538 "Non-nil if element or object BLOB should be interpreted as Org syntax.
1539 Check is done according to export options INFO, stored as
1540 a plist."
1541 (case (car blob)
1542 ;; ... entities...
1543 (entity (plist-get info :with-entities))
1544 ;; ... emphasis...
1545 (emphasis (plist-get info :with-emphasize))
1546 ;; ... fixed-width areas.
1547 (fixed-width (plist-get info :with-fixed-width))
1548 ;; ... footnotes...
1549 ((footnote-definition footnote-reference)
1550 (plist-get info :with-footnotes))
1551 ;; ... sub/superscripts...
1552 ((subscript superscript)
1553 (let ((sub/super-p (plist-get info :with-sub-superscript)))
1554 (if (eq sub/super-p '{})
1555 (org-element-get-property :use-brackets-p blob)
1556 sub/super-p)))
1557 ;; ... tables...
1558 (table (plist-get info :with-tables))
1559 (otherwise t)))
1561 (defsubst org-export-expand (blob contents)
1562 "Expand a parsed element or object to its original state.
1563 BLOB is either an element or an object. CONTENTS is its
1564 contents, as a string or nil."
1565 (funcall
1566 (intern (format "org-element-%s-interpreter" (car blob))) blob contents))
1570 ;;; The Filter System
1572 ;; Filters allow end-users to tweak easily the transcoded output.
1573 ;; They are the functional counterpart of hooks, as every filter in
1574 ;; a set is applied to the return value of the previous one.
1576 ;; Every set is back-end agnostic. Although, a filter is always
1577 ;; called, in addition to the string it applies to, with the back-end
1578 ;; used as argument, so it's easy enough for the end-user to add
1579 ;; back-end specific filters in the set.
1581 ;; Filters sets are defined below. There are of four types:
1583 ;; - `org-export-filter-parse-tree-functions' applies directly on the
1584 ;; complete parsed tree. It's the only filters set that doesn't
1585 ;; apply to a string.
1586 ;; - `org-export-filter-final-output-functions' applies to the final
1587 ;; transcoded string.
1588 ;; - `org-export-filter-plain-text-functions' applies to any string
1589 ;; not recognized as Org syntax.
1590 ;; - `org-export-filter-TYPE-functions' applies on the string returned
1591 ;; after an element or object of type TYPE has been transcoded.
1593 ;; All filters sets are applied through
1594 ;; `org-export-filter-apply-functions' function. Filters in a set are
1595 ;; applied in reverse order, that is in the order of consing. It
1596 ;; allows developers to be reasonably sure that their filters will be
1597 ;; applied first.
1599 ;;;; Special Filters
1600 (defvar org-export-filter-parse-tree-functions nil
1601 "Filter, or list of filters, applied to the parsed tree.
1602 Each filter is called with two arguments: the parse tree, as
1603 returned by `org-element-parse-buffer', and the back-end as
1604 a symbol. It must return the modified parse tree to transcode.")
1606 (defvar org-export-filter-final-output-functions nil
1607 "Filter, or list of filters, applied to the transcoded string.
1608 Each filter is called with two arguments: the full transcoded
1609 string, and the back-end as a symbol. It must return a string
1610 that will be used as the final export output.")
1612 (defvar org-export-filter-plain-text-functions nil
1613 "Filter, or list of filters, applied to plain text.
1614 Each filter is called with two arguments: a string which contains
1615 no Org syntax, and the back-end as a symbol. It must return
1616 a string or nil.")
1619 ;;;; Elements Filters
1621 (defvar org-export-filter-center-block-functions nil
1622 "Filter, or list of filters, applied to a transcoded center block.
1623 Each filter is called with two arguments: the transcoded center
1624 block, as a string, and the back-end, as a symbol. It must
1625 return a string or nil.")
1627 (defvar org-export-filter-drawer-functions nil
1628 "Filter, or list of filters, applied to a transcoded drawer.
1629 Each filter is called with two arguments: the transcoded drawer,
1630 as a string, and the back-end, as a symbol. It must return
1631 a string or nil.")
1633 (defvar org-export-filter-dynamic-block-functions nil
1634 "Filter, or list of filters, applied to a transcoded dynamic-block.
1635 Each filter is called with two arguments: the transcoded
1636 dynamic-block, as a string, and the back-end, as a symbol. It
1637 must return a string or nil.")
1639 (defvar org-export-filter-headline-functions nil
1640 "Filter, or list of filters, applied to a transcoded headline.
1641 Each filter is called with two arguments: the transcoded
1642 headline, as a string, and the back-end, as a symbol. It must
1643 return a string or nil.")
1645 (defvar org-export-filter-inlinetask-functions nil
1646 "Filter, or list of filters, applied to a transcoded inlinetask.
1647 Each filter is called with two arguments: the transcoded
1648 inlinetask, as a string, and the back-end, as a symbol. It must
1649 return a string or nil.")
1651 (defvar org-export-filter-plain-list-functions nil
1652 "Filter, or list of filters, applied to a transcoded plain-list.
1653 Each filter is called with two arguments: the transcoded
1654 plain-list, as a string, and the back-end, as a symbol. It must
1655 return a string or nil.")
1657 (defvar org-export-filter-item-functions nil
1658 "Filter, or list of filters, applied to a transcoded item.
1659 Each filter is called with two arguments: the transcoded item, as
1660 a string, and the back-end, as a symbol. It must return a string
1661 or nil.")
1663 (defvar org-export-filter-comment-functions nil
1664 "Filter, or list of filters, applied to a transcoded comment.
1665 Each filter is called with two arguments: the transcoded comment,
1666 as a string, and the back-end, as a symbol. It must return
1667 a string or nil.")
1669 (defvar org-export-filter-comment-block-functions nil
1670 "Filter, or list of filters, applied to a transcoded comment-comment.
1671 Each filter is called with two arguments: the transcoded
1672 comment-block, as a string, and the back-end, as a symbol. It
1673 must return a string or nil.")
1675 (defvar org-export-filter-example-block-functions nil
1676 "Filter, or list of filters, applied to a transcoded example-block.
1677 Each filter is called with two arguments: the transcoded
1678 example-block, as a string, and the back-end, as a symbol. It
1679 must return a string or nil.")
1681 (defvar org-export-filter-export-block-functions nil
1682 "Filter, or list of filters, applied to a transcoded export-block.
1683 Each filter is called with two arguments: the transcoded
1684 export-block, as a string, and the back-end, as a symbol. It
1685 must return a string or nil.")
1687 (defvar org-export-filter-fixed-width-functions nil
1688 "Filter, or list of filters, applied to a transcoded fixed-width.
1689 Each filter is called with two arguments: the transcoded
1690 fixed-width, as a string, and the back-end, as a symbol. It must
1691 return a string or nil.")
1693 (defvar org-export-filter-footnote-definition-functions nil
1694 "Filter, or list of filters, applied to a transcoded footnote-definition.
1695 Each filter is called with two arguments: the transcoded
1696 footnote-definition, as a string, and the back-end, as a symbol.
1697 It must return a string or nil.")
1699 (defvar org-export-filter-horizontal-rule-functions nil
1700 "Filter, or list of filters, applied to a transcoded horizontal-rule.
1701 Each filter is called with two arguments: the transcoded
1702 horizontal-rule, as a string, and the back-end, as a symbol. It
1703 must return a string or nil.")
1705 (defvar org-export-filter-keyword-functions nil
1706 "Filter, or list of filters, applied to a transcoded keyword.
1707 Each filter is called with two arguments: the transcoded keyword,
1708 as a string, and the back-end, as a symbol. It must return
1709 a string or nil.")
1711 (defvar org-export-filter-latex-environment-functions nil
1712 "Filter, or list of filters, applied to a transcoded latex-environment.
1713 Each filter is called with two arguments: the transcoded
1714 latex-environment, as a string, and the back-end, as a symbol.
1715 It must return a string or nil.")
1717 (defvar org-export-filter-babel-call-functions nil
1718 "Filter, or list of filters, applied to a transcoded babel-call.
1719 Each filter is called with two arguments: the transcoded
1720 babel-call, as a string, and the back-end, as a symbol. It must
1721 return a string or nil.")
1723 (defvar org-export-filter-paragraph-functions nil
1724 "Filter, or list of filters, applied to a transcoded paragraph.
1725 Each filter is called with two arguments: the transcoded
1726 paragraph, as a string, and the back-end, as a symbol. It must
1727 return a string or nil.")
1729 (defvar org-export-filter-property-drawer-functions nil
1730 "Filter, or list of filters, applied to a transcoded property-drawer.
1731 Each filter is called with two arguments: the transcoded
1732 property-drawer, as a string, and the back-end, as a symbol. It
1733 must return a string or nil.")
1735 (defvar org-export-filter-quote-block-functions nil
1736 "Filter, or list of filters, applied to a transcoded quote block.
1737 Each filter is called with two arguments: the transcoded quote
1738 block, as a string, and the back-end, as a symbol. It must
1739 return a string or nil.")
1741 (defvar org-export-filter-quote-section-functions nil
1742 "Filter, or list of filters, applied to a transcoded quote-section.
1743 Each filter is called with two arguments: the transcoded
1744 quote-section, as a string, and the back-end, as a symbol. It
1745 must return a string or nil.")
1747 (defvar org-export-filter-special-block-functions nil
1748 "Filter, or list of filters, applied to a transcoded special block.
1749 Each filter is called with two arguments: the transcoded special
1750 block, as a string, and the back-end, as a symbol. It must
1751 return a string or nil.")
1753 (defvar org-export-filter-src-block-functions nil
1754 "Filter, or list of filters, applied to a transcoded src-block.
1755 Each filter is called with two arguments: the transcoded
1756 src-block, as a string, and the back-end, as a symbol. It must
1757 return a string or nil.")
1759 (defvar org-export-filter-table-functions nil
1760 "Filter, or list of filters, applied to a transcoded table.
1761 Each filter is called with two arguments: the transcoded table,
1762 as a string, and the back-end, as a symbol. It must return
1763 a string or nil.")
1765 (defvar org-export-filter-verse-block-functions nil
1766 "Filter, or list of filters, applied to a transcoded verse block.
1767 Each filter is called with two arguments: the transcoded verse
1768 block, as a string, and the back-end, as a symbol. It must
1769 return a string or nil.")
1772 ;;;; Objects Filters
1774 (defvar org-export-filter-emphasis-functions nil
1775 "Filter, or list of filters, applied to a transcoded emphasis.
1776 Each filter is called with two arguments: the transcoded
1777 emphasis, as a string, and the back-end, as a symbol. It must
1778 return a string or nil.")
1780 (defvar org-export-filter-entity-functions nil
1781 "Filter, or list of filters, applied to a transcoded entity.
1782 Each filter is called with two arguments: the transcoded entity,
1783 as a string, and the back-end, as a symbol. It must return
1784 a string or nil.")
1786 (defvar org-export-filter-export-snippet-functions nil
1787 "Filter, or list of filters, applied to a transcoded export-snippet.
1788 Each filter is called with two arguments: the transcoded
1789 export-snippet, as a string, and the back-end, as a symbol. It
1790 must return a string or nil.")
1792 (defvar org-export-filter-footnote-reference-functions nil
1793 "Filter, or list of filters, applied to a transcoded footnote-reference.
1794 Each filter is called with two arguments: the transcoded
1795 footnote-reference, as a string, and the back-end, as a symbol.
1796 It must return a string or nil.")
1798 (defvar org-export-filter-inline-babel-call-functions nil
1799 "Filter, or list of filters, applied to a transcoded inline-babel-call.
1800 Each filter is called with two arguments: the transcoded
1801 inline-babel-call, as a string, and the back-end, as a symbol. It
1802 must return a string or nil.")
1804 (defvar org-export-filter-inline-src-block-functions nil
1805 "Filter, or list of filters, applied to a transcoded inline-src-block.
1806 Each filter is called with two arguments: the transcoded
1807 inline-src-block, as a string, and the back-end, as a symbol. It
1808 must return a string or nil.")
1810 (defvar org-export-filter-latex-fragment-functions nil
1811 "Filter, or list of filters, applied to a transcoded latex-fragment.
1812 Each filter is called with two arguments: the transcoded
1813 latex-fragment, as a string, and the back-end, as a symbol. It
1814 must return a string or nil.")
1816 (defvar org-export-filter-line-break-functions nil
1817 "Filter, or list of filters, applied to a transcoded line-break.
1818 Each filter is called with two arguments: the transcoded
1819 line-break, as a string, and the back-end, as a symbol. It must
1820 return a string or nil.")
1822 (defvar org-export-filter-link-functions nil
1823 "Filter, or list of filters, applied to a transcoded link.
1824 Each filter is called with two arguments: the transcoded link, as
1825 a string, and the back-end, as a symbol. It must return a string
1826 or nil.")
1828 (defvar org-export-filter-macro-functions nil
1829 "Filter, or list of filters, applied to a transcoded macro.
1830 Each filter is called with two arguments: the transcoded macro,
1831 as a string, and the back-end, as a symbol. It must return
1832 a string or nil.")
1834 (defvar org-export-filter-radio-target-functions nil
1835 "Filter, or list of filters, applied to a transcoded radio-target.
1836 Each filter is called with two arguments: the transcoded
1837 radio-target, as a string, and the back-end, as a symbol. It
1838 must return a string or nil.")
1840 (defvar org-export-filter-statistics-cookie-functions nil
1841 "Filter, or list of filters, applied to a transcoded statistics-cookie.
1842 Each filter is called with two arguments: the transcoded
1843 statistics-cookie, as a string, and the back-end, as a symbol.
1844 It must return a string or nil.")
1846 (defvar org-export-filter-subscript-functions nil
1847 "Filter, or list of filters, applied to a transcoded subscript.
1848 Each filter is called with two arguments: the transcoded
1849 subscript, as a string, and the back-end, as a symbol. It must
1850 return a string or nil.")
1852 (defvar org-export-filter-superscript-functions nil
1853 "Filter, or list of filters, applied to a transcoded superscript.
1854 Each filter is called with two arguments: the transcoded
1855 superscript, as a string, and the back-end, as a symbol. It must
1856 return a string or nil.")
1858 (defvar org-export-filter-target-functions nil
1859 "Filter, or list of filters, applied to a transcoded target.
1860 Each filter is called with two arguments: the transcoded target,
1861 as a string, and the back-end, as a symbol. It must return
1862 a string or nil.")
1864 (defvar org-export-filter-time-stamp-functions nil
1865 "Filter, or list of filters, applied to a transcoded time-stamp.
1866 Each filter is called with two arguments: the transcoded
1867 time-stamp, as a string, and the back-end, as a symbol. It must
1868 return a string or nil.")
1870 (defvar org-export-filter-verbatim-functions nil
1871 "Filter, or list of filters, applied to a transcoded verbatim.
1872 Each filter is called with two arguments: the transcoded
1873 verbatim, as a string, and the back-end, as a symbol. It must
1874 return a string or nil.")
1876 (defun org-export-filter-apply-functions (filters value backend)
1877 "Call every function in FILTERS with arguments VALUE and BACKEND.
1878 Functions are called in reverse order, to be reasonably sure that
1879 developer-specified filters, if any, are called first."
1880 ;; Ensure FILTERS is a list.
1881 (let ((filters (if (listp filters) (reverse filters) (list filters))))
1882 (loop for filter in filters
1883 if (not value) return nil else
1884 do (setq value (funcall filter value backend))))
1885 value)
1889 ;;; Core functions
1891 ;; This is the room for the main function, `org-export-as', along with
1892 ;; its derivative, `org-export-to-buffer'. They differ only by the
1893 ;; way they output the resulting code.
1895 ;; Note that `org-export-as' doesn't really parse the current buffer,
1896 ;; but a copy of it (with the same buffer-local variables and
1897 ;; visibility), where Babel blocks are executed, if appropriate.
1898 ;; `org-export-with-current-buffer-copy' macro prepares that copy.
1900 (defun org-export-as (backend
1901 &optional subtreep visible-only body-only ext-plist)
1902 "Transcode current Org buffer into BACKEND code.
1904 If narrowing is active in the current buffer, only transcode its
1905 narrowed part.
1907 If a region is active, transcode that region.
1909 When optional argument SUBTREEP is non-nil, transcode the
1910 sub-tree at point, extracting information from the headline
1911 properties first.
1913 When optional argument VISIBLE-ONLY is non-nil, don't export
1914 contents of hidden elements.
1916 When optional argument BODY-ONLY is non-nil, only return body
1917 code, without preamble nor postamble.
1919 EXT-PLIST, when provided, is a property list with external
1920 parameters overriding Org default settings, but still inferior to
1921 file-local settings.
1923 Return code as a string."
1924 (save-excursion
1925 (save-restriction
1926 ;; Narrow buffer to an appropriate region for parsing.
1927 (when (org-region-active-p)
1928 (narrow-to-region (region-beginning) (region-end)))
1929 (goto-char (point-min))
1930 (when subtreep
1931 (unless (org-at-heading-p)
1932 (org-with-limited-levels (outline-next-heading)))
1933 (let ((end (save-excursion (org-end-of-subtree t)))
1934 (begin (progn (forward-line)
1935 (org-skip-whitespace)
1936 (point-at-bol))))
1937 (narrow-to-region begin end)))
1938 ;; Retrieve export options (INFO) and parsed tree (RAW-DATA).
1939 ;; Buffer isn't parsed directly. Instead, a temporary copy is
1940 ;; created, where all code blocks are evaluated. RAW-DATA is
1941 ;; the parsed tree of the buffer resulting from that process.
1942 ;; Eventually call `org-export-filter-parse-tree-functions'..
1943 (let ((info (org-export-collect-options backend subtreep ext-plist))
1944 (raw-data (org-export-filter-apply-functions
1945 org-export-filter-parse-tree-functions
1946 (org-export-with-current-buffer-copy
1947 (org-export-blocks-preprocess)
1948 (org-element-parse-buffer nil visible-only))
1949 backend)))
1950 ;; Initialize the communication system and combine it to INFO.
1951 (setq info
1952 (org-combine-plists
1953 info
1954 (org-export-initialize-persistent-properties
1955 raw-data info backend)))
1956 ;; Now transcode RAW-DATA. Also call
1957 ;; `org-export-filter-final-output-functions'.
1958 (let ((body (org-element-normalize-string
1959 (org-export-data raw-data backend info)))
1960 (template (intern (format "org-%s-template" backend))))
1961 (if (and (not body-only) (fboundp template))
1962 (org-trim
1963 (org-export-filter-apply-functions
1964 org-export-filter-final-output-functions
1965 (funcall template body info)
1966 backend))
1967 (org-export-filter-apply-functions
1968 org-export-filter-final-output-functions body backend)))))))
1970 (defun org-export-to-buffer (backend buffer &optional subtreep visible-only body-only ext-plist)
1971 "Call `org-export-as' with output to a specified buffer.
1973 BACKEND is the back-end used for transcoding, as a symbol.
1975 BUFFER is the output buffer. If it already exists, it will be
1976 erased first, otherwise, it will be created.
1978 Arguments SUBTREEP, VISIBLE-ONLY and BODY-ONLY are similar to
1979 those used in `org-export-as'.
1981 EXT-PLIST, when provided, is a property list with external
1982 parameters overriding Org default settings, but still inferior to
1983 file-local settings.
1985 Return buffer."
1986 (let ((out (org-export-as backend subtreep visible-only body-only ext-plist))
1987 (buffer (get-buffer-create buffer)))
1988 (with-current-buffer buffer
1989 (erase-buffer)
1990 (insert out)
1991 (goto-char (point-min)))
1992 buffer))
1994 (defmacro org-export-with-current-buffer-copy (&rest body)
1995 "Apply BODY in a copy of the current buffer.
1997 The copy preserves local variables and visibility of the original
1998 buffer.
2000 Point is at buffer's beginning when BODY is applied."
2001 (org-with-gensyms (original-buffer offset buffer-string overlays)
2002 `(let ((,original-buffer ,(current-buffer))
2003 (,offset ,(1- (point-min)))
2004 (,buffer-string ,(buffer-string))
2005 (,overlays (mapcar
2006 'copy-overlay (overlays-in (point-min) (point-max)))))
2007 (with-temp-buffer
2008 (let ((buffer-invisibility-spec nil))
2009 (org-clone-local-variables
2010 ,original-buffer "^\\(org-\\|orgtbl-\\|major-mode$\\)")
2011 (insert ,buffer-string)
2012 (mapc (lambda (ov)
2013 (move-overlay
2015 (- (overlay-start ov) ,offset)
2016 (- (overlay-end ov) ,offset)
2017 (current-buffer)))
2018 ,overlays)
2019 (goto-char (point-min))
2020 (progn ,@body))))))
2021 (def-edebug-spec org-export-with-current-buffer-copy (body))
2025 ;;; Tools For Back-Ends
2027 ;; A whole set of tools is available to help build new exporters. Any
2028 ;; function general enough to have its use across many back-ends
2029 ;; should be added here.
2031 ;; As of now, functions operating on headlines, include keywords,
2032 ;; links, macros, src-blocks, tables and tables of contents are
2033 ;; implemented.
2036 ;;;; For Headlines
2038 ;; `org-export-get-relative-level' is a shortcut to get headline
2039 ;; level, relatively to the lower headline level in the parsed tree.
2041 ;; `org-export-get-headline-number' returns the section number of an
2042 ;; headline, while `org-export-number-to-roman' allows to convert it
2043 ;; to roman numbers.
2045 ;; `org-export-first-sibling-p' and `org-export-last-sibling-p' are
2046 ;; two useful predicates when it comes to fulfill the
2047 ;; `:headline-levels' property.
2049 (defun org-export-get-relative-level (headline info)
2050 "Return HEADLINE relative level within current parsed tree.
2051 INFO is a plist holding contextual information."
2052 (+ (org-element-get-property :level headline)
2053 (or (plist-get info :headline-offset) 0)))
2055 (defun org-export-get-headline-number (headline info)
2056 "Return HEADLINE numbering as a list of numbers.
2057 INFO is a plist holding contextual information."
2058 (let ((relative-level (org-export-get-relative-level headline info))
2059 (previous-numbering (or (plist-get info :previous-section-number)
2060 (make-vector org-export-max-depth 0))))
2061 (loop for n across previous-numbering
2062 for i from 1 to relative-level
2063 collect (if (= i relative-level) (1+ n) n))))
2065 (defun org-export-number-to-roman (n)
2066 "Convert integer N into a roman numeral."
2067 (let ((roman '((1000 . "M") (900 . "CM") (500 . "D") (400 . "CD")
2068 ( 100 . "C") ( 90 . "XC") ( 50 . "L") ( 40 . "XL")
2069 ( 10 . "X") ( 9 . "IX") ( 5 . "V") ( 4 . "IV")
2070 ( 1 . "I")))
2071 (res ""))
2072 (if (<= n 0)
2073 (number-to-string n)
2074 (while roman
2075 (if (>= n (caar roman))
2076 (setq n (- n (caar roman))
2077 res (concat res (cdar roman)))
2078 (pop roman)))
2079 res)))
2081 (defun org-export-first-sibling-p (headline info)
2082 "Non-nil when HEADLINE is the first sibling in its sub-tree.
2083 INFO is the plist used as a communication channel."
2084 (not (eq (plist-get info :previous-element) 'headline)))
2086 (defun org-export-last-sibling-p (headline info)
2087 "Non-nil when HEADLINE is the last sibling in its sub-tree.
2088 INFO is the plist used as a communication channel."
2089 (= (org-element-get-property :end headline)
2090 (or (plist-get (plist-get info :parent-properties) :end)
2091 (plist-get info :point-max))))
2094 ;;;; For Include Keywords
2096 ;; This section provides a tool to properly handle insertion of files
2097 ;; during export: `org-export-included-files'. It recursively
2098 ;; transcodes a file specfied by an include keyword.
2100 ;; It uses two helper functions: `org-export-get-file-contents'
2101 ;; returns contents of a file according to parameters specified in the
2102 ;; keyword while `org-export-parse-included-file' parses the file
2103 ;; specified by it.
2105 (defun org-export-included-file (keyword backend info)
2106 "Transcode file specified with include KEYWORD.
2108 KEYWORD is the include keyword element transcoded. BACKEND is
2109 the language back-end used for transcoding. INFO is the plist
2110 used as a communication channel.
2112 This function updates `:included-files' and `:headline-offset'
2113 properties.
2115 Return the transcoded string."
2116 (let ((data (org-export-parse-included-file keyword info))
2117 (file (let ((value (org-element-get-property :value keyword)))
2118 (and (string-match "^\"\\(\\S-+\\)\"" value)
2119 (match-string 1 value)))))
2120 (org-element-normalize-string
2121 (org-export-data
2122 data backend
2123 (org-combine-plists
2124 info
2125 ;; Store full path of already included files to avoid
2126 ;; recursive file inclusion.
2127 `(:included-files
2128 ,(cons (expand-file-name file) (plist-get info :included-files))
2129 ;; Ensure that a top-level headline in the included
2130 ;; file becomes a direct child of the current headline
2131 ;; in the buffer.
2132 :headline-offset
2133 ,(- (+ (plist-get (plist-get info :inherited-properties) :level)
2134 (plist-get info :headline-offset))
2135 (1- (org-export-get-min-level data info)))))))))
2137 (defun org-export-get-file-contents (file &optional lines)
2138 "Get the contents of FILE and return them as a string.
2139 When optional argument LINES is a string specifying a range of
2140 lines, include only those lines."
2141 (with-temp-buffer
2142 (insert-file-contents file)
2143 (when lines
2144 (let* ((lines (split-string lines "-"))
2145 (lbeg (string-to-number (car lines)))
2146 (lend (string-to-number (cadr lines)))
2147 (beg (if (zerop lbeg) (point-min)
2148 (goto-char (point-min))
2149 (forward-line (1- lbeg))
2150 (point)))
2151 (end (if (zerop lend) (point-max)
2152 (goto-char (point-min))
2153 (forward-line (1- lend))
2154 (point))))
2155 (narrow-to-region beg end)))
2156 (buffer-string)))
2158 (defun org-export-parse-included-file (keyword info)
2159 "Parse file specified by include KEYWORD.
2161 KEYWORD is the include keyword element transcoded. BACKEND is the
2162 language back-end used for transcoding. INFO is the plist used as
2163 a communication channel.
2165 Return the parsed tree."
2166 (let* ((value (org-element-get-property :value keyword))
2167 (file (and (string-match "^\"\\(\\S-+\\)\"" value)
2168 (prog1 (match-string 1 value)
2169 (setq value (replace-match "" nil nil value)))))
2170 (lines (and (string-match
2171 ":lines +\"\\(\\(?:[0-9]+\\)?-\\(?:[0-9]+\\)?\\)\"" value)
2172 (prog1 (match-string 1 value)
2173 (setq value (replace-match "" nil nil value)))))
2174 (env (cond ((string-match "\\<example\\>" value) "example")
2175 ((string-match "\\<src\\(?: +\\(.*\\)\\)?" value)
2176 (match-string 1 value)))))
2177 (cond
2178 ((or (not file)
2179 (not (file-exists-p file))
2180 (not (file-readable-p file)))
2181 (format "Cannot include file %s" file))
2182 ((and (not env)
2183 (member (expand-file-name file) (plist-get info :included-files)))
2184 (error "Recursive file inclusion: %S" file))
2185 (t (let ((raw (org-element-normalize-string
2186 (org-export-get-file-contents
2187 (expand-file-name file) lines))))
2188 ;; If environment isn't specified, Insert file in
2189 ;; a temporary buffer and parse it as Org syntax.
2190 ;; Otherwise, build the element representing the file.
2191 (cond
2192 ((not env)
2193 (with-temp-buffer
2194 (insert raw) (org-mode) (org-element-parse-buffer)))
2195 ((string= "example" env)
2196 `(org-data nil (example-block (:value ,raw :post-blank 0))))
2198 `(org-data
2200 (src-block (:value ,raw :language ,env :post-blank 0))))))))))
2203 ;;;; For Links
2205 ;; `org-export-solidify-link-text' turns a string into a safer version
2206 ;; for links, replacing most non-standard characters with hyphens.
2208 ;; `org-export-get-coderef-format' returns an appropriate format
2209 ;; string for coderefs.
2211 ;; `org-export-inline-image-p' returns a non-nil value when the link
2212 ;; provided should be considered as an inline image.
2214 ;; `org-export-resolve-fuzzy-link' searches destination of fuzzy links
2215 ;; (i.e. links with "fuzzy" as type) within the parsed tree, and
2216 ;; returns an appropriate unique identifier when found, or nil.
2218 (defun org-export-solidify-link-text (s)
2219 "Take link text and make a safe target out of it."
2220 (save-match-data
2221 (mapconcat 'identity (org-split-string s "[^a-zA-Z0-9_\\.-]+") "-")))
2223 (defun org-export-get-coderef-format (path desc)
2224 "Return format string for code reference link.
2225 PATH is the link path. DESC is its description."
2226 (save-match-data
2227 (cond ((string-match (regexp-quote (concat "(" path ")")) desc)
2228 (replace-match "%s" t t desc))
2229 ((string= desc "") "%s")
2230 (t desc))))
2232 (defun org-export-inline-image-p (link contents &optional extensions)
2233 "Non-nil if LINK object points to an inline image.
2235 CONTENTS is the link description part, as a string, or nil.
2237 When non-nil, optional argument EXTENSIONS is a list of valid
2238 extensions for image files, as strings. Otherwise, a default
2239 list is provided \(cf. `org-image-file-name-regexp'\)."
2240 (and (or (not contents) (string= contents ""))
2241 (string= (org-element-get-property :type link) "file")
2242 (org-file-image-p
2243 (expand-file-name (org-element-get-property :path link))
2244 extensions)))
2246 (defun org-export-resolve-fuzzy-link (link info)
2247 "Return an unique identifier for LINK destination.
2249 INFO is a plist holding contextual information.
2251 Return value can be a string, an buffer position, or nil:
2253 - If LINK path exactly matches any target, return its name as the
2254 identifier.
2256 - If LINK path exactly matches any headline name, return
2257 headline's beginning position as the identifier. If more than
2258 one headline share that name, priority will be given to the one
2259 with the closest common ancestor, if any, or the first one in
2260 the parse tree otherwise.
2262 - Otherwise, return nil.
2264 Assume LINK type is \"fuzzy\"."
2265 (let ((path (org-element-get-property :path link)))
2266 (if (member path (plist-get info :target-list))
2267 ;; Link points to a target: return its name as a string.
2268 path
2269 ;; Link either points to an headline or nothing. Try to find
2270 ;; the source, with priority given to headlines with the closest
2271 ;; common ancestor. If such candidate is found, return its
2272 ;; beginning position as an unique identifier, otherwise return
2273 ;; nil.
2274 (let* ((head-alist (plist-get info :headline-alist))
2275 (link-begin (org-element-get-property :begin link))
2276 (link-end (org-element-get-property :end link))
2277 ;; Store candidates as a list of cons cells holding their
2278 ;; beginning and ending position.
2279 (cands (loop for head in head-alist
2280 when (string= (car head) path)
2281 collect (cons (nth 1 head) (nth 2 head)))))
2282 (cond
2283 ;; No candidate: return nil.
2284 ((not cands) nil)
2285 ;; If one or more candidates share common ancestors with
2286 ;; LINK, return beginning position of the first one matching
2287 ;; the closer ancestor shared.
2288 ((let ((ancestors (loop for head in head-alist
2289 when (and (> link-begin (nth 1 head))
2290 (<= link-end (nth 2 head)))
2291 collect (cons (nth 1 head) (nth 2 head)))))
2292 (loop named main for ancestor in (nreverse ancestors) do
2293 (loop for candidate in cands
2294 when (and (>= (car candidate) (car ancestor))
2295 (<= (cdr candidate) (cdr ancestor)))
2296 do (return-from main (car candidate))))))
2297 ;; No candidate have a common ancestor with link: First match
2298 ;; will do. Return its beginning position.
2299 (t (caar cands)))))))
2302 ;;;; For Macros
2304 ;; `org-export-expand-macro' simply takes care of expanding macros.
2306 (defun org-export-expand-macro (macro info)
2307 "Expand MACRO and return it as a string.
2308 INFO is a plist holding export options."
2309 (let* ((key (org-element-get-property :key macro))
2310 (args (org-element-get-property :args macro))
2311 (value (plist-get info (intern (format ":macro-%s" key)))))
2312 ;; Replace arguments in VALUE.
2313 (let ((s 0) n)
2314 (while (string-match "\\$\\([0-9]+\\)" value s)
2315 (setq s (1+ (match-beginning 0))
2316 n (string-to-number (match-string 1 value)))
2317 (and (>= (length args) n)
2318 (setq value (replace-match (nth (1- n) args) t t value)))))
2319 ;; VALUE starts with "(eval": it is a s-exp, `eval' it.
2320 (when (string-match "\\`(eval\\>" value)
2321 (setq value (eval (read value))))
2322 ;; Return expanded string.
2323 (format "%s" value)))
2326 ;;;; For Src-Blocks
2328 ;; `org-export-handle-code' takes care of line numbering and reference
2329 ;; cleaning in source code, when appropriate. It also updates global
2330 ;; LOC count (`:total-loc' property) and code references alist
2331 ;; (`:code-refs' property).
2333 (defun org-export-handle-code (code switches info
2334 &optional language num-fmt ref-fmt)
2335 "Handle line numbers and code references in CODE.
2337 CODE is the string to process. SWITCHES is the option string
2338 determining which changes will be applied to CODE. INFO is the
2339 plist used as a communication channel during export.
2341 Optional argument LANGUAGE, when non-nil, is a string specifying
2342 code's language.
2344 If optional argument NUM-FMT is a string, it will be used as
2345 a format string for numbers at beginning of each line.
2347 If optional argument REF-FMT is a string, it will be used as
2348 a format string for each line of code containing a reference.
2350 Update the following INFO properties by side-effect: `:total-loc'
2351 and `:code-refs'.
2353 Return new code as a string."
2354 (let* ((switches (or switches ""))
2355 (numberp (string-match "[-+]n\\>" switches))
2356 (continuep (string-match "\\+n\\>" switches))
2357 (total-LOC (if (and numberp (not continuep))
2359 (or (plist-get info :total-loc) 0)))
2360 (preserve-indent-p (or org-src-preserve-indentation
2361 (string-match "-i\\>" switches)))
2362 (replace-labels (when (string-match "-r\\>" switches)
2363 (if (string-match "-k\\>" switches) 'keep t)))
2364 ;; Get code and clean it. Remove blank lines at its
2365 ;; beginning and end. Also remove protective commas.
2366 (code (let ((c (replace-regexp-in-string
2367 "\\`\\([ \t]*\n\\)+" ""
2368 (replace-regexp-in-string
2369 "\\(:?[ \t]*\n\\)*[ \t]*\\'" "\n" code))))
2370 ;; If appropriate, remove global indentation.
2371 (unless preserve-indent-p (setq c (org-remove-indentation c)))
2372 ;; Free up the protected lines. Note: Org blocks
2373 ;; have commas at the beginning or every line.
2374 (if (string= language "org")
2375 (replace-regexp-in-string "^," "" c)
2376 (replace-regexp-in-string
2377 "^\\(,\\)\\(:?\\*\\|[ \t]*#\\+\\)" "" c nil nil 1))))
2378 ;; Split code to process it line by line.
2379 (code-lines (org-split-string code "\n"))
2380 ;; Ensure line numbers will be correctly padded before
2381 ;; applying the format string.
2382 (num-fmt (format (if (stringp num-fmt) num-fmt "%s: ")
2383 (format "%%%ds"
2384 (length (number-to-string
2385 (+ (length code-lines)
2386 total-LOC))))))
2387 ;; Get format used for references.
2388 (label-fmt (or (and (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
2389 (match-string 1 switches))
2390 org-coderef-label-format))
2391 ;; Build a regexp matching a loc with a reference.
2392 (with-ref-re (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
2393 (replace-regexp-in-string
2394 "%s" "\\([-a-zA-Z0-9_ ]+\\)" label-fmt nil t)))
2395 coderefs)
2396 (org-element-normalize-string
2397 (mapconcat (lambda (loc)
2398 ;; Maybe add line number to current line of code
2399 ;; (LOC).
2400 (when numberp
2401 (setq loc (concat (format num-fmt (incf total-LOC)) loc)))
2402 ;; Take action if at a ref line.
2403 (when (string-match with-ref-re loc)
2404 (let ((ref (match-string 3 loc)))
2405 (setq loc
2406 (cond
2407 ;; Option "-k": don't remove labels. Use
2408 ;; numbers for references when lines are
2409 ;; numbered, use labels otherwise.
2410 ((eq replace-labels 'keep)
2411 (let ((full-ref (format "(%s)" ref)))
2412 (push (cons ref (if numberp total-LOC full-ref))
2413 coderefs)
2414 (replace-match full-ref nil nil loc 2))
2415 (replace-match (format "(%s)" ref) nil nil loc 2))
2416 ;; Option "-r" without "-k": remove labels.
2417 ;; Use numbers for references when lines are
2418 ;; numbered, use labels otherwise.
2419 (replace-labels
2420 (push (cons ref (if numberp total-LOC ref))
2421 coderefs)
2422 (replace-match "" nil nil loc 1))
2423 ;; Else: don't remove labels and don't use
2424 ;; numbers for references.
2426 (let ((full-ref (format "(%s)" ref)))
2427 (push (cons ref full-ref) coderefs)
2428 (replace-match full-ref nil nil loc 2)))))))
2429 ;; If REF-FMT is defined, apply it to current LOC.
2430 (when (stringp ref-fmt) (setq loc (format ref-fmt loc)))
2431 ;; Update by side-effect communication channel.
2432 ;; Return updated LOC.
2433 (setq info (org-export-set-property
2434 (org-export-set-property
2435 info :code-refs coderefs)
2436 :total-loc total-LOC))
2437 loc)
2438 code-lines "\n"))))
2441 ;;;; For Tables
2443 ;; `org-export-table-format-info' extracts formatting information
2444 ;; (alignment, column groups and presence of a special column) from
2445 ;; a raw table and returns it as a property list.
2447 ;; `org-export-clean-table' cleans the raw table from any Org
2448 ;; table-specific syntax.
2450 (defun org-export-table-format-info (table)
2451 "Extract info from TABLE.
2452 Return a plist whose properties and values are:
2453 `:alignment' vector of strings among \"r\", \"l\" and \"c\",
2454 `:column-groups' vector of symbols among `start', `end', `startend',
2455 `:special-column-p' boolean."
2456 (with-temp-buffer
2457 (insert table)
2458 (goto-char 1)
2459 (org-table-align)
2460 (let ((align (vconcat (mapcar (lambda (c) (if c "r" "l"))
2461 org-table-last-alignment)))
2462 (colgroups (make-vector (length org-table-last-alignment) nil))
2463 (special-column-p 'empty))
2464 (mapc (lambda (row)
2465 ;; Determine if a special column is present by looking
2466 ;; for special markers in the first column. More
2467 ;; accurately, the first column is considered special if
2468 ;; it only contains special markers and, maybe, empty
2469 ;; cells.
2470 (unless (string-match "^[ \t]*|[-+]+|[ \t]*$" row)
2471 (setq special-column-p
2472 (cond
2473 ((not special-column-p) nil)
2474 ((string-match "^[ \t]*| *\\\\?\\([\#!$*_^]\\) *|"
2475 row) 'special)
2476 ((string-match "^[ \t]*| +|" row) special-column-p))))
2477 (cond
2478 ((org-table-cookie-line-p row)
2479 ;; Read forced alignment information, if any, and
2480 ;; determine final alignment for the table.
2481 (let ((col 0))
2482 (mapc (lambda (field)
2483 (when (string-match "<\\([lrc]\\)[0-9]*>" field)
2484 (aset align col (match-string 1 field)))
2485 (incf col))
2486 (org-split-string row "[ \t]*|[ \t]*"))))
2487 ;; Read column groups information.
2488 ((org-table-colgroup-line-p row)
2489 (let ((col 0))
2490 (mapc (lambda (field)
2491 (aset colgroups col
2492 (cond ((string= "<" field) 'start)
2493 ((string= ">" field) 'end)
2494 ((string= "<>" field) 'start-end)))
2495 (incf col))
2496 (org-split-string row "[ \t]*|[ \t]*"))))))
2497 (org-split-string table "\n"))
2498 ;; Return plist.
2499 (list :alignment align
2500 :column-groups colgroups
2501 :special-column-p (eq special-column-p 'special)))))
2503 (defun org-export-clean-table (table specialp)
2504 "Clean string TABLE from its formatting elements.
2505 Remove any row containing column groups or formatting cookies and
2506 rows starting with a special marker. If SPECIALP is non-nil,
2507 assume the table contains a special formatting column and remove
2508 it also."
2509 (let ((rows (org-split-string table "\n")))
2510 (mapconcat 'identity
2511 (delq nil
2512 (mapcar
2513 (lambda (row)
2514 (cond
2515 ((org-table-colgroup-line-p row) nil)
2516 ((org-table-cookie-line-p row) nil)
2517 ;; Ignore rows starting with a special marker.
2518 ((string-match "^[ \t]*| *[!_^/] *|" row) nil)
2519 ;; Remove special column.
2520 ((and specialp
2521 (or (string-match "^\\([ \t]*\\)|-+\\+" row)
2522 (string-match "^\\([ \t]*\\)|[^|]*|" row)))
2523 (replace-match "\\1|" t nil row))
2524 (t row)))
2525 rows))
2526 "\n")))
2529 ;;;; For Tables Of Contents
2531 ;; `org-export-get-headlines' builds a table of contents in the shape
2532 ;; of a nested list of cons cells whose car is headline's name and cdr
2533 ;; an unique identifier. One can then easily parse it and transcode
2534 ;; it in a back-end. Identifiers can be used to construct internal
2535 ;; links.
2537 ;; Building lists of tables, figures or listings is quite similar.
2538 ;; Once the generic function `org-export-collect-elements' is defined,
2539 ;; `org-export-collect-tables', `org-export-collect-figures' and
2540 ;; `org-export-collect-listings' can be derived from it.
2542 (defun org-export-get-headlines (backend info &optional n)
2543 "Build a table of contents.
2545 BACKEND is the back-end used to transcode headline's name. INFO
2546 is a plist holding export options.
2548 When non-nil, optional argument N must be an integer. It
2549 specifies the depth of the table of contents.
2551 Return an alist whose keys are headlines' name and value their
2552 relative level and an unique identifier that might be used for
2553 internal links.
2555 For example, on the following tree, where numbers in parens are
2556 buffer position at beginning of the line:
2558 * Title 1 (1)
2559 ** Sub-title 1 (21)
2560 ** Sub-title 2 (42)
2561 * Title 2 (62)
2563 the function will return:
2565 \(\(\"Title 1\" 1 1\)
2566 \(\"Sub-title 1\" 2 21\)
2567 \(\"Sub-title 2\" 2 42\)
2568 \(\"Title 2\" 1 62\)\)"
2569 (org-element-map
2570 (plist-get info :parse-tree)
2571 'headline
2572 (lambda (headline local-info)
2573 ;; Get HEADLINE's relative level.
2574 (let ((level (+ (or (plist-get local-info :headline-offset) 0)
2575 (org-element-get-property :level headline))))
2576 (unless (and (wholenump n) (> level n))
2577 (list
2578 (org-export-secondary-string
2579 (org-element-get-property :title headline) backend info)
2580 level
2581 (org-element-get-property :begin headline)))))
2582 info))
2584 (defun org-export-collect-elements (type backend info)
2585 "Collect named elements of type TYPE.
2587 Only elements with a caption or a name are collected.
2589 BACKEND is the back-end used to transcode their caption or name.
2590 INFO is a plist holding export options.
2592 Return an alist where key is entry's name and value an unique
2593 identifier that might be used for internal links."
2594 (org-element-map
2595 (plist-get info :parse-tree)
2596 type
2597 (lambda (element info)
2598 (let ((entry
2599 (cond
2600 ((org-element-get-property :caption element)
2601 (org-export-secondary-string
2602 (org-element-get-property :caption element) backend info))
2603 ((org-element-get-property :name element)
2604 (org-export-secondary-string
2605 (org-element-get-property :name element) backend info)))))
2606 ;; Skip elements with neither a caption nor a name.
2607 (when entry (cons entry (org-element-get-property :begin element)))))
2608 info))
2610 (defun org-export-collect-tables (backend info)
2611 "Build a list of tables.
2613 BACKEND is the back-end used to transcode table's name. INFO is
2614 a plist holding export options.
2616 Return an alist where key is the caption of the table and value
2617 an unique identifier that might be used for internal links."
2618 (org-export-collect-elements 'table backend info))
2620 (defun org-export-get-figures (backend info)
2621 "Build a list of figures.
2623 A figure is a paragraph type element with a caption or a name.
2625 BACKEND is the back-end used to transcode headline's name. INFO
2626 is a plist holding export options.
2628 Return an alist where key is the caption of the figure and value
2629 an unique indentifier that might be used for internal links."
2630 (org-export-collect-elements 'paragraph backend info))
2632 (defun org-export-collect-listings (backend info)
2633 "Build a list of src blocks.
2635 BACKEND is the back-end used to transcode src block's name. INFO
2636 is a plist holding export options.
2638 Return an alist where key is the caption of the src block and
2639 value an unique indentifier that might be used for internal
2640 links."
2641 (org-export-collect-elements 'src-block backend info))
2644 (provide 'org-export)
2645 ;;; org-export.el ends here