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