org-export: New `org-export-get-parent-paragraph' function
[org-mode.git] / contrib / lisp / org-export.el
blob72afa9d52d641b789920376bd897c72640d3fb95
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-latex "../../EXPERIMENTAL/org-e-latex.el")
98 (require 'org-e-publish "../../EXPERIMENTAL/org-e-publish.el")
101 ;;; Internal Variables
103 ;; Among internal variables, the most important is
104 ;; `org-export-option-alist'. This variable define the global export
105 ;; options, shared between every exporter, and how they are acquired.
107 (defconst org-export-max-depth 19
108 "Maximum nesting depth for headlines, counting from 0.")
110 (defconst org-export-option-alist
111 '((:author "AUTHOR" nil user-full-name t)
112 (:creator "CREATOR" nil org-export-creator-string)
113 (:date "DATE" nil nil t)
114 (:description "DESCRIPTION" nil nil newline)
115 (:email "EMAIL" nil user-mail-address t)
116 (:exclude-tags "EXPORT_EXCLUDE_TAGS" nil org-export-exclude-tags split)
117 (:headline-levels nil "H" org-export-headline-levels)
118 (:keywords "KEYWORDS" nil nil space)
119 (:language "LANGUAGE" nil org-export-default-language t)
120 (:preserve-breaks nil "\\n" org-export-preserve-breaks)
121 (:section-numbers nil "num" org-export-with-section-numbers)
122 (:select-tags "EXPORT_SELECT_TAGS" nil org-export-select-tags split)
123 (:time-stamp-file nil "timestamp" org-export-time-stamp-file)
124 (:title "TITLE" nil nil space)
125 (:with-archived-trees nil "arch" org-export-with-archived-trees)
126 (:with-author nil "author" org-export-with-author)
127 (:with-creator nil "creator" org-export-with-creator)
128 (:with-drawers nil "d" org-export-with-drawers)
129 (:with-email nil "email" org-export-with-email)
130 (:with-emphasize nil "*" org-export-with-emphasize)
131 (:with-entities nil "e" org-export-with-entities)
132 (:with-fixed-width nil ":" org-export-with-fixed-width)
133 (:with-footnotes nil "f" org-export-with-footnotes)
134 (:with-priority nil "pri" org-export-with-priority)
135 (:with-special-strings nil "-" org-export-with-special-strings)
136 (:with-sub-superscript nil "^" org-export-with-sub-superscripts)
137 (:with-toc nil "toc" org-export-with-toc)
138 (:with-tables nil "|" org-export-with-tables)
139 (:with-tags nil "tags" org-export-with-tags)
140 (:with-tasks nil "tasks" org-export-with-tasks)
141 (:with-timestamps nil "<" org-export-with-timestamps)
142 (:with-todo-keywords nil "todo" org-export-with-todo-keywords))
143 "Alist between export properties and ways to set them.
145 The car of the alist is the property name, and the cdr is a list
146 like \(KEYWORD OPTION DEFAULT BEHAVIOUR\) where:
148 KEYWORD is a string representing a buffer keyword, or nil.
149 OPTION is a string that could be found in an #+OPTIONS: line.
150 DEFAULT is the default value for the property.
151 BEHAVIOUR determine how Org should handle multiple keywords for
152 the same property. It is a symbol among:
153 nil Keep old value and discard the new one.
154 t Replace old value with the new one.
155 `space' Concatenate the values, separating them with a space.
156 `newline' Concatenate the values, separating them with
157 a newline.
158 `split' Split values at white spaces, and cons them to the
159 previous list.
161 KEYWORD and OPTION have precedence over DEFAULT.
163 All these properties should be back-end agnostic. For back-end
164 specific properties, define a similar variable named
165 `org-BACKEND-option-alist', replacing BACKEND with the name of
166 the appropriate back-end. You can also redefine properties
167 there, as they have precedence over these.")
169 (defconst org-export-special-keywords
170 '("SETUP_FILE" "OPTIONS" "MACRO")
171 "List of in-buffer keywords that require special treatment.
172 These keywords are not directly associated to a property. The
173 way they are handled must be hard-coded into
174 `org-export-get-inbuffer-options' function.")
176 (defconst org-export-filters-alist
177 '((:filter-babel-call . org-export-filter-babel-call-functions)
178 (:filter-center-block . org-export-filter-center-block-functions)
179 (:filter-comment . org-export-filter-comment-functions)
180 (:filter-comment-block . org-export-filter-comment-block-functions)
181 (:filter-drawer . org-export-filter-drawer-functions)
182 (:filter-dynamic-block . org-export-filter-dynamic-block-functions)
183 (:filter-emphasis . org-export-filter-emphasis-functions)
184 (:filter-entity . org-export-filter-entity-functions)
185 (:filter-example-block . org-export-filter-example-block-functions)
186 (:filter-export-block . org-export-filter-export-block-functions)
187 (:filter-export-snippet . org-export-filter-export-snippet-functions)
188 (:filter-final-output . org-export-filter-final-output-functions)
189 (:filter-fixed-width . org-export-filter-fixed-width-functions)
190 (:filter-footnote-definition . org-export-filter-footnote-definition-functions)
191 (:filter-footnote-reference . org-export-filter-footnote-reference-functions)
192 (:filter-headline . org-export-filter-headline-functions)
193 (:filter-horizontal-rule . org-export-filter-horizontal-rule-functions)
194 (:filter-inline-babel-call . org-export-filter-inline-babel-call-functions)
195 (:filter-inline-src-block . org-export-filter-inline-src-block-functions)
196 (:filter-inlinetask . org-export-filter-inlinetask-functions)
197 (:filter-item . org-export-filter-item-functions)
198 (:filter-keyword . org-export-filter-keyword-functions)
199 (:filter-latex-environment . org-export-filter-latex-environment-functions)
200 (:filter-latex-fragment . org-export-filter-latex-fragment-functions)
201 (:filter-line-break . org-export-filter-line-break-functions)
202 (:filter-link . org-export-filter-link-functions)
203 (:filter-macro . org-export-filter-macro-functions)
204 (:filter-paragraph . org-export-filter-paragraph-functions)
205 (:filter-parse-tree . org-export-filter-parse-tree-functions)
206 (:filter-plain-list . org-export-filter-plain-list-functions)
207 (:filter-plain-text . org-export-filter-plain-text-functions)
208 (:filter-property-drawer . org-export-filter-property-drawer-functions)
209 (:filter-quote-block . org-export-filter-quote-block-functions)
210 (:filter-quote-section . org-export-filter-quote-section-functions)
211 (:filter-radio-target . org-export-filter-radio-target-functions)
212 (:filter-section . org-export-filter-section-functions)
213 (:filter-special-block . org-export-filter-special-block-functions)
214 (:filter-src-block . org-export-filter-src-block-functions)
215 (:filter-statistics-cookie . org-export-filter-statistics-cookie-functions)
216 (:filter-subscript . org-export-filter-subscript-functions)
217 (:filter-superscript . org-export-filter-superscript-functions)
218 (:filter-table . org-export-filter-table-functions)
219 (:filter-target . org-export-filter-target-functions)
220 (:filter-time-stamp . org-export-filter-time-stamp-functions)
221 (:filter-verbatim . org-export-filter-verbatim-functions)
222 (:filter-verse-block . org-export-filter-verse-block-functions))
223 "Alist between filters properties and initial values.
225 The key of each association is a property name accessible through
226 the communication channel its value is a configurable global
227 variable defining initial filters.
229 This list is meant to install user specified filters. Back-end
230 developers may install their own filters using
231 `org-BACKEND-filters-alist', where BACKEND is the name of the
232 considered back-end. Filters defined there will always be
233 prepended to the current list, so they always get applied
234 first.")
236 (defconst org-export-default-inline-image-rule
237 `(("file" .
238 ,(format "\\.%s\\'"
239 (regexp-opt
240 '("png" "jpeg" "jpg" "gif" "tiff" "tif" "xbm"
241 "xpm" "pbm" "pgm" "ppm") t))))
242 "Default rule for link matching an inline image.
243 This rule applies to links with no description. By default, it
244 will be considered as an inline image if it targets a local file
245 whose extension is either \"png\", \"jpeg\", \"jpg\", \"gif\",
246 \"tiff\", \"tif\", \"xbm\", \"xpm\", \"pbm\", \"pgm\" or \"ppm\".
247 See `org-export-inline-image-p' for more information about
248 rules.")
252 ;;; User-configurable Variables
254 ;; Configuration for the masses.
256 ;; They should never be evaled directly, as their value is to be
257 ;; stored in a property list (cf. `org-export-option-alist').
259 (defgroup org-export nil
260 "Options for exporting Org mode files."
261 :tag "Org Export"
262 :group 'org)
264 (defgroup org-export-general nil
265 "General options for export engine."
266 :tag "Org Export General"
267 :group 'org-export)
269 (defcustom org-export-with-archived-trees 'headline
270 "Whether sub-trees with the ARCHIVE tag should be exported.
272 This can have three different values:
273 nil Do not export, pretend this tree is not present.
274 t Do export the entire tree.
275 `headline' Only export the headline, but skip the tree below it.
277 This option can also be set with the #+OPTIONS line,
278 e.g. \"arch:nil\"."
279 :group 'org-export-general
280 :type '(choice
281 (const :tag "Not at all" nil)
282 (const :tag "Headline only" 'headline)
283 (const :tag "Entirely" t)))
285 (defcustom org-export-with-author t
286 "Non-nil means insert author name into the exported file.
287 This option can also be set with the #+OPTIONS line,
288 e.g. \"author:nil\"."
289 :group 'org-export-general
290 :type 'boolean)
292 (defcustom org-export-with-creator 'comment
293 "Non-nil means the postamble should contain a creator sentence.
295 The sentence can be set in `org-export-creator-string' and
296 defaults to \"Generated by Org mode XX in Emacs XXX.\".
298 If the value is `comment' insert it as a comment."
299 :group 'org-export-general
300 :type '(choice
301 (const :tag "No creator sentence" nil)
302 (const :tag "Sentence as a comment" 'comment)
303 (const :tag "Insert the sentence" t)))
305 (defcustom org-export-creator-string
306 (format "Generated by Org mode %s in Emacs %s." org-version emacs-version)
307 "String to insert at the end of the generated document."
308 :group 'org-export-general
309 :type '(string :tag "Creator string"))
311 (defcustom org-export-with-drawers t
312 "Non-nil means export contents of standard drawers.
314 When t, all drawers are exported. This may also be a list of
315 drawer names to export. This variable doesn't apply to
316 properties drawers.
318 This option can also be set with the #+OPTIONS line,
319 e.g. \"d:nil\"."
320 :group 'org-export-general
321 :type '(choice
322 (const :tag "All drawers" t)
323 (const :tag "None" nil)
324 (repeat :tag "Selected drawers"
325 (string :tag "Drawer name"))))
327 (defcustom org-export-with-email nil
328 "Non-nil means insert author email into the exported file.
329 This option can also be set with the #+OPTIONS line,
330 e.g. \"email:t\"."
331 :group 'org-export-general
332 :type 'boolean)
334 (defcustom org-export-with-emphasize t
335 "Non-nil means interpret *word*, /word/, and _word_ as emphasized text.
337 If the export target supports emphasizing text, the word will be
338 typeset in bold, italic, or underlined, respectively. Not all
339 export backends support this.
341 This option can also be set with the #+OPTIONS line, e.g. \"*:nil\"."
342 :group 'org-export-general
343 :type 'boolean)
345 (defcustom org-export-exclude-tags '("noexport")
346 "Tags that exclude a tree from export.
347 All trees carrying any of these tags will be excluded from
348 export. This is without condition, so even subtrees inside that
349 carry one of the `org-export-select-tags' will be removed."
350 :group 'org-export-general
351 :type '(repeat (string :tag "Tag")))
353 (defcustom org-export-with-fixed-width t
354 "Non-nil means lines starting with \":\" will be in fixed width font.
356 This can be used to have pre-formatted text, fragments of code
357 etc. For example:
358 : ;; Some Lisp examples
359 : (while (defc cnt)
360 : (ding))
361 will be looking just like this in also HTML. See also the QUOTE
362 keyword. Not all export backends support this.
364 This option can also be set with the #+OPTIONS line, e.g. \"::nil\"."
365 :group 'org-export-translation
366 :type 'boolean)
368 (defcustom org-export-with-footnotes t
369 "Non-nil means Org footnotes should be exported.
370 This option can also be set with the #+OPTIONS line,
371 e.g. \"f:nil\"."
372 :group 'org-export-general
373 :type 'boolean)
375 (defcustom org-export-headline-levels 3
376 "The last level which is still exported as a headline.
378 Inferior levels will produce itemize lists when exported. Note
379 that a numeric prefix argument to an exporter function overrides
380 this setting.
382 This option can also be set with the #+OPTIONS line, e.g. \"H:2\"."
383 :group 'org-export-general
384 :type 'integer)
386 (defcustom org-export-default-language "en"
387 "The default language for export and clocktable translations, as a string.
388 This may have an association in
389 `org-clock-clocktable-language-setup'."
390 :group 'org-export-general
391 :type '(string :tag "Language"))
393 (defcustom org-export-preserve-breaks nil
394 "Non-nil means preserve all line breaks when exporting.
396 Normally, in HTML output paragraphs will be reformatted.
398 This option can also be set with the #+OPTIONS line,
399 e.g. \"\\n:t\"."
400 :group 'org-export-general
401 :type 'boolean)
403 (defcustom org-export-with-entities t
404 "Non-nil means interpret entities when exporting.
406 For example, HTML export converts \\alpha to &alpha; and \\AA to
407 &Aring;.
409 For a list of supported names, see the constant `org-entities'
410 and the user option `org-entities-user'.
412 This option can also be set with the #+OPTIONS line,
413 e.g. \"e:nil\"."
414 :group 'org-export-general
415 :type 'boolean)
417 (defcustom org-export-with-priority nil
418 "Non-nil means include priority cookies in export.
419 When nil, remove priority cookies for export."
420 :group 'org-export-general
421 :type 'boolean)
423 (defcustom org-export-with-section-numbers t
424 "Non-nil means add section numbers to headlines when exporting.
426 When set to an integer n, numbering will only happen for
427 headlines whose relative level is higher or equal to n.
429 This option can also be set with the #+OPTIONS line,
430 e.g. \"num:t\"."
431 :group 'org-export-general
432 :type 'boolean)
434 (defcustom org-export-select-tags '("export")
435 "Tags that select a tree for export.
436 If any such tag is found in a buffer, all trees that do not carry
437 one of these tags will be deleted before export. Inside trees
438 that are selected like this, you can still deselect a subtree by
439 tagging it with one of the `org-export-exclude-tags'."
440 :group 'org-export-general
441 :type '(repeat (string :tag "Tag")))
443 (defcustom org-export-with-special-strings t
444 "Non-nil means interpret \"\-\", \"--\" and \"---\" for export.
446 When this option is turned on, these strings will be exported as:
448 Org HTML LaTeX
449 -----+----------+--------
450 \\- &shy; \\-
451 -- &ndash; --
452 --- &mdash; ---
453 ... &hellip; \ldots
455 This option can also be set with the #+OPTIONS line,
456 e.g. \"-:nil\"."
457 :group 'org-export-general
458 :type 'boolean)
460 (defcustom org-export-with-sub-superscripts t
461 "Non-nil means interpret \"_\" and \"^\" for export.
463 When this option is turned on, you can use TeX-like syntax for
464 sub- and superscripts. Several characters after \"_\" or \"^\"
465 will be considered as a single item - so grouping with {} is
466 normally not needed. For example, the following things will be
467 parsed as single sub- or superscripts.
469 10^24 or 10^tau several digits will be considered 1 item.
470 10^-12 or 10^-tau a leading sign with digits or a word
471 x^2-y^3 will be read as x^2 - y^3, because items are
472 terminated by almost any nonword/nondigit char.
473 x_{i^2} or x^(2-i) braces or parenthesis do grouping.
475 Still, ambiguity is possible - so when in doubt use {} to enclose
476 the sub/superscript. If you set this variable to the symbol
477 `{}', the braces are *required* in order to trigger
478 interpretations as sub/superscript. This can be helpful in
479 documents that need \"_\" frequently in plain text.
481 This option can also be set with the #+OPTIONS line,
482 e.g. \"^:nil\"."
483 :group 'org-export-general
484 :type '(choice
485 (const :tag "Interpret them" t)
486 (const :tag "Curly brackets only" {})
487 (const :tag "Do not interpret them" nil)))
489 (defcustom org-export-with-toc t
490 "Non-nil means create a table of contents in exported files.
492 The TOC contains headlines with levels up
493 to`org-export-headline-levels'. When an integer, include levels
494 up to N in the toc, this may then be different from
495 `org-export-headline-levels', but it will not be allowed to be
496 larger than the number of headline levels. When nil, no table of
497 contents is made.
499 This option can also be set with the #+OPTIONS line,
500 e.g. \"toc:nil\" or \"toc:3\"."
501 :group 'org-export-general
502 :type '(choice
503 (const :tag "No Table of Contents" nil)
504 (const :tag "Full Table of Contents" t)
505 (integer :tag "TOC to level")))
507 (defcustom org-export-with-tables t
508 "If non-nil, lines starting with \"|\" define a table.
509 For example:
511 | Name | Address | Birthday |
512 |-------------+----------+-----------|
513 | Arthur Dent | England | 29.2.2100 |
515 This option can also be set with the #+OPTIONS line, e.g. \"|:nil\"."
516 :group 'org-export-general
517 :type 'boolean)
519 (defcustom org-export-with-tags t
520 "If nil, do not export tags, just remove them from headlines.
522 If this is the symbol `not-in-toc', tags will be removed from
523 table of contents entries, but still be shown in the headlines of
524 the document.
526 This option can also be set with the #+OPTIONS line,
527 e.g. \"tags:nil\"."
528 :group 'org-export-general
529 :type '(choice
530 (const :tag "Off" nil)
531 (const :tag "Not in TOC" not-in-toc)
532 (const :tag "On" t)))
534 (defcustom org-export-with-tasks t
535 "Non-nil means include TODO items for export.
536 This may have the following values:
537 t include tasks independent of state.
538 todo include only tasks that are not yet done.
539 done include only tasks that are already done.
540 nil remove all tasks before export
541 list of keywords keep only tasks with these keywords"
542 :group 'org-export-general
543 :type '(choice
544 (const :tag "All tasks" t)
545 (const :tag "No tasks" nil)
546 (const :tag "Not-done tasks" todo)
547 (const :tag "Only done tasks" done)
548 (repeat :tag "Specific TODO keywords"
549 (string :tag "Keyword"))))
551 (defcustom org-export-time-stamp-file t
552 "Non-nil means insert a time stamp into the exported file.
553 The time stamp shows when the file was created.
555 This option can also be set with the #+OPTIONS line,
556 e.g. \"timestamp:nil\"."
557 :group 'org-export-general
558 :type 'boolean)
560 (defcustom org-export-with-timestamps t
561 "If nil, do not export time stamps and associated keywords."
562 :group 'org-export-general
563 :type 'boolean)
565 (defcustom org-export-with-todo-keywords t
566 "Non-nil means include TODO keywords in export.
567 When nil, remove all these keywords from the export.")
569 (defcustom org-export-allow-BIND 'confirm
570 "Non-nil means allow #+BIND to define local variable values for export.
571 This is a potential security risk, which is why the user must
572 confirm the use of these lines."
573 :group 'org-export-general
574 :type '(choice
575 (const :tag "Never" nil)
576 (const :tag "Always" t)
577 (const :tag "Ask a confirmation for each file" confirm)))
579 (defcustom org-export-snippet-translation-alist nil
580 "Alist between export snippets back-ends and exporter back-ends.
582 This variable allows to provide shortcuts for export snippets.
584 For example, with a value of '\(\(\"h\" . \"html\"\)\), the HTML
585 back-end will recognize the contents of \"@h{<b>}\" as HTML code
586 while every other back-end will ignore it."
587 :group 'org-export-general
588 :type '(repeat
589 (cons
590 (string :tag "Shortcut")
591 (string :tag "Back-end"))))
593 (defcustom org-export-coding-system nil
594 "Coding system for the exported file."
595 :group 'org-export-general
596 :type 'coding-system)
598 (defcustom org-export-copy-to-kill-ring t
599 "Non-nil means exported stuff will also be pushed onto the kill ring."
600 :group 'org-export-general
601 :type 'boolean)
603 (defcustom org-export-initial-scope 'buffer
604 "The initial scope when exporting with `org-export-dispatch'.
605 This variable can be either set to `buffer' or `subtree'."
606 :group 'org-export-general
607 :type '(choice
608 (const :tag "Export current buffer" 'buffer)
609 (const :tag "Export current subtree" 'subtree)))
611 (defcustom org-export-show-temporary-export-buffer t
612 "Non-nil means show buffer after exporting to temp buffer.
613 When Org exports to a file, the buffer visiting that file is ever
614 shown, but remains buried. However, when exporting to a temporary
615 buffer, that buffer is popped up in a second window. When this variable
616 is nil, the buffer remains buried also in these cases."
617 :group 'org-export-general
618 :type 'boolean)
620 (defcustom org-export-dispatch-use-expert-ui nil
621 "Non-nil means using a non-intrusive `org-export-dispatch'.
622 In that case, no help buffer is displayed. Though, an indicator
623 for current export scope is added to the prompt \(i.e. \"b\" when
624 output is restricted to body only, \"s\" when it is restricted to
625 the current subtree and \"v\" when only visible elements are
626 considered for export\). Also, \[?] allows to switch back to
627 standard mode."
628 :group 'org-export-general
629 :type 'boolean)
633 ;;; The Communication Channel
635 ;; During export process, every function has access to a number of
636 ;; properties. They are of three types:
638 ;; 1. Environment options are collected once at the very beginning of
639 ;; the process, out of the original buffer and configuration.
640 ;; Associated to the parse tree, they make an Org closure.
641 ;; Collecting them is handled by `org-export-get-environment'
642 ;; function.
644 ;; Most environment options are defined through the
645 ;; `org-export-option-alist' variable.
647 ;; 2. Tree properties are extracted directly from the parsed tree,
648 ;; just before export, by `org-export-collect-tree-properties'.
650 ;; 3. Local options are updated during parsing, and their value
651 ;; depends on the level of recursion. For now, only `:genealogy'
652 ;; belongs to that category.
654 ;; Here is the full list of properties available during transcode
655 ;; process, with their category (option, tree or local) and their
656 ;; value type.
658 ;; + `:author' :: Author's name.
659 ;; - category :: option
660 ;; - type :: string
662 ;; + `:back-end' :: Current back-end used for transcoding.
663 ;; - category :: tree
664 ;; - type :: symbol
666 ;; + `:creator' :: String to write as creation information.
667 ;; - category :: option
668 ;; - type :: string
670 ;; + `:date' :: String to use as date.
671 ;; - category :: option
672 ;; - type :: string
674 ;; + `:description' :: Description text for the current data.
675 ;; - category :: option
676 ;; - type :: string
678 ;; + `:email' :: Author's email.
679 ;; - category :: option
680 ;; - type :: string
682 ;; + `:exclude-tags' :: Tags for exclusion of subtrees from export
683 ;; process.
684 ;; - category :: option
685 ;; - type :: list of strings
687 ;; + `:footnote-definition-alist' :: Alist between footnote labels and
688 ;; their definition, as parsed data. Only non-inlined footnotes
689 ;; are represented in this alist. Also, every definition isn't
690 ;; guaranteed to be referenced in the parse tree. The purpose of
691 ;; this property is to preserve definitions from oblivion
692 ;; (i.e. when the parse tree comes from a part of the original
693 ;; buffer), it isn't meant for direct use in a back-end. To
694 ;; retrieve a definition relative to a reference, use
695 ;; `org-export-get-footnote-definition' instead.
696 ;; - category :: option
697 ;; - type :: alist (STRING . LIST)
699 ;; + `:genealogy' :: Flat list of current object or element's parents
700 ;; from closest to farthest.
701 ;; - category :: local
702 ;; - type :: list of elements and objects
704 ;; + `:headline-levels' :: Maximum level being exported as an
705 ;; headline. Comparison is done with the relative level of
706 ;; headlines in the parse tree, not necessarily with their
707 ;; actual level.
708 ;; - category :: option
709 ;; - type :: integer
711 ;; + `:headline-offset' :: Difference between relative and real level
712 ;; of headlines in the parse tree. For example, a value of -1
713 ;; means a level 2 headline should be considered as level
714 ;; 1 (cf. `org-export-get-relative-level').
715 ;; - category :: tree
716 ;; - type :: integer
718 ;; + `:headline-numbering' :: Alist between headlines' beginning
719 ;; position and their numbering, as a list of numbers
720 ;; (cf. `org-export-get-headline-number').
721 ;; - category :: tree
722 ;; - type :: alist (INTEGER . LIST)
724 ;; + `:input-file' :: Full path to input file, if any.
725 ;; - category :: option
726 ;; - type :: string or nil
728 ;; + `:keywords' :: List of keywords attached to data.
729 ;; - category :: option
730 ;; - type :: string
732 ;; + `:language' :: Default language used for translations.
733 ;; - category :: option
734 ;; - type :: string
736 ;; + `:macro-input-file' :: Macro returning file name of input file,
737 ;; or nil.
738 ;; - category :: option
739 ;; - type :: string or nil
741 ;; + `:parse-tree' :: Whole parse tree, available at any time during
742 ;; transcoding.
743 ;; - category :: global
744 ;; - type :: list (as returned by `org-element-parse-buffer')
746 ;; + `:preserve-breaks' :: Non-nil means transcoding should preserve
747 ;; all line breaks.
748 ;; - category :: option
749 ;; - type :: symbol (nil, t)
751 ;; + `:section-numbers' :: Non-nil means transcoding should add
752 ;; section numbers to headlines.
753 ;; - category :: option
754 ;; - type :: symbol (nil, t)
756 ;; + `:select-tags' :: List of tags enforcing inclusion of sub-trees
757 ;; in transcoding. When such a tag is present,
758 ;; subtrees without it are de facto excluded from
759 ;; the process. See `use-select-tags'.
760 ;; - category :: option
761 ;; - type :: list of strings
763 ;; + `:target-list' :: List of targets encountered in the parse tree.
764 ;; This is used to partly resolve "fuzzy" links
765 ;; (cf. `org-export-resolve-fuzzy-link').
766 ;; - category :: tree
767 ;; - type :: list of strings
769 ;; + `:time-stamp-file' :: Non-nil means transcoding should insert
770 ;; a time stamp in the output.
771 ;; - category :: option
772 ;; - type :: symbol (nil, t)
774 ;; + `:use-select-tags' :: When non-nil, a select tags has been found
775 ;; in the parse tree. Thus, any headline without one will be
776 ;; filtered out. See `select-tags'.
777 ;; - category :: tree
778 ;; - type :: interger or nil
780 ;; + `:with-archived-trees' :: Non-nil when archived subtrees should
781 ;; also be transcoded. If it is set to the `headline' symbol,
782 ;; only the archived headline's name is retained.
783 ;; - category :: option
784 ;; - type :: symbol (nil, t, `headline')
786 ;; + `:with-author' :: Non-nil means author's name should be included
787 ;; in the output.
788 ;; - category :: option
789 ;; - type :: symbol (nil, t)
791 ;; + `:with-creator' :: Non-nild means a creation sentence should be
792 ;; inserted at the end of the transcoded string. If the value
793 ;; is `comment', it should be commented.
794 ;; - category :: option
795 ;; - type :: symbol (`comment', nil, t)
797 ;; + `:with-drawers' :: Non-nil means drawers should be exported. If
798 ;; its value is a list of names, only drawers with such names
799 ;; will be transcoded.
800 ;; - category :: option
801 ;; - type :: symbol (nil, t) or list of strings
803 ;; + `:with-email' :: Non-nil means output should contain author's
804 ;; email.
805 ;; - category :: option
806 ;; - type :: symbol (nil, t)
808 ;; + `:with-emphasize' :: Non-nil means emphasized text should be
809 ;; interpreted.
810 ;; - category :: option
811 ;; - type :: symbol (nil, t)
813 ;; + `:with-fixed-width' :: Non-nil if transcoder should interpret
814 ;; strings starting with a colon as a fixed-with (verbatim) area.
815 ;; - category :: option
816 ;; - type :: symbol (nil, t)
818 ;; + `:with-footnotes' :: Non-nil if transcoder should interpret
819 ;; footnotes.
820 ;; - category :: option
821 ;; - type :: symbol (nil, t)
823 ;; + `:with-priority' :: Non-nil means transcoding should include
824 ;; priority cookies.
825 ;; - category :: option
826 ;; - type :: symbol (nil, t)
828 ;; + `:with-special-strings' :: Non-nil means transcoding should
829 ;; interpret special strings in plain text.
830 ;; - category :: option
831 ;; - type :: symbol (nil, t)
833 ;; + `:with-sub-superscript' :: Non-nil means transcoding should
834 ;; interpret subscript and superscript. With a value of "{}",
835 ;; only interpret those using curly brackets.
836 ;; - category :: option
837 ;; - type :: symbol (nil, {}, t)
839 ;; + `:with-tables' :: Non-nil means transcoding should interpret
840 ;; tables.
841 ;; - category :: option
842 ;; - type :: symbol (nil, t)
844 ;; + `:with-tags' :: Non-nil means transcoding should keep tags in
845 ;; headlines. A `not-in-toc' value will remove them
846 ;; from the table of contents, if any, nonetheless.
847 ;; - category :: option
848 ;; - type :: symbol (nil, t, `not-in-toc')
850 ;; + `:with-tasks' :: Non-nil means transcoding should include
851 ;; headlines with a TODO keyword. A `todo' value
852 ;; will only include headlines with a todo type
853 ;; keyword while a `done' value will do the
854 ;; contrary. If a list of strings is provided, only
855 ;; tasks with keywords belonging to that list will
856 ;; be kept.
857 ;; - category :: option
858 ;; - type :: symbol (t, todo, done, nil) or list of strings
860 ;; + `:with-timestamps' :: Non-nil means transcoding should include
861 ;; time stamps and associated keywords. Otherwise, completely
862 ;; remove them.
863 ;; - category :: option
864 ;; - type :: symbol: (t, nil)
866 ;; + `:with-toc' :: Non-nil means that a table of contents has to be
867 ;; added to the output. An integer value limits its
868 ;; depth.
869 ;; - category :: option
870 ;; - type :: symbol (nil, t or integer)
872 ;; + `:with-todo-keywords' :: Non-nil means transcoding should
873 ;; include TODO keywords.
874 ;; - category :: option
875 ;; - type :: symbol (nil, t)
878 ;;;; Environment Options
880 ;; Environment options encompass all parameters defined outside the
881 ;; scope of the parsed data. They come from five sources, in
882 ;; increasing precedence order:
884 ;; - Global variables,
885 ;; - Options keyword symbols,
886 ;; - Buffer keywords,
887 ;; - Subtree properties.
889 ;; The central internal function with regards to environment options
890 ;; is `org-export-get-environment'. It updates global variables with
891 ;; "#+BIND:" keywords, then retrieve and prioritize properties from
892 ;; the different sources.
894 ;; The internal functions doing the retrieval are:
895 ;; `org-export-parse-option-keyword' ,
896 ;; `org-export-get-subtree-options' ,
897 ;; `org-export-get-inbuffer-options' and
898 ;; `org-export-get-global-options'.
900 ;; Some properties, which do not rely on the previous sources but
901 ;; still depend on the original buffer, are taken care of with
902 ;; `org-export-initial-options'.
904 ;; Also, `org-export-confirm-letbind' and `org-export-install-letbind'
905 ;; take care of the part relative to "#+BIND:" keywords.
907 (defun org-export-get-environment (&optional backend subtreep ext-plist)
908 "Collect export options from the current buffer.
910 Optional argument BACKEND is a symbol specifying which back-end
911 specific options to read, if any.
913 When optional argument SUBTREEP is non-nil, assume the export is
914 done against the current sub-tree.
916 Third optional argument EXT-PLIST is a property list with
917 external parameters overriding Org default settings, but still
918 inferior to file-local settings."
919 ;; First install #+BIND variables.
920 (org-export-install-letbind-maybe)
921 ;; Get and prioritize export options...
922 (let ((options (org-combine-plists
923 ;; ... from global variables...
924 (org-export-get-global-options backend)
925 ;; ... from buffer's name (default title)...
926 `(:title
927 ,(or (let ((file (buffer-file-name (buffer-base-buffer))))
928 (and file
929 (file-name-sans-extension
930 (file-name-nondirectory file))))
931 (buffer-name (buffer-base-buffer))))
932 ;; ... from an external property list...
933 ext-plist
934 ;; ... from in-buffer settings...
935 (org-export-get-inbuffer-options
936 backend
937 (and buffer-file-name
938 (org-remove-double-quotes buffer-file-name)))
939 ;; ... and from subtree, when appropriate.
940 (and subtreep (org-export-get-subtree-options)))))
941 ;; Add initial options.
942 (setq options (append (org-export-initial-options) options))
943 ;; Return plist.
944 options))
946 (defun org-export-parse-option-keyword (options &optional backend)
947 "Parse an OPTIONS line and return values as a plist.
948 Optional argument BACKEND is a symbol specifying which back-end
949 specific items to read, if any."
950 (let* ((all
951 (append org-export-option-alist
952 (and backend
953 (let ((var (intern
954 (format "org-%s-option-alist" backend))))
955 (and (boundp var) (eval var))))))
956 ;; Build an alist between #+OPTION: item and property-name.
957 (alist (delq nil
958 (mapcar (lambda (e)
959 (when (nth 2 e) (cons (regexp-quote (nth 2 e))
960 (car e))))
961 all)))
962 plist)
963 (mapc (lambda (e)
964 (when (string-match (concat "\\(\\`\\|[ \t]\\)"
965 (car e)
966 ":\\(([^)\n]+)\\|[^ \t\n\r;,.]*\\)")
967 options)
968 (setq plist (plist-put plist
969 (cdr e)
970 (car (read-from-string
971 (match-string 2 options)))))))
972 alist)
973 plist))
975 (defun org-export-get-subtree-options ()
976 "Get export options in subtree at point.
978 Assume point is at subtree's beginning.
980 Return options as a plist."
981 (let (prop plist)
982 (when (setq prop (progn (looking-at org-todo-line-regexp)
983 (or (save-match-data
984 (org-entry-get (point) "EXPORT_TITLE"))
985 (org-match-string-no-properties 3))))
986 (setq plist
987 (plist-put
988 plist :title
989 (org-element-parse-secondary-string
990 prop
991 (cdr (assq 'keyword org-element-string-restrictions))))))
992 (when (setq prop (org-entry-get (point) "EXPORT_TEXT"))
993 (setq plist (plist-put plist :text prop)))
994 (when (setq prop (org-entry-get (point) "EXPORT_AUTHOR"))
995 (setq plist (plist-put plist :author prop)))
996 (when (setq prop (org-entry-get (point) "EXPORT_DATE"))
997 (setq plist (plist-put plist :date prop)))
998 (when (setq prop (org-entry-get (point) "EXPORT_OPTIONS"))
999 (setq plist (org-export-add-options-to-plist plist prop)))
1000 plist))
1002 (defun org-export-get-inbuffer-options (&optional backend files)
1003 "Return current buffer export options, as a plist.
1005 Optional argument BACKEND, when non-nil, is a symbol specifying
1006 which back-end specific options should also be read in the
1007 process.
1009 Optional argument FILES is a list of setup files names read so
1010 far, used to avoid circular dependencies.
1012 Assume buffer is in Org mode. Narrowing, if any, is ignored."
1013 (org-with-wide-buffer
1014 (goto-char (point-min))
1015 (let ((case-fold-search t) plist)
1016 ;; 1. Special keywords, as in `org-export-special-keywords'.
1017 (let ((special-re (org-make-options-regexp org-export-special-keywords)))
1018 (while (re-search-forward special-re nil t)
1019 (let ((element (org-element-at-point)))
1020 (when (eq (car element) 'keyword)
1021 (let ((key (upcase (org-element-get-property :key element)))
1022 (val (org-element-get-property :value element)))
1023 (setq plist
1024 (org-combine-plists
1025 plist
1026 (cond
1027 ((string= key "SETUP_FILE")
1028 (let ((file
1029 (expand-file-name
1030 (org-remove-double-quotes (org-trim val)))))
1031 ;; Avoid circular dependencies.
1032 (unless (member file files)
1033 (with-temp-buffer
1034 (insert (org-file-contents file 'noerror))
1035 (org-mode)
1036 (org-export-get-inbuffer-options
1037 backend (cons file files))))))
1038 ((string= key "OPTIONS")
1039 (org-export-parse-option-keyword val backend))
1040 ((string= key "MACRO")
1041 (when (string-match
1042 "^\\([-a-zA-Z0-9_]+\\)\\(?:[ \t]+\\(.*?\\)[ \t]*$\\)?"
1043 val)
1044 (let ((key
1045 (intern
1046 (concat ":macro-"
1047 (downcase (match-string 1 val)))))
1048 (value (match-string 2 val)))
1049 (cond
1050 ((not value) "")
1051 ((string-match "\\`(eval\\>" value)
1052 (list key value))
1054 (list
1056 ;; If user explicitly asks for a newline, be
1057 ;; sure to preserve it from further filling
1058 ;; with `hard-newline'.
1059 (replace-regexp-in-string
1060 "\\\\n" hard-newline value)))))))))))))))
1061 ;; 2. Standard options, as in `org-export-option-alist'.
1062 (let* ((all (append org-export-option-alist
1063 ;; Also look for back-end specific options
1064 ;; if BACKEND is defined.
1065 (and backend
1066 (let ((var
1067 (intern
1068 (format "org-%s-option-alist" backend))))
1069 (and (boundp var) (eval var))))))
1070 ;; Build alist between keyword name and property name.
1071 (alist
1072 (delq nil (mapcar
1073 (lambda (e) (when (nth 1 e) (cons (nth 1 e) (car e))))
1074 all)))
1075 ;; Build regexp matching all keywords associated to export
1076 ;; options. Note: the search is case insensitive.
1077 (opt-re (org-make-options-regexp
1078 (delq nil (mapcar (lambda (e) (nth 1 e)) all)))))
1079 (goto-char (point-min))
1080 (while (re-search-forward opt-re nil t)
1081 (let ((element (org-element-at-point)))
1082 (when (eq (car element) 'keyword)
1083 (let* ((key (upcase (org-element-get-property :key element)))
1084 (val (org-element-get-property :value element))
1085 (prop (cdr (assoc key alist)))
1086 (behaviour (nth 4 (assq prop all))))
1087 (setq plist
1088 (plist-put
1089 plist prop
1090 ;; Handle value depending on specified BEHAVIOUR.
1091 (case behaviour
1092 (space
1093 (if (not (plist-get plist prop)) (org-trim val)
1094 (concat (plist-get plist prop) " " (org-trim val))))
1095 (newline
1096 (org-trim
1097 (concat (plist-get plist prop) "\n" (org-trim val))))
1098 (split
1099 `(,@(plist-get plist prop) ,@(org-split-string val)))
1100 ('t val)
1101 (otherwise (plist-get plist prop)))))))))
1102 ;; Parse keywords specified in `org-element-parsed-keywords'.
1103 (mapc
1104 (lambda (key)
1105 (let* ((prop (cdr (assoc key alist)))
1106 (value (and prop (plist-get plist prop))))
1107 (when (stringp value)
1108 (setq plist
1109 (plist-put
1110 plist prop
1111 (org-element-parse-secondary-string
1112 value
1113 (cdr (assq 'keyword org-element-string-restrictions))))))))
1114 org-element-parsed-keywords))
1115 ;; 3. Return final value.
1116 plist)))
1118 (defun org-export-get-global-options (&optional backend)
1119 "Return global export options as a plist.
1121 Optional argument BACKEND, if non-nil, is a symbol specifying
1122 which back-end specific export options should also be read in the
1123 process."
1124 (let ((all (append org-export-option-alist
1125 (and backend
1126 (let ((var (intern
1127 (format "org-%s-option-alist" backend))))
1128 (and (boundp var) (eval var))))))
1129 ;; Output value.
1130 plist)
1131 (mapc (lambda (cell)
1132 (setq plist (plist-put plist (car cell) (eval (nth 3 cell)))))
1133 all)
1134 ;; Return value.
1135 plist))
1137 (defun org-export-initial-options ()
1138 "Return a plist with properties related to input buffer."
1139 (let ((visited-file (buffer-file-name (buffer-base-buffer))))
1140 (list
1141 ;; Store full path of input file name, or nil. For internal use.
1142 :input-file visited-file
1143 ;; `:macro-date', `:macro-time' and `:macro-property' could as well
1144 ;; be initialized as tree properties, since they don't depend on
1145 ;; initial environment. Though, it may be more logical to keep
1146 ;; them close to other ":macro-" properties.
1147 :macro-date "(eval (format-time-string \"$1\"))"
1148 :macro-time "(eval (format-time-string \"$1\"))"
1149 :macro-property "(eval (org-entry-get nil \"$1\" 'selective))"
1150 :macro-modification-time
1151 (and visited-file
1152 (file-exists-p visited-file)
1153 (concat "(eval (format-time-string \"$1\" '"
1154 (prin1-to-string (nth 5 (file-attributes visited-file)))
1155 "))"))
1156 ;; Store input file name as a macro.
1157 :macro-input-file (and visited-file (file-name-nondirectory visited-file))
1158 ;; Footnotes definitions must be collected in the original buffer,
1159 ;; as there's no insurance that they will still be in the parse
1160 ;; tree, due to some narrowing.
1161 :footnote-definition-alist
1162 (let (alist)
1163 (org-with-wide-buffer
1164 (goto-char (point-min))
1165 (while (re-search-forward org-footnote-definition-re nil t)
1166 (let ((def (org-footnote-at-definition-p)))
1167 (when def
1168 (org-skip-whitespace)
1169 (push (cons (car def)
1170 (save-restriction
1171 (narrow-to-region (point) (nth 2 def))
1172 ;; Like `org-element-parse-buffer', but
1173 ;; makes sure the definition doesn't start
1174 ;; with a section element.
1175 (nconc
1176 (list 'org-data nil)
1177 (org-element-parse-elements
1178 (point-min) (point-max) nil nil nil nil nil))))
1179 alist))))
1180 alist)))))
1182 (defvar org-export-allow-BIND-local nil)
1183 (defun org-export-confirm-letbind ()
1184 "Can we use #+BIND values during export?
1185 By default this will ask for confirmation by the user, to divert
1186 possible security risks."
1187 (cond
1188 ((not org-export-allow-BIND) nil)
1189 ((eq org-export-allow-BIND t) t)
1190 ((local-variable-p 'org-export-allow-BIND-local) org-export-allow-BIND-local)
1191 (t (org-set-local 'org-export-allow-BIND-local
1192 (yes-or-no-p "Allow BIND values in this buffer? ")))))
1194 (defun org-export-install-letbind-maybe ()
1195 "Install the values from #+BIND lines as local variables.
1196 Variables must be installed before in-buffer options are
1197 retrieved."
1198 (let (letbind pair)
1199 (org-with-wide-buffer
1200 (goto-char (point-min))
1201 (while (re-search-forward (org-make-options-regexp '("BIND")) nil t)
1202 (when (org-export-confirm-letbind)
1203 (push (read (concat "(" (org-match-string-no-properties 2) ")"))
1204 letbind))))
1205 (while (setq pair (pop letbind))
1206 (org-set-local (car pair) (nth 1 pair)))))
1209 ;;;; Tree Properties
1211 ;; Tree properties are infromation extracted from parse tree. They
1212 ;; are initialized at the beginning of the transcoding process by
1213 ;; `org-export-collect-tree-properties'.
1215 ;; Dedicated functions focus on computing the value of specific tree
1216 ;; properties during initialization. Thus,
1217 ;; `org-export-use-select-tag-p' determines if an headline makes use
1218 ;; of an export tag enforcing inclusion. `org-export-get-min-level'
1219 ;; gets the minimal exportable level, used as a basis to compute
1220 ;; relative level for headlines. `org-export-get-point-max' returns
1221 ;; the maximum exportable ending position in the parse tree.
1222 ;; Eventually `org-export-collect-headline-numbering' builds an alist
1223 ;; between headlines' beginning position and their numbering.
1225 (defun org-export-collect-tree-properties (data info backend)
1226 "Extract tree properties from parse tree.
1228 DATA is the parse tree from which information is retrieved. INFO
1229 is a list holding export options. BACKEND is the back-end called
1230 for transcoding, as a symbol.
1232 Following tree properties are set:
1233 `:back-end' Back-end used for transcoding.
1235 `:headline-offset' Offset between true level of headlines and
1236 local level. An offset of -1 means an headline
1237 of level 2 should be considered as a level
1238 1 headline in the context.
1240 `:headline-numbering' Alist of all headlines' beginning position
1241 as key an the associated numbering as value.
1243 `:parse-tree' Whole parse tree.
1245 `:target-list' List of all targets in the parse tree.
1247 `:use-select-tags' Non-nil when parsed tree use a special tag to
1248 enforce transcoding of the headline."
1249 ;; First, set `:use-select-tags' property, as it will be required
1250 ;; for further computations.
1251 (setq info
1252 (org-combine-plists
1253 info `(:use-select-tags ,(org-export-use-select-tags-p data info))))
1254 ;; Then get `:headline-offset' in order to be able to use
1255 ;; `org-export-get-relative-level'.
1256 (setq info
1257 (org-combine-plists
1258 info `(:headline-offset ,(- 1 (org-export-get-min-level data info)))))
1259 ;; Now, get the rest of the tree properties, now `:use-select-tags'
1260 ;; is set...
1261 (nconc
1262 `(:parse-tree
1263 ,data
1264 :target-list
1265 ,(org-element-map data 'target (lambda (target local) target) info)
1266 :headline-numbering ,(org-export-collect-headline-numbering data info)
1267 :back-end ,backend)
1268 info))
1270 (defun org-export-use-select-tags-p (data options)
1271 "Non-nil when data use a tag enforcing transcoding.
1272 DATA is parsed data as returned by `org-element-parse-buffer'.
1273 OPTIONS is a plist holding export options."
1274 (org-element-map
1275 data
1276 'headline
1277 (lambda (headline info)
1278 (let ((tags (org-element-get-property :with-tags headline)))
1279 (and tags (string-match
1280 (format ":%s:" (plist-get info :select-tags)) tags))))
1281 options
1282 'stop-at-first-match))
1284 (defun org-export-get-min-level (data options)
1285 "Return minimum exportable headline's level in DATA.
1286 DATA is parsed tree as returned by `org-element-parse-buffer'.
1287 OPTIONS is a plist holding export options."
1288 (catch 'exit
1289 (let ((min-level 10000))
1290 (mapc (lambda (blob)
1291 (when (and (eq (car blob) 'headline)
1292 (not (org-export-skip-p blob options)))
1293 (setq min-level
1294 (min (org-element-get-property :level blob) min-level)))
1295 (when (= min-level 1) (throw 'exit 1)))
1296 (org-element-get-contents data))
1297 ;; If no headline was found, for the sake of consistency, set
1298 ;; minimum level to 1 nonetheless.
1299 (if (= min-level 10000) 1 min-level))))
1301 (defun org-export-collect-headline-numbering (data options)
1302 "Return numbering of all exportable headlines in a parse tree.
1304 DATA is the parse tree. OPTIONS is the plist holding export
1305 options.
1307 Return an alist whose key is an headline and value is its
1308 associated numbering \(in the shape of a list of numbers\)."
1309 (let ((numbering (make-vector org-export-max-depth 0)))
1310 (org-element-map
1311 data
1312 'headline
1313 (lambda (headline info)
1314 (let ((relative-level
1315 (1- (org-export-get-relative-level headline info))))
1316 (cons
1317 headline
1318 (loop for n across numbering
1319 for idx from 0 to org-export-max-depth
1320 when (< idx relative-level) collect n
1321 when (= idx relative-level) collect (aset numbering idx (1+ n))
1322 when (> idx relative-level) do (aset numbering idx 0)))))
1323 options)))
1327 ;;; The Transcoder
1329 ;; This function reads Org data (obtained with, i.e.
1330 ;; `org-element-parse-buffer') and transcodes it into a specified
1331 ;; back-end output. It takes care of updating local properties,
1332 ;; filtering out elements or objects according to export options and
1333 ;; organizing the output blank lines and white space are preserved.
1335 ;; Though, this function is inapropriate for secondary strings, which
1336 ;; require a fresh copy of the plist passed as INFO argument. Thus,
1337 ;; `org-export-secondary-string' is provided for that specific task.
1339 ;; Internally, three functions handle the filtering of objects and
1340 ;; elements during the export. More precisely, `org-export-skip-p'
1341 ;; determines if the considered object or element should be ignored
1342 ;; altogether, `org-export-interpret-p' tells which elements or
1343 ;; objects should be seen as real Org syntax and `org-export-expand'
1344 ;; transforms the others back into their original shape.
1346 (defun org-export-data (data backend info)
1347 "Convert DATA to a string into BACKEND format.
1349 DATA is a nested list as returned by `org-element-parse-buffer'.
1351 BACKEND is a symbol among supported exporters.
1353 INFO is a plist holding export options and also used as
1354 a communication channel between elements when walking the nested
1355 list. See `org-export-update-info' function for more
1356 details.
1358 Return transcoded string."
1359 (mapconcat
1360 ;; BLOB can be an element, an object, a string, or nil.
1361 (lambda (blob)
1362 (cond
1363 ((not blob) nil)
1364 ;; BLOB is a string. Check if the optional transcoder for plain
1365 ;; text exists, and call it in that case. Otherwise, simply
1366 ;; return string. Also update INFO and call
1367 ;; `org-export-filter-plain-text-functions'.
1368 ((stringp blob)
1369 (let ((transcoder (intern (format "org-%s-plain-text" backend))))
1370 (org-export-filter-apply-functions
1371 (plist-get info :filter-plain-text)
1372 (if (fboundp transcoder) (funcall transcoder blob info) blob)
1373 backend info)))
1374 ;; BLOB is an element or an object.
1376 (let* ((type (if (stringp blob) 'plain-text (car blob)))
1377 ;; 1. Determine the appropriate TRANSCODER.
1378 (transcoder
1379 (cond
1380 ;; 1.0 A full Org document is inserted.
1381 ((eq type 'org-data) 'identity)
1382 ;; 1.1. BLOB should be ignored.
1383 ((org-export-skip-p blob info) nil)
1384 ;; 1.2. BLOB shouldn't be transcoded. Interpret it
1385 ;; back into Org syntax.
1386 ((not (org-export-interpret-p blob info))
1387 'org-export-expand)
1388 ;; 1.3. Else apply naming convention.
1389 (t (let ((trans (intern
1390 (format "org-%s-%s" backend type))))
1391 (and (fboundp trans) trans)))))
1392 ;; 2. Compute CONTENTS of BLOB.
1393 (contents
1394 (cond
1395 ;; Case 0. No transcoder defined: ignore BLOB.
1396 ((not transcoder) nil)
1397 ;; Case 1. Transparently export an Org document.
1398 ((eq type 'org-data) (org-export-data blob backend info))
1399 ;; Case 2. For a recursive object.
1400 ((memq type org-element-recursive-objects)
1401 (org-export-data
1402 blob backend
1403 (org-combine-plists
1404 info
1405 `(:genealogy ,(cons blob (plist-get info :genealogy))))))
1406 ;; Case 3. For a recursive element.
1407 ((memq type org-element-greater-elements)
1408 ;; Ignore contents of an archived tree
1409 ;; when `:with-archived-trees' is `headline'.
1410 (unless (and
1411 (eq type 'headline)
1412 (eq (plist-get info :with-archived-trees) 'headline)
1413 (org-element-get-property :archivedp blob))
1414 (org-element-normalize-string
1415 (org-export-data
1416 blob backend
1417 (org-combine-plists
1418 info `(:genealogy
1419 ,(cons blob (plist-get info :genealogy))))))))
1420 ;; Case 4. For a paragraph.
1421 ((eq type 'paragraph)
1422 (let ((paragraph
1423 (org-element-normalize-contents
1424 blob
1425 ;; When normalizing contents of an item or
1426 ;; a footnote definition, ignore first line's
1427 ;; indentation: there is none and it might be
1428 ;; misleading.
1429 (and (not (org-export-get-previous-element blob info))
1430 (let ((parent (caar (plist-get info :genealogy))))
1431 (memq parent '(footnote-definition item)))))))
1432 (org-export-data
1433 paragraph backend
1434 (org-combine-plists
1435 info `(:genealogy
1436 ,(cons paragraph (plist-get info :genealogy)))))))))
1437 ;; 3. Transcode BLOB into RESULTS string.
1438 (results (cond
1439 ((not transcoder) nil)
1440 ((eq transcoder 'org-export-expand)
1441 (org-export-data
1442 `(org-data nil ,(funcall transcoder blob contents))
1443 backend info))
1444 (t (funcall transcoder blob contents info)))))
1445 ;; 4. Discard nil results. Otherwise, update INFO, append
1446 ;; the same white space between elements or objects as in
1447 ;; the original buffer, and call appropriate filters.
1448 (when results
1449 ;; No filter for a full document.
1450 (if (eq type 'org-data) results
1451 (org-export-filter-apply-functions
1452 (plist-get info (intern (format ":filter-%s" type)))
1453 (let ((post-blank (org-element-get-property :post-blank blob)))
1454 (if (memq type org-element-all-elements)
1455 (concat (org-element-normalize-string results)
1456 (make-string post-blank ?\n))
1457 (concat results (make-string post-blank ? ))))
1458 backend info)))))))
1459 (org-element-get-contents data) ""))
1461 (defun org-export-secondary-string (secondary backend info)
1462 "Convert SECONDARY string into BACKEND format.
1464 SECONDARY is a nested list as returned by
1465 `org-element-parse-secondary-string'.
1467 BACKEND is a symbol among supported exporters. INFO is a plist
1468 used as a communication channel.
1470 Return transcoded string."
1471 ;; Make SECONDARY acceptable for `org-export-data'.
1472 (let ((s (if (listp secondary) secondary (list secondary))))
1473 (org-export-data `(org-data nil ,@s) backend (copy-sequence info))))
1475 (defun org-export-skip-p (blob info)
1476 "Non-nil when element or object BLOB should be skipped during export.
1477 INFO is the plist holding export options."
1478 ;; Check headline.
1479 (unless (stringp blob)
1480 (case (car blob)
1481 ('headline
1482 (let ((with-tasks (plist-get info :with-tasks))
1483 (todo (org-element-get-property :todo-keyword blob))
1484 (todo-type (org-element-get-property :todo-type blob))
1485 (archived (plist-get info :with-archived-trees))
1486 (tag-list (let ((tags (org-element-get-property :tags blob)))
1487 (and tags (org-split-string tags ":")))))
1489 ;; Ignore subtrees with an exclude tag.
1490 (loop for k in (plist-get info :exclude-tags)
1491 thereis (member k tag-list))
1492 ;; Ignore subtrees without a select tag, when such tag is found
1493 ;; in the buffer.
1494 (and (plist-get info :use-select-tags)
1495 (loop for k in (plist-get info :select-tags)
1496 never (member k tag-list)))
1497 ;; Ignore commented sub-trees.
1498 (org-element-get-property :commentedp blob)
1499 ;; Ignore archived subtrees if `:with-archived-trees' is nil.
1500 (and (not archived) (org-element-get-property :archivedp blob))
1501 ;; Ignore tasks, if specified by `:with-tasks' property.
1502 (and todo (not with-tasks))
1503 (and todo
1504 (memq with-tasks '(todo done))
1505 (not (eq todo-type with-tasks)))
1506 (and todo
1507 (consp with-tasks)
1508 (not (member todo with-tasks))))))
1509 ;; Check time-stamp.
1510 ('time-stamp (not (plist-get info :with-timestamps)))
1511 ;; Check drawer.
1512 ('drawer
1513 (or (not (plist-get info :with-drawers))
1514 (and (consp (plist-get info :with-drawers))
1515 (not (member (org-element-get-property :drawer-name blob)
1516 (plist-get info :with-drawers))))))
1517 ;; Check export snippet.
1518 ('export-snippet
1519 (let* ((raw-back-end (org-element-get-property :back-end blob))
1520 (true-back-end
1521 (or (cdr (assoc raw-back-end org-export-snippet-translation-alist))
1522 raw-back-end)))
1523 (not (string= (symbol-name (plist-get info :back-end))
1524 true-back-end)))))))
1526 (defun org-export-interpret-p (blob info)
1527 "Non-nil if element or object BLOB should be interpreted as Org syntax.
1528 Check is done according to export options INFO, stored as
1529 a plist."
1530 (case (car blob)
1531 ;; ... entities...
1532 (entity (plist-get info :with-entities))
1533 ;; ... emphasis...
1534 (emphasis (plist-get info :with-emphasize))
1535 ;; ... fixed-width areas.
1536 (fixed-width (plist-get info :with-fixed-width))
1537 ;; ... footnotes...
1538 ((footnote-definition footnote-reference)
1539 (plist-get info :with-footnotes))
1540 ;; ... sub/superscripts...
1541 ((subscript superscript)
1542 (let ((sub/super-p (plist-get info :with-sub-superscript)))
1543 (if (eq sub/super-p '{})
1544 (org-element-get-property :use-brackets-p blob)
1545 sub/super-p)))
1546 ;; ... tables...
1547 (table (plist-get info :with-tables))
1548 (otherwise t)))
1550 (defsubst org-export-expand (blob contents)
1551 "Expand a parsed element or object to its original state.
1552 BLOB is either an element or an object. CONTENTS is its
1553 contents, as a string or nil."
1554 (funcall
1555 (intern (format "org-element-%s-interpreter" (car blob))) blob contents))
1559 ;;; The Filter System
1561 ;; Filters allow end-users to tweak easily the transcoded output.
1562 ;; They are the functional counterpart of hooks, as every filter in
1563 ;; a set is applied to the return value of the previous one.
1565 ;; Every set is back-end agnostic. Although, a filter is always
1566 ;; called, in addition to the string it applies to, with the back-end
1567 ;; used as argument, so it's easy enough for the end-user to add
1568 ;; back-end specific filters in the set. The communication channel,
1569 ;; as a plist, is required as the third argument.
1571 ;; Filters sets are defined below. There are of four types:
1573 ;; - `org-export-filter-parse-tree-functions' applies directly on the
1574 ;; complete parsed tree. It's the only filters set that doesn't
1575 ;; apply to a string.
1576 ;; - `org-export-filter-final-output-functions' applies to the final
1577 ;; transcoded string.
1578 ;; - `org-export-filter-plain-text-functions' applies to any string
1579 ;; not recognized as Org syntax.
1580 ;; - `org-export-filter-TYPE-functions' applies on the string returned
1581 ;; after an element or object of type TYPE has been transcoded.
1583 ;; All filters sets are applied through
1584 ;; `org-export-filter-apply-functions' function. Filters in a set are
1585 ;; applied in a LIFO fashion. It allows developers to be sure that
1586 ;; their filters will be applied first.
1588 ;; Filters properties are installed in communication channel just
1589 ;; before parsing, with `org-export-install-filters' function.
1591 ;;;; Special Filters
1592 (defvar org-export-filter-parse-tree-functions nil
1593 "Filter, or list of filters, applied to the parsed tree.
1594 Each filter is called with three arguments: the parse tree, as
1595 returned by `org-element-parse-buffer', the back-end, as
1596 a symbol, and the communication channel, as a plist. It must
1597 return the modified parse tree to transcode.")
1599 (defvar org-export-filter-final-output-functions nil
1600 "Filter, or list of filters, applied to the transcoded string.
1601 Each filter is called with three arguments: the full transcoded
1602 string, the back-end, as a symbol, and the communication channel,
1603 as a plist. It must return a string that will be used as the
1604 final export output.")
1606 (defvar org-export-filter-plain-text-functions nil
1607 "Filter, or list of filters, applied to plain text.
1608 Each filter is called with three arguments: a string which
1609 contains no Org syntax, the back-end, as a symbol, and the
1610 communication channel, as a plist. It must return a string or
1611 nil.")
1614 ;;;; Elements Filters
1616 (defvar org-export-filter-center-block-functions nil
1617 "List of functions applied to a transcoded center block.
1618 Each filter is called with three arguments: the transcoded center
1619 block, as a string, the back-end, as a symbol, and the
1620 communication channel, as a plist. It must return a string or
1621 nil.")
1623 (defvar org-export-filter-drawer-functions nil
1624 "List of functions applied to a transcoded drawer.
1625 Each filter is called with three arguments: the transcoded
1626 drawer, as a string, the back-end, as a symbol, and the
1627 communication channel, as a plist. It must return a string or
1628 nil.")
1630 (defvar org-export-filter-dynamic-block-functions nil
1631 "List of functions applied to a transcoded dynamic-block.
1632 Each filter is called with three arguments: the transcoded
1633 dynamic-block, as a string, the back-end, as a symbol, and the
1634 communication channel, as a plist. It must return a string or
1635 nil.")
1637 (defvar org-export-filter-headline-functions nil
1638 "List of functions applied to a transcoded headline.
1639 Each filter is called with three arguments: the transcoded
1640 headline, as a string, the back-end, as a symbol, and the
1641 communication channel, as a plist. It must return a string or
1642 nil.")
1644 (defvar org-export-filter-inlinetask-functions nil
1645 "List of functions applied to a transcoded inlinetask.
1646 Each filter is called with three arguments: the transcoded
1647 inlinetask, as a string, the back-end, as a symbol, and the
1648 communication channel, as a plist. It must return a string or
1649 nil.")
1651 (defvar org-export-filter-plain-list-functions nil
1652 "List of functions applied to a transcoded plain-list.
1653 Each filter is called with three arguments: the transcoded
1654 plain-list, as a string, the back-end, as a symbol, and the
1655 communication channel, as a plist. It must return a string or
1656 nil.")
1658 (defvar org-export-filter-item-functions nil
1659 "List of functions applied to a transcoded item.
1660 Each filter is called with three arguments: the transcoded item,
1661 as a string, the back-end, as a symbol, and the communication
1662 channel, as a plist. It must return a string or nil.")
1664 (defvar org-export-filter-comment-functions nil
1665 "List of functions applied to a transcoded comment.
1666 Each filter is called with three arguments: the transcoded
1667 comment, as a string, the back-end, as a symbol, and the
1668 communication channel, as a plist. It must return a string or
1669 nil.")
1671 (defvar org-export-filter-comment-block-functions nil
1672 "List of functions applied to a transcoded comment-comment.
1673 Each filter is called with three arguments: the transcoded
1674 comment-block, as a string, the back-end, as a symbol, and the
1675 communication channel, as a plist. It must return a string or
1676 nil.")
1678 (defvar org-export-filter-example-block-functions nil
1679 "List of functions applied to a transcoded example-block.
1680 Each filter is called with three arguments: the transcoded
1681 example-block, as a string, the back-end, as a symbol, and the
1682 communication channel, as a plist. It must return a string or
1683 nil.")
1685 (defvar org-export-filter-export-block-functions nil
1686 "List of functions applied to a transcoded export-block.
1687 Each filter is called with three arguments: the transcoded
1688 export-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-fixed-width-functions nil
1693 "List of functions applied to a transcoded fixed-width.
1694 Each filter is called with three arguments: the transcoded
1695 fixed-width, 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-footnote-definition-functions nil
1700 "List of functions applied to a transcoded footnote-definition.
1701 Each filter is called with three arguments: the transcoded
1702 footnote-definition, as a string, the back-end, as a symbol, and
1703 the communication channel, as a plist. It must return a string
1704 or nil.")
1706 (defvar org-export-filter-horizontal-rule-functions nil
1707 "List of functions applied to a transcoded horizontal-rule.
1708 Each filter is called with three arguments: the transcoded
1709 horizontal-rule, 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-keyword-functions nil
1714 "List of functions applied to a transcoded keyword.
1715 Each filter is called with three arguments: the transcoded
1716 keyword, 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-latex-environment-functions nil
1721 "List of functions applied to a transcoded latex-environment.
1722 Each filter is called with three arguments: the transcoded
1723 latex-environment, as a string, the back-end, as a symbol, and
1724 the communication channel, as a plist. It must return a string
1725 or nil.")
1727 (defvar org-export-filter-babel-call-functions nil
1728 "List of functions applied to a transcoded babel-call.
1729 Each filter is called with three arguments: the transcoded
1730 babel-call, as a string, the back-end, as a symbol, and the
1731 communication channel, as a plist. It must return a string or
1732 nil.")
1734 (defvar org-export-filter-paragraph-functions nil
1735 "List of functions applied to a transcoded paragraph.
1736 Each filter is called with three arguments: the transcoded
1737 paragraph, as a string, the back-end, as a symbol, and the
1738 communication channel, as a plist. It must return a string or
1739 nil.")
1741 (defvar org-export-filter-property-drawer-functions nil
1742 "List of functions applied to a transcoded property-drawer.
1743 Each filter is called with three arguments: the transcoded
1744 property-drawer, as a string, the back-end, as a symbol, and the
1745 communication channel, as a plist. It must return a string or
1746 nil.")
1748 (defvar org-export-filter-quote-block-functions nil
1749 "List of functions applied to a transcoded quote block.
1750 Each filter is called with three arguments: the transcoded quote
1751 block, as a string, the back-end, as a symbol, and the
1752 communication channel, as a plist. It must return a string or
1753 nil.")
1755 (defvar org-export-filter-quote-section-functions nil
1756 "List of functions applied to a transcoded quote-section.
1757 Each filter is called with three arguments: the transcoded
1758 quote-section, as a string, the back-end, as a symbol, and the
1759 communication channel, as a plist. It must return a string or
1760 nil.")
1762 (defvar org-export-filter-section-functions nil
1763 "List of functions applied to a transcoded section.
1764 Each filter is called with three arguments: the transcoded
1765 section, as a string, the back-end, as a symbol, and the
1766 communication channel, as a plist. It must return a string or
1767 nil.")
1769 (defvar org-export-filter-special-block-functions nil
1770 "List of functions applied to a transcoded special block.
1771 Each filter is called with three arguments: the transcoded
1772 special block, as a string, the back-end, as a symbol, and the
1773 communication channel, as a plist. It must return a string or
1774 nil.")
1776 (defvar org-export-filter-src-block-functions nil
1777 "List of functions applied to a transcoded src-block.
1778 Each filter is called with three arguments: the transcoded
1779 src-block, as a string, the back-end, as a symbol, and the
1780 communication channel, as a plist. It must return a string or
1781 nil.")
1783 (defvar org-export-filter-table-functions nil
1784 "List of functions applied to a transcoded table.
1785 Each filter is called with three arguments: the transcoded table,
1786 as a string, the back-end, as a symbol, and the communication
1787 channel, as a plist. It must return a string or nil.")
1789 (defvar org-export-filter-verse-block-functions nil
1790 "List of functions applied to a transcoded verse block.
1791 Each filter is called with three arguments: the transcoded verse
1792 block, as a string, the back-end, as a symbol, and the
1793 communication channel, as a plist. It must return a string or
1794 nil.")
1797 ;;;; Objects Filters
1799 (defvar org-export-filter-emphasis-functions nil
1800 "List of functions applied to a transcoded emphasis.
1801 Each filter is called with three arguments: the transcoded
1802 emphasis, as a string, the back-end, as a symbol, and the
1803 communication channel, as a plist. It must return a string or
1804 nil.")
1806 (defvar org-export-filter-entity-functions nil
1807 "List of functions applied to a transcoded entity.
1808 Each filter is called with three arguments: the transcoded
1809 entity, as a string, the back-end, as a symbol, and the
1810 communication channel, as a plist. It must return a string or
1811 nil.")
1813 (defvar org-export-filter-export-snippet-functions nil
1814 "List of functions applied to a transcoded export-snippet.
1815 Each filter is called with three arguments: the transcoded
1816 export-snippet, as a string, the back-end, as a symbol, and the
1817 communication channel, as a plist. It must return a string or
1818 nil.")
1820 (defvar org-export-filter-footnote-reference-functions nil
1821 "List of functions applied to a transcoded footnote-reference.
1822 Each filter is called with three arguments: the transcoded
1823 footnote-reference, as a string, the back-end, as a symbol, and
1824 the communication channel, as a plist. It must return a string
1825 or nil.")
1827 (defvar org-export-filter-inline-babel-call-functions nil
1828 "List of functions applied to a transcoded inline-babel-call.
1829 Each filter is called with three arguments: the transcoded
1830 inline-babel-call, as a string, the back-end, as a symbol, and
1831 the communication channel, as a plist. It must return a string
1832 or nil.")
1834 (defvar org-export-filter-inline-src-block-functions nil
1835 "List of functions applied to a transcoded inline-src-block.
1836 Each filter is called with three arguments: the transcoded
1837 inline-src-block, as a string, the back-end, as a symbol, and the
1838 communication channel, as a plist. It must return a string or
1839 nil.")
1841 (defvar org-export-filter-latex-fragment-functions nil
1842 "List of functions applied to a transcoded latex-fragment.
1843 Each filter is called with three arguments: the transcoded
1844 latex-fragment, as a string, the back-end, as a symbol, and the
1845 communication channel, as a plist. It must return a string or
1846 nil.")
1848 (defvar org-export-filter-line-break-functions nil
1849 "List of functions applied to a transcoded line-break.
1850 Each filter is called with three arguments: the transcoded
1851 line-break, as a string, the back-end, as a symbol, and the
1852 communication channel, as a plist. It must return a string or
1853 nil.")
1855 (defvar org-export-filter-link-functions nil
1856 "List of functions applied to a transcoded link.
1857 Each filter is called with three arguments: the transcoded link,
1858 as a string, the back-end, as a symbol, and the communication
1859 channel, as a plist. It must return a string or nil.")
1861 (defvar org-export-filter-macro-functions nil
1862 "List of functions applied to a transcoded macro.
1863 Each filter is called with three arguments: the transcoded macro,
1864 as a string, the back-end, as a symbol, and the communication
1865 channel, as a plist. It must return a string or nil.")
1867 (defvar org-export-filter-radio-target-functions nil
1868 "List of functions applied to a transcoded radio-target.
1869 Each filter is called with three arguments: the transcoded
1870 radio-target, as a string, the back-end, as a symbol, and the
1871 communication channel, as a plist. It must return a string or
1872 nil.")
1874 (defvar org-export-filter-statistics-cookie-functions nil
1875 "List of functions applied to a transcoded statistics-cookie.
1876 Each filter is called with three arguments: the transcoded
1877 statistics-cookie, as a string, the back-end, as a symbol, and
1878 the communication channel, as a plist. It must return a string
1879 or nil.")
1881 (defvar org-export-filter-subscript-functions nil
1882 "List of functions applied to a transcoded subscript.
1883 Each filter is called with three arguments: the transcoded
1884 subscript, as a string, the back-end, as a symbol, and the
1885 communication channel, as a plist. It must return a string or
1886 nil.")
1888 (defvar org-export-filter-superscript-functions nil
1889 "List of functions applied to a transcoded superscript.
1890 Each filter is called with three arguments: the transcoded
1891 superscript, as a string, the back-end, as a symbol, and the
1892 communication channel, as a plist. It must return a string or
1893 nil.")
1895 (defvar org-export-filter-target-functions nil
1896 "List of functions applied to a transcoded target.
1897 Each filter is called with three arguments: the transcoded
1898 target, as a string, the back-end, as a symbol, and the
1899 communication channel, as a plist. It must return a string or
1900 nil.")
1902 (defvar org-export-filter-time-stamp-functions nil
1903 "List of functions applied to a transcoded time-stamp.
1904 Each filter is called with three arguments: the transcoded
1905 time-stamp, as a string, the back-end, as a symbol, and the
1906 communication channel, as a plist. It must return a string or
1907 nil.")
1909 (defvar org-export-filter-verbatim-functions nil
1910 "List of functions applied to a transcoded verbatim.
1911 Each filter is called with three arguments: the transcoded
1912 verbatim, as a string, the back-end, as a symbol, and the
1913 communication channel, as a plist. It must return a string or
1914 nil.")
1916 (defun org-export-filter-apply-functions (filters value backend info)
1917 "Call every function in FILTERS with arguments VALUE, BACKEND and INFO.
1918 Functions are called in a LIFO fashion, to be sure that developer
1919 specified filters, if any, are called first."
1920 (loop for filter in filters
1921 if (not value) return nil else
1922 do (setq value (funcall filter value backend info)))
1923 value)
1925 (defun org-export-install-filters (backend info)
1926 "Install filters properties in communication channel.
1928 BACKEND is a symbol specifying which back-end specific filters to
1929 install, if any. INFO is a plist containing the current
1930 communication channel.
1932 Return the updated communication channel."
1933 (let (plist)
1934 ;; Install user defined filters with `org-export-filters-alist'.
1935 (mapc (lambda (p)
1936 (setq plist (plist-put plist (car p) (eval (cdr p)))))
1937 org-export-filters-alist)
1938 ;; Prepend back-end specific filters to that list.
1939 (let ((back-end-filters (intern (format "org-%s-filters-alist" backend))))
1940 (when (boundp back-end-filters)
1941 (mapc (lambda (p)
1942 ;; Single values get consed, lists are prepended.
1943 (let ((key (car p)) (value (cdr p)))
1944 (when value
1945 (setq plist
1946 (plist-put
1947 plist key
1948 (if (atom value) (cons value (plist-get plist key))
1949 (append value (plist-get plist key))))))))
1950 (eval back-end-filters))))
1951 ;; Return new communication channel.
1952 (org-combine-plists info plist)))
1956 ;;; Core functions
1958 ;; This is the room for the main function, `org-export-as', along with
1959 ;; its derivatives, `org-export-to-buffer' and `org-export-to-file'.
1960 ;; They differ only by the way they output the resulting code.
1962 ;; `org-export-output-file-name' is an auxiliary function meant to be
1963 ;; used with `org-export-to-file'. With a given extension, it tries
1964 ;; to provide a canonical file name to write export output to.
1966 ;; Note that `org-export-as' doesn't really parse the current buffer,
1967 ;; but a copy of it (with the same buffer-local variables and
1968 ;; visibility), where include keywords are expanded and Babel blocks
1969 ;; are executed, if appropriate.
1970 ;; `org-export-with-current-buffer-copy' macro prepares that copy.
1972 ;; File inclusion is taken care of by
1973 ;; `org-export-expand-include-keyword' and
1974 ;; `org-export-prepare-file-contents'. Structure wise, including
1975 ;; a whole Org file in a buffer often makes little sense. For
1976 ;; example, if the file contains an headline and the include keyword
1977 ;; was within an item, the item should contain the headline. That's
1978 ;; why file inclusion should be done before any structure can be
1979 ;; associated to the file, that is before parsing.
1981 (defun org-export-as (backend
1982 &optional subtreep visible-only body-only ext-plist)
1983 "Transcode current Org buffer into BACKEND code.
1985 If narrowing is active in the current buffer, only transcode its
1986 narrowed part.
1988 If a region is active, transcode that region.
1990 When optional argument SUBTREEP is non-nil, transcode the
1991 sub-tree at point, extracting information from the headline
1992 properties first.
1994 When optional argument VISIBLE-ONLY is non-nil, don't export
1995 contents of hidden elements.
1997 When optional argument BODY-ONLY is non-nil, only return body
1998 code, without preamble nor postamble.
2000 EXT-PLIST, when provided, is a property list with external
2001 parameters overriding Org default settings, but still inferior to
2002 file-local settings.
2004 Return code as a string."
2005 (save-excursion
2006 (save-restriction
2007 ;; Narrow buffer to an appropriate region for parsing.
2008 (when (org-region-active-p)
2009 (narrow-to-region (region-beginning) (region-end)))
2010 (when (and subtreep (not (org-at-heading-p)))
2011 ;; Ensure point is at sub-tree's beginning.
2012 (org-narrow-to-subtree))
2013 ;; Retrieve export options (INFO) and parsed tree (RAW-DATA),
2014 ;; Then options can be completed with tree properties. Note:
2015 ;; Buffer isn't parsed directly. Instead, a temporary copy is
2016 ;; created, where include keywords are expanded and code blocks
2017 ;; are evaluated. RAW-DATA is the parsed tree of the buffer
2018 ;; resulting from that process. Eventually call
2019 ;; `org-export-filter-parse-tree-functions'.
2020 (goto-char (point-min))
2021 (let ((info (org-export-get-environment backend subtreep ext-plist)))
2022 ;; Remove subtree's headline from contents if subtree mode is
2023 ;; activated.
2024 (when subtreep (forward-line) (narrow-to-region (point) (point-max)))
2025 ;; Install filters in communication channel.
2026 (setq info (org-export-install-filters backend info))
2027 (let ((raw-data (org-export-filter-apply-functions
2028 (plist-get info :filter-parse-tree)
2029 (org-export-with-current-buffer-copy
2030 (org-export-expand-include-keyword nil)
2031 (let ((org-current-export-file (current-buffer)))
2032 (org-export-blocks-preprocess))
2033 (org-element-parse-buffer nil visible-only))
2034 backend info)))
2035 ;; Complete communication channel with tree properties.
2036 (setq info
2037 (org-combine-plists
2038 info
2039 (org-export-collect-tree-properties raw-data info backend)))
2040 ;; Transcode RAW-DATA. Also call
2041 ;; `org-export-filter-final-output-functions'.
2042 (let* ((body (org-element-normalize-string
2043 (org-export-data raw-data backend info)))
2044 (template (intern (format "org-%s-template" backend)))
2045 (output (org-export-filter-apply-functions
2046 (plist-get info :filter-final-output)
2047 (if (or (not (fboundp template)) body-only) body
2048 (funcall template body info))
2049 backend info)))
2050 ;; Maybe add final OUTPUT to kill ring before returning it.
2051 (when org-export-copy-to-kill-ring (org-kill-new output))
2052 output))))))
2054 (defun org-export-to-buffer (backend buffer &optional subtreep visible-only
2055 body-only ext-plist)
2056 "Call `org-export-as' with output to a specified buffer.
2058 BACKEND is the back-end used for transcoding, as a symbol.
2060 BUFFER is the output buffer. If it already exists, it will be
2061 erased first, otherwise, it will be created.
2063 Arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and EXT-PLIST are
2064 similar to those used in `org-export-as', which see.
2066 Return buffer."
2067 (let ((out (org-export-as backend subtreep visible-only body-only ext-plist))
2068 (buffer (get-buffer-create buffer)))
2069 (with-current-buffer buffer
2070 (erase-buffer)
2071 (insert out)
2072 (goto-char (point-min)))
2073 buffer))
2075 (defun org-export-to-file
2076 (backend file &optional subtreep visible-only body-only ext-plist)
2077 "Call `org-export-as' with output to a specified file.
2079 BACKEND is the back-end used for transcoding, as a symbol. FILE
2080 is the name of the output file, as a string.
2082 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and
2083 EXT-PLIST are similar to those used in `org-export-as', which
2084 see.
2086 Return output file's name."
2087 ;; Checks for FILE permissions. `write-file' would do the same, but
2088 ;; we'd rather avoid needless transcoding of parse tree.
2089 (unless (file-writable-p file) (error "Output file not writable"))
2090 ;; Insert contents to a temporary buffer and write it to FILE.
2091 (let ((out (org-export-as
2092 backend subtreep visible-only body-only ext-plist)))
2093 (with-temp-buffer
2094 (insert out)
2095 (let ((coding-system-for-write org-export-coding-system))
2096 (write-file file))))
2097 ;; Return full path.
2098 file)
2100 (defun org-export-output-file-name (extension &optional subtreep pub-dir)
2101 "Return output file's name according to buffer specifications.
2103 EXTENSION is a string representing the output file extension,
2104 with the leading dot.
2106 With a non-nil optional argument SUBTREEP, try to determine
2107 output file's name by looking for \"EXPORT_FILE_NAME\" property
2108 of subtree at point.
2110 When optional argument PUB-DIR is set, use it as the publishing
2111 directory.
2113 Return file name as a string, or nil if it couldn't be
2114 determined."
2115 (let ((base-name
2116 ;; File name may come from EXPORT_FILE_NAME subtree property,
2117 ;; assuming point is at beginning of said sub-tree.
2118 (file-name-sans-extension
2119 (or (and subtreep
2120 (org-entry-get
2121 (save-excursion
2122 (ignore-errors
2123 (org-back-to-heading (not visible-only)) (point)))
2124 "EXPORT_FILE_NAME" t))
2125 ;; File name may be extracted from buffer's associated
2126 ;; file, if any.
2127 (buffer-file-name (buffer-base-buffer))
2128 ;; Can't determine file name on our own: Ask user.
2129 (let ((read-file-name-function
2130 (and org-completion-use-ido 'ido-read-file-name)))
2131 (read-file-name
2132 "Output file: " pub-dir nil nil nil
2133 (lambda (name)
2134 (string= (file-name-extension name t) extension))))))))
2135 ;; Build file name. Enforce EXTENSION over whatever user may have
2136 ;; come up with. PUB-DIR, if defined, always has precedence over
2137 ;; any provided path.
2138 (cond
2139 (pub-dir
2140 (concat (file-name-as-directory pub-dir)
2141 (file-name-nondirectory base-name)
2142 extension))
2143 ((string= (file-name-nondirectory base-name) base-name)
2144 (concat (file-name-as-directory ".") base-name extension))
2145 (t (concat base-name extension)))))
2147 (defmacro org-export-with-current-buffer-copy (&rest body)
2148 "Apply BODY in a copy of the current buffer.
2150 The copy preserves local variables and visibility of the original
2151 buffer.
2153 Point is at buffer's beginning when BODY is applied."
2154 (org-with-gensyms (original-buffer offset buffer-string overlays)
2155 `(let ((,original-buffer ,(current-buffer))
2156 (,offset ,(1- (point-min)))
2157 (,buffer-string ,(buffer-string))
2158 (,overlays (mapcar
2159 'copy-overlay (overlays-in (point-min) (point-max)))))
2160 (with-temp-buffer
2161 (let ((buffer-invisibility-spec nil))
2162 (org-clone-local-variables
2163 ,original-buffer
2164 "^\\(org-\\|orgtbl-\\|major-mode$\\|outline-\\(regexp\\|level\\)$\\)")
2165 (insert ,buffer-string)
2166 (mapc (lambda (ov)
2167 (move-overlay
2169 (- (overlay-start ov) ,offset)
2170 (- (overlay-end ov) ,offset)
2171 (current-buffer)))
2172 ,overlays)
2173 (goto-char (point-min))
2174 (progn ,@body))))))
2175 (def-edebug-spec org-export-with-current-buffer-copy (body))
2177 (defun org-export-expand-include-keyword (included)
2178 "Expand every include keyword in buffer.
2179 INCLUDED is a list of included file names along with their line
2180 restriction, when appropriate. It is used to avoid infinite
2181 recursion."
2182 (let ((case-fold-search nil))
2183 (goto-char (point-min))
2184 (while (re-search-forward "^[ \t]*#\\+include: \\(.*\\)" nil t)
2185 (when (eq (car (save-match-data (org-element-at-point))) 'keyword)
2186 (beginning-of-line)
2187 ;; Extract arguments from keyword's value.
2188 (let* ((value (match-string 1))
2189 (ind (org-get-indentation))
2190 (file (and (string-match "^\"\\(\\S-+\\)\"" value)
2191 (prog1 (expand-file-name (match-string 1 value))
2192 (setq value (replace-match "" nil nil value)))))
2193 (lines
2194 (and (string-match
2195 ":lines +\"\\(\\(?:[0-9]+\\)?-\\(?:[0-9]+\\)?\\)\"" value)
2196 (prog1 (match-string 1 value)
2197 (setq value (replace-match "" nil nil value)))))
2198 (env (cond ((string-match "\\<example\\>" value) 'example)
2199 ((string-match "\\<src\\(?: +\\(.*\\)\\)?" value)
2200 (match-string 1 value))))
2201 ;; Minimal level of included file defaults to the child
2202 ;; level of the current headline, if any, or one. It
2203 ;; only applies is the file is meant to be included as
2204 ;; an Org one.
2205 (minlevel
2206 (and (not env)
2207 (if (string-match ":minlevel +\\([0-9]+\\)" value)
2208 (prog1 (string-to-number (match-string 1 value))
2209 (setq value (replace-match "" nil nil value)))
2210 (let ((cur (org-current-level)))
2211 (if cur (1+ (org-reduced-level cur)) 1))))))
2212 ;; Remove keyword.
2213 (delete-region (point) (progn (forward-line) (point)))
2214 (cond
2215 ((not (file-readable-p file)) (error "Cannot include file %s" file))
2216 ;; Check if files has already been parsed. Look after
2217 ;; inclusion lines too, as different parts of the same file
2218 ;; can be included too.
2219 ((member (list file lines) included)
2220 (error "Recursive file inclusion: %s" file))
2222 (cond
2223 ((eq env 'example)
2224 (insert
2225 (let ((ind-str (make-string ind ? ))
2226 (contents
2227 ;; Protect sensitive contents with commas.
2228 (replace-regexp-in-string
2229 "\\(^\\)\\([*]\\|[ \t]*#\\+\\)" ","
2230 (org-export-prepare-file-contents file lines)
2231 nil nil 1)))
2232 (format "%s#+begin_example\n%s%s#+end_example\n"
2233 ind-str contents ind-str))))
2234 ((stringp env)
2235 (insert
2236 (let ((ind-str (make-string ind ? ))
2237 (contents
2238 ;; Protect sensitive contents with commas.
2239 (replace-regexp-in-string
2240 (if (string= env "org") "\\(^\\)\\(.\\)"
2241 "\\(^\\)\\([*]\\|[ \t]*#\\+\\)") ","
2242 (org-export-prepare-file-contents file lines)
2243 nil nil 1)))
2244 (format "%s#+begin_src %s\n%s%s#+end_src\n"
2245 ind-str env contents ind-str))))
2247 (insert
2248 (with-temp-buffer
2249 (org-mode)
2250 (insert
2251 (org-export-prepare-file-contents file lines ind minlevel))
2252 (org-export-expand-include-keyword
2253 (cons (list file lines) included))
2254 (buffer-string))))))))))))
2256 (defun org-export-prepare-file-contents (file &optional lines ind minlevel)
2257 "Prepare the contents of FILE for inclusion and return them as a string.
2259 When optional argument LINES is a string specifying a range of
2260 lines, include only those lines.
2262 Optional argument IND, when non-nil, is an integer specifying the
2263 global indentation of returned contents. Since its purpose is to
2264 allow an included file to stay in the same environment it was
2265 created \(i.e. a list item), it doesn't apply past the first
2266 headline encountered.
2268 Optional argument MINLEVEL, when non-nil, is an integer
2269 specifying the level that any top-level headline in the included
2270 file should have."
2271 (with-temp-buffer
2272 (insert-file-contents file)
2273 (when lines
2274 (let* ((lines (split-string lines "-"))
2275 (lbeg (string-to-number (car lines)))
2276 (lend (string-to-number (cadr lines)))
2277 (beg (if (zerop lbeg) (point-min)
2278 (goto-char (point-min))
2279 (forward-line (1- lbeg))
2280 (point)))
2281 (end (if (zerop lend) (point-max)
2282 (goto-char (point-min))
2283 (forward-line (1- lend))
2284 (point))))
2285 (narrow-to-region beg end)))
2286 ;; Remove blank lines at beginning and end of contents. The logic
2287 ;; behind that removal is that blank lines around include keyword
2288 ;; override blank lines in included file.
2289 (goto-char (point-min))
2290 (org-skip-whitespace)
2291 (beginning-of-line)
2292 (delete-region (point-min) (point))
2293 (goto-char (point-max))
2294 (skip-chars-backward " \r\t\n")
2295 (forward-line)
2296 (delete-region (point) (point-max))
2297 ;; If IND is set, preserve indentation of include keyword until
2298 ;; the first headline encountered.
2299 (when ind
2300 (unless (eq major-mode 'org-mode) (org-mode))
2301 (goto-char (point-min))
2302 (let ((ind-str (make-string ind ? )))
2303 (while (not (or (eobp) (looking-at org-outline-regexp-bol)))
2304 ;; Do not move footnote definitions out of column 0.
2305 (unless (and (looking-at org-footnote-definition-re)
2306 (eq (car (org-element-at-point)) 'footnote-definition))
2307 (insert ind-str))
2308 (forward-line))))
2309 ;; When MINLEVEL is specified, compute minimal level for headlines
2310 ;; in the file (CUR-MIN), and remove stars to each headline so
2311 ;; that headlines with minimal level have a level of MINLEVEL.
2312 (when minlevel
2313 (unless (eq major-mode 'org-mode) (org-mode))
2314 (let ((levels (org-map-entries
2315 (lambda () (org-reduced-level (org-current-level))))))
2316 (when levels
2317 (let ((offset (- minlevel (apply 'min levels))))
2318 (unless (zerop offset)
2319 (when org-odd-levels-only (setq offset (* offset 2)))
2320 ;; Only change stars, don't bother moving whole
2321 ;; sections.
2322 (org-map-entries
2323 (lambda () (if (< offset 0) (delete-char (abs offset))
2324 (insert (make-string offset ?*))))))))))
2325 (buffer-string)))
2328 ;;; Tools For Back-Ends
2330 ;; A whole set of tools is available to help build new exporters. Any
2331 ;; function general enough to have its use across many back-ends
2332 ;; should be added here.
2334 ;; As of now, functions operating on footnotes, headlines, links,
2335 ;; macros, references, src-blocks, tables and tables of contents are
2336 ;; implemented.
2338 ;;;; For Footnotes
2340 ;; `org-export-collect-footnote-definitions' is a tool to list
2341 ;; actually used footnotes definitions in the whole parse tree, or in
2342 ;; an headline, in order to add footnote listings throughout the
2343 ;; transcoded data.
2345 ;; `org-export-footnote-first-reference-p' is a predicate used by some
2346 ;; back-ends, when they need to attach the footnote definition only to
2347 ;; the first occurrence of the corresponding label.
2349 ;; `org-export-get-footnote-definition' and
2350 ;; `org-export-get-footnote-number' provide easier access to
2351 ;; additional information relative to a footnote reference.
2353 (defun org-export-collect-footnote-definitions (data info)
2354 "Return an alist between footnote numbers, labels and definitions.
2356 DATA is the parse tree from which definitions are collected.
2357 INFO is the plist used as a communication channel.
2359 Definitions are sorted by order of references. They either
2360 appear as Org data \(transcoded with `org-export-data'\) or as
2361 a secondary string for inlined footnotes \(transcoded with
2362 `org-export-secondary-string'\). Unreferenced definitions are
2363 ignored."
2364 (let (refs)
2365 ;; Collect seen references in REFS.
2366 (org-element-map
2367 data 'footnote-reference
2368 (lambda (footnote local)
2369 (when (org-export-footnote-first-reference-p footnote local)
2370 (list (org-export-get-footnote-number footnote local)
2371 (org-element-get-property :label footnote)
2372 (org-export-get-footnote-definition footnote local))))
2373 info)))
2375 (defun org-export-footnote-first-reference-p (footnote-reference info)
2376 "Non-nil when a footnote reference is the first one for its label.
2378 FOOTNOTE-REFERENCE is the footnote reference being considered.
2379 INFO is the plist used as a communication channel."
2380 (let ((label (org-element-get-property :label footnote-reference)))
2381 (or (not label)
2382 (equal
2383 footnote-reference
2384 (org-element-map
2385 (plist-get info :parse-tree) 'footnote-reference
2386 (lambda (footnote local)
2387 (when (string= (org-element-get-property :label footnote) label)
2388 footnote))
2389 info 'first-match)))))
2391 (defun org-export-get-footnote-definition (footnote-reference info)
2392 "Return definition of FOOTNOTE-REFERENCE as parsed data.
2393 INFO is the plist used as a communication channel."
2394 (let ((label (org-element-get-property :label footnote-reference)))
2395 (or (org-element-get-property :inline-definition footnote-reference)
2396 (cdr (assoc label (plist-get info :footnote-definition-alist))))))
2398 (defun org-export-get-footnote-number (footnote info)
2399 "Return number associated to a footnote.
2401 FOOTNOTE is either a footnote reference or a footnote definition.
2402 INFO is the plist used as a communication channel."
2403 (let ((label (org-element-get-property :label footnote)) seen-refs)
2404 (org-element-map
2405 (plist-get info :parse-tree) 'footnote-reference
2406 (lambda (fn local)
2407 (let ((fn-lbl (org-element-get-property :label fn)))
2408 (cond
2409 ((and (not fn-lbl) (equal fn footnote)) (1+ (length seen-refs)))
2410 ((and label (string= label fn-lbl)) (1+ (length seen-refs)))
2411 ;; Anonymous footnote: it's always a new one. Also, be sure
2412 ;; to return nil from the `cond' so `first-match' doesn't
2413 ;; get us out of the loop.
2414 ((not fn-lbl) (push 'inline seen-refs) nil)
2415 ;; Label not seen so far: add it so SEEN-REFS. Again,
2416 ;; return nil to stay in the loop.
2417 ((not (member fn-lbl seen-refs)) (push fn-lbl seen-refs) nil))))
2418 info 'first-match)))
2421 ;;;; For Headlines
2423 ;; `org-export-get-relative-level' is a shortcut to get headline
2424 ;; level, relatively to the lower headline level in the parsed tree.
2426 ;; `org-export-get-headline-number' returns the section number of an
2427 ;; headline, while `org-export-number-to-roman' allows to convert it
2428 ;; to roman numbers.
2430 ;; `org-export-low-level-p', `org-export-first-sibling-p' and
2431 ;; `org-export-last-sibling-p' are three useful predicates when it
2432 ;; comes to fulfill the `:headline-levels' property.
2434 (defun org-export-get-relative-level (headline info)
2435 "Return HEADLINE relative level within current parsed tree.
2436 INFO is a plist holding contextual information."
2437 (+ (org-element-get-property :level headline)
2438 (or (plist-get info :headline-offset) 0)))
2440 (defun org-export-low-level-p (headline info)
2441 "Non-nil when HEADLINE is considered as low level.
2443 INFO is a plist used as a communication channel.
2445 A low level headlines has a relative level greater than
2446 `:headline-levels' property value.
2448 Return value is the difference between HEADLINE relative level
2449 and the last level being considered as high enough, or nil."
2450 (let ((limit (plist-get info :headline-levels)))
2451 (when (wholenump limit)
2452 (let ((level (org-export-get-relative-level headline info)))
2453 (and (> level limit) (- level limit))))))
2455 (defun org-export-get-headline-number (headline info)
2456 "Return HEADLINE numbering as a list of numbers.
2457 INFO is a plist holding contextual information."
2458 (cdr (assoc headline (plist-get info :headline-numbering))))
2460 (defun org-export-number-to-roman (n)
2461 "Convert integer N into a roman numeral."
2462 (let ((roman '((1000 . "M") (900 . "CM") (500 . "D") (400 . "CD")
2463 ( 100 . "C") ( 90 . "XC") ( 50 . "L") ( 40 . "XL")
2464 ( 10 . "X") ( 9 . "IX") ( 5 . "V") ( 4 . "IV")
2465 ( 1 . "I")))
2466 (res ""))
2467 (if (<= n 0)
2468 (number-to-string n)
2469 (while roman
2470 (if (>= n (caar roman))
2471 (setq n (- n (caar roman))
2472 res (concat res (cdar roman)))
2473 (pop roman)))
2474 res)))
2476 (defun org-export-first-sibling-p (headline info)
2477 "Non-nil when HEADLINE is the first sibling in its sub-tree.
2478 INFO is the plist used as a communication channel."
2479 (not (eq (car (org-export-get-previous-element headline info)) 'headline)))
2481 (defun org-export-last-sibling-p (headline info)
2482 "Non-nil when HEADLINE is the last sibling in its sub-tree.
2483 INFO is the plist used as a communication channel."
2484 (equal
2485 (car (last (org-element-get-contents (car (plist-get info :genealogy)))))
2486 headline))
2489 ;;;; For Links
2491 ;; `org-export-solidify-link-text' turns a string into a safer version
2492 ;; for links, replacing most non-standard characters with hyphens.
2494 ;; `org-export-get-coderef-format' returns an appropriate format
2495 ;; string for coderefs.
2497 ;; `org-export-inline-image-p' returns a non-nil value when the link
2498 ;; provided should be considered as an inline image.
2500 ;; `org-export-resolve-fuzzy-link' searches destination of fuzzy links
2501 ;; (i.e. links with "fuzzy" as type) within the parsed tree, and
2502 ;; returns an appropriate unique identifier when found, or nil.
2504 ;; `org-export-resolve-id-link' returns the first headline with
2505 ;; specified id or custom-id in parse tree, or nil when none was
2506 ;; found.
2508 ;; `org-export-resolve-coderef' associates a reference to a line
2509 ;; number in the element it belongs, or returns the reference itself
2510 ;; when the element isn't numbered.
2512 (defun org-export-solidify-link-text (s)
2513 "Take link text S and make a safe target out of it."
2514 (save-match-data
2515 (mapconcat 'identity (org-split-string s "[^a-zA-Z0-9_\\.-]+") "-")))
2517 (defun org-export-get-coderef-format (path desc)
2518 "Return format string for code reference link.
2519 PATH is the link path. DESC is its description."
2520 (save-match-data
2521 (cond ((string-match (regexp-quote (concat "(" path ")")) desc)
2522 (replace-match "%s" t t desc))
2523 ((string= desc "") "%s")
2524 (t desc))))
2526 (defun org-export-inline-image-p (link &optional rules)
2527 "Non-nil if LINK object points to an inline image.
2529 Optional argument is a set of RULES defining inline images. It
2530 is an alist where associations have the following shape:
2532 \(TYPE . REGEXP)
2534 Applying a rule means apply REGEXP against LINK's path when its
2535 type is TYPE. The function will return a non-nil value if any of
2536 the provided rules is non-nil. The default rule is
2537 `org-export-default-inline-image-rule'.
2539 This only applies to links without a description."
2540 (and (not (org-element-get-contents link))
2541 (let ((case-fold-search t)
2542 (rules (or rules org-export-default-inline-image-rule)))
2543 (some
2544 (lambda (rule)
2545 (and (string= (org-element-get-property :type link) (car rule))
2546 (string-match (cdr rule)
2547 (org-element-get-property :path link))))
2548 rules))))
2550 (defun org-export-resolve-fuzzy-link (link info)
2551 "Return LINK destination.
2553 INFO is a plist holding contextual information.
2555 Return value can be an object, an element, or nil:
2557 - If LINK path exactly matches any target, return the target
2558 object.
2560 - If LINK path exactly matches any headline name, return that
2561 element. If more than one headline share that name, priority
2562 will be given to the one with the closest common ancestor, if
2563 any, or the first one in the parse tree otherwise.
2565 - Otherwise, return nil.
2567 Assume LINK type is \"fuzzy\"."
2568 (let ((path (org-element-get-property :path link)))
2569 ;; Link points to a target: return it.
2570 (or (loop for target in (plist-get info :target-list)
2571 when (string= (org-element-get-property :raw-value target) path)
2572 return target)
2573 ;; Link either points to an headline or nothing. Try to find
2574 ;; the source, with priority given to headlines with the closest
2575 ;; common ancestor. If such candidate is found, return its
2576 ;; beginning position as an unique identifier, otherwise return
2577 ;; nil.
2578 (let ((find-headline
2579 (function
2580 ;; Return first headline whose `:raw-value' property
2581 ;; is NAME in parse tree DATA, or nil.
2582 (lambda (name data)
2583 (org-element-map
2584 data 'headline
2585 (lambda (headline local)
2586 (when (string=
2587 (org-element-get-property :raw-value headline)
2588 name)
2589 headline))
2590 info 'first-match)))))
2591 ;; Search among headlines sharing an ancestor with link,
2592 ;; from closest to farthest.
2593 (or (catch 'exit
2594 (mapc
2595 (lambda (parent)
2596 (when (eq (car parent) 'headline)
2597 (let ((foundp (funcall find-headline path parent)))
2598 (when foundp (throw 'exit foundp)))))
2599 (plist-get info :genealogy)) nil)
2600 ;; No match with a common ancestor: try the full parse-tree.
2601 (funcall find-headline path (plist-get info :parse-tree)))))))
2603 (defun org-export-resolve-id-link (link info)
2604 "Return headline referenced as LINK destination.
2606 INFO is a plist used as a communication channel.
2608 Return value can be an headline element or nil. Assume LINK type
2609 is either \"id\" or \"custom-id\"."
2610 (let ((id (org-element-get-property :path link)))
2611 (org-element-map
2612 (plist-get info :parse-tree) 'headline
2613 (lambda (headline local)
2614 (when (or (string= (org-element-get-property :id headline) id)
2615 (string= (org-element-get-property :custom-id headline) id))
2616 headline))
2617 info 'first-match)))
2619 (defun org-export-resolve-coderef (ref info)
2620 "Resolve a code reference REF.
2622 INFO is a plist used as a communication channel.
2624 Return associated line number in source code, or REF itself,
2625 depending on src-block or example element's switches."
2626 (org-element-map
2627 (plist-get info :parse-tree) '(src-block example)
2628 (lambda (el local)
2629 (let ((switches (or (org-element-get-property :switches el) "")))
2630 (with-temp-buffer
2631 (insert (org-trim (org-element-get-property :value el)))
2632 ;; Build reference regexp.
2633 (let* ((label
2634 (or (and (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
2635 (match-string 1 switches))
2636 org-coderef-label-format))
2637 (ref-re
2638 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
2639 (replace-regexp-in-string "%s" ref label nil t))))
2640 ;; Element containing REF is found. Only associate REF to
2641 ;; a line number if element has "+n" or "-n" and "-k" or
2642 ;; "-r" as switches. When it has "+n", count accumulated
2643 ;; locs before, too.
2644 (when (re-search-backward ref-re nil t)
2645 (cond
2646 ((not (string-match "-[kr]\\>" switches)) ref)
2647 ((string-match "-n\\>" switches) (line-number-at-pos))
2648 ((string-match "\\+n\\>" switches)
2649 (+ (org-export-get-loc el local) (line-number-at-pos)))
2650 (t ref)))))))
2651 info 'first-match))
2654 ;;;; For Macros
2656 ;; `org-export-expand-macro' simply takes care of expanding macros.
2658 (defun org-export-expand-macro (macro info)
2659 "Expand MACRO and return it as a string.
2660 INFO is a plist holding export options."
2661 (let* ((key (org-element-get-property :key macro))
2662 (args (org-element-get-property :args macro))
2663 ;; User's macros are stored in the communication channel with
2664 ;; a ":macro-" prefix.
2665 (value (plist-get info (intern (format ":macro-%s" key)))))
2666 ;; Replace arguments in VALUE. A nil VALUE removes the macro call
2667 ;; from export.
2668 (when (stringp value)
2669 (let ((s 0) n)
2670 (while (string-match "\\$\\([0-9]+\\)" value s)
2671 (setq s (1+ (match-beginning 0))
2672 n (string-to-number (match-string 1 value)))
2673 (and (>= (length args) n)
2674 (setq value (replace-match (nth (1- n) args) t t value)))))
2675 ;; VALUE starts with "(eval": it is a s-exp, `eval' it.
2676 (when (string-match "\\`(eval\\>" value)
2677 (setq value (eval (read value))))
2678 ;; Return string.
2679 (format "%s" (or value "")))))
2682 ;;;; For References
2684 ;; `org-export-get-ordinal' associates a sequence number to any object
2685 ;; or element.
2687 (defun org-export-get-ordinal
2688 (element info &optional types within-section predicate)
2689 "Return ordinal number of an element or object.
2691 ELEMENT is the element or object considered. INFO is the plist
2692 used as a communication channel.
2694 Optional argument TYPES, when non-nil, is a list of element or
2695 object types, as symbols, that should also be counted in.
2696 Otherwise, only provided element's type is considered.
2698 When optional argument WITHIN-SECTION is non-nil, narrow counting
2699 to the section containing ELEMENT.
2701 Optional argument PREDICATE is a function returning a non-nil
2702 value if the current element or object should be counted in. It
2703 accepts one argument: the element or object being considered.
2704 This argument allows to count only a certain type of objects,
2705 like inline images, which are a subset of links \(in that case,
2706 `org-export-inline-image-p' might be an useful predicate\)."
2707 (let ((counter 0)
2708 ;; Determine if search should apply to current section, in
2709 ;; which case it should be retrieved first, or to full parse
2710 ;; tree. As a special case, an element or object without
2711 ;; a parent headline will also trigger a full search,
2712 ;; notwithstanding WITHIN-SECTION value.
2713 (data
2714 (if (not within-section) (plist-get info :parse-tree)
2715 (or (org-export-get-parent-headline element info)
2716 (plist-get info :parse-tree)))))
2717 ;; Increment counter until ELEMENT is found again.
2718 (org-element-map
2719 data (or types (car element))
2720 (lambda (el local)
2721 (cond
2722 ((equal element el) (1+ counter))
2723 ((not predicate) (incf counter) nil)
2724 ((funcall predicate el) (incf counter) nil)))
2725 info 'first-match)))
2728 ;;;; For Src-Blocks
2730 ;; `org-export-get-loc' counts number of code lines accumulated in
2731 ;; src-block or example-block elements with a "+n" switch until
2732 ;; a given element, excluded. Note: "-n" switches reset that count.
2734 ;; `org-export-handle-code' takes care of line numbering and reference
2735 ;; cleaning in source code, when appropriate.
2737 (defun org-export-get-loc (element info)
2738 "Return accumulated lines of code up to ELEMENT.
2740 INFO is the plist used as a communication channel.
2742 ELEMENT is excluded from count."
2743 (let ((loc 0))
2744 (org-element-map
2745 (plist-get info :parse-tree) `(src-block example-block ,(car element))
2746 (lambda (el local)
2747 (cond
2748 ;; ELEMENT is reached: Quit the loop.
2749 ((equal el element) t)
2750 ;; Only count lines from src-block and example-block elements
2751 ;; with a "+n" or "-n" switch. A "-n" switch resets counter.
2752 ((not (memq (car el) '(src-block example-block))) nil)
2753 ((let ((switches (org-element-get-property :switches el)))
2754 (when (and switches (string-match "\\([-+]\\)n\\>" switches))
2755 ;; Accumulate locs or reset them.
2756 (let ((accumulatep (string= (match-string 1 switches) "-"))
2757 (lines (org-count-lines
2758 (org-trim (org-element-get-property :value el)))))
2759 (setq loc (if accumulatep lines (+ loc lines))))))
2760 ;; Return nil to stay in the loop.
2761 nil)))
2762 info 'first-match)
2763 ;; Return value.
2764 loc))
2766 (defun org-export-handle-code (element info &optional num-fmt ref-fmt delayed)
2767 "Handle line numbers and code references in ELEMENT.
2769 ELEMENT has either a `src-block' an `example-block' type. INFO
2770 is a plist used as a communication channel.
2772 If optional argument NUM-FMT is a string, it will be used as
2773 a format string for numbers at beginning of each line.
2775 If optional argument REF-FMT is a string, it will be used as
2776 a format string for each line of code containing a reference.
2778 When optional argument DELAYED is non-nil, `org-loc' and
2779 `org-coderef' properties, set to an adequate value, are applied
2780 to, respectively, numbered lines and lines with a reference. No
2781 line numbering is done and all references are stripped from the
2782 resulting string. Both NUM-FMT and REF-FMT arguments are ignored
2783 in that situation.
2785 Return new code as a string."
2786 (let* ((switches (or (org-element-get-property :switches element) ""))
2787 (code (org-element-get-property :value element))
2788 (numberp (string-match "[-+]n\\>" switches))
2789 (accumulatep (string-match "\\+n\\>" switches))
2790 ;; Initialize loc counter when any kind of numbering is
2791 ;; active.
2792 (total-LOC (cond
2793 (accumulatep (org-export-get-loc element info))
2794 (numberp 0)))
2795 ;; Get code and clean it. Remove blank lines at its
2796 ;; beginning and end. Also remove protective commas.
2797 (preserve-indent-p (or org-src-preserve-indentation
2798 (string-match "-i\\>" switches)))
2799 (replace-labels (when (string-match "-r\\>" switches)
2800 (if (string-match "-k\\>" switches) 'keep t)))
2801 (code (let ((c (replace-regexp-in-string
2802 "\\`\\([ \t]*\n\\)+" ""
2803 (replace-regexp-in-string
2804 "\\(:?[ \t]*\n\\)*[ \t]*\\'" "\n" code))))
2805 ;; If appropriate, remove global indentation.
2806 (unless preserve-indent-p (setq c (org-remove-indentation c)))
2807 ;; Free up the protected lines. Note: Org blocks
2808 ;; have commas at the beginning or every line.
2809 (if (string=
2810 (or (org-element-get-property :language element) "")
2811 "org")
2812 (replace-regexp-in-string "^," "" c)
2813 (replace-regexp-in-string
2814 "^\\(,\\)\\(:?\\*\\|[ \t]*#\\+\\)" "" c nil nil 1))))
2815 ;; Split code to process it line by line.
2816 (code-lines (org-split-string code "\n"))
2817 ;; If numbering is active, ensure line numbers will be
2818 ;; correctly padded before applying the format string.
2819 (num-fmt
2820 (when (and (not delayed) numberp)
2821 (format (if (stringp num-fmt) num-fmt "%s: ")
2822 (format "%%%ds"
2823 (length (number-to-string
2824 (+ (length code-lines) total-LOC)))))))
2825 ;; Get format used for references.
2826 (label-fmt (or (and (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
2827 (match-string 1 switches))
2828 org-coderef-label-format))
2829 ;; Build a regexp matching a loc with a reference.
2830 (with-ref-re (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
2831 (replace-regexp-in-string
2832 "%s" "\\([-a-zA-Z0-9_ ]+\\)" label-fmt nil t))))
2833 (org-element-normalize-string
2834 (mapconcat
2835 (lambda (loc)
2836 ;; Maybe add line number to current line of code (LOC).
2837 (when numberp
2838 (incf total-LOC)
2839 (setq loc (if delayed (org-add-props loc nil 'org-loc total-LOC)
2840 (concat (format num-fmt total-LOC) loc))))
2841 ;; Take action if at a ref line.
2842 (when (string-match with-ref-re loc)
2843 (let ((ref (match-string 3 loc)))
2844 (setq loc
2845 ;; Option "-r" without "-k" removes labels.
2846 ;; A non-nil DELAYED removes labels unconditionally.
2847 (if (or delayed
2848 (and replace-labels (not (eq replace-labels 'keep))))
2849 (replace-match "" nil nil loc 1)
2850 (replace-match (format "(%s)" ref) nil nil loc 2)))
2851 ;; Store REF in `org-coderef' property if DELAYED asks to.
2852 (cond (delayed (setq loc (org-add-props loc nil 'org-coderef ref)))
2853 ;; If REF-FMT is defined, apply it to current LOC.
2854 ((stringp ref-fmt) (setq loc (format ref-fmt loc))))))
2855 ;; Return updated LOC for concatenation.
2856 loc)
2857 code-lines "\n"))))
2860 ;;;; For Tables
2862 ;; `org-export-table-format-info' extracts formatting information
2863 ;; (alignment, column groups and presence of a special column) from
2864 ;; a raw table and returns it as a property list.
2866 ;; `org-export-clean-table' cleans the raw table from any Org
2867 ;; table-specific syntax.
2869 (defun org-export-table-format-info (table)
2870 "Extract info from TABLE.
2871 Return a plist whose properties and values are:
2872 `:alignment' vector of strings among \"r\", \"l\" and \"c\",
2873 `:column-groups' vector of symbols among `start', `end', `start-end',
2874 `:row-groups' list of integers representing row groups.
2875 `:special-column-p' non-nil if table has a special column.
2876 `:width' vector of integers representing desired width of
2877 current column, or nil."
2878 (with-temp-buffer
2879 (insert table)
2880 (goto-char 1)
2881 (org-table-align)
2882 (let ((align (vconcat (mapcar (lambda (c) (if c "r" "l"))
2883 org-table-last-alignment)))
2884 (width (make-vector (length org-table-last-alignment) nil))
2885 (colgroups (make-vector (length org-table-last-alignment) nil))
2886 (row-group 0)
2887 (rowgroups)
2888 (special-column-p 'empty))
2889 (mapc (lambda (row)
2890 (if (string-match "^[ \t]*|[-+]+|[ \t]*$" row)
2891 (incf row-group)
2892 ;; Determine if a special column is present by looking
2893 ;; for special markers in the first column. More
2894 ;; accurately, the first column is considered special
2895 ;; if it only contains special markers and, maybe,
2896 ;; empty cells.
2897 (setq special-column-p
2898 (cond
2899 ((not special-column-p) nil)
2900 ((string-match "^[ \t]*| *\\\\?\\([/#!$*_^]\\) *|" row)
2901 'special)
2902 ((string-match "^[ \t]*| +|" row) special-column-p))))
2903 (cond
2904 ;; Read forced alignment and width information, if any,
2905 ;; and determine final alignment for the table.
2906 ((org-table-cookie-line-p row)
2907 (let ((col 0))
2908 (mapc (lambda (field)
2909 (when (string-match
2910 "<\\([lrc]\\)?\\([0-9]+\\)?>" field)
2911 (let ((align-data (match-string 1 field)))
2912 (when align-data (aset align col align-data)))
2913 (let ((w-data (match-string 2 field)))
2914 (when w-data
2915 (aset width col (string-to-number w-data)))))
2916 (incf col))
2917 (org-split-string row "[ \t]*|[ \t]*"))))
2918 ;; Read column groups information.
2919 ((org-table-colgroup-line-p row)
2920 (let ((col 0))
2921 (mapc (lambda (field)
2922 (aset colgroups col
2923 (cond ((string= "<" field) 'start)
2924 ((string= ">" field) 'end)
2925 ((string= "<>" field) 'start-end)))
2926 (incf col))
2927 (org-split-string row "[ \t]*|[ \t]*"))))
2928 ;; Contents line.
2929 (t (push row-group rowgroups))))
2930 (org-split-string table "\n"))
2931 ;; Return plist.
2932 (list :alignment align
2933 :column-groups colgroups
2934 :row-groups (reverse rowgroups)
2935 :special-column-p (eq special-column-p 'special)
2936 :width width))))
2938 (defun org-export-clean-table (table specialp)
2939 "Clean string TABLE from its formatting elements.
2940 Remove any row containing column groups or formatting cookies and
2941 rows starting with a special marker. If SPECIALP is non-nil,
2942 assume the table contains a special formatting column and remove
2943 it also."
2944 (let ((rows (org-split-string table "\n")))
2945 (mapconcat 'identity
2946 (delq nil
2947 (mapcar
2948 (lambda (row)
2949 (cond
2950 ((org-table-colgroup-line-p row) nil)
2951 ((org-table-cookie-line-p row) nil)
2952 ;; Ignore rows starting with a special marker.
2953 ((string-match "^[ \t]*| *[!_^/] *|" row) nil)
2954 ;; Remove special column.
2955 ((and specialp
2956 (or (string-match "^\\([ \t]*\\)|-+\\+" row)
2957 (string-match "^\\([ \t]*\\)|[^|]*|" row)))
2958 (replace-match "\\1|" t nil row))
2959 (t row)))
2960 rows))
2961 "\n")))
2964 ;;;; For Tables Of Contents
2966 ;; `org-export-collect-headlines' builds a list of all exportable
2967 ;; headline elements, maybe limited to a certain depth. One can then
2968 ;; easily parse it and transcode it.
2970 ;; Building lists of tables, figures or listings is quite similar.
2971 ;; Once the generic function `org-export-collect-elements' is defined,
2972 ;; `org-export-collect-tables', `org-export-collect-figures' and
2973 ;; `org-export-collect-listings' can be derived from it.
2975 (defun org-export-collect-headlines (info &optional n)
2976 "Collect headlines in order to build a table of contents.
2978 INFO is a plist used as a communication channel.
2980 When non-nil, optional argument N must be an integer. It
2981 specifies the depth of the table of contents.
2983 Return a list of all exportable headlines as parsed elements."
2984 (org-element-map
2985 (plist-get info :parse-tree)
2986 'headline
2987 (lambda (headline local)
2988 ;; Strip contents from HEADLINE.
2989 (let ((relative-level (org-export-get-relative-level headline local)))
2990 (unless (and n (> relative-level n)) headline)))
2991 info))
2993 (defun org-export-collect-elements (type info &optional predicate)
2994 "Collect referenceable elements of a determined type.
2996 TYPE can be a symbol or a list of symbols specifying element
2997 types to search. Only elements with a caption or a name are
2998 collected.
3000 INFO is a plist used as a communication channel.
3002 When non-nil, optional argument PREDICATE is a function accepting
3003 one argument, an element of type TYPE. It returns a non-nil
3004 value when that element should be collected.
3006 Return a list of all elements found, in order of appearance."
3007 (org-element-map
3008 (plist-get info :parse-tree) type
3009 (lambda (element local)
3010 (and (or (org-element-get-property :caption element)
3011 (org-element-get-property :name element))
3012 (or (not predicate) (funcall predicate element))
3013 element)) info))
3015 (defun org-export-collect-tables (info)
3016 "Build a list of tables.
3018 INFO is a plist used as a communication channel.
3020 Return a list of table elements with a caption or a name
3021 affiliated keyword."
3022 (org-export-collect-elements 'table info))
3024 (defun org-export-collect-figures (info predicate)
3025 "Build a list of figures.
3027 INFO is a plist used as a communication channel. PREDICATE is
3028 a function which accepts one argument: a paragraph element and
3029 whose return value is non-nil when that element should be
3030 collected.
3032 A figure is a paragraph type element, with a caption or a name,
3033 verifying PREDICATE. The latter has to be provided since
3034 a \"figure\" is a vague concept that may depend on back-end.
3036 Return a list of elements recognized as figures."
3037 (org-export-collect-elements 'paragraph info predicate))
3039 (defun org-export-collect-listings (info)
3040 "Build a list of src blocks.
3042 INFO is a plist used as a communication channel.
3044 Return a list of src-block elements with a caption or a name
3045 affiliated keyword."
3046 (org-export-collect-elements 'src-block info))
3049 ;;;; Topology
3051 ;; Here are various functions to retrieve information about the
3052 ;; neighbourhood of a given element or object. Neighbours of interest
3053 ;; are parent headline (`org-export-get-parent-headline'), parent
3054 ;; paragraph (`org-export-get-parent-paragraph'), previous element or
3055 ;; object (`org-export-get-previous-element') and next element or
3056 ;; object (`org-export-get-next-element').
3058 ;; All of these functions are just a specific use of the more generic
3059 ;; `org-export-get-genealogy', which returns the genealogy relative to
3060 ;; the element or object.
3062 (defun org-export-get-genealogy (blob info)
3063 "Return genealogy relative to a given element or object.
3064 BLOB is the element or object being considered. INFO is a plist
3065 used as a communication channel."
3066 ;; LOCALP tells if current `:genealogy' is sufficient to find parent
3067 ;; headline, or if it should be computed.
3068 (let ((localp (member blob (org-element-get-contents
3069 (car (plist-get info :genealogy))))))
3070 (if localp (plist-get info :genealogy)
3071 (catch 'exit
3072 (org-element-map
3073 (plist-get info :parse-tree) (car blob)
3074 (lambda (el local) (when (equal el blob) (throw 'exit local)))
3075 info)))))
3077 (defun org-export-get-parent-headline (blob info)
3078 "Return closest parent headline or nil.
3080 BLOB is the element or object being considered. INFO is a plist
3081 used as a communication channel."
3082 (catch 'exit
3083 (mapc
3084 (lambda (el) (when (eq (car el) 'headline) (throw 'exit el)))
3085 (org-export-get-genealogy blob info))
3086 nil))
3088 (defun org-export-get-parent-paragraph (object info)
3089 "Return parent paragraph or nil.
3091 INFO is a plist used as a communication channel.
3093 Optional argument OBJECT, when provided, is the object to consider.
3094 Otherwise, return the paragraph containing current object.
3096 This is useful for objects, which share attributes with the
3097 paragraph containing them."
3098 (catch 'exit
3099 (mapc
3100 (lambda (el) (when (eq (car el) 'paragraph) (throw 'exit el)))
3101 (org-export-get-genealogy object info))
3102 nil))
3104 (defun org-export-get-previous-element (blob info)
3105 "Return previous element or object.
3107 BLOB is an element or object. INFO is a plist used as
3108 a communication channel.
3110 Return previous element or object, a string, or nil."
3111 (let ((parent (car (org-export-get-genealogy blob info))))
3112 (cadr (member blob (reverse (org-element-get-contents parent))))))
3114 (defun org-export-get-next-element (blob info)
3115 "Return next element or object.
3117 BLOB is an element or object. INFO is a plist used as
3118 a communication channel.
3120 Return next element or object, a string, or nil."
3121 (let ((parent (car (org-export-get-genealogy blob info))))
3122 (cadr (member blob (org-element-get-contents parent)))))
3126 ;;; The Dispatcher
3128 ;; `org-export-dispatch' is the standard interactive way to start an
3129 ;; export process. It uses `org-export-dispatch-ui' as a subroutine
3130 ;; for its interface. Most commons back-ends should have an entry in
3131 ;; it.
3133 (defun org-export-dispatch ()
3134 "Export dispatcher for Org mode.
3136 It provides an access to common export related tasks in a buffer.
3137 Its interface comes in two flavours: standard and expert. While
3138 both share the same set of bindings, only the former displays the
3139 valid keys associations. Set `org-export-dispatch-use-expert-ui'
3140 to switch to one or the other.
3142 Return an error if key pressed has no associated command."
3143 (interactive)
3144 (let* ((input (org-export-dispatch-ui
3145 (if (listp org-export-initial-scope) org-export-initial-scope
3146 (list org-export-initial-scope))
3147 org-export-dispatch-use-expert-ui))
3148 (raw-key (car input))
3149 (optns (cdr input)))
3150 ;; Translate "C-a", "C-b"... into "a", "b"... Then take action
3151 ;; depending on user's key pressed.
3152 (case (if (< raw-key 27) (+ raw-key 96) raw-key)
3153 ;; Export with `e-ascii' back-end.
3154 ((?A ?N ?U)
3155 (let ((outbuf
3156 (org-export-to-buffer
3157 'e-ascii "*Org E-ASCII Export*"
3158 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)
3159 `(:ascii-charset
3160 ,(case raw-key (?A 'ascii) (?N 'latin1) (t 'utf-8))))))
3161 (with-current-buffer outbuf (text-mode))
3162 (when org-export-show-temporary-export-buffer
3163 (switch-to-buffer-other-window outbuf))))
3164 ((?a ?n ?u)
3165 (org-e-ascii-export-to-ascii
3166 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)
3167 `(:ascii-charset ,(case raw-key (?a 'ascii) (?n 'latin1) (t 'utf-8)))))
3168 ;; Export with `e-latex' back-end.
3170 (let ((outbuf
3171 (org-export-to-buffer
3172 'e-latex "*Org E-LaTeX Export*"
3173 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))))
3174 (with-current-buffer outbuf (latex-mode))
3175 (when org-export-show-temporary-export-buffer
3176 (switch-to-buffer-other-window outbuf))))
3177 (?l (org-e-latex-export-to-latex
3178 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
3179 (?p (org-e-latex-export-to-pdf
3180 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
3181 (?d (org-open-file
3182 (org-e-latex-export-to-pdf
3183 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))))
3184 ;; Publishing facilities
3185 (?F (org-e-publish-current-file (memq 'force optns)))
3186 (?P (org-e-publish-current-project (memq 'force optns)))
3187 (?X (let ((project
3188 (assoc (org-icompleting-read
3189 "Publish project: " org-e-publish-project-alist nil t)
3190 org-e-publish-project-alist)))
3191 (org-e-publish project (memq 'force optns))))
3192 (?E (org-e-publish-all (memq 'force optns)))
3193 ;; Undefined command.
3194 (t (error "No command associated with key %s"
3195 (char-to-string raw-key))))))
3197 (defun org-export-dispatch-ui (options expertp)
3198 "Handle interface for `org-export-dispatch'.
3200 OPTIONS is a list containing current interactive options set for
3201 export. It can contain any of the following symbols:
3202 `body' toggles a body-only export
3203 `subtree' restricts export to current subtree
3204 `visible' restricts export to visible part of buffer.
3205 `force' force publishing files.
3207 EXPERTP, when non-nil, triggers expert UI. In that case, no help
3208 buffer is provided, but indications about currently active
3209 options are given in the prompt. Moreover, \[?] allows to switch
3210 back to standard interface.
3212 Return value is a list with key pressed as CAR and a list of
3213 final interactive export options as CDR."
3214 (let ((help
3215 (format "-------------------- General Options --------------------
3216 \[1] Body only: %s [2] Export scope: %s
3217 \[3] Visible only: %s [4] Force publishing: %s
3220 -------------- ASCII/Latin-1/UTF-8 Export ---------------
3221 \[a/n/u] to TXT file [A/N/U] to temporary buffer
3223 --------------------- LaTeX Export ----------------------
3224 \[l] to TEX file [L] to temporary buffer
3225 \[p] to PDF file [d] ... and open it
3227 ------------------------- Publish -------------------------
3228 \[F] current file [P] current project
3229 \[X] a project [E] every project"
3230 (if (memq 'body options) "On" "Off")
3231 (if (memq 'subtree options) "Subtree" "Buffer")
3232 (if (memq 'visible options) "On" "Off")
3233 (if (memq 'force options) "On" "Off")))
3234 (standard-prompt "Export command: ")
3235 (expert-prompt (format "Export command (%s%s%s%s): "
3236 (if (memq 'body options) "b" "-")
3237 (if (memq 'subtree options) "s" "-")
3238 (if (memq 'visible options) "v" "-")
3239 (if (memq 'force options) "f" "-")))
3240 (handle-keypress
3241 (function
3242 ;; Read a character from command input, toggling interactive
3243 ;; options when applicable. PROMPT is the displayed prompt,
3244 ;; as a string.
3245 (lambda (prompt)
3246 (let ((key (read-char-exclusive prompt)))
3247 (cond
3248 ;; Ignore non-standard characters (i.e. "M-a").
3249 ((not (characterp key)) (org-export-dispatch-ui options expertp))
3250 ;; Help key: Switch back to standard interface if
3251 ;; expert UI was active.
3252 ((eq key ??) (org-export-dispatch-ui options nil))
3253 ;; Toggle export options.
3254 ((memq key '(?1 ?2 ?3 ?4))
3255 (org-export-dispatch-ui
3256 (let ((option (case key (?1 'body) (?2 'subtree) (?3 'visible)
3257 (?4 'force))))
3258 (if (memq option options) (remq option options)
3259 (cons option options)))
3260 expertp))
3261 ;; Action selected: Send key and options back to
3262 ;; `org-export-dispatch'.
3263 (t (cons key options))))))))
3264 ;; With expert UI, just read key with a fancy prompt. In standard
3265 ;; UI, display an intrusive help buffer.
3266 (if expertp (funcall handle-keypress expert-prompt)
3267 (save-window-excursion
3268 (delete-other-windows)
3269 (with-output-to-temp-buffer "*Org Export/Publishing Help*" (princ help))
3270 (org-fit-window-to-buffer
3271 (get-buffer-window "*Org Export/Publishing Help*"))
3272 (funcall handle-keypress standard-prompt)))))
3275 (provide 'org-export)
3276 ;;; org-export.el ends here