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