org-export: Fix byte-compilation
[org-mode.git] / contrib / lisp / org-export.el
blobf23671836657a0b988e39e1a162d4096e6427a6f
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. See "The Filter System"
45 ;; section for more information.
47 ;; The core function is `org-export-as'. It returns the transcoded
48 ;; buffer as a string.
50 ;; A back-end is defined through one mandatory variable: his
51 ;; translation table. Its name is always
52 ;; `org-BACKEND-translate-alist' where BACKEND stands for the name
53 ;; chosen for the back-end. Its value is an alist whose keys are
54 ;; elements and objects types and values translator functions.
56 ;; These functions should return a string without any trailing space,
57 ;; or nil. They must accept three arguments: the object or element
58 ;; itself, its contents or nil when it isn't recursive and the
59 ;; property list used as a communication channel.
61 ;; Contents, when not nil, are stripped from any global indentation
62 ;; (although the relative one is preserved). They also always end
63 ;; with a single newline character.
65 ;; If, for a given type, no function is found, that element or object
66 ;; type will simply be ignored, along with any blank line or white
67 ;; space at its end. The same will happen if the function returns the
68 ;; nil value. If that function returns the empty string, the type
69 ;; will be ignored, but the blank lines or white spaces will be kept.
71 ;; In addition to element and object types, one function can be
72 ;; associated to the `template' symbol and another one to the
73 ;; `plain-text' symbol.
75 ;; The former returns the final transcoded string, and can be used to
76 ;; add a preamble and a postamble to document's body. It must accept
77 ;; two arguments: the transcoded string and the property list
78 ;; containing export options.
80 ;; The latter, when defined, is to be called on every text not
81 ;; recognized as an element or an object. It must accept two
82 ;; arguments: the text string and the information channel. It is an
83 ;; appropriate place to protect special chars relative to the
84 ;; back-end.
86 ;; Optionally, a back-end can support specific buffer keywords and
87 ;; OPTION keyword's items by setting `org-BACKEND-filters-alist'
88 ;; variable. Refer to `org-export-options-alist' documentation for
89 ;; more information about its value.
91 ;; If the new back-end shares most properties with another one,
92 ;; `org-export-define-derived-backend' can be used to simplify the
93 ;; process.
95 ;; Any back-end can define its own variables. Among them, those
96 ;; customizables should belong to the `org-export-BACKEND' group.
98 ;; Tools for common tasks across back-ends are implemented in the
99 ;; penultimate part of this file. A dispatcher for standard back-ends
100 ;; is provided in the last one.
102 ;;; Code:
104 (eval-when-compile (require 'cl))
105 (require 'org-element)
107 (declare-function org-e-ascii-export-to-ascii "org-e-ascii"
108 (&optional subtreep visible-only body-only ext-plist pub-dir))
109 (declare-function org-e-html-export-to-html "org-e-html"
110 (&optional subtreep visible-only body-only ext-plist pub-dir))
111 (declare-function org-e-latex-export-to-latex "org-e-latex"
112 (&optional subtreep visible-only body-only ext-plist pub-dir))
113 (declare-function org-e-latex-export-to-pdf "org-e-latex"
114 (&optional subtreep visible-only body-only ext-plist pub-dir))
115 (declare-function org-e-odt-export-to-odt "org-e-odt"
116 (&optional subtreep visible-only body-only ext-plist pub-dir))
117 (declare-function org-e-publish "org-e-publish" (project &optional force))
118 (declare-function org-e-publish-all "org-e-publish" (&optional force))
119 (declare-function org-e-publish-current-file "org-e-publish" (&optional force))
120 (declare-function org-e-publish-current-project "org-e-publish"
121 (&optional force))
122 (declare-function org-export-blocks-preprocess "org-exp-blocks")
124 (defvar org-e-publish-project-alist)
125 (defvar org-table-number-fraction)
126 (defvar org-table-number-regexp)
130 ;;; Internal Variables
132 ;; Among internal variables, the most important is
133 ;; `org-export-options-alist'. This variable define the global export
134 ;; options, shared between every exporter, and how they are acquired.
136 (defconst org-export-max-depth 19
137 "Maximum nesting depth for headlines, counting from 0.")
139 (defconst org-export-options-alist
140 '((:author "AUTHOR" nil user-full-name t)
141 (:creator "CREATOR" nil org-export-creator-string)
142 (:date "DATE" nil nil t)
143 (:description "DESCRIPTION" nil nil newline)
144 (:email "EMAIL" nil user-mail-address t)
145 (:exclude-tags "EXPORT_EXCLUDE_TAGS" nil org-export-exclude-tags split)
146 (:headline-levels nil "H" org-export-headline-levels)
147 (:keywords "KEYWORDS" nil nil space)
148 (:language "LANGUAGE" nil org-export-default-language t)
149 (:preserve-breaks nil "\\n" org-export-preserve-breaks)
150 (:section-numbers nil "num" org-export-with-section-numbers)
151 (:select-tags "EXPORT_SELECT_TAGS" nil org-export-select-tags split)
152 (:time-stamp-file nil "timestamp" org-export-time-stamp-file)
153 (:title "TITLE" nil nil space)
154 (:with-archived-trees nil "arch" org-export-with-archived-trees)
155 (:with-author nil "author" org-export-with-author)
156 (:with-clocks nil "c" org-export-with-clocks)
157 (:with-creator nil "creator" org-export-with-creator)
158 (:with-drawers nil "d" org-export-with-drawers)
159 (:with-email nil "email" org-export-with-email)
160 (:with-emphasize nil "*" org-export-with-emphasize)
161 (:with-entities nil "e" org-export-with-entities)
162 (:with-fixed-width nil ":" org-export-with-fixed-width)
163 (:with-footnotes nil "f" org-export-with-footnotes)
164 (:with-plannings nil "p" org-export-with-planning)
165 (:with-priority nil "pri" org-export-with-priority)
166 (:with-special-strings nil "-" org-export-with-special-strings)
167 (:with-sub-superscript nil "^" org-export-with-sub-superscripts)
168 (:with-toc nil "toc" org-export-with-toc)
169 (:with-tables nil "|" org-export-with-tables)
170 (:with-tags nil "tags" org-export-with-tags)
171 (:with-tasks nil "tasks" org-export-with-tasks)
172 (:with-timestamps nil "<" org-export-with-timestamps)
173 (:with-todo-keywords nil "todo" org-export-with-todo-keywords))
174 "Alist between export properties and ways to set them.
176 The CAR of the alist is the property name, and the CDR is a list
177 like (KEYWORD OPTION DEFAULT BEHAVIOUR) where:
179 KEYWORD is a string representing a buffer keyword, or nil.
180 OPTION is a string that could be found in an #+OPTIONS: line.
181 DEFAULT is the default value for the property.
182 BEHAVIOUR determine how Org should handle multiple keywords for
183 the same property. It is a symbol among:
184 nil Keep old value and discard the new one.
185 t Replace old value with the new one.
186 `space' Concatenate the values, separating them with a space.
187 `newline' Concatenate the values, separating them with
188 a newline.
189 `split' Split values at white spaces, and cons them to the
190 previous list.
192 KEYWORD and OPTION have precedence over DEFAULT.
194 All these properties should be back-end agnostic. For back-end
195 specific properties, define a similar variable named
196 `org-BACKEND-options-alist', replacing BACKEND with the name of
197 the appropriate back-end. You can also redefine properties
198 there, as they have precedence over these.")
200 (defconst org-export-special-keywords
201 '("SETUP_FILE" "OPTIONS" "MACRO")
202 "List of in-buffer keywords that require special treatment.
203 These keywords are not directly associated to a property. The
204 way they are handled must be hard-coded into
205 `org-export-get-inbuffer-options' function.")
207 (defconst org-export-filters-alist
208 '((:filter-bold . org-export-filter-bold-functions)
209 (:filter-babel-call . org-export-filter-babel-call-functions)
210 (:filter-center-block . org-export-filter-center-block-functions)
211 (:filter-clock . org-export-filter-clock-functions)
212 (:filter-code . org-export-filter-code-functions)
213 (:filter-comment . org-export-filter-comment-functions)
214 (:filter-comment-block . org-export-filter-comment-block-functions)
215 (:filter-drawer . org-export-filter-drawer-functions)
216 (:filter-dynamic-block . org-export-filter-dynamic-block-functions)
217 (:filter-entity . org-export-filter-entity-functions)
218 (:filter-example-block . org-export-filter-example-block-functions)
219 (:filter-export-block . org-export-filter-export-block-functions)
220 (:filter-export-snippet . org-export-filter-export-snippet-functions)
221 (:filter-final-output . org-export-filter-final-output-functions)
222 (:filter-fixed-width . org-export-filter-fixed-width-functions)
223 (:filter-footnote-definition . org-export-filter-footnote-definition-functions)
224 (:filter-footnote-reference . org-export-filter-footnote-reference-functions)
225 (:filter-headline . org-export-filter-headline-functions)
226 (:filter-horizontal-rule . org-export-filter-horizontal-rule-functions)
227 (:filter-inline-babel-call . org-export-filter-inline-babel-call-functions)
228 (:filter-inline-src-block . org-export-filter-inline-src-block-functions)
229 (:filter-inlinetask . org-export-filter-inlinetask-functions)
230 (:filter-italic . org-export-filter-italic-functions)
231 (:filter-item . org-export-filter-item-functions)
232 (:filter-keyword . org-export-filter-keyword-functions)
233 (:filter-latex-environment . org-export-filter-latex-environment-functions)
234 (:filter-latex-fragment . org-export-filter-latex-fragment-functions)
235 (:filter-line-break . org-export-filter-line-break-functions)
236 (:filter-link . org-export-filter-link-functions)
237 (:filter-macro . org-export-filter-macro-functions)
238 (:filter-paragraph . org-export-filter-paragraph-functions)
239 (:filter-parse-tree . org-export-filter-parse-tree-functions)
240 (:filter-plain-list . org-export-filter-plain-list-functions)
241 (:filter-plain-text . org-export-filter-plain-text-functions)
242 (:filter-planning . org-export-filter-planning-functions)
243 (:filter-property-drawer . org-export-filter-property-drawer-functions)
244 (:filter-quote-block . org-export-filter-quote-block-functions)
245 (:filter-quote-section . org-export-filter-quote-section-functions)
246 (:filter-radio-target . org-export-filter-radio-target-functions)
247 (:filter-section . org-export-filter-section-functions)
248 (:filter-special-block . org-export-filter-special-block-functions)
249 (:filter-src-block . org-export-filter-src-block-functions)
250 (:filter-statistics-cookie . org-export-filter-statistics-cookie-functions)
251 (:filter-strike-through . org-export-filter-strike-through-functions)
252 (:filter-subscript . org-export-filter-subscript-functions)
253 (:filter-superscript . org-export-filter-superscript-functions)
254 (:filter-table . org-export-filter-table-functions)
255 (:filter-table-cell . org-export-filter-table-cell-functions)
256 (:filter-table-row . org-export-filter-table-row-functions)
257 (:filter-target . org-export-filter-target-functions)
258 (:filter-timestamp . org-export-filter-timestamp-functions)
259 (:filter-underline . org-export-filter-underline-functions)
260 (:filter-verbatim . org-export-filter-verbatim-functions)
261 (:filter-verse-block . org-export-filter-verse-block-functions))
262 "Alist between filters properties and initial values.
264 The key of each association is a property name accessible through
265 the communication channel its value is a configurable global
266 variable defining initial filters.
268 This list is meant to install user specified filters. Back-end
269 developers may install their own filters using
270 `org-BACKEND-filters-alist', where BACKEND is the name of the
271 considered back-end. Filters defined there will always be
272 prepended to the current list, so they always get applied
273 first.")
275 (defconst org-export-default-inline-image-rule
276 `(("file" .
277 ,(format "\\.%s\\'"
278 (regexp-opt
279 '("png" "jpeg" "jpg" "gif" "tiff" "tif" "xbm"
280 "xpm" "pbm" "pgm" "ppm") t))))
281 "Default rule for link matching an inline image.
282 This rule applies to links with no description. By default, it
283 will be considered as an inline image if it targets a local file
284 whose extension is either \"png\", \"jpeg\", \"jpg\", \"gif\",
285 \"tiff\", \"tif\", \"xbm\", \"xpm\", \"pbm\", \"pgm\" or \"ppm\".
286 See `org-export-inline-image-p' for more information about
287 rules.")
291 ;;; User-configurable Variables
293 ;; Configuration for the masses.
295 ;; They should never be accessed directly, as their value is to be
296 ;; stored in a property list (cf. `org-export-options-alist').
297 ;; Back-ends will read their value from there instead.
299 (defgroup org-export nil
300 "Options for exporting Org mode files."
301 :tag "Org Export"
302 :group 'org)
304 (defgroup org-export-general nil
305 "General options for export engine."
306 :tag "Org Export General"
307 :group 'org-export)
309 (defcustom org-export-with-archived-trees 'headline
310 "Whether sub-trees with the ARCHIVE tag should be exported.
312 This can have three different values:
313 nil Do not export, pretend this tree is not present.
314 t Do export the entire tree.
315 `headline' Only export the headline, but skip the tree below it.
317 This option can also be set with the #+OPTIONS line,
318 e.g. \"arch:nil\"."
319 :group 'org-export-general
320 :type '(choice
321 (const :tag "Not at all" nil)
322 (const :tag "Headline only" 'headline)
323 (const :tag "Entirely" t)))
325 (defcustom org-export-with-author t
326 "Non-nil means insert author name into the exported file.
327 This option can also be set with the #+OPTIONS line,
328 e.g. \"author:nil\"."
329 :group 'org-export-general
330 :type 'boolean)
332 (defcustom org-export-with-clocks nil
333 "Non-nil means export CLOCK keywords.
334 This option can also be set with the #+OPTIONS line,
335 e.g. \"c:t\"."
336 :group 'org-export-general
337 :type 'boolean)
339 (defcustom org-export-with-creator 'comment
340 "Non-nil means the postamble should contain a creator sentence.
342 The sentence can be set in `org-export-creator-string' and
343 defaults to \"Generated by Org mode XX in Emacs XXX.\".
345 If the value is `comment' insert it as a comment."
346 :group 'org-export-general
347 :type '(choice
348 (const :tag "No creator sentence" nil)
349 (const :tag "Sentence as a comment" 'comment)
350 (const :tag "Insert the sentence" t)))
352 (defcustom org-export-creator-string
353 (format "Generated by Org mode %s in Emacs %s."
354 (if (fboundp 'org-version) (org-version) "(Unknown)")
355 emacs-version)
356 "String to insert at the end of the generated document."
357 :group 'org-export-general
358 :type '(string :tag "Creator string"))
360 (defcustom org-export-with-drawers t
361 "Non-nil means export contents of standard drawers.
363 When t, all drawers are exported. This may also be a list of
364 drawer names to export. This variable doesn't apply to
365 properties drawers.
367 This option can also be set with the #+OPTIONS line,
368 e.g. \"d:nil\"."
369 :group 'org-export-general
370 :type '(choice
371 (const :tag "All drawers" t)
372 (const :tag "None" nil)
373 (repeat :tag "Selected drawers"
374 (string :tag "Drawer name"))))
376 (defcustom org-export-with-email nil
377 "Non-nil means insert author email into the exported file.
378 This option can also be set with the #+OPTIONS line,
379 e.g. \"email:t\"."
380 :group 'org-export-general
381 :type 'boolean)
383 (defcustom org-export-with-emphasize t
384 "Non-nil means interpret *word*, /word/, and _word_ as emphasized text.
386 If the export target supports emphasizing text, the word will be
387 typeset in bold, italic, or underlined, respectively. Not all
388 export backends support this.
390 This option can also be set with the #+OPTIONS line, e.g. \"*:nil\"."
391 :group 'org-export-general
392 :type 'boolean)
394 (defcustom org-export-exclude-tags '("noexport")
395 "Tags that exclude a tree from export.
397 All trees carrying any of these tags will be excluded from
398 export. This is without condition, so even subtrees inside that
399 carry one of the `org-export-select-tags' will be removed.
401 This option can also be set with the #+EXPORT_EXCLUDE_TAGS:
402 keyword."
403 :group 'org-export-general
404 :type '(repeat (string :tag "Tag")))
406 (defcustom org-export-with-fixed-width t
407 "Non-nil means lines starting with \":\" will be in fixed width font.
409 This can be used to have pre-formatted text, fragments of code
410 etc. For example:
411 : ;; Some Lisp examples
412 : (while (defc cnt)
413 : (ding))
414 will be looking just like this in also HTML. See also the QUOTE
415 keyword. Not all export backends support this.
417 This option can also be set with the #+OPTIONS line, e.g. \"::nil\"."
418 :group 'org-export-translation
419 :type 'boolean)
421 (defcustom org-export-with-footnotes t
422 "Non-nil means Org footnotes should be exported.
423 This option can also be set with the #+OPTIONS line,
424 e.g. \"f:nil\"."
425 :group 'org-export-general
426 :type 'boolean)
428 (defcustom org-export-headline-levels 3
429 "The last level which is still exported as a headline.
431 Inferior levels will produce itemize lists when exported. Note
432 that a numeric prefix argument to an exporter function overrides
433 this setting.
435 This option can also be set with the #+OPTIONS line, e.g. \"H:2\"."
436 :group 'org-export-general
437 :type 'integer)
439 (defcustom org-export-default-language "en"
440 "The default language for export and clocktable translations, as a string.
441 This may have an association in
442 `org-clock-clocktable-language-setup'."
443 :group 'org-export-general
444 :type '(string :tag "Language"))
446 (defcustom org-export-preserve-breaks nil
447 "Non-nil means preserve all line breaks when exporting.
449 Normally, in HTML output paragraphs will be reformatted.
451 This option can also be set with the #+OPTIONS line,
452 e.g. \"\\n:t\"."
453 :group 'org-export-general
454 :type 'boolean)
456 (defcustom org-export-with-entities t
457 "Non-nil means interpret entities when exporting.
459 For example, HTML export converts \\alpha to &alpha; and \\AA to
460 &Aring;.
462 For a list of supported names, see the constant `org-entities'
463 and the user option `org-entities-user'.
465 This option can also be set with the #+OPTIONS line,
466 e.g. \"e:nil\"."
467 :group 'org-export-general
468 :type 'boolean)
470 (defcustom org-export-with-planning nil
471 "Non-nil means include planning info in export.
472 This option can also be set with the #+OPTIONS: line,
473 e.g. \"p:t\"."
474 :group 'org-export-general
475 :type 'boolean)
477 (defcustom org-export-with-priority nil
478 "Non-nil means include priority cookies in export.
480 When nil, remove priority cookies for export.
482 This option can also be set with the #+OPTIONS line,
483 e.g. \"pri:t\"."
484 :group 'org-export-general
485 :type 'boolean)
487 (defcustom org-export-with-section-numbers t
488 "Non-nil means add section numbers to headlines when exporting.
490 When set to an integer n, numbering will only happen for
491 headlines whose relative level is higher or equal to n.
493 This option can also be set with the #+OPTIONS line,
494 e.g. \"num:t\"."
495 :group 'org-export-general
496 :type 'boolean)
498 (defcustom org-export-select-tags '("export")
499 "Tags that select a tree for export.
501 If any such tag is found in a buffer, all trees that do not carry
502 one of these tags will be ignored during export. Inside trees
503 that are selected like this, you can still deselect a subtree by
504 tagging it with one of the `org-export-exclude-tags'.
506 This option can also be set with the #+EXPORT_SELECT_TAGS:
507 keyword."
508 :group 'org-export-general
509 :type '(repeat (string :tag "Tag")))
511 (defcustom org-export-with-special-strings t
512 "Non-nil means interpret \"\-\", \"--\" and \"---\" for export.
514 When this option is turned on, these strings will be exported as:
516 Org HTML LaTeX
517 -----+----------+--------
518 \\- &shy; \\-
519 -- &ndash; --
520 --- &mdash; ---
521 ... &hellip; \ldots
523 This option can also be set with the #+OPTIONS line,
524 e.g. \"-:nil\"."
525 :group 'org-export-general
526 :type 'boolean)
528 (defcustom org-export-with-sub-superscripts t
529 "Non-nil means interpret \"_\" and \"^\" for export.
531 When this option is turned on, you can use TeX-like syntax for
532 sub- and superscripts. Several characters after \"_\" or \"^\"
533 will be considered as a single item - so grouping with {} is
534 normally not needed. For example, the following things will be
535 parsed as single sub- or superscripts.
537 10^24 or 10^tau several digits will be considered 1 item.
538 10^-12 or 10^-tau a leading sign with digits or a word
539 x^2-y^3 will be read as x^2 - y^3, because items are
540 terminated by almost any nonword/nondigit char.
541 x_{i^2} or x^(2-i) braces or parenthesis do grouping.
543 Still, ambiguity is possible - so when in doubt use {} to enclose
544 the sub/superscript. If you set this variable to the symbol
545 `{}', the braces are *required* in order to trigger
546 interpretations as sub/superscript. This can be helpful in
547 documents that need \"_\" frequently in plain text.
549 This option can also be set with the #+OPTIONS line,
550 e.g. \"^:nil\"."
551 :group 'org-export-general
552 :type '(choice
553 (const :tag "Interpret them" t)
554 (const :tag "Curly brackets only" {})
555 (const :tag "Do not interpret them" nil)))
557 (defcustom org-export-with-toc t
558 "Non-nil means create a table of contents in exported files.
560 The TOC contains headlines with levels up
561 to`org-export-headline-levels'. When an integer, include levels
562 up to N in the toc, this may then be different from
563 `org-export-headline-levels', but it will not be allowed to be
564 larger than the number of headline levels. When nil, no table of
565 contents is made.
567 This option can also be set with the #+OPTIONS line,
568 e.g. \"toc:nil\" or \"toc:3\"."
569 :group 'org-export-general
570 :type '(choice
571 (const :tag "No Table of Contents" nil)
572 (const :tag "Full Table of Contents" t)
573 (integer :tag "TOC to level")))
575 (defcustom org-export-with-tables t
576 "If non-nil, lines starting with \"|\" define a table.
577 For example:
579 | Name | Address | Birthday |
580 |-------------+----------+-----------|
581 | Arthur Dent | England | 29.2.2100 |
583 This option can also be set with the #+OPTIONS line, e.g. \"|:nil\"."
584 :group 'org-export-general
585 :type 'boolean)
587 (defcustom org-export-with-tags t
588 "If nil, do not export tags, just remove them from headlines.
590 If this is the symbol `not-in-toc', tags will be removed from
591 table of contents entries, but still be shown in the headlines of
592 the document.
594 This option can also be set with the #+OPTIONS line,
595 e.g. \"tags:nil\"."
596 :group 'org-export-general
597 :type '(choice
598 (const :tag "Off" nil)
599 (const :tag "Not in TOC" not-in-toc)
600 (const :tag "On" t)))
602 (defcustom org-export-with-tasks t
603 "Non-nil means include TODO items for export.
604 This may have the following values:
605 t include tasks independent of state.
606 todo include only tasks that are not yet done.
607 done include only tasks that are already done.
608 nil remove all tasks before export
609 list of keywords keep only tasks with these keywords"
610 :group 'org-export-general
611 :type '(choice
612 (const :tag "All tasks" t)
613 (const :tag "No tasks" nil)
614 (const :tag "Not-done tasks" todo)
615 (const :tag "Only done tasks" done)
616 (repeat :tag "Specific TODO keywords"
617 (string :tag "Keyword"))))
619 (defcustom org-export-time-stamp-file t
620 "Non-nil means insert a time stamp into the exported file.
621 The time stamp shows when the file was created.
623 This option can also be set with the #+OPTIONS line,
624 e.g. \"timestamp:nil\"."
625 :group 'org-export-general
626 :type 'boolean)
628 (defcustom org-export-with-timestamps t
629 "Non nil means allow timestamps in export.
631 It can be set to `active', `inactive', t or nil, in order to
632 export, respectively, only active timestamps, only inactive ones,
633 all of them or none.
635 This option can also be set with the #+OPTIONS line, e.g.
636 \"<:nil\"."
637 :group 'org-export-general
638 :type '(choice
639 (const :tag "All timestamps" t)
640 (const :tag "Only active timestamps" active)
641 (const :tag "Only inactive timestamps" inactive)
642 (const :tag "No timestamp" nil)))
644 (defcustom org-export-with-todo-keywords t
645 "Non-nil means include TODO keywords in export.
646 When nil, remove all these keywords from the export."
647 :group 'org-export-general
648 :type 'boolean)
650 (defcustom org-export-allow-BIND 'confirm
651 "Non-nil means allow #+BIND to define local variable values for export.
652 This is a potential security risk, which is why the user must
653 confirm the use of these lines."
654 :group 'org-export-general
655 :type '(choice
656 (const :tag "Never" nil)
657 (const :tag "Always" t)
658 (const :tag "Ask a confirmation for each file" confirm)))
660 (defcustom org-export-snippet-translation-alist nil
661 "Alist between export snippets back-ends and exporter back-ends.
663 This variable allows to provide shortcuts for export snippets.
665 For example, with a value of '\(\(\"h\" . \"e-html\"\)\), the
666 HTML back-end will recognize the contents of \"@@h:<b>@@\" as
667 HTML code while every other back-end will ignore it."
668 :group 'org-export-general
669 :type '(repeat
670 (cons
671 (string :tag "Shortcut")
672 (string :tag "Back-end"))))
674 (defcustom org-export-coding-system nil
675 "Coding system for the exported file."
676 :group 'org-export-general
677 :type 'coding-system)
679 (defcustom org-export-copy-to-kill-ring t
680 "Non-nil means exported stuff will also be pushed onto the kill ring."
681 :group 'org-export-general
682 :type 'boolean)
684 (defcustom org-export-initial-scope 'buffer
685 "The initial scope when exporting with `org-export-dispatch'.
686 This variable can be either set to `buffer' or `subtree'."
687 :group 'org-export-general
688 :type '(choice
689 (const :tag "Export current buffer" 'buffer)
690 (const :tag "Export current subtree" 'subtree)))
692 (defcustom org-export-show-temporary-export-buffer t
693 "Non-nil means show buffer after exporting to temp buffer.
694 When Org exports to a file, the buffer visiting that file is ever
695 shown, but remains buried. However, when exporting to
696 a temporary buffer, that buffer is popped up in a second window.
697 When this variable is nil, the buffer remains buried also in
698 these cases."
699 :group 'org-export-general
700 :type 'boolean)
702 (defcustom org-export-dispatch-use-expert-ui nil
703 "Non-nil means using a non-intrusive `org-export-dispatch'.
704 In that case, no help buffer is displayed. Though, an indicator
705 for current export scope is added to the prompt \(i.e. \"b\" when
706 output is restricted to body only, \"s\" when it is restricted to
707 the current subtree and \"v\" when only visible elements are
708 considered for export\). Also, \[?] allows to switch back to
709 standard mode."
710 :group 'org-export-general
711 :type 'boolean)
715 ;;; Defining New Back-ends
717 (defmacro org-export-define-derived-backend (child parent &rest body)
718 "Create a new back-end as a variant of an existing one.
720 CHILD is the name of the derived back-end. PARENT is the name of
721 the parent back-end.
723 BODY can start with pre-defined keyword arguments. The following
724 keywords are understood:
726 `:filters-alist'
728 Alist of filters that will overwrite or complete filters
729 defined in PARENT back-end, if any.
731 `:options-alist'
733 Alist of buffer keywords or #+OPTIONS items that will
734 overwrite or complete those defined in PARENT back-end, if
735 any.
737 `:translate-alist'
739 Alist of element and object types and transcoders that will
740 overwrite or complete transcode table from PARENT back-end.
742 As an example, here is how one could define \"my-latex\" back-end
743 as a variant of `e-latex' back-end with a custom template
744 function:
746 \(org-export-define-derived-backend my-latex e-latex
747 :translate-alist ((template . my-latex-template-fun)))
749 The back-end could then be called with, for example:
751 \(org-export-to-buffer 'my-latex \"*Test my-latex\")"
752 (declare (debug (&define name symbolp [&rest keywordp sexp] def-body))
753 (indent 2))
754 (let (filters options translate)
755 (while (keywordp (car body))
756 (case (pop body)
757 (:filters-alist (setq filters (pop body)))
758 (:options-alist (setq options (pop body)))
759 (:translate-alist (setq translate (pop body)))
760 (t (pop body))))
761 `(progn
762 ;; Define filters.
763 ,(let ((parent-filters (intern (format "org-%s-filters-alist" parent))))
764 (when (or (boundp parent-filters) filters)
765 `(defconst ,(intern (format "org-%s-filters-alist" child))
766 ',(append filters
767 (and (boundp parent-filters)
768 (copy-sequence (symbol-value parent-filters))))
769 "Alist between filters keywords and back-end specific filters.
770 See `org-export-filters-alist' for more information.")))
771 ;; Define options.
772 ,(let ((parent-options (intern (format "org-%s-options-alist" parent))))
773 (when (or (boundp parent-options) options)
774 `(defconst ,(intern (format "org-%s-options-alist" child))
775 ',(append options
776 (and (boundp parent-options)
777 (copy-sequence (symbol-value parent-options))))
778 "Alist between LaTeX export properties and ways to set them.
779 See `org-export-options-alist' for more information on the
780 structure of the values.")))
781 ;; Define translators.
782 (defvar ,(intern (format "org-%s-translate-alist" child))
783 ',(append translate
784 (copy-sequence
785 (symbol-value
786 (intern (format "org-%s-translate-alist" parent)))))
787 "Alist between element or object types and translators.")
788 ;; Splice in the body, if any.
789 ,@body)))
793 ;;; The Communication Channel
795 ;; During export process, every function has access to a number of
796 ;; properties. They are of two types:
798 ;; 1. Environment options are collected once at the very beginning of
799 ;; the process, out of the original buffer and configuration.
800 ;; Collecting them is handled by `org-export-get-environment'
801 ;; function.
803 ;; Most environment options are defined through the
804 ;; `org-export-options-alist' variable.
806 ;; 2. Tree properties are extracted directly from the parsed tree,
807 ;; just before export, by `org-export-collect-tree-properties'.
809 ;; Here is the full list of properties available during transcode
810 ;; process, with their category (option, tree or local) and their
811 ;; value type.
813 ;; + `:author' :: Author's name.
814 ;; - category :: option
815 ;; - type :: string
817 ;; + `:back-end' :: Current back-end used for transcoding.
818 ;; - category :: tree
819 ;; - type :: symbol
821 ;; + `:creator' :: String to write as creation information.
822 ;; - category :: option
823 ;; - type :: string
825 ;; + `:date' :: String to use as date.
826 ;; - category :: option
827 ;; - type :: string
829 ;; + `:description' :: Description text for the current data.
830 ;; - category :: option
831 ;; - type :: string
833 ;; + `:email' :: Author's email.
834 ;; - category :: option
835 ;; - type :: string
837 ;; + `:exclude-tags' :: Tags for exclusion of subtrees from export
838 ;; process.
839 ;; - category :: option
840 ;; - type :: list of strings
842 ;; + `:footnote-definition-alist' :: Alist between footnote labels and
843 ;; their definition, as parsed data. Only non-inlined footnotes
844 ;; are represented in this alist. Also, every definition isn't
845 ;; guaranteed to be referenced in the parse tree. The purpose of
846 ;; this property is to preserve definitions from oblivion
847 ;; (i.e. when the parse tree comes from a part of the original
848 ;; buffer), it isn't meant for direct use in a back-end. To
849 ;; retrieve a definition relative to a reference, use
850 ;; `org-export-get-footnote-definition' instead.
851 ;; - category :: option
852 ;; - type :: alist (STRING . LIST)
854 ;; + `:headline-levels' :: Maximum level being exported as an
855 ;; headline. Comparison is done with the relative level of
856 ;; headlines in the parse tree, not necessarily with their
857 ;; actual level.
858 ;; - category :: option
859 ;; - type :: integer
861 ;; + `:headline-offset' :: Difference between relative and real level
862 ;; of headlines in the parse tree. For example, a value of -1
863 ;; means a level 2 headline should be considered as level
864 ;; 1 (cf. `org-export-get-relative-level').
865 ;; - category :: tree
866 ;; - type :: integer
868 ;; + `:headline-numbering' :: Alist between headlines and their
869 ;; numbering, as a list of numbers
870 ;; (cf. `org-export-get-headline-number').
871 ;; - category :: tree
872 ;; - type :: alist (INTEGER . LIST)
874 ;; + `:id-alist' :: Alist between ID strings and destination file's
875 ;; path, relative to current directory. It is used by
876 ;; `org-export-resolve-id-link' to resolve ID links targeting an
877 ;; external file.
878 ;; - category :: option
879 ;; - type :: alist (STRING . STRING)
881 ;; + `:ignore-list' :: List of elements and objects that should be
882 ;; ignored during export.
883 ;; - category :: tree
884 ;; - type :: list of elements and objects
886 ;; + `:input-file' :: Full path to input file, if any.
887 ;; - category :: option
888 ;; - type :: string or nil
890 ;; + `:keywords' :: List of keywords attached to data.
891 ;; - category :: option
892 ;; - type :: string
894 ;; + `:language' :: Default language used for translations.
895 ;; - category :: option
896 ;; - type :: string
898 ;; + `:parse-tree' :: Whole parse tree, available at any time during
899 ;; transcoding.
900 ;; - category :: option
901 ;; - type :: list (as returned by `org-element-parse-buffer')
903 ;; + `:preserve-breaks' :: Non-nil means transcoding should preserve
904 ;; all line breaks.
905 ;; - category :: option
906 ;; - type :: symbol (nil, t)
908 ;; + `:section-numbers' :: Non-nil means transcoding should add
909 ;; section numbers to headlines.
910 ;; - category :: option
911 ;; - type :: symbol (nil, t)
913 ;; + `:select-tags' :: List of tags enforcing inclusion of sub-trees
914 ;; in transcoding. When such a tag is present, subtrees without
915 ;; it are de facto excluded from the process. See
916 ;; `use-select-tags'.
917 ;; - category :: option
918 ;; - type :: list of strings
920 ;; + `:target-list' :: List of targets encountered in the parse tree.
921 ;; This is used to partly resolve "fuzzy" links
922 ;; (cf. `org-export-resolve-fuzzy-link').
923 ;; - category :: tree
924 ;; - type :: list of strings
926 ;; + `:time-stamp-file' :: Non-nil means transcoding should insert
927 ;; a time stamp in the output.
928 ;; - category :: option
929 ;; - type :: symbol (nil, t)
931 ;; + `:translate-alist' :: Alist between element and object types and
932 ;; transcoding functions relative to the current back-end.
933 ;; Special keys `template' and `plain-text' are also possible.
934 ;; - category :: option
935 ;; - type :: alist (SYMBOL . FUNCTION)
937 ;; + `:with-archived-trees' :: Non-nil when archived subtrees should
938 ;; also be transcoded. If it is set to the `headline' symbol,
939 ;; only the archived headline's name is retained.
940 ;; - category :: option
941 ;; - type :: symbol (nil, t, `headline')
943 ;; + `:with-author' :: Non-nil means author's name should be included
944 ;; in the output.
945 ;; - category :: option
946 ;; - type :: symbol (nil, t)
948 ;; + `:with-clocks' :: Non-nild means clock keywords should be exported.
949 ;; - category :: option
950 ;; - type :: symbol (nil, t)
952 ;; + `:with-creator' :: Non-nild means a creation sentence should be
953 ;; inserted at the end of the transcoded string. If the value
954 ;; is `comment', it should be commented.
955 ;; - category :: option
956 ;; - type :: symbol (`comment', nil, t)
958 ;; + `:with-drawers' :: Non-nil means drawers should be exported. If
959 ;; its value is a list of names, only drawers with such names
960 ;; will be transcoded.
961 ;; - category :: option
962 ;; - type :: symbol (nil, t) or list of strings
964 ;; + `:with-email' :: Non-nil means output should contain author's
965 ;; email.
966 ;; - category :: option
967 ;; - type :: symbol (nil, t)
969 ;; + `:with-emphasize' :: Non-nil means emphasized text should be
970 ;; interpreted.
971 ;; - category :: option
972 ;; - type :: symbol (nil, t)
974 ;; + `:with-fixed-width' :: Non-nil if transcoder should interpret
975 ;; strings starting with a colon as a fixed-with (verbatim) area.
976 ;; - category :: option
977 ;; - type :: symbol (nil, t)
979 ;; + `:with-footnotes' :: Non-nil if transcoder should interpret
980 ;; footnotes.
981 ;; - category :: option
982 ;; - type :: symbol (nil, t)
984 ;; + `:with-plannings' :: Non-nil means transcoding should include
985 ;; planning info.
986 ;; - category :: option
987 ;; - type :: symbol (nil, t)
989 ;; + `:with-priority' :: Non-nil means transcoding should include
990 ;; priority cookies.
991 ;; - category :: option
992 ;; - type :: symbol (nil, t)
994 ;; + `:with-special-strings' :: Non-nil means transcoding should
995 ;; interpret special strings in plain text.
996 ;; - category :: option
997 ;; - type :: symbol (nil, t)
999 ;; + `:with-sub-superscript' :: Non-nil means transcoding should
1000 ;; interpret subscript and superscript. With a value of "{}",
1001 ;; only interpret those using curly brackets.
1002 ;; - category :: option
1003 ;; - type :: symbol (nil, {}, t)
1005 ;; + `:with-tables' :: Non-nil means transcoding should interpret
1006 ;; tables.
1007 ;; - category :: option
1008 ;; - type :: symbol (nil, t)
1010 ;; + `:with-tags' :: Non-nil means transcoding should keep tags in
1011 ;; headlines. A `not-in-toc' value will remove them from the
1012 ;; table of contents, if any, nonetheless.
1013 ;; - category :: option
1014 ;; - type :: symbol (nil, t, `not-in-toc')
1016 ;; + `:with-tasks' :: Non-nil means transcoding should include
1017 ;; headlines with a TODO keyword. A `todo' value will only
1018 ;; include headlines with a todo type keyword while a `done'
1019 ;; value will do the contrary. If a list of strings is provided,
1020 ;; only tasks with keywords belonging to that list will be kept.
1021 ;; - category :: option
1022 ;; - type :: symbol (t, todo, done, nil) or list of strings
1024 ;; + `:with-timestamps' :: Non-nil means transcoding should include
1025 ;; time stamps. Special value `active' (resp. `inactive') ask to
1026 ;; export only active (resp. inactive) timestamps. Otherwise,
1027 ;; completely remove them.
1028 ;; - category :: option
1029 ;; - type :: symbol: (`active', `inactive', t, nil)
1031 ;; + `:with-toc' :: Non-nil means that a table of contents has to be
1032 ;; added to the output. An integer value limits its depth.
1033 ;; - category :: option
1034 ;; - type :: symbol (nil, t or integer)
1036 ;; + `:with-todo-keywords' :: Non-nil means transcoding should
1037 ;; include TODO keywords.
1038 ;; - category :: option
1039 ;; - type :: symbol (nil, t)
1042 ;;;; Environment Options
1044 ;; Environment options encompass all parameters defined outside the
1045 ;; scope of the parsed data. They come from five sources, in
1046 ;; increasing precedence order:
1048 ;; - Global variables,
1049 ;; - Buffer's attributes,
1050 ;; - Options keyword symbols,
1051 ;; - Buffer keywords,
1052 ;; - Subtree properties.
1054 ;; The central internal function with regards to environment options
1055 ;; is `org-export-get-environment'. It updates global variables with
1056 ;; "#+BIND:" keywords, then retrieve and prioritize properties from
1057 ;; the different sources.
1059 ;; The internal functions doing the retrieval are:
1060 ;; `org-export-get-global-options',
1061 ;; `org-export-get-buffer-attributes',
1062 ;; `org-export-parse-option-keyword',
1063 ;; `org-export-get-subtree-options' and
1064 ;; `org-export-get-inbuffer-options'
1066 ;; Also, `org-export-confirm-letbind' and `org-export-install-letbind'
1067 ;; take care of the part relative to "#+BIND:" keywords.
1069 (defun org-export-get-environment (&optional backend subtreep ext-plist)
1070 "Collect export options from the current buffer.
1072 Optional argument BACKEND is a symbol specifying which back-end
1073 specific options to read, if any.
1075 When optional argument SUBTREEP is non-nil, assume the export is
1076 done against the current sub-tree.
1078 Third optional argument EXT-PLIST is a property list with
1079 external parameters overriding Org default settings, but still
1080 inferior to file-local settings."
1081 ;; First install #+BIND variables.
1082 (org-export-install-letbind-maybe)
1083 ;; Get and prioritize export options...
1084 (org-combine-plists
1085 ;; ... from global variables...
1086 (org-export-get-global-options backend)
1087 ;; ... from buffer's attributes...
1088 (org-export-get-buffer-attributes)
1089 ;; ... from an external property list...
1090 ext-plist
1091 ;; ... from in-buffer settings...
1092 (org-export-get-inbuffer-options
1093 backend
1094 (and buffer-file-name (org-remove-double-quotes buffer-file-name)))
1095 ;; ... and from subtree, when appropriate.
1096 (and subtreep (org-export-get-subtree-options backend))
1097 ;; Eventually install back-end symbol and its translation table.
1098 `(:back-end
1099 ,backend
1100 :translate-alist
1101 ,(let ((trans-alist (intern (format "org-%s-translate-alist" backend))))
1102 (when (boundp trans-alist) (symbol-value trans-alist))))))
1104 (defun org-export-parse-option-keyword (options &optional backend)
1105 "Parse an OPTIONS line and return values as a plist.
1106 Optional argument BACKEND is a symbol specifying which back-end
1107 specific items to read, if any."
1108 (let* ((all
1109 (append org-export-options-alist
1110 (and backend
1111 (let ((var (intern
1112 (format "org-%s-options-alist" backend))))
1113 (and (boundp var) (eval var))))))
1114 ;; Build an alist between #+OPTION: item and property-name.
1115 (alist (delq nil
1116 (mapcar (lambda (e)
1117 (when (nth 2 e) (cons (regexp-quote (nth 2 e))
1118 (car e))))
1119 all)))
1120 plist)
1121 (mapc (lambda (e)
1122 (when (string-match (concat "\\(\\`\\|[ \t]\\)"
1123 (car e)
1124 ":\\(([^)\n]+)\\|[^ \t\n\r;,.]*\\)")
1125 options)
1126 (setq plist (plist-put plist
1127 (cdr e)
1128 (car (read-from-string
1129 (match-string 2 options)))))))
1130 alist)
1131 plist))
1133 (defun org-export-get-subtree-options (&optional backend)
1134 "Get export options in subtree at point.
1135 Optional argument BACKEND is a symbol specifying back-end used
1136 for export. Return options as a plist."
1137 (org-with-wide-buffer
1138 (let (prop plist)
1139 ;; Make sure point is at an heading.
1140 (unless (org-at-heading-p) (org-back-to-heading t))
1141 (when (setq prop (progn (looking-at org-todo-line-regexp)
1142 (or (save-match-data
1143 (org-entry-get (point) "EXPORT_TITLE"))
1144 (org-match-string-no-properties 3))))
1145 (setq plist
1146 (plist-put
1147 plist :title
1148 (org-element-parse-secondary-string
1149 prop (org-element-restriction 'keyword)))))
1150 (when (setq prop (org-entry-get (point) "EXPORT_TEXT"))
1151 (setq plist (plist-put plist :text prop)))
1152 (when (setq prop (org-entry-get (point) "EXPORT_AUTHOR"))
1153 (setq plist (plist-put plist :author prop)))
1154 (when (setq prop (org-entry-get (point) "EXPORT_DATE"))
1155 (setq plist (plist-put plist :date prop)))
1156 (when (setq prop (org-entry-get (point) "EXPORT_OPTIONS"))
1157 (setq plist
1158 (nconc plist (org-export-parse-option-keyword prop backend))))
1159 plist)))
1161 (defun org-export-get-inbuffer-options (&optional backend files)
1162 "Return current buffer export options, as a plist.
1164 Optional argument BACKEND, when non-nil, is a symbol specifying
1165 which back-end specific options should also be read in the
1166 process.
1168 Optional argument FILES is a list of setup files names read so
1169 far, used to avoid circular dependencies.
1171 Assume buffer is in Org mode. Narrowing, if any, is ignored."
1172 (org-with-wide-buffer
1173 (goto-char (point-min))
1174 (let ((case-fold-search t) plist)
1175 ;; 1. Special keywords, as in `org-export-special-keywords'.
1176 (let ((special-re (org-make-options-regexp org-export-special-keywords)))
1177 (while (re-search-forward special-re nil t)
1178 (let ((element (org-element-at-point)))
1179 (when (eq (org-element-type element) 'keyword)
1180 (let* ((key (org-element-property :key element))
1181 (val (org-element-property :value element))
1182 (prop
1183 (cond
1184 ((string= key "SETUP_FILE")
1185 (let ((file
1186 (expand-file-name
1187 (org-remove-double-quotes (org-trim val)))))
1188 ;; Avoid circular dependencies.
1189 (unless (member file files)
1190 (with-temp-buffer
1191 (insert (org-file-contents file 'noerror))
1192 (org-mode)
1193 (org-export-get-inbuffer-options
1194 backend (cons file files))))))
1195 ((string= key "OPTIONS")
1196 (org-export-parse-option-keyword val backend))
1197 ((string= key "MACRO")
1198 (when (string-match
1199 "^\\([-a-zA-Z0-9_]+\\)\\(?:[ \t]+\\(.*?\\)[ \t]*$\\)?"
1200 val)
1201 (let ((key
1202 (intern
1203 (concat ":macro-"
1204 (downcase (match-string 1 val)))))
1205 (value (org-match-string-no-properties 2 val)))
1206 (cond
1207 ((not value) nil)
1208 ;; Value will be evaled: do not parse it.
1209 ((string-match "\\`(eval\\>" value)
1210 (list key (list value)))
1211 ;; Value has to be parsed for nested
1212 ;; macros.
1214 (list
1216 (let ((restr (org-element-restriction 'macro)))
1217 (org-element-parse-secondary-string
1218 ;; If user explicitly asks for
1219 ;; a newline, be sure to preserve it
1220 ;; from further filling with
1221 ;; `hard-newline'. Also replace
1222 ;; "\\n" with "\n", "\\\n" with "\\n"
1223 ;; and so on...
1224 (replace-regexp-in-string
1225 "\\(\\\\\\\\\\)n" "\\\\"
1226 (replace-regexp-in-string
1227 "\\(?:^\\|[^\\\\]\\)\\(\\\\n\\)"
1228 hard-newline value nil nil 1)
1229 nil nil 1)
1230 restr)))))))))))
1231 (setq plist (org-combine-plists plist prop)))))))
1232 ;; 2. Standard options, as in `org-export-options-alist'.
1233 (let* ((all (append org-export-options-alist
1234 ;; Also look for back-end specific options
1235 ;; if BACKEND is defined.
1236 (and backend
1237 (let ((var
1238 (intern
1239 (format "org-%s-options-alist" backend))))
1240 (and (boundp var) (eval var))))))
1241 ;; Build alist between keyword name and property name.
1242 (alist
1243 (delq nil (mapcar
1244 (lambda (e) (when (nth 1 e) (cons (nth 1 e) (car e))))
1245 all)))
1246 ;; Build regexp matching all keywords associated to export
1247 ;; options. Note: the search is case insensitive.
1248 (opt-re (org-make-options-regexp
1249 (delq nil (mapcar (lambda (e) (nth 1 e)) all)))))
1250 (goto-char (point-min))
1251 (while (re-search-forward opt-re nil t)
1252 (let ((element (org-element-at-point)))
1253 (when (eq (org-element-type element) 'keyword)
1254 (let* ((key (org-element-property :key element))
1255 (val (org-element-property :value element))
1256 (prop (cdr (assoc key alist)))
1257 (behaviour (nth 4 (assq prop all))))
1258 (setq plist
1259 (plist-put
1260 plist prop
1261 ;; Handle value depending on specified BEHAVIOUR.
1262 (case behaviour
1263 (space
1264 (if (not (plist-get plist prop)) (org-trim val)
1265 (concat (plist-get plist prop) " " (org-trim val))))
1266 (newline
1267 (org-trim
1268 (concat (plist-get plist prop) "\n" (org-trim val))))
1269 (split
1270 `(,@(plist-get plist prop) ,@(org-split-string val)))
1271 ('t val)
1272 (otherwise (if (not (plist-member plist prop)) val
1273 (plist-get plist prop))))))))))
1274 ;; Parse keywords specified in `org-element-parsed-keywords'.
1275 (mapc
1276 (lambda (key)
1277 (let* ((prop (cdr (assoc key alist)))
1278 (value (and prop (plist-get plist prop))))
1279 (when (stringp value)
1280 (setq plist
1281 (plist-put
1282 plist prop
1283 (org-element-parse-secondary-string
1284 value (org-element-restriction 'keyword)))))))
1285 org-element-parsed-keywords))
1286 ;; 3. Return final value.
1287 plist)))
1289 (defun org-export-get-buffer-attributes ()
1290 "Return properties related to buffer attributes, as a plist."
1291 (let ((visited-file (buffer-file-name (buffer-base-buffer))))
1292 (list
1293 ;; Store full path of input file name, or nil. For internal use.
1294 :input-file visited-file
1295 :title (or (and visited-file
1296 (file-name-sans-extension
1297 (file-name-nondirectory visited-file)))
1298 (buffer-name (buffer-base-buffer)))
1299 :footnote-definition-alist
1300 ;; Footnotes definitions must be collected in the original
1301 ;; buffer, as there's no insurance that they will still be in the
1302 ;; parse tree, due to possible narrowing.
1303 (let (alist)
1304 (org-with-wide-buffer
1305 (goto-char (point-min))
1306 (while (re-search-forward org-footnote-definition-re nil t)
1307 (let ((def (org-footnote-at-definition-p)))
1308 (when def
1309 (org-skip-whitespace)
1310 (push (cons (car def)
1311 (save-restriction
1312 (narrow-to-region (point) (nth 2 def))
1313 ;; Like `org-element-parse-buffer', but
1314 ;; makes sure the definition doesn't start
1315 ;; with a section element.
1316 (org-element-parse-elements
1317 (point-min) (point-max) nil nil nil nil
1318 (list 'org-data nil))))
1319 alist))))
1320 alist))
1321 :id-alist
1322 ;; Collect id references.
1323 (let (alist)
1324 (org-with-wide-buffer
1325 (goto-char (point-min))
1326 (while (re-search-forward
1327 "\\[\\[id:\\(\\S-+?\\)\\]\\(?:\\[.*?\\]\\)?\\]" nil t)
1328 (let* ((id (org-match-string-no-properties 1))
1329 (file (org-id-find-id-file id)))
1330 (when file (push (cons id (file-relative-name file)) alist)))))
1331 alist)
1332 :macro-modification-time
1333 (and visited-file
1334 (file-exists-p visited-file)
1335 (concat "(eval (format-time-string \"$1\" '"
1336 (prin1-to-string (nth 5 (file-attributes visited-file)))
1337 "))"))
1338 ;; Store input file name as a macro.
1339 :macro-input-file (and visited-file (file-name-nondirectory visited-file))
1340 ;; `:macro-date', `:macro-time' and `:macro-property' could as
1341 ;; well be initialized as tree properties, since they don't
1342 ;; depend on buffer properties. Though, it may be more logical
1343 ;; to keep them close to other ":macro-" properties.
1344 :macro-date "(eval (format-time-string \"$1\"))"
1345 :macro-time "(eval (format-time-string \"$1\"))"
1346 :macro-property "(eval (org-entry-get nil \"$1\" 'selective))")))
1348 (defun org-export-get-global-options (&optional backend)
1349 "Return global export options as a plist.
1351 Optional argument BACKEND, if non-nil, is a symbol specifying
1352 which back-end specific export options should also be read in the
1353 process."
1354 (let ((all (append org-export-options-alist
1355 (and backend
1356 (let ((var (intern
1357 (format "org-%s-options-alist" backend))))
1358 (and (boundp var) (symbol-value var))))))
1359 ;; Output value.
1360 plist)
1361 (mapc
1362 (lambda (cell)
1363 (setq plist
1364 (plist-put
1365 plist
1366 (car cell)
1367 ;; Eval default value provided. If keyword is a member
1368 ;; of `org-element-parsed-keywords', parse it as
1369 ;; a secondary string before storing it.
1370 (let ((value (eval (nth 3 cell))))
1371 (if (not (stringp value)) value
1372 (let ((keyword (nth 1 cell)))
1373 (if (not (member keyword org-element-parsed-keywords)) value
1374 (org-element-parse-secondary-string
1375 value (org-element-restriction 'keyword)))))))))
1376 all)
1377 ;; Return value.
1378 plist))
1380 (defvar org-export-allow-BIND-local nil)
1381 (defun org-export-confirm-letbind ()
1382 "Can we use #+BIND values during export?
1383 By default this will ask for confirmation by the user, to divert
1384 possible security risks."
1385 (cond
1386 ((not org-export-allow-BIND) nil)
1387 ((eq org-export-allow-BIND t) t)
1388 ((local-variable-p 'org-export-allow-BIND-local) org-export-allow-BIND-local)
1389 (t (org-set-local 'org-export-allow-BIND-local
1390 (yes-or-no-p "Allow BIND values in this buffer? ")))))
1392 (defun org-export-install-letbind-maybe ()
1393 "Install the values from #+BIND lines as local variables.
1394 Variables must be installed before in-buffer options are
1395 retrieved."
1396 (let (letbind pair)
1397 (org-with-wide-buffer
1398 (goto-char (point-min))
1399 (while (re-search-forward (org-make-options-regexp '("BIND")) nil t)
1400 (when (org-export-confirm-letbind)
1401 (push (read (concat "(" (org-match-string-no-properties 2) ")"))
1402 letbind))))
1403 (while (setq pair (pop letbind))
1404 (org-set-local (car pair) (nth 1 pair)))))
1407 ;;;; Tree Properties
1409 ;; Tree properties are infromation extracted from parse tree. They
1410 ;; are initialized at the beginning of the transcoding process by
1411 ;; `org-export-collect-tree-properties'.
1413 ;; Dedicated functions focus on computing the value of specific tree
1414 ;; properties during initialization. Thus,
1415 ;; `org-export-populate-ignore-list' lists elements and objects that
1416 ;; should be skipped during export, `org-export-get-min-level' gets
1417 ;; the minimal exportable level, used as a basis to compute relative
1418 ;; level for headlines. Eventually
1419 ;; `org-export-collect-headline-numbering' builds an alist between
1420 ;; headlines and their numbering.
1422 (defun org-export-collect-tree-properties (data info)
1423 "Extract tree properties from parse tree.
1425 DATA is the parse tree from which information is retrieved. INFO
1426 is a list holding export options.
1428 Following tree properties are set or updated:
1429 `:footnote-definition-alist' List of footnotes definitions in
1430 original buffer and current parse tree.
1432 `:headline-offset' Offset between true level of headlines and
1433 local level. An offset of -1 means an headline
1434 of level 2 should be considered as a level
1435 1 headline in the context.
1437 `:headline-numbering' Alist of all headlines as key an the
1438 associated numbering as value.
1440 `:ignore-list' List of elements that should be ignored during
1441 export.
1443 `:target-list' List of all targets in the parse tree.
1445 Return updated plist."
1446 ;; Install the parse tree in the communication channel, in order to
1447 ;; use `org-export-get-genealogy' and al.
1448 (setq info (plist-put info :parse-tree data))
1449 ;; Get the list of elements and objects to ignore, and put it into
1450 ;; `:ignore-list'. Do not overwrite any user ignore that might have
1451 ;; been done during parse tree filtering.
1452 (setq info
1453 (plist-put info
1454 :ignore-list
1455 (append (org-export-populate-ignore-list data info)
1456 (plist-get info :ignore-list))))
1457 ;; Compute `:headline-offset' in order to be able to use
1458 ;; `org-export-get-relative-level'.
1459 (setq info
1460 (plist-put info
1461 :headline-offset (- 1 (org-export-get-min-level data info))))
1462 ;; Update footnotes definitions list with definitions in parse tree.
1463 ;; This is required since buffer expansion might have modified
1464 ;; boundaries of footnote definitions contained in the parse tree.
1465 ;; This way, definitions in `footnote-definition-alist' are bound to
1466 ;; match those in the parse tree.
1467 (let ((defs (plist-get info :footnote-definition-alist)))
1468 (org-element-map
1469 data 'footnote-definition
1470 (lambda (fn)
1471 (push (cons (org-element-property :label fn)
1472 `(org-data nil ,@(org-element-contents fn)))
1473 defs)))
1474 (setq info (plist-put info :footnote-definition-alist defs)))
1475 ;; Properties order doesn't matter: get the rest of the tree
1476 ;; properties.
1477 (nconc
1478 `(:target-list
1479 ,(org-element-map
1480 data '(keyword target)
1481 (lambda (blob)
1482 (when (or (eq (org-element-type blob) 'target)
1483 (string= (org-element-property :key blob) "TARGET"))
1484 blob)) info)
1485 :headline-numbering ,(org-export-collect-headline-numbering data info))
1486 info))
1488 (defun org-export-get-min-level (data options)
1489 "Return minimum exportable headline's level in DATA.
1490 DATA is parsed tree as returned by `org-element-parse-buffer'.
1491 OPTIONS is a plist holding export options."
1492 (catch 'exit
1493 (let ((min-level 10000))
1494 (mapc
1495 (lambda (blob)
1496 (when (and (eq (org-element-type blob) 'headline)
1497 (not (member blob (plist-get options :ignore-list))))
1498 (setq min-level
1499 (min (org-element-property :level blob) min-level)))
1500 (when (= min-level 1) (throw 'exit 1)))
1501 (org-element-contents data))
1502 ;; If no headline was found, for the sake of consistency, set
1503 ;; minimum level to 1 nonetheless.
1504 (if (= min-level 10000) 1 min-level))))
1506 (defun org-export-collect-headline-numbering (data options)
1507 "Return numbering of all exportable headlines in a parse tree.
1509 DATA is the parse tree. OPTIONS is the plist holding export
1510 options.
1512 Return an alist whose key is an headline and value is its
1513 associated numbering \(in the shape of a list of numbers\)."
1514 (let ((numbering (make-vector org-export-max-depth 0)))
1515 (org-element-map
1516 data
1517 'headline
1518 (lambda (headline)
1519 (let ((relative-level
1520 (1- (org-export-get-relative-level headline options))))
1521 (cons
1522 headline
1523 (loop for n across numbering
1524 for idx from 0 to org-export-max-depth
1525 when (< idx relative-level) collect n
1526 when (= idx relative-level) collect (aset numbering idx (1+ n))
1527 when (> idx relative-level) do (aset numbering idx 0)))))
1528 options)))
1530 (defun org-export-populate-ignore-list (data options)
1531 "Return list of elements and objects to ignore during export.
1532 DATA is the parse tree to traverse. OPTIONS is the plist holding
1533 export options."
1534 (let* (ignore
1535 walk-data ; for byte-compiler.
1536 (walk-data
1537 (function
1538 (lambda (data options selected)
1539 ;; Collect ignored elements or objects into IGNORE-LIST.
1540 (mapc
1541 (lambda (el)
1542 (if (org-export--skip-p el options selected) (push el ignore)
1543 (let ((type (org-element-type el)))
1544 (if (and (eq (plist-get options :with-archived-trees)
1545 'headline)
1546 (eq (org-element-type el) 'headline)
1547 (org-element-property :archivedp el))
1548 ;; If headline is archived but tree below has
1549 ;; to be skipped, add it to ignore list.
1550 (mapc (lambda (e) (push e ignore))
1551 (org-element-contents el))
1552 ;; Move into recursive objects/elements.
1553 (when (org-element-contents el)
1554 (funcall walk-data el options selected))))))
1555 (org-element-contents data))))))
1556 ;; Main call. First find trees containing a select tag, if any.
1557 (funcall walk-data data options (org-export--selected-trees data options))
1558 ;; Return value.
1559 ignore))
1561 (defun org-export--selected-trees (data info)
1562 "Return list of headlines containing a select tag in their tree.
1563 DATA is parsed data as returned by `org-element-parse-buffer'.
1564 INFO is a plist holding export options."
1565 (let* (selected-trees
1566 walk-data ; for byte-compiler.
1567 (walk-data
1568 (function
1569 (lambda (data genealogy)
1570 (case (org-element-type data)
1571 (org-data (mapc (lambda (el) (funcall walk-data el genealogy))
1572 (org-element-contents data)))
1573 (headline
1574 (let ((tags (org-element-property :tags data)))
1575 (if (loop for tag in (plist-get info :select-tags)
1576 thereis (member tag tags))
1577 ;; When a select tag is found, mark full
1578 ;; genealogy and every headline within the tree
1579 ;; as acceptable.
1580 (setq selected-trees
1581 (append
1582 genealogy
1583 (org-element-map data 'headline 'identity)
1584 selected-trees))
1585 ;; Else, continue searching in tree, recursively.
1586 (mapc
1587 (lambda (el) (funcall walk-data el (cons data genealogy)))
1588 (org-element-contents data))))))))))
1589 (funcall walk-data data nil) selected-trees))
1591 (defun org-export--skip-p (blob options selected)
1592 "Non-nil when element or object BLOB should be skipped during export.
1593 OPTIONS is the plist holding export options. SELECTED, when
1594 non-nil, is a list of headlines belonging to a tree with a select
1595 tag."
1596 (case (org-element-type blob)
1597 ;; Check headline.
1598 (headline
1599 (let ((with-tasks (plist-get options :with-tasks))
1600 (todo (org-element-property :todo-keyword blob))
1601 (todo-type (org-element-property :todo-type blob))
1602 (archived (plist-get options :with-archived-trees))
1603 (tags (org-element-property :tags blob)))
1605 ;; Ignore subtrees with an exclude tag.
1606 (loop for k in (plist-get options :exclude-tags)
1607 thereis (member k tags))
1608 ;; When a select tag is present in the buffer, ignore any tree
1609 ;; without it.
1610 (and selected (not (member blob selected)))
1611 ;; Ignore commented sub-trees.
1612 (org-element-property :commentedp blob)
1613 ;; Ignore archived subtrees if `:with-archived-trees' is nil.
1614 (and (not archived) (org-element-property :archivedp blob))
1615 ;; Ignore tasks, if specified by `:with-tasks' property.
1616 (and todo
1617 (or (not with-tasks)
1618 (and (memq with-tasks '(todo done))
1619 (not (eq todo-type with-tasks)))
1620 (and (consp with-tasks) (not (member todo with-tasks))))))))
1621 ;; Check timestamp.
1622 (timestamp
1623 (case (plist-get options :with-timestamps)
1624 ;; No timestamp allowed.
1625 ('nil t)
1626 ;; Only active timestamps allowed and the current one isn't
1627 ;; active.
1628 (active
1629 (not (memq (org-element-property :type blob)
1630 '(active active-range))))
1631 ;; Only inactive timestamps allowed and the current one isn't
1632 ;; inactive.
1633 (inactive
1634 (not (memq (org-element-property :type blob)
1635 '(inactive inactive-range))))))
1636 ;; Check drawer.
1637 (drawer
1638 (or (not (plist-get options :with-drawers))
1639 (and (consp (plist-get options :with-drawers))
1640 (not (member (org-element-property :drawer-name blob)
1641 (plist-get options :with-drawers))))))
1642 ;; Check table-row.
1643 (table-row (org-export-table-row-is-special-p blob options))
1644 ;; Check table-cell.
1645 (table-cell
1646 (and (org-export-table-has-special-column-p
1647 (org-export-get-parent-table blob))
1648 (not (org-export-get-previous-element blob))))
1649 ;; Check clock.
1650 (clock (not (plist-get options :with-clocks)))
1651 ;; Check planning.
1652 (planning (not (plist-get options :with-plannings)))))
1656 ;;; The Transcoder
1658 ;; `org-export-data' reads a parse tree (obtained with, i.e.
1659 ;; `org-element-parse-buffer') and transcodes it into a specified
1660 ;; back-end output. It takes care of filtering out elements or
1661 ;; objects according to export options and organizing the output blank
1662 ;; lines and white space are preserved.
1664 ;; Internally, three functions handle the filtering of objects and
1665 ;; elements during the export. In particular,
1666 ;; `org-export-ignore-element' marks an element or object so future
1667 ;; parse tree traversals skip it, `org-export-interpret-p' tells which
1668 ;; elements or objects should be seen as real Org syntax and
1669 ;; `org-export-expand' transforms the others back into their original
1670 ;; shape
1672 ;; `org-export-transcoder' is an accessor returning appropriate
1673 ;; translator function for a given element or object.
1675 (defun org-export-transcoder (blob info)
1676 "Return appropriate transcoder for BLOB.
1677 INFO is a plist containing export directives."
1678 (let ((type (org-element-type blob)))
1679 ;; Return contents only for complete parse trees.
1680 (if (eq type 'org-data) (lambda (blob contents info) contents)
1681 (let ((transcoder (cdr (assq type (plist-get info :translate-alist)))))
1682 (and (fboundp transcoder) transcoder)))))
1684 (defun org-export-data (data info)
1685 "Convert DATA into current back-end format.
1687 DATA is a parse tree, an element or an object or a secondary
1688 string. INFO is a plist holding export options.
1690 Return transcoded string."
1691 (let* ((type (org-element-type data))
1692 (results
1693 (cond
1694 ;; Ignored element/object.
1695 ((member data (plist-get info :ignore-list)) nil)
1696 ;; Plain text.
1697 ((eq type 'plain-text)
1698 (org-export-filter-apply-functions
1699 (plist-get info :filter-plain-text)
1700 (let ((transcoder (org-export-transcoder data info)))
1701 (if transcoder (funcall transcoder data info) data))
1702 info))
1703 ;; Uninterpreted element/object: change it back to Org
1704 ;; syntax and export again resulting raw string.
1705 ((not (org-export-interpret-p data info))
1706 (org-export-data
1707 (org-export-expand
1708 data
1709 (mapconcat (lambda (blob) (org-export-data blob info))
1710 (org-element-contents data)
1711 ""))
1712 info))
1713 ;; Secondary string.
1714 ((not type)
1715 (mapconcat (lambda (obj) (org-export-data obj info)) data ""))
1716 ;; Element/Object without contents or, as a special case,
1717 ;; headline with archive tag and archived trees restricted
1718 ;; to title only.
1719 ((or (not (org-element-contents data))
1720 (and (eq type 'headline)
1721 (eq (plist-get info :with-archived-trees) 'headline)
1722 (org-element-property :archivedp data)))
1723 (let ((transcoder (org-export-transcoder data info)))
1724 (and (fboundp transcoder) (funcall transcoder data nil info))))
1725 ;; Element/Object with contents.
1727 (let ((transcoder (org-export-transcoder data info)))
1728 (when transcoder
1729 (let* ((greaterp (memq type org-element-greater-elements))
1730 (objectp (and (not greaterp)
1731 (memq type org-element-recursive-objects)))
1732 (contents
1733 (mapconcat
1734 (lambda (element) (org-export-data element info))
1735 (org-element-contents
1736 (if (or greaterp objectp) data
1737 ;; Elements directly containing objects
1738 ;; must have their indentation normalized
1739 ;; first.
1740 (org-element-normalize-contents
1741 data
1742 ;; When normalizing contents of the first
1743 ;; paragraph in an item or a footnote
1744 ;; definition, ignore first line's
1745 ;; indentation: there is none and it
1746 ;; might be misleading.
1747 (when (eq type 'paragraph)
1748 (let ((parent (org-export-get-parent data)))
1749 (and (equal (car (org-element-contents parent))
1750 data)
1751 (memq (org-element-type parent)
1752 '(footnote-definition item))))))))
1753 "")))
1754 (funcall transcoder data
1755 (if greaterp (org-element-normalize-string contents)
1756 contents)
1757 info))))))))
1758 (cond
1759 ((not results) nil)
1760 ((memq type '(org-data plain-text nil)) results)
1761 ;; Append the same white space between elements or objects as in
1762 ;; the original buffer, and call appropriate filters.
1764 (let ((results
1765 (org-export-filter-apply-functions
1766 (plist-get info (intern (format ":filter-%s" type)))
1767 (let ((post-blank (org-element-property :post-blank data)))
1768 (if (memq type org-element-all-elements)
1769 (concat (org-element-normalize-string results)
1770 (make-string post-blank ?\n))
1771 (concat results (make-string post-blank ? ))))
1772 info)))
1773 ;; Eventually return string.
1774 results)))))
1776 (defun org-export-interpret-p (blob info)
1777 "Non-nil if element or object BLOB should be interpreted as Org syntax.
1778 Check is done according to export options INFO, stored as
1779 a plist."
1780 (case (org-element-type blob)
1781 ;; ... entities...
1782 (entity (plist-get info :with-entities))
1783 ;; ... emphasis...
1784 (emphasis (plist-get info :with-emphasize))
1785 ;; ... fixed-width areas.
1786 (fixed-width (plist-get info :with-fixed-width))
1787 ;; ... footnotes...
1788 ((footnote-definition footnote-reference)
1789 (plist-get info :with-footnotes))
1790 ;; ... sub/superscripts...
1791 ((subscript superscript)
1792 (let ((sub/super-p (plist-get info :with-sub-superscript)))
1793 (if (eq sub/super-p '{})
1794 (org-element-property :use-brackets-p blob)
1795 sub/super-p)))
1796 ;; ... tables...
1797 (table (plist-get info :with-tables))
1798 (otherwise t)))
1800 (defun org-export-expand (blob contents)
1801 "Expand a parsed element or object to its original state.
1802 BLOB is either an element or an object. CONTENTS is its
1803 contents, as a string or nil."
1804 (funcall
1805 (intern (format "org-element-%s-interpreter" (org-element-type blob)))
1806 blob contents))
1808 (defun org-export-ignore-element (element info)
1809 "Add ELEMENT to `:ignore-list' in INFO.
1811 Any element in `:ignore-list' will be skipped when using
1812 `org-element-map'. INFO is modified by side effects."
1813 (plist-put info :ignore-list (cons element (plist-get info :ignore-list))))
1817 ;;; The Filter System
1819 ;; Filters allow end-users to tweak easily the transcoded output.
1820 ;; They are the functional counterpart of hooks, as every filter in
1821 ;; a set is applied to the return value of the previous one.
1823 ;; Every set is back-end agnostic. Although, a filter is always
1824 ;; called, in addition to the string it applies to, with the back-end
1825 ;; used as argument, so it's easy for the end-user to add back-end
1826 ;; specific filters in the set. The communication channel, as
1827 ;; a plist, is required as the third argument.
1829 ;; From the developer side, filters sets can be installed in the
1830 ;; process with the help of `org-BACKEND-filters-alist' variable.
1831 ;; Each association has a key among the following symbols and
1832 ;; a function or a list of functions as value.
1834 ;; - `:filter-parse-tree' applies directly on the complete parsed
1835 ;; tree. It's the only filters set that doesn't apply to a string.
1836 ;; Users can set it through `org-export-filter-parse-tree-functions'
1837 ;; variable.
1839 ;; - `:filter-final-output' applies to the final transcoded string.
1840 ;; Users can set it with `org-export-filter-final-output-functions'
1841 ;; variable
1843 ;; - `:filter-plain-text' applies to any string not recognized as Org
1844 ;; syntax. `org-export-filter-plain-text-functions' allows users to
1845 ;; configure it.
1847 ;; - `:filter-TYPE' applies on the string returned after an element or
1848 ;; object of type TYPE has been transcoded. An user can modify
1849 ;; `org-export-filter-TYPE-functions'
1851 ;; All filters sets are applied with
1852 ;; `org-export-filter-apply-functions' function. Filters in a set are
1853 ;; applied in a LIFO fashion. It allows developers to be sure that
1854 ;; their filters will be applied first.
1856 ;; Filters properties are installed in communication channel with
1857 ;; `org-export-install-filters' function.
1859 ;; Eventually, a hook (`org-export-before-parsing-hook') is run just
1860 ;; before parsing to allow for heavy structure modifications.
1863 ;;;; Before Parsing Hook
1865 (defvar org-export-before-parsing-hook nil
1866 "Hook run before parsing an export buffer.
1867 This is run after include keywords have been expanded and Babel
1868 code executed, on a copy of original buffer's area being
1869 exported. Visibility is the same as in the original one. Point
1870 is left at the beginning of the new one.")
1873 ;;;; Special Filters
1875 (defvar org-export-filter-parse-tree-functions nil
1876 "List of functions applied to the parsed tree.
1877 Each filter is called with three arguments: the parse tree, as
1878 returned by `org-element-parse-buffer', the back-end, as
1879 a symbol, and the communication channel, as a plist. It must
1880 return the modified parse tree to transcode.")
1882 (defvar org-export-filter-final-output-functions nil
1883 "List of functions applied to the transcoded string.
1884 Each filter is called with three arguments: the full transcoded
1885 string, the back-end, as a symbol, and the communication channel,
1886 as a plist. It must return a string that will be used as the
1887 final export output.")
1889 (defvar org-export-filter-plain-text-functions nil
1890 "List of functions applied to plain text.
1891 Each filter is called with three arguments: a string which
1892 contains no Org syntax, the back-end, as a symbol, and the
1893 communication channel, as a plist. It must return a string or
1894 nil.")
1897 ;;;; Elements Filters
1899 (defvar org-export-filter-center-block-functions nil
1900 "List of functions applied to a transcoded center block.
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-clock-functions nil
1906 "List of functions applied to a transcoded clock.
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-drawer-functions nil
1912 "List of functions applied to a transcoded drawer.
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-dynamic-block-functions nil
1918 "List of functions applied to a transcoded dynamic-block.
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-headline-functions nil
1924 "List of functions applied to a transcoded headline.
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-inlinetask-functions nil
1930 "List of functions applied to a transcoded inlinetask.
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-plain-list-functions nil
1936 "List of functions applied to a transcoded plain-list.
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.")
1941 (defvar org-export-filter-item-functions nil
1942 "List of functions applied to a transcoded item.
1943 Each filter is called with three arguments: the transcoded data,
1944 as a string, the back-end, as a symbol, and the communication
1945 channel, as a plist. It must return a string or nil.")
1947 (defvar org-export-filter-comment-functions nil
1948 "List of functions applied to a transcoded comment.
1949 Each filter is called with three arguments: the transcoded data,
1950 as a string, the back-end, as a symbol, and the communication
1951 channel, as a plist. It must return a string or nil.")
1953 (defvar org-export-filter-comment-block-functions nil
1954 "List of functions applied to a transcoded comment-comment.
1955 Each filter is called with three arguments: the transcoded data,
1956 as a string, the back-end, as a symbol, and the communication
1957 channel, as a plist. It must return a string or nil.")
1959 (defvar org-export-filter-example-block-functions nil
1960 "List of functions applied to a transcoded example-block.
1961 Each filter is called with three arguments: the transcoded data,
1962 as a string, the back-end, as a symbol, and the communication
1963 channel, as a plist. It must return a string or nil.")
1965 (defvar org-export-filter-export-block-functions nil
1966 "List of functions applied to a transcoded export-block.
1967 Each filter is called with three arguments: the transcoded data,
1968 as a string, the back-end, as a symbol, and the communication
1969 channel, as a plist. It must return a string or nil.")
1971 (defvar org-export-filter-fixed-width-functions nil
1972 "List of functions applied to a transcoded fixed-width.
1973 Each filter is called with three arguments: the transcoded data,
1974 as a string, the back-end, as a symbol, and the communication
1975 channel, as a plist. It must return a string or nil.")
1977 (defvar org-export-filter-footnote-definition-functions nil
1978 "List of functions applied to a transcoded footnote-definition.
1979 Each filter is called with three arguments: the transcoded data,
1980 as a string, the back-end, as a symbol, and the communication
1981 channel, as a plist. It must return a string or nil.")
1983 (defvar org-export-filter-horizontal-rule-functions nil
1984 "List of functions applied to a transcoded horizontal-rule.
1985 Each filter is called with three arguments: the transcoded data,
1986 as a string, the back-end, as a symbol, and the communication
1987 channel, as a plist. It must return a string or nil.")
1989 (defvar org-export-filter-keyword-functions nil
1990 "List of functions applied to a transcoded keyword.
1991 Each filter is called with three arguments: the transcoded data,
1992 as a string, the back-end, as a symbol, and the communication
1993 channel, as a plist. It must return a string or nil.")
1995 (defvar org-export-filter-latex-environment-functions nil
1996 "List of functions applied to a transcoded latex-environment.
1997 Each filter is called with three arguments: the transcoded data,
1998 as a string, the back-end, as a symbol, and the communication
1999 channel, as a plist. It must return a string or nil.")
2001 (defvar org-export-filter-babel-call-functions nil
2002 "List of functions applied to a transcoded babel-call.
2003 Each filter is called with three arguments: the transcoded data,
2004 as a string, the back-end, as a symbol, and the communication
2005 channel, as a plist. It must return a string or nil.")
2007 (defvar org-export-filter-paragraph-functions nil
2008 "List of functions applied to a transcoded paragraph.
2009 Each filter is called with three arguments: the transcoded data,
2010 as a string, the back-end, as a symbol, and the communication
2011 channel, as a plist. It must return a string or nil.")
2013 (defvar org-export-filter-planning-functions nil
2014 "List of functions applied to a transcoded planning.
2015 Each filter is called with three arguments: the transcoded data,
2016 as a string, the back-end, as a symbol, and the communication
2017 channel, as a plist. It must return a string or nil.")
2019 (defvar org-export-filter-property-drawer-functions nil
2020 "List of functions applied to a transcoded property-drawer.
2021 Each filter is called with three arguments: the transcoded data,
2022 as a string, the back-end, as a symbol, and the communication
2023 channel, as a plist. It must return a string or nil.")
2025 (defvar org-export-filter-quote-block-functions nil
2026 "List of functions applied to a transcoded quote block.
2027 Each filter is called with three arguments: the transcoded quote
2028 data, as a string, the back-end, as a symbol, and the
2029 communication channel, as a plist. It must return a string or
2030 nil.")
2032 (defvar org-export-filter-quote-section-functions nil
2033 "List of functions applied to a transcoded quote-section.
2034 Each filter is called with three arguments: the transcoded data,
2035 as a string, the back-end, as a symbol, and the communication
2036 channel, as a plist. It must return a string or nil.")
2038 (defvar org-export-filter-section-functions nil
2039 "List of functions applied to a transcoded section.
2040 Each filter is called with three arguments: the transcoded data,
2041 as a string, the back-end, as a symbol, and the communication
2042 channel, as a plist. It must return a string or nil.")
2044 (defvar org-export-filter-special-block-functions nil
2045 "List of functions applied to a transcoded special block.
2046 Each filter is called with three arguments: the transcoded data,
2047 as a string, the back-end, as a symbol, and the communication
2048 channel, as a plist. It must return a string or nil.")
2050 (defvar org-export-filter-src-block-functions nil
2051 "List of functions applied to a transcoded src-block.
2052 Each filter is called with three arguments: the transcoded data,
2053 as a string, the back-end, as a symbol, and the communication
2054 channel, as a plist. It must return a string or nil.")
2056 (defvar org-export-filter-table-functions nil
2057 "List of functions applied to a transcoded table.
2058 Each filter is called with three arguments: the transcoded data,
2059 as a string, the back-end, as a symbol, and the communication
2060 channel, as a plist. It must return a string or nil.")
2062 (defvar org-export-filter-table-cell-functions nil
2063 "List of functions applied to a transcoded table-cell.
2064 Each filter is called with three arguments: the transcoded data,
2065 as a string, the back-end, as a symbol, and the communication
2066 channel, as a plist. It must return a string or nil.")
2068 (defvar org-export-filter-table-row-functions nil
2069 "List of functions applied to a transcoded table-row.
2070 Each filter is called with three arguments: the transcoded data,
2071 as a string, the back-end, as a symbol, and the communication
2072 channel, as a plist. It must return a string or nil.")
2074 (defvar org-export-filter-verse-block-functions nil
2075 "List of functions applied to a transcoded verse block.
2076 Each filter is called with three arguments: the transcoded data,
2077 as a string, the back-end, as a symbol, and the communication
2078 channel, as a plist. It must return a string or nil.")
2081 ;;;; Objects Filters
2083 (defvar org-export-filter-bold-functions nil
2084 "List of functions applied to transcoded bold text.
2085 Each filter is called with three arguments: the transcoded data,
2086 as a string, the back-end, as a symbol, and the communication
2087 channel, as a plist. It must return a string or nil.")
2089 (defvar org-export-filter-code-functions nil
2090 "List of functions applied to transcoded code text.
2091 Each filter is called with three arguments: the transcoded data,
2092 as a string, the back-end, as a symbol, and the communication
2093 channel, as a plist. It must return a string or nil.")
2095 (defvar org-export-filter-entity-functions nil
2096 "List of functions applied to a transcoded entity.
2097 Each filter is called with three arguments: the transcoded data,
2098 as a string, the back-end, as a symbol, and the communication
2099 channel, as a plist. It must return a string or nil.")
2101 (defvar org-export-filter-export-snippet-functions nil
2102 "List of functions applied to a transcoded export-snippet.
2103 Each filter is called with three arguments: the transcoded data,
2104 as a string, the back-end, as a symbol, and the communication
2105 channel, as a plist. It must return a string or nil.")
2107 (defvar org-export-filter-footnote-reference-functions nil
2108 "List of functions applied to a transcoded footnote-reference.
2109 Each filter is called with three arguments: the transcoded data,
2110 as a string, the back-end, as a symbol, and the communication
2111 channel, as a plist. It must return a string or nil.")
2113 (defvar org-export-filter-inline-babel-call-functions nil
2114 "List of functions applied to a transcoded inline-babel-call.
2115 Each filter is called with three arguments: the transcoded data,
2116 as a string, the back-end, as a symbol, and the communication
2117 channel, as a plist. It must return a string or nil.")
2119 (defvar org-export-filter-inline-src-block-functions nil
2120 "List of functions applied to a transcoded inline-src-block.
2121 Each filter is called with three arguments: the transcoded data,
2122 as a string, the back-end, as a symbol, and the communication
2123 channel, as a plist. It must return a string or nil.")
2125 (defvar org-export-filter-italic-functions nil
2126 "List of functions applied to transcoded italic text.
2127 Each filter is called with three arguments: the transcoded data,
2128 as a string, the back-end, as a symbol, and the communication
2129 channel, as a plist. It must return a string or nil.")
2131 (defvar org-export-filter-latex-fragment-functions nil
2132 "List of functions applied to a transcoded latex-fragment.
2133 Each filter is called with three arguments: the transcoded data,
2134 as a string, the back-end, as a symbol, and the communication
2135 channel, as a plist. It must return a string or nil.")
2137 (defvar org-export-filter-line-break-functions nil
2138 "List of functions applied to a transcoded line-break.
2139 Each filter is called with three arguments: the transcoded data,
2140 as a string, the back-end, as a symbol, and the communication
2141 channel, as a plist. It must return a string or nil.")
2143 (defvar org-export-filter-link-functions nil
2144 "List of functions applied to a transcoded link.
2145 Each filter is called with three arguments: the transcoded data,
2146 as a string, the back-end, as a symbol, and the communication
2147 channel, as a plist. It must return a string or nil.")
2149 (defvar org-export-filter-macro-functions nil
2150 "List of functions applied to a transcoded macro.
2151 Each filter is called with three arguments: the transcoded data,
2152 as a string, the back-end, as a symbol, and the communication
2153 channel, as a plist. It must return a string or nil.")
2155 (defvar org-export-filter-radio-target-functions nil
2156 "List of functions applied to a transcoded radio-target.
2157 Each filter is called with three arguments: the transcoded data,
2158 as a string, the back-end, as a symbol, and the communication
2159 channel, as a plist. It must return a string or nil.")
2161 (defvar org-export-filter-statistics-cookie-functions nil
2162 "List of functions applied to a transcoded statistics-cookie.
2163 Each filter is called with three arguments: the transcoded data,
2164 as a string, the back-end, as a symbol, and the communication
2165 channel, as a plist. It must return a string or nil.")
2167 (defvar org-export-filter-strike-through-functions nil
2168 "List of functions applied to transcoded strike-through text.
2169 Each filter is called with three arguments: the transcoded data,
2170 as a string, the back-end, as a symbol, and the communication
2171 channel, as a plist. It must return a string or nil.")
2173 (defvar org-export-filter-subscript-functions nil
2174 "List of functions applied to a transcoded subscript.
2175 Each filter is called with three arguments: the transcoded data,
2176 as a string, the back-end, as a symbol, and the communication
2177 channel, as a plist. It must return a string or nil.")
2179 (defvar org-export-filter-superscript-functions nil
2180 "List of functions applied to a transcoded superscript.
2181 Each filter is called with three arguments: the transcoded data,
2182 as a string, the back-end, as a symbol, and the communication
2183 channel, as a plist. It must return a string or nil.")
2185 (defvar org-export-filter-target-functions nil
2186 "List of functions applied to a transcoded target.
2187 Each filter is called with three arguments: the transcoded data,
2188 as a string, the back-end, as a symbol, and the communication
2189 channel, as a plist. It must return a string or nil.")
2191 (defvar org-export-filter-timestamp-functions nil
2192 "List of functions applied to a transcoded timestamp.
2193 Each filter is called with three arguments: the transcoded data,
2194 as a string, the back-end, as a symbol, and the communication
2195 channel, as a plist. It must return a string or nil.")
2197 (defvar org-export-filter-underline-functions nil
2198 "List of functions applied to transcoded underline text.
2199 Each filter is called with three arguments: the transcoded data,
2200 as a string, the back-end, as a symbol, and the communication
2201 channel, as a plist. It must return a string or nil.")
2203 (defvar org-export-filter-verbatim-functions nil
2204 "List of functions applied to transcoded verbatim text.
2205 Each filter is called with three arguments: the transcoded data,
2206 as a string, the back-end, as a symbol, and the communication
2207 channel, as a plist. It must return a string or nil.")
2210 ;;;; Filters Tools
2212 ;; Internal function `org-export-install-filters' installs filters
2213 ;; hard-coded in back-ends (developer filters) and filters from global
2214 ;; variables (user filters) in the communication channel.
2216 ;; Internal function `org-export-filter-apply-functions' takes care
2217 ;; about applying each filter in order to a given data. It stops
2218 ;; whenever a filter returns a nil value.
2220 ;; User-oriented function `org-export-set-element' replaces one
2221 ;; element or object in the parse tree with another one. It is meant
2222 ;; to be used as a tool for parse tree filters.
2224 (defun org-export-filter-apply-functions (filters value info)
2225 "Call every function in FILTERS.
2226 Functions are called with arguments VALUE, current export
2227 back-end and INFO. Call is done in a LIFO fashion, to be sure
2228 that developer specified filters, if any, are called first."
2229 (loop for filter in filters
2230 if (not value) return nil else
2231 do (setq value (funcall filter value (plist-get info :back-end) info)))
2232 value)
2234 (defun org-export-install-filters (info)
2235 "Install filters properties in communication channel.
2237 INFO is a plist containing the current communication channel.
2239 Return the updated communication channel."
2240 (let (plist)
2241 ;; Install user defined filters with `org-export-filters-alist'.
2242 (mapc (lambda (p)
2243 (setq plist (plist-put plist (car p) (eval (cdr p)))))
2244 org-export-filters-alist)
2245 ;; Prepend back-end specific filters to that list.
2246 (let ((back-end-filters (intern (format "org-%s-filters-alist"
2247 (plist-get info :back-end)))))
2248 (when (boundp back-end-filters)
2249 (mapc (lambda (p)
2250 ;; Single values get consed, lists are prepended.
2251 (let ((key (car p)) (value (cdr p)))
2252 (when value
2253 (setq plist
2254 (plist-put
2255 plist key
2256 (if (atom value) (cons value (plist-get plist key))
2257 (append value (plist-get plist key))))))))
2258 (eval back-end-filters))))
2259 ;; Return new communication channel.
2260 (org-combine-plists info plist)))
2262 (defun org-export-set-element (old new)
2263 "Replace element or object OLD with element or object NEW.
2264 The function takes care of setting `:parent' property for NEW."
2265 ;; OLD can belong to the contents of PARENT or to its secondary
2266 ;; string.
2267 (let* ((parent (org-element-property :parent old))
2268 (sec-loc (cdr (assq (org-element-type parent)
2269 org-element-secondary-value-alist)))
2270 (sec-value (and sec-loc (org-element-property sec-loc parent)))
2271 (place (or (member old sec-value) (member old parent))))
2272 ;; Ensure NEW has correct parent. Then replace OLD with NEW.
2273 (let ((props (nth 1 new)))
2274 (if props (plist-put props :parent parent)
2275 (setcar (cdr new) `(:parent ,parent))))
2276 (setcar place new)))
2280 ;;; Core functions
2282 ;; This is the room for the main function, `org-export-as', along with
2283 ;; its derivatives, `org-export-to-buffer' and `org-export-to-file'.
2284 ;; They differ only by the way they output the resulting code.
2286 ;; `org-export-output-file-name' is an auxiliary function meant to be
2287 ;; used with `org-export-to-file'. With a given extension, it tries
2288 ;; to provide a canonical file name to write export output to.
2290 ;; Note that `org-export-as' doesn't really parse the current buffer,
2291 ;; but a copy of it (with the same buffer-local variables and
2292 ;; visibility), where include keywords are expanded and Babel blocks
2293 ;; are executed, if appropriate.
2294 ;; `org-export-with-current-buffer-copy' macro prepares that copy.
2296 ;; File inclusion is taken care of by
2297 ;; `org-export-expand-include-keyword' and
2298 ;; `org-export-prepare-file-contents'. Structure wise, including
2299 ;; a whole Org file in a buffer often makes little sense. For
2300 ;; example, if the file contains an headline and the include keyword
2301 ;; was within an item, the item should contain the headline. That's
2302 ;; why file inclusion should be done before any structure can be
2303 ;; associated to the file, that is before parsing.
2305 (defvar org-current-export-file) ; Dynamically scoped
2306 (defvar org-export-current-backend) ; Dynamically scoped
2307 (defun org-export-as
2308 (backend &optional subtreep visible-only body-only ext-plist noexpand)
2309 "Transcode current Org buffer into BACKEND code.
2311 If narrowing is active in the current buffer, only transcode its
2312 narrowed part.
2314 If a region is active, transcode that region.
2316 When optional argument SUBTREEP is non-nil, transcode the
2317 sub-tree at point, extracting information from the headline
2318 properties first.
2320 When optional argument VISIBLE-ONLY is non-nil, don't export
2321 contents of hidden elements.
2323 When optional argument BODY-ONLY is non-nil, only return body
2324 code, without preamble nor postamble.
2326 Optional argument EXT-PLIST, when provided, is a property list
2327 with external parameters overriding Org default settings, but
2328 still inferior to file-local settings.
2330 Optional argument NOEXPAND, when non-nil, prevents included files
2331 to be expanded and Babel code to be executed.
2333 Return code as a string."
2334 (save-excursion
2335 (save-restriction
2336 ;; Narrow buffer to an appropriate region or subtree for
2337 ;; parsing. If parsing subtree, be sure to remove main headline
2338 ;; too.
2339 (cond ((org-region-active-p)
2340 (narrow-to-region (region-beginning) (region-end)))
2341 (subtreep
2342 (org-narrow-to-subtree)
2343 (goto-char (point-min))
2344 (forward-line)
2345 (narrow-to-region (point) (point-max))))
2346 ;; 1. Get export environment from original buffer. Also install
2347 ;; user's and developer's filters.
2348 (let ((info (org-export-install-filters
2349 (org-export-get-environment backend subtreep ext-plist)))
2350 ;; 2. Get parse tree. Buffer isn't parsed directly.
2351 ;; Instead, a temporary copy is created, where include
2352 ;; keywords are expanded and code blocks are evaluated.
2353 (tree (let ((buf (or (buffer-file-name (buffer-base-buffer))
2354 (current-buffer))))
2355 (org-export-with-current-buffer-copy
2356 (unless noexpand
2357 (org-export-expand-include-keyword)
2358 ;; Setting `org-current-export-file' is
2359 ;; required by Org Babel to properly resolve
2360 ;; noweb references.
2361 (let ((org-current-export-file buf))
2362 (org-export-blocks-preprocess)))
2363 (goto-char (point-min))
2364 ;; Run hook with `org-export-current-backend' set
2365 ;; to BACKEND.
2366 (let ((org-export-current-backend backend))
2367 (run-hooks 'org-export-before-parsing-hook))
2368 ;; Eventually parse buffer.
2369 (org-element-parse-buffer nil visible-only)))))
2370 ;; 3. Call parse-tree filters to get the final tree.
2371 (setq tree
2372 (org-export-filter-apply-functions
2373 (plist-get info :filter-parse-tree) tree info))
2374 ;; 4. Now tree is complete, compute its properties and add
2375 ;; them to communication channel.
2376 (setq info
2377 (org-combine-plists
2378 info (org-export-collect-tree-properties tree info)))
2379 ;; 5. Eventually transcode TREE. Wrap the resulting string
2380 ;; into a template, if required. Eventually call
2381 ;; final-output filter.
2382 (let* ((body (org-element-normalize-string (org-export-data tree info)))
2383 (template (cdr (assq 'template
2384 (plist-get info :translate-alist))))
2385 (output (org-export-filter-apply-functions
2386 (plist-get info :filter-final-output)
2387 (if (or (not (fboundp template)) body-only) body
2388 (funcall template body info))
2389 info)))
2390 ;; Maybe add final OUTPUT to kill ring, then return it.
2391 (when org-export-copy-to-kill-ring (org-kill-new output))
2392 output)))))
2394 (defun org-export-to-buffer
2395 (backend buffer &optional subtreep visible-only body-only ext-plist noexpand)
2396 "Call `org-export-as' with output to a specified buffer.
2398 BACKEND is the back-end used for transcoding, as a symbol.
2400 BUFFER is the output buffer. If it already exists, it will be
2401 erased first, otherwise, it will be created.
2403 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY, EXT-PLIST
2404 and NOEXPAND are similar to those used in `org-export-as', which
2405 see.
2407 Return buffer."
2408 (let ((out (org-export-as
2409 backend subtreep visible-only body-only ext-plist noexpand))
2410 (buffer (get-buffer-create buffer)))
2411 (with-current-buffer buffer
2412 (erase-buffer)
2413 (insert out)
2414 (goto-char (point-min)))
2415 buffer))
2417 (defun org-export-to-file
2418 (backend file &optional subtreep visible-only body-only ext-plist noexpand)
2419 "Call `org-export-as' with output to a specified file.
2421 BACKEND is the back-end used for transcoding, as a symbol. FILE
2422 is the name of the output file, as a string.
2424 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY, EXT-PLIST
2425 and NOEXPAND are similar to those used in `org-export-as', which
2426 see.
2428 Return output file's name."
2429 ;; Checks for FILE permissions. `write-file' would do the same, but
2430 ;; we'd rather avoid needless transcoding of parse tree.
2431 (unless (file-writable-p file) (error "Output file not writable"))
2432 ;; Insert contents to a temporary buffer and write it to FILE.
2433 (let ((out (org-export-as
2434 backend subtreep visible-only body-only ext-plist noexpand)))
2435 (with-temp-buffer
2436 (insert out)
2437 (let ((coding-system-for-write org-export-coding-system))
2438 (write-file file))))
2439 ;; Return full path.
2440 file)
2442 (defun org-export-output-file-name (extension &optional subtreep pub-dir)
2443 "Return output file's name according to buffer specifications.
2445 EXTENSION is a string representing the output file extension,
2446 with the leading dot.
2448 With a non-nil optional argument SUBTREEP, try to determine
2449 output file's name by looking for \"EXPORT_FILE_NAME\" property
2450 of subtree at point.
2452 When optional argument PUB-DIR is set, use it as the publishing
2453 directory.
2455 When optional argument VISIBLE-ONLY is non-nil, don't export
2456 contents of hidden elements.
2458 Return file name as a string, or nil if it couldn't be
2459 determined."
2460 (let ((base-name
2461 ;; File name may come from EXPORT_FILE_NAME subtree property,
2462 ;; assuming point is at beginning of said sub-tree.
2463 (file-name-sans-extension
2464 (or (and subtreep
2465 (org-entry-get
2466 (save-excursion
2467 (ignore-errors (org-back-to-heading) (point)))
2468 "EXPORT_FILE_NAME" t))
2469 ;; File name may be extracted from buffer's associated
2470 ;; file, if any.
2471 (buffer-file-name (buffer-base-buffer))
2472 ;; Can't determine file name on our own: Ask user.
2473 (let ((read-file-name-function
2474 (and org-completion-use-ido 'ido-read-file-name)))
2475 (read-file-name
2476 "Output file: " pub-dir nil nil nil
2477 (lambda (name)
2478 (string= (file-name-extension name t) extension))))))))
2479 ;; Build file name. Enforce EXTENSION over whatever user may have
2480 ;; come up with. PUB-DIR, if defined, always has precedence over
2481 ;; any provided path.
2482 (cond
2483 (pub-dir
2484 (concat (file-name-as-directory pub-dir)
2485 (file-name-nondirectory base-name)
2486 extension))
2487 ((string= (file-name-nondirectory base-name) base-name)
2488 (concat (file-name-as-directory ".") base-name extension))
2489 (t (concat base-name extension)))))
2491 (defmacro org-export-with-current-buffer-copy (&rest body)
2492 "Apply BODY in a copy of the current buffer.
2494 The copy preserves local variables and visibility of the original
2495 buffer.
2497 Point is at buffer's beginning when BODY is applied."
2498 (org-with-gensyms (original-buffer offset buffer-string overlays)
2499 `(let ((,original-buffer (current-buffer))
2500 (,offset (1- (point-min)))
2501 (,buffer-string (buffer-string))
2502 (,overlays (mapcar
2503 'copy-overlay (overlays-in (point-min) (point-max)))))
2504 (with-temp-buffer
2505 (let ((buffer-invisibility-spec nil))
2506 (org-clone-local-variables
2507 ,original-buffer
2508 "^\\(org-\\|orgtbl-\\|major-mode$\\|outline-\\(regexp\\|level\\)$\\)")
2509 (insert ,buffer-string)
2510 (mapc (lambda (ov)
2511 (move-overlay
2513 (- (overlay-start ov) ,offset)
2514 (- (overlay-end ov) ,offset)
2515 (current-buffer)))
2516 ,overlays)
2517 (goto-char (point-min))
2518 (progn ,@body))))))
2519 (def-edebug-spec org-export-with-current-buffer-copy (body))
2521 (defun org-export-expand-include-keyword (&optional included dir)
2522 "Expand every include keyword in buffer.
2523 Optional argument INCLUDED is a list of included file names along
2524 with their line restriction, when appropriate. It is used to
2525 avoid infinite recursion. Optional argument DIR is the current
2526 working directory. It is used to properly resolve relative
2527 paths."
2528 (let ((case-fold-search t))
2529 (goto-char (point-min))
2530 (while (re-search-forward "^[ \t]*#\\+INCLUDE: \\(.*\\)" nil t)
2531 (when (eq (org-element-type (save-match-data (org-element-at-point)))
2532 'keyword)
2533 (beginning-of-line)
2534 ;; Extract arguments from keyword's value.
2535 (let* ((value (match-string 1))
2536 (ind (org-get-indentation))
2537 (file (and (string-match "^\"\\(\\S-+\\)\"" value)
2538 (prog1 (expand-file-name (match-string 1 value) dir)
2539 (setq value (replace-match "" nil nil value)))))
2540 (lines
2541 (and (string-match
2542 ":lines +\"\\(\\(?:[0-9]+\\)?-\\(?:[0-9]+\\)?\\)\"" value)
2543 (prog1 (match-string 1 value)
2544 (setq value (replace-match "" nil nil value)))))
2545 (env (cond ((string-match "\\<example\\>" value) 'example)
2546 ((string-match "\\<src\\(?: +\\(.*\\)\\)?" value)
2547 (match-string 1 value))))
2548 ;; Minimal level of included file defaults to the child
2549 ;; level of the current headline, if any, or one. It
2550 ;; only applies is the file is meant to be included as
2551 ;; an Org one.
2552 (minlevel
2553 (and (not env)
2554 (if (string-match ":minlevel +\\([0-9]+\\)" value)
2555 (prog1 (string-to-number (match-string 1 value))
2556 (setq value (replace-match "" nil nil value)))
2557 (let ((cur (org-current-level)))
2558 (if cur (1+ (org-reduced-level cur)) 1))))))
2559 ;; Remove keyword.
2560 (delete-region (point) (progn (forward-line) (point)))
2561 (cond
2562 ((not (file-readable-p file)) (error "Cannot include file %s" file))
2563 ;; Check if files has already been parsed. Look after
2564 ;; inclusion lines too, as different parts of the same file
2565 ;; can be included too.
2566 ((member (list file lines) included)
2567 (error "Recursive file inclusion: %s" file))
2569 (cond
2570 ((eq env 'example)
2571 (insert
2572 (let ((ind-str (make-string ind ? ))
2573 (contents
2574 ;; Protect sensitive contents with commas.
2575 (replace-regexp-in-string
2576 "\\(^\\)\\([*]\\|[ \t]*#\\+\\)" ","
2577 (org-export-prepare-file-contents file lines)
2578 nil nil 1)))
2579 (format "%s#+BEGIN_EXAMPLE\n%s%s#+END_EXAMPLE\n"
2580 ind-str contents ind-str))))
2581 ((stringp env)
2582 (insert
2583 (let ((ind-str (make-string ind ? ))
2584 (contents
2585 ;; Protect sensitive contents with commas.
2586 (replace-regexp-in-string
2587 (if (string= env "org") "\\(^\\)\\(.\\)"
2588 "\\(^\\)\\([*]\\|[ \t]*#\\+\\)") ","
2589 (org-export-prepare-file-contents file lines)
2590 nil nil 1)))
2591 (format "%s#+BEGIN_SRC %s\n%s%s#+END_SRC\n"
2592 ind-str env contents ind-str))))
2594 (insert
2595 (with-temp-buffer
2596 (org-mode)
2597 (insert
2598 (org-export-prepare-file-contents file lines ind minlevel))
2599 (org-export-expand-include-keyword
2600 (cons (list file lines) included)
2601 (file-name-directory file))
2602 (buffer-string))))))))))))
2604 (defun org-export-prepare-file-contents (file &optional lines ind minlevel)
2605 "Prepare the contents of FILE for inclusion and return them as a string.
2607 When optional argument LINES is a string specifying a range of
2608 lines, include only those lines.
2610 Optional argument IND, when non-nil, is an integer specifying the
2611 global indentation of returned contents. Since its purpose is to
2612 allow an included file to stay in the same environment it was
2613 created \(i.e. a list item), it doesn't apply past the first
2614 headline encountered.
2616 Optional argument MINLEVEL, when non-nil, is an integer
2617 specifying the level that any top-level headline in the included
2618 file should have."
2619 (with-temp-buffer
2620 (insert-file-contents file)
2621 (when lines
2622 (let* ((lines (split-string lines "-"))
2623 (lbeg (string-to-number (car lines)))
2624 (lend (string-to-number (cadr lines)))
2625 (beg (if (zerop lbeg) (point-min)
2626 (goto-char (point-min))
2627 (forward-line (1- lbeg))
2628 (point)))
2629 (end (if (zerop lend) (point-max)
2630 (goto-char (point-min))
2631 (forward-line (1- lend))
2632 (point))))
2633 (narrow-to-region beg end)))
2634 ;; Remove blank lines at beginning and end of contents. The logic
2635 ;; behind that removal is that blank lines around include keyword
2636 ;; override blank lines in included file.
2637 (goto-char (point-min))
2638 (org-skip-whitespace)
2639 (beginning-of-line)
2640 (delete-region (point-min) (point))
2641 (goto-char (point-max))
2642 (skip-chars-backward " \r\t\n")
2643 (forward-line)
2644 (delete-region (point) (point-max))
2645 ;; If IND is set, preserve indentation of include keyword until
2646 ;; the first headline encountered.
2647 (when ind
2648 (unless (eq major-mode 'org-mode) (org-mode))
2649 (goto-char (point-min))
2650 (let ((ind-str (make-string ind ? )))
2651 (while (not (or (eobp) (looking-at org-outline-regexp-bol)))
2652 ;; Do not move footnote definitions out of column 0.
2653 (unless (and (looking-at org-footnote-definition-re)
2654 (eq (org-element-type (org-element-at-point))
2655 'footnote-definition))
2656 (insert ind-str))
2657 (forward-line))))
2658 ;; When MINLEVEL is specified, compute minimal level for headlines
2659 ;; in the file (CUR-MIN), and remove stars to each headline so
2660 ;; that headlines with minimal level have a level of MINLEVEL.
2661 (when minlevel
2662 (unless (eq major-mode 'org-mode) (org-mode))
2663 (let ((levels (org-map-entries
2664 (lambda () (org-reduced-level (org-current-level))))))
2665 (when levels
2666 (let ((offset (- minlevel (apply 'min levels))))
2667 (unless (zerop offset)
2668 (when org-odd-levels-only (setq offset (* offset 2)))
2669 ;; Only change stars, don't bother moving whole
2670 ;; sections.
2671 (org-map-entries
2672 (lambda () (if (< offset 0) (delete-char (abs offset))
2673 (insert (make-string offset ?*))))))))))
2674 (buffer-string)))
2677 ;;; Tools For Back-Ends
2679 ;; A whole set of tools is available to help build new exporters. Any
2680 ;; function general enough to have its use across many back-ends
2681 ;; should be added here.
2683 ;; As of now, functions operating on footnotes, headlines, links,
2684 ;; macros, references, src-blocks, tables and tables of contents are
2685 ;; implemented.
2687 ;;;; For Affiliated Keywords
2689 ;; `org-export-read-attribute' reads a property from a given element
2690 ;; as a plist. It can be used to normalize affiliated keywords'
2691 ;; syntax.
2693 (defun org-export-read-attribute (attribute element)
2694 "Turn ATTRIBUTE property from ELEMENT into a plist.
2695 This function assumes attributes are defined as \":keyword
2696 value\" pairs. It is appropriate for `:attr_html' like
2697 properties."
2698 (let ((value (org-element-property attribute element)))
2699 (and value
2700 (read (format "(%s)" (mapconcat 'identity value " "))))))
2703 ;;;; For Export Snippets
2705 ;; Every export snippet is transmitted to the back-end. Though, the
2706 ;; latter will only retain one type of export-snippet, ignoring
2707 ;; others, based on the former's target back-end. The function
2708 ;; `org-export-snippet-backend' returns that back-end for a given
2709 ;; export-snippet.
2711 (defun org-export-snippet-backend (export-snippet)
2712 "Return EXPORT-SNIPPET targeted back-end as a symbol.
2713 Translation, with `org-export-snippet-translation-alist', is
2714 applied."
2715 (let ((back-end (org-element-property :back-end export-snippet)))
2716 (intern
2717 (or (cdr (assoc back-end org-export-snippet-translation-alist))
2718 back-end))))
2721 ;;;; For Footnotes
2723 ;; `org-export-collect-footnote-definitions' is a tool to list
2724 ;; actually used footnotes definitions in the whole parse tree, or in
2725 ;; an headline, in order to add footnote listings throughout the
2726 ;; transcoded data.
2728 ;; `org-export-footnote-first-reference-p' is a predicate used by some
2729 ;; back-ends, when they need to attach the footnote definition only to
2730 ;; the first occurrence of the corresponding label.
2732 ;; `org-export-get-footnote-definition' and
2733 ;; `org-export-get-footnote-number' provide easier access to
2734 ;; additional information relative to a footnote reference.
2736 (defun org-export-collect-footnote-definitions (data info)
2737 "Return an alist between footnote numbers, labels and definitions.
2739 DATA is the parse tree from which definitions are collected.
2740 INFO is the plist used as a communication channel.
2742 Definitions are sorted by order of references. They either
2743 appear as Org data or as a secondary string for inlined
2744 footnotes. Unreferenced definitions are ignored."
2745 (let* (num-alist
2746 collect-fn ; for byte-compiler.
2747 (collect-fn
2748 (function
2749 (lambda (data)
2750 ;; Collect footnote number, label and definition in DATA.
2751 (org-element-map
2752 data 'footnote-reference
2753 (lambda (fn)
2754 (when (org-export-footnote-first-reference-p fn info)
2755 (let ((def (org-export-get-footnote-definition fn info)))
2756 (push
2757 (list (org-export-get-footnote-number fn info)
2758 (org-element-property :label fn)
2759 def)
2760 num-alist)
2761 ;; Also search in definition for nested footnotes.
2762 (when (eq (org-element-property :type fn) 'standard)
2763 (funcall collect-fn def)))))
2764 ;; Don't enter footnote definitions since it will happen
2765 ;; when their first reference is found.
2766 info nil 'footnote-definition)))))
2767 (funcall collect-fn (plist-get info :parse-tree))
2768 (reverse num-alist)))
2770 (defun org-export-footnote-first-reference-p (footnote-reference info)
2771 "Non-nil when a footnote reference is the first one for its label.
2773 FOOTNOTE-REFERENCE is the footnote reference being considered.
2774 INFO is the plist used as a communication channel."
2775 (let ((label (org-element-property :label footnote-reference)))
2776 ;; Anonymous footnotes are always a first reference.
2777 (if (not label) t
2778 ;; Otherwise, return the first footnote with the same LABEL and
2779 ;; test if it is equal to FOOTNOTE-REFERENCE.
2780 (let* (search-refs ; for byte-compiler.
2781 (search-refs
2782 (function
2783 (lambda (data)
2784 (org-element-map
2785 data 'footnote-reference
2786 (lambda (fn)
2787 (cond
2788 ((string= (org-element-property :label fn) label)
2789 (throw 'exit fn))
2790 ;; If FN isn't inlined, be sure to traverse its
2791 ;; definition before resuming search. See
2792 ;; comments in `org-export-get-footnote-number'
2793 ;; for more information.
2794 ((eq (org-element-property :type fn) 'standard)
2795 (funcall search-refs
2796 (org-export-get-footnote-definition fn info)))))
2797 ;; Don't enter footnote definitions since it will
2798 ;; happen when their first reference is found.
2799 info 'first-match 'footnote-definition)))))
2800 (equal (catch 'exit (funcall search-refs (plist-get info :parse-tree)))
2801 footnote-reference)))))
2803 (defun org-export-get-footnote-definition (footnote-reference info)
2804 "Return definition of FOOTNOTE-REFERENCE as parsed data.
2805 INFO is the plist used as a communication channel."
2806 (let ((label (org-element-property :label footnote-reference)))
2807 (or (org-element-property :inline-definition footnote-reference)
2808 (cdr (assoc label (plist-get info :footnote-definition-alist))))))
2810 (defun org-export-get-footnote-number (footnote info)
2811 "Return number associated to a footnote.
2813 FOOTNOTE is either a footnote reference or a footnote definition.
2814 INFO is the plist used as a communication channel."
2815 (let* ((label (org-element-property :label footnote))
2816 seen-refs
2817 search-ref ; for byte-compiler.
2818 (search-ref
2819 (function
2820 (lambda (data)
2821 ;; Search footnote references through DATA, filling
2822 ;; SEEN-REFS along the way.
2823 (org-element-map
2824 data 'footnote-reference
2825 (lambda (fn)
2826 (let ((fn-lbl (org-element-property :label fn)))
2827 (cond
2828 ;; Anonymous footnote match: return number.
2829 ((and (not fn-lbl) (equal fn footnote))
2830 (throw 'exit (1+ (length seen-refs))))
2831 ;; Labels match: return number.
2832 ((and label (string= label fn-lbl))
2833 (throw 'exit (1+ (length seen-refs))))
2834 ;; Anonymous footnote: it's always a new one. Also,
2835 ;; be sure to return nil from the `cond' so
2836 ;; `first-match' doesn't get us out of the loop.
2837 ((not fn-lbl) (push 'inline seen-refs) nil)
2838 ;; Label not seen so far: add it so SEEN-REFS.
2840 ;; Also search for subsequent references in footnote
2841 ;; definition so numbering following reading logic.
2842 ;; Note that we don't have to care about inline
2843 ;; definitions, since `org-element-map' already
2844 ;; traverse them at the right time.
2846 ;; Once again, return nil to stay in the loop.
2847 ((not (member fn-lbl seen-refs))
2848 (push fn-lbl seen-refs)
2849 (funcall search-ref
2850 (org-export-get-footnote-definition fn info))
2851 nil))))
2852 ;; Don't enter footnote definitions since it will happen
2853 ;; when their first reference is found.
2854 info 'first-match 'footnote-definition)))))
2855 (catch 'exit (funcall search-ref (plist-get info :parse-tree)))))
2858 ;;;; For Headlines
2860 ;; `org-export-get-relative-level' is a shortcut to get headline
2861 ;; level, relatively to the lower headline level in the parsed tree.
2863 ;; `org-export-get-headline-number' returns the section number of an
2864 ;; headline, while `org-export-number-to-roman' allows to convert it
2865 ;; to roman numbers.
2867 ;; `org-export-low-level-p', `org-export-first-sibling-p' and
2868 ;; `org-export-last-sibling-p' are three useful predicates when it
2869 ;; comes to fulfill the `:headline-levels' property.
2871 (defun org-export-get-relative-level (headline info)
2872 "Return HEADLINE relative level within current parsed tree.
2873 INFO is a plist holding contextual information."
2874 (+ (org-element-property :level headline)
2875 (or (plist-get info :headline-offset) 0)))
2877 (defun org-export-low-level-p (headline info)
2878 "Non-nil when HEADLINE is considered as low level.
2880 INFO is a plist used as a communication channel.
2882 A low level headlines has a relative level greater than
2883 `:headline-levels' property value.
2885 Return value is the difference between HEADLINE relative level
2886 and the last level being considered as high enough, or nil."
2887 (let ((limit (plist-get info :headline-levels)))
2888 (when (wholenump limit)
2889 (let ((level (org-export-get-relative-level headline info)))
2890 (and (> level limit) (- level limit))))))
2892 (defun org-export-get-headline-number (headline info)
2893 "Return HEADLINE numbering as a list of numbers.
2894 INFO is a plist holding contextual information."
2895 (cdr (assoc headline (plist-get info :headline-numbering))))
2897 (defun org-export-numbered-headline-p (headline info)
2898 "Return a non-nil value if HEADLINE element should be numbered.
2899 INFO is a plist used as a communication channel."
2900 (let ((sec-num (plist-get info :section-numbers))
2901 (level (org-export-get-relative-level headline info)))
2902 (if (wholenump sec-num) (<= level sec-num) sec-num)))
2904 (defun org-export-number-to-roman (n)
2905 "Convert integer N into a roman numeral."
2906 (let ((roman '((1000 . "M") (900 . "CM") (500 . "D") (400 . "CD")
2907 ( 100 . "C") ( 90 . "XC") ( 50 . "L") ( 40 . "XL")
2908 ( 10 . "X") ( 9 . "IX") ( 5 . "V") ( 4 . "IV")
2909 ( 1 . "I")))
2910 (res ""))
2911 (if (<= n 0)
2912 (number-to-string n)
2913 (while roman
2914 (if (>= n (caar roman))
2915 (setq n (- n (caar roman))
2916 res (concat res (cdar roman)))
2917 (pop roman)))
2918 res)))
2920 (defun org-export-get-tags (element info &optional tags)
2921 "Return list of tags associated to ELEMENT.
2923 ELEMENT has either an `headline' or an `inlinetask' type. INFO
2924 is a plist used as a communication channel.
2926 Select tags (see `org-export-select-tags') and exclude tags (see
2927 `org-export-exclude-tags') are removed from the list.
2929 When non-nil, optional argument TAGS should be a list of strings.
2930 Any tag belonging to this list will also be removed."
2931 (org-remove-if (lambda (tag) (or (member tag (plist-get info :select-tags))
2932 (member tag (plist-get info :exclude-tags))
2933 (member tag tags)))
2934 (org-element-property :tags element)))
2936 (defun org-export-first-sibling-p (headline)
2937 "Non-nil when HEADLINE is the first sibling in its sub-tree."
2938 (not (eq (org-element-type (org-export-get-previous-element headline))
2939 'headline)))
2941 (defun org-export-last-sibling-p (headline)
2942 "Non-nil when HEADLINE is the last sibling in its sub-tree."
2943 (not (org-export-get-next-element headline)))
2946 ;;;; For Links
2948 ;; `org-export-solidify-link-text' turns a string into a safer version
2949 ;; for links, replacing most non-standard characters with hyphens.
2951 ;; `org-export-get-coderef-format' returns an appropriate format
2952 ;; string for coderefs.
2954 ;; `org-export-inline-image-p' returns a non-nil value when the link
2955 ;; provided should be considered as an inline image.
2957 ;; `org-export-resolve-fuzzy-link' searches destination of fuzzy links
2958 ;; (i.e. links with "fuzzy" as type) within the parsed tree, and
2959 ;; returns an appropriate unique identifier when found, or nil.
2961 ;; `org-export-resolve-id-link' returns the first headline with
2962 ;; specified id or custom-id in parse tree, the path to the external
2963 ;; file with the id or nil when neither was found.
2965 ;; `org-export-resolve-coderef' associates a reference to a line
2966 ;; number in the element it belongs, or returns the reference itself
2967 ;; when the element isn't numbered.
2969 (defun org-export-solidify-link-text (s)
2970 "Take link text S and make a safe target out of it."
2971 (save-match-data
2972 (mapconcat 'identity (org-split-string s "[^a-zA-Z0-9_.-]+") "-")))
2974 (defun org-export-get-coderef-format (path desc)
2975 "Return format string for code reference link.
2976 PATH is the link path. DESC is its description."
2977 (save-match-data
2978 (cond ((not desc) "%s")
2979 ((string-match (regexp-quote (concat "(" path ")")) desc)
2980 (replace-match "%s" t t desc))
2981 (t desc))))
2983 (defun org-export-inline-image-p (link &optional rules)
2984 "Non-nil if LINK object points to an inline image.
2986 Optional argument is a set of RULES defining inline images. It
2987 is an alist where associations have the following shape:
2989 \(TYPE . REGEXP)
2991 Applying a rule means apply REGEXP against LINK's path when its
2992 type is TYPE. The function will return a non-nil value if any of
2993 the provided rules is non-nil. The default rule is
2994 `org-export-default-inline-image-rule'.
2996 This only applies to links without a description."
2997 (and (not (org-element-contents link))
2998 (let ((case-fold-search t)
2999 (rules (or rules org-export-default-inline-image-rule)))
3000 (catch 'exit
3001 (mapc
3002 (lambda (rule)
3003 (and (string= (org-element-property :type link) (car rule))
3004 (string-match (cdr rule)
3005 (org-element-property :path link))
3006 (throw 'exit t)))
3007 rules)
3008 ;; Return nil if no rule matched.
3009 nil))))
3011 (defun org-export-resolve-coderef (ref info)
3012 "Resolve a code reference REF.
3014 INFO is a plist used as a communication channel.
3016 Return associated line number in source code, or REF itself,
3017 depending on src-block or example element's switches."
3018 (org-element-map
3019 (plist-get info :parse-tree) '(example-block src-block)
3020 (lambda (el)
3021 (with-temp-buffer
3022 (insert (org-trim (org-element-property :value el)))
3023 (let* ((label-fmt (regexp-quote
3024 (or (org-element-property :label-fmt el)
3025 org-coderef-label-format)))
3026 (ref-re
3027 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
3028 (replace-regexp-in-string "%s" ref label-fmt nil t))))
3029 ;; Element containing REF is found. Resolve it to either
3030 ;; a label or a line number, as needed.
3031 (when (re-search-backward ref-re nil t)
3032 (cond
3033 ((org-element-property :use-labels el) ref)
3034 ((eq (org-element-property :number-lines el) 'continued)
3035 (+ (org-export-get-loc el info) (line-number-at-pos)))
3036 (t (line-number-at-pos)))))))
3037 info 'first-match))
3039 (defun org-export-resolve-fuzzy-link (link info)
3040 "Return LINK destination.
3042 INFO is a plist holding contextual information.
3044 Return value can be an object, an element, or nil:
3046 - If LINK path matches a target object (i.e. <<path>>) or
3047 element (i.e. \"#+TARGET: path\"), return it.
3049 - If LINK path exactly matches the name affiliated keyword
3050 \(i.e. #+NAME: path) of an element, return that element.
3052 - If LINK path exactly matches any headline name, return that
3053 element. If more than one headline share that name, priority
3054 will be given to the one with the closest common ancestor, if
3055 any, or the first one in the parse tree otherwise.
3057 - Otherwise, return nil.
3059 Assume LINK type is \"fuzzy\"."
3060 (let* ((path (org-element-property :path link))
3061 (match-title-p (eq (aref path 0) ?*)))
3062 (cond
3063 ;; First try to find a matching "<<path>>" unless user specified
3064 ;; he was looking for an headline (path starts with a *
3065 ;; character).
3066 ((and (not match-title-p)
3067 (loop for target in (plist-get info :target-list)
3068 when (string= (org-element-property :value target) path)
3069 return target)))
3070 ;; Then try to find an element with a matching "#+NAME: path"
3071 ;; affiliated keyword.
3072 ((and (not match-title-p)
3073 (org-element-map
3074 (plist-get info :parse-tree) org-element-all-elements
3075 (lambda (el)
3076 (when (string= (org-element-property :name el) path) el))
3077 info 'first-match)))
3078 ;; Last case: link either points to an headline or to
3079 ;; nothingness. Try to find the source, with priority given to
3080 ;; headlines with the closest common ancestor. If such candidate
3081 ;; is found, return it, otherwise return nil.
3083 (let ((find-headline
3084 (function
3085 ;; Return first headline whose `:raw-value' property
3086 ;; is NAME in parse tree DATA, or nil.
3087 (lambda (name data)
3088 (org-element-map
3089 data 'headline
3090 (lambda (headline)
3091 (when (string=
3092 (org-element-property :raw-value headline)
3093 name)
3094 headline))
3095 info 'first-match)))))
3096 ;; Search among headlines sharing an ancestor with link,
3097 ;; from closest to farthest.
3098 (or (catch 'exit
3099 (mapc
3100 (lambda (parent)
3101 (when (eq (org-element-type parent) 'headline)
3102 (let ((foundp (funcall find-headline path parent)))
3103 (when foundp (throw 'exit foundp)))))
3104 (org-export-get-genealogy link)) nil)
3105 ;; No match with a common ancestor: try the full parse-tree.
3106 (funcall find-headline
3107 (if match-title-p (substring path 1) path)
3108 (plist-get info :parse-tree))))))))
3110 (defun org-export-resolve-id-link (link info)
3111 "Return headline referenced as LINK destination.
3113 INFO is a plist used as a communication channel.
3115 Return value can be the headline element matched in current parse
3116 tree, a file name or nil. Assume LINK type is either \"id\" or
3117 \"custom-id\"."
3118 (let ((id (org-element-property :path link)))
3119 ;; First check if id is within the current parse tree.
3120 (or (org-element-map
3121 (plist-get info :parse-tree) 'headline
3122 (lambda (headline)
3123 (when (or (string= (org-element-property :id headline) id)
3124 (string= (org-element-property :custom-id headline) id))
3125 headline))
3126 info 'first-match)
3127 ;; Otherwise, look for external files.
3128 (cdr (assoc id (plist-get info :id-alist))))))
3130 (defun org-export-resolve-radio-link (link info)
3131 "Return radio-target object referenced as LINK destination.
3133 INFO is a plist used as a communication channel.
3135 Return value can be a radio-target object or nil. Assume LINK
3136 has type \"radio\"."
3137 (let ((path (org-element-property :path link)))
3138 (org-element-map
3139 (plist-get info :parse-tree) 'radio-target
3140 (lambda (radio)
3141 (when (equal (org-element-property :value radio) path) radio))
3142 info 'first-match)))
3145 ;;;; For Macros
3147 ;; `org-export-expand-macro' simply takes care of expanding macros.
3149 (defun org-export-expand-macro (macro info)
3150 "Expand MACRO and return it as a string.
3151 INFO is a plist holding export options."
3152 (let* ((key (org-element-property :key macro))
3153 (args (org-element-property :args macro))
3154 ;; User's macros are stored in the communication channel with
3155 ;; a ":macro-" prefix. Replace arguments in VALUE. Also
3156 ;; expand recursively macros within.
3157 (value (org-export-data
3158 (mapcar
3159 (lambda (obj)
3160 (if (not (stringp obj)) (org-export-data obj info)
3161 (replace-regexp-in-string
3162 "\\$[0-9]+"
3163 (lambda (arg)
3164 (nth (1- (string-to-number (substring arg 1))) args))
3165 obj)))
3166 (plist-get info (intern (format ":macro-%s" key))))
3167 info)))
3168 ;; VALUE starts with "(eval": it is a s-exp, `eval' it.
3169 (when (string-match "\\`(eval\\>" value) (setq value (eval (read value))))
3170 ;; Return string.
3171 (format "%s" (or value ""))))
3174 ;;;; For References
3176 ;; `org-export-get-ordinal' associates a sequence number to any object
3177 ;; or element.
3179 (defun org-export-get-ordinal (element info &optional types predicate)
3180 "Return ordinal number of an element or object.
3182 ELEMENT is the element or object considered. INFO is the plist
3183 used as a communication channel.
3185 Optional argument TYPES, when non-nil, is a list of element or
3186 object types, as symbols, that should also be counted in.
3187 Otherwise, only provided element's type is considered.
3189 Optional argument PREDICATE is a function returning a non-nil
3190 value if the current element or object should be counted in. It
3191 accepts two arguments: the element or object being considered and
3192 the plist used as a communication channel. This allows to count
3193 only a certain type of objects (i.e. inline images).
3195 Return value is a list of numbers if ELEMENT is an headline or an
3196 item. It is nil for keywords. It represents the footnote number
3197 for footnote definitions and footnote references. If ELEMENT is
3198 a target, return the same value as if ELEMENT was the closest
3199 table, item or headline containing the target. In any other
3200 case, return the sequence number of ELEMENT among elements or
3201 objects of the same type."
3202 ;; A target keyword, representing an invisible target, never has
3203 ;; a sequence number.
3204 (unless (eq (org-element-type element) 'keyword)
3205 ;; Ordinal of a target object refer to the ordinal of the closest
3206 ;; table, item, or headline containing the object.
3207 (when (eq (org-element-type element) 'target)
3208 (setq element
3209 (loop for parent in (org-export-get-genealogy element)
3210 when
3211 (memq
3212 (org-element-type parent)
3213 '(footnote-definition footnote-reference headline item
3214 table))
3215 return parent)))
3216 (case (org-element-type element)
3217 ;; Special case 1: An headline returns its number as a list.
3218 (headline (org-export-get-headline-number element info))
3219 ;; Special case 2: An item returns its number as a list.
3220 (item (let ((struct (org-element-property :structure element)))
3221 (org-list-get-item-number
3222 (org-element-property :begin element)
3223 struct
3224 (org-list-prevs-alist struct)
3225 (org-list-parents-alist struct))))
3226 ((footnote-definition footnote-reference)
3227 (org-export-get-footnote-number element info))
3228 (otherwise
3229 (let ((counter 0))
3230 ;; Increment counter until ELEMENT is found again.
3231 (org-element-map
3232 (plist-get info :parse-tree) (or types (org-element-type element))
3233 (lambda (el)
3234 (cond
3235 ((equal element el) (1+ counter))
3236 ((not predicate) (incf counter) nil)
3237 ((funcall predicate el info) (incf counter) nil)))
3238 info 'first-match))))))
3241 ;;;; For Src-Blocks
3243 ;; `org-export-get-loc' counts number of code lines accumulated in
3244 ;; src-block or example-block elements with a "+n" switch until
3245 ;; a given element, excluded. Note: "-n" switches reset that count.
3247 ;; `org-export-unravel-code' extracts source code (along with a code
3248 ;; references alist) from an `element-block' or `src-block' type
3249 ;; element.
3251 ;; `org-export-format-code' applies a formatting function to each line
3252 ;; of code, providing relative line number and code reference when
3253 ;; appropriate. Since it doesn't access the original element from
3254 ;; which the source code is coming, it expects from the code calling
3255 ;; it to know if lines should be numbered and if code references
3256 ;; should appear.
3258 ;; Eventually, `org-export-format-code-default' is a higher-level
3259 ;; function (it makes use of the two previous functions) which handles
3260 ;; line numbering and code references inclusion, and returns source
3261 ;; code in a format suitable for plain text or verbatim output.
3263 (defun org-export-get-loc (element info)
3264 "Return accumulated lines of code up to ELEMENT.
3266 INFO is the plist used as a communication channel.
3268 ELEMENT is excluded from count."
3269 (let ((loc 0))
3270 (org-element-map
3271 (plist-get info :parse-tree)
3272 `(src-block example-block ,(org-element-type element))
3273 (lambda (el)
3274 (cond
3275 ;; ELEMENT is reached: Quit the loop.
3276 ((equal el element) t)
3277 ;; Only count lines from src-block and example-block elements
3278 ;; with a "+n" or "-n" switch. A "-n" switch resets counter.
3279 ((not (memq (org-element-type el) '(src-block example-block))) nil)
3280 ((let ((linums (org-element-property :number-lines el)))
3281 (when linums
3282 ;; Accumulate locs or reset them.
3283 (let ((lines (org-count-lines
3284 (org-trim (org-element-property :value el)))))
3285 (setq loc (if (eq linums 'new) lines (+ loc lines))))))
3286 ;; Return nil to stay in the loop.
3287 nil)))
3288 info 'first-match)
3289 ;; Return value.
3290 loc))
3292 (defun org-export-unravel-code (element)
3293 "Clean source code and extract references out of it.
3295 ELEMENT has either a `src-block' an `example-block' type.
3297 Return a cons cell whose CAR is the source code, cleaned from any
3298 reference and protective comma and CDR is an alist between
3299 relative line number (integer) and name of code reference on that
3300 line (string)."
3301 (let* ((line 0) refs
3302 ;; Get code and clean it. Remove blank lines at its
3303 ;; beginning and end. Also remove protective commas.
3304 (code (let ((c (replace-regexp-in-string
3305 "\\`\\([ \t]*\n\\)+" ""
3306 (replace-regexp-in-string
3307 "\\(:?[ \t]*\n\\)*[ \t]*\\'" "\n"
3308 (org-element-property :value element)))))
3309 ;; If appropriate, remove global indentation.
3310 (unless (or org-src-preserve-indentation
3311 (org-element-property :preserve-indent element))
3312 (setq c (org-remove-indentation c)))
3313 ;; Free up the protected lines. Note: Org blocks
3314 ;; have commas at the beginning or every line.
3315 (if (string= (org-element-property :language element) "org")
3316 (replace-regexp-in-string "^," "" c)
3317 (replace-regexp-in-string
3318 "^\\(,\\)\\(:?\\*\\|[ \t]*#\\+\\)" "" c nil nil 1))))
3319 ;; Get format used for references.
3320 (label-fmt (regexp-quote
3321 (or (org-element-property :label-fmt element)
3322 org-coderef-label-format)))
3323 ;; Build a regexp matching a loc with a reference.
3324 (with-ref-re
3325 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)[ \t]*\\)$"
3326 (replace-regexp-in-string
3327 "%s" "\\([-a-zA-Z0-9_ ]+\\)" label-fmt nil t))))
3328 ;; Return value.
3329 (cons
3330 ;; Code with references removed.
3331 (org-element-normalize-string
3332 (mapconcat
3333 (lambda (loc)
3334 (incf line)
3335 (if (not (string-match with-ref-re loc)) loc
3336 ;; Ref line: remove ref, and signal its position in REFS.
3337 (push (cons line (match-string 3 loc)) refs)
3338 (replace-match "" nil nil loc 1)))
3339 (org-split-string code "\n") "\n"))
3340 ;; Reference alist.
3341 refs)))
3343 (defun org-export-format-code (code fun &optional num-lines ref-alist)
3344 "Format CODE by applying FUN line-wise and return it.
3346 CODE is a string representing the code to format. FUN is
3347 a function. It must accept three arguments: a line of
3348 code (string), the current line number (integer) or nil and the
3349 reference associated to the current line (string) or nil.
3351 Optional argument NUM-LINES can be an integer representing the
3352 number of code lines accumulated until the current code. Line
3353 numbers passed to FUN will take it into account. If it is nil,
3354 FUN's second argument will always be nil. This number can be
3355 obtained with `org-export-get-loc' function.
3357 Optional argument REF-ALIST can be an alist between relative line
3358 number (i.e. ignoring NUM-LINES) and the name of the code
3359 reference on it. If it is nil, FUN's third argument will always
3360 be nil. It can be obtained through the use of
3361 `org-export-unravel-code' function."
3362 (let ((--locs (org-split-string code "\n"))
3363 (--line 0))
3364 (org-element-normalize-string
3365 (mapconcat
3366 (lambda (--loc)
3367 (incf --line)
3368 (let ((--ref (cdr (assq --line ref-alist))))
3369 (funcall fun --loc (and num-lines (+ num-lines --line)) --ref)))
3370 --locs "\n"))))
3372 (defun org-export-format-code-default (element info)
3373 "Return source code from ELEMENT, formatted in a standard way.
3375 ELEMENT is either a `src-block' or `example-block' element. INFO
3376 is a plist used as a communication channel.
3378 This function takes care of line numbering and code references
3379 inclusion. Line numbers, when applicable, appear at the
3380 beginning of the line, separated from the code by two white
3381 spaces. Code references, on the other hand, appear flushed to
3382 the right, separated by six white spaces from the widest line of
3383 code."
3384 ;; Extract code and references.
3385 (let* ((code-info (org-export-unravel-code element))
3386 (code (car code-info))
3387 (code-lines (org-split-string code "\n"))
3388 (refs (and (org-element-property :retain-labels element)
3389 (cdr code-info)))
3390 ;; Handle line numbering.
3391 (num-start (case (org-element-property :number-lines element)
3392 (continued (org-export-get-loc element info))
3393 (new 0)))
3394 (num-fmt
3395 (and num-start
3396 (format "%%%ds "
3397 (length (number-to-string
3398 (+ (length code-lines) num-start))))))
3399 ;; Prepare references display, if required. Any reference
3400 ;; should start six columns after the widest line of code,
3401 ;; wrapped with parenthesis.
3402 (max-width
3403 (+ (apply 'max (mapcar 'length code-lines))
3404 (if (not num-start) 0 (length (format num-fmt num-start))))))
3405 (org-export-format-code
3406 code
3407 (lambda (loc line-num ref)
3408 (let ((number-str (and num-fmt (format num-fmt line-num))))
3409 (concat
3410 number-str
3412 (and ref
3413 (concat (make-string
3414 (- (+ 6 max-width)
3415 (+ (length loc) (length number-str))) ? )
3416 (format "(%s)" ref))))))
3417 num-start refs)))
3420 ;;;; For Tables
3422 ;; `org-export-table-has-special-column-p' and and
3423 ;; `org-export-table-row-is-special-p' are predicates used to look for
3424 ;; meta-information about the table structure.
3426 ;; `org-table-has-header-p' tells when the rows before the first rule
3427 ;; should be considered as table's header.
3429 ;; `org-export-table-cell-width', `org-export-table-cell-alignment'
3430 ;; and `org-export-table-cell-borders' extract information from
3431 ;; a table-cell element.
3433 ;; `org-export-table-dimensions' gives the number on rows and columns
3434 ;; in the table, ignoring horizontal rules and special columns.
3435 ;; `org-export-table-cell-address', given a table-cell object, returns
3436 ;; the absolute address of a cell. On the other hand,
3437 ;; `org-export-get-table-cell-at' does the contrary.
3439 ;; `org-export-table-cell-starts-colgroup-p',
3440 ;; `org-export-table-cell-ends-colgroup-p',
3441 ;; `org-export-table-row-starts-rowgroup-p',
3442 ;; `org-export-table-row-ends-rowgroup-p',
3443 ;; `org-export-table-row-starts-header-p' and
3444 ;; `org-export-table-row-ends-header-p' indicate position of current
3445 ;; row or cell within the table.
3447 (defun org-export-table-has-special-column-p (table)
3448 "Non-nil when TABLE has a special column.
3449 All special columns will be ignored during export."
3450 ;; The table has a special column when every first cell of every row
3451 ;; has an empty value or contains a symbol among "/", "#", "!", "$",
3452 ;; "*" "_" and "^". Though, do not consider a first row containing
3453 ;; only empty cells as special.
3454 (let ((special-column-p 'empty))
3455 (catch 'exit
3456 (mapc
3457 (lambda (row)
3458 (when (eq (org-element-property :type row) 'standard)
3459 (let ((value (org-element-contents
3460 (car (org-element-contents row)))))
3461 (cond ((member value '(("/") ("#") ("!") ("$") ("*") ("_") ("^")))
3462 (setq special-column-p 'special))
3463 ((not value))
3464 (t (throw 'exit nil))))))
3465 (org-element-contents table))
3466 (eq special-column-p 'special))))
3468 (defun org-export-table-has-header-p (table info)
3469 "Non-nil when TABLE has an header.
3471 INFO is a plist used as a communication channel.
3473 A table has an header when it contains at least two row groups."
3474 (let ((rowgroup 1) row-flag)
3475 (org-element-map
3476 table 'table-row
3477 (lambda (row)
3478 (cond
3479 ((> rowgroup 1) t)
3480 ((and row-flag (eq (org-element-property :type row) 'rule))
3481 (incf rowgroup) (setq row-flag nil))
3482 ((and (not row-flag) (eq (org-element-property :type row) 'standard))
3483 (setq row-flag t) nil)))
3484 info)))
3486 (defun org-export-table-row-is-special-p (table-row info)
3487 "Non-nil if TABLE-ROW is considered special.
3489 INFO is a plist used as the communication channel.
3491 All special rows will be ignored during export."
3492 (when (eq (org-element-property :type table-row) 'standard)
3493 (let ((first-cell (org-element-contents
3494 (car (org-element-contents table-row)))))
3495 ;; A row is special either when...
3497 ;; ... it starts with a field only containing "/",
3498 (equal first-cell '("/"))
3499 ;; ... the table contains a special column and the row start
3500 ;; with a marking character among, "^", "_", "$" or "!",
3501 (and (org-export-table-has-special-column-p
3502 (org-export-get-parent table-row))
3503 (member first-cell '(("^") ("_") ("$") ("!"))))
3504 ;; ... it contains only alignment cookies and empty cells.
3505 (let ((special-row-p 'empty))
3506 (catch 'exit
3507 (mapc
3508 (lambda (cell)
3509 (let ((value (org-element-contents cell)))
3510 ;; Since VALUE is a secondary string, the following
3511 ;; checks avoid expanding it with `org-export-data'.
3512 (cond ((not value))
3513 ((and (not (cdr value))
3514 (stringp (car value))
3515 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'"
3516 (car value)))
3517 (setq special-row-p 'cookie))
3518 (t (throw 'exit nil)))))
3519 (org-element-contents table-row))
3520 (eq special-row-p 'cookie)))))))
3522 (defun org-export-table-row-group (table-row info)
3523 "Return TABLE-ROW's group.
3525 INFO is a plist used as the communication channel.
3527 Return value is the group number, as an integer, or nil special
3528 rows and table rules. Group 1 is also table's header."
3529 (unless (or (eq (org-element-property :type table-row) 'rule)
3530 (org-export-table-row-is-special-p table-row info))
3531 (let ((group 0) row-flag)
3532 (catch 'found
3533 (mapc
3534 (lambda (row)
3535 (cond
3536 ((and (eq (org-element-property :type row) 'standard)
3537 (not (org-export-table-row-is-special-p row info)))
3538 (unless row-flag (incf group) (setq row-flag t)))
3539 ((eq (org-element-property :type row) 'rule)
3540 (setq row-flag nil)))
3541 (when (equal table-row row) (throw 'found group)))
3542 (org-element-contents (org-export-get-parent table-row)))))))
3544 (defun org-export-table-cell-width (table-cell info)
3545 "Return TABLE-CELL contents width.
3547 INFO is a plist used as the communication channel.
3549 Return value is the width given by the last width cookie in the
3550 same column as TABLE-CELL, or nil."
3551 (let* ((row (org-export-get-parent table-cell))
3552 (column (let ((cells (org-element-contents row)))
3553 (- (length cells) (length (member table-cell cells)))))
3554 (table (org-export-get-parent-table table-cell))
3555 cookie-width)
3556 (mapc
3557 (lambda (row)
3558 (cond
3559 ;; In a special row, try to find a width cookie at COLUMN.
3560 ((org-export-table-row-is-special-p row info)
3561 (let ((value (org-element-contents
3562 (elt (org-element-contents row) column))))
3563 ;; The following checks avoid expanding unnecessarily the
3564 ;; cell with `org-export-data'
3565 (when (and value
3566 (not (cdr value))
3567 (stringp (car value))
3568 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'" (car value))
3569 (match-string 1 (car value)))
3570 (setq cookie-width
3571 (string-to-number (match-string 1 (car value)))))))
3572 ;; Ignore table rules.
3573 ((eq (org-element-property :type row) 'rule))))
3574 (org-element-contents table))
3575 ;; Return value.
3576 cookie-width))
3578 (defun org-export-table-cell-alignment (table-cell info)
3579 "Return TABLE-CELL contents alignment.
3581 INFO is a plist used as the communication channel.
3583 Return alignment as specified by the last alignment cookie in the
3584 same column as TABLE-CELL. If no such cookie is found, a default
3585 alignment value will be deduced from fraction of numbers in the
3586 column (see `org-table-number-fraction' for more information).
3587 Possible values are `left', `right' and `center'."
3588 (let* ((row (org-export-get-parent table-cell))
3589 (column (let ((cells (org-element-contents row)))
3590 (- (length cells) (length (member table-cell cells)))))
3591 (table (org-export-get-parent-table table-cell))
3592 (number-cells 0)
3593 (total-cells 0)
3594 cookie-align)
3595 (mapc
3596 (lambda (row)
3597 (cond
3598 ;; In a special row, try to find an alignment cookie at
3599 ;; COLUMN.
3600 ((org-export-table-row-is-special-p row info)
3601 (let ((value (org-element-contents
3602 (elt (org-element-contents row) column))))
3603 ;; Since VALUE is a secondary string, the following checks
3604 ;; avoid useless expansion through `org-export-data'.
3605 (when (and value
3606 (not (cdr value))
3607 (stringp (car value))
3608 (string-match "\\`<\\([lrc]\\)?\\([0-9]+\\)?>\\'"
3609 (car value))
3610 (match-string 1 (car value)))
3611 (setq cookie-align (match-string 1 (car value))))))
3612 ;; Ignore table rules.
3613 ((eq (org-element-property :type row) 'rule))
3614 ;; In a standard row, check if cell's contents are expressing
3615 ;; some kind of number. Increase NUMBER-CELLS accordingly.
3616 ;; Though, don't bother if an alignment cookie has already
3617 ;; defined cell's alignment.
3618 ((not cookie-align)
3619 (let ((value (org-export-data
3620 (org-element-contents
3621 (elt (org-element-contents row) column))
3622 info)))
3623 (incf total-cells)
3624 (when (string-match org-table-number-regexp value)
3625 (incf number-cells))))))
3626 (org-element-contents table))
3627 ;; Return value. Alignment specified by cookies has precedence
3628 ;; over alignment deduced from cells contents.
3629 (cond ((equal cookie-align "l") 'left)
3630 ((equal cookie-align "r") 'right)
3631 ((equal cookie-align "c") 'center)
3632 ((>= (/ (float number-cells) total-cells) org-table-number-fraction)
3633 'right)
3634 (t 'left))))
3636 (defun org-export-table-cell-borders (table-cell info)
3637 "Return TABLE-CELL borders.
3639 INFO is a plist used as a communication channel.
3641 Return value is a list of symbols, or nil. Possible values are:
3642 `top', `bottom', `above', `below', `left' and `right'. Note:
3643 `top' (resp. `bottom') only happen for a cell in the first
3644 row (resp. last row) of the table, ignoring table rules, if any.
3646 Returned borders ignore special rows."
3647 (let* ((row (org-export-get-parent table-cell))
3648 (table (org-export-get-parent-table table-cell))
3649 borders)
3650 ;; Top/above border? TABLE-CELL has a border above when a rule
3651 ;; used to demarcate row groups can be found above. Hence,
3652 ;; finding a rule isn't sufficient to push `above' in BORDERS:
3653 ;; another regular row has to be found above that rule.
3654 (let (rule-flag)
3655 (catch 'exit
3656 (mapc (lambda (row)
3657 (cond ((eq (org-element-property :type row) 'rule)
3658 (setq rule-flag t))
3659 ((not (org-export-table-row-is-special-p row info))
3660 (if rule-flag (throw 'exit (push 'above borders))
3661 (throw 'exit nil)))))
3662 ;; Look at every row before the current one.
3663 (cdr (member row (reverse (org-element-contents table)))))
3664 ;; No rule above, or rule found starts the table (ignoring any
3665 ;; special row): TABLE-CELL is at the top of the table.
3666 (when rule-flag (push 'above borders))
3667 (push 'top borders)))
3668 ;; Bottom/below border? TABLE-CELL has a border below when next
3669 ;; non-regular row below is a rule.
3670 (let (rule-flag)
3671 (catch 'exit
3672 (mapc (lambda (row)
3673 (cond ((eq (org-element-property :type row) 'rule)
3674 (setq rule-flag t))
3675 ((not (org-export-table-row-is-special-p row info))
3676 (if rule-flag (throw 'exit (push 'below borders))
3677 (throw 'exit nil)))))
3678 ;; Look at every row after the current one.
3679 (cdr (member row (org-element-contents table))))
3680 ;; No rule below, or rule found ends the table (modulo some
3681 ;; special row): TABLE-CELL is at the bottom of the table.
3682 (when rule-flag (push 'below borders))
3683 (push 'bottom borders)))
3684 ;; Right/left borders? They can only be specified by column
3685 ;; groups. Column groups are defined in a row starting with "/".
3686 ;; Also a column groups row only contains "<", "<>", ">" or blank
3687 ;; cells.
3688 (catch 'exit
3689 (let ((column (let ((cells (org-element-contents row)))
3690 (- (length cells) (length (member table-cell cells))))))
3691 (mapc
3692 (lambda (row)
3693 (unless (eq (org-element-property :type row) 'rule)
3694 (when (equal (org-element-contents
3695 (car (org-element-contents row)))
3696 '("/"))
3697 (let ((column-groups
3698 (mapcar
3699 (lambda (cell)
3700 (let ((value (org-element-contents cell)))
3701 (when (member value '(("<") ("<>") (">") nil))
3702 (car value))))
3703 (org-element-contents row))))
3704 ;; There's a left border when previous cell, if
3705 ;; any, ends a group, or current one starts one.
3706 (when (or (and (not (zerop column))
3707 (member (elt column-groups (1- column))
3708 '(">" "<>")))
3709 (member (elt column-groups column) '("<" "<>")))
3710 (push 'left borders))
3711 ;; There's a right border when next cell, if any,
3712 ;; starts a group, or current one ends one.
3713 (when (or (and (/= (1+ column) (length column-groups))
3714 (member (elt column-groups (1+ column))
3715 '("<" "<>")))
3716 (member (elt column-groups column) '(">" "<>")))
3717 (push 'right borders))
3718 (throw 'exit nil)))))
3719 ;; Table rows are read in reverse order so last column groups
3720 ;; row has precedence over any previous one.
3721 (reverse (org-element-contents table)))))
3722 ;; Return value.
3723 borders))
3725 (defun org-export-table-cell-starts-colgroup-p (table-cell info)
3726 "Non-nil when TABLE-CELL is at the beginning of a row group.
3727 INFO is a plist used as a communication channel."
3728 ;; A cell starts a column group either when it is at the beginning
3729 ;; of a row (or after the special column, if any) or when it has
3730 ;; a left border.
3731 (or (equal (org-element-map
3732 (org-export-get-parent table-cell)
3733 'table-cell 'identity info 'first-match)
3734 table-cell)
3735 (memq 'left (org-export-table-cell-borders table-cell info))))
3737 (defun org-export-table-cell-ends-colgroup-p (table-cell info)
3738 "Non-nil when TABLE-CELL is at the end of a row group.
3739 INFO is a plist used as a communication channel."
3740 ;; A cell ends a column group either when it is at the end of a row
3741 ;; or when it has a right border.
3742 (or (equal (car (last (org-element-contents
3743 (org-export-get-parent table-cell))))
3744 table-cell)
3745 (memq 'right (org-export-table-cell-borders table-cell info))))
3747 (defun org-export-table-row-starts-rowgroup-p (table-row info)
3748 "Non-nil when TABLE-ROW is at the beginning of a column group.
3749 INFO is a plist used as a communication channel."
3750 (unless (or (eq (org-element-property :type table-row) 'rule)
3751 (org-export-table-row-is-special-p table-row info))
3752 (let ((borders (org-export-table-cell-borders
3753 (car (org-element-contents table-row)) info)))
3754 (or (memq 'top borders) (memq 'above borders)))))
3756 (defun org-export-table-row-ends-rowgroup-p (table-row info)
3757 "Non-nil when TABLE-ROW is at the end of a column group.
3758 INFO is a plist used as a communication channel."
3759 (unless (or (eq (org-element-property :type table-row) 'rule)
3760 (org-export-table-row-is-special-p table-row info))
3761 (let ((borders (org-export-table-cell-borders
3762 (car (org-element-contents table-row)) info)))
3763 (or (memq 'bottom borders) (memq 'below borders)))))
3765 (defun org-export-table-row-starts-header-p (table-row info)
3766 "Non-nil when TABLE-ROW is the first table header's row.
3767 INFO is a plist used as a communication channel."
3768 (and (org-export-table-has-header-p
3769 (org-export-get-parent-table table-row) info)
3770 (org-export-table-row-starts-rowgroup-p table-row info)
3771 (= (org-export-table-row-group table-row info) 1)))
3773 (defun org-export-table-row-ends-header-p (table-row info)
3774 "Non-nil when TABLE-ROW is the last table header's row.
3775 INFO is a plist used as a communication channel."
3776 (and (org-export-table-has-header-p
3777 (org-export-get-parent-table table-row) info)
3778 (org-export-table-row-ends-rowgroup-p table-row info)
3779 (= (org-export-table-row-group table-row info) 1)))
3781 (defun org-export-table-dimensions (table info)
3782 "Return TABLE dimensions.
3784 INFO is a plist used as a communication channel.
3786 Return value is a CONS like (ROWS . COLUMNS) where
3787 ROWS (resp. COLUMNS) is the number of exportable
3788 rows (resp. columns)."
3789 (let (first-row (columns 0) (rows 0))
3790 ;; Set number of rows, and extract first one.
3791 (org-element-map
3792 table 'table-row
3793 (lambda (row)
3794 (when (eq (org-element-property :type row) 'standard)
3795 (incf rows)
3796 (unless first-row (setq first-row row)))) info)
3797 ;; Set number of columns.
3798 (org-element-map first-row 'table-cell (lambda (cell) (incf columns)) info)
3799 ;; Return value.
3800 (cons rows columns)))
3802 (defun org-export-table-cell-address (table-cell info)
3803 "Return address of a regular TABLE-CELL object.
3805 TABLE-CELL is the cell considered. INFO is a plist used as
3806 a communication channel.
3808 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
3809 zero-based index. Only exportable cells are considered. The
3810 function returns nil for other cells."
3811 (let* ((table-row (org-export-get-parent table-cell))
3812 (table (org-export-get-parent-table table-cell)))
3813 ;; Ignore cells in special rows or in special column.
3814 (unless (or (org-export-table-row-is-special-p table-row info)
3815 (and (org-export-table-has-special-column-p table)
3816 (equal (car (org-element-contents table-row)) table-cell)))
3817 (cons
3818 ;; Row number.
3819 (let ((row-count 0))
3820 (org-element-map
3821 table 'table-row
3822 (lambda (row)
3823 (cond ((eq (org-element-property :type row) 'rule) nil)
3824 ((equal row table-row) row-count)
3825 (t (incf row-count) nil)))
3826 info 'first-match))
3827 ;; Column number.
3828 (let ((col-count 0))
3829 (org-element-map
3830 table-row 'table-cell
3831 (lambda (cell)
3832 (if (equal cell table-cell) col-count
3833 (incf col-count) nil))
3834 info 'first-match))))))
3836 (defun org-export-get-table-cell-at (address table info)
3837 "Return regular table-cell object at ADDRESS in TABLE.
3839 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
3840 zero-based index. TABLE is a table type element. INFO is
3841 a plist used as a communication channel.
3843 If no table-cell, among exportable cells, is found at ADDRESS,
3844 return nil."
3845 (let ((column-pos (cdr address)) (column-count 0))
3846 (org-element-map
3847 ;; Row at (car address) or nil.
3848 (let ((row-pos (car address)) (row-count 0))
3849 (org-element-map
3850 table 'table-row
3851 (lambda (row)
3852 (cond ((eq (org-element-property :type row) 'rule) nil)
3853 ((= row-count row-pos) row)
3854 (t (incf row-count) nil)))
3855 info 'first-match))
3856 'table-cell
3857 (lambda (cell)
3858 (if (= column-count column-pos) cell
3859 (incf column-count) nil))
3860 info 'first-match)))
3863 ;;;; For Tables Of Contents
3865 ;; `org-export-collect-headlines' builds a list of all exportable
3866 ;; headline elements, maybe limited to a certain depth. One can then
3867 ;; easily parse it and transcode it.
3869 ;; Building lists of tables, figures or listings is quite similar.
3870 ;; Once the generic function `org-export-collect-elements' is defined,
3871 ;; `org-export-collect-tables', `org-export-collect-figures' and
3872 ;; `org-export-collect-listings' can be derived from it.
3874 (defun org-export-collect-headlines (info &optional n)
3875 "Collect headlines in order to build a table of contents.
3877 INFO is a plist used as a communication channel.
3879 When non-nil, optional argument N must be an integer. It
3880 specifies the depth of the table of contents.
3882 Return a list of all exportable headlines as parsed elements."
3883 (org-element-map
3884 (plist-get info :parse-tree)
3885 'headline
3886 (lambda (headline)
3887 ;; Strip contents from HEADLINE.
3888 (let ((relative-level (org-export-get-relative-level headline info)))
3889 (unless (and n (> relative-level n)) headline)))
3890 info))
3892 (defun org-export-collect-elements (type info &optional predicate)
3893 "Collect referenceable elements of a determined type.
3895 TYPE can be a symbol or a list of symbols specifying element
3896 types to search. Only elements with a caption are collected.
3898 INFO is a plist used as a communication channel.
3900 When non-nil, optional argument PREDICATE is a function accepting
3901 one argument, an element of type TYPE. It returns a non-nil
3902 value when that element should be collected.
3904 Return a list of all elements found, in order of appearance."
3905 (org-element-map
3906 (plist-get info :parse-tree) type
3907 (lambda (element)
3908 (and (org-element-property :caption element)
3909 (or (not predicate) (funcall predicate element))
3910 element))
3911 info))
3913 (defun org-export-collect-tables (info)
3914 "Build a list of tables.
3915 INFO is a plist used as a communication channel.
3917 Return a list of table elements with a caption."
3918 (org-export-collect-elements 'table info))
3920 (defun org-export-collect-figures (info predicate)
3921 "Build a list of figures.
3923 INFO is a plist used as a communication channel. PREDICATE is
3924 a function which accepts one argument: a paragraph element and
3925 whose return value is non-nil when that element should be
3926 collected.
3928 A figure is a paragraph type element, with a caption, verifying
3929 PREDICATE. The latter has to be provided since a \"figure\" is
3930 a vague concept that may depend on back-end.
3932 Return a list of elements recognized as figures."
3933 (org-export-collect-elements 'paragraph info predicate))
3935 (defun org-export-collect-listings (info)
3936 "Build a list of src blocks.
3938 INFO is a plist used as a communication channel.
3940 Return a list of src-block elements with a caption."
3941 (org-export-collect-elements 'src-block info))
3944 ;;;; Topology
3946 ;; Here are various functions to retrieve information about the
3947 ;; neighbourhood of a given element or object. Neighbours of interest
3948 ;; are direct parent (`org-export-get-parent'), parent headline
3949 ;; (`org-export-get-parent-headline'), first element containing an
3950 ;; object, (`org-export-get-parent-element'), parent table
3951 ;; (`org-export-get-parent-table'), previous element or object
3952 ;; (`org-export-get-previous-element') and next element or object
3953 ;; (`org-export-get-next-element').
3955 ;; `org-export-get-genealogy' returns the full genealogy of a given
3956 ;; element or object, from closest parent to full parse tree.
3958 (defun org-export-get-parent (blob)
3959 "Return BLOB parent or nil.
3960 BLOB is the element or object considered."
3961 (org-element-property :parent blob))
3963 (defun org-export-get-genealogy (blob)
3964 "Return full genealogy relative to a given element or object.
3965 BLOB is the element or object being considered."
3966 (let (genealogy (parent blob))
3967 (while (setq parent (org-element-property :parent parent))
3968 (push parent genealogy))
3969 (nreverse genealogy)))
3971 (defun org-export-get-parent-headline (blob)
3972 "Return BLOB parent headline or nil.
3973 BLOB is the element or object being considered."
3974 (let ((parent blob))
3975 (while (and (setq parent (org-element-property :parent parent))
3976 (not (eq (org-element-type parent) 'headline))))
3977 parent))
3979 (defun org-export-get-parent-element (object)
3980 "Return first element containing OBJECT or nil.
3981 OBJECT is the object to consider."
3982 (let ((parent object))
3983 (while (and (setq parent (org-element-property :parent parent))
3984 (memq (org-element-type parent) org-element-all-objects)))
3985 parent))
3987 (defun org-export-get-parent-table (object)
3988 "Return OBJECT parent table or nil.
3989 OBJECT is either a `table-cell' or `table-element' type object."
3990 (let ((parent object))
3991 (while (and (setq parent (org-element-property :parent parent))
3992 (not (eq (org-element-type parent) 'table))))
3993 parent))
3995 (defun org-export-get-previous-element (blob)
3996 "Return previous element or object.
3997 BLOB is an element or object. Return previous element or object,
3998 a string, or nil."
3999 (let ((parent (org-export-get-parent blob)))
4000 (cadr (member blob (reverse (org-element-contents parent))))))
4002 (defun org-export-get-next-element (blob)
4003 "Return next element or object.
4004 BLOB is an element or object. Return next element or object,
4005 a string, or nil."
4006 (let ((parent (org-export-get-parent blob)))
4007 (cadr (member blob (org-element-contents parent)))))
4011 ;;; The Dispatcher
4013 ;; `org-export-dispatch' is the standard interactive way to start an
4014 ;; export process. It uses `org-export-dispatch-ui' as a subroutine
4015 ;; for its interface. Most commons back-ends should have an entry in
4016 ;; it.
4018 (defun org-export-dispatch ()
4019 "Export dispatcher for Org mode.
4021 It provides an access to common export related tasks in a buffer.
4022 Its interface comes in two flavours: standard and expert. While
4023 both share the same set of bindings, only the former displays the
4024 valid keys associations. Set `org-export-dispatch-use-expert-ui'
4025 to switch to one or the other.
4027 Return an error if key pressed has no associated command."
4028 (interactive)
4029 (let* ((input (org-export-dispatch-ui
4030 (if (listp org-export-initial-scope) org-export-initial-scope
4031 (list org-export-initial-scope))
4032 org-export-dispatch-use-expert-ui))
4033 (raw-key (car input))
4034 (optns (cdr input)))
4035 ;; Translate "C-a", "C-b"... into "a", "b"... Then take action
4036 ;; depending on user's key pressed.
4037 (case (if (< raw-key 27) (+ raw-key 96) raw-key)
4038 ;; Allow to quit with "q" key.
4039 (?q nil)
4040 ;; Export with `e-ascii' back-end.
4041 ((?A ?N ?U)
4042 (let ((outbuf
4043 (org-export-to-buffer
4044 'e-ascii "*Org E-ASCII Export*"
4045 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)
4046 `(:ascii-charset
4047 ,(case raw-key (?A 'ascii) (?N 'latin1) (t 'utf-8))))))
4048 (with-current-buffer outbuf (text-mode))
4049 (when org-export-show-temporary-export-buffer
4050 (switch-to-buffer-other-window outbuf))))
4051 ((?a ?n ?u)
4052 (org-e-ascii-export-to-ascii
4053 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)
4054 `(:ascii-charset ,(case raw-key (?a 'ascii) (?n 'latin1) (t 'utf-8)))))
4055 ;; Export with `e-latex' back-end.
4057 (let ((outbuf
4058 (org-export-to-buffer
4059 'e-latex "*Org E-LaTeX Export*"
4060 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))))
4061 (with-current-buffer outbuf (latex-mode))
4062 (when org-export-show-temporary-export-buffer
4063 (switch-to-buffer-other-window outbuf))))
4065 (org-e-latex-export-to-latex
4066 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
4068 (org-e-latex-export-to-pdf
4069 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
4071 (org-open-file
4072 (org-e-latex-export-to-pdf
4073 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))))
4074 ;; Export with `e-html' back-end.
4076 (let ((outbuf
4077 (org-export-to-buffer
4078 'e-html "*Org E-HTML Export*"
4079 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))))
4080 ;; set major mode
4081 (with-current-buffer outbuf (nxml-mode))
4082 (when org-export-show-temporary-export-buffer
4083 (switch-to-buffer-other-window outbuf))))
4085 (org-e-html-export-to-html
4086 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
4088 (org-open-file
4089 (org-e-html-export-to-html
4090 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))))
4091 ;; Export with `e-odt' back-end.
4093 (org-e-odt-export-to-odt
4094 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
4096 (org-open-file
4097 (org-e-odt-export-to-odt
4098 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))))
4099 ;; Publishing facilities
4101 (org-e-publish-current-file (memq 'force optns)))
4103 (org-e-publish-current-project (memq 'force optns)))
4105 (let ((project
4106 (assoc (org-icompleting-read
4107 "Publish project: " org-e-publish-project-alist nil t)
4108 org-e-publish-project-alist)))
4109 (org-e-publish project (memq 'force optns))))
4111 (org-e-publish-all (memq 'force optns)))
4112 ;; Undefined command.
4113 (t (error "No command associated with key %s"
4114 (char-to-string raw-key))))))
4116 (defun org-export-dispatch-ui (options expertp)
4117 "Handle interface for `org-export-dispatch'.
4119 OPTIONS is a list containing current interactive options set for
4120 export. It can contain any of the following symbols:
4121 `body' toggles a body-only export
4122 `subtree' restricts export to current subtree
4123 `visible' restricts export to visible part of buffer.
4124 `force' force publishing files.
4126 EXPERTP, when non-nil, triggers expert UI. In that case, no help
4127 buffer is provided, but indications about currently active
4128 options are given in the prompt. Moreover, \[?] allows to switch
4129 back to standard interface.
4131 Return value is a list with key pressed as CAR and a list of
4132 final interactive export options as CDR."
4133 (let ((help
4134 (format "---- (Options) -------------------------------------------
4136 \[1] Body only: %s [2] Export scope: %s
4137 \[3] Visible only: %s [4] Force publishing: %s
4140 --- (ASCII/Latin-1/UTF-8 Export) -------------------------
4142 \[a/n/u] to TXT file [A/N/U] to temporary buffer
4144 --- (HTML Export) ----------------------------------------
4146 \[h] to HTML file [b] ... and open it
4147 \[H] to temporary buffer
4149 --- (LaTeX Export) ---------------------------------------
4151 \[l] to TEX file [L] to temporary buffer
4152 \[p] to PDF file [d] ... and open it
4154 --- (ODF Export) -----------------------------------------
4156 \[o] to ODT file [O] ... and open it
4158 --- (Publish) --------------------------------------------
4160 \[F] current file [P] current project
4161 \[X] a project [E] every project"
4162 (if (memq 'body options) "On " "Off")
4163 (if (memq 'subtree options) "Subtree" "Buffer ")
4164 (if (memq 'visible options) "On " "Off")
4165 (if (memq 'force options) "On " "Off")))
4166 (standard-prompt "Export command: ")
4167 (expert-prompt (format "Export command (%s%s%s%s): "
4168 (if (memq 'body options) "b" "-")
4169 (if (memq 'subtree options) "s" "-")
4170 (if (memq 'visible options) "v" "-")
4171 (if (memq 'force options) "f" "-")))
4172 (handle-keypress
4173 (function
4174 ;; Read a character from command input, toggling interactive
4175 ;; options when applicable. PROMPT is the displayed prompt,
4176 ;; as a string.
4177 (lambda (prompt)
4178 (let ((key (read-char-exclusive prompt)))
4179 (cond
4180 ;; Ignore non-standard characters (i.e. "M-a").
4181 ((not (characterp key)) (org-export-dispatch-ui options expertp))
4182 ;; Help key: Switch back to standard interface if
4183 ;; expert UI was active.
4184 ((eq key ??) (org-export-dispatch-ui options nil))
4185 ;; Toggle export options.
4186 ((memq key '(?1 ?2 ?3 ?4))
4187 (org-export-dispatch-ui
4188 (let ((option (case key (?1 'body) (?2 'subtree) (?3 'visible)
4189 (?4 'force))))
4190 (if (memq option options) (remq option options)
4191 (cons option options)))
4192 expertp))
4193 ;; Action selected: Send key and options back to
4194 ;; `org-export-dispatch'.
4195 (t (cons key options))))))))
4196 ;; With expert UI, just read key with a fancy prompt. In standard
4197 ;; UI, display an intrusive help buffer.
4198 (if expertp (funcall handle-keypress expert-prompt)
4199 (save-window-excursion
4200 (delete-other-windows)
4201 (with-output-to-temp-buffer "*Org Export/Publishing Help*" (princ help))
4202 (org-fit-window-to-buffer
4203 (get-buffer-window "*Org Export/Publishing Help*"))
4204 (funcall handle-keypress standard-prompt)))))
4207 (provide 'org-export)
4208 ;;; org-export.el ends here