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