org-drill: prevent errors caused by attempting to display inline images on a non...
[org-mode/org-mode-NeilSmithlineMods.git] / contrib / lisp / org-export.el
blob627c55ca181e714cb81c5719f13129e48386f8ab
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 "./org-e-ascii.el")
97 (require 'org-e-html "./org-e-html.el")
98 (require 'org-e-latex "./org-e-latex.el")
99 (require 'org-e-odt "./org-e-odt.el")
100 (require 'org-e-publish "./org-e-publish.el")
104 ;;; Internal Variables
106 ;; Among internal variables, the most important is
107 ;; `org-export-option-alist'. This variable define the global export
108 ;; options, shared between every exporter, and how they are acquired.
110 (defconst org-export-max-depth 19
111 "Maximum nesting depth for headlines, counting from 0.")
113 (defconst org-export-option-alist
114 '((:author "AUTHOR" nil user-full-name t)
115 (:creator "CREATOR" nil org-export-creator-string)
116 (:date "DATE" nil nil t)
117 (:description "DESCRIPTION" nil nil newline)
118 (:email "EMAIL" nil user-mail-address t)
119 (:exclude-tags "EXPORT_EXCLUDE_TAGS" nil org-export-exclude-tags split)
120 (:headline-levels nil "H" org-export-headline-levels)
121 (:keywords "KEYWORDS" nil nil space)
122 (:language "LANGUAGE" nil org-export-default-language t)
123 (:preserve-breaks nil "\\n" org-export-preserve-breaks)
124 (:section-numbers nil "num" org-export-with-section-numbers)
125 (:select-tags "EXPORT_SELECT_TAGS" nil org-export-select-tags split)
126 (:time-stamp-file nil "timestamp" org-export-time-stamp-file)
127 (:title "TITLE" nil nil space)
128 (:with-archived-trees nil "arch" org-export-with-archived-trees)
129 (:with-author nil "author" org-export-with-author)
130 (:with-clocks nil "c" org-export-with-clocks)
131 (:with-creator nil "creator" org-export-with-creator)
132 (:with-drawers nil "d" org-export-with-drawers)
133 (:with-email nil "email" org-export-with-email)
134 (:with-emphasize nil "*" org-export-with-emphasize)
135 (:with-entities nil "e" org-export-with-entities)
136 (:with-fixed-width nil ":" org-export-with-fixed-width)
137 (:with-footnotes nil "f" org-export-with-footnotes)
138 (:with-plannings nil "p" org-export-with-planning)
139 (:with-priority nil "pri" org-export-with-priority)
140 (:with-special-strings nil "-" org-export-with-special-strings)
141 (:with-sub-superscript nil "^" org-export-with-sub-superscripts)
142 (:with-toc nil "toc" org-export-with-toc)
143 (:with-tables nil "|" org-export-with-tables)
144 (:with-tags nil "tags" org-export-with-tags)
145 (:with-tasks nil "tasks" org-export-with-tasks)
146 (:with-timestamps nil "<" org-export-with-timestamps)
147 (:with-todo-keywords nil "todo" org-export-with-todo-keywords))
148 "Alist between export properties and ways to set them.
150 The CAR of the alist is the property name, and the CDR is a list
151 like (KEYWORD OPTION DEFAULT BEHAVIOUR) where:
153 KEYWORD is a string representing a buffer keyword, or nil.
154 OPTION is a string that could be found in an #+OPTIONS: line.
155 DEFAULT is the default value for the property.
156 BEHAVIOUR determine how Org should handle multiple keywords for
157 the same property. It is a symbol among:
158 nil Keep old value and discard the new one.
159 t Replace old value with the new one.
160 `space' Concatenate the values, separating them with a space.
161 `newline' Concatenate the values, separating them with
162 a newline.
163 `split' Split values at white spaces, and cons them to the
164 previous list.
166 KEYWORD and OPTION have precedence over DEFAULT.
168 All these properties should be back-end agnostic. For back-end
169 specific properties, define a similar variable named
170 `org-BACKEND-option-alist', replacing BACKEND with the name of
171 the appropriate back-end. You can also redefine properties
172 there, as they have precedence over these.")
174 (defconst org-export-special-keywords
175 '("SETUP_FILE" "OPTIONS" "MACRO")
176 "List of in-buffer keywords that require special treatment.
177 These keywords are not directly associated to a property. The
178 way they are handled must be hard-coded into
179 `org-export-get-inbuffer-options' function.")
181 (defconst org-export-filters-alist
182 '((:filter-bold . org-export-filter-bold-functions)
183 (:filter-babel-call . org-export-filter-babel-call-functions)
184 (:filter-center-block . org-export-filter-center-block-functions)
185 (:filter-clock . org-export-filter-clock-functions)
186 (:filter-code . org-export-filter-code-functions)
187 (:filter-comment . org-export-filter-comment-functions)
188 (:filter-comment-block . org-export-filter-comment-block-functions)
189 (:filter-drawer . org-export-filter-drawer-functions)
190 (:filter-dynamic-block . org-export-filter-dynamic-block-functions)
191 (:filter-entity . org-export-filter-entity-functions)
192 (:filter-example-block . org-export-filter-example-block-functions)
193 (:filter-export-block . org-export-filter-export-block-functions)
194 (:filter-export-snippet . org-export-filter-export-snippet-functions)
195 (:filter-final-output . org-export-filter-final-output-functions)
196 (:filter-fixed-width . org-export-filter-fixed-width-functions)
197 (:filter-footnote-definition . org-export-filter-footnote-definition-functions)
198 (:filter-footnote-reference . org-export-filter-footnote-reference-functions)
199 (:filter-headline . org-export-filter-headline-functions)
200 (:filter-horizontal-rule . org-export-filter-horizontal-rule-functions)
201 (:filter-inline-babel-call . org-export-filter-inline-babel-call-functions)
202 (:filter-inline-src-block . org-export-filter-inline-src-block-functions)
203 (:filter-inlinetask . org-export-filter-inlinetask-functions)
204 (:filter-italic . org-export-filter-italic-functions)
205 (:filter-item . org-export-filter-item-functions)
206 (:filter-keyword . org-export-filter-keyword-functions)
207 (:filter-latex-environment . org-export-filter-latex-environment-functions)
208 (:filter-latex-fragment . org-export-filter-latex-fragment-functions)
209 (:filter-line-break . org-export-filter-line-break-functions)
210 (:filter-link . org-export-filter-link-functions)
211 (:filter-macro . org-export-filter-macro-functions)
212 (:filter-paragraph . org-export-filter-paragraph-functions)
213 (:filter-parse-tree . org-export-filter-parse-tree-functions)
214 (:filter-plain-list . org-export-filter-plain-list-functions)
215 (:filter-plain-text . org-export-filter-plain-text-functions)
216 (:filter-planning . org-export-filter-planning-functions)
217 (:filter-property-drawer . org-export-filter-property-drawer-functions)
218 (:filter-quote-block . org-export-filter-quote-block-functions)
219 (:filter-quote-section . org-export-filter-quote-section-functions)
220 (:filter-radio-target . org-export-filter-radio-target-functions)
221 (:filter-section . org-export-filter-section-functions)
222 (:filter-special-block . org-export-filter-special-block-functions)
223 (:filter-src-block . org-export-filter-src-block-functions)
224 (:filter-statistics-cookie . org-export-filter-statistics-cookie-functions)
225 (:filter-strike-through . org-export-filter-strike-through-functions)
226 (:filter-subscript . org-export-filter-subscript-functions)
227 (:filter-superscript . org-export-filter-superscript-functions)
228 (:filter-table . org-export-filter-table-functions)
229 (:filter-table-cell . org-export-filter-table-cell-functions)
230 (:filter-table-row . org-export-filter-table-row-functions)
231 (:filter-target . org-export-filter-target-functions)
232 (:filter-timestamp . org-export-filter-timestamp-functions)
233 (:filter-underline . org-export-filter-underline-functions)
234 (:filter-verbatim . org-export-filter-verbatim-functions)
235 (:filter-verse-block . org-export-filter-verse-block-functions))
236 "Alist between filters properties and initial values.
238 The key of each association is a property name accessible through
239 the communication channel its value is a configurable global
240 variable defining initial filters.
242 This list is meant to install user specified filters. Back-end
243 developers may install their own filters using
244 `org-BACKEND-filters-alist', where BACKEND is the name of the
245 considered back-end. Filters defined there will always be
246 prepended to the current list, so they always get applied
247 first.")
249 (defconst org-export-default-inline-image-rule
250 `(("file" .
251 ,(format "\\.%s\\'"
252 (regexp-opt
253 '("png" "jpeg" "jpg" "gif" "tiff" "tif" "xbm"
254 "xpm" "pbm" "pgm" "ppm") t))))
255 "Default rule for link matching an inline image.
256 This rule applies to links with no description. By default, it
257 will be considered as an inline image if it targets a local file
258 whose extension is either \"png\", \"jpeg\", \"jpg\", \"gif\",
259 \"tiff\", \"tif\", \"xbm\", \"xpm\", \"pbm\", \"pgm\" or \"ppm\".
260 See `org-export-inline-image-p' for more information about
261 rules.")
265 ;;; User-configurable Variables
267 ;; Configuration for the masses.
269 ;; They should never be accessed directly, as their value is to be
270 ;; stored in a property list (cf. `org-export-option-alist').
271 ;; Back-ends will read their value from there instead.
273 (defgroup org-export nil
274 "Options for exporting Org mode files."
275 :tag "Org Export"
276 :group 'org)
278 (defgroup org-export-general nil
279 "General options for export engine."
280 :tag "Org Export General"
281 :group 'org-export)
283 (defcustom org-export-with-archived-trees 'headline
284 "Whether sub-trees with the ARCHIVE tag should be exported.
286 This can have three different values:
287 nil Do not export, pretend this tree is not present.
288 t Do export the entire tree.
289 `headline' Only export the headline, but skip the tree below it.
291 This option can also be set with the #+OPTIONS line,
292 e.g. \"arch:nil\"."
293 :group 'org-export-general
294 :type '(choice
295 (const :tag "Not at all" nil)
296 (const :tag "Headline only" 'headline)
297 (const :tag "Entirely" t)))
299 (defcustom org-export-with-author t
300 "Non-nil means insert author name into the exported file.
301 This option can also be set with the #+OPTIONS line,
302 e.g. \"author:nil\"."
303 :group 'org-export-general
304 :type 'boolean)
306 (defcustom org-export-with-clocks nil
307 "Non-nil means export CLOCK keywords.
308 This option can also be set with the #+OPTIONS line,
309 e.g. \"c:t\"."
310 :group 'org-export-general
311 :type 'boolean)
313 (defcustom org-export-with-creator 'comment
314 "Non-nil means the postamble should contain a creator sentence.
316 The sentence can be set in `org-export-creator-string' and
317 defaults to \"Generated by Org mode XX in Emacs XXX.\".
319 If the value is `comment' insert it as a comment."
320 :group 'org-export-general
321 :type '(choice
322 (const :tag "No creator sentence" nil)
323 (const :tag "Sentence as a comment" 'comment)
324 (const :tag "Insert the sentence" t)))
326 (defcustom org-export-creator-string
327 (format "Generated by Org mode %s in Emacs %s."
328 (if (fboundp 'org-version) (org-version) "(Unknown)")
329 emacs-version)
330 "String to insert at the end of the generated document."
331 :group 'org-export-general
332 :type '(string :tag "Creator string"))
334 (defcustom org-export-with-drawers t
335 "Non-nil means export contents of standard drawers.
337 When t, all drawers are exported. This may also be a list of
338 drawer names to export. This variable doesn't apply to
339 properties drawers.
341 This option can also be set with the #+OPTIONS line,
342 e.g. \"d:nil\"."
343 :group 'org-export-general
344 :type '(choice
345 (const :tag "All drawers" t)
346 (const :tag "None" nil)
347 (repeat :tag "Selected drawers"
348 (string :tag "Drawer name"))))
350 (defcustom org-export-with-email nil
351 "Non-nil means insert author email into the exported file.
352 This option can also be set with the #+OPTIONS line,
353 e.g. \"email:t\"."
354 :group 'org-export-general
355 :type 'boolean)
357 (defcustom org-export-with-emphasize t
358 "Non-nil means interpret *word*, /word/, and _word_ as emphasized text.
360 If the export target supports emphasizing text, the word will be
361 typeset in bold, italic, or underlined, respectively. Not all
362 export backends support this.
364 This option can also be set with the #+OPTIONS line, e.g. \"*:nil\"."
365 :group 'org-export-general
366 :type 'boolean)
368 (defcustom org-export-exclude-tags '("noexport")
369 "Tags that exclude a tree from export.
371 All trees carrying any of these tags will be excluded from
372 export. This is without condition, so even subtrees inside that
373 carry one of the `org-export-select-tags' will be removed.
375 This option can also be set with the #+EXPORT_EXCLUDE_TAGS:
376 keyword."
377 :group 'org-export-general
378 :type '(repeat (string :tag "Tag")))
380 (defcustom org-export-with-fixed-width t
381 "Non-nil means lines starting with \":\" will be in fixed width font.
383 This can be used to have pre-formatted text, fragments of code
384 etc. For example:
385 : ;; Some Lisp examples
386 : (while (defc cnt)
387 : (ding))
388 will be looking just like this in also HTML. See also the QUOTE
389 keyword. Not all export backends support this.
391 This option can also be set with the #+OPTIONS line, e.g. \"::nil\"."
392 :group 'org-export-translation
393 :type 'boolean)
395 (defcustom org-export-with-footnotes t
396 "Non-nil means Org footnotes should be exported.
397 This option can also be set with the #+OPTIONS line,
398 e.g. \"f:nil\"."
399 :group 'org-export-general
400 :type 'boolean)
402 (defcustom org-export-headline-levels 3
403 "The last level which is still exported as a headline.
405 Inferior levels will produce itemize lists when exported. Note
406 that a numeric prefix argument to an exporter function overrides
407 this setting.
409 This option can also be set with the #+OPTIONS line, e.g. \"H:2\"."
410 :group 'org-export-general
411 :type 'integer)
413 (defcustom org-export-default-language "en"
414 "The default language for export and clocktable translations, as a string.
415 This may have an association in
416 `org-clock-clocktable-language-setup'."
417 :group 'org-export-general
418 :type '(string :tag "Language"))
420 (defcustom org-export-preserve-breaks nil
421 "Non-nil means preserve all line breaks when exporting.
423 Normally, in HTML output paragraphs will be reformatted.
425 This option can also be set with the #+OPTIONS line,
426 e.g. \"\\n:t\"."
427 :group 'org-export-general
428 :type 'boolean)
430 (defcustom org-export-with-entities t
431 "Non-nil means interpret entities when exporting.
433 For example, HTML export converts \\alpha to &alpha; and \\AA to
434 &Aring;.
436 For a list of supported names, see the constant `org-entities'
437 and the user option `org-entities-user'.
439 This option can also be set with the #+OPTIONS line,
440 e.g. \"e:nil\"."
441 :group 'org-export-general
442 :type 'boolean)
444 (defcustom org-export-with-planning nil
445 "Non-nil means include planning info in export.
446 This option can also be set with the #+OPTIONS: line,
447 e.g. \"p:t\"."
448 :group 'org-export-general
449 :type 'boolean)
451 (defcustom org-export-with-priority nil
452 "Non-nil means include priority cookies in export.
454 When nil, remove priority cookies for export.
456 This option can also be set with the #+OPTIONS line,
457 e.g. \"pri:t\"."
458 :group 'org-export-general
459 :type 'boolean)
461 (defcustom org-export-with-section-numbers t
462 "Non-nil means add section numbers to headlines when exporting.
464 When set to an integer n, numbering will only happen for
465 headlines whose relative level is higher or equal to n.
467 This option can also be set with the #+OPTIONS line,
468 e.g. \"num:t\"."
469 :group 'org-export-general
470 :type 'boolean)
472 (defcustom org-export-select-tags '("export")
473 "Tags that select a tree for export.
475 If any such tag is found in a buffer, all trees that do not carry
476 one of these tags will be ignored during export. Inside trees
477 that are selected like this, you can still deselect a subtree by
478 tagging it with one of the `org-export-exclude-tags'.
480 This option can also be set with the #+EXPORT_SELECT_TAGS:
481 keyword."
482 :group 'org-export-general
483 :type '(repeat (string :tag "Tag")))
485 (defcustom org-export-with-special-strings t
486 "Non-nil means interpret \"\-\", \"--\" and \"---\" for export.
488 When this option is turned on, these strings will be exported as:
490 Org HTML LaTeX
491 -----+----------+--------
492 \\- &shy; \\-
493 -- &ndash; --
494 --- &mdash; ---
495 ... &hellip; \ldots
497 This option can also be set with the #+OPTIONS line,
498 e.g. \"-:nil\"."
499 :group 'org-export-general
500 :type 'boolean)
502 (defcustom org-export-with-sub-superscripts t
503 "Non-nil means interpret \"_\" and \"^\" for export.
505 When this option is turned on, you can use TeX-like syntax for
506 sub- and superscripts. Several characters after \"_\" or \"^\"
507 will be considered as a single item - so grouping with {} is
508 normally not needed. For example, the following things will be
509 parsed as single sub- or superscripts.
511 10^24 or 10^tau several digits will be considered 1 item.
512 10^-12 or 10^-tau a leading sign with digits or a word
513 x^2-y^3 will be read as x^2 - y^3, because items are
514 terminated by almost any nonword/nondigit char.
515 x_{i^2} or x^(2-i) braces or parenthesis do grouping.
517 Still, ambiguity is possible - so when in doubt use {} to enclose
518 the sub/superscript. If you set this variable to the symbol
519 `{}', the braces are *required* in order to trigger
520 interpretations as sub/superscript. This can be helpful in
521 documents that need \"_\" frequently in plain text.
523 This option can also be set with the #+OPTIONS line,
524 e.g. \"^:nil\"."
525 :group 'org-export-general
526 :type '(choice
527 (const :tag "Interpret them" t)
528 (const :tag "Curly brackets only" {})
529 (const :tag "Do not interpret them" nil)))
531 (defcustom org-export-with-toc t
532 "Non-nil means create a table of contents in exported files.
534 The TOC contains headlines with levels up
535 to`org-export-headline-levels'. When an integer, include levels
536 up to N in the toc, this may then be different from
537 `org-export-headline-levels', but it will not be allowed to be
538 larger than the number of headline levels. When nil, no table of
539 contents is made.
541 This option can also be set with the #+OPTIONS line,
542 e.g. \"toc:nil\" or \"toc:3\"."
543 :group 'org-export-general
544 :type '(choice
545 (const :tag "No Table of Contents" nil)
546 (const :tag "Full Table of Contents" t)
547 (integer :tag "TOC to level")))
549 (defcustom org-export-with-tables t
550 "If non-nil, lines starting with \"|\" define a table.
551 For example:
553 | Name | Address | Birthday |
554 |-------------+----------+-----------|
555 | Arthur Dent | England | 29.2.2100 |
557 This option can also be set with the #+OPTIONS line, e.g. \"|:nil\"."
558 :group 'org-export-general
559 :type 'boolean)
561 (defcustom org-export-with-tags t
562 "If nil, do not export tags, just remove them from headlines.
564 If this is the symbol `not-in-toc', tags will be removed from
565 table of contents entries, but still be shown in the headlines of
566 the document.
568 This option can also be set with the #+OPTIONS line,
569 e.g. \"tags:nil\"."
570 :group 'org-export-general
571 :type '(choice
572 (const :tag "Off" nil)
573 (const :tag "Not in TOC" not-in-toc)
574 (const :tag "On" t)))
576 (defcustom org-export-with-tasks t
577 "Non-nil means include TODO items for export.
578 This may have the following values:
579 t include tasks independent of state.
580 todo include only tasks that are not yet done.
581 done include only tasks that are already done.
582 nil remove all tasks before export
583 list of keywords keep only tasks with these keywords"
584 :group 'org-export-general
585 :type '(choice
586 (const :tag "All tasks" t)
587 (const :tag "No tasks" nil)
588 (const :tag "Not-done tasks" todo)
589 (const :tag "Only done tasks" done)
590 (repeat :tag "Specific TODO keywords"
591 (string :tag "Keyword"))))
593 (defcustom org-export-time-stamp-file t
594 "Non-nil means insert a time stamp into the exported file.
595 The time stamp shows when the file was created.
597 This option can also be set with the #+OPTIONS line,
598 e.g. \"timestamp:nil\"."
599 :group 'org-export-general
600 :type 'boolean)
602 (defcustom org-export-with-timestamps t
603 "Non nil means allow timestamps in export.
605 It can be set to `active', `inactive', t or nil, in order to
606 export, respectively, only active timestamps, only inactive ones,
607 all of them or none.
609 This option can also be set with the #+OPTIONS line, e.g.
610 \"<:nil\"."
611 :group 'org-export-general
612 :type '(choice
613 (const :tag "All timestamps" t)
614 (const :tag "Only active timestamps" active)
615 (const :tag "Only inactive timestamps" inactive)
616 (const :tag "No timestamp" nil)))
618 (defcustom org-export-with-todo-keywords t
619 "Non-nil means include TODO keywords in export.
620 When nil, remove all these keywords from the export."
621 :group 'org-export-general
622 :type 'boolean)
624 (defcustom org-export-allow-BIND 'confirm
625 "Non-nil means allow #+BIND to define local variable values for export.
626 This is a potential security risk, which is why the user must
627 confirm the use of these lines."
628 :group 'org-export-general
629 :type '(choice
630 (const :tag "Never" nil)
631 (const :tag "Always" t)
632 (const :tag "Ask a confirmation for each file" confirm)))
634 (defcustom org-export-snippet-translation-alist nil
635 "Alist between export snippets back-ends and exporter back-ends.
637 This variable allows to provide shortcuts for export snippets.
639 For example, with a value of '\(\(\"h\" . \"html\"\)\), the HTML
640 back-end will recognize the contents of \"@h{<b>}\" as HTML code
641 while every other back-end will ignore it."
642 :group 'org-export-general
643 :type '(repeat
644 (cons
645 (string :tag "Shortcut")
646 (string :tag "Back-end"))))
648 (defcustom org-export-coding-system nil
649 "Coding system for the exported file."
650 :group 'org-export-general
651 :type 'coding-system)
653 (defcustom org-export-copy-to-kill-ring t
654 "Non-nil means exported stuff will also be pushed onto the kill ring."
655 :group 'org-export-general
656 :type 'boolean)
658 (defcustom org-export-initial-scope 'buffer
659 "The initial scope when exporting with `org-export-dispatch'.
660 This variable can be either set to `buffer' or `subtree'."
661 :group 'org-export-general
662 :type '(choice
663 (const :tag "Export current buffer" 'buffer)
664 (const :tag "Export current subtree" 'subtree)))
666 (defcustom org-export-show-temporary-export-buffer t
667 "Non-nil means show buffer after exporting to temp buffer.
668 When Org exports to a file, the buffer visiting that file is ever
669 shown, but remains buried. However, when exporting to
670 a temporary buffer, that buffer is popped up in a second window.
671 When this variable is nil, the buffer remains buried also in
672 these cases."
673 :group 'org-export-general
674 :type 'boolean)
676 (defcustom org-export-dispatch-use-expert-ui nil
677 "Non-nil means using a non-intrusive `org-export-dispatch'.
678 In that case, no help buffer is displayed. Though, an indicator
679 for current export scope is added to the prompt \(i.e. \"b\" when
680 output is restricted to body only, \"s\" when it is restricted to
681 the current subtree and \"v\" when only visible elements are
682 considered for export\). Also, \[?] allows to switch back to
683 standard mode."
684 :group 'org-export-general
685 :type 'boolean)
689 ;;; The Communication Channel
691 ;; During export process, every function has access to a number of
692 ;; properties. They are of three types:
694 ;; 1. Environment options are collected once at the very beginning of
695 ;; the process, out of the original buffer and configuration.
696 ;; Collecting them is handled by `org-export-get-environment'
697 ;; function.
699 ;; Most environment options are defined through the
700 ;; `org-export-option-alist' variable.
702 ;; 2. Tree properties are extracted directly from the parsed tree,
703 ;; just before export, by `org-export-collect-tree-properties'.
705 ;; 3. Local options are updated during parsing, and their value
706 ;; depends on the level of recursion. For now, only `:ignore-list'
707 ;; belongs to that category.
709 ;; Here is the full list of properties available during transcode
710 ;; process, with their category (option, tree or local) and their
711 ;; value type.
713 ;; + `:author' :: Author's name.
714 ;; - category :: option
715 ;; - type :: string
717 ;; + `:back-end' :: Current back-end used for transcoding.
718 ;; - category :: tree
719 ;; - type :: symbol
721 ;; + `:creator' :: String to write as creation information.
722 ;; - category :: option
723 ;; - type :: string
725 ;; + `:date' :: String to use as date.
726 ;; - category :: option
727 ;; - type :: string
729 ;; + `:description' :: Description text for the current data.
730 ;; - category :: option
731 ;; - type :: string
733 ;; + `:email' :: Author's email.
734 ;; - category :: option
735 ;; - type :: string
737 ;; + `:exclude-tags' :: Tags for exclusion of subtrees from export
738 ;; process.
739 ;; - category :: option
740 ;; - type :: list of strings
742 ;; + `:footnote-definition-alist' :: Alist between footnote labels and
743 ;; their definition, as parsed data. Only non-inlined footnotes
744 ;; are represented in this alist. Also, every definition isn't
745 ;; guaranteed to be referenced in the parse tree. The purpose of
746 ;; this property is to preserve definitions from oblivion
747 ;; (i.e. when the parse tree comes from a part of the original
748 ;; buffer), it isn't meant for direct use in a back-end. To
749 ;; retrieve a definition relative to a reference, use
750 ;; `org-export-get-footnote-definition' instead.
751 ;; - category :: option
752 ;; - type :: alist (STRING . LIST)
754 ;; + `:headline-levels' :: Maximum level being exported as an
755 ;; headline. Comparison is done with the relative level of
756 ;; headlines in the parse tree, not necessarily with their
757 ;; actual level.
758 ;; - category :: option
759 ;; - type :: integer
761 ;; + `:headline-offset' :: Difference between relative and real level
762 ;; of headlines in the parse tree. For example, a value of -1
763 ;; means a level 2 headline should be considered as level
764 ;; 1 (cf. `org-export-get-relative-level').
765 ;; - category :: tree
766 ;; - type :: integer
768 ;; + `:headline-numbering' :: Alist between headlines and their
769 ;; numbering, as a list of numbers
770 ;; (cf. `org-export-get-headline-number').
771 ;; - category :: tree
772 ;; - type :: alist (INTEGER . LIST)
774 ;; + `:ignore-list' :: List of elements and objects that should be
775 ;; ignored during export.
776 ;; - category :: local
777 ;; - type :: list of elements and objects
779 ;; + `:input-file' :: Full path to input file, if any.
780 ;; - category :: option
781 ;; - type :: string or nil
783 ;; + `:keywords' :: List of keywords attached to data.
784 ;; - category :: option
785 ;; - type :: string
787 ;; + `:language' :: Default language used for translations.
788 ;; - category :: option
789 ;; - type :: string
791 ;; + `:parse-tree' :: Whole parse tree, available at any time during
792 ;; transcoding.
793 ;; - category :: global
794 ;; - type :: list (as returned by `org-element-parse-buffer')
796 ;; + `:preserve-breaks' :: Non-nil means transcoding should preserve
797 ;; all line breaks.
798 ;; - category :: option
799 ;; - type :: symbol (nil, t)
801 ;; + `:section-numbers' :: Non-nil means transcoding should add
802 ;; section numbers to headlines.
803 ;; - category :: option
804 ;; - type :: symbol (nil, t)
806 ;; + `:select-tags' :: List of tags enforcing inclusion of sub-trees
807 ;; in transcoding. When such a tag is present, subtrees without
808 ;; it are de facto excluded from the process. See
809 ;; `use-select-tags'.
810 ;; - category :: option
811 ;; - type :: list of strings
813 ;; + `:target-list' :: List of targets encountered in the parse tree.
814 ;; This is used to partly resolve "fuzzy" links
815 ;; (cf. `org-export-resolve-fuzzy-link').
816 ;; - category :: tree
817 ;; - type :: list of strings
819 ;; + `:time-stamp-file' :: Non-nil means transcoding should insert
820 ;; a time stamp in the output.
821 ;; - category :: option
822 ;; - type :: symbol (nil, t)
824 ;; + `:with-archived-trees' :: Non-nil when archived subtrees should
825 ;; also be transcoded. If it is set to the `headline' symbol,
826 ;; only the archived headline's name is retained.
827 ;; - category :: option
828 ;; - type :: symbol (nil, t, `headline')
830 ;; + `:with-author' :: Non-nil means author's name should be included
831 ;; in the output.
832 ;; - category :: option
833 ;; - type :: symbol (nil, t)
835 ;; + `:with-clocks' :: Non-nild means clock keywords should be exported.
836 ;; - category :: option
837 ;; - type :: symbol (nil, t)
839 ;; + `:with-creator' :: Non-nild means a creation sentence should be
840 ;; inserted at the end of the transcoded string. If the value
841 ;; is `comment', it should be commented.
842 ;; - category :: option
843 ;; - type :: symbol (`comment', nil, t)
845 ;; + `:with-drawers' :: Non-nil means drawers should be exported. If
846 ;; its value is a list of names, only drawers with such names
847 ;; will be transcoded.
848 ;; - category :: option
849 ;; - type :: symbol (nil, t) or list of strings
851 ;; + `:with-email' :: Non-nil means output should contain author's
852 ;; email.
853 ;; - category :: option
854 ;; - type :: symbol (nil, t)
856 ;; + `:with-emphasize' :: Non-nil means emphasized text should be
857 ;; interpreted.
858 ;; - category :: option
859 ;; - type :: symbol (nil, t)
861 ;; + `:with-fixed-width' :: Non-nil if transcoder should interpret
862 ;; strings starting with a colon as a fixed-with (verbatim) area.
863 ;; - category :: option
864 ;; - type :: symbol (nil, t)
866 ;; + `:with-footnotes' :: Non-nil if transcoder should interpret
867 ;; footnotes.
868 ;; - category :: option
869 ;; - type :: symbol (nil, t)
871 ;; + `:with-plannings' :: Non-nil means transcoding should include
872 ;; planning info.
873 ;; - category :: option
874 ;; - type :: symbol (nil, t)
876 ;; + `:with-priority' :: Non-nil means transcoding should include
877 ;; priority cookies.
878 ;; - category :: option
879 ;; - type :: symbol (nil, t)
881 ;; + `:with-special-strings' :: Non-nil means transcoding should
882 ;; interpret special strings in plain text.
883 ;; - category :: option
884 ;; - type :: symbol (nil, t)
886 ;; + `:with-sub-superscript' :: Non-nil means transcoding should
887 ;; interpret subscript and superscript. With a value of "{}",
888 ;; only interpret those using curly brackets.
889 ;; - category :: option
890 ;; - type :: symbol (nil, {}, t)
892 ;; + `:with-tables' :: Non-nil means transcoding should interpret
893 ;; tables.
894 ;; - category :: option
895 ;; - type :: symbol (nil, t)
897 ;; + `:with-tags' :: Non-nil means transcoding should keep tags in
898 ;; headlines. A `not-in-toc' value will remove them from the
899 ;; table of contents, if any, nonetheless.
900 ;; - category :: option
901 ;; - type :: symbol (nil, t, `not-in-toc')
903 ;; + `:with-tasks' :: Non-nil means transcoding should include
904 ;; headlines with a TODO keyword. A `todo' value will only
905 ;; include headlines with a todo type keyword while a `done'
906 ;; value will do the contrary. If a list of strings is provided,
907 ;; only tasks with keywords belonging to that list will be kept.
908 ;; - category :: option
909 ;; - type :: symbol (t, todo, done, nil) or list of strings
911 ;; + `:with-timestamps' :: Non-nil means transcoding should include
912 ;; time stamps. Special value `active' (resp. `inactive') ask to
913 ;; export only active (resp. inactive) timestamps. Otherwise,
914 ;; completely remove them.
915 ;; - category :: option
916 ;; - type :: symbol: (`active', `inactive', t, nil)
918 ;; + `:with-toc' :: Non-nil means that a table of contents has to be
919 ;; added to the output. An integer value limits its depth.
920 ;; - category :: option
921 ;; - type :: symbol (nil, t or integer)
923 ;; + `:with-todo-keywords' :: Non-nil means transcoding should
924 ;; include TODO keywords.
925 ;; - category :: option
926 ;; - type :: symbol (nil, t)
929 ;;;; Environment Options
931 ;; Environment options encompass all parameters defined outside the
932 ;; scope of the parsed data. They come from five sources, in
933 ;; increasing precedence order:
935 ;; - Global variables,
936 ;; - Buffer's attributes,
937 ;; - Options keyword symbols,
938 ;; - Buffer keywords,
939 ;; - Subtree properties.
941 ;; The central internal function with regards to environment options
942 ;; is `org-export-get-environment'. It updates global variables with
943 ;; "#+BIND:" keywords, then retrieve and prioritize properties from
944 ;; the different sources.
946 ;; The internal functions doing the retrieval are:
947 ;; `org-export-get-global-options',
948 ;; `org-export-get-buffer-attributes',
949 ;; `org-export-parse-option-keyword',
950 ;; `org-export-get-subtree-options' and
951 ;; `org-export-get-inbuffer-options'
953 ;; Also, `org-export-confirm-letbind' and `org-export-install-letbind'
954 ;; take care of the part relative to "#+BIND:" keywords.
956 (defun org-export-get-environment (&optional backend subtreep ext-plist)
957 "Collect export options from the current buffer.
959 Optional argument BACKEND is a symbol specifying which back-end
960 specific options to read, if any.
962 When optional argument SUBTREEP is non-nil, assume the export is
963 done against the current sub-tree.
965 Third optional argument EXT-PLIST is a property list with
966 external parameters overriding Org default settings, but still
967 inferior to file-local settings."
968 ;; First install #+BIND variables.
969 (org-export-install-letbind-maybe)
970 ;; Get and prioritize export options...
971 (let ((options (org-combine-plists
972 ;; ... from global variables...
973 (org-export-get-global-options backend)
974 ;; ... from buffer's attributes...
975 (org-export-get-buffer-attributes)
976 ;; ... from an external property list...
977 ext-plist
978 ;; ... from in-buffer settings...
979 (org-export-get-inbuffer-options
980 backend
981 (and buffer-file-name
982 (org-remove-double-quotes buffer-file-name)))
983 ;; ... and from subtree, when appropriate.
984 (and subtreep (org-export-get-subtree-options))
985 ;; Also install back-end symbol.
986 `(:back-end ,backend))))
987 ;; Return plist.
988 options))
990 (defun org-export-parse-option-keyword (options &optional backend)
991 "Parse an OPTIONS line and return values as a plist.
992 Optional argument BACKEND is a symbol specifying which back-end
993 specific items to read, if any."
994 (let* ((all
995 (append org-export-option-alist
996 (and backend
997 (let ((var (intern
998 (format "org-%s-option-alist" backend))))
999 (and (boundp var) (eval var))))))
1000 ;; Build an alist between #+OPTION: item and property-name.
1001 (alist (delq nil
1002 (mapcar (lambda (e)
1003 (when (nth 2 e) (cons (regexp-quote (nth 2 e))
1004 (car e))))
1005 all)))
1006 plist)
1007 (mapc (lambda (e)
1008 (when (string-match (concat "\\(\\`\\|[ \t]\\)"
1009 (car e)
1010 ":\\(([^)\n]+)\\|[^ \t\n\r;,.]*\\)")
1011 options)
1012 (setq plist (plist-put plist
1013 (cdr e)
1014 (car (read-from-string
1015 (match-string 2 options)))))))
1016 alist)
1017 plist))
1019 (defun org-export-get-subtree-options ()
1020 "Get export options in subtree at point.
1022 Assume point is at subtree's beginning.
1024 Return options as a plist."
1025 (let (prop plist)
1026 (when (setq prop (progn (looking-at org-todo-line-regexp)
1027 (or (save-match-data
1028 (org-entry-get (point) "EXPORT_TITLE"))
1029 (org-match-string-no-properties 3))))
1030 (setq plist
1031 (plist-put
1032 plist :title
1033 (org-element-parse-secondary-string
1034 prop
1035 (cdr (assq 'keyword org-element-string-restrictions))))))
1036 (when (setq prop (org-entry-get (point) "EXPORT_TEXT"))
1037 (setq plist (plist-put plist :text prop)))
1038 (when (setq prop (org-entry-get (point) "EXPORT_AUTHOR"))
1039 (setq plist (plist-put plist :author prop)))
1040 (when (setq prop (org-entry-get (point) "EXPORT_DATE"))
1041 (setq plist (plist-put plist :date prop)))
1042 (when (setq prop (org-entry-get (point) "EXPORT_OPTIONS"))
1043 (setq plist (org-export-add-options-to-plist plist prop)))
1044 plist))
1046 (defun org-export-get-inbuffer-options (&optional backend files)
1047 "Return current buffer export options, as a plist.
1049 Optional argument BACKEND, when non-nil, is a symbol specifying
1050 which back-end specific options should also be read in the
1051 process.
1053 Optional argument FILES is a list of setup files names read so
1054 far, used to avoid circular dependencies.
1056 Assume buffer is in Org mode. Narrowing, if any, is ignored."
1057 (org-with-wide-buffer
1058 (goto-char (point-min))
1059 (let ((case-fold-search t) plist)
1060 ;; 1. Special keywords, as in `org-export-special-keywords'.
1061 (let ((special-re (org-make-options-regexp org-export-special-keywords)))
1062 (while (re-search-forward special-re nil t)
1063 (let ((element (org-element-at-point)))
1064 (when (eq (org-element-type element) 'keyword)
1065 (let* ((key (org-element-property :key element))
1066 (val (org-element-property :value element))
1067 (prop
1068 (cond
1069 ((string= key "SETUP_FILE")
1070 (let ((file
1071 (expand-file-name
1072 (org-remove-double-quotes (org-trim val)))))
1073 ;; Avoid circular dependencies.
1074 (unless (member file files)
1075 (with-temp-buffer
1076 (insert (org-file-contents file 'noerror))
1077 (org-mode)
1078 (org-export-get-inbuffer-options
1079 backend (cons file files))))))
1080 ((string= key "OPTIONS")
1081 (org-export-parse-option-keyword val backend))
1082 ((string= key "MACRO")
1083 (when (string-match
1084 "^\\([-a-zA-Z0-9_]+\\)\\(?:[ \t]+\\(.*?\\)[ \t]*$\\)?"
1085 val)
1086 (let ((key
1087 (intern
1088 (concat ":macro-"
1089 (downcase (match-string 1 val)))))
1090 (value (org-match-string-no-properties 2 val)))
1091 (cond
1092 ((not value) nil)
1093 ;; Value will be evaled. Leave it as-is.
1094 ((string-match "\\`(eval\\>" value)
1095 (list key value))
1096 ;; Value has to be parsed for nested
1097 ;; macros.
1099 (list
1101 (let ((restr (org-element-restriction 'macro)))
1102 (org-element-parse-secondary-string
1103 ;; If user explicitly asks for
1104 ;; a newline, be sure to preserve it
1105 ;; from further filling with
1106 ;; `hard-newline'. Also replace
1107 ;; "\\n" with "\n", "\\\n" with "\\n"
1108 ;; and so on...
1109 (replace-regexp-in-string
1110 "\\(\\\\\\\\\\)n" "\\\\"
1111 (replace-regexp-in-string
1112 "\\(?:^\\|[^\\\\]\\)\\(\\\\n\\)"
1113 hard-newline value nil nil 1)
1114 nil nil 1)
1115 restr)))))))))))
1116 (setq plist (org-combine-plists plist prop)))))))
1117 ;; 2. Standard options, as in `org-export-option-alist'.
1118 (let* ((all (append org-export-option-alist
1119 ;; Also look for back-end specific options
1120 ;; if BACKEND is defined.
1121 (and backend
1122 (let ((var
1123 (intern
1124 (format "org-%s-option-alist" backend))))
1125 (and (boundp var) (eval var))))))
1126 ;; Build alist between keyword name and property name.
1127 (alist
1128 (delq nil (mapcar
1129 (lambda (e) (when (nth 1 e) (cons (nth 1 e) (car e))))
1130 all)))
1131 ;; Build regexp matching all keywords associated to export
1132 ;; options. Note: the search is case insensitive.
1133 (opt-re (org-make-options-regexp
1134 (delq nil (mapcar (lambda (e) (nth 1 e)) all)))))
1135 (goto-char (point-min))
1136 (while (re-search-forward opt-re nil t)
1137 (let ((element (org-element-at-point)))
1138 (when (eq (org-element-type element) 'keyword)
1139 (let* ((key (org-element-property :key element))
1140 (val (org-element-property :value element))
1141 (prop (cdr (assoc key alist)))
1142 (behaviour (nth 4 (assq prop all))))
1143 (setq plist
1144 (plist-put
1145 plist prop
1146 ;; Handle value depending on specified BEHAVIOUR.
1147 (case behaviour
1148 (space
1149 (if (not (plist-get plist prop)) (org-trim val)
1150 (concat (plist-get plist prop) " " (org-trim val))))
1151 (newline
1152 (org-trim
1153 (concat (plist-get plist prop) "\n" (org-trim val))))
1154 (split
1155 `(,@(plist-get plist prop) ,@(org-split-string val)))
1156 ('t val)
1157 (otherwise (if (not (plist-member plist prop)) val
1158 (plist-get plist prop))))))))))
1159 ;; Parse keywords specified in `org-element-parsed-keywords'.
1160 (mapc
1161 (lambda (key)
1162 (let* ((prop (cdr (assoc key alist)))
1163 (value (and prop (plist-get plist prop))))
1164 (when (stringp value)
1165 (setq plist
1166 (plist-put
1167 plist prop
1168 (org-element-parse-secondary-string
1169 value (org-element-restriction 'keyword)))))))
1170 org-element-parsed-keywords))
1171 ;; 3. Return final value.
1172 plist)))
1174 (defun org-export-get-buffer-attributes ()
1175 "Return properties related to buffer attributes, as a plist."
1176 (let ((visited-file (buffer-file-name (buffer-base-buffer))))
1177 (list
1178 ;; Store full path of input file name, or nil. For internal use.
1179 :input-file visited-file
1180 :title (or (and visited-file
1181 (file-name-sans-extension
1182 (file-name-nondirectory visited-file)))
1183 (buffer-name (buffer-base-buffer)))
1184 :macro-modification-time
1185 (and visited-file
1186 (file-exists-p visited-file)
1187 (concat "(eval (format-time-string \"$1\" '"
1188 (prin1-to-string (nth 5 (file-attributes visited-file)))
1189 "))"))
1190 ;; Store input file name as a macro.
1191 :macro-input-file (and visited-file (file-name-nondirectory visited-file))
1192 ;; `:macro-date', `:macro-time' and `:macro-property' could as
1193 ;; well be initialized as tree properties, since they don't
1194 ;; depend on buffer properties. Though, it may be more logical
1195 ;; to keep them close to other ":macro-" properties.
1196 :macro-date "(eval (format-time-string \"$1\"))"
1197 :macro-time "(eval (format-time-string \"$1\"))"
1198 :macro-property "(eval (org-entry-get nil \"$1\" 'selective))")))
1200 (defun org-export-get-global-options (&optional backend)
1201 "Return global export options as a plist.
1203 Optional argument BACKEND, if non-nil, is a symbol specifying
1204 which back-end specific export options should also be read in the
1205 process."
1206 (let ((all (append org-export-option-alist
1207 (and backend
1208 (let ((var (intern
1209 (format "org-%s-option-alist" backend))))
1210 (and (boundp var) (eval var))))))
1211 ;; Output value.
1212 plist)
1213 (mapc (lambda (cell)
1214 (setq plist (plist-put plist (car cell) (eval (nth 3 cell)))))
1215 all)
1216 ;; Return value.
1217 plist))
1219 (defun org-export-store-footnote-definitions (info)
1220 "Collect and store footnote definitions from current buffer in INFO.
1222 INFO is a plist containing export options.
1224 Footnotes definitions are stored as a alist whose CAR is
1225 footnote's label, as a string, and CDR the contents, as a parse
1226 tree. This alist will be consed to the value of
1227 `:footnote-definition-alist' in INFO, if any.
1229 The new plist is returned; use
1231 \(setq info (org-export-store-footnote-definitions info))
1233 to be sure to use the new value. INFO is modified by side
1234 effects."
1235 ;; Footnotes definitions must be collected in the original buffer,
1236 ;; as there's no insurance that they will still be in the parse
1237 ;; tree, due to some narrowing.
1238 (plist-put
1239 info :footnote-definition-alist
1240 (let ((alist (plist-get info :footnote-definition-alist)))
1241 (org-with-wide-buffer
1242 (goto-char (point-min))
1243 (while (re-search-forward org-footnote-definition-re nil t)
1244 (let ((def (org-footnote-at-definition-p)))
1245 (when def
1246 (org-skip-whitespace)
1247 (push (cons (car def)
1248 (save-restriction
1249 (narrow-to-region (point) (nth 2 def))
1250 ;; Like `org-element-parse-buffer', but
1251 ;; makes sure the definition doesn't start
1252 ;; with a section element.
1253 (nconc
1254 (list 'org-data nil)
1255 (org-element-parse-elements
1256 (point-min) (point-max) nil nil nil nil nil))))
1257 alist))))
1258 alist))))
1260 (defvar org-export-allow-BIND-local nil)
1261 (defun org-export-confirm-letbind ()
1262 "Can we use #+BIND values during export?
1263 By default this will ask for confirmation by the user, to divert
1264 possible security risks."
1265 (cond
1266 ((not org-export-allow-BIND) nil)
1267 ((eq org-export-allow-BIND t) t)
1268 ((local-variable-p 'org-export-allow-BIND-local) org-export-allow-BIND-local)
1269 (t (org-set-local 'org-export-allow-BIND-local
1270 (yes-or-no-p "Allow BIND values in this buffer? ")))))
1272 (defun org-export-install-letbind-maybe ()
1273 "Install the values from #+BIND lines as local variables.
1274 Variables must be installed before in-buffer options are
1275 retrieved."
1276 (let (letbind pair)
1277 (org-with-wide-buffer
1278 (goto-char (point-min))
1279 (while (re-search-forward (org-make-options-regexp '("BIND")) nil t)
1280 (when (org-export-confirm-letbind)
1281 (push (read (concat "(" (org-match-string-no-properties 2) ")"))
1282 letbind))))
1283 (while (setq pair (pop letbind))
1284 (org-set-local (car pair) (nth 1 pair)))))
1287 ;;;; Tree Properties
1289 ;; Tree properties are infromation extracted from parse tree. They
1290 ;; are initialized at the beginning of the transcoding process by
1291 ;; `org-export-collect-tree-properties'.
1293 ;; Dedicated functions focus on computing the value of specific tree
1294 ;; properties during initialization. Thus,
1295 ;; `org-export-populate-ignore-list' lists elements and objects that
1296 ;; should be skipped during export, `org-export-get-min-level' gets
1297 ;; the minimal exportable level, used as a basis to compute relative
1298 ;; level for headlines. Eventually
1299 ;; `org-export-collect-headline-numbering' builds an alist between
1300 ;; headlines and their numbering.
1302 (defun org-export-collect-tree-properties (data info)
1303 "Extract tree properties from parse tree.
1305 DATA is the parse tree from which information is retrieved. INFO
1306 is a list holding export options.
1308 Following tree properties are set or updated:
1309 `:footnote-definition-alist' List of footnotes definitions in
1310 original buffer and current parse tree.
1312 `:headline-offset' Offset between true level of headlines and
1313 local level. An offset of -1 means an headline
1314 of level 2 should be considered as a level
1315 1 headline in the context.
1317 `:headline-numbering' Alist of all headlines as key an the
1318 associated numbering as value.
1320 `:ignore-list' List of elements that should be ignored during
1321 export.
1323 `:target-list' List of all targets in the parse tree."
1324 ;; Install the parse tree in the communication channel, in order to
1325 ;; use `org-export-get-genealogy' and al.
1326 (setq info (plist-put info :parse-tree data))
1327 ;; Get the list of elements and objects to ignore, and put it into
1328 ;; `:ignore-list'. Do not overwrite any user ignore that might have
1329 ;; been done during parse tree filtering.
1330 (setq info
1331 (plist-put info
1332 :ignore-list
1333 (append (org-export-populate-ignore-list data info)
1334 (plist-get info :ignore-list))))
1335 ;; Compute `:headline-offset' in order to be able to use
1336 ;; `org-export-get-relative-level'.
1337 (setq info
1338 (plist-put info
1339 :headline-offset (- 1 (org-export-get-min-level data info))))
1340 ;; Update footnotes definitions list with definitions in parse tree.
1341 ;; This is required since buffer expansion might have modified
1342 ;; boundaries of footnote definitions contained in the parse tree.
1343 ;; This way, definitions in `footnote-definition-alist' are bound to
1344 ;; match those in the parse tree.
1345 (let ((defs (plist-get info :footnote-definition-alist)))
1346 (org-element-map
1347 data 'footnote-definition
1348 (lambda (fn)
1349 (push (cons (org-element-property :label fn)
1350 `(org-data nil ,@(org-element-contents fn)))
1351 defs)))
1352 (setq info (plist-put info :footnote-definition-alist defs)))
1353 ;; Properties order doesn't matter: get the rest of the tree
1354 ;; properties.
1355 (nconc
1356 `(:target-list
1357 ,(org-element-map
1358 data '(keyword target)
1359 (lambda (blob)
1360 (when (or (eq (org-element-type blob) 'target)
1361 (string= (org-element-property :key blob) "TARGET"))
1362 blob)) info)
1363 :headline-numbering ,(org-export-collect-headline-numbering data info))
1364 info))
1366 (defun org-export-get-min-level (data options)
1367 "Return minimum exportable headline's level in DATA.
1368 DATA is parsed tree as returned by `org-element-parse-buffer'.
1369 OPTIONS is a plist holding export options."
1370 (catch 'exit
1371 (let ((min-level 10000))
1372 (mapc
1373 (lambda (blob)
1374 (when (and (eq (org-element-type blob) 'headline)
1375 (not (member blob (plist-get options :ignore-list))))
1376 (setq min-level
1377 (min (org-element-property :level blob) min-level)))
1378 (when (= min-level 1) (throw 'exit 1)))
1379 (org-element-contents data))
1380 ;; If no headline was found, for the sake of consistency, set
1381 ;; minimum level to 1 nonetheless.
1382 (if (= min-level 10000) 1 min-level))))
1384 (defun org-export-collect-headline-numbering (data options)
1385 "Return numbering of all exportable headlines in a parse tree.
1387 DATA is the parse tree. OPTIONS is the plist holding export
1388 options.
1390 Return an alist whose key is an headline and value is its
1391 associated numbering \(in the shape of a list of numbers\)."
1392 (let ((numbering (make-vector org-export-max-depth 0)))
1393 (org-element-map
1394 data
1395 'headline
1396 (lambda (headline)
1397 (let ((relative-level
1398 (1- (org-export-get-relative-level headline options))))
1399 (cons
1400 headline
1401 (loop for n across numbering
1402 for idx from 0 to org-export-max-depth
1403 when (< idx relative-level) collect n
1404 when (= idx relative-level) collect (aset numbering idx (1+ n))
1405 when (> idx relative-level) do (aset numbering idx 0)))))
1406 options)))
1408 (defun org-export-populate-ignore-list (data options)
1409 "Return list of elements and objects to ignore during export.
1411 DATA is the parse tree to traverse. OPTIONS is the plist holding
1412 export options.
1414 Return elements or objects to ignore as a list."
1415 (let (ignore
1416 (walk-data
1417 (function
1418 (lambda (data options selected)
1419 ;; Collect ignored elements or objects into IGNORE-LIST.
1420 (mapc
1421 (lambda (el)
1422 (if (org-export--skip-p el options selected) (push el ignore)
1423 (let ((type (org-element-type el)))
1424 (if (and (eq (plist-get info :with-archived-trees) 'headline)
1425 (eq (org-element-type el) 'headline)
1426 (org-element-property :archivedp el))
1427 ;; If headline is archived but tree below has
1428 ;; to be skipped, add it to ignore list.
1429 (mapc (lambda (e) (push e ignore))
1430 (org-element-contents el))
1431 ;; Move into recursive objects/elements.
1432 (when (org-element-contents el)
1433 (funcall walk-data el options selected))))))
1434 (org-element-contents data))))))
1435 ;; Main call. First find trees containing a select tag, if any.
1436 (funcall walk-data data options (org-export--selected-trees data options))
1437 ;; Return value.
1438 ignore))
1440 (defun org-export--selected-trees (data info)
1441 "Return list of headlines containing a select tag in their tree.
1442 DATA is parsed data as returned by `org-element-parse-buffer'.
1443 INFO is a plist holding export options."
1444 (let (selected-trees
1445 (walk-data
1446 (function
1447 (lambda (data genealogy)
1448 (case (org-element-type data)
1449 (org-data
1450 (funcall walk-data (org-element-contents data) genealogy))
1451 (headline
1452 (let ((tags (org-element-property :tags headline)))
1453 (if (loop for tag in (plist-get info :select-tags)
1454 thereis (member tag tags))
1455 ;; When a select tag is found, mark as acceptable
1456 ;; full genealogy and every headline within the
1457 ;; tree.
1458 (setq selected-trees
1459 (append
1460 (cons data genealogy)
1461 (org-element-map data 'headline 'identity)
1462 selected-trees))
1463 ;; Else, continue searching in tree, recursively.
1464 (funcall walk-data data (cons data genealogy))))))))))
1465 (funcall walk-data data nil) selected-trees))
1467 (defun org-export--skip-p (blob options select-tags)
1468 "Non-nil when element or object BLOB should be skipped during export.
1469 OPTIONS is the plist holding export options. SELECT-TAGS, when
1470 non-nil, is a list of tags marking a subtree as exportable."
1471 (case (org-element-type blob)
1472 ;; Check headline.
1473 (headline
1474 (let ((with-tasks (plist-get options :with-tasks))
1475 (todo (org-element-property :todo-keyword blob))
1476 (todo-type (org-element-property :todo-type blob))
1477 (archived (plist-get options :with-archived-trees))
1478 (tags (org-element-property :tags blob)))
1480 ;; Ignore subtrees with an exclude tag.
1481 (loop for k in (plist-get options :exclude-tags)
1482 thereis (member k tags))
1483 ;; Ignore subtrees without a select tag, when such tag is
1484 ;; found in the buffer.
1485 (member blob select-tags)
1486 ;; Ignore commented sub-trees.
1487 (org-element-property :commentedp blob)
1488 ;; Ignore archived subtrees if `:with-archived-trees' is nil.
1489 (and (not archived) (org-element-property :archivedp blob))
1490 ;; Ignore tasks, if specified by `:with-tasks' property.
1491 (and todo
1492 (or (not with-tasks)
1493 (and (memq with-tasks '(todo done))
1494 (not (eq todo-type with-tasks)))
1495 (and (consp with-tasks) (not (member todo with-tasks))))))))
1496 ;; Check timestamp.
1497 (timestamp
1498 (case (plist-get options :with-timestamps)
1499 ;; No timestamp allowed.
1500 ('nil t)
1501 ;; Only active timestamps allowed and the current one isn't
1502 ;; active.
1503 (active
1504 (not (memq (org-element-property :type blob)
1505 '(active active-range))))
1506 ;; Only inactive timestamps allowed and the current one isn't
1507 ;; inactive.
1508 (inactive
1509 (not (memq (org-element-property :type blob)
1510 '(inactive inactive-range))))))
1511 ;; Check drawer.
1512 (drawer
1513 (or (not (plist-get options :with-drawers))
1514 (and (consp (plist-get options :with-drawers))
1515 (not (member (org-element-property :drawer-name blob)
1516 (plist-get options :with-drawers))))))
1517 ;; Check table-row.
1518 (table-row (org-export-table-row-is-special-p blob options))
1519 ;; Check table-cell.
1520 (table-cell
1521 (and (org-export-table-has-special-column-p
1522 (nth 1 (org-export-get-genealogy blob options)))
1523 (not (org-export-get-previous-element blob options))))
1524 ;; Check clock.
1525 (clock (not (plist-get options :with-clocks)))
1526 ;; Check planning.
1527 (planning (not (plist-get options :with-plannings)))))
1531 ;;; The Transcoder
1533 ;; `org-export-data' reads a parse tree (obtained with, i.e.
1534 ;; `org-element-parse-buffer') and transcodes it into a specified
1535 ;; back-end output. It takes care of filtering out elements or
1536 ;; objects according to export options and organizing the output blank
1537 ;; lines and white space are preserved.
1539 ;; Internally, three functions handle the filtering of objects and
1540 ;; elements during the export. In particular,
1541 ;; `org-export-ignore-element' marks an element or object so future
1542 ;; parse tree traversals skip it, `org-export-interpret-p' tells which
1543 ;; elements or objects should be seen as real Org syntax and
1544 ;; `org-export-expand' transforms the others back into their original
1545 ;; shape.
1547 (defun org-export-transcoder (blob info)
1548 "Return appropriate transcoder for BLOB.
1549 INFO is a plist containing export directives."
1550 (let ((type (org-element-type blob)))
1551 ;; Return contents only for complete parse trees.
1552 (if (eq type 'org-data) (lambda (blob contents info) contents)
1553 (let ((transcoder
1554 (intern (format "org-%s-%s" (plist-get info :back-end) type))))
1555 (and (fboundp transcoder) transcoder)))))
1557 (defun org-export-data (data info)
1558 "Convert DATA into current back-end format.
1560 DATA is a parse tree, an element or an object or a secondary
1561 string. INFO is a plist holding export options.
1563 Return transcoded string."
1564 (let* ((type (org-element-type data))
1565 (results
1566 (cond
1567 ;; Ignored element/object.
1568 ((member data (plist-get info :ignore-list)) nil)
1569 ;; Plain text.
1570 ((eq type 'plain-text)
1571 (org-export-filter-apply-functions
1572 (plist-get info :filter-plain-text)
1573 (let ((transcoder (org-export-transcoder data info)))
1574 (if transcoder (funcall transcoder data info) data))
1575 info))
1576 ;; Uninterpreted element/object: change it back to Org
1577 ;; syntax.
1578 ((not (org-export-interpret-p data info))
1579 (org-export-expand
1580 data
1581 (mapconcat (lambda (blob) (org-export-data blob info))
1582 (org-element-contents data)
1583 "")))
1584 ;; Secondary string.
1585 ((not type)
1586 (mapconcat (lambda (obj) (org-export-data obj info)) data ""))
1587 ;; Element/Object without contents or, as a special case,
1588 ;; headline with archive tag and archived trees restricted
1589 ;; to title only.
1590 ((or (not (org-element-contents data))
1591 (and (eq type 'headline)
1592 (eq (plist-get info :with-archived-trees) 'headline)
1593 (org-element-property :archivedp data)))
1594 (let ((transcoder (org-export-transcoder data info)))
1595 (and (fboundp transcoder) (funcall transcoder data nil info))))
1596 ;; Element/Object with contents.
1598 (let ((transcoder (org-export-transcoder data info)))
1599 (when transcoder
1600 (let* ((greaterp (memq type org-element-greater-elements))
1601 (objectp (and (not greaterp)
1602 (memq type org-element-recursive-objects)))
1603 (contents
1604 (mapconcat
1605 (lambda (element) (org-export-data element info))
1606 (org-element-contents
1607 (if (or greaterp objectp) data
1608 ;; Elements directly containing objects
1609 ;; must have their indentation normalized
1610 ;; first.
1611 (org-element-normalize-contents
1612 data
1613 ;; When normalizing contents of the first
1614 ;; paragraph in an item or a footnote
1615 ;; definition, ignore first line's
1616 ;; indentation: there is none and it
1617 ;; might be misleading.
1618 (when (eq type 'paragraph)
1619 (let ((parent (org-export-get-parent data info)))
1620 (and (equal (car (org-element-contents parent))
1621 data)
1622 (memq (org-element-type parent)
1623 '(footnote-definition item))))))))
1624 "")))
1625 (funcall transcoder data
1626 (if greaterp (org-element-normalize-string contents)
1627 contents)
1628 info))))))))
1629 (cond
1630 ((not results) nil)
1631 ((memq type '(org-data plain-text nil)) results)
1632 ;; Append the same white space between elements or objects as in
1633 ;; the original buffer, and call appropriate filters.
1635 (let ((results
1636 (org-export-filter-apply-functions
1637 (plist-get info (intern (format ":filter-%s" type)))
1638 (let ((post-blank (org-element-property :post-blank data)))
1639 (if (memq type org-element-all-elements)
1640 (concat (org-element-normalize-string results)
1641 (make-string post-blank ?\n))
1642 (concat results (make-string post-blank ? ))))
1643 info)))
1644 ;; Eventually return string.
1645 results)))))
1647 (defun org-export-interpret-p (blob info)
1648 "Non-nil if element or object BLOB should be interpreted as Org syntax.
1649 Check is done according to export options INFO, stored as
1650 a plist."
1651 (case (org-element-type blob)
1652 ;; ... entities...
1653 (entity (plist-get info :with-entities))
1654 ;; ... emphasis...
1655 (emphasis (plist-get info :with-emphasize))
1656 ;; ... fixed-width areas.
1657 (fixed-width (plist-get info :with-fixed-width))
1658 ;; ... footnotes...
1659 ((footnote-definition footnote-reference)
1660 (plist-get info :with-footnotes))
1661 ;; ... sub/superscripts...
1662 ((subscript superscript)
1663 (let ((sub/super-p (plist-get info :with-sub-superscript)))
1664 (if (eq sub/super-p '{})
1665 (org-element-property :use-brackets-p blob)
1666 sub/super-p)))
1667 ;; ... tables...
1668 (table (plist-get info :with-tables))
1669 (otherwise t)))
1671 (defsubst org-export-expand (blob contents)
1672 "Expand a parsed element or object to its original state.
1673 BLOB is either an element or an object. CONTENTS is its
1674 contents, as a string or nil."
1675 (funcall
1676 (intern (format "org-element-%s-interpreter" (org-element-type blob)))
1677 blob contents))
1679 (defun org-export-ignore-element (element info)
1680 "Add ELEMENT to `:ignore-list' in INFO.
1682 Any element in `:ignore-list' will be skipped when using
1683 `org-element-map'. INFO is modified by side effects."
1684 (plist-put info :ignore-list (cons element (plist-get info :ignore-list))))
1688 ;;; The Filter System
1690 ;; Filters allow end-users to tweak easily the transcoded output.
1691 ;; They are the functional counterpart of hooks, as every filter in
1692 ;; a set is applied to the return value of the previous one.
1694 ;; Every set is back-end agnostic. Although, a filter is always
1695 ;; called, in addition to the string it applies to, with the back-end
1696 ;; used as argument, so it's easy enough for the end-user to add
1697 ;; back-end specific filters in the set. The communication channel,
1698 ;; as a plist, is required as the third argument.
1700 ;; Filters sets are defined below. There are of four types:
1702 ;; - `org-export-filter-parse-tree-functions' applies directly on the
1703 ;; complete parsed tree. It's the only filters set that doesn't
1704 ;; apply to a string.
1705 ;; - `org-export-filter-final-output-functions' applies to the final
1706 ;; transcoded string.
1707 ;; - `org-export-filter-plain-text-functions' applies to any string
1708 ;; not recognized as Org syntax.
1709 ;; - `org-export-filter-TYPE-functions' applies on the string returned
1710 ;; after an element or object of type TYPE has been transcoded.
1712 ;; All filters sets are applied through
1713 ;; `org-export-filter-apply-functions' function. Filters in a set are
1714 ;; applied in a LIFO fashion. It allows developers to be sure that
1715 ;; their filters will be applied first.
1717 ;; Filters properties are installed in communication channel with
1718 ;; `org-export-install-filters' function.
1720 ;; Eventually, a hook (`org-export-before-parsing-hook') is run just
1721 ;; before parsing to allow for heavy structure modifications.
1724 ;;;; Before Parsing Hook
1726 (defvar org-export-before-parsing-hook nil
1727 "Hook run before parsing an export buffer.
1728 This is run after include keywords have been expanded and Babel
1729 code executed, on a copy of original buffer's area being
1730 exported. Visibility is the same as in the original one. Point
1731 is left at the beginning of the new one.")
1734 ;;;; Special Filters
1736 (defvar org-export-filter-parse-tree-functions nil
1737 "List of functions applied to the parsed tree.
1738 Each filter is called with three arguments: the parse tree, as
1739 returned by `org-element-parse-buffer', the back-end, as
1740 a symbol, and the communication channel, as a plist. It must
1741 return the modified parse tree to transcode.")
1743 (defvar org-export-filter-final-output-functions nil
1744 "List of functions applied to the transcoded string.
1745 Each filter is called with three arguments: the full transcoded
1746 string, the back-end, as a symbol, and the communication channel,
1747 as a plist. It must return a string that will be used as the
1748 final export output.")
1750 (defvar org-export-filter-plain-text-functions nil
1751 "List of functions applied to plain text.
1752 Each filter is called with three arguments: a string which
1753 contains no Org syntax, the back-end, as a symbol, and the
1754 communication channel, as a plist. It must return a string or
1755 nil.")
1758 ;;;; Elements Filters
1760 (defvar org-export-filter-center-block-functions nil
1761 "List of functions applied to a transcoded center block.
1762 Each filter is called with three arguments: the transcoded data,
1763 as a string, the back-end, as a symbol, and the communication
1764 channel, as a plist. It must return a string or nil.")
1766 (defvar org-export-filter-clock-functions nil
1767 "List of functions applied to a transcoded clock.
1768 Each filter is called with three arguments: the transcoded data,
1769 as a string, the back-end, as a symbol, and the communication
1770 channel, as a plist. It must return a string or nil.")
1772 (defvar org-export-filter-drawer-functions nil
1773 "List of functions applied to a transcoded drawer.
1774 Each filter is called with three arguments: the transcoded data,
1775 as a string, the back-end, as a symbol, and the communication
1776 channel, as a plist. It must return a string or nil.")
1778 (defvar org-export-filter-dynamic-block-functions nil
1779 "List of functions applied to a transcoded dynamic-block.
1780 Each filter is called with three arguments: the transcoded data,
1781 as a string, the back-end, as a symbol, and the communication
1782 channel, as a plist. It must return a string or nil.")
1784 (defvar org-export-filter-headline-functions nil
1785 "List of functions applied to a transcoded headline.
1786 Each filter is called with three arguments: the transcoded data,
1787 as a string, the back-end, as a symbol, and the communication
1788 channel, as a plist. It must return a string or nil.")
1790 (defvar org-export-filter-inlinetask-functions nil
1791 "List of functions applied to a transcoded inlinetask.
1792 Each filter is called with three arguments: the transcoded data,
1793 as a string, the back-end, as a symbol, and the communication
1794 channel, as a plist. It must return a string or nil.")
1796 (defvar org-export-filter-plain-list-functions nil
1797 "List of functions applied to a transcoded plain-list.
1798 Each filter is called with three arguments: the transcoded data,
1799 as a string, the back-end, as a symbol, and the communication
1800 channel, as a plist. It must return a string or nil.")
1802 (defvar org-export-filter-item-functions nil
1803 "List of functions applied to a transcoded item.
1804 Each filter is called with three arguments: the transcoded data,
1805 as a string, the back-end, as a symbol, and the communication
1806 channel, as a plist. It must return a string or nil.")
1808 (defvar org-export-filter-comment-functions nil
1809 "List of functions applied to a transcoded comment.
1810 Each filter is called with three arguments: the transcoded data,
1811 as a string, the back-end, as a symbol, and the communication
1812 channel, as a plist. It must return a string or nil.")
1814 (defvar org-export-filter-comment-block-functions nil
1815 "List of functions applied to a transcoded comment-comment.
1816 Each filter is called with three arguments: the transcoded data,
1817 as a string, the back-end, as a symbol, and the communication
1818 channel, as a plist. It must return a string or nil.")
1820 (defvar org-export-filter-example-block-functions nil
1821 "List of functions applied to a transcoded example-block.
1822 Each filter is called with three arguments: the transcoded data,
1823 as a string, the back-end, as a symbol, and the communication
1824 channel, as a plist. It must return a string or nil.")
1826 (defvar org-export-filter-export-block-functions nil
1827 "List of functions applied to a transcoded export-block.
1828 Each filter is called with three arguments: the transcoded data,
1829 as a string, the back-end, as a symbol, and the communication
1830 channel, as a plist. It must return a string or nil.")
1832 (defvar org-export-filter-fixed-width-functions nil
1833 "List of functions applied to a transcoded fixed-width.
1834 Each filter is called with three arguments: the transcoded data,
1835 as a string, the back-end, as a symbol, and the communication
1836 channel, as a plist. It must return a string or nil.")
1838 (defvar org-export-filter-footnote-definition-functions nil
1839 "List of functions applied to a transcoded footnote-definition.
1840 Each filter is called with three arguments: the transcoded data,
1841 as a string, the back-end, as a symbol, and the communication
1842 channel, as a plist. It must return a string or nil.")
1844 (defvar org-export-filter-horizontal-rule-functions nil
1845 "List of functions applied to a transcoded horizontal-rule.
1846 Each filter is called with three arguments: the transcoded data,
1847 as a string, the back-end, as a symbol, and the communication
1848 channel, as a plist. It must return a string or nil.")
1850 (defvar org-export-filter-keyword-functions nil
1851 "List of functions applied to a transcoded keyword.
1852 Each filter is called with three arguments: the transcoded data,
1853 as a string, the back-end, as a symbol, and the communication
1854 channel, as a plist. It must return a string or nil.")
1856 (defvar org-export-filter-latex-environment-functions nil
1857 "List of functions applied to a transcoded latex-environment.
1858 Each filter is called with three arguments: the transcoded data,
1859 as a string, the back-end, as a symbol, and the communication
1860 channel, as a plist. It must return a string or nil.")
1862 (defvar org-export-filter-babel-call-functions nil
1863 "List of functions applied to a transcoded babel-call.
1864 Each filter is called with three arguments: the transcoded data,
1865 as a string, the back-end, as a symbol, and the communication
1866 channel, as a plist. It must return a string or nil.")
1868 (defvar org-export-filter-paragraph-functions nil
1869 "List of functions applied to a transcoded paragraph.
1870 Each filter is called with three arguments: the transcoded data,
1871 as a string, the back-end, as a symbol, and the communication
1872 channel, as a plist. It must return a string or nil.")
1874 (defvar org-export-filter-planning-functions nil
1875 "List of functions applied to a transcoded planning.
1876 Each filter is called with three arguments: the transcoded data,
1877 as a string, the back-end, as a symbol, and the communication
1878 channel, as a plist. It must return a string or nil.")
1880 (defvar org-export-filter-property-drawer-functions nil
1881 "List of functions applied to a transcoded property-drawer.
1882 Each filter is called with three arguments: the transcoded data,
1883 as a string, the back-end, as a symbol, and the communication
1884 channel, as a plist. It must return a string or nil.")
1886 (defvar org-export-filter-quote-block-functions nil
1887 "List of functions applied to a transcoded quote block.
1888 Each filter is called with three arguments: the transcoded quote
1889 data, as a string, the back-end, as a symbol, and the
1890 communication channel, as a plist. It must return a string or
1891 nil.")
1893 (defvar org-export-filter-quote-section-functions nil
1894 "List of functions applied to a transcoded quote-section.
1895 Each filter is called with three arguments: the transcoded data,
1896 as a string, the back-end, as a symbol, and the communication
1897 channel, as a plist. It must return a string or nil.")
1899 (defvar org-export-filter-section-functions nil
1900 "List of functions applied to a transcoded section.
1901 Each filter is called with three arguments: the transcoded data,
1902 as a string, the back-end, as a symbol, and the communication
1903 channel, as a plist. It must return a string or nil.")
1905 (defvar org-export-filter-special-block-functions nil
1906 "List of functions applied to a transcoded special block.
1907 Each filter is called with three arguments: the transcoded data,
1908 as a string, the back-end, as a symbol, and the communication
1909 channel, as a plist. It must return a string or nil.")
1911 (defvar org-export-filter-src-block-functions nil
1912 "List of functions applied to a transcoded src-block.
1913 Each filter is called with three arguments: the transcoded data,
1914 as a string, the back-end, as a symbol, and the communication
1915 channel, as a plist. It must return a string or nil.")
1917 (defvar org-export-filter-table-functions nil
1918 "List of functions applied to a transcoded table.
1919 Each filter is called with three arguments: the transcoded data,
1920 as a string, the back-end, as a symbol, and the communication
1921 channel, as a plist. It must return a string or nil.")
1923 (defvar org-export-filter-table-cell-functions nil
1924 "List of functions applied to a transcoded table-cell.
1925 Each filter is called with three arguments: the transcoded data,
1926 as a string, the back-end, as a symbol, and the communication
1927 channel, as a plist. It must return a string or nil.")
1929 (defvar org-export-filter-table-row-functions nil
1930 "List of functions applied to a transcoded table-row.
1931 Each filter is called with three arguments: the transcoded data,
1932 as a string, the back-end, as a symbol, and the communication
1933 channel, as a plist. It must return a string or nil.")
1935 (defvar org-export-filter-verse-block-functions nil
1936 "List of functions applied to a transcoded verse block.
1937 Each filter is called with three arguments: the transcoded data,
1938 as a string, the back-end, as a symbol, and the communication
1939 channel, as a plist. It must return a string or nil.")
1942 ;;;; Objects Filters
1944 (defvar org-export-filter-bold-functions nil
1945 "List of functions applied to transcoded bold text.
1946 Each filter is called with three arguments: the transcoded data,
1947 as a string, the back-end, as a symbol, and the communication
1948 channel, as a plist. It must return a string or nil.")
1950 (defvar org-export-filter-code-functions nil
1951 "List of functions applied to transcoded code text.
1952 Each filter is called with three arguments: the transcoded data,
1953 as a string, the back-end, as a symbol, and the communication
1954 channel, as a plist. It must return a string or nil.")
1956 (defvar org-export-filter-entity-functions nil
1957 "List of functions applied to a transcoded entity.
1958 Each filter is called with three arguments: the transcoded data,
1959 as a string, the back-end, as a symbol, and the communication
1960 channel, as a plist. It must return a string or nil.")
1962 (defvar org-export-filter-export-snippet-functions nil
1963 "List of functions applied to a transcoded export-snippet.
1964 Each filter is called with three arguments: the transcoded data,
1965 as a string, the back-end, as a symbol, and the communication
1966 channel, as a plist. It must return a string or nil.")
1968 (defvar org-export-filter-footnote-reference-functions nil
1969 "List of functions applied to a transcoded footnote-reference.
1970 Each filter is called with three arguments: the transcoded data,
1971 as a string, the back-end, as a symbol, and the communication
1972 channel, as a plist. It must return a string or nil.")
1974 (defvar org-export-filter-inline-babel-call-functions nil
1975 "List of functions applied to a transcoded inline-babel-call.
1976 Each filter is called with three arguments: the transcoded data,
1977 as a string, the back-end, as a symbol, and the communication
1978 channel, as a plist. It must return a string or nil.")
1980 (defvar org-export-filter-inline-src-block-functions nil
1981 "List of functions applied to a transcoded inline-src-block.
1982 Each filter is called with three arguments: the transcoded data,
1983 as a string, the back-end, as a symbol, and the communication
1984 channel, as a plist. It must return a string or nil.")
1986 (defvar org-export-filter-italic-functions nil
1987 "List of functions applied to transcoded italic text.
1988 Each filter is called with three arguments: the transcoded data,
1989 as a string, the back-end, as a symbol, and the communication
1990 channel, as a plist. It must return a string or nil.")
1992 (defvar org-export-filter-latex-fragment-functions nil
1993 "List of functions applied to a transcoded latex-fragment.
1994 Each filter is called with three arguments: the transcoded data,
1995 as a string, the back-end, as a symbol, and the communication
1996 channel, as a plist. It must return a string or nil.")
1998 (defvar org-export-filter-line-break-functions nil
1999 "List of functions applied to a transcoded line-break.
2000 Each filter is called with three arguments: the transcoded data,
2001 as a string, the back-end, as a symbol, and the communication
2002 channel, as a plist. It must return a string or nil.")
2004 (defvar org-export-filter-link-functions nil
2005 "List of functions applied to a transcoded link.
2006 Each filter is called with three arguments: the transcoded data,
2007 as a string, the back-end, as a symbol, and the communication
2008 channel, as a plist. It must return a string or nil.")
2010 (defvar org-export-filter-macro-functions nil
2011 "List of functions applied to a transcoded macro.
2012 Each filter is called with three arguments: the transcoded data,
2013 as a string, the back-end, as a symbol, and the communication
2014 channel, as a plist. It must return a string or nil.")
2016 (defvar org-export-filter-radio-target-functions nil
2017 "List of functions applied to a transcoded radio-target.
2018 Each filter is called with three arguments: the transcoded data,
2019 as a string, the back-end, as a symbol, and the communication
2020 channel, as a plist. It must return a string or nil.")
2022 (defvar org-export-filter-statistics-cookie-functions nil
2023 "List of functions applied to a transcoded statistics-cookie.
2024 Each filter is called with three arguments: the transcoded data,
2025 as a string, the back-end, as a symbol, and the communication
2026 channel, as a plist. It must return a string or nil.")
2028 (defvar org-export-filter-strike-through-functions nil
2029 "List of functions applied to transcoded strike-through text.
2030 Each filter is called with three arguments: the transcoded data,
2031 as a string, the back-end, as a symbol, and the communication
2032 channel, as a plist. It must return a string or nil.")
2034 (defvar org-export-filter-subscript-functions nil
2035 "List of functions applied to a transcoded subscript.
2036 Each filter is called with three arguments: the transcoded data,
2037 as a string, the back-end, as a symbol, and the communication
2038 channel, as a plist. It must return a string or nil.")
2040 (defvar org-export-filter-superscript-functions nil
2041 "List of functions applied to a transcoded superscript.
2042 Each filter is called with three arguments: the transcoded data,
2043 as a string, the back-end, as a symbol, and the communication
2044 channel, as a plist. It must return a string or nil.")
2046 (defvar org-export-filter-target-functions nil
2047 "List of functions applied to a transcoded target.
2048 Each filter is called with three arguments: the transcoded data,
2049 as a string, the back-end, as a symbol, and the communication
2050 channel, as a plist. It must return a string or nil.")
2052 (defvar org-export-filter-timestamp-functions nil
2053 "List of functions applied to a transcoded timestamp.
2054 Each filter is called with three arguments: the transcoded data,
2055 as a string, the back-end, as a symbol, and the communication
2056 channel, as a plist. It must return a string or nil.")
2058 (defvar org-export-filter-underline-functions nil
2059 "List of functions applied to transcoded underline text.
2060 Each filter is called with three arguments: the transcoded data,
2061 as a string, the back-end, as a symbol, and the communication
2062 channel, as a plist. It must return a string or nil.")
2064 (defvar org-export-filter-verbatim-functions nil
2065 "List of functions applied to transcoded verbatim text.
2066 Each filter is called with three arguments: the transcoded data,
2067 as a string, the back-end, as a symbol, and the communication
2068 channel, as a plist. It must return a string or nil.")
2070 (defun org-export-filter-apply-functions (filters value info)
2071 "Call every function in FILTERS.
2072 Functions are called with arguments VALUE, current export
2073 back-end and INFO. Call is done in a LIFO fashion, to be sure
2074 that developer specified filters, if any, are called first."
2075 (loop for filter in filters
2076 if (not value) return nil else
2077 do (setq value (funcall filter value (plist-get info :back-end) info)))
2078 value)
2080 (defun org-export-install-filters (info)
2081 "Install filters properties in communication channel.
2083 INFO is a plist containing the current communication channel.
2085 Return the updated communication channel."
2086 (let (plist)
2087 ;; Install user defined filters with `org-export-filters-alist'.
2088 (mapc (lambda (p)
2089 (setq plist (plist-put plist (car p) (eval (cdr p)))))
2090 org-export-filters-alist)
2091 ;; Prepend back-end specific filters to that list.
2092 (let ((back-end-filters (intern (format "org-%s-filters-alist"
2093 (plist-get info :back-end)))))
2094 (when (boundp back-end-filters)
2095 (mapc (lambda (p)
2096 ;; Single values get consed, lists are prepended.
2097 (let ((key (car p)) (value (cdr p)))
2098 (when value
2099 (setq plist
2100 (plist-put
2101 plist key
2102 (if (atom value) (cons value (plist-get plist key))
2103 (append value (plist-get plist key))))))))
2104 (eval back-end-filters))))
2105 ;; Return new communication channel.
2106 (org-combine-plists info plist)))
2110 ;;; Core functions
2112 ;; This is the room for the main function, `org-export-as', along with
2113 ;; its derivatives, `org-export-to-buffer' and `org-export-to-file'.
2114 ;; They differ only by the way they output the resulting code.
2116 ;; `org-export-output-file-name' is an auxiliary function meant to be
2117 ;; used with `org-export-to-file'. With a given extension, it tries
2118 ;; to provide a canonical file name to write export output to.
2120 ;; Note that `org-export-as' doesn't really parse the current buffer,
2121 ;; but a copy of it (with the same buffer-local variables and
2122 ;; visibility), where include keywords are expanded and Babel blocks
2123 ;; are executed, if appropriate.
2124 ;; `org-export-with-current-buffer-copy' macro prepares that copy.
2126 ;; File inclusion is taken care of by
2127 ;; `org-export-expand-include-keyword' and
2128 ;; `org-export-prepare-file-contents'. Structure wise, including
2129 ;; a whole Org file in a buffer often makes little sense. For
2130 ;; example, if the file contains an headline and the include keyword
2131 ;; was within an item, the item should contain the headline. That's
2132 ;; why file inclusion should be done before any structure can be
2133 ;; associated to the file, that is before parsing.
2135 (defun org-export-as
2136 (backend &optional subtreep visible-only body-only ext-plist noexpand)
2137 "Transcode current Org buffer into BACKEND code.
2139 If narrowing is active in the current buffer, only transcode its
2140 narrowed part.
2142 If a region is active, transcode that region.
2144 When optional argument SUBTREEP is non-nil, transcode the
2145 sub-tree at point, extracting information from the headline
2146 properties first.
2148 When optional argument VISIBLE-ONLY is non-nil, don't export
2149 contents of hidden elements.
2151 When optional argument BODY-ONLY is non-nil, only return body
2152 code, without preamble nor postamble.
2154 Optional argument EXT-PLIST, when provided, is a property list
2155 with external parameters overriding Org default settings, but
2156 still inferior to file-local settings.
2158 Optional argument NOEXPAND, when non-nil, prevents included files
2159 to be expanded and Babel code to be executed.
2161 Return code as a string."
2162 (save-excursion
2163 (save-restriction
2164 ;; Narrow buffer to an appropriate region or subtree for
2165 ;; parsing. If parsing subtree, be sure to remove main headline
2166 ;; too.
2167 (cond ((org-region-active-p)
2168 (narrow-to-region (region-beginning) (region-end)))
2169 (subtreep
2170 (org-narrow-to-subtree)
2171 (goto-char (point-min))
2172 (forward-line)
2173 (narrow-to-region (point) (point-max))))
2174 ;; 1. Get export environment from original buffer. Store
2175 ;; original footnotes definitions in communication channel as
2176 ;; they might not be accessible anymore in a narrowed parse
2177 ;; tree. Also install user's and developer's filters.
2178 (let ((info (org-export-install-filters
2179 (org-export-store-footnote-definitions
2180 (org-export-get-environment backend subtreep ext-plist))))
2181 ;; 2. Get parse tree. Buffer isn't parsed directly.
2182 ;; Instead, a temporary copy is created, where include
2183 ;; keywords are expanded and code blocks are evaluated.
2184 (tree (let ((buf (or (buffer-file-name (buffer-base-buffer))
2185 (current-buffer))))
2186 (org-export-with-current-buffer-copy
2187 (unless noexpand
2188 (org-export-expand-include-keyword)
2189 ;; Setting `org-current-export-file' is
2190 ;; required by Org Babel to properly resolve
2191 ;; noweb references.
2192 (let ((org-current-export-file buf))
2193 (org-export-blocks-preprocess)))
2194 (goto-char (point-min))
2195 (run-hooks 'org-export-before-parsing-hook)
2196 (org-element-parse-buffer nil visible-only)))))
2197 ;; 3. Call parse-tree filters to get the final tree.
2198 (setq tree
2199 (org-export-filter-apply-functions
2200 (plist-get info :filter-parse-tree) tree info))
2201 ;; 4. Now tree is complete, compute its properties and add
2202 ;; them to communication channel.
2203 (setq info
2204 (org-combine-plists
2205 info (org-export-collect-tree-properties tree info)))
2206 ;; 5. Eventually transcode TREE. Wrap the resulting string
2207 ;; into a template, if required. Eventually call
2208 ;; final-output filter.
2209 (let* ((body (org-element-normalize-string (org-export-data tree info)))
2210 (template (intern (format "org-%s-template" backend)))
2211 (output (org-export-filter-apply-functions
2212 (plist-get info :filter-final-output)
2213 (if (or (not (fboundp template)) body-only) body
2214 (funcall template body info))
2215 info)))
2216 ;; Maybe add final OUTPUT to kill ring, then return it.
2217 (when org-export-copy-to-kill-ring (org-kill-new output))
2218 output)))))
2220 (defun org-export-to-buffer
2221 (backend buffer &optional subtreep visible-only body-only ext-plist noexpand)
2222 "Call `org-export-as' with output to a specified buffer.
2224 BACKEND is the back-end used for transcoding, as a symbol.
2226 BUFFER is the output buffer. If it already exists, it will be
2227 erased first, otherwise, it will be created.
2229 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY, EXT-PLIST
2230 and NOEXPAND are similar to those used in `org-export-as', which
2231 see.
2233 Return buffer."
2234 (let ((out (org-export-as
2235 backend subtreep visible-only body-only ext-plist noexpand))
2236 (buffer (get-buffer-create buffer)))
2237 (with-current-buffer buffer
2238 (erase-buffer)
2239 (insert out)
2240 (goto-char (point-min)))
2241 buffer))
2243 (defun org-export-to-file
2244 (backend file &optional subtreep visible-only body-only ext-plist noexpand)
2245 "Call `org-export-as' with output to a specified file.
2247 BACKEND is the back-end used for transcoding, as a symbol. FILE
2248 is the name of the output file, as a string.
2250 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY, EXT-PLIST
2251 and NOEXPAND are similar to those used in `org-export-as', which
2252 see.
2254 Return output file's name."
2255 ;; Checks for FILE permissions. `write-file' would do the same, but
2256 ;; we'd rather avoid needless transcoding of parse tree.
2257 (unless (file-writable-p file) (error "Output file not writable"))
2258 ;; Insert contents to a temporary buffer and write it to FILE.
2259 (let ((out (org-export-as
2260 backend subtreep visible-only body-only ext-plist noexpand)))
2261 (with-temp-buffer
2262 (insert out)
2263 (let ((coding-system-for-write org-export-coding-system))
2264 (write-file file))))
2265 ;; Return full path.
2266 file)
2268 (defun org-export-output-file-name (extension &optional subtreep pub-dir)
2269 "Return output file's name according to buffer specifications.
2271 EXTENSION is a string representing the output file extension,
2272 with the leading dot.
2274 With a non-nil optional argument SUBTREEP, try to determine
2275 output file's name by looking for \"EXPORT_FILE_NAME\" property
2276 of subtree at point.
2278 When optional argument PUB-DIR is set, use it as the publishing
2279 directory.
2281 Return file name as a string, or nil if it couldn't be
2282 determined."
2283 (let ((base-name
2284 ;; File name may come from EXPORT_FILE_NAME subtree property,
2285 ;; assuming point is at beginning of said sub-tree.
2286 (file-name-sans-extension
2287 (or (and subtreep
2288 (org-entry-get
2289 (save-excursion
2290 (ignore-errors
2291 (org-back-to-heading (not visible-only)) (point)))
2292 "EXPORT_FILE_NAME" t))
2293 ;; File name may be extracted from buffer's associated
2294 ;; file, if any.
2295 (buffer-file-name (buffer-base-buffer))
2296 ;; Can't determine file name on our own: Ask user.
2297 (let ((read-file-name-function
2298 (and org-completion-use-ido 'ido-read-file-name)))
2299 (read-file-name
2300 "Output file: " pub-dir nil nil nil
2301 (lambda (name)
2302 (string= (file-name-extension name t) extension))))))))
2303 ;; Build file name. Enforce EXTENSION over whatever user may have
2304 ;; come up with. PUB-DIR, if defined, always has precedence over
2305 ;; any provided path.
2306 (cond
2307 (pub-dir
2308 (concat (file-name-as-directory pub-dir)
2309 (file-name-nondirectory base-name)
2310 extension))
2311 ((string= (file-name-nondirectory base-name) base-name)
2312 (concat (file-name-as-directory ".") base-name extension))
2313 (t (concat base-name extension)))))
2315 (defmacro org-export-with-current-buffer-copy (&rest body)
2316 "Apply BODY in a copy of the current buffer.
2318 The copy preserves local variables and visibility of the original
2319 buffer.
2321 Point is at buffer's beginning when BODY is applied."
2322 (org-with-gensyms (original-buffer offset buffer-string overlays)
2323 `(let ((,original-buffer ,(current-buffer))
2324 (,offset ,(1- (point-min)))
2325 (,buffer-string ,(buffer-string))
2326 (,overlays (mapcar
2327 'copy-overlay (overlays-in (point-min) (point-max)))))
2328 (with-temp-buffer
2329 (let ((buffer-invisibility-spec nil))
2330 (org-clone-local-variables
2331 ,original-buffer
2332 "^\\(org-\\|orgtbl-\\|major-mode$\\|outline-\\(regexp\\|level\\)$\\)")
2333 (insert ,buffer-string)
2334 (mapc (lambda (ov)
2335 (move-overlay
2337 (- (overlay-start ov) ,offset)
2338 (- (overlay-end ov) ,offset)
2339 (current-buffer)))
2340 ,overlays)
2341 (goto-char (point-min))
2342 (progn ,@body))))))
2343 (def-edebug-spec org-export-with-current-buffer-copy (body))
2345 (defun org-export-expand-include-keyword (&optional included dir)
2346 "Expand every include keyword in buffer.
2347 Optional argument INCLUDED is a list of included file names along
2348 with their line restriction, when appropriate. It is used to
2349 avoid infinite recursion. Optional argument DIR is the current
2350 working directory. It is used to properly resolve relative
2351 paths."
2352 (let ((case-fold-search t))
2353 (goto-char (point-min))
2354 (while (re-search-forward "^[ \t]*#\\+INCLUDE: \\(.*\\)" nil t)
2355 (when (eq (org-element-type (save-match-data (org-element-at-point)))
2356 'keyword)
2357 (beginning-of-line)
2358 ;; Extract arguments from keyword's value.
2359 (let* ((value (match-string 1))
2360 (ind (org-get-indentation))
2361 (file (and (string-match "^\"\\(\\S-+\\)\"" value)
2362 (prog1 (expand-file-name (match-string 1 value) dir)
2363 (setq value (replace-match "" nil nil value)))))
2364 (lines
2365 (and (string-match
2366 ":lines +\"\\(\\(?:[0-9]+\\)?-\\(?:[0-9]+\\)?\\)\"" value)
2367 (prog1 (match-string 1 value)
2368 (setq value (replace-match "" nil nil value)))))
2369 (env (cond ((string-match "\\<example\\>" value) 'example)
2370 ((string-match "\\<src\\(?: +\\(.*\\)\\)?" value)
2371 (match-string 1 value))))
2372 ;; Minimal level of included file defaults to the child
2373 ;; level of the current headline, if any, or one. It
2374 ;; only applies is the file is meant to be included as
2375 ;; an Org one.
2376 (minlevel
2377 (and (not env)
2378 (if (string-match ":minlevel +\\([0-9]+\\)" value)
2379 (prog1 (string-to-number (match-string 1 value))
2380 (setq value (replace-match "" nil nil value)))
2381 (let ((cur (org-current-level)))
2382 (if cur (1+ (org-reduced-level cur)) 1))))))
2383 ;; Remove keyword.
2384 (delete-region (point) (progn (forward-line) (point)))
2385 (cond
2386 ((not (file-readable-p file)) (error "Cannot include file %s" file))
2387 ;; Check if files has already been parsed. Look after
2388 ;; inclusion lines too, as different parts of the same file
2389 ;; can be included too.
2390 ((member (list file lines) included)
2391 (error "Recursive file inclusion: %s" file))
2393 (cond
2394 ((eq env 'example)
2395 (insert
2396 (let ((ind-str (make-string ind ? ))
2397 (contents
2398 ;; Protect sensitive contents with commas.
2399 (replace-regexp-in-string
2400 "\\(^\\)\\([*]\\|[ \t]*#\\+\\)" ","
2401 (org-export-prepare-file-contents file lines)
2402 nil nil 1)))
2403 (format "%s#+BEGIN_EXAMPLE\n%s%s#+END_EXAMPLE\n"
2404 ind-str contents ind-str))))
2405 ((stringp env)
2406 (insert
2407 (let ((ind-str (make-string ind ? ))
2408 (contents
2409 ;; Protect sensitive contents with commas.
2410 (replace-regexp-in-string
2411 (if (string= env "org") "\\(^\\)\\(.\\)"
2412 "\\(^\\)\\([*]\\|[ \t]*#\\+\\)") ","
2413 (org-export-prepare-file-contents file lines)
2414 nil nil 1)))
2415 (format "%s#+BEGIN_SRC %s\n%s%s#+END_SRC\n"
2416 ind-str env contents ind-str))))
2418 (insert
2419 (with-temp-buffer
2420 (org-mode)
2421 (insert
2422 (org-export-prepare-file-contents file lines ind minlevel))
2423 (org-export-expand-include-keyword
2424 (cons (list file lines) included)
2425 (file-name-directory file))
2426 (buffer-string))))))))))))
2428 (defun org-export-prepare-file-contents (file &optional lines ind minlevel)
2429 "Prepare the contents of FILE for inclusion and return them as a string.
2431 When optional argument LINES is a string specifying a range of
2432 lines, include only those lines.
2434 Optional argument IND, when non-nil, is an integer specifying the
2435 global indentation of returned contents. Since its purpose is to
2436 allow an included file to stay in the same environment it was
2437 created \(i.e. a list item), it doesn't apply past the first
2438 headline encountered.
2440 Optional argument MINLEVEL, when non-nil, is an integer
2441 specifying the level that any top-level headline in the included
2442 file should have."
2443 (with-temp-buffer
2444 (insert-file-contents file)
2445 (when lines
2446 (let* ((lines (split-string lines "-"))
2447 (lbeg (string-to-number (car lines)))
2448 (lend (string-to-number (cadr lines)))
2449 (beg (if (zerop lbeg) (point-min)
2450 (goto-char (point-min))
2451 (forward-line (1- lbeg))
2452 (point)))
2453 (end (if (zerop lend) (point-max)
2454 (goto-char (point-min))
2455 (forward-line (1- lend))
2456 (point))))
2457 (narrow-to-region beg end)))
2458 ;; Remove blank lines at beginning and end of contents. The logic
2459 ;; behind that removal is that blank lines around include keyword
2460 ;; override blank lines in included file.
2461 (goto-char (point-min))
2462 (org-skip-whitespace)
2463 (beginning-of-line)
2464 (delete-region (point-min) (point))
2465 (goto-char (point-max))
2466 (skip-chars-backward " \r\t\n")
2467 (forward-line)
2468 (delete-region (point) (point-max))
2469 ;; If IND is set, preserve indentation of include keyword until
2470 ;; the first headline encountered.
2471 (when ind
2472 (unless (eq major-mode 'org-mode) (org-mode))
2473 (goto-char (point-min))
2474 (let ((ind-str (make-string ind ? )))
2475 (while (not (or (eobp) (looking-at org-outline-regexp-bol)))
2476 ;; Do not move footnote definitions out of column 0.
2477 (unless (and (looking-at org-footnote-definition-re)
2478 (eq (org-element-type (org-element-at-point))
2479 'footnote-definition))
2480 (insert ind-str))
2481 (forward-line))))
2482 ;; When MINLEVEL is specified, compute minimal level for headlines
2483 ;; in the file (CUR-MIN), and remove stars to each headline so
2484 ;; that headlines with minimal level have a level of MINLEVEL.
2485 (when minlevel
2486 (unless (eq major-mode 'org-mode) (org-mode))
2487 (let ((levels (org-map-entries
2488 (lambda () (org-reduced-level (org-current-level))))))
2489 (when levels
2490 (let ((offset (- minlevel (apply 'min levels))))
2491 (unless (zerop offset)
2492 (when org-odd-levels-only (setq offset (* offset 2)))
2493 ;; Only change stars, don't bother moving whole
2494 ;; sections.
2495 (org-map-entries
2496 (lambda () (if (< offset 0) (delete-char (abs offset))
2497 (insert (make-string offset ?*))))))))))
2498 (buffer-string)))
2501 ;;; Tools For Back-Ends
2503 ;; A whole set of tools is available to help build new exporters. Any
2504 ;; function general enough to have its use across many back-ends
2505 ;; should be added here.
2507 ;; As of now, functions operating on footnotes, headlines, links,
2508 ;; macros, references, src-blocks, tables and tables of contents are
2509 ;; implemented.
2511 ;;;; For Export Snippets
2513 ;; Every export snippet is transmitted to the back-end. Though, the
2514 ;; latter will only retain one type of export-snippet, ignoring
2515 ;; others, based on the former's target back-end. The function
2516 ;; `org-export-snippet-backend' returns that back-end for a given
2517 ;; export-snippet.
2519 (defun org-export-snippet-backend (export-snippet)
2520 "Return EXPORT-SNIPPET targeted back-end as a symbol.
2521 Translation, with `org-export-snippet-translation-alist', is
2522 applied."
2523 (let ((back-end (org-element-property :back-end export-snippet)))
2524 (intern
2525 (or (cdr (assoc back-end org-export-snippet-translation-alist))
2526 back-end))))
2529 ;;;; For Footnotes
2531 ;; `org-export-collect-footnote-definitions' is a tool to list
2532 ;; actually used footnotes definitions in the whole parse tree, or in
2533 ;; an headline, in order to add footnote listings throughout the
2534 ;; transcoded data.
2536 ;; `org-export-footnote-first-reference-p' is a predicate used by some
2537 ;; back-ends, when they need to attach the footnote definition only to
2538 ;; the first occurrence of the corresponding label.
2540 ;; `org-export-get-footnote-definition' and
2541 ;; `org-export-get-footnote-number' provide easier access to
2542 ;; additional information relative to a footnote reference.
2544 (defun org-export-collect-footnote-definitions (data info)
2545 "Return an alist between footnote numbers, labels and definitions.
2547 DATA is the parse tree from which definitions are collected.
2548 INFO is the plist used as a communication channel.
2550 Definitions are sorted by order of references. They either
2551 appear as Org data or as a secondary string for inlined
2552 footnotes. Unreferenced definitions are ignored."
2553 (let (num-alist
2554 (collect-fn
2555 (function
2556 (lambda (data)
2557 ;; Collect footnote number, label and definition in DATA.
2558 (org-element-map
2559 data 'footnote-reference
2560 (lambda (fn)
2561 (when (org-export-footnote-first-reference-p fn info)
2562 (let ((def (org-export-get-footnote-definition fn info)))
2563 (push
2564 (list (org-export-get-footnote-number fn info)
2565 (org-element-property :label fn)
2566 def)
2567 num-alist)
2568 ;; Also search in definition for nested footnotes.
2569 (when (eq (org-element-property :type fn) 'standard)
2570 (funcall collect-fn def)))))
2571 ;; Don't enter footnote definitions since it will happen
2572 ;; when their first reference is found.
2573 info nil 'footnote-definition)))))
2574 (funcall collect-fn (plist-get info :parse-tree))
2575 (reverse num-alist)))
2577 (defun org-export-footnote-first-reference-p (footnote-reference info)
2578 "Non-nil when a footnote reference is the first one for its label.
2580 FOOTNOTE-REFERENCE is the footnote reference being considered.
2581 INFO is the plist used as a communication channel."
2582 (let ((label (org-element-property :label footnote-reference)))
2583 ;; Anonymous footnotes are always a first reference.
2584 (if (not label) t
2585 ;; Otherwise, return the first footnote with the same LABEL and
2586 ;; test if it is equal to FOOTNOTE-REFERENCE.
2587 (let ((search-refs
2588 (function
2589 (lambda (data)
2590 (org-element-map
2591 data 'footnote-reference
2592 (lambda (fn)
2593 (cond
2594 ((string= (org-element-property :label fn) label)
2595 (throw 'exit fn))
2596 ;; If FN isn't inlined, be sure to traverse its
2597 ;; definition before resuming search. See
2598 ;; comments in `org-export-get-footnote-number'
2599 ;; for more information.
2600 ((eq (org-element-property :type fn) 'standard)
2601 (funcall search-refs
2602 (org-export-get-footnote-definition fn info)))))
2603 ;; Don't enter footnote definitions since it will
2604 ;; happen when their first reference is found.
2605 info 'first-match 'footnote-definition)))))
2606 (equal (catch 'exit (funcall search-refs (plist-get info :parse-tree)))
2607 footnote-reference)))))
2609 (defun org-export-get-footnote-definition (footnote-reference info)
2610 "Return definition of FOOTNOTE-REFERENCE as parsed data.
2611 INFO is the plist used as a communication channel."
2612 (let ((label (org-element-property :label footnote-reference)))
2613 (or (org-element-property :inline-definition footnote-reference)
2614 (cdr (assoc label (plist-get info :footnote-definition-alist))))))
2616 (defun org-export-get-footnote-number (footnote info)
2617 "Return number associated to a footnote.
2619 FOOTNOTE is either a footnote reference or a footnote definition.
2620 INFO is the plist used as a communication channel."
2621 (let ((label (org-element-property :label footnote))
2622 seen-refs
2623 (search-ref
2624 (function
2625 (lambda (data)
2626 ;; Search footnote references through DATA, filling
2627 ;; SEEN-REFS along the way.
2628 (org-element-map
2629 data 'footnote-reference
2630 (lambda (fn)
2631 (let ((fn-lbl (org-element-property :label fn)))
2632 (cond
2633 ;; Anonymous footnote match: return number.
2634 ((and (not fn-lbl) (equal fn footnote))
2635 (throw 'exit (1+ (length seen-refs))))
2636 ;; Labels match: return number.
2637 ((and label (string= label fn-lbl))
2638 (throw 'exit (1+ (length seen-refs))))
2639 ;; Anonymous footnote: it's always a new one. Also,
2640 ;; be sure to return nil from the `cond' so
2641 ;; `first-match' doesn't get us out of the loop.
2642 ((not fn-lbl) (push 'inline seen-refs) nil)
2643 ;; Label not seen so far: add it so SEEN-REFS.
2645 ;; Also search for subsequent references in footnote
2646 ;; definition so numbering following reading logic.
2647 ;; Note that we don't have to care about inline
2648 ;; definitions, since `org-element-map' already
2649 ;; traverse them at the right time.
2651 ;; Once again, return nil to stay in the loop.
2652 ((not (member fn-lbl seen-refs))
2653 (push fn-lbl seen-refs)
2654 (funcall search-ref
2655 (org-export-get-footnote-definition fn info))
2656 nil))))
2657 ;; Don't enter footnote definitions since it will happen
2658 ;; when their first reference is found.
2659 info 'first-match 'footnote-definition)))))
2660 (catch 'exit (funcall search-ref (plist-get info :parse-tree)))))
2663 ;;;; For Headlines
2665 ;; `org-export-get-relative-level' is a shortcut to get headline
2666 ;; level, relatively to the lower headline level in the parsed tree.
2668 ;; `org-export-get-headline-number' returns the section number of an
2669 ;; headline, while `org-export-number-to-roman' allows to convert it
2670 ;; to roman numbers.
2672 ;; `org-export-low-level-p', `org-export-first-sibling-p' and
2673 ;; `org-export-last-sibling-p' are three useful predicates when it
2674 ;; comes to fulfill the `:headline-levels' property.
2676 (defun org-export-get-relative-level (headline info)
2677 "Return HEADLINE relative level within current parsed tree.
2678 INFO is a plist holding contextual information."
2679 (+ (org-element-property :level headline)
2680 (or (plist-get info :headline-offset) 0)))
2682 (defun org-export-low-level-p (headline info)
2683 "Non-nil when HEADLINE is considered as low level.
2685 INFO is a plist used as a communication channel.
2687 A low level headlines has a relative level greater than
2688 `:headline-levels' property value.
2690 Return value is the difference between HEADLINE relative level
2691 and the last level being considered as high enough, or nil."
2692 (let ((limit (plist-get info :headline-levels)))
2693 (when (wholenump limit)
2694 (let ((level (org-export-get-relative-level headline info)))
2695 (and (> level limit) (- level limit))))))
2697 (defun org-export-get-headline-number (headline info)
2698 "Return HEADLINE numbering as a list of numbers.
2699 INFO is a plist holding contextual information."
2700 (cdr (assoc headline (plist-get info :headline-numbering))))
2702 (defun org-export-numbered-headline-p (headline info)
2703 "Return a non-nil value if HEADLINE element should be numbered.
2704 INFO is a plist used as a communication channel."
2705 (let ((sec-num (plist-get info :section-numbers))
2706 (level (org-export-get-relative-level headline info)))
2707 (if (wholenump sec-num) (<= level sec-num) sec-num)))
2709 (defun org-export-number-to-roman (n)
2710 "Convert integer N into a roman numeral."
2711 (let ((roman '((1000 . "M") (900 . "CM") (500 . "D") (400 . "CD")
2712 ( 100 . "C") ( 90 . "XC") ( 50 . "L") ( 40 . "XL")
2713 ( 10 . "X") ( 9 . "IX") ( 5 . "V") ( 4 . "IV")
2714 ( 1 . "I")))
2715 (res ""))
2716 (if (<= n 0)
2717 (number-to-string n)
2718 (while roman
2719 (if (>= n (caar roman))
2720 (setq n (- n (caar roman))
2721 res (concat res (cdar roman)))
2722 (pop roman)))
2723 res)))
2725 (defun org-export-first-sibling-p (headline info)
2726 "Non-nil when HEADLINE is the first sibling in its sub-tree.
2727 INFO is the plist used as a communication channel."
2728 (not (eq (org-element-type (org-export-get-previous-element headline info))
2729 'headline)))
2731 (defun org-export-last-sibling-p (headline info)
2732 "Non-nil when HEADLINE is the last sibling in its sub-tree.
2733 INFO is the plist used as a communication channel."
2734 (not (org-export-get-next-element headline info)))
2737 ;;;; For Links
2739 ;; `org-export-solidify-link-text' turns a string into a safer version
2740 ;; for links, replacing most non-standard characters with hyphens.
2742 ;; `org-export-get-coderef-format' returns an appropriate format
2743 ;; string for coderefs.
2745 ;; `org-export-inline-image-p' returns a non-nil value when the link
2746 ;; provided should be considered as an inline image.
2748 ;; `org-export-resolve-fuzzy-link' searches destination of fuzzy links
2749 ;; (i.e. links with "fuzzy" as type) within the parsed tree, and
2750 ;; returns an appropriate unique identifier when found, or nil.
2752 ;; `org-export-resolve-id-link' returns the first headline with
2753 ;; specified id or custom-id in parse tree, or nil when none was
2754 ;; found.
2756 ;; `org-export-resolve-coderef' associates a reference to a line
2757 ;; number in the element it belongs, or returns the reference itself
2758 ;; when the element isn't numbered.
2760 (defun org-export-solidify-link-text (s)
2761 "Take link text S and make a safe target out of it."
2762 (save-match-data
2763 (mapconcat 'identity (org-split-string s "[^a-zA-Z0-9_.-]+") "-")))
2765 (defun org-export-get-coderef-format (path desc)
2766 "Return format string for code reference link.
2767 PATH is the link path. DESC is its description."
2768 (save-match-data
2769 (cond ((not desc) "%s")
2770 ((string-match (regexp-quote (concat "(" path ")")) desc)
2771 (replace-match "%s" t t desc))
2772 (t desc))))
2774 (defun org-export-inline-image-p (link &optional rules)
2775 "Non-nil if LINK object points to an inline image.
2777 Optional argument is a set of RULES defining inline images. It
2778 is an alist where associations have the following shape:
2780 \(TYPE . REGEXP)
2782 Applying a rule means apply REGEXP against LINK's path when its
2783 type is TYPE. The function will return a non-nil value if any of
2784 the provided rules is non-nil. The default rule is
2785 `org-export-default-inline-image-rule'.
2787 This only applies to links without a description."
2788 (and (not (org-element-contents link))
2789 (let ((case-fold-search t)
2790 (rules (or rules org-export-default-inline-image-rule)))
2791 (some
2792 (lambda (rule)
2793 (and (string= (org-element-property :type link) (car rule))
2794 (string-match (cdr rule)
2795 (org-element-property :path link))))
2796 rules))))
2798 (defun org-export-resolve-fuzzy-link (link info)
2799 "Return LINK destination.
2801 INFO is a plist holding contextual information.
2803 Return value can be an object, an element, or nil:
2805 - If LINK path matches a target object (i.e. <<path>>) or
2806 element (i.e. \"#+TARGET: path\"), return it.
2808 - If LINK path exactly matches the name affiliated keyword
2809 \(i.e. #+NAME: path) of an element, return that element.
2811 - If LINK path exactly matches any headline name, return that
2812 element. If more than one headline share that name, priority
2813 will be given to the one with the closest common ancestor, if
2814 any, or the first one in the parse tree otherwise.
2816 - Otherwise, return nil.
2818 Assume LINK type is \"fuzzy\"."
2819 (let ((path (org-element-property :path link)))
2820 (cond
2821 ;; First try to find a matching "<<path>>" unless user specified
2822 ;; he was looking for an headline (path starts with a *
2823 ;; character).
2824 ((and (not (eq (substring path 0 1) ?*))
2825 (loop for target in (plist-get info :target-list)
2826 when (string= (org-element-property :value target) path)
2827 return target)))
2828 ;; Then try to find an element with a matching "#+NAME: path"
2829 ;; affiliated keyword.
2830 ((and (not (eq (substring path 0 1) ?*))
2831 (org-element-map
2832 (plist-get info :parse-tree) org-element-all-elements
2833 (lambda (el)
2834 (when (string= (org-element-property :name el) path) el))
2835 info 'first-match)))
2836 ;; Last case: link either points to an headline or to
2837 ;; nothingness. Try to find the source, with priority given to
2838 ;; headlines with the closest common ancestor. If such candidate
2839 ;; is found, return its beginning position as an unique
2840 ;; identifier, otherwise return nil.
2842 (let ((find-headline
2843 (function
2844 ;; Return first headline whose `:raw-value' property
2845 ;; is NAME in parse tree DATA, or nil.
2846 (lambda (name data)
2847 (org-element-map
2848 data 'headline
2849 (lambda (headline)
2850 (when (string=
2851 (org-element-property :raw-value headline)
2852 name)
2853 headline))
2854 info 'first-match)))))
2855 ;; Search among headlines sharing an ancestor with link,
2856 ;; from closest to farthest.
2857 (or (catch 'exit
2858 (mapc
2859 (lambda (parent)
2860 (when (eq (org-element-type parent) 'headline)
2861 (let ((foundp (funcall find-headline path parent)))
2862 (when foundp (throw 'exit foundp)))))
2863 (org-export-get-genealogy link info)) nil)
2864 ;; No match with a common ancestor: try the full parse-tree.
2865 (funcall find-headline path (plist-get info :parse-tree))))))))
2867 (defun org-export-resolve-id-link (link info)
2868 "Return headline referenced as LINK destination.
2870 INFO is a plist used as a communication channel.
2872 Return value can be an headline element or nil. Assume LINK type
2873 is either \"id\" or \"custom-id\"."
2874 (let ((id (org-element-property :path link)))
2875 (org-element-map
2876 (plist-get info :parse-tree) 'headline
2877 (lambda (headline)
2878 (when (or (string= (org-element-property :id headline) id)
2879 (string= (org-element-property :custom-id headline) id))
2880 headline))
2881 info 'first-match)))
2883 (defun org-export-resolve-coderef (ref info)
2884 "Resolve a code reference REF.
2886 INFO is a plist used as a communication channel.
2888 Return associated line number in source code, or REF itself,
2889 depending on src-block or example element's switches."
2890 (org-element-map
2891 (plist-get info :parse-tree) '(example-block src-block)
2892 (lambda (el)
2893 (with-temp-buffer
2894 (insert (org-trim (org-element-property :value el)))
2895 (let* ((label-fmt (regexp-quote
2896 (or (org-element-property :label-fmt el)
2897 org-coderef-label-format)))
2898 (ref-re
2899 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
2900 (replace-regexp-in-string "%s" ref label-fmt nil t))))
2901 ;; Element containing REF is found. Resolve it to either
2902 ;; a label or a line number, as needed.
2903 (when (re-search-backward ref-re nil t)
2904 (cond
2905 ((org-element-property :use-labels el) ref)
2906 ((eq (org-element-property :number-lines el) 'continued)
2907 (+ (org-export-get-loc el info) (line-number-at-pos)))
2908 (t (line-number-at-pos)))))))
2909 info 'first-match))
2912 ;;;; For Macros
2914 ;; `org-export-expand-macro' simply takes care of expanding macros.
2916 (defun org-export-expand-macro (macro info)
2917 "Expand MACRO and return it as a string.
2918 INFO is a plist holding export options."
2919 (let* ((key (org-element-property :key macro))
2920 (args (org-element-property :args macro))
2921 ;; User's macros are stored in the communication channel with
2922 ;; a ":macro-" prefix.
2923 (value (org-export-data
2924 (plist-get info (intern (format ":macro-%s" key))) val info)))
2925 ;; Replace arguments in VALUE.
2926 (let ((s 0) n)
2927 (while (string-match "\\$\\([0-9]+\\)" value s)
2928 (setq s (1+ (match-beginning 0))
2929 n (string-to-number (match-string 1 value)))
2930 (and (>= (length args) n)
2931 (setq value (replace-match (nth (1- n) args) t t value)))))
2932 ;; VALUE starts with "(eval": it is a s-exp, `eval' it.
2933 (when (string-match "\\`(eval\\>" value)
2934 (setq value (eval (read value))))
2935 ;; Return string.
2936 (format "%s" (or value ""))))
2939 ;;;; For References
2941 ;; `org-export-get-ordinal' associates a sequence number to any object
2942 ;; or element.
2944 (defun org-export-get-ordinal (element info &optional types predicate)
2945 "Return ordinal number of an element or object.
2947 ELEMENT is the element or object considered. INFO is the plist
2948 used as a communication channel.
2950 Optional argument TYPES, when non-nil, is a list of element or
2951 object types, as symbols, that should also be counted in.
2952 Otherwise, only provided element's type is considered.
2954 Optional argument PREDICATE is a function returning a non-nil
2955 value if the current element or object should be counted in. It
2956 accepts two arguments: the element or object being considered and
2957 the plist used as a communication channel. This allows to count
2958 only a certain type of objects (i.e. inline images).
2960 Return value is a list of numbers if ELEMENT is an headline or an
2961 item. It is nil for keywords. It represents the footnote number
2962 for footnote definitions and footnote references. If ELEMENT is
2963 a target, return the same value as if ELEMENT was the closest
2964 table, item or headline containing the target. In any other
2965 case, return the sequence number of ELEMENT among elements or
2966 objects of the same type."
2967 ;; A target keyword, representing an invisible target, never has
2968 ;; a sequence number.
2969 (unless (eq (org-element-type element) 'keyword)
2970 ;; Ordinal of a target object refer to the ordinal of the closest
2971 ;; table, item, or headline containing the object.
2972 (when (eq (org-element-type element) 'target)
2973 (setq element
2974 (loop for parent in (org-export-get-genealogy element info)
2975 when
2976 (memq
2977 (org-element-type parent)
2978 '(footnote-definition footnote-reference headline item
2979 table))
2980 return parent)))
2981 (case (org-element-type element)
2982 ;; Special case 1: An headline returns its number as a list.
2983 (headline (org-export-get-headline-number element info))
2984 ;; Special case 2: An item returns its number as a list.
2985 (item (let ((struct (org-element-property :structure element)))
2986 (org-list-get-item-number
2987 (org-element-property :begin element)
2988 struct
2989 (org-list-prevs-alist struct)
2990 (org-list-parents-alist struct))))
2991 ((footnote definition footnote-reference)
2992 (org-export-get-footnote-number element info))
2993 (otherwise
2994 (let ((counter 0))
2995 ;; Increment counter until ELEMENT is found again.
2996 (org-element-map
2997 (plist-get info :parse-tree) (or types (org-element-type element))
2998 (lambda (el)
2999 (cond
3000 ((equal element el) (1+ counter))
3001 ((not predicate) (incf counter) nil)
3002 ((funcall predicate el info) (incf counter) nil)))
3003 info 'first-match))))))
3006 ;;;; For Src-Blocks
3008 ;; `org-export-get-loc' counts number of code lines accumulated in
3009 ;; src-block or example-block elements with a "+n" switch until
3010 ;; a given element, excluded. Note: "-n" switches reset that count.
3012 ;; `org-export-unravel-code' extracts source code (along with a code
3013 ;; references alist) from an `element-block' or `src-block' type
3014 ;; element.
3016 ;; `org-export-format-code' applies a formatting function to each line
3017 ;; of code, providing relative line number and code reference when
3018 ;; appropriate. Since it doesn't access the original element from
3019 ;; which the source code is coming, it expects from the code calling
3020 ;; it to know if lines should be numbered and if code references
3021 ;; should appear.
3023 ;; Eventually, `org-export-format-code-default' is a higher-level
3024 ;; function (it makes use of the two previous functions) which handles
3025 ;; line numbering and code references inclusion, and returns source
3026 ;; code in a format suitable for plain text or verbatim output.
3028 (defun org-export-get-loc (element info)
3029 "Return accumulated lines of code up to ELEMENT.
3031 INFO is the plist used as a communication channel.
3033 ELEMENT is excluded from count."
3034 (let ((loc 0))
3035 (org-element-map
3036 (plist-get info :parse-tree)
3037 `(src-block example-block ,(org-element-type element))
3038 (lambda (el)
3039 (cond
3040 ;; ELEMENT is reached: Quit the loop.
3041 ((equal el element) t)
3042 ;; Only count lines from src-block and example-block elements
3043 ;; with a "+n" or "-n" switch. A "-n" switch resets counter.
3044 ((not (memq (org-element-type el) '(src-block example-block))) nil)
3045 ((let ((linums (org-element-property :number-lines el)))
3046 (when linums
3047 ;; Accumulate locs or reset them.
3048 (let ((lines (org-count-lines
3049 (org-trim (org-element-property :value el)))))
3050 (setq loc (if (eq linums 'new) lines (+ loc lines))))))
3051 ;; Return nil to stay in the loop.
3052 nil)))
3053 info 'first-match)
3054 ;; Return value.
3055 loc))
3057 (defun org-export-unravel-code (element)
3058 "Clean source code and extract references out of it.
3060 ELEMENT has either a `src-block' an `example-block' type.
3062 Return a cons cell whose CAR is the source code, cleaned from any
3063 reference and protective comma and CDR is an alist between
3064 relative line number (integer) and name of code reference on that
3065 line (string)."
3066 (let* ((line 0) refs
3067 ;; Get code and clean it. Remove blank lines at its
3068 ;; beginning and end. Also remove protective commas.
3069 (code (let ((c (replace-regexp-in-string
3070 "\\`\\([ \t]*\n\\)+" ""
3071 (replace-regexp-in-string
3072 "\\(:?[ \t]*\n\\)*[ \t]*\\'" "\n"
3073 (org-element-property :value element)))))
3074 ;; If appropriate, remove global indentation.
3075 (unless (or org-src-preserve-indentation
3076 (org-element-property :preserve-indent element))
3077 (setq c (org-remove-indentation c)))
3078 ;; Free up the protected lines. Note: Org blocks
3079 ;; have commas at the beginning or every line.
3080 (if (string= (org-element-property :language element) "org")
3081 (replace-regexp-in-string "^," "" c)
3082 (replace-regexp-in-string
3083 "^\\(,\\)\\(:?\\*\\|[ \t]*#\\+\\)" "" c nil nil 1))))
3084 ;; Get format used for references.
3085 (label-fmt (regexp-quote
3086 (or (org-element-property :label-fmt element)
3087 org-coderef-label-format)))
3088 ;; Build a regexp matching a loc with a reference.
3089 (with-ref-re
3090 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)[ \t]*\\)$"
3091 (replace-regexp-in-string
3092 "%s" "\\([-a-zA-Z0-9_ ]+\\)" label-fmt nil t))))
3093 ;; Return value.
3094 (cons
3095 ;; Code with references removed.
3096 (org-element-normalize-string
3097 (mapconcat
3098 (lambda (loc)
3099 (incf line)
3100 (if (not (string-match with-ref-re loc)) loc
3101 ;; Ref line: remove ref, and signal its position in REFS.
3102 (push (cons line (match-string 3 loc)) refs)
3103 (replace-match "" nil nil loc 1)))
3104 (org-split-string code "\n") "\n"))
3105 ;; Reference alist.
3106 refs)))
3108 (defun org-export-format-code (code fun &optional num-lines ref-alist)
3109 "Format CODE by applying FUN line-wise and return it.
3111 CODE is a string representing the code to format. FUN is
3112 a function. It must accept three arguments: a line of
3113 code (string), the current line number (integer) or nil and the
3114 reference associated to the current line (string) or nil.
3116 Optional argument NUM-LINES can be an integer representing the
3117 number of code lines accumulated until the current code. Line
3118 numbers passed to FUN will take it into account. If it is nil,
3119 FUN's second argument will always be nil. This number can be
3120 obtained with `org-export-get-loc' function.
3122 Optional argument REF-ALIST can be an alist between relative line
3123 number (i.e. ignoring NUM-LINES) and the name of the code
3124 reference on it. If it is nil, FUN's third argument will always
3125 be nil. It can be obtained through the use of
3126 `org-export-unravel-code' function."
3127 (let ((--locs (org-split-string code "\n"))
3128 (--line 0))
3129 (org-element-normalize-string
3130 (mapconcat
3131 (lambda (--loc)
3132 (incf --line)
3133 (let ((--ref (cdr (assq --line ref-alist))))
3134 (funcall fun --loc (and num-lines (+ num-lines --line)) --ref)))
3135 --locs "\n"))))
3137 (defun org-export-format-code-default (element info)
3138 "Return source code from ELEMENT, formatted in a standard way.
3140 ELEMENT is either a `src-block' or `example-block' element. INFO
3141 is a plist used as a communication channel.
3143 This function takes care of line numbering and code references
3144 inclusion. Line numbers, when applicable, appear at the
3145 beginning of the line, separated from the code by two white
3146 spaces. Code references, on the other hand, appear flushed to
3147 the right, separated by six white spaces from the widest line of
3148 code."
3149 ;; Extract code and references.
3150 (let* ((code-info (org-export-unravel-code element))
3151 (code (car code-info))
3152 (code-lines (org-split-string code "\n"))
3153 (refs (and (org-element-property :retain-labels element)
3154 (cdr code-info)))
3155 ;; Handle line numbering.
3156 (num-start (case (org-element-property :number-lines element)
3157 (continued (org-export-get-loc element info))
3158 (new 0)))
3159 (num-fmt
3160 (and num-start
3161 (format "%%%ds "
3162 (length (number-to-string
3163 (+ (length code-lines) num-start))))))
3164 ;; Prepare references display, if required. Any reference
3165 ;; should start six columns after the widest line of code,
3166 ;; wrapped with parenthesis.
3167 (max-width
3168 (+ (apply 'max (mapcar 'length code-lines))
3169 (if (not num-start) 0 (length (format num-fmt num-start))))))
3170 (org-export-format-code
3171 code
3172 (lambda (loc line-num ref)
3173 (let ((number-str (and num-fmt (format num-fmt line-num))))
3174 (concat
3175 number-str
3177 (and ref
3178 (concat (make-string
3179 (- (+ 6 max-width)
3180 (+ (length loc) (length number-str))) ? )
3181 (format "(%s)" ref))))))
3182 num-start refs)))
3185 ;;;; For Tables
3187 ;; `org-export-table-has-special-column-p' and
3188 ;; `org-export-table-row-is-special-p' are predicates used to look for
3189 ;; meta-information about the table structure.
3191 ;; `org-export-table-cell-width', `org-export-table-cell-alignment'
3192 ;; and `org-export-table-cell-borders' extract information from
3193 ;; a table-cell element.
3195 ;; `org-export-table-dimensions' gives the number on rows and columns
3196 ;; in the table, ignoring horizontal rules and special columns.
3197 ;; `org-export-table-cell-address', given a table-cell object, returns
3198 ;; the absolute address of a cell. On the other hand,
3199 ;; `org-export-get-table-cell-at' does the contrary.
3201 (defun org-export-table-has-special-column-p (table)
3202 "Non-nil when TABLE has a special column.
3203 All special columns will be ignored during export."
3204 ;; The table has a special column when every first cell of every row
3205 ;; has an empty value or contains a symbol among "/", "#", "!", "$",
3206 ;; "*" "_" and "^". Though, do not consider a first row containing
3207 ;; only empty cells as special.
3208 (let ((special-column-p 'empty))
3209 (catch 'exit
3210 (mapc
3211 (lambda (row)
3212 (when (eq (org-element-property :type row) 'standard)
3213 (let ((value (org-element-contents
3214 (car (org-element-contents row)))))
3215 (cond ((member value '(("/") ("#") ("!") ("$") ("*") ("_") ("^")))
3216 (setq special-column-p 'special))
3217 ((not value))
3218 (t (throw 'exit nil))))))
3219 (org-element-contents table))
3220 (eq special-column-p 'special))))
3222 (defun org-export-table-has-header-p (table info)
3223 "Non-nil when TABLE has an header.
3225 INFO is a plist used as a communication channel.
3227 A table has an header when it contains at least two row groups."
3228 (let ((rowgroup 1) row-flag)
3229 (org-element-map
3230 table 'table-row
3231 (lambda (row)
3232 (cond
3233 ((> rowgroup 1) t)
3234 ((and row-flag (eq (org-element-property :type row) 'rule))
3235 (incf rowgroup) (setq row-flag nil))
3236 ((and (not row-flag) (eq (org-element-property :type row) 'standard))
3237 (setq row-flag t) nil)))
3238 info)))
3240 (defun org-export-table-row-is-special-p (table-row info)
3241 "Non-nil if TABLE-ROW is considered special.
3243 INFO is a plist used as the communication channel.
3245 All special rows will be ignored during export."
3246 (when (eq (org-element-property :type table-row) 'standard)
3247 (let ((first-cell (org-element-contents
3248 (car (org-element-contents table-row)))))
3249 ;; A row is special either when...
3251 ;; ... it starts with a field only containing "/",
3252 (equal first-cell '("/"))
3253 ;; ... the table contains a special column and the row start
3254 ;; with a marking character among, "^", "_", "$" or "!",
3255 (and (org-export-table-has-special-column-p
3256 (org-export-get-parent table-row info))
3257 (member first-cell '(("^") ("_") ("$") ("!"))))
3258 ;; ... it contains only alignment cookies and empty cells.
3259 (let ((special-row-p 'empty))
3260 (catch 'exit
3261 (mapc
3262 (lambda (cell)
3263 (let ((value (org-element-contents cell)))
3264 ;; Since VALUE is a secondary string, the following
3265 ;; checks avoid expanding it with `org-export-data'.
3266 (cond ((not value))
3267 ((and (not (cdr value))
3268 (stringp (car value))
3269 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'"
3270 (car value)))
3271 (setq special-row-p 'cookie))
3272 (t (throw 'exit nil)))))
3273 (org-element-contents table-row))
3274 (eq special-row-p 'cookie)))))))
3276 (defun org-export-table-row-group (table-row info)
3277 "Return TABLE-ROW's group.
3279 INFO is a plist used as the communication channel.
3281 Return value is the group number, as an integer, or nil special
3282 rows and table rules. Group 1 is also table's header."
3283 (unless (or (eq (org-element-property :type table-row) 'rule)
3284 (org-export-table-row-is-special-p table-row info))
3285 (let ((group 0) row-flag)
3286 (catch 'found
3287 (mapc
3288 (lambda (row)
3289 (cond
3290 ((and (eq (org-element-property :type row) 'standard)
3291 (not (org-export-table-row-is-special-p row info)))
3292 (unless row-flag (incf group) (setq row-flag t)))
3293 ((eq (org-element-property :type row) 'rule)
3294 (setq row-flag nil)))
3295 (when (equal table-row row) (throw 'found group)))
3296 (org-element-contents (org-export-get-parent table-row info)))))))
3298 (defun org-export-table-cell-width (table-cell info)
3299 "Return TABLE-CELL contents width.
3301 INFO is a plist used as the communication channel.
3303 Return value is the width given by the last width cookie in the
3304 same column as TABLE-CELL, or nil."
3305 (let* ((genealogy (org-export-get-genealogy table-cell info))
3306 (row (car genealogy))
3307 (column (let ((cells (org-element-contents row)))
3308 (- (length cells) (length (member table-cell cells)))))
3309 (table (nth 1 genealogy))
3310 cookie-width)
3311 (mapc
3312 (lambda (row)
3313 (cond
3314 ;; In a special row, try to find a width cookie at COLUMN.
3315 ((org-export-table-row-is-special-p row info)
3316 (let ((value (org-element-contents
3317 (elt (org-element-contents row) column))))
3318 ;; The following checks avoid expanding unnecessarily the
3319 ;; cell with `org-export-data'
3320 (when (and value
3321 (not (cdr value))
3322 (stringp (car value))
3323 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'" (car value))
3324 (match-string 1 (car value)))
3325 (setq cookie-width
3326 (string-to-number (match-string 1 (car value)))))))
3327 ;; Ignore table rules.
3328 ((eq (org-element-property :type row) 'rule))))
3329 (org-element-contents table))
3330 ;; Return value.
3331 cookie-width))
3333 (defun org-export-table-cell-alignment (table-cell info)
3334 "Return TABLE-CELL contents alignment.
3336 INFO is a plist used as the communication channel.
3338 Return alignment as specified by the last alignment cookie in the
3339 same column as TABLE-CELL. If no such cookie is found, a default
3340 alignment value will be deduced from fraction of numbers in the
3341 column (see `org-table-number-fraction' for more information).
3342 Possible values are `left', `right' and `center'."
3343 (let* ((genealogy (org-export-get-genealogy table-cell info))
3344 (row (car genealogy))
3345 (column (let ((cells (org-element-contents row)))
3346 (- (length cells) (length (member table-cell cells)))))
3347 (table (nth 1 genealogy))
3348 (number-cells 0)
3349 (total-cells 0)
3350 cookie-align)
3351 (mapc
3352 (lambda (row)
3353 (cond
3354 ;; In a special row, try to find an alignment cookie at
3355 ;; COLUMN.
3356 ((org-export-table-row-is-special-p row info)
3357 (let ((value (org-element-contents
3358 (elt (org-element-contents row) column))))
3359 ;; Since VALUE is a secondary string, the following checks
3360 ;; avoid useless expansion through `org-export-data'.
3361 (when (and value
3362 (not (cdr value))
3363 (stringp (car value))
3364 (string-match "\\`<\\([lrc]\\)?\\([0-9]+\\)?>\\'"
3365 (car value))
3366 (match-string 1 (car value)))
3367 (setq cookie-align (match-string 1 (car value))))))
3368 ;; Ignore table rules.
3369 ((eq (org-element-property :type row) 'rule))
3370 ;; In a standard row, check if cell's contents are expressing
3371 ;; some kind of number. Increase NUMBER-CELLS accordingly.
3372 ;; Though, don't bother if an alignment cookie has already
3373 ;; defined cell's alignment.
3374 ((not cookie-align)
3375 (let ((value (org-export-data
3376 (org-element-contents
3377 (elt (org-element-contents row) column))
3378 info)))
3379 (incf total-cells)
3380 (when (string-match org-table-number-regexp value)
3381 (incf number-cells))))))
3382 (org-element-contents table))
3383 ;; Return value. Alignment specified by cookies has precedence
3384 ;; over alignment deduced from cells contents.
3385 (cond ((equal cookie-align "l") 'left)
3386 ((equal cookie-align "r") 'right)
3387 ((equal cookie-align "c") 'center)
3388 ((>= (/ (float number-cells) total-cells) org-table-number-fraction)
3389 'right)
3390 (t 'left))))
3392 (defun org-export-table-cell-borders (table-cell info)
3393 "Return TABLE-CELL borders.
3395 INFO is a plist used as a communication channel.
3397 Return value is a list of symbols, or nil. Possible values are:
3398 `top', `bottom', `above', `below', `left' and `right'. Note:
3399 `top' (resp. `bottom') only happen for a cell in the first
3400 row (resp. last row) of the table, ignoring table rules, if any.
3402 Returned borders ignore special rows."
3403 (let* ((genealogy (org-export-get-genealogy table-cell info))
3404 (row (car genealogy))
3405 (table (nth 1 genealogy))
3406 borders)
3407 ;; Top/above border? TABLE-CELL has a border above when a rule
3408 ;; used to demarcate row groups can be found above. Hence,
3409 ;; finding a rule isn't sufficient to push `above' in BORDERS:
3410 ;; another regular row has to be found above that rule.
3411 (let (rule-flag)
3412 (catch 'exit
3413 (mapc (lambda (row)
3414 (cond ((eq (org-element-property :type row) 'rule)
3415 (setq rule-flag t))
3416 ((not (org-export-table-row-is-special-p row info))
3417 (if rule-flag (throw 'exit (push 'above borders))
3418 (throw 'exit nil)))))
3419 ;; Look at every row before the current one.
3420 (cdr (member row (reverse (org-element-contents table)))))
3421 ;; No rule above, or rule found starts the table (ignoring any
3422 ;; special row): TABLE-CELL is at the top of the table.
3423 (when rule-flag (push 'above borders))
3424 (push 'top borders)))
3425 ;; Bottom/below border? TABLE-CELL has a border below when next
3426 ;; non-regular row below is a rule.
3427 (let (rule-flag)
3428 (catch 'exit
3429 (mapc (lambda (row)
3430 (cond ((eq (org-element-property :type row) 'rule)
3431 (setq rule-flag t))
3432 ((not (org-export-table-row-is-special-p row info))
3433 (if rule-flag (throw 'exit (push 'below borders))
3434 (throw 'exit nil)))))
3435 ;; Look at every row after the current one.
3436 (cdr (member row (org-element-contents table))))
3437 ;; No rule below, or rule found ends the table (modulo some
3438 ;; special row): TABLE-CELL is at the bottom of the table.
3439 (when rule-flag (push 'below borders))
3440 (push 'bottom borders)))
3441 ;; Right/left borders? They can only be specified by column
3442 ;; groups. Column groups are defined in a row starting with "/".
3443 ;; Also a column groups row only contains "<", "<>", ">" or blank
3444 ;; cells.
3445 (catch 'exit
3446 (let ((column (let ((cells (org-element-contents row)))
3447 (- (length cells) (length (member table-cell cells))))))
3448 (mapc
3449 (lambda (row)
3450 (unless (eq (org-element-property :type row) 'rule)
3451 (when (equal (org-element-contents
3452 (car (org-element-contents row)))
3453 '("/"))
3454 (let ((column-groups
3455 (mapcar
3456 (lambda (cell)
3457 (let ((value (org-element-contents cell)))
3458 (when (member value '(("<") ("<>") (">") nil))
3459 (car value))))
3460 (org-element-contents row))))
3461 ;; There's a left border when previous cell, if
3462 ;; any, ends a group, or current one starts one.
3463 (when (or (and (not (zerop column))
3464 (member (elt column-groups (1- column))
3465 '(">" "<>")))
3466 (member (elt column-groups column) '("<" "<>")))
3467 (push 'left borders))
3468 ;; There's a right border when next cell, if any,
3469 ;; starts a group, or current one ends one.
3470 (when (or (and (/= (1+ column) (length column-groups))
3471 (member (elt column-groups (1+ column))
3472 '("<" "<>")))
3473 (member (elt column-groups column) '(">" "<>")))
3474 (push 'right borders))
3475 (throw 'exit nil)))))
3476 ;; Table rows are read in reverse order so last column groups
3477 ;; row has precedence over any previous one.
3478 (reverse (org-element-contents table)))))
3479 ;; Return value.
3480 borders))
3482 (defun org-export-table-cell-starts-colgroup-p (table-cell info)
3483 "Non-nil when TABLE-CELL is at the beginning of a row group.
3484 INFO is a plist used as a communication channel."
3485 ;; A cell starts a column group either when it is at the beginning
3486 ;; of a row (or after the special column, if any) or when it has
3487 ;; a left border.
3488 (or (equal (org-element-map
3489 (org-export-get-parent table-cell info)
3490 'table-cell 'identity info 'first-match)
3491 table-cell)
3492 (memq 'left (org-export-table-cell-borders table-cell info))))
3494 (defun org-export-table-cell-ends-colgroup-p (table-cell info)
3495 "Non-nil when TABLE-CELL is at the end of a row group.
3496 INFO is a plist used as a communication channel."
3497 ;; A cell ends a column group either when it is at the end of a row
3498 ;; or when it has a right border.
3499 (or (equal (car (last (org-element-contents
3500 (org-export-get-parent table-cell info))))
3501 table-cell)
3502 (memq 'right (org-export-table-cell-borders table-cell info))))
3504 (defun org-export-table-row-starts-rowgroup-p (table-row info)
3505 "Non-nil when TABLE-ROW is at the beginning of a column group.
3506 INFO is a plist used as a communication channel."
3507 (unless (or (eq (org-element-property :type table-row) 'rule)
3508 (org-export-table-row-is-special-p table-row info))
3509 (let ((borders (org-export-table-cell-borders
3510 (car (org-element-contents table-row)) info)))
3511 (or (memq 'top borders) (memq 'above borders)))))
3513 (defun org-export-table-row-ends-rowgroup-p (table-row info)
3514 "Non-nil when TABLE-ROW is at the end of a column group.
3515 INFO is a plist used as a communication channel."
3516 (unless (or (eq (org-element-property :type table-row) 'rule)
3517 (org-export-table-row-is-special-p table-row info))
3518 (let ((borders (org-export-table-cell-borders
3519 (car (org-element-contents table-row)) info)))
3520 (or (memq 'bottom borders) (memq 'below borders)))))
3522 (defun org-export-table-row-starts-header-p (table-row info)
3523 "Non-nil when TABLE-ROW is the first table header's row.
3524 INFO is a plist used as a communication channel."
3525 (and (org-export-table-has-header-p
3526 (org-export-get-parent-table table-row info) info)
3527 (org-export-table-row-starts-rowgroup-p table-row info)
3528 (= (org-export-table-row-group table-row info) 1)))
3530 (defun org-export-table-row-ends-header-p (table-row info)
3531 "Non-nil when TABLE-ROW is the last table header's row.
3532 INFO is a plist used as a communication channel."
3533 (and (org-export-table-has-header-p
3534 (org-export-get-parent-table table-row info) info)
3535 (org-export-table-row-ends-rowgroup-p table-row info)
3536 (= (org-export-table-row-group table-row info) 1)))
3538 (defun org-export-table-dimensions (table info)
3539 "Return TABLE dimensions.
3541 INFO is a plist used as a communication channel.
3543 Return value is a CONS like (ROWS . COLUMNS) where
3544 ROWS (resp. COLUMNS) is the number of exportable
3545 rows (resp. columns)."
3546 (let (first-row (columns 0) (rows 0))
3547 ;; Set number of rows, and extract first one.
3548 (org-element-map
3549 table 'table-row
3550 (lambda (row)
3551 (when (eq (org-element-property :type row) 'standard)
3552 (incf rows)
3553 (unless first-row (setq first-row row)))) info)
3554 ;; Set number of columns.
3555 (org-element-map first-row 'table-cell (lambda (cell) (incf columns)) info)
3556 ;; Return value.
3557 (cons rows columns)))
3559 (defun org-export-table-cell-address (table-cell info)
3560 "Return address of a regular TABLE-CELL object.
3562 TABLE-CELL is the cell considered. INFO is a plist used as
3563 a communication channel.
3565 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
3566 zero-based index. Only exportable cells are considered. The
3567 function returns nil for other cells."
3568 (let* ((table-row (org-export-get-parent table-cell info))
3569 (table (org-export-get-parent-table table-cell info)))
3570 ;; Ignore cells in special rows or in special column.
3571 (unless (or (org-export-table-row-is-special-p table-row info)
3572 (and (org-export-table-has-special-column-p table)
3573 (equal (car (org-element-contents table-row)) table-cell)))
3574 (cons
3575 ;; Row number.
3576 (let ((row-count 0))
3577 (org-element-map
3578 table 'table-row
3579 (lambda (row)
3580 (cond ((eq (org-element-property :type row) 'rule) nil)
3581 ((equal row table-row) row-count)
3582 (t (incf row-count) nil)))
3583 info 'first-match))
3584 ;; Column number.
3585 (let ((col-count 0))
3586 (org-element-map
3587 table-row 'table-cell
3588 (lambda (cell)
3589 (if (equal cell table-cell) col-count
3590 (incf col-count) nil))
3591 info 'first-match))))))
3593 (defun org-export-get-table-cell-at (address table info)
3594 "Return regular table-cell object at ADDRESS in TABLE.
3596 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
3597 zero-based index. TABLE is a table type element. INFO is
3598 a plist used as a communication channel.
3600 If no table-cell, among exportable cells, is found at ADDRESS,
3601 return nil."
3602 (let ((column-pos (cdr address)) (column-count 0))
3603 (org-element-map
3604 ;; Row at (car address) or nil.
3605 (let ((row-pos (car address)) (row-count 0))
3606 (org-element-map
3607 table 'table-row
3608 (lambda (row)
3609 (cond ((eq (org-element-property :type row) 'rule) nil)
3610 ((= row-count row-pos) row)
3611 (t (incf row-count) nil)))
3612 info 'first-match))
3613 'table-cell
3614 (lambda (cell)
3615 (if (= column-count column-pos) cell
3616 (incf column-count) nil))
3617 info 'first-match)))
3620 ;;;; For Tables Of Contents
3622 ;; `org-export-collect-headlines' builds a list of all exportable
3623 ;; headline elements, maybe limited to a certain depth. One can then
3624 ;; easily parse it and transcode it.
3626 ;; Building lists of tables, figures or listings is quite similar.
3627 ;; Once the generic function `org-export-collect-elements' is defined,
3628 ;; `org-export-collect-tables', `org-export-collect-figures' and
3629 ;; `org-export-collect-listings' can be derived from it.
3631 (defun org-export-collect-headlines (info &optional n)
3632 "Collect headlines in order to build a table of contents.
3634 INFO is a plist used as a communication channel.
3636 When non-nil, optional argument N must be an integer. It
3637 specifies the depth of the table of contents.
3639 Return a list of all exportable headlines as parsed elements."
3640 (org-element-map
3641 (plist-get info :parse-tree)
3642 'headline
3643 (lambda (headline)
3644 ;; Strip contents from HEADLINE.
3645 (let ((relative-level (org-export-get-relative-level headline info)))
3646 (unless (and n (> relative-level n)) headline)))
3647 info))
3649 (defun org-export-collect-elements (type info &optional predicate)
3650 "Collect referenceable elements of a determined type.
3652 TYPE can be a symbol or a list of symbols specifying element
3653 types to search. Only elements with a caption or a name are
3654 collected.
3656 INFO is a plist used as a communication channel.
3658 When non-nil, optional argument PREDICATE is a function accepting
3659 one argument, an element of type TYPE. It returns a non-nil
3660 value when that element should be collected.
3662 Return a list of all elements found, in order of appearance."
3663 (org-element-map
3664 (plist-get info :parse-tree) type
3665 (lambda (element)
3666 (and (or (org-element-property :caption element)
3667 (org-element-property :name element))
3668 (or (not predicate) (funcall predicate element))
3669 element)) info))
3671 (defun org-export-collect-tables (info)
3672 "Build a list of tables.
3674 INFO is a plist used as a communication channel.
3676 Return a list of table elements with a caption or a name
3677 affiliated keyword."
3678 (org-export-collect-elements 'table info))
3680 (defun org-export-collect-figures (info predicate)
3681 "Build a list of figures.
3683 INFO is a plist used as a communication channel. PREDICATE is
3684 a function which accepts one argument: a paragraph element and
3685 whose return value is non-nil when that element should be
3686 collected.
3688 A figure is a paragraph type element, with a caption or a name,
3689 verifying PREDICATE. The latter has to be provided since
3690 a \"figure\" is a vague concept that may depend on back-end.
3692 Return a list of elements recognized as figures."
3693 (org-export-collect-elements 'paragraph info predicate))
3695 (defun org-export-collect-listings (info)
3696 "Build a list of src blocks.
3698 INFO is a plist used as a communication channel.
3700 Return a list of src-block elements with a caption or a name
3701 affiliated keyword."
3702 (org-export-collect-elements 'src-block info))
3705 ;;;; Topology
3707 ;; Here are various functions to retrieve information about the
3708 ;; neighbourhood of a given element or object. Neighbours of interest
3709 ;; are direct parent (`org-export-get-parent'), parent headline
3710 ;; (`org-export-get-parent-headline'), parent paragraph
3711 ;; (`org-export-get-parent-paragraph'), previous element or object
3712 ;; (`org-export-get-previous-element') and next element or object
3713 ;; (`org-export-get-next-element').
3715 ;; All of these functions are just a specific use of the more generic
3716 ;; `org-export-get-genealogy', which returns the genealogy relative to
3717 ;; the element or object.
3719 (defun org-export-get-genealogy (blob info)
3720 "Return genealogy relative to a given element or object.
3721 BLOB is the element or object being considered. INFO is a plist
3722 used as a communication channel."
3723 (let* ((type (org-element-type blob))
3724 (end (org-element-property :end blob))
3725 (walk-data
3726 (lambda (data genealogy)
3727 ;; Walk DATA, looking for BLOB. GENEALOGY is the list of
3728 ;; parents of all elements in DATA.
3729 (mapc
3730 (lambda (el)
3731 (cond
3732 ((stringp el) nil)
3733 ((equal el blob) (throw 'exit genealogy))
3734 ((>= (org-element-property :end el) end)
3735 ;; If BLOB is an object and EL contains a secondary
3736 ;; string, be sure to check it.
3737 (when (memq type org-element-all-objects)
3738 (let ((sec-prop
3739 (cdr (assq (org-element-type el)
3740 org-element-secondary-value-alist))))
3741 (when sec-prop
3742 (funcall
3743 walk-data
3744 (cons 'org-data
3745 (cons nil (org-element-property sec-prop el)))
3746 (cons el genealogy)))))
3747 (funcall walk-data el (cons el genealogy)))))
3748 (org-element-contents data)))))
3749 (catch 'exit (funcall walk-data (plist-get info :parse-tree) nil) nil)))
3751 (defun org-export-get-parent (blob info)
3752 "Return BLOB parent or nil.
3753 BLOB is the element or object considered. INFO is a plist used
3754 as a communication channel."
3755 (car (org-export-get-genealogy blob info)))
3757 (defun org-export-get-parent-headline (blob info)
3758 "Return BLOB parent headline or nil.
3759 BLOB is the element or object being considered. INFO is a plist
3760 used as a communication channel."
3761 (catch 'exit
3762 (mapc
3763 (lambda (el) (when (eq (org-element-type el) 'headline) (throw 'exit el)))
3764 (org-export-get-genealogy blob info))
3765 nil))
3767 (defun org-export-get-parent-paragraph (object info)
3768 "Return OBJECT parent paragraph or nil.
3769 OBJECT is the object to consider. INFO is a plist used as
3770 a communication channel."
3771 (catch 'exit
3772 (mapc
3773 (lambda (el) (when (eq (org-element-type el) 'paragraph) (throw 'exit el)))
3774 (org-export-get-genealogy object info))
3775 nil))
3777 (defun org-export-get-parent-table (object info)
3778 "Return OBJECT parent table or nil.
3779 OBJECT is either a `table-cell' or `table-element' type object.
3780 INFO is a plist used as a communication channel."
3781 (catch 'exit
3782 (mapc
3783 (lambda (el) (when (eq (org-element-type el) 'table) (throw 'exit el)))
3784 (org-export-get-genealogy object info))
3785 nil))
3787 (defun org-export-get-previous-element (blob info)
3788 "Return previous element or object.
3790 BLOB is an element or object. INFO is a plist used as
3791 a communication channel.
3793 Return previous element or object, a string, or nil."
3794 (let ((parent (org-export-get-parent blob info)))
3795 (cadr (member blob (reverse (org-element-contents parent))))))
3797 (defun org-export-get-next-element (blob info)
3798 "Return next element or object.
3800 BLOB is an element or object. INFO is a plist used as
3801 a communication channel.
3803 Return next element or object, a string, or nil."
3804 (let ((parent (org-export-get-parent blob info)))
3805 (cadr (member blob (org-element-contents parent)))))
3809 ;;; The Dispatcher
3811 ;; `org-export-dispatch' is the standard interactive way to start an
3812 ;; export process. It uses `org-export-dispatch-ui' as a subroutine
3813 ;; for its interface. Most commons back-ends should have an entry in
3814 ;; it.
3816 (defun org-export-dispatch ()
3817 "Export dispatcher for Org mode.
3819 It provides an access to common export related tasks in a buffer.
3820 Its interface comes in two flavours: standard and expert. While
3821 both share the same set of bindings, only the former displays the
3822 valid keys associations. Set `org-export-dispatch-use-expert-ui'
3823 to switch to one or the other.
3825 Return an error if key pressed has no associated command."
3826 (interactive)
3827 (let* ((input (org-export-dispatch-ui
3828 (if (listp org-export-initial-scope) org-export-initial-scope
3829 (list org-export-initial-scope))
3830 org-export-dispatch-use-expert-ui))
3831 (raw-key (car input))
3832 (optns (cdr input)))
3833 ;; Translate "C-a", "C-b"... into "a", "b"... Then take action
3834 ;; depending on user's key pressed.
3835 (case (if (< raw-key 27) (+ raw-key 96) raw-key)
3836 ;; Allow to quit with "q" key.
3837 (?q nil)
3838 ;; Export with `e-ascii' back-end.
3839 ((?A ?N ?U)
3840 (let ((outbuf
3841 (org-export-to-buffer
3842 'e-ascii "*Org E-ASCII Export*"
3843 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)
3844 `(:ascii-charset
3845 ,(case raw-key (?A 'ascii) (?N 'latin1) (t 'utf-8))))))
3846 (with-current-buffer outbuf (text-mode))
3847 (when org-export-show-temporary-export-buffer
3848 (switch-to-buffer-other-window outbuf))))
3849 ((?a ?n ?u)
3850 (org-e-ascii-export-to-ascii
3851 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)
3852 `(:ascii-charset ,(case raw-key (?a 'ascii) (?n 'latin1) (t 'utf-8)))))
3853 ;; Export with `e-latex' back-end.
3855 (let ((outbuf
3856 (org-export-to-buffer
3857 'e-latex "*Org E-LaTeX Export*"
3858 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))))
3859 (with-current-buffer outbuf (latex-mode))
3860 (when org-export-show-temporary-export-buffer
3861 (switch-to-buffer-other-window outbuf))))
3862 (?l (org-e-latex-export-to-latex
3863 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
3864 (?p (org-e-latex-export-to-pdf
3865 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
3866 (?d (org-open-file
3867 (org-e-latex-export-to-pdf
3868 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))))
3869 ;; Export with `e-html' back-end.
3871 (let ((outbuf
3872 (org-export-to-buffer
3873 'e-html "*Org E-HTML Export*"
3874 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))))
3875 ;; set major mode
3876 (with-current-buffer outbuf
3877 (if (featurep 'nxhtml-mode) (nxhtml-mode) (nxml-mode)))
3878 (when org-export-show-temporary-export-buffer
3879 (switch-to-buffer-other-window outbuf))))
3880 (?h (org-e-html-export-to-html
3881 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
3882 (?b (org-open-file
3883 (org-e-html-export-to-html
3884 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))))
3885 ;; Export with `e-odt' back-end.
3886 (?o (org-e-odt-export-to-odt
3887 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
3888 (?O (org-open-file
3889 (org-e-odt-export-to-odt
3890 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))))
3891 ;; Publishing facilities
3892 (?F (org-e-publish-current-file (memq 'force optns)))
3893 (?P (org-e-publish-current-project (memq 'force optns)))
3894 (?X (let ((project
3895 (assoc (org-icompleting-read
3896 "Publish project: " org-e-publish-project-alist nil t)
3897 org-e-publish-project-alist)))
3898 (org-e-publish project (memq 'force optns))))
3899 (?E (org-e-publish-all (memq 'force optns)))
3900 ;; Undefined command.
3901 (t (error "No command associated with key %s"
3902 (char-to-string raw-key))))))
3904 (defun org-export-dispatch-ui (options expertp)
3905 "Handle interface for `org-export-dispatch'.
3907 OPTIONS is a list containing current interactive options set for
3908 export. It can contain any of the following symbols:
3909 `body' toggles a body-only export
3910 `subtree' restricts export to current subtree
3911 `visible' restricts export to visible part of buffer.
3912 `force' force publishing files.
3914 EXPERTP, when non-nil, triggers expert UI. In that case, no help
3915 buffer is provided, but indications about currently active
3916 options are given in the prompt. Moreover, \[?] allows to switch
3917 back to standard interface.
3919 Return value is a list with key pressed as CAR and a list of
3920 final interactive export options as CDR."
3921 (let ((help
3922 (format "---- (Options) -------------------------------------------
3924 \[1] Body only: %s [2] Export scope: %s
3925 \[3] Visible only: %s [4] Force publishing: %s
3928 --- (ASCII/Latin-1/UTF-8 Export) -------------------------
3930 \[a/n/u] to TXT file [A/N/U] to temporary buffer
3932 --- (HTML Export) ----------------------------------------
3934 \[h] to HTML file [b] ... and open it
3935 \[H] to temporary buffer
3937 --- (LaTeX Export) ---------------------------------------
3939 \[l] to TEX file [L] to temporary buffer
3940 \[p] to PDF file [d] ... and open it
3942 --- (ODF Export) -----------------------------------------
3944 \[o] to ODT file [O] ... and open it
3946 --- (Publish) --------------------------------------------
3948 \[F] current file [P] current project
3949 \[X] a project [E] every project"
3950 (if (memq 'body options) "On " "Off")
3951 (if (memq 'subtree options) "Subtree" "Buffer ")
3952 (if (memq 'visible options) "On " "Off")
3953 (if (memq 'force options) "On " "Off")))
3954 (standard-prompt "Export command: ")
3955 (expert-prompt (format "Export command (%s%s%s%s): "
3956 (if (memq 'body options) "b" "-")
3957 (if (memq 'subtree options) "s" "-")
3958 (if (memq 'visible options) "v" "-")
3959 (if (memq 'force options) "f" "-")))
3960 (handle-keypress
3961 (function
3962 ;; Read a character from command input, toggling interactive
3963 ;; options when applicable. PROMPT is the displayed prompt,
3964 ;; as a string.
3965 (lambda (prompt)
3966 (let ((key (read-char-exclusive prompt)))
3967 (cond
3968 ;; Ignore non-standard characters (i.e. "M-a").
3969 ((not (characterp key)) (org-export-dispatch-ui options expertp))
3970 ;; Help key: Switch back to standard interface if
3971 ;; expert UI was active.
3972 ((eq key ??) (org-export-dispatch-ui options nil))
3973 ;; Toggle export options.
3974 ((memq key '(?1 ?2 ?3 ?4))
3975 (org-export-dispatch-ui
3976 (let ((option (case key (?1 'body) (?2 'subtree) (?3 'visible)
3977 (?4 'force))))
3978 (if (memq option options) (remq option options)
3979 (cons option options)))
3980 expertp))
3981 ;; Action selected: Send key and options back to
3982 ;; `org-export-dispatch'.
3983 (t (cons key options))))))))
3984 ;; With expert UI, just read key with a fancy prompt. In standard
3985 ;; UI, display an intrusive help buffer.
3986 (if expertp (funcall handle-keypress expert-prompt)
3987 (save-window-excursion
3988 (delete-other-windows)
3989 (with-output-to-temp-buffer "*Org Export/Publishing Help*" (princ help))
3990 (org-fit-window-to-buffer
3991 (get-buffer-window "*Org Export/Publishing Help*"))
3992 (funcall handle-keypress standard-prompt)))))
3995 (provide 'org-export)
3996 ;;; org-export.el ends here