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