org-agenda.el: small indentation fix.
[org-mode.git] / contrib / lisp / org-export.el
blob7b7649883c59c08fb000b8fb59ad4d8b59447529
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 ;; + `headline-numbering' :: Alist between headlines' beginning
614 ;; position and their numbering, as a list of numbers
615 ;; (cf. `org-export-get-headline-number').
616 ;; - category :: persistent
617 ;; - type :: alist (INTEGER . LIST)
619 ;; + `included-files' :: List of files, with full path, included in
620 ;; the current buffer, through the "#+include:" keyword. It is
621 ;; mainly used to verify that no infinite recursive inclusion
622 ;; happens.
623 ;; - category :: persistent
624 ;; - type :: list of strings
626 ;; + `inherited-properties' :: Properties of the headline ancestors
627 ;; of the current element or object. Those from the closest
628 ;; headline have precedence over the others.
629 ;; - category :: local
630 ;; - type :: plist
632 ;; + `keywords' :: List of keywords attached to data.
633 ;; - category :: option
634 ;; - type :: string
636 ;; + `language' :: Default language used for translations.
637 ;; - category :: option
638 ;; - type :: string
640 ;; + `parent-properties' :: Properties of the parent element.
641 ;; - category :: local
642 ;; - type :: plist
643 ;; - update :: `org-export-update-info'
645 ;; + `parse-tree' :: Whole parse tree, available at any time during
646 ;; transcoding.
647 ;; - category :: global
648 ;; - type :: list (as returned by `org-element-parse-buffer')
650 ;; + `point-max' :: Last ending position in the parse tree.
651 ;; - category :: global
652 ;; - type :: integer
654 ;; + `preserve-breaks' :: Non-nil means transcoding should preserve
655 ;; all line breaks.
656 ;; - category :: option
657 ;; - type :: symbol (nil, t)
659 ;; + `previous-element' :: Previous element's type at the same
660 ;; level.
661 ;; - category :: local
662 ;; - type :: symbol
663 ;; - update :: `org-export-update-info'
665 ;; + `previous-object' :: Previous object type (or `plain-text') at
666 ;; the same level.
667 ;; - category :: local
668 ;; - type :: symbol
669 ;; - update :: `org-export-update-info'
671 ;; + `previous-section-number' :: Numbering of the previous
672 ;; headline. As it might not be practical for direct use, the
673 ;; function `org-export-get-headline-level' is provided
674 ;; to extract useful information out of it.
675 ;; - category :: local
676 ;; - type :: vector
678 ;; + `section-numbers' :: Non-nil means transcoding should add
679 ;; section numbers to headlines.
680 ;; - category :: option
681 ;; - type :: symbol (nil, t)
683 ;; + `seen-footnote-labels' :: List of already transcoded footnote
684 ;; labels.
685 ;; - category :: persistent
686 ;; - type :: list of strings
687 ;; - update :: `org-export-update-info'
689 ;; + `select-tags' :: List of tags enforcing inclusion of sub-trees in
690 ;; transcoding. When such a tag is present,
691 ;; subtrees without it are de facto excluded from
692 ;; the process. See `use-select-tags'.
693 ;; - category :: option
694 ;; - type :: list of strings
696 ;; + `target-list' :: List of targets raw names encoutered in the
697 ;; parse tree. This is used to partly resolve
698 ;; "fuzzy" links
699 ;; (cf. `org-export-resolve-fuzzy-link').
700 ;; - category :: persistent
701 ;; - type :: list of strings
703 ;; + `time-stamp-file' :: Non-nil means transcoding should insert
704 ;; a time stamp in the output.
705 ;; - category :: option
706 ;; - type :: symbol (nil, t)
708 ;; + `total-loc' :: Contains total lines of code accumulated by source
709 ;; blocks with the "+n" option so far.
710 ;; - category :: option
711 ;; - type :: integer
712 ;; - update :: `org-export-handle-code'
714 ;; + `use-select-tags' :: When non-nil, a select tags has been found
715 ;; in the parse tree. Thus, any headline without one will be
716 ;; filtered out. See `select-tags'.
717 ;; - category :: persistent
718 ;; - type :: interger or nil
720 ;; + `with-archived-trees' :: Non-nil when archived subtrees should
721 ;; also be transcoded. If it is set to the `headline' symbol,
722 ;; only the archived headline's name is retained.
723 ;; - category :: option
724 ;; - type :: symbol (nil, t, `headline')
726 ;; + `with-author' :: Non-nil means author's name should be included
727 ;; in the output.
728 ;; - category :: option
729 ;; - type :: symbol (nil, t)
731 ;; + `with-creator' :: Non-nild means a creation sentence should be
732 ;; inserted at the end of the transcoded string. If the value
733 ;; is `comment', it should be commented.
734 ;; - category :: option
735 ;; - type :: symbol (`comment', nil, t)
737 ;; + `with-drawers' :: Non-nil means drawers should be exported. If
738 ;; its value is a list of names, only drawers with such names
739 ;; will be transcoded.
740 ;; - category :: option
741 ;; - type :: symbol (nil, t) or list of strings
743 ;; + `with-email' :: Non-nil means output should contain author's
744 ;; email.
745 ;; - category :: option
746 ;; - type :: symbol (nil, t)
748 ;; + `with-emphasize' :: Non-nil means emphasized text should be
749 ;; interpreted.
750 ;; - category :: option
751 ;; - type :: symbol (nil, t)
753 ;; + `with-fixed-width' :: Non-nil if transcoder should interpret
754 ;; strings starting with a colon as a fixed-with (verbatim)
755 ;; area.
756 ;; - category :: option
757 ;; - type :: symbol (nil, t)
759 ;; + `with-footnotes' :: Non-nil if transcoder should interpret
760 ;; footnotes.
761 ;; - category :: option
762 ;; - type :: symbol (nil, t)
764 ;; + `with-priority' :: Non-nil means transcoding should include
765 ;; priority cookies.
766 ;; - category :: option
767 ;; - type :: symbol (nil, t)
769 ;; + `with-special-strings' :: Non-nil means transcoding should
770 ;; interpret special strings in plain text.
771 ;; - category :: option
772 ;; - type :: symbol (nil, t)
774 ;; + `with-sub-superscript' :: Non-nil means transcoding should
775 ;; interpret subscript and superscript. With a value of "{}",
776 ;; only interpret those using curly brackets.
777 ;; - category :: option
778 ;; - type :: symbol (nil, {}, t)
780 ;; + `with-tables' :: Non-nil means transcoding should interpret
781 ;; tables.
782 ;; - category :: option
783 ;; - type :: symbol (nil, t)
785 ;; + `with-tags' :: Non-nil means transcoding should keep tags in
786 ;; headlines. A `not-in-toc' value will remove them
787 ;; from the table of contents, if any, nonetheless.
788 ;; - category :: option
789 ;; - type :: symbol (nil, t, `not-in-toc')
791 ;; + `with-tasks' :: Non-nil means transcoding should include
792 ;; headlines with a TODO keyword. A `todo' value
793 ;; will only include headlines with a todo type
794 ;; keyword while a `done' value will do the
795 ;; contrary. If a list of strings is provided, only
796 ;; tasks with keywords belonging to that list will
797 ;; be kept.
798 ;; - category :: option
799 ;; - type :: symbol (t, todo, done, nil) or list of strings
801 ;; + `with-timestamps' :: Non-nil means transcoding should include
802 ;; time stamps and associated keywords. Otherwise, completely
803 ;; remove them.
804 ;; - category :: option
805 ;; - type :: symbol: (t, nil)
807 ;; + `with-toc' :: Non-nil means that a table of contents has to be
808 ;; added to the output. An integer value limits its
809 ;; depth.
810 ;; - category :: option
811 ;; - type :: symbol (nil, t or integer)
813 ;; + `with-todo-keywords' :: Non-nil means transcoding should
814 ;; include TODO keywords.
815 ;; - category :: option
816 ;; - type :: symbol (nil, t)
818 ;;;; Export Options
820 ;; Export options come from five sources, in increasing precedence
821 ;; order:
823 ;; - Global variables,
824 ;; - External options provided at export time,
825 ;; - Options keyword symbols,
826 ;; - Buffer keywords,
827 ;; - Subtree properties.
829 ;; The central internal function with regards to export options is
830 ;; `org-export-collect-options'. It updates global variables with
831 ;; "#+BIND:" keywords, then retrieve and prioritize properties from
832 ;; the different sources.
834 ;; The internal functions doing the retrieval are:
835 ;; `org-export-parse-option-keyword' ,
836 ;; `org-export-get-subtree-options' ,
837 ;; `org-export-get-inbuffer-options' and
838 ;; `org-export-get-global-options'.
840 ;; Some properties do not rely on the previous sources but still
841 ;; depend on the original buffer are taken care of in
842 ;; `org-export-initial-options'.
844 ;; Also, `org-export-confirm-letbind' and `org-export-install-letbind'
845 ;; take care of the part relative to "#+BIND:" keywords.
847 (defun org-export-collect-options (backend subtreep ext-plist)
848 "Collect export options from the current buffer.
850 BACKEND is a symbol specifying the back-end to use.
852 When SUBTREEP is non-nil, assume the export is done against the
853 current sub-tree.
855 EXT-PLIST is a property list with external parameters overriding
856 org-mode's default settings, but still inferior to file-local
857 settings."
858 ;; First install #+BIND variables.
859 (org-export-install-letbind-maybe)
860 ;; Get and prioritize export options...
861 (let ((options (org-combine-plists
862 ;; ... from global variables...
863 (org-export-get-global-options backend)
864 ;; ... from an external property list...
865 ext-plist
866 ;; ... from in-buffer settings...
867 (org-export-get-inbuffer-options
868 (org-with-wide-buffer (buffer-string)) backend
869 (and buffer-file-name
870 (org-remove-double-quotes buffer-file-name)))
871 ;; ... and from subtree, when appropriate.
872 (and subtreep
873 (org-export-get-subtree-options)))))
874 ;; Add initial options.
875 (setq options (append (org-export-initial-options options)
876 options))
877 ;; Set a default title if none has been specified so far.
878 (unless (plist-get options :title)
879 (setq options (plist-put options :title
880 (or (and buffer-file-name
881 (file-name-sans-extension
882 (file-name-nondirectory
883 buffer-file-name)))
884 (buffer-name)))))
885 ;; Return plist.
886 options))
888 (defun org-export-parse-option-keyword (options backend)
889 "Parse an OPTIONS line and return values as a plist.
890 BACKEND is a symbol specifying the back-end to use."
891 (let* ((all (append org-export-option-alist
892 (let ((var (intern
893 (format "org-%s-option-alist" backend))))
894 (and (boundp var) (eval var)))))
895 ;; Build an alist between #+OPTION: item and property-name.
896 (alist (delq nil
897 (mapcar (lambda (e)
898 (when (nth 2 e) (cons (regexp-quote (nth 2 e))
899 (car e))))
900 all)))
901 plist)
902 (mapc (lambda (e)
903 (when (string-match (concat "\\(\\`\\|[ \t]\\)"
904 (car e)
905 ":\\(([^)\n]+)\\|[^ \t\n\r;,.]*\\)")
906 options)
907 (setq plist (plist-put plist
908 (cdr e)
909 (car (read-from-string
910 (match-string 2 options)))))))
911 alist)
912 plist))
914 (defun org-export-get-subtree-options ()
915 "Get export options in subtree at point.
916 Return the options as a plist."
917 (org-with-wide-buffer
918 (when (ignore-errors (org-back-to-heading t))
919 (let (prop plist)
920 (when (setq prop (progn (looking-at org-todo-line-regexp)
921 (or (org-entry-get (point) "EXPORT_TITLE")
922 (org-match-string-no-properties 3))))
923 (setq plist (plist-put plist :title prop)))
924 (when (setq prop (org-entry-get (point) "EXPORT_TEXT"))
925 (setq plist (plist-put plist :text prop)))
926 (when (setq prop (org-entry-get (point) "EXPORT_AUTHOR"))
927 (setq plist (plist-put plist :author prop)))
928 (when (setq prop (org-entry-get (point) "EXPORT_DATE"))
929 (setq plist (plist-put plist :date prop)))
930 (when (setq prop (org-entry-get (point) "EXPORT_OPTIONS"))
931 (setq plist (org-export-add-options-to-plist plist prop)))
932 plist))))
934 (defun org-export-get-inbuffer-options (buffer-string backend files)
935 "Return in-buffer options as a plist.
936 BUFFER-STRING is the string of the buffer. BACKEND is a symbol
937 specifying which back-end should be used."
938 (let ((case-fold-search t) plist)
939 ;; 1. Special keywords, as in `org-export-special-keywords'.
940 (let ((start 0)
941 (special-re (org-make-options-regexp org-export-special-keywords)))
942 (while (string-match special-re buffer-string start)
943 (setq start (match-end 0))
944 (let ((key (upcase (org-match-string-no-properties 1 buffer-string)))
945 ;; Special keywords do not have their value expanded.
946 (val (org-match-string-no-properties 2 buffer-string)))
947 (setq plist
948 (org-combine-plists
949 (cond
950 ((string= key "SETUP_FILE")
951 (let ((file (expand-file-name
952 (org-remove-double-quotes (org-trim val)))))
953 ;; Avoid circular dependencies.
954 (unless (member file files)
955 (org-export-get-inbuffer-options
956 (org-file-contents file 'noerror)
957 backend
958 (cons file files)))))
959 ((string= key "OPTIONS")
960 (org-export-parse-option-keyword val backend))
961 ((string= key "MACRO")
962 (string-match "^\\([-a-zA-Z0-9_]+\\)[ \t]+\\(.*?[ \t]*$\\)"
963 val)
964 (plist-put nil
965 (intern (concat ":macro-"
966 (downcase (match-string 1 val))))
967 (match-string 2 val))))
968 plist)))))
969 ;; 2. Standard options, as in `org-export-option-alist'.
970 (let* ((all (append org-export-option-alist
971 (let ((var (intern
972 (format "org-%s-option-alist" backend))))
973 (and (boundp var) (eval var)))))
974 ;; Build alist between keyword name and property name.
975 (alist (delq nil (mapcar (lambda (e)
976 (when (nth 1 e) (cons (nth 1 e) (car e))))
977 all)))
978 ;; Build regexp matching all keywords associated to export
979 ;; options. Note: the search is case insensitive.
980 (opt-re (org-make-options-regexp
981 (delq nil (mapcar (lambda (e) (nth 1 e)) all))))
982 (start 0))
983 (while (string-match opt-re buffer-string start)
984 (setq start (match-end 0))
985 (let* ((key (upcase (org-match-string-no-properties 1 buffer-string)))
986 ;; Expand value, applying restrictions for keywords.
987 (val (org-match-string-no-properties 2 buffer-string))
988 (prop (cdr (assoc key alist)))
989 (behaviour (nth 4 (assq prop all))))
990 (setq plist
991 (plist-put
992 plist prop
993 ;; Handle value depending on specified BEHAVIOUR.
994 (case behaviour
995 (space (if (plist-get plist prop)
996 (concat (plist-get plist prop) " " (org-trim val))
997 (org-trim val)))
998 (newline (org-trim
999 (concat
1000 (plist-get plist prop) "\n" (org-trim val))))
1001 (split `(,@(plist-get plist prop) ,@(org-split-string val)))
1002 ('t val)
1003 (otherwise (plist-get plist prop)))))))
1004 ;; Parse keywords specified in `org-element-parsed-keywords'.
1005 (mapc
1006 (lambda (key)
1007 (let* ((prop (cdr (assoc (upcase key) alist)))
1008 (value (and prop (plist-get plist prop))))
1009 (when (stringp value)
1010 (setq plist
1011 (plist-put
1012 plist prop
1013 (org-element-parse-secondary-string
1014 value
1015 (cdr (assq 'keyword org-element-string-restrictions))))))))
1016 org-element-parsed-keywords))
1017 ;; Return final value.
1018 plist))
1020 (defun org-export-get-global-options (backend)
1021 "Return global export options as a plist.
1022 BACKEND is a symbol specifying which back-end should be used."
1023 (let ((all (append org-export-option-alist
1024 (let ((var (intern
1025 (format "org-%s-option-alist" backend))))
1026 (and (boundp var) (eval var)))))
1027 ;; Output value.
1028 plist)
1029 (mapc (lambda (cell)
1030 (setq plist
1031 (plist-put plist (car cell) (eval (nth 3 cell)))))
1032 all)
1033 ;; Return value.
1034 plist))
1036 (defun org-export-initial-options (options)
1037 "Return a plist with non-optional properties.
1038 OPTIONS is the export options plist computed so far."
1039 (list
1040 :macro-date "(eval (format-time-string \"$1\"))"
1041 :macro-time "(eval (format-time-string \"$1\"))"
1042 :macro-property "(eval (org-entry-get nil \"$1\" 'selective))"
1043 :macro-modification-time
1044 (and (buffer-file-name)
1045 (file-exists-p (buffer-file-name))
1046 (concat "(eval (format-time-string \"$1\" '"
1047 (prin1-to-string (nth 5 (file-attributes (buffer-file-name))))
1048 "))"))
1049 :macro-input-file (and (buffer-file-name)
1050 (file-name-nondirectory (buffer-file-name)))
1051 :footnotes-labels-alist
1052 (let (alist)
1053 (org-with-wide-buffer
1054 (goto-char (point-min))
1055 (while (re-search-forward org-footnote-definition-re nil t)
1056 (let ((def (org-footnote-at-definition-p)))
1057 (org-skip-whitespace)
1058 (push (cons (car def)
1059 (save-restriction
1060 (narrow-to-region (point) (nth 2 def))
1061 (org-element-parse-buffer)))
1062 alist)))
1063 alist))))
1065 (defvar org-export-allow-BIND-local nil)
1066 (defun org-export-confirm-letbind ()
1067 "Can we use #+BIND values during export?
1068 By default this will ask for confirmation by the user, to divert
1069 possible security risks."
1070 (cond
1071 ((not org-export-allow-BIND) nil)
1072 ((eq org-export-allow-BIND t) t)
1073 ((local-variable-p 'org-export-allow-BIND-local) org-export-allow-BIND-local)
1074 (t (org-set-local 'org-export-allow-BIND-local
1075 (yes-or-no-p "Allow BIND values in this buffer? ")))))
1077 (defun org-export-install-letbind-maybe ()
1078 "Install the values from #+BIND lines as local variables.
1079 Variables must be installed before in-buffer options are
1080 retrieved."
1081 (let (letbind pair)
1082 (org-with-wide-buffer
1083 (goto-char (point-min))
1084 (while (re-search-forward (org-make-options-regexp '("BIND")) nil t)
1085 (when (org-export-confirm-letbind)
1086 (push (read (concat "(" (org-match-string-no-properties 2) ")"))
1087 letbind))))
1088 (while (setq pair (pop letbind))
1089 (org-set-local (car pair) (nth 1 pair)))))
1092 ;;;; Persistent Properties
1094 ;; Persistent properties are declared in
1095 ;; `org-export-persistent-properties-list' variable. Most of them are
1096 ;; initialized at the beginning of the transcoding process by
1097 ;; `org-export-initialize-persistent-properties'. The others are
1098 ;; updated during that process.
1100 ;; Dedicated functions focus on computing the value of specific
1101 ;; persistent properties during initialization. Thus,
1102 ;; `org-export-use-select-tag-p' determines if an headline makes use
1103 ;; of an export tag enforcing inclusion. `org-export-get-min-level'
1104 ;; gets the minimal exportable level, used as a basis to compute
1105 ;; relative level for headlines. `org-export-get-point-max' returns
1106 ;; the maximum exportable ending position in the parse tree.
1107 ;; Eventually `org-export-collect-headline-numbering' builds an alist
1108 ;; between headlines' beginning position and their numbering.
1110 (defconst org-export-persistent-properties-list
1111 '(:code-refs :headline-alist :headline-offset :headline-offset :parse-tree
1112 :point-max :seen-footnote-labels :total-loc :use-select-tags)
1113 "List of persistent properties.")
1115 (defconst org-export-persistent-properties nil
1116 "Used internally to store properties and values during transcoding.
1118 Only properties that should survive recursion are saved here.
1120 This variable is reset before each transcoding.")
1122 (defun org-export-initialize-persistent-properties (data options backend)
1123 "Initialize `org-export-persistent-properties'.
1125 DATA is the parse tree from which information is retrieved.
1126 OPTIONS is a list holding export options. BACKEND is the
1127 back-end called for transcoding, as a symbol.
1129 Following initial persistent properties are set:
1130 `:back-end' Back-end used for transcoding.
1132 `:headline-alist' Alist of all headlines' name as key and a list
1133 holding beginning and ending positions as
1134 value.
1136 `:headline-offset' Offset between true level of headlines and
1137 local level. An offset of -1 means an headline
1138 of level 2 should be considered as a level
1139 1 headline in the context.
1141 `:headline-numbering' Alist of all headlines' beginning position
1142 as key an the associated numbering as value.
1144 `:parse-tree' Whole parse tree.
1146 `:point-max' Last position in the parse tree
1148 `:target-list' List of all targets' raw name in the parse tree.
1150 `:use-select-tags' Non-nil when parsed tree use a special tag to
1151 enforce transcoding of the headline."
1152 ;; First delete any residual persistent property.
1153 (setq org-export-persistent-properties nil)
1154 ;; Immediately after, set `:use-select-tags' property, as it will be
1155 ;; required for further computations.
1156 (setq options
1157 (org-export-set-property
1158 options
1159 :use-select-tags
1160 (org-export-use-select-tags-p data options)))
1161 ;; Get the rest of the initial persistent properties, now
1162 ;; `:use-select-tags' is set...
1163 ;; 1. `:parse-tree' ...
1164 (setq options (org-export-set-property options :parse-tree data))
1165 ;; 2. `:headline-offset' ...
1166 (setq options
1167 (org-export-set-property
1168 options :headline-offset
1169 (- 1 (org-export-get-min-level data options))))
1170 ;; 3. `:point-max' ...
1171 (setq options (org-export-set-property
1172 options :point-max
1173 (org-export-get-point-max data options)))
1174 ;; 4. `:target-list'...
1175 (setq options (org-export-set-property
1176 options :target-list
1177 (org-element-map
1178 data 'target
1179 (lambda (target info)
1180 (org-element-get-property :raw-value target)))))
1181 ;; 5. `:headline-alist'
1182 (setq options (org-export-set-property
1183 options :headline-alist
1184 (org-element-map
1185 data 'headline
1186 (lambda (headline info)
1187 (list (org-element-get-property :raw-value headline)
1188 (org-element-get-property :begin headline)
1189 (org-element-get-property :end headline))))))
1190 ;; 6. `:headline-numbering'
1191 (setq options (org-export-set-property
1192 options :headline-numbering
1193 (org-export-collect-headline-numbering data options)))
1194 ;; 7. `:back-end'
1195 (setq options (org-export-set-property options :back-end backend)))
1197 (defun org-export-use-select-tags-p (data options)
1198 "Non-nil when data use a tag enforcing transcoding.
1199 DATA is parsed data as returned by `org-element-parse-buffer'.
1200 OPTIONS is a plist holding export options."
1201 (org-element-map
1202 data
1203 'headline
1204 (lambda (headline info)
1205 (let ((tags (org-element-get-property :with-tags headline)))
1206 (and tags (string-match
1207 (format ":%s:" (plist-get info :select-tags)) tags))))
1208 options
1209 'stop-at-first-match))
1211 (defun org-export-get-min-level (data options)
1212 "Return minimum exportable headline's level in DATA.
1213 DATA is parsed tree as returned by `org-element-parse-buffer'.
1214 OPTIONS is a plist holding export options."
1215 (catch 'exit
1216 (let ((min-level 10000))
1217 (mapc (lambda (blob)
1218 (when (and (eq (car blob) 'headline)
1219 (not (org-export-skip-p blob options)))
1220 (setq min-level
1221 (min (org-element-get-property :level blob) min-level)))
1222 (when (= min-level 1) (throw 'exit 1)))
1223 (org-element-get-contents data))
1224 ;; If no headline was found, for the sake of consistency, set
1225 ;; minimum level to 1 nonetheless.
1226 (if (= min-level 10000) 1 min-level))))
1228 (defun org-export-get-point-max (data options)
1229 "Return last exportable ending position in DATA.
1230 DATA is parsed tree as returned by `org-element-parse-buffer'.
1231 OPTIONS is a plist holding export options."
1232 (let ((pos-max 1))
1233 (mapc (lambda (blob)
1234 (unless (and (eq (car blob) 'headline)
1235 (org-export-skip-p blob options))
1236 (setq pos-max (org-element-get-property :end blob))))
1237 (org-element-get-contents data))
1238 pos-max))
1240 (defun org-export-collect-headline-numbering (data options)
1241 "Return numbering of all exportable headlines in a parse tree.
1243 DATA is the parse tree. OPTIONS is the plist holding export
1244 options.
1246 Return an alist whose key is headline's beginning position and
1247 value is its associated numbering (in the shape of a list of
1248 numbers)."
1249 (let ((numbering (make-vector org-export-max-depth 0)))
1250 (org-element-map
1251 data
1252 'headline
1253 (lambda (headline info)
1254 (let ((relative-level (1- (org-export-get-relative-level blob info))))
1255 (cons
1256 (org-element-get-property :begin headline)
1257 (loop for n across numbering
1258 for idx from 0 to org-export-max-depth
1259 when (< idx relative-level) collect n
1260 when (= idx relative-level) collect (aset numbering idx (1+ n))
1261 when (> idx relative-level) do (aset numbering idx 0)))))
1262 options)))
1265 ;;;; Properties Management
1267 ;; This is mostly done with the help of two functions. On the one
1268 ;; hand `org-export-update-info' is used to keep up-to-date local
1269 ;; information while walking the nested list representing the parsed
1270 ;; document. On the other end, `org-export-set-property' handles
1271 ;; properties modifications according to their type (persistent or
1272 ;; local).
1274 ;; As exceptions, `:code-refs' and `:total-loc' properties are updated
1275 ;; with `org-export-handle-code' function.
1277 (defun org-export-update-info (blob info recursep)
1278 "Update export options depending on context.
1280 BLOB is the element or object being parsed. INFO is the plist
1281 holding the export options.
1283 When RECURSEP is non-nil, assume the following element or object
1284 will be inside the current one.
1286 The following properties are updated:
1287 `genealogy' List of current element's parents
1288 (symbol list).
1289 `inherited-properties' List of inherited properties from
1290 parent headlines (plist).
1291 `parent-properties' List of last element's properties
1292 (plist).
1293 `previous-element' Previous element's type (symbol).
1294 `previous-object' Previous object's type (symbol).
1295 `seen-footnote-labels' List of already parsed footnote
1296 labels (string list)
1298 Return the property list."
1299 (let* ((type (and (not (stringp blob)) (car blob))))
1300 (cond
1301 ;; Case 1: We're moving into a recursive blob.
1302 (recursep
1303 (org-combine-plists
1304 info
1305 `(:genealogy ,(cons type (plist-get info :genealogy))
1306 :previous-element nil
1307 :previous-object nil
1308 :parent-properties
1309 ,(if (memq type org-element-all-elements)
1310 (nth 1 blob)
1311 (plist-get info :parent-properties))
1312 :inherited-properties
1313 ,(if (eq type 'headline)
1314 (org-combine-plists
1315 (plist-get info :inherited-properties) (nth 1 blob))
1316 (plist-get info :inherited-properties)))
1317 ;; Add persistent properties.
1318 org-export-persistent-properties))
1319 ;; Case 2: No recursion.
1321 ;; At a footnote reference: mark its label as seen, if not
1322 ;; already the case.
1323 (when (eq type 'footnote-reference)
1324 (let ((label (org-element-get-property :label blob))
1325 (seen-labels (plist-get org-export-persistent-properties
1326 :seen-footnote-labels)))
1327 ;; Store anonymous footnotes (nil label) without checking if
1328 ;; another anonymous footnote was seen before.
1329 (unless (and label (member label seen-labels))
1330 (setq info (org-export-set-property
1331 info :seen-footnote-labels (push label seen-labels))))))
1332 ;; Set `:previous-element' or `:previous-object' according to
1333 ;; BLOB.
1334 (setq info (cond ((not type)
1335 (org-export-set-property
1336 info :previous-object 'plain-text))
1337 ((memq type org-element-all-elements)
1338 (org-export-set-property info :previous-element type))
1339 (t (org-export-set-property info :previous-object type))))
1340 ;; Return updated value.
1341 info))))
1343 (defun org-export-set-property (info prop value)
1344 "Set property PROP to VALUE in plist INFO.
1345 Return the new plist."
1346 (when (memq prop org-export-persistent-properties-list)
1347 (setq org-export-persistent-properties
1348 (plist-put org-export-persistent-properties prop value)))
1349 (plist-put info prop value))
1353 ;;; The Transcoder
1355 ;; This function reads Org data (obtained with, i.e.
1356 ;; `org-element-parse-buffer') and transcodes it into a specified
1357 ;; back-end output. It takes care of updating local properties,
1358 ;; filtering out elements or objects according to export options and
1359 ;; organizing the output blank lines and white space are preserved.
1361 ;; Though, this function is inapropriate for secondary strings, which
1362 ;; require a fresh copy of the plist passed as INFO argument. Thus,
1363 ;; `org-export-secondary-string' is provided for that specific task.
1365 ;; Internally, three functions handle the filtering of objects and
1366 ;; elements during the export. More precisely, `org-export-skip-p'
1367 ;; determines if the considered object or element should be ignored
1368 ;; altogether, `org-export-interpret-p' tells which elements or
1369 ;; objects should be seen as real Org syntax and `org-export-expand'
1370 ;; transforms the others back into their original shape.
1372 (defun org-export-data (data backend info)
1373 "Convert DATA to a string into BACKEND format.
1375 DATA is a nested list as returned by `org-element-parse-buffer'.
1377 BACKEND is a symbol among supported exporters.
1379 INFO is a plist holding export options and also used as
1380 a communication channel between elements when walking the nested
1381 list. See `org-export-update-info' function for more
1382 details.
1384 Return transcoded string."
1385 (mapconcat
1386 ;; BLOB can be an element, an object, a string, or nil.
1387 (lambda (blob)
1388 (cond
1389 ((not blob) nil) ((equal blob "") nil)
1390 ;; BLOB is a string. Check if the optional transcoder for plain
1391 ;; text exists, and call it in that case. Otherwise, simply
1392 ;; return string. Also update INFO and call
1393 ;; `org-export-filter-plain-text-functions'.
1394 ((stringp blob)
1395 (setq info (org-export-update-info blob info nil))
1396 (let ((transcoder (intern (format "org-%s-plain-text" backend))))
1397 (org-export-filter-apply-functions
1398 org-export-filter-plain-text-functions
1399 (if (fboundp transcoder) (funcall transcoder blob info) blob)
1400 backend)))
1401 ;; BLOB is an element or an object.
1403 (let* ((type (if (stringp blob) 'plain-text (car blob)))
1404 ;; 1. Determine the appropriate TRANSCODER.
1405 (transcoder
1406 (cond
1407 ;; 1.0 A full Org document is inserted.
1408 ((eq type 'org-data) 'identity)
1409 ;; 1.1. BLOB should be ignored.
1410 ((org-export-skip-p blob info) nil)
1411 ;; 1.2. BLOB shouldn't be transcoded. Interpret it
1412 ;; back into Org syntax.
1413 ((not (org-export-interpret-p blob info))
1414 'org-export-expand)
1415 ;; 1.3. Else apply naming convention.
1416 (t (let ((trans (intern
1417 (format "org-%s-%s" backend type))))
1418 (and (fboundp trans) trans)))))
1419 ;; 2. Compute CONTENTS of BLOB.
1420 (contents
1421 (cond
1422 ;; Case 0. No transcoder defined: ignore BLOB.
1423 ((not transcoder) nil)
1424 ;; Case 1. Transparently export an Org document.
1425 ((eq type 'org-data)
1426 (org-export-data blob backend info))
1427 ;; Case 2. For a recursive object.
1428 ((memq type org-element-recursive-objects)
1429 (org-export-data
1430 blob backend (org-export-update-info blob info t)))
1431 ;; Case 3. For a recursive element.
1432 ((memq type org-element-greater-elements)
1433 ;; Ignore contents of an archived tree
1434 ;; when `:with-archived-trees' is `headline'.
1435 (unless (and
1436 (eq type 'headline)
1437 (eq (plist-get info :with-archived-trees) 'headline)
1438 (org-element-get-property :archivedp blob))
1439 (org-element-normalize-string
1440 (org-export-data
1441 blob backend (org-export-update-info blob info t)))))
1442 ;; Case 4. For a paragraph.
1443 ((eq type 'paragraph)
1444 (let ((paragraph
1445 (org-element-normalize-contents
1446 blob
1447 ;; When normalizing contents of an item or
1448 ;; a footnote definition, ignore first line's
1449 ;; indentation: there is none and it might be
1450 ;; misleading.
1451 (and (not (plist-get info :previous-element))
1452 (let ((parent (car (plist-get info :genealogy))))
1453 (memq parent '(footnote-definition item)))))))
1454 (org-export-data
1455 paragraph
1456 backend
1457 (org-export-update-info blob info t))))))
1458 ;; 3. Transcode BLOB into RESULTS string.
1459 (results (cond
1460 ((not transcoder) nil)
1461 ((eq transcoder 'org-export-expand)
1462 (org-export-data
1463 `(org-data nil ,(funcall transcoder blob contents))
1464 backend info))
1465 (t (funcall transcoder blob contents info)))))
1466 ;; 4. Discard nil results. Otherwise, update INFO, append
1467 ;; the same white space between elements or objects as in
1468 ;; the original buffer, and call appropriate filters.
1469 (when results
1470 (setq info (org-export-update-info blob info nil))
1471 ;; No filter for a full document.
1472 (if (eq type 'org-data)
1473 results
1474 (org-export-filter-apply-functions
1475 (eval (intern (format "org-export-filter-%s-functions" type)))
1476 (if (memq type org-element-all-elements)
1477 (concat
1478 (org-element-normalize-string results)
1479 (make-string (org-element-get-property :post-blank blob) 10))
1480 (concat
1481 results
1482 (make-string
1483 (org-element-get-property :post-blank blob) 32)))
1484 backend)))))))
1485 (org-element-get-contents data) ""))
1487 (defun org-export-secondary-string (secondary backend info)
1488 "Convert SECONDARY string into BACKEND format.
1490 SECONDARY is a nested list as returned by
1491 `org-element-parse-secondary-string'.
1493 BACKEND is a symbol among supported exporters.
1495 INFO is a plist holding export options and also used as
1496 a communication channel between elements when walking the nested
1497 list. See `org-export-update-info' function for more
1498 details.
1500 Return transcoded string."
1501 ;; Make SECONDARY acceptable for `org-export-data'.
1502 (let ((s (if (listp secondary) secondary (list secondary))))
1503 (org-export-data `(org-data nil ,@s) backend (copy-sequence info))))
1505 (defun org-export-skip-p (blob info)
1506 "Non-nil when element or object BLOB should be skipped during export.
1507 INFO is the plist holding export options."
1508 ;; Check headline.
1509 (unless (stringp blob)
1510 (case (car blob)
1511 ('headline
1512 (let ((with-tasks (plist-get info :with-tasks))
1513 (todo (org-element-get-property :todo-keyword blob))
1514 (todo-type (org-element-get-property :todo-type blob))
1515 (archived (plist-get info :with-archived-trees))
1516 (tag-list (let ((tags (org-element-get-property :tags blob)))
1517 (and tags (org-split-string tags ":")))))
1519 ;; Ignore subtrees with an exclude tag.
1520 (loop for k in (plist-get info :exclude-tags)
1521 thereis (member k tag-list))
1522 ;; Ignore subtrees without a select tag, when such tag is found
1523 ;; in the buffer.
1524 (and (plist-get info :use-select-tags)
1525 (loop for k in (plist-get info :select-tags)
1526 never (member k tag-list)))
1527 ;; Ignore commented sub-trees.
1528 (org-element-get-property :commentedp blob)
1529 ;; Ignore archived subtrees if `:with-archived-trees' is nil.
1530 (and (not archived) (org-element-get-property :archivedp blob))
1531 ;; Ignore tasks, if specified by `:with-tasks' property.
1532 (and todo (not with-tasks))
1533 (and todo
1534 (memq with-tasks '(todo done))
1535 (not (eq todo-type with-tasks)))
1536 (and todo
1537 (consp with-tasks)
1538 (not (member todo with-tasks))))))
1539 ;; Check time-stamp.
1540 ('time-stamp (not (plist-get info :with-timestamps)))
1541 ;; Check drawer.
1542 ('drawer
1543 (or (not (plist-get info :with-drawers))
1544 (and (consp (plist-get info :with-drawers))
1545 (not (member (org-element-get-property :drawer-name blob)
1546 (plist-get info :with-drawers))))))
1547 ;; Check export snippet.
1548 ('export-snippet
1549 (let* ((raw-back-end (org-element-get-property :back-end blob))
1550 (true-back-end
1551 (or (cdr (assoc raw-back-end org-export-snippet-translation-alist))
1552 raw-back-end)))
1553 (not (string= (symbol-name (plist-get info :back-end))
1554 true-back-end)))))))
1556 (defun org-export-interpret-p (blob info)
1557 "Non-nil if element or object BLOB should be interpreted as Org syntax.
1558 Check is done according to export options INFO, stored as
1559 a plist."
1560 (case (car blob)
1561 ;; ... entities...
1562 (entity (plist-get info :with-entities))
1563 ;; ... emphasis...
1564 (emphasis (plist-get info :with-emphasize))
1565 ;; ... fixed-width areas.
1566 (fixed-width (plist-get info :with-fixed-width))
1567 ;; ... footnotes...
1568 ((footnote-definition footnote-reference)
1569 (plist-get info :with-footnotes))
1570 ;; ... sub/superscripts...
1571 ((subscript superscript)
1572 (let ((sub/super-p (plist-get info :with-sub-superscript)))
1573 (if (eq sub/super-p '{})
1574 (org-element-get-property :use-brackets-p blob)
1575 sub/super-p)))
1576 ;; ... tables...
1577 (table (plist-get info :with-tables))
1578 (otherwise t)))
1580 (defsubst org-export-expand (blob contents)
1581 "Expand a parsed element or object to its original state.
1582 BLOB is either an element or an object. CONTENTS is its
1583 contents, as a string or nil."
1584 (funcall
1585 (intern (format "org-element-%s-interpreter" (car blob))) blob contents))
1589 ;;; The Filter System
1591 ;; Filters allow end-users to tweak easily the transcoded output.
1592 ;; They are the functional counterpart of hooks, as every filter in
1593 ;; a set is applied to the return value of the previous one.
1595 ;; Every set is back-end agnostic. Although, a filter is always
1596 ;; called, in addition to the string it applies to, with the back-end
1597 ;; used as argument, so it's easy enough for the end-user to add
1598 ;; back-end specific filters in the set.
1600 ;; Filters sets are defined below. There are of four types:
1602 ;; - `org-export-filter-parse-tree-functions' applies directly on the
1603 ;; complete parsed tree. It's the only filters set that doesn't
1604 ;; apply to a string.
1605 ;; - `org-export-filter-final-output-functions' applies to the final
1606 ;; transcoded string.
1607 ;; - `org-export-filter-plain-text-functions' applies to any string
1608 ;; not recognized as Org syntax.
1609 ;; - `org-export-filter-TYPE-functions' applies on the string returned
1610 ;; after an element or object of type TYPE has been transcoded.
1612 ;; All filters sets are applied through
1613 ;; `org-export-filter-apply-functions' function. Filters in a set are
1614 ;; applied in reverse order, that is in the order of consing. It
1615 ;; allows developers to be reasonably sure that their filters will be
1616 ;; applied first.
1618 ;;;; Special Filters
1619 (defvar org-export-filter-parse-tree-functions nil
1620 "Filter, or list of filters, applied to the parsed tree.
1621 Each filter is called with two arguments: the parse tree, as
1622 returned by `org-element-parse-buffer', and the back-end as
1623 a symbol. It must return the modified parse tree to transcode.")
1625 (defvar org-export-filter-final-output-functions nil
1626 "Filter, or list of filters, applied to the transcoded string.
1627 Each filter is called with two arguments: the full transcoded
1628 string, and the back-end as a symbol. It must return a string
1629 that will be used as the final export output.")
1631 (defvar org-export-filter-plain-text-functions nil
1632 "Filter, or list of filters, applied to plain text.
1633 Each filter is called with two arguments: a string which contains
1634 no Org syntax, and the back-end as a symbol. It must return
1635 a string or nil.")
1638 ;;;; Elements Filters
1640 (defvar org-export-filter-center-block-functions nil
1641 "Filter, or list of filters, applied to a transcoded center block.
1642 Each filter is called with two arguments: the transcoded center
1643 block, as a string, and the back-end, as a symbol. It must
1644 return a string or nil.")
1646 (defvar org-export-filter-drawer-functions nil
1647 "Filter, or list of filters, applied to a transcoded drawer.
1648 Each filter is called with two arguments: the transcoded drawer,
1649 as a string, and the back-end, as a symbol. It must return
1650 a string or nil.")
1652 (defvar org-export-filter-dynamic-block-functions nil
1653 "Filter, or list of filters, applied to a transcoded dynamic-block.
1654 Each filter is called with two arguments: the transcoded
1655 dynamic-block, as a string, and the back-end, as a symbol. It
1656 must return a string or nil.")
1658 (defvar org-export-filter-headline-functions nil
1659 "Filter, or list of filters, applied to a transcoded headline.
1660 Each filter is called with two arguments: the transcoded
1661 headline, as a string, and the back-end, as a symbol. It must
1662 return a string or nil.")
1664 (defvar org-export-filter-inlinetask-functions nil
1665 "Filter, or list of filters, applied to a transcoded inlinetask.
1666 Each filter is called with two arguments: the transcoded
1667 inlinetask, as a string, and the back-end, as a symbol. It must
1668 return a string or nil.")
1670 (defvar org-export-filter-plain-list-functions nil
1671 "Filter, or list of filters, applied to a transcoded plain-list.
1672 Each filter is called with two arguments: the transcoded
1673 plain-list, as a string, and the back-end, as a symbol. It must
1674 return a string or nil.")
1676 (defvar org-export-filter-item-functions nil
1677 "Filter, or list of filters, applied to a transcoded item.
1678 Each filter is called with two arguments: the transcoded item, as
1679 a string, and the back-end, as a symbol. It must return a string
1680 or nil.")
1682 (defvar org-export-filter-comment-functions nil
1683 "Filter, or list of filters, applied to a transcoded comment.
1684 Each filter is called with two arguments: the transcoded comment,
1685 as a string, and the back-end, as a symbol. It must return
1686 a string or nil.")
1688 (defvar org-export-filter-comment-block-functions nil
1689 "Filter, or list of filters, applied to a transcoded comment-comment.
1690 Each filter is called with two arguments: the transcoded
1691 comment-block, as a string, and the back-end, as a symbol. It
1692 must return a string or nil.")
1694 (defvar org-export-filter-example-block-functions nil
1695 "Filter, or list of filters, applied to a transcoded example-block.
1696 Each filter is called with two arguments: the transcoded
1697 example-block, as a string, and the back-end, as a symbol. It
1698 must return a string or nil.")
1700 (defvar org-export-filter-export-block-functions nil
1701 "Filter, or list of filters, applied to a transcoded export-block.
1702 Each filter is called with two arguments: the transcoded
1703 export-block, as a string, and the back-end, as a symbol. It
1704 must return a string or nil.")
1706 (defvar org-export-filter-fixed-width-functions nil
1707 "Filter, or list of filters, applied to a transcoded fixed-width.
1708 Each filter is called with two arguments: the transcoded
1709 fixed-width, as a string, and the back-end, as a symbol. It must
1710 return a string or nil.")
1712 (defvar org-export-filter-footnote-definition-functions nil
1713 "Filter, or list of filters, applied to a transcoded footnote-definition.
1714 Each filter is called with two arguments: the transcoded
1715 footnote-definition, as a string, and the back-end, as a symbol.
1716 It must return a string or nil.")
1718 (defvar org-export-filter-horizontal-rule-functions nil
1719 "Filter, or list of filters, applied to a transcoded horizontal-rule.
1720 Each filter is called with two arguments: the transcoded
1721 horizontal-rule, as a string, and the back-end, as a symbol. It
1722 must return a string or nil.")
1724 (defvar org-export-filter-keyword-functions nil
1725 "Filter, or list of filters, applied to a transcoded keyword.
1726 Each filter is called with two arguments: the transcoded keyword,
1727 as a string, and the back-end, as a symbol. It must return
1728 a string or nil.")
1730 (defvar org-export-filter-latex-environment-functions nil
1731 "Filter, or list of filters, applied to a transcoded latex-environment.
1732 Each filter is called with two arguments: the transcoded
1733 latex-environment, as a string, and the back-end, as a symbol.
1734 It must return a string or nil.")
1736 (defvar org-export-filter-babel-call-functions nil
1737 "Filter, or list of filters, applied to a transcoded babel-call.
1738 Each filter is called with two arguments: the transcoded
1739 babel-call, as a string, and the back-end, as a symbol. It must
1740 return a string or nil.")
1742 (defvar org-export-filter-paragraph-functions nil
1743 "Filter, or list of filters, applied to a transcoded paragraph.
1744 Each filter is called with two arguments: the transcoded
1745 paragraph, as a string, and the back-end, as a symbol. It must
1746 return a string or nil.")
1748 (defvar org-export-filter-property-drawer-functions nil
1749 "Filter, or list of filters, applied to a transcoded property-drawer.
1750 Each filter is called with two arguments: the transcoded
1751 property-drawer, as a string, and the back-end, as a symbol. It
1752 must return a string or nil.")
1754 (defvar org-export-filter-quote-block-functions nil
1755 "Filter, or list of filters, applied to a transcoded quote block.
1756 Each filter is called with two arguments: the transcoded quote
1757 block, as a string, and the back-end, as a symbol. It must
1758 return a string or nil.")
1760 (defvar org-export-filter-quote-section-functions nil
1761 "Filter, or list of filters, applied to a transcoded quote-section.
1762 Each filter is called with two arguments: the transcoded
1763 quote-section, as a string, and the back-end, as a symbol. It
1764 must return a string or nil.")
1766 (defvar org-export-filter-special-block-functions nil
1767 "Filter, or list of filters, applied to a transcoded special block.
1768 Each filter is called with two arguments: the transcoded special
1769 block, as a string, and the back-end, as a symbol. It must
1770 return a string or nil.")
1772 (defvar org-export-filter-src-block-functions nil
1773 "Filter, or list of filters, applied to a transcoded src-block.
1774 Each filter is called with two arguments: the transcoded
1775 src-block, as a string, and the back-end, as a symbol. It must
1776 return a string or nil.")
1778 (defvar org-export-filter-table-functions nil
1779 "Filter, or list of filters, applied to a transcoded table.
1780 Each filter is called with two arguments: the transcoded table,
1781 as a string, and the back-end, as a symbol. It must return
1782 a string or nil.")
1784 (defvar org-export-filter-verse-block-functions nil
1785 "Filter, or list of filters, applied to a transcoded verse block.
1786 Each filter is called with two arguments: the transcoded verse
1787 block, as a string, and the back-end, as a symbol. It must
1788 return a string or nil.")
1791 ;;;; Objects Filters
1793 (defvar org-export-filter-emphasis-functions nil
1794 "Filter, or list of filters, applied to a transcoded emphasis.
1795 Each filter is called with two arguments: the transcoded
1796 emphasis, as a string, and the back-end, as a symbol. It must
1797 return a string or nil.")
1799 (defvar org-export-filter-entity-functions nil
1800 "Filter, or list of filters, applied to a transcoded entity.
1801 Each filter is called with two arguments: the transcoded entity,
1802 as a string, and the back-end, as a symbol. It must return
1803 a string or nil.")
1805 (defvar org-export-filter-export-snippet-functions nil
1806 "Filter, or list of filters, applied to a transcoded export-snippet.
1807 Each filter is called with two arguments: the transcoded
1808 export-snippet, as a string, and the back-end, as a symbol. It
1809 must return a string or nil.")
1811 (defvar org-export-filter-footnote-reference-functions nil
1812 "Filter, or list of filters, applied to a transcoded footnote-reference.
1813 Each filter is called with two arguments: the transcoded
1814 footnote-reference, as a string, and the back-end, as a symbol.
1815 It must return a string or nil.")
1817 (defvar org-export-filter-inline-babel-call-functions nil
1818 "Filter, or list of filters, applied to a transcoded inline-babel-call.
1819 Each filter is called with two arguments: the transcoded
1820 inline-babel-call, as a string, and the back-end, as a symbol. It
1821 must return a string or nil.")
1823 (defvar org-export-filter-inline-src-block-functions nil
1824 "Filter, or list of filters, applied to a transcoded inline-src-block.
1825 Each filter is called with two arguments: the transcoded
1826 inline-src-block, as a string, and the back-end, as a symbol. It
1827 must return a string or nil.")
1829 (defvar org-export-filter-latex-fragment-functions nil
1830 "Filter, or list of filters, applied to a transcoded latex-fragment.
1831 Each filter is called with two arguments: the transcoded
1832 latex-fragment, as a string, and the back-end, as a symbol. It
1833 must return a string or nil.")
1835 (defvar org-export-filter-line-break-functions nil
1836 "Filter, or list of filters, applied to a transcoded line-break.
1837 Each filter is called with two arguments: the transcoded
1838 line-break, as a string, and the back-end, as a symbol. It must
1839 return a string or nil.")
1841 (defvar org-export-filter-link-functions nil
1842 "Filter, or list of filters, applied to a transcoded link.
1843 Each filter is called with two arguments: the transcoded link, as
1844 a string, and the back-end, as a symbol. It must return a string
1845 or nil.")
1847 (defvar org-export-filter-macro-functions nil
1848 "Filter, or list of filters, applied to a transcoded macro.
1849 Each filter is called with two arguments: the transcoded macro,
1850 as a string, and the back-end, as a symbol. It must return
1851 a string or nil.")
1853 (defvar org-export-filter-radio-target-functions nil
1854 "Filter, or list of filters, applied to a transcoded radio-target.
1855 Each filter is called with two arguments: the transcoded
1856 radio-target, as a string, and the back-end, as a symbol. It
1857 must return a string or nil.")
1859 (defvar org-export-filter-statistics-cookie-functions nil
1860 "Filter, or list of filters, applied to a transcoded statistics-cookie.
1861 Each filter is called with two arguments: the transcoded
1862 statistics-cookie, as a string, and the back-end, as a symbol.
1863 It must return a string or nil.")
1865 (defvar org-export-filter-subscript-functions nil
1866 "Filter, or list of filters, applied to a transcoded subscript.
1867 Each filter is called with two arguments: the transcoded
1868 subscript, as a string, and the back-end, as a symbol. It must
1869 return a string or nil.")
1871 (defvar org-export-filter-superscript-functions nil
1872 "Filter, or list of filters, applied to a transcoded superscript.
1873 Each filter is called with two arguments: the transcoded
1874 superscript, as a string, and the back-end, as a symbol. It must
1875 return a string or nil.")
1877 (defvar org-export-filter-target-functions nil
1878 "Filter, or list of filters, applied to a transcoded target.
1879 Each filter is called with two arguments: the transcoded target,
1880 as a string, and the back-end, as a symbol. It must return
1881 a string or nil.")
1883 (defvar org-export-filter-time-stamp-functions nil
1884 "Filter, or list of filters, applied to a transcoded time-stamp.
1885 Each filter is called with two arguments: the transcoded
1886 time-stamp, as a string, and the back-end, as a symbol. It must
1887 return a string or nil.")
1889 (defvar org-export-filter-verbatim-functions nil
1890 "Filter, or list of filters, applied to a transcoded verbatim.
1891 Each filter is called with two arguments: the transcoded
1892 verbatim, as a string, and the back-end, as a symbol. It must
1893 return a string or nil.")
1895 (defun org-export-filter-apply-functions (filters value backend)
1896 "Call every function in FILTERS with arguments VALUE and BACKEND.
1897 Functions are called in reverse order, to be reasonably sure that
1898 developer-specified filters, if any, are called first."
1899 ;; Ensure FILTERS is a list.
1900 (let ((filters (if (listp filters) (reverse filters) (list filters))))
1901 (loop for filter in filters
1902 if (not value) return nil else
1903 do (setq value (funcall filter value backend))))
1904 value)
1908 ;;; Core functions
1910 ;; This is the room for the main function, `org-export-as', along with
1911 ;; its derivative, `org-export-to-buffer'. They differ only by the
1912 ;; way they output the resulting code.
1914 ;; Note that `org-export-as' doesn't really parse the current buffer,
1915 ;; but a copy of it (with the same buffer-local variables and
1916 ;; visibility), where Babel blocks are executed, if appropriate.
1917 ;; `org-export-with-current-buffer-copy' macro prepares that copy.
1919 (defun org-export-as (backend
1920 &optional subtreep visible-only body-only ext-plist)
1921 "Transcode current Org buffer into BACKEND code.
1923 If narrowing is active in the current buffer, only transcode its
1924 narrowed part.
1926 If a region is active, transcode that region.
1928 When optional argument SUBTREEP is non-nil, transcode the
1929 sub-tree at point, extracting information from the headline
1930 properties first.
1932 When optional argument VISIBLE-ONLY is non-nil, don't export
1933 contents of hidden elements.
1935 When optional argument BODY-ONLY is non-nil, only return body
1936 code, without preamble nor postamble.
1938 EXT-PLIST, when provided, is a property list with external
1939 parameters overriding Org default settings, but still inferior to
1940 file-local settings.
1942 Return code as a string."
1943 (save-excursion
1944 (save-restriction
1945 ;; Narrow buffer to an appropriate region for parsing.
1946 (when (org-region-active-p)
1947 (narrow-to-region (region-beginning) (region-end)))
1948 (goto-char (point-min))
1949 (when subtreep
1950 (unless (org-at-heading-p)
1951 (org-with-limited-levels (outline-next-heading)))
1952 (let ((end (save-excursion (org-end-of-subtree t)))
1953 (begin (progn (forward-line)
1954 (org-skip-whitespace)
1955 (point-at-bol))))
1956 (narrow-to-region begin end)))
1957 ;; Retrieve export options (INFO) and parsed tree (RAW-DATA).
1958 ;; Buffer isn't parsed directly. Instead, a temporary copy is
1959 ;; created, where all code blocks are evaluated. RAW-DATA is
1960 ;; the parsed tree of the buffer resulting from that process.
1961 ;; Eventually call `org-export-filter-parse-tree-functions'..
1962 (let ((info (org-export-collect-options backend subtreep ext-plist))
1963 (raw-data (org-export-filter-apply-functions
1964 org-export-filter-parse-tree-functions
1965 (org-export-with-current-buffer-copy
1966 (org-export-blocks-preprocess)
1967 (org-element-parse-buffer nil visible-only))
1968 backend)))
1969 ;; Initialize the communication system and combine it to INFO.
1970 (setq info
1971 (org-combine-plists
1972 info
1973 (org-export-initialize-persistent-properties
1974 raw-data info backend)))
1975 ;; Now transcode RAW-DATA. Also call
1976 ;; `org-export-filter-final-output-functions'.
1977 (let ((body (org-element-normalize-string
1978 (org-export-data raw-data backend info)))
1979 (template (intern (format "org-%s-template" backend))))
1980 (if (and (not body-only) (fboundp template))
1981 (org-trim
1982 (org-export-filter-apply-functions
1983 org-export-filter-final-output-functions
1984 (funcall template body info)
1985 backend))
1986 (org-export-filter-apply-functions
1987 org-export-filter-final-output-functions body backend)))))))
1989 (defun org-export-to-buffer (backend buffer &optional subtreep visible-only body-only ext-plist)
1990 "Call `org-export-as' with output to a specified buffer.
1992 BACKEND is the back-end used for transcoding, as a symbol.
1994 BUFFER is the output buffer. If it already exists, it will be
1995 erased first, otherwise, it will be created.
1997 Arguments SUBTREEP, VISIBLE-ONLY and BODY-ONLY are similar to
1998 those used in `org-export-as'.
2000 EXT-PLIST, when provided, is a property list with external
2001 parameters overriding Org default settings, but still inferior to
2002 file-local settings.
2004 Return buffer."
2005 (let ((out (org-export-as backend subtreep visible-only body-only ext-plist))
2006 (buffer (get-buffer-create buffer)))
2007 (with-current-buffer buffer
2008 (erase-buffer)
2009 (insert out)
2010 (goto-char (point-min)))
2011 buffer))
2013 (defmacro org-export-with-current-buffer-copy (&rest body)
2014 "Apply BODY in a copy of the current buffer.
2016 The copy preserves local variables and visibility of the original
2017 buffer.
2019 Point is at buffer's beginning when BODY is applied."
2020 (org-with-gensyms (original-buffer offset buffer-string overlays)
2021 `(let ((,original-buffer ,(current-buffer))
2022 (,offset ,(1- (point-min)))
2023 (,buffer-string ,(buffer-string))
2024 (,overlays (mapcar
2025 'copy-overlay (overlays-in (point-min) (point-max)))))
2026 (with-temp-buffer
2027 (let ((buffer-invisibility-spec nil))
2028 (org-clone-local-variables
2029 ,original-buffer "^\\(org-\\|orgtbl-\\|major-mode$\\)")
2030 (insert ,buffer-string)
2031 (mapc (lambda (ov)
2032 (move-overlay
2034 (- (overlay-start ov) ,offset)
2035 (- (overlay-end ov) ,offset)
2036 (current-buffer)))
2037 ,overlays)
2038 (goto-char (point-min))
2039 (progn ,@body))))))
2040 (def-edebug-spec org-export-with-current-buffer-copy (body))
2044 ;;; Tools For Back-Ends
2046 ;; A whole set of tools is available to help build new exporters. Any
2047 ;; function general enough to have its use across many back-ends
2048 ;; should be added here.
2050 ;; As of now, functions operating on headlines, include keywords,
2051 ;; links, macros, src-blocks, tables and tables of contents are
2052 ;; implemented.
2055 ;;;; For Headlines
2057 ;; `org-export-get-relative-level' is a shortcut to get headline
2058 ;; level, relatively to the lower headline level in the parsed tree.
2060 ;; `org-export-get-headline-number' returns the section number of an
2061 ;; headline, while `org-export-number-to-roman' allows to convert it
2062 ;; to roman numbers.
2064 ;; `org-export-first-sibling-p' and `org-export-last-sibling-p' are
2065 ;; two useful predicates when it comes to fulfill the
2066 ;; `:headline-levels' property.
2068 (defun org-export-get-relative-level (headline info)
2069 "Return HEADLINE relative level within current parsed tree.
2070 INFO is a plist holding contextual information."
2071 (+ (org-element-get-property :level headline)
2072 (or (plist-get info :headline-offset) 0)))
2074 (defun org-export-get-headline-number (headline info)
2075 "Return HEADLINE numbering as a list of numbers.
2076 INFO is a plist holding contextual information."
2077 (cdr (assq (org-element-get-property :begin headline)
2078 (plist-get info :headline-numbering))))
2080 (defun org-export-number-to-roman (n)
2081 "Convert integer N into a roman numeral."
2082 (let ((roman '((1000 . "M") (900 . "CM") (500 . "D") (400 . "CD")
2083 ( 100 . "C") ( 90 . "XC") ( 50 . "L") ( 40 . "XL")
2084 ( 10 . "X") ( 9 . "IX") ( 5 . "V") ( 4 . "IV")
2085 ( 1 . "I")))
2086 (res ""))
2087 (if (<= n 0)
2088 (number-to-string n)
2089 (while roman
2090 (if (>= n (caar roman))
2091 (setq n (- n (caar roman))
2092 res (concat res (cdar roman)))
2093 (pop roman)))
2094 res)))
2096 (defun org-export-first-sibling-p (headline info)
2097 "Non-nil when HEADLINE is the first sibling in its sub-tree.
2098 INFO is the plist used as a communication channel."
2099 (not (eq (plist-get info :previous-element) 'headline)))
2101 (defun org-export-last-sibling-p (headline info)
2102 "Non-nil when HEADLINE is the last sibling in its sub-tree.
2103 INFO is the plist used as a communication channel."
2104 (= (org-element-get-property :end headline)
2105 (or (plist-get (plist-get info :parent-properties) :end)
2106 (plist-get info :point-max))))
2109 ;;;; For Include Keywords
2111 ;; This section provides a tool to properly handle insertion of files
2112 ;; during export: `org-export-included-files'. It recursively
2113 ;; transcodes a file specfied by an include keyword.
2115 ;; It uses two helper functions: `org-export-get-file-contents'
2116 ;; returns contents of a file according to parameters specified in the
2117 ;; keyword while `org-export-parse-included-file' parses the file
2118 ;; specified by it.
2120 (defun org-export-included-file (keyword backend info)
2121 "Transcode file specified with include KEYWORD.
2123 KEYWORD is the include keyword element transcoded. BACKEND is
2124 the language back-end used for transcoding. INFO is the plist
2125 used as a communication channel.
2127 This function updates `:included-files' and `:headline-offset'
2128 properties.
2130 Return the transcoded string."
2131 (let ((data (org-export-parse-included-file keyword info))
2132 (file (let ((value (org-element-get-property :value keyword)))
2133 (and (string-match "^\"\\(\\S-+\\)\"" value)
2134 (match-string 1 value)))))
2135 (org-element-normalize-string
2136 (org-export-data
2137 data backend
2138 (org-combine-plists
2139 info
2140 ;; Store full path of already included files to avoid
2141 ;; recursive file inclusion.
2142 `(:included-files
2143 ,(cons (expand-file-name file) (plist-get info :included-files))
2144 ;; Ensure that a top-level headline in the included
2145 ;; file becomes a direct child of the current headline
2146 ;; in the buffer.
2147 :headline-offset
2148 ,(- (+ (plist-get (plist-get info :inherited-properties) :level)
2149 (plist-get info :headline-offset))
2150 (1- (org-export-get-min-level data info)))))))))
2152 (defun org-export-get-file-contents (file &optional lines)
2153 "Get the contents of FILE and return them as a string.
2154 When optional argument LINES is a string specifying a range of
2155 lines, include only those lines."
2156 (with-temp-buffer
2157 (insert-file-contents file)
2158 (when lines
2159 (let* ((lines (split-string lines "-"))
2160 (lbeg (string-to-number (car lines)))
2161 (lend (string-to-number (cadr lines)))
2162 (beg (if (zerop lbeg) (point-min)
2163 (goto-char (point-min))
2164 (forward-line (1- lbeg))
2165 (point)))
2166 (end (if (zerop lend) (point-max)
2167 (goto-char (point-min))
2168 (forward-line (1- lend))
2169 (point))))
2170 (narrow-to-region beg end)))
2171 (buffer-string)))
2173 (defun org-export-parse-included-file (keyword info)
2174 "Parse file specified by include KEYWORD.
2176 KEYWORD is the include keyword element transcoded. BACKEND is the
2177 language back-end used for transcoding. INFO is the plist used as
2178 a communication channel.
2180 Return the parsed tree."
2181 (let* ((value (org-element-get-property :value keyword))
2182 (file (and (string-match "^\"\\(\\S-+\\)\"" value)
2183 (prog1 (match-string 1 value)
2184 (setq value (replace-match "" nil nil value)))))
2185 (lines (and (string-match
2186 ":lines +\"\\(\\(?:[0-9]+\\)?-\\(?:[0-9]+\\)?\\)\"" value)
2187 (prog1 (match-string 1 value)
2188 (setq value (replace-match "" nil nil value)))))
2189 (env (cond ((string-match "\\<example\\>" value) "example")
2190 ((string-match "\\<src\\(?: +\\(.*\\)\\)?" value)
2191 (match-string 1 value)))))
2192 (cond
2193 ((or (not file)
2194 (not (file-exists-p file))
2195 (not (file-readable-p file)))
2196 (format "Cannot include file %s" file))
2197 ((and (not env)
2198 (member (expand-file-name file) (plist-get info :included-files)))
2199 (error "Recursive file inclusion: %S" file))
2200 (t (let ((raw (org-element-normalize-string
2201 (org-export-get-file-contents
2202 (expand-file-name file) lines))))
2203 ;; If environment isn't specified, Insert file in
2204 ;; a temporary buffer and parse it as Org syntax.
2205 ;; Otherwise, build the element representing the file.
2206 (cond
2207 ((not env)
2208 (with-temp-buffer
2209 (insert raw) (org-mode) (org-element-parse-buffer)))
2210 ((string= "example" env)
2211 `(org-data nil (example-block (:value ,raw :post-blank 0))))
2213 `(org-data
2215 (src-block (:value ,raw :language ,env :post-blank 0))))))))))
2218 ;;;; For Links
2220 ;; `org-export-solidify-link-text' turns a string into a safer version
2221 ;; for links, replacing most non-standard characters with hyphens.
2223 ;; `org-export-get-coderef-format' returns an appropriate format
2224 ;; string for coderefs.
2226 ;; `org-export-inline-image-p' returns a non-nil value when the link
2227 ;; provided should be considered as an inline image.
2229 ;; `org-export-resolve-fuzzy-link' searches destination of fuzzy links
2230 ;; (i.e. links with "fuzzy" as type) within the parsed tree, and
2231 ;; returns an appropriate unique identifier when found, or nil.
2233 (defun org-export-solidify-link-text (s)
2234 "Take link text and make a safe target out of it."
2235 (save-match-data
2236 (mapconcat 'identity (org-split-string s "[^a-zA-Z0-9_\\.-]+") "-")))
2238 (defun org-export-get-coderef-format (path desc)
2239 "Return format string for code reference link.
2240 PATH is the link path. DESC is its description."
2241 (save-match-data
2242 (cond ((string-match (regexp-quote (concat "(" path ")")) desc)
2243 (replace-match "%s" t t desc))
2244 ((string= desc "") "%s")
2245 (t desc))))
2247 (defun org-export-inline-image-p (link contents &optional extensions)
2248 "Non-nil if LINK object points to an inline image.
2250 CONTENTS is the link description part, as a string, or nil.
2252 When non-nil, optional argument EXTENSIONS is a list of valid
2253 extensions for image files, as strings. Otherwise, a default
2254 list is provided \(cf. `org-image-file-name-regexp'\)."
2255 (and (or (not contents) (string= contents ""))
2256 (string= (org-element-get-property :type link) "file")
2257 (org-file-image-p
2258 (expand-file-name (org-element-get-property :path link))
2259 extensions)))
2261 (defun org-export-resolve-fuzzy-link (link info)
2262 "Return an unique identifier for LINK destination.
2264 INFO is a plist holding contextual information.
2266 Return value can be a string, an buffer position, or nil:
2268 - If LINK path exactly matches any target, return its name as the
2269 identifier.
2271 - If LINK path exactly matches any headline name, return
2272 headline's beginning position as the identifier. If more than
2273 one headline share that name, priority will be given to the one
2274 with the closest common ancestor, if any, or the first one in
2275 the parse tree otherwise.
2277 - Otherwise, return nil.
2279 Assume LINK type is \"fuzzy\"."
2280 (let ((path (org-element-get-property :path link)))
2281 (if (member path (plist-get info :target-list))
2282 ;; Link points to a target: return its name as a string.
2283 path
2284 ;; Link either points to an headline or nothing. Try to find
2285 ;; the source, with priority given to headlines with the closest
2286 ;; common ancestor. If such candidate is found, return its
2287 ;; beginning position as an unique identifier, otherwise return
2288 ;; nil.
2289 (let* ((head-alist (plist-get info :headline-alist))
2290 (link-begin (org-element-get-property :begin link))
2291 (link-end (org-element-get-property :end link))
2292 ;; Store candidates as a list of cons cells holding their
2293 ;; beginning and ending position.
2294 (cands (loop for head in head-alist
2295 when (string= (car head) path)
2296 collect (cons (nth 1 head) (nth 2 head)))))
2297 (cond
2298 ;; No candidate: return nil.
2299 ((not cands) nil)
2300 ;; If one or more candidates share common ancestors with
2301 ;; LINK, return beginning position of the first one matching
2302 ;; the closer ancestor shared.
2303 ((let ((ancestors (loop for head in head-alist
2304 when (and (> link-begin (nth 1 head))
2305 (<= link-end (nth 2 head)))
2306 collect (cons (nth 1 head) (nth 2 head)))))
2307 (loop named main for ancestor in (nreverse ancestors) do
2308 (loop for candidate in cands
2309 when (and (>= (car candidate) (car ancestor))
2310 (<= (cdr candidate) (cdr ancestor)))
2311 do (return-from main (car candidate))))))
2312 ;; No candidate have a common ancestor with link: First match
2313 ;; will do. Return its beginning position.
2314 (t (caar cands)))))))
2317 ;;;; For Macros
2319 ;; `org-export-expand-macro' simply takes care of expanding macros.
2321 (defun org-export-expand-macro (macro info)
2322 "Expand MACRO and return it as a string.
2323 INFO is a plist holding export options."
2324 (let* ((key (org-element-get-property :key macro))
2325 (args (org-element-get-property :args macro))
2326 (value (plist-get info (intern (format ":macro-%s" key)))))
2327 ;; Replace arguments in VALUE.
2328 (let ((s 0) n)
2329 (while (string-match "\\$\\([0-9]+\\)" value s)
2330 (setq s (1+ (match-beginning 0))
2331 n (string-to-number (match-string 1 value)))
2332 (and (>= (length args) n)
2333 (setq value (replace-match (nth (1- n) args) t t value)))))
2334 ;; VALUE starts with "(eval": it is a s-exp, `eval' it.
2335 (when (string-match "\\`(eval\\>" value)
2336 (setq value (eval (read value))))
2337 ;; Return expanded string.
2338 (format "%s" value)))
2341 ;;;; For Src-Blocks
2343 ;; `org-export-handle-code' takes care of line numbering and reference
2344 ;; cleaning in source code, when appropriate. It also updates global
2345 ;; LOC count (`:total-loc' property) and code references alist
2346 ;; (`:code-refs' property).
2348 (defun org-export-handle-code (code switches info
2349 &optional language num-fmt ref-fmt)
2350 "Handle line numbers and code references in CODE.
2352 CODE is the string to process. SWITCHES is the option string
2353 determining which changes will be applied to CODE. INFO is the
2354 plist used as a communication channel during export.
2356 Optional argument LANGUAGE, when non-nil, is a string specifying
2357 code's language.
2359 If optional argument NUM-FMT is a string, it will be used as
2360 a format string for numbers at beginning of each line.
2362 If optional argument REF-FMT is a string, it will be used as
2363 a format string for each line of code containing a reference.
2365 Update the following INFO properties by side-effect: `:total-loc'
2366 and `:code-refs'.
2368 Return new code as a string."
2369 (let* ((switches (or switches ""))
2370 (numberp (string-match "[-+]n\\>" switches))
2371 (continuep (string-match "\\+n\\>" switches))
2372 (total-LOC (if (and numberp (not continuep))
2374 (or (plist-get info :total-loc) 0)))
2375 (preserve-indent-p (or org-src-preserve-indentation
2376 (string-match "-i\\>" switches)))
2377 (replace-labels (when (string-match "-r\\>" switches)
2378 (if (string-match "-k\\>" switches) 'keep t)))
2379 ;; Get code and clean it. Remove blank lines at its
2380 ;; beginning and end. Also remove protective commas.
2381 (code (let ((c (replace-regexp-in-string
2382 "\\`\\([ \t]*\n\\)+" ""
2383 (replace-regexp-in-string
2384 "\\(:?[ \t]*\n\\)*[ \t]*\\'" "\n" code))))
2385 ;; If appropriate, remove global indentation.
2386 (unless preserve-indent-p (setq c (org-remove-indentation c)))
2387 ;; Free up the protected lines. Note: Org blocks
2388 ;; have commas at the beginning or every line.
2389 (if (string= language "org")
2390 (replace-regexp-in-string "^," "" c)
2391 (replace-regexp-in-string
2392 "^\\(,\\)\\(:?\\*\\|[ \t]*#\\+\\)" "" c nil nil 1))))
2393 ;; Split code to process it line by line.
2394 (code-lines (org-split-string code "\n"))
2395 ;; Ensure line numbers will be correctly padded before
2396 ;; applying the format string.
2397 (num-fmt (format (if (stringp num-fmt) num-fmt "%s: ")
2398 (format "%%%ds"
2399 (length (number-to-string
2400 (+ (length code-lines)
2401 total-LOC))))))
2402 ;; Get format used for references.
2403 (label-fmt (or (and (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
2404 (match-string 1 switches))
2405 org-coderef-label-format))
2406 ;; Build a regexp matching a loc with a reference.
2407 (with-ref-re (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
2408 (replace-regexp-in-string
2409 "%s" "\\([-a-zA-Z0-9_ ]+\\)" label-fmt nil t)))
2410 coderefs)
2411 (org-element-normalize-string
2412 (mapconcat (lambda (loc)
2413 ;; Maybe add line number to current line of code
2414 ;; (LOC).
2415 (when numberp
2416 (setq loc (concat (format num-fmt (incf total-LOC)) loc)))
2417 ;; Take action if at a ref line.
2418 (when (string-match with-ref-re loc)
2419 (let ((ref (match-string 3 loc)))
2420 (setq loc
2421 (cond
2422 ;; Option "-k": don't remove labels. Use
2423 ;; numbers for references when lines are
2424 ;; numbered, use labels otherwise.
2425 ((eq replace-labels 'keep)
2426 (let ((full-ref (format "(%s)" ref)))
2427 (push (cons ref (if numberp total-LOC full-ref))
2428 coderefs)
2429 (replace-match full-ref nil nil loc 2))
2430 (replace-match (format "(%s)" ref) nil nil loc 2))
2431 ;; Option "-r" without "-k": remove labels.
2432 ;; Use numbers for references when lines are
2433 ;; numbered, use labels otherwise.
2434 (replace-labels
2435 (push (cons ref (if numberp total-LOC ref))
2436 coderefs)
2437 (replace-match "" nil nil loc 1))
2438 ;; Else: don't remove labels and don't use
2439 ;; numbers for references.
2441 (let ((full-ref (format "(%s)" ref)))
2442 (push (cons ref full-ref) coderefs)
2443 (replace-match full-ref nil nil loc 2)))))))
2444 ;; If REF-FMT is defined, apply it to current LOC.
2445 (when (stringp ref-fmt) (setq loc (format ref-fmt loc)))
2446 ;; Update by side-effect communication channel.
2447 ;; Return updated LOC.
2448 (setq info (org-export-set-property
2449 (org-export-set-property
2450 info :code-refs coderefs)
2451 :total-loc total-LOC))
2452 loc)
2453 code-lines "\n"))))
2456 ;;;; For Tables
2458 ;; `org-export-table-format-info' extracts formatting information
2459 ;; (alignment, column groups and presence of a special column) from
2460 ;; a raw table and returns it as a property list.
2462 ;; `org-export-clean-table' cleans the raw table from any Org
2463 ;; table-specific syntax.
2465 (defun org-export-table-format-info (table)
2466 "Extract info from TABLE.
2467 Return a plist whose properties and values are:
2468 `:alignment' vector of strings among \"r\", \"l\" and \"c\",
2469 `:column-groups' vector of symbols among `start', `end', `start-end',
2470 `:row-groups' list of integers representing row groups.
2471 `:special-column-p' non-nil if table has a special column.
2472 `:width' vector of integers representing desired width of
2473 current column, or nil."
2474 (with-temp-buffer
2475 (insert table)
2476 (goto-char 1)
2477 (org-table-align)
2478 (let ((align (vconcat (mapcar (lambda (c) (if c "r" "l"))
2479 org-table-last-alignment)))
2480 (width (make-vector (length org-table-last-alignment) nil))
2481 (colgroups (make-vector (length org-table-last-alignment) nil))
2482 (row-group 0)
2483 (rowgroups)
2484 (special-column-p 'empty))
2485 (mapc (lambda (row)
2486 (if (string-match "^[ \t]*|[-+]+|[ \t]*$" row)
2487 (incf row-group)
2488 (push row-group rowgroups)
2489 ;; Determine if a special column is present by looking
2490 ;; for special markers in the first column. More
2491 ;; accurately, the first column is considered special
2492 ;; if it only contains special markers and, maybe,
2493 ;; empty cells.
2494 (setq special-column-p
2495 (cond
2496 ((not special-column-p) nil)
2497 ((string-match "^[ \t]*| *\\\\?\\([\#!$*_^]\\) *|"
2498 row) 'special)
2499 ((string-match "^[ \t]*| +|" row) special-column-p))))
2500 (cond
2501 ;; Read forced alignment and width information, if any,
2502 ;; and determine final alignment for the table.
2503 ((org-table-cookie-line-p row)
2504 (let ((col 0))
2505 (mapc (lambda (field)
2506 (when (string-match "<\\([lrc]\\)\\([0-9]+\\)?>" field)
2507 (aset align col (match-string 1 field))
2508 (aset width col (let ((w (match-string 2 field)))
2509 (and w (string-to-number w)))))
2510 (incf col))
2511 (org-split-string row "[ \t]*|[ \t]*"))))
2512 ;; Read column groups information.
2513 ((org-table-colgroup-line-p row)
2514 (let ((col 0))
2515 (mapc (lambda (field)
2516 (aset colgroups col
2517 (cond ((string= "<" field) 'start)
2518 ((string= ">" field) 'end)
2519 ((string= "<>" field) 'start-end)))
2520 (incf col))
2521 (org-split-string row "[ \t]*|[ \t]*"))))))
2522 (org-split-string table "\n"))
2523 ;; Return plist.
2524 (list :alignment align
2525 :column-groups colgroups
2526 :row-groups (reverse rowgroups)
2527 :special-column-p (eq special-column-p 'special)
2528 :width width))))
2530 (defun org-export-clean-table (table specialp)
2531 "Clean string TABLE from its formatting elements.
2532 Remove any row containing column groups or formatting cookies and
2533 rows starting with a special marker. If SPECIALP is non-nil,
2534 assume the table contains a special formatting column and remove
2535 it also."
2536 (let ((rows (org-split-string table "\n")))
2537 (mapconcat 'identity
2538 (delq nil
2539 (mapcar
2540 (lambda (row)
2541 (cond
2542 ((org-table-colgroup-line-p row) nil)
2543 ((org-table-cookie-line-p row) nil)
2544 ;; Ignore rows starting with a special marker.
2545 ((string-match "^[ \t]*| *[!_^/] *|" row) nil)
2546 ;; Remove special column.
2547 ((and specialp
2548 (or (string-match "^\\([ \t]*\\)|-+\\+" row)
2549 (string-match "^\\([ \t]*\\)|[^|]*|" row)))
2550 (replace-match "\\1|" t nil row))
2551 (t row)))
2552 rows))
2553 "\n")))
2556 ;;;; For Tables Of Contents
2558 ;; `org-export-get-headlines' builds a table of contents in the shape
2559 ;; of a nested list of cons cells whose car is headline's name and cdr
2560 ;; an unique identifier. One can then easily parse it and transcode
2561 ;; it in a back-end. Identifiers can be used to construct internal
2562 ;; links.
2564 ;; Building lists of tables, figures or listings is quite similar.
2565 ;; Once the generic function `org-export-collect-elements' is defined,
2566 ;; `org-export-collect-tables', `org-export-collect-figures' and
2567 ;; `org-export-collect-listings' can be derived from it.
2569 (defun org-export-get-headlines (backend info &optional n)
2570 "Build a table of contents.
2572 BACKEND is the back-end used to transcode headline's name. INFO
2573 is a plist holding export options.
2575 When non-nil, optional argument N must be an integer. It
2576 specifies the depth of the table of contents.
2578 Return an alist whose keys are headlines' name and value their
2579 relative level and an unique identifier that might be used for
2580 internal links.
2582 For example, on the following tree, where numbers in parens are
2583 buffer position at beginning of the line:
2585 * Title 1 (1)
2586 ** Sub-title 1 (21)
2587 ** Sub-title 2 (42)
2588 * Title 2 (62)
2590 the function will return:
2592 \(\(\"Title 1\" 1 1\)
2593 \(\"Sub-title 1\" 2 21\)
2594 \(\"Sub-title 2\" 2 42\)
2595 \(\"Title 2\" 1 62\)\)"
2596 (org-element-map
2597 (plist-get info :parse-tree)
2598 'headline
2599 (lambda (headline local-info)
2600 ;; Get HEADLINE's relative level.
2601 (let ((level (+ (or (plist-get local-info :headline-offset) 0)
2602 (org-element-get-property :level headline))))
2603 (unless (and (wholenump n) (> level n))
2604 (list
2605 (org-export-secondary-string
2606 (org-element-get-property :title headline) backend info)
2607 level
2608 (org-element-get-property :begin headline)))))
2609 info))
2611 (defun org-export-collect-elements (type backend info)
2612 "Collect named elements of type TYPE.
2614 Only elements with a caption or a name are collected.
2616 BACKEND is the back-end used to transcode their caption or name.
2617 INFO is a plist holding export options.
2619 Return an alist where key is entry's name and value an unique
2620 identifier that might be used for internal links."
2621 (org-element-map
2622 (plist-get info :parse-tree)
2623 type
2624 (lambda (element info)
2625 (let ((entry
2626 (cond
2627 ((org-element-get-property :caption element)
2628 (org-export-secondary-string
2629 (org-element-get-property :caption element) backend info))
2630 ((org-element-get-property :name element)
2631 (org-export-secondary-string
2632 (org-element-get-property :name element) backend info)))))
2633 ;; Skip elements with neither a caption nor a name.
2634 (when entry (cons entry (org-element-get-property :begin element)))))
2635 info))
2637 (defun org-export-collect-tables (backend info)
2638 "Build a list of tables.
2640 BACKEND is the back-end used to transcode table's name. INFO is
2641 a plist holding export options.
2643 Return an alist where key is the caption of the table and value
2644 an unique identifier that might be used for internal links."
2645 (org-export-collect-elements 'table backend info))
2647 (defun org-export-get-figures (backend info)
2648 "Build a list of figures.
2650 A figure is a paragraph type element with a caption or a name.
2652 BACKEND is the back-end used to transcode headline's name. INFO
2653 is a plist holding export options.
2655 Return an alist where key is the caption of the figure and value
2656 an unique indentifier that might be used for internal links."
2657 (org-export-collect-elements 'paragraph backend info))
2659 (defun org-export-collect-listings (backend info)
2660 "Build a list of src blocks.
2662 BACKEND is the back-end used to transcode src block's name. INFO
2663 is a plist holding export options.
2665 Return an alist where key is the caption of the src block and
2666 value an unique indentifier that might be used for internal
2667 links."
2668 (org-export-collect-elements 'src-block backend info))
2671 (provide 'org-export)
2672 ;;; org-export.el ends here