org-export: Fix code comments
[org-mode.git] / contrib / lisp / org-export.el
blob3ca7b5de7a067b0b0f55d07a6a09542b71b94c16
1 ;;; org-export.el --- Generic Export Engine For Org
3 ;; Copyright (C) 2012 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
21 ;;; Commentary:
23 ;; This library implements a generic export engine for Org, built on
24 ;; its syntactical parser: Org Elements.
26 ;; Besides that parser, the generic exporter is made of three distinct
27 ;; parts:
29 ;; - The communication channel consists in a property list, which is
30 ;; created and updated during the process. Its use is to offer
31 ;; every piece of information, would it be about initial environment
32 ;; or contextual data, all in a single place. The exhaustive list
33 ;; of properties is given in "The Communication Channel" section of
34 ;; this file.
36 ;; - The transcoder walks the parse tree, ignores or treat as plain
37 ;; text elements and objects according to export options, and
38 ;; eventually calls back-end specific functions to do the real
39 ;; transcoding, concatenating their return value along the way.
41 ;; - The filter system is activated at the very beginning and the very
42 ;; end of the export process, and each time an element or an object
43 ;; has been converted. It is the entry point to fine-tune standard
44 ;; output from back-end transcoders. See "The Filter System"
45 ;; section for more information.
47 ;; The core function is `org-export-as'. It returns the transcoded
48 ;; buffer as a string.
50 ;; An export back-end is defined with `org-export-define-backend',
51 ;; which sets one mandatory variable: his translation table. Its name
52 ;; is always `org-BACKEND-translate-alist' where BACKEND stands for
53 ;; the name chosen for the back-end. Its value is an alist whose keys
54 ;; are elements and objects types and values translator functions.
55 ;; See function's docstring for more information about translators.
57 ;; Optionally, `org-export-define-backend' can also support specific
58 ;; buffer keywords, OPTION keyword's items and filters. Also refer to
59 ;; function documentation for more information.
61 ;; If the new back-end shares most properties with another one,
62 ;; `org-export-define-derived-backend' can be used to simplify the
63 ;; process.
65 ;; Any back-end can define its own variables. Among them, those
66 ;; customizable should belong to the `org-export-BACKEND' group.
68 ;; Tools for common tasks across back-ends are implemented in the
69 ;; penultimate part of this file. A dispatcher for standard back-ends
70 ;; is provided in the last one.
72 ;;; Code:
74 (eval-when-compile (require 'cl))
75 (require 'org-element)
78 (declare-function org-e-ascii-export-as-ascii "org-e-ascii"
79 (&optional subtreep visible-only body-only ext-plist))
80 (declare-function org-e-ascii-export-to-ascii "org-e-ascii"
81 (&optional subtreep visible-only body-only ext-plist pub-dir))
82 (declare-function org-e-html-export-as-html "org-e-html"
83 (&optional subtreep visible-only body-only ext-plist))
84 (declare-function org-e-html-export-to-html "org-e-html"
85 (&optional subtreep visible-only body-only ext-plist pub-dir))
86 (declare-function org-e-latex-export-as-latex "org-e-latex"
87 (&optional subtreep visible-only body-only ext-plist))
88 (declare-function org-e-latex-export-to-latex "org-e-latex"
89 (&optional subtreep visible-only body-only ext-plist pub-dir))
90 (declare-function org-e-latex-export-to-pdf "org-e-latex"
91 (&optional subtreep visible-only body-only ext-plist pub-dir))
92 (declare-function org-e-odt-export-to-odt "org-e-odt"
93 (&optional subtreep visible-only body-only ext-plist pub-dir))
94 (declare-function org-e-publish "org-e-publish" (project &optional force))
95 (declare-function org-e-publish-all "org-e-publish" (&optional force))
96 (declare-function org-e-publish-current-file "org-e-publish" (&optional force))
97 (declare-function org-e-publish-current-project "org-e-publish"
98 (&optional force))
99 (declare-function org-export-blocks-preprocess "org-exp-blocks")
101 (defvar org-e-publish-project-alist)
102 (defvar org-table-number-fraction)
103 (defvar org-table-number-regexp)
107 ;;; Internal Variables
109 ;; Among internal variables, the most important is
110 ;; `org-export-options-alist'. This variable define the global export
111 ;; options, shared between every exporter, and how they are acquired.
113 (defconst org-export-max-depth 19
114 "Maximum nesting depth for headlines, counting from 0.")
116 (defconst org-export-options-alist
117 '((:author "AUTHOR" nil user-full-name t)
118 (:creator "CREATOR" nil org-export-creator-string)
119 (:date "DATE" nil nil t)
120 (:description "DESCRIPTION" nil nil newline)
121 (:email "EMAIL" nil user-mail-address t)
122 (:exclude-tags "EXCLUDE_TAGS" nil org-export-exclude-tags split)
123 (:headline-levels nil "H" org-export-headline-levels)
124 (:keywords "KEYWORDS" nil nil space)
125 (:language "LANGUAGE" nil org-export-default-language t)
126 (:preserve-breaks nil "\\n" org-export-preserve-breaks)
127 (:section-numbers nil "num" org-export-with-section-numbers)
128 (:select-tags "SELECT_TAGS" nil org-export-select-tags split)
129 (:time-stamp-file nil "timestamp" org-export-time-stamp-file)
130 (:title "TITLE" nil nil space)
131 (:with-archived-trees nil "arch" org-export-with-archived-trees)
132 (:with-author nil "author" org-export-with-author)
133 (:with-clocks nil "c" org-export-with-clocks)
134 (:with-creator nil "creator" org-export-with-creator)
135 (:with-drawers nil "d" org-export-with-drawers)
136 (:with-email nil "email" org-export-with-email)
137 (:with-emphasize nil "*" org-export-with-emphasize)
138 (:with-entities nil "e" org-export-with-entities)
139 (:with-fixed-width nil ":" org-export-with-fixed-width)
140 (:with-footnotes nil "f" org-export-with-footnotes)
141 (:with-inlinetasks nil "inline" org-export-with-inlinetasks)
142 (:with-plannings nil "p" org-export-with-planning)
143 (:with-priority nil "pri" org-export-with-priority)
144 (:with-special-strings nil "-" org-export-with-special-strings)
145 (:with-sub-superscript nil "^" org-export-with-sub-superscripts)
146 (:with-toc nil "toc" org-export-with-toc)
147 (:with-tables nil "|" org-export-with-tables)
148 (:with-tags nil "tags" org-export-with-tags)
149 (:with-tasks nil "tasks" org-export-with-tasks)
150 (:with-timestamps nil "<" org-export-with-timestamps)
151 (:with-todo-keywords nil "todo" org-export-with-todo-keywords))
152 "Alist between export properties and ways to set them.
154 The CAR of the alist is the property name, and the CDR is a list
155 like (KEYWORD OPTION DEFAULT BEHAVIOUR) where:
157 KEYWORD is a string representing a buffer keyword, or nil. Each
158 property defined this way can also be set, during subtree
159 export, through an headline property named after the keyword
160 with the \"EXPORT_\" prefix (i.e. DATE keyword and EXPORT_DATE
161 property).
162 OPTION is a string that could be found in an #+OPTIONS: line.
163 DEFAULT is the default value for the property.
164 BEHAVIOUR determine how Org should handle multiple keywords for
165 the same property. It is a symbol among:
166 nil Keep old value and discard the new one.
167 t Replace old value with the new one.
168 `space' Concatenate the values, separating them with a space.
169 `newline' Concatenate the values, separating them with
170 a newline.
171 `split' Split values at white spaces, and cons them to the
172 previous list.
174 KEYWORD and OPTION have precedence over DEFAULT.
176 All these properties should be back-end agnostic. Back-end
177 specific properties are set through `org-export-define-backend'.
178 Properties redefined there have precedence over these.")
180 (defconst org-export-special-keywords
181 '("SETUP_FILE" "OPTIONS" "MACRO")
182 "List of in-buffer keywords that require special treatment.
183 These keywords are not directly associated to a property. The
184 way they are handled must be hard-coded into
185 `org-export--get-inbuffer-options' function.")
187 (defconst org-export-filters-alist
188 '((:filter-bold . org-export-filter-bold-functions)
189 (:filter-babel-call . org-export-filter-babel-call-functions)
190 (:filter-center-block . org-export-filter-center-block-functions)
191 (:filter-clock . org-export-filter-clock-functions)
192 (:filter-code . org-export-filter-code-functions)
193 (:filter-comment . org-export-filter-comment-functions)
194 (:filter-comment-block . org-export-filter-comment-block-functions)
195 (:filter-drawer . org-export-filter-drawer-functions)
196 (:filter-dynamic-block . org-export-filter-dynamic-block-functions)
197 (:filter-entity . org-export-filter-entity-functions)
198 (:filter-example-block . org-export-filter-example-block-functions)
199 (:filter-export-block . org-export-filter-export-block-functions)
200 (:filter-export-snippet . org-export-filter-export-snippet-functions)
201 (:filter-final-output . org-export-filter-final-output-functions)
202 (:filter-fixed-width . org-export-filter-fixed-width-functions)
203 (:filter-footnote-definition . org-export-filter-footnote-definition-functions)
204 (:filter-footnote-reference . org-export-filter-footnote-reference-functions)
205 (:filter-headline . org-export-filter-headline-functions)
206 (:filter-horizontal-rule . org-export-filter-horizontal-rule-functions)
207 (:filter-inline-babel-call . org-export-filter-inline-babel-call-functions)
208 (:filter-inline-src-block . org-export-filter-inline-src-block-functions)
209 (:filter-inlinetask . org-export-filter-inlinetask-functions)
210 (:filter-italic . org-export-filter-italic-functions)
211 (:filter-item . org-export-filter-item-functions)
212 (:filter-keyword . org-export-filter-keyword-functions)
213 (:filter-latex-environment . org-export-filter-latex-environment-functions)
214 (:filter-latex-fragment . org-export-filter-latex-fragment-functions)
215 (:filter-line-break . org-export-filter-line-break-functions)
216 (:filter-link . org-export-filter-link-functions)
217 (:filter-macro . org-export-filter-macro-functions)
218 (:filter-paragraph . org-export-filter-paragraph-functions)
219 (:filter-parse-tree . org-export-filter-parse-tree-functions)
220 (:filter-plain-list . org-export-filter-plain-list-functions)
221 (:filter-plain-text . org-export-filter-plain-text-functions)
222 (:filter-planning . org-export-filter-planning-functions)
223 (:filter-property-drawer . org-export-filter-property-drawer-functions)
224 (:filter-quote-block . org-export-filter-quote-block-functions)
225 (:filter-quote-section . org-export-filter-quote-section-functions)
226 (:filter-radio-target . org-export-filter-radio-target-functions)
227 (:filter-section . org-export-filter-section-functions)
228 (:filter-special-block . org-export-filter-special-block-functions)
229 (:filter-src-block . org-export-filter-src-block-functions)
230 (:filter-statistics-cookie . org-export-filter-statistics-cookie-functions)
231 (:filter-strike-through . org-export-filter-strike-through-functions)
232 (:filter-subscript . org-export-filter-subscript-functions)
233 (:filter-superscript . org-export-filter-superscript-functions)
234 (:filter-table . org-export-filter-table-functions)
235 (:filter-table-cell . org-export-filter-table-cell-functions)
236 (:filter-table-row . org-export-filter-table-row-functions)
237 (:filter-target . org-export-filter-target-functions)
238 (:filter-timestamp . org-export-filter-timestamp-functions)
239 (:filter-underline . org-export-filter-underline-functions)
240 (:filter-verbatim . org-export-filter-verbatim-functions)
241 (:filter-verse-block . org-export-filter-verse-block-functions))
242 "Alist between filters properties and initial values.
244 The key of each association is a property name accessible through
245 the communication channel. Its value is a configurable global
246 variable defining initial filters.
248 This list is meant to install user specified filters. Back-end
249 developers may install their own filters using
250 `org-export-define-backend'. Filters defined there will always
251 be prepended to the current list, so they always get applied
252 first.")
254 (defconst org-export-default-inline-image-rule
255 `(("file" .
256 ,(format "\\.%s\\'"
257 (regexp-opt
258 '("png" "jpeg" "jpg" "gif" "tiff" "tif" "xbm"
259 "xpm" "pbm" "pgm" "ppm") t))))
260 "Default rule for link matching an inline image.
261 This rule applies to links with no description. By default, it
262 will be considered as an inline image if it targets a local file
263 whose extension is either \"png\", \"jpeg\", \"jpg\", \"gif\",
264 \"tiff\", \"tif\", \"xbm\", \"xpm\", \"pbm\", \"pgm\" or \"ppm\".
265 See `org-export-inline-image-p' for more information about
266 rules.")
270 ;;; User-configurable Variables
272 ;; Configuration for the masses.
274 ;; They should never be accessed directly, as their value is to be
275 ;; stored in a property list (cf. `org-export-options-alist').
276 ;; Back-ends will read their value from there instead.
278 (defgroup org-export nil
279 "Options for exporting Org mode files."
280 :tag "Org Export"
281 :group 'org)
283 (defgroup org-export-general nil
284 "General options for export engine."
285 :tag "Org Export General"
286 :group 'org-export)
288 (defcustom org-export-with-archived-trees 'headline
289 "Whether sub-trees with the ARCHIVE tag should be exported.
291 This can have three different values:
292 nil Do not export, pretend this tree is not present.
293 t Do export the entire tree.
294 `headline' Only export the headline, but skip the tree below it.
296 This option can also be set with the #+OPTIONS line,
297 e.g. \"arch:nil\"."
298 :group 'org-export-general
299 :type '(choice
300 (const :tag "Not at all" nil)
301 (const :tag "Headline only" 'headline)
302 (const :tag "Entirely" t)))
304 (defcustom org-export-with-author t
305 "Non-nil means insert author name into the exported file.
306 This option can also be set with the #+OPTIONS line,
307 e.g. \"author:nil\"."
308 :group 'org-export-general
309 :type 'boolean)
311 (defcustom org-export-with-clocks nil
312 "Non-nil means export CLOCK keywords.
313 This option can also be set with the #+OPTIONS line,
314 e.g. \"c:t\"."
315 :group 'org-export-general
316 :type 'boolean)
318 (defcustom org-export-with-creator 'comment
319 "Non-nil means the postamble should contain a creator sentence.
321 The sentence can be set in `org-export-creator-string' and
322 defaults to \"Generated by Org mode XX in Emacs XXX.\".
324 If the value is `comment' insert it as a comment."
325 :group 'org-export-general
326 :type '(choice
327 (const :tag "No creator sentence" nil)
328 (const :tag "Sentence as a comment" 'comment)
329 (const :tag "Insert the sentence" t)))
331 (defcustom org-export-creator-string
332 (format "Generated by Org mode %s in Emacs %s."
333 (if (fboundp 'org-version) (org-version) "(Unknown)")
334 emacs-version)
335 "String to insert at the end of the generated document."
336 :group 'org-export-general
337 :type '(string :tag "Creator string"))
339 (defcustom org-export-with-drawers t
340 "Non-nil means export contents of standard drawers.
342 When t, all drawers are exported. This may also be a list of
343 drawer names to export. This variable doesn't apply to
344 properties drawers.
346 This option can also be set with the #+OPTIONS line,
347 e.g. \"d:nil\"."
348 :group 'org-export-general
349 :type '(choice
350 (const :tag "All drawers" t)
351 (const :tag "None" nil)
352 (repeat :tag "Selected drawers"
353 (string :tag "Drawer name"))))
355 (defcustom org-export-with-email nil
356 "Non-nil means insert author email into the exported file.
357 This option can also be set with the #+OPTIONS line,
358 e.g. \"email:t\"."
359 :group 'org-export-general
360 :type 'boolean)
362 (defcustom org-export-with-emphasize t
363 "Non-nil means interpret *word*, /word/, and _word_ as emphasized text.
365 If the export target supports emphasizing text, the word will be
366 typeset in bold, italic, or underlined, respectively. Not all
367 export backends support this.
369 This option can also be set with the #+OPTIONS line, e.g. \"*:nil\"."
370 :group 'org-export-general
371 :type 'boolean)
373 (defcustom org-export-exclude-tags '("noexport")
374 "Tags that exclude a tree from export.
376 All trees carrying any of these tags will be excluded from
377 export. This is without condition, so even subtrees inside that
378 carry one of the `org-export-select-tags' will be removed.
380 This option can also be set with the #+EXCLUDE_TAGS: keyword."
381 :group 'org-export-general
382 :type '(repeat (string :tag "Tag")))
384 (defcustom org-export-with-fixed-width t
385 "Non-nil means lines starting with \":\" will be in fixed width font.
387 This can be used to have pre-formatted text, fragments of code
388 etc. For example:
389 : ;; Some Lisp examples
390 : (while (defc cnt)
391 : (ding))
392 will be looking just like this in also HTML. See also the QUOTE
393 keyword. Not all export backends support this.
395 This option can also be set with the #+OPTIONS line, e.g. \"::nil\"."
396 :group 'org-export-translation
397 :type 'boolean)
399 (defcustom org-export-with-footnotes t
400 "Non-nil means Org footnotes should be exported.
401 This option can also be set with the #+OPTIONS line,
402 e.g. \"f:nil\"."
403 :group 'org-export-general
404 :type 'boolean)
406 (defcustom org-export-headline-levels 3
407 "The last level which is still exported as a headline.
409 Inferior levels will produce itemize lists when exported.
411 This option can also be set with the #+OPTIONS line, e.g. \"H:2\"."
412 :group 'org-export-general
413 :type 'integer)
415 (defcustom org-export-default-language "en"
416 "The default language for export and clocktable translations, as a string.
417 This may have an association in
418 `org-clock-clocktable-language-setup'."
419 :group 'org-export-general
420 :type '(string :tag "Language"))
422 (defcustom org-export-preserve-breaks nil
423 "Non-nil means preserve all line breaks when exporting.
425 Normally, in HTML output paragraphs will be reformatted.
427 This option can also be set with the #+OPTIONS line,
428 e.g. \"\\n:t\"."
429 :group 'org-export-general
430 :type 'boolean)
432 (defcustom org-export-with-entities t
433 "Non-nil means interpret entities when exporting.
435 For example, HTML export converts \\alpha to &alpha; and \\AA to
436 &Aring;.
438 For a list of supported names, see the constant `org-entities'
439 and the user option `org-entities-user'.
441 This option can also be set with the #+OPTIONS line,
442 e.g. \"e:nil\"."
443 :group 'org-export-general
444 :type 'boolean)
446 (defcustom org-export-with-inlinetasks t
447 "Non-nil means inlinetasks should be exported.
448 This option can also be set with the #+OPTIONS line,
449 e.g. \"inline:nil\"."
450 :group 'org-export-general
451 :type 'boolean)
453 (defcustom org-export-with-planning nil
454 "Non-nil means include planning info in export.
455 This option can also be set with the #+OPTIONS: line,
456 e.g. \"p:t\"."
457 :group 'org-export-general
458 :type 'boolean)
460 (defcustom org-export-with-priority nil
461 "Non-nil means include priority cookies in export.
462 This option can also be set with the #+OPTIONS line,
463 e.g. \"pri:t\"."
464 :group 'org-export-general
465 :type 'boolean)
467 (defcustom org-export-with-section-numbers t
468 "Non-nil means add section numbers to headlines when exporting.
470 When set to an integer n, numbering will only happen for
471 headlines whose relative level is higher or equal to n.
473 This option can also be set with the #+OPTIONS line,
474 e.g. \"num:t\"."
475 :group 'org-export-general
476 :type 'boolean)
478 (defcustom org-export-select-tags '("export")
479 "Tags that select a tree for export.
481 If any such tag is found in a buffer, all trees that do not carry
482 one of these tags will be ignored during export. Inside trees
483 that are selected like this, you can still deselect a subtree by
484 tagging it with one of the `org-export-exclude-tags'.
486 This option can also be set with the #+SELECT_TAGS: keyword."
487 :group 'org-export-general
488 :type '(repeat (string :tag "Tag")))
490 (defcustom org-export-with-special-strings t
491 "Non-nil means interpret \"\-\", \"--\" and \"---\" for export.
493 When this option is turned on, these strings will be exported as:
495 Org HTML LaTeX
496 -----+----------+--------
497 \\- &shy; \\-
498 -- &ndash; --
499 --- &mdash; ---
500 ... &hellip; \ldots
502 This option can also be set with the #+OPTIONS line,
503 e.g. \"-:nil\"."
504 :group 'org-export-general
505 :type 'boolean)
507 (defcustom org-export-with-sub-superscripts t
508 "Non-nil means interpret \"_\" and \"^\" for export.
510 When this option is turned on, you can use TeX-like syntax for
511 sub- and superscripts. Several characters after \"_\" or \"^\"
512 will be considered as a single item - so grouping with {} is
513 normally not needed. For example, the following things will be
514 parsed as single sub- or superscripts.
516 10^24 or 10^tau several digits will be considered 1 item.
517 10^-12 or 10^-tau a leading sign with digits or a word
518 x^2-y^3 will be read as x^2 - y^3, because items are
519 terminated by almost any nonword/nondigit char.
520 x_{i^2} or x^(2-i) braces or parenthesis do grouping.
522 Still, ambiguity is possible - so when in doubt use {} to enclose
523 the sub/superscript. If you set this variable to the symbol
524 `{}', the braces are *required* in order to trigger
525 interpretations as sub/superscript. This can be helpful in
526 documents that need \"_\" frequently in plain text.
528 This option can also be set with the #+OPTIONS line,
529 e.g. \"^:nil\"."
530 :group 'org-export-general
531 :type '(choice
532 (const :tag "Interpret them" t)
533 (const :tag "Curly brackets only" {})
534 (const :tag "Do not interpret them" nil)))
536 (defcustom org-export-with-toc t
537 "Non-nil means create a table of contents in exported files.
539 The TOC contains headlines with levels up
540 to`org-export-headline-levels'. When an integer, include levels
541 up to N in the toc, this may then be different from
542 `org-export-headline-levels', but it will not be allowed to be
543 larger than the number of headline levels. When nil, no table of
544 contents is made.
546 This option can also be set with the #+OPTIONS line,
547 e.g. \"toc:nil\" or \"toc:3\"."
548 :group 'org-export-general
549 :type '(choice
550 (const :tag "No Table of Contents" nil)
551 (const :tag "Full Table of Contents" t)
552 (integer :tag "TOC to level")))
554 (defcustom org-export-with-tables t
555 "If non-nil, lines starting with \"|\" define a table.
556 For example:
558 | Name | Address | Birthday |
559 |-------------+----------+-----------|
560 | Arthur Dent | England | 29.2.2100 |
562 This option can also be set with the #+OPTIONS line, e.g. \"|:nil\"."
563 :group 'org-export-general
564 :type 'boolean)
566 (defcustom org-export-with-tags t
567 "If nil, do not export tags, just remove them from headlines.
569 If this is the symbol `not-in-toc', tags will be removed from
570 table of contents entries, but still be shown in the headlines of
571 the document.
573 This option can also be set with the #+OPTIONS line,
574 e.g. \"tags:nil\"."
575 :group 'org-export-general
576 :type '(choice
577 (const :tag "Off" nil)
578 (const :tag "Not in TOC" not-in-toc)
579 (const :tag "On" t)))
581 (defcustom org-export-with-tasks t
582 "Non-nil means include TODO items for export.
583 This may have the following values:
584 t include tasks independent of state.
585 todo include only tasks that are not yet done.
586 done include only tasks that are already done.
587 nil remove all tasks before export
588 list of keywords keep only tasks with these keywords"
589 :group 'org-export-general
590 :type '(choice
591 (const :tag "All tasks" t)
592 (const :tag "No tasks" nil)
593 (const :tag "Not-done tasks" todo)
594 (const :tag "Only done tasks" done)
595 (repeat :tag "Specific TODO keywords"
596 (string :tag "Keyword"))))
598 (defcustom org-export-time-stamp-file t
599 "Non-nil means insert a time stamp into the exported file.
600 The time stamp shows when the file was created.
602 This option can also be set with the #+OPTIONS line,
603 e.g. \"timestamp:nil\"."
604 :group 'org-export-general
605 :type 'boolean)
607 (defcustom org-export-with-timestamps t
608 "Non nil means allow timestamps in export.
610 It can be set to `active', `inactive', t or nil, in order to
611 export, respectively, only active timestamps, only inactive ones,
612 all of them or none.
614 This option can also be set with the #+OPTIONS line, e.g.
615 \"<:nil\"."
616 :group 'org-export-general
617 :type '(choice
618 (const :tag "All timestamps" t)
619 (const :tag "Only active timestamps" active)
620 (const :tag "Only inactive timestamps" inactive)
621 (const :tag "No timestamp" nil)))
623 (defcustom org-export-with-todo-keywords t
624 "Non-nil means include TODO keywords in export.
625 When nil, remove all these keywords from the export."
626 :group 'org-export-general
627 :type 'boolean)
629 (defcustom org-export-allow-BIND 'confirm
630 "Non-nil means allow #+BIND to define local variable values for export.
631 This is a potential security risk, which is why the user must
632 confirm the use of these lines."
633 :group 'org-export-general
634 :type '(choice
635 (const :tag "Never" nil)
636 (const :tag "Always" t)
637 (const :tag "Ask a confirmation for each file" confirm)))
639 (defcustom org-export-snippet-translation-alist nil
640 "Alist between export snippets back-ends and exporter back-ends.
642 This variable allows to provide shortcuts for export snippets.
644 For example, with a value of '\(\(\"h\" . \"e-html\"\)\), the
645 HTML back-end will recognize the contents of \"@@h:<b>@@\" as
646 HTML code while every other back-end will ignore it."
647 :group 'org-export-general
648 :type '(repeat
649 (cons
650 (string :tag "Shortcut")
651 (string :tag "Back-end"))))
653 (defcustom org-export-coding-system nil
654 "Coding system for the exported file."
655 :group 'org-export-general
656 :type 'coding-system)
658 (defcustom org-export-copy-to-kill-ring t
659 "Non-nil means exported stuff will also be pushed onto the kill ring."
660 :group 'org-export-general
661 :type 'boolean)
663 (defcustom org-export-initial-scope 'buffer
664 "The initial scope when exporting with `org-export-dispatch'.
665 This variable can be either set to `buffer' or `subtree'."
666 :group 'org-export-general
667 :type '(choice
668 (const :tag "Export current buffer" 'buffer)
669 (const :tag "Export current subtree" 'subtree)))
671 (defcustom org-export-show-temporary-export-buffer t
672 "Non-nil means show buffer after exporting to temp buffer.
673 When Org exports to a file, the buffer visiting that file is ever
674 shown, but remains buried. However, when exporting to
675 a temporary buffer, that buffer is popped up in a second window.
676 When this variable is nil, the buffer remains buried also in
677 these cases."
678 :group 'org-export-general
679 :type 'boolean)
681 (defcustom org-export-dispatch-use-expert-ui nil
682 "Non-nil means using a non-intrusive `org-export-dispatch'.
683 In that case, no help buffer is displayed. Though, an indicator
684 for current export scope is added to the prompt \(i.e. \"b\" when
685 output is restricted to body only, \"s\" when it is restricted to
686 the current subtree and \"v\" when only visible elements are
687 considered for export\). Also, \[?] allows to switch back to
688 standard mode."
689 :group 'org-export-general
690 :type 'boolean)
694 ;;; Defining New Back-ends
696 (defmacro org-export-define-backend (backend translators &rest body)
697 "Define a new back-end BACKEND.
699 TRANSLATORS is an alist between object or element types and
700 functions handling them.
702 These functions should return a string without any trailing
703 space, or nil. They must accept three arguments: the object or
704 element itself, its contents or nil when it isn't recursive and
705 the property list used as a communication channel.
707 Contents, when not nil, are stripped from any global indentation
708 \(although the relative one is preserved). They also always end
709 with a single newline character.
711 If, for a given type, no function is found, that element or
712 object type will simply be ignored, along with any blank line or
713 white space at its end. The same will happen if the function
714 returns the nil value. If that function returns the empty
715 string, the type will be ignored, but the blank lines or white
716 spaces will be kept.
718 In addition to element and object types, one function can be
719 associated to the `template' symbol and another one to the
720 `plain-text' symbol.
722 The former returns the final transcoded string, and can be used
723 to add a preamble and a postamble to document's body. It must
724 accept two arguments: the transcoded string and the property list
725 containing export options.
727 The latter, when defined, is to be called on every text not
728 recognized as an element or an object. It must accept two
729 arguments: the text string and the information channel. It is an
730 appropriate place to protect special chars relative to the
731 back-end.
733 BODY can start with pre-defined keyword arguments. The following
734 keywords are understood:
736 :export-block
738 String, or list of strings, representing block names that
739 will not be parsed. This is used to specify blocks that will
740 contain raw code specific to the back-end. These blocks
741 still have to be handled by the relative `export-block' type
742 translator.
744 :filters-alist
746 Alist between filters and function, or list of functions,
747 specific to the back-end. See `org-export-filters-alist' for
748 a list of all allowed filters.
750 :options-alist
752 Alist between back-end specific properties introduced in
753 communication channel and how their value are acquired. See
754 `org-export-options-alist' for more information about
755 structure of the values.
757 As an example, here is how the `e-ascii' back-end is defined:
759 \(org-export-define-backend e-ascii
760 \((bold . org-e-ascii-bold)
761 \(center-block . org-e-ascii-center-block)
762 \(clock . org-e-ascii-clock)
763 \(code . org-e-ascii-code)
764 \(drawer . org-e-ascii-drawer)
765 \(dynamic-block . org-e-ascii-dynamic-block)
766 \(entity . org-e-ascii-entity)
767 \(example-block . org-e-ascii-example-block)
768 \(export-block . org-e-ascii-export-block)
769 \(export-snippet . org-e-ascii-export-snippet)
770 \(fixed-width . org-e-ascii-fixed-width)
771 \(footnote-definition . org-e-ascii-footnote-definition)
772 \(footnote-reference . org-e-ascii-footnote-reference)
773 \(headline . org-e-ascii-headline)
774 \(horizontal-rule . org-e-ascii-horizontal-rule)
775 \(inline-src-block . org-e-ascii-inline-src-block)
776 \(inlinetask . org-e-ascii-inlinetask)
777 \(italic . org-e-ascii-italic)
778 \(item . org-e-ascii-item)
779 \(keyword . org-e-ascii-keyword)
780 \(latex-environment . org-e-ascii-latex-environment)
781 \(latex-fragment . org-e-ascii-latex-fragment)
782 \(line-break . org-e-ascii-line-break)
783 \(link . org-e-ascii-link)
784 \(macro . org-e-ascii-macro)
785 \(paragraph . org-e-ascii-paragraph)
786 \(plain-list . org-e-ascii-plain-list)
787 \(plain-text . org-e-ascii-plain-text)
788 \(planning . org-e-ascii-planning)
789 \(property-drawer . org-e-ascii-property-drawer)
790 \(quote-block . org-e-ascii-quote-block)
791 \(quote-section . org-e-ascii-quote-section)
792 \(radio-target . org-e-ascii-radio-target)
793 \(section . org-e-ascii-section)
794 \(special-block . org-e-ascii-special-block)
795 \(src-block . org-e-ascii-src-block)
796 \(statistics-cookie . org-e-ascii-statistics-cookie)
797 \(strike-through . org-e-ascii-strike-through)
798 \(subscript . org-e-ascii-subscript)
799 \(superscript . org-e-ascii-superscript)
800 \(table . org-e-ascii-table)
801 \(table-cell . org-e-ascii-table-cell)
802 \(table-row . org-e-ascii-table-row)
803 \(target . org-e-ascii-target)
804 \(template . org-e-ascii-template)
805 \(timestamp . org-e-ascii-timestamp)
806 \(underline . org-e-ascii-underline)
807 \(verbatim . org-e-ascii-verbatim)
808 \(verse-block . org-e-ascii-verse-block))
809 :export-block \"ASCII\"
810 :filters-alist ((:filter-headline . org-e-ascii-filter-headline-blank-lines)
811 \(:filter-section . org-e-ascii-filter-headline-blank-lines))
812 :options-alist ((:ascii-charset nil nil org-e-ascii-charset)))"
813 (declare (debug (&define name sexp [&rest [keywordp sexp]] defbody))
814 (indent 1))
815 (let (filters options block-name)
816 (while (keywordp (car body))
817 (case (pop body)
818 (:export-block (let ((names (pop body)))
819 (setq export-block
820 (if (consp names) (mapcar 'upcase names)
821 (list (upcase names))))))
822 (:filters-alist (setq filters (pop body)))
823 (:options-alist (setq options (pop body)))
824 (t (pop body))))
825 `(progn
826 ;; Define translators.
827 (defvar ,(intern (format "org-%s-translate-alist" backend)) ',translators
828 "Alist between element or object types and translators.")
829 ;; Define options.
830 ,(when options
831 `(defconst ,(intern (format "org-%s-options-alist" backend)) ',options
832 ,(format "Alist between %s export properties and ways to set them.
833 See `org-export-options-alist' for more information on the
834 structure of the values."
835 backend)))
836 ;; Define filters.
837 ,(when filters
838 `(defconst ,(intern (format "org-%s-filters-alist" backend)) ',filters
839 "Alist between filters keywords and back-end specific filters.
840 See `org-export-filters-alist' for more information."))
841 ;; Tell parser to not parse EXPORT-BLOCK blocks.
842 ,(when export-block
843 `(mapc
844 (lambda (name)
845 (add-to-list 'org-element-block-name-alist
846 `(,name . org-element-export-block-parser)))
847 ',export-block))
848 ;; Splice in the body, if any.
849 ,@body)))
851 (defmacro org-export-define-derived-backend (child parent &rest body)
852 "Create a new back-end as a variant of an existing one.
854 CHILD is the name of the derived back-end. PARENT is the name of
855 the parent back-end.
857 BODY can start with pre-defined keyword arguments. The following
858 keywords are understood:
860 :export-block
862 String, or list of strings, representing block names that
863 will not be parsed. This is used to specify blocks that will
864 contain raw code specific to the back-end. These blocks
865 still have to be handled by the relative `export-block' type
866 translator.
868 :filters-alist
870 Alist of filters that will overwrite or complete filters
871 defined in PARENT back-end. See `org-export-filters-alist'
872 for more a list of allowed filters.
874 :options-alist
876 Alist of back-end specific properties that will overwrite or
877 complete those defined in PARENT back-end. Refer to
878 `org-export-options-alist' for more information about
879 structure of the values.
881 :translate-alist
883 Alist of element and object types and transcoders that will
884 overwrite or complete transcode table from PARENT back-end.
885 Refer to `org-export-define-backend' for detailed information
886 about transcoders.
888 As an example, here is how one could define \"my-latex\" back-end
889 as a variant of `e-latex' back-end with a custom template
890 function:
892 \(org-export-define-derived-backend my-latex e-latex
893 :translate-alist ((template . my-latex-template-fun)))
895 The back-end could then be called with, for example:
897 \(org-export-to-buffer 'my-latex \"*Test my-latex*\")"
898 (declare (debug (&define name sexp [&rest [keywordp sexp]] def-body))
899 (indent 2))
900 (let (filters options translate)
901 (while (keywordp (car body))
902 (case (pop body)
903 (:export-block (let ((names (pop body)))
904 (setq export-block
905 (if (consp names) (mapcar 'upcase names)
906 (list (upcase names))))))
907 (:filters-alist (setq filters (pop body)))
908 (:options-alist (setq options (pop body)))
909 (:translate-alist (setq translate (pop body)))
910 (t (pop body))))
911 `(progn
912 ;; Tell parser to not parse EXPORT-BLOCK blocks.
913 ,(when export-block
914 `(mapc
915 (lambda (name)
916 (add-to-list 'org-element-block-name-alist
917 `(,name . org-element-export-block-parser)))
918 ',export-block))
919 ;; Define filters.
920 ,(let ((parent-filters (intern (format "org-%s-filters-alist" parent))))
921 (when (or (boundp parent-filters) filters)
922 `(defconst ,(intern (format "org-%s-filters-alist" child))
923 ',(append filters
924 (and (boundp parent-filters)
925 (copy-sequence (symbol-value parent-filters))))
926 "Alist between filters keywords and back-end specific filters.
927 See `org-export-filters-alist' for more information.")))
928 ;; Define options.
929 ,(let ((parent-options (intern (format "org-%s-options-alist" parent))))
930 (when (or (boundp parent-options) options)
931 `(defconst ,(intern (format "org-%s-options-alist" child))
932 ',(append options
933 (and (boundp parent-options)
934 (copy-sequence (symbol-value parent-options))))
935 ,(format "Alist between %s export properties and ways to set them.
936 See `org-export-options-alist' for more information on the
937 structure of the values."
938 child))))
939 ;; Define translators.
940 (defvar ,(intern (format "org-%s-translate-alist" child))
941 ',(append translate
942 (copy-sequence
943 (symbol-value
944 (intern (format "org-%s-translate-alist" parent)))))
945 "Alist between element or object types and translators.")
946 ;; Splice in the body, if any.
947 ,@body)))
951 ;;; The Communication Channel
953 ;; During export process, every function has access to a number of
954 ;; properties. They are of two types:
956 ;; 1. Environment options are collected once at the very beginning of
957 ;; the process, out of the original buffer and configuration.
958 ;; Collecting them is handled by `org-export-get-environment'
959 ;; function.
961 ;; Most environment options are defined through the
962 ;; `org-export-options-alist' variable.
964 ;; 2. Tree properties are extracted directly from the parsed tree,
965 ;; just before export, by `org-export-collect-tree-properties'.
967 ;; Here is the full list of properties available during transcode
968 ;; process, with their category (option, tree or local) and their
969 ;; value type.
971 ;; + `:author' :: Author's name.
972 ;; - category :: option
973 ;; - type :: string
975 ;; + `:back-end' :: Current back-end used for transcoding.
976 ;; - category :: tree
977 ;; - type :: symbol
979 ;; + `:creator' :: String to write as creation information.
980 ;; - category :: option
981 ;; - type :: string
983 ;; + `:date' :: String to use as date.
984 ;; - category :: option
985 ;; - type :: string
987 ;; + `:description' :: Description text for the current data.
988 ;; - category :: option
989 ;; - type :: string
991 ;; + `:email' :: Author's email.
992 ;; - category :: option
993 ;; - type :: string
995 ;; + `:exclude-tags' :: Tags for exclusion of subtrees from export
996 ;; process.
997 ;; - category :: option
998 ;; - type :: list of strings
1000 ;; + `:exported-data' :: Hash table used for memoizing
1001 ;; `org-export-data'.
1002 ;; - category :: tree
1003 ;; - type :: hash table
1005 ;; + `:footnote-definition-alist' :: Alist between footnote labels and
1006 ;; their definition, as parsed data. Only non-inlined footnotes
1007 ;; are represented in this alist. Also, every definition isn't
1008 ;; guaranteed to be referenced in the parse tree. The purpose of
1009 ;; this property is to preserve definitions from oblivion
1010 ;; (i.e. when the parse tree comes from a part of the original
1011 ;; buffer), it isn't meant for direct use in a back-end. To
1012 ;; retrieve a definition relative to a reference, use
1013 ;; `org-export-get-footnote-definition' instead.
1014 ;; - category :: option
1015 ;; - type :: alist (STRING . LIST)
1017 ;; + `:headline-levels' :: Maximum level being exported as an
1018 ;; headline. Comparison is done with the relative level of
1019 ;; headlines in the parse tree, not necessarily with their
1020 ;; actual level.
1021 ;; - category :: option
1022 ;; - type :: integer
1024 ;; + `:headline-offset' :: Difference between relative and real level
1025 ;; of headlines in the parse tree. For example, a value of -1
1026 ;; means a level 2 headline should be considered as level
1027 ;; 1 (cf. `org-export-get-relative-level').
1028 ;; - category :: tree
1029 ;; - type :: integer
1031 ;; + `:headline-numbering' :: Alist between headlines and their
1032 ;; numbering, as a list of numbers
1033 ;; (cf. `org-export-get-headline-number').
1034 ;; - category :: tree
1035 ;; - type :: alist (INTEGER . LIST)
1037 ;; + `:id-alist' :: Alist between ID strings and destination file's
1038 ;; path, relative to current directory. It is used by
1039 ;; `org-export-resolve-id-link' to resolve ID links targeting an
1040 ;; external file.
1041 ;; - category :: option
1042 ;; - type :: alist (STRING . STRING)
1044 ;; + `:ignore-list' :: List of elements and objects that should be
1045 ;; ignored during export.
1046 ;; - category :: tree
1047 ;; - type :: list of elements and objects
1049 ;; + `:input-file' :: Full path to input file, if any.
1050 ;; - category :: option
1051 ;; - type :: string or nil
1053 ;; + `:keywords' :: List of keywords attached to data.
1054 ;; - category :: option
1055 ;; - type :: string
1057 ;; + `:language' :: Default language used for translations.
1058 ;; - category :: option
1059 ;; - type :: string
1061 ;; + `:parse-tree' :: Whole parse tree, available at any time during
1062 ;; transcoding.
1063 ;; - category :: option
1064 ;; - type :: list (as returned by `org-element-parse-buffer')
1066 ;; + `:preserve-breaks' :: Non-nil means transcoding should preserve
1067 ;; all line breaks.
1068 ;; - category :: option
1069 ;; - type :: symbol (nil, t)
1071 ;; + `:section-numbers' :: Non-nil means transcoding should add
1072 ;; section numbers to headlines.
1073 ;; - category :: option
1074 ;; - type :: symbol (nil, t)
1076 ;; + `:select-tags' :: List of tags enforcing inclusion of sub-trees
1077 ;; in transcoding. When such a tag is present, subtrees without
1078 ;; it are de facto excluded from the process. See
1079 ;; `use-select-tags'.
1080 ;; - category :: option
1081 ;; - type :: list of strings
1083 ;; + `:target-list' :: List of targets encountered in the parse tree.
1084 ;; This is used to partly resolve "fuzzy" links
1085 ;; (cf. `org-export-resolve-fuzzy-link').
1086 ;; - category :: tree
1087 ;; - type :: list of strings
1089 ;; + `:time-stamp-file' :: Non-nil means transcoding should insert
1090 ;; a time stamp in the output.
1091 ;; - category :: option
1092 ;; - type :: symbol (nil, t)
1094 ;; + `:translate-alist' :: Alist between element and object types and
1095 ;; transcoding functions relative to the current back-end.
1096 ;; Special keys `template' and `plain-text' are also possible.
1097 ;; - category :: option
1098 ;; - type :: alist (SYMBOL . FUNCTION)
1100 ;; + `:with-archived-trees' :: Non-nil when archived subtrees should
1101 ;; also be transcoded. If it is set to the `headline' symbol,
1102 ;; only the archived headline's name is retained.
1103 ;; - category :: option
1104 ;; - type :: symbol (nil, t, `headline')
1106 ;; + `:with-author' :: Non-nil means author's name should be included
1107 ;; in the output.
1108 ;; - category :: option
1109 ;; - type :: symbol (nil, t)
1111 ;; + `:with-clocks' :: Non-nild means clock keywords should be exported.
1112 ;; - category :: option
1113 ;; - type :: symbol (nil, t)
1115 ;; + `:with-creator' :: Non-nild means a creation sentence should be
1116 ;; inserted at the end of the transcoded string. If the value
1117 ;; is `comment', it should be commented.
1118 ;; - category :: option
1119 ;; - type :: symbol (`comment', nil, t)
1121 ;; + `:with-drawers' :: Non-nil means drawers should be exported. If
1122 ;; its value is a list of names, only drawers with such names
1123 ;; will be transcoded.
1124 ;; - category :: option
1125 ;; - type :: symbol (nil, t) or list of strings
1127 ;; + `:with-email' :: Non-nil means output should contain author's
1128 ;; email.
1129 ;; - category :: option
1130 ;; - type :: symbol (nil, t)
1132 ;; + `:with-emphasize' :: Non-nil means emphasized text should be
1133 ;; interpreted.
1134 ;; - category :: option
1135 ;; - type :: symbol (nil, t)
1137 ;; + `:with-fixed-width' :: Non-nil if transcoder should interpret
1138 ;; strings starting with a colon as a fixed-with (verbatim) area.
1139 ;; - category :: option
1140 ;; - type :: symbol (nil, t)
1142 ;; + `:with-footnotes' :: Non-nil if transcoder should interpret
1143 ;; footnotes.
1144 ;; - category :: option
1145 ;; - type :: symbol (nil, t)
1147 ;; + `:with-plannings' :: Non-nil means transcoding should include
1148 ;; planning info.
1149 ;; - category :: option
1150 ;; - type :: symbol (nil, t)
1152 ;; + `:with-priority' :: Non-nil means transcoding should include
1153 ;; priority cookies.
1154 ;; - category :: option
1155 ;; - type :: symbol (nil, t)
1157 ;; + `:with-special-strings' :: Non-nil means transcoding should
1158 ;; interpret special strings in plain text.
1159 ;; - category :: option
1160 ;; - type :: symbol (nil, t)
1162 ;; + `:with-sub-superscript' :: Non-nil means transcoding should
1163 ;; interpret subscript and superscript. With a value of "{}",
1164 ;; only interpret those using curly brackets.
1165 ;; - category :: option
1166 ;; - type :: symbol (nil, {}, t)
1168 ;; + `:with-tables' :: Non-nil means transcoding should interpret
1169 ;; tables.
1170 ;; - category :: option
1171 ;; - type :: symbol (nil, t)
1173 ;; + `:with-tags' :: Non-nil means transcoding should keep tags in
1174 ;; headlines. A `not-in-toc' value will remove them from the
1175 ;; table of contents, if any, nonetheless.
1176 ;; - category :: option
1177 ;; - type :: symbol (nil, t, `not-in-toc')
1179 ;; + `:with-tasks' :: Non-nil means transcoding should include
1180 ;; headlines with a TODO keyword. A `todo' value will only
1181 ;; include headlines with a todo type keyword while a `done'
1182 ;; value will do the contrary. If a list of strings is provided,
1183 ;; only tasks with keywords belonging to that list will be kept.
1184 ;; - category :: option
1185 ;; - type :: symbol (t, todo, done, nil) or list of strings
1187 ;; + `:with-timestamps' :: Non-nil means transcoding should include
1188 ;; time stamps. Special value `active' (resp. `inactive') ask to
1189 ;; export only active (resp. inactive) timestamps. Otherwise,
1190 ;; completely remove them.
1191 ;; - category :: option
1192 ;; - type :: symbol: (`active', `inactive', t, nil)
1194 ;; + `:with-toc' :: Non-nil means that a table of contents has to be
1195 ;; added to the output. An integer value limits its depth.
1196 ;; - category :: option
1197 ;; - type :: symbol (nil, t or integer)
1199 ;; + `:with-todo-keywords' :: Non-nil means transcoding should
1200 ;; include TODO keywords.
1201 ;; - category :: option
1202 ;; - type :: symbol (nil, t)
1205 ;;;; Environment Options
1207 ;; Environment options encompass all parameters defined outside the
1208 ;; scope of the parsed data. They come from five sources, in
1209 ;; increasing precedence order:
1211 ;; - Global variables,
1212 ;; - Buffer's attributes,
1213 ;; - Options keyword symbols,
1214 ;; - Buffer keywords,
1215 ;; - Subtree properties.
1217 ;; The central internal function with regards to environment options
1218 ;; is `org-export-get-environment'. It updates global variables with
1219 ;; "#+BIND:" keywords, then retrieve and prioritize properties from
1220 ;; the different sources.
1222 ;; The internal functions doing the retrieval are:
1223 ;; `org-export--get-global-options',
1224 ;; `org-export--get-buffer-attributes',
1225 ;; `org-export--parse-option-keyword',
1226 ;; `org-export--get-subtree-options' and
1227 ;; `org-export--get-inbuffer-options'
1229 ;; Also, `org-export--confirm-letbind' and `org-export--install-letbind'
1230 ;; take care of the part relative to "#+BIND:" keywords.
1232 (defun org-export-get-environment (&optional backend subtreep ext-plist)
1233 "Collect export options from the current buffer.
1235 Optional argument BACKEND is a symbol specifying which back-end
1236 specific options to read, if any.
1238 When optional argument SUBTREEP is non-nil, assume the export is
1239 done against the current sub-tree.
1241 Third optional argument EXT-PLIST is a property list with
1242 external parameters overriding Org default settings, but still
1243 inferior to file-local settings."
1244 ;; First install #+BIND variables.
1245 (org-export--install-letbind-maybe)
1246 ;; Get and prioritize export options...
1247 (org-combine-plists
1248 ;; ... from global variables...
1249 (org-export--get-global-options backend)
1250 ;; ... from buffer's attributes...
1251 (org-export--get-buffer-attributes)
1252 ;; ... from an external property list...
1253 ext-plist
1254 ;; ... from in-buffer settings...
1255 (org-export--get-inbuffer-options
1256 backend
1257 (and buffer-file-name (org-remove-double-quotes buffer-file-name)))
1258 ;; ... and from subtree, when appropriate.
1259 (and subtreep (org-export--get-subtree-options backend))
1260 ;; Eventually install back-end symbol and its translation table.
1261 `(:back-end
1262 ,backend
1263 :translate-alist
1264 ,(let ((trans-alist (intern (format "org-%s-translate-alist" backend))))
1265 (when (boundp trans-alist) (symbol-value trans-alist))))))
1267 (defun org-export--parse-option-keyword (options &optional backend)
1268 "Parse an OPTIONS line and return values as a plist.
1269 Optional argument BACKEND is a symbol specifying which back-end
1270 specific items to read, if any."
1271 (let* ((all
1272 (append org-export-options-alist
1273 (and backend
1274 (let ((var (intern
1275 (format "org-%s-options-alist" backend))))
1276 (and (boundp var) (eval var))))))
1277 ;; Build an alist between #+OPTION: item and property-name.
1278 (alist (delq nil
1279 (mapcar (lambda (e)
1280 (when (nth 2 e) (cons (regexp-quote (nth 2 e))
1281 (car e))))
1282 all)))
1283 plist)
1284 (mapc (lambda (e)
1285 (when (string-match (concat "\\(\\`\\|[ \t]\\)"
1286 (car e)
1287 ":\\(([^)\n]+)\\|[^ \t\n\r;,.]*\\)")
1288 options)
1289 (setq plist (plist-put plist
1290 (cdr e)
1291 (car (read-from-string
1292 (match-string 2 options)))))))
1293 alist)
1294 plist))
1296 (defun org-export--get-subtree-options (&optional backend)
1297 "Get export options in subtree at point.
1298 Optional argument BACKEND is a symbol specifying back-end used
1299 for export. Return options as a plist."
1300 ;; For each buffer keyword, create an headline property setting the
1301 ;; same property in communication channel. The name for the property
1302 ;; is the keyword with "EXPORT_" appended to it.
1303 (org-with-wide-buffer
1304 (let (prop plist)
1305 ;; Make sure point is at an heading.
1306 (unless (org-at-heading-p) (org-back-to-heading t))
1307 ;; Take care of EXPORT_TITLE. If it isn't defined, use headline's
1308 ;; title as its fallback value.
1309 (when (setq prop (progn (looking-at org-todo-line-regexp)
1310 (or (save-match-data
1311 (org-entry-get (point) "EXPORT_TITLE"))
1312 (org-match-string-no-properties 3))))
1313 (setq plist
1314 (plist-put
1315 plist :title
1316 (org-element-parse-secondary-string
1317 prop (org-element-restriction 'keyword)))))
1318 ;; EXPORT_OPTIONS are parsed in a non-standard way.
1319 (when (setq prop (org-entry-get (point) "EXPORT_OPTIONS"))
1320 (setq plist
1321 (nconc plist (org-export--parse-option-keyword prop backend))))
1322 ;; Handle other keywords.
1323 (let ((seen '("TITLE")))
1324 (mapc
1325 (lambda (option)
1326 (let ((property (nth 1 option)))
1327 (when (and property (not (member property seen)))
1328 (let* ((subtree-prop (concat "EXPORT_" property))
1329 ;; Export properties are not case-sensitive.
1330 (value (let ((case-fold-search t))
1331 (org-entry-get (point) subtree-prop))))
1332 (push property seen)
1333 (when value
1334 (setq plist
1335 (plist-put
1336 plist
1337 (car option)
1338 ;; Parse VALUE if required.
1339 (if (member property org-element-parsed-keywords)
1340 (org-element-parse-secondary-string
1341 value (org-element-restriction 'keyword))
1342 value))))))))
1343 ;; Also look for both general keywords and back-end specific
1344 ;; options if BACKEND is provided.
1345 (append (and backend
1346 (let ((var (intern
1347 (format "org-%s-options-alist" backend))))
1348 (and (boundp var) (symbol-value var))))
1349 org-export-options-alist)))
1350 ;; Return value.
1351 plist)))
1353 (defun org-export--get-inbuffer-options (&optional backend files)
1354 "Return current buffer export options, as a plist.
1356 Optional argument BACKEND, when non-nil, is a symbol specifying
1357 which back-end specific options should also be read in the
1358 process.
1360 Optional argument FILES is a list of setup files names read so
1361 far, used to avoid circular dependencies.
1363 Assume buffer is in Org mode. Narrowing, if any, is ignored."
1364 (org-with-wide-buffer
1365 (goto-char (point-min))
1366 (let ((case-fold-search t) plist)
1367 ;; 1. Special keywords, as in `org-export-special-keywords'.
1368 (let ((special-re (org-make-options-regexp org-export-special-keywords)))
1369 (while (re-search-forward special-re nil t)
1370 (let ((element (org-element-at-point)))
1371 (when (eq (org-element-type element) 'keyword)
1372 (let* ((key (org-element-property :key element))
1373 (val (org-element-property :value element))
1374 (prop
1375 (cond
1376 ((string= key "SETUP_FILE")
1377 (let ((file
1378 (expand-file-name
1379 (org-remove-double-quotes (org-trim val)))))
1380 ;; Avoid circular dependencies.
1381 (unless (member file files)
1382 (with-temp-buffer
1383 (insert (org-file-contents file 'noerror))
1384 (org-mode)
1385 (org-export--get-inbuffer-options
1386 backend (cons file files))))))
1387 ((string= key "OPTIONS")
1388 (org-export--parse-option-keyword val backend))
1389 ((string= key "MACRO")
1390 (when (string-match
1391 "^\\([-a-zA-Z0-9_]+\\)\\(?:[ \t]+\\(.*?\\)[ \t]*$\\)?"
1392 val)
1393 (let ((key
1394 (intern
1395 (concat ":macro-"
1396 (downcase (match-string 1 val)))))
1397 (value (org-match-string-no-properties 2 val)))
1398 (cond
1399 ((not value) nil)
1400 ;; Value will be evaled: do not parse it.
1401 ((string-match "\\`(eval\\>" value)
1402 (list key (list value)))
1403 ;; Value has to be parsed for nested
1404 ;; macros.
1406 (list
1408 (let ((restr (org-element-restriction 'macro)))
1409 (org-element-parse-secondary-string
1410 ;; If user explicitly asks for
1411 ;; a newline, be sure to preserve it
1412 ;; from further filling with
1413 ;; `hard-newline'. Also replace
1414 ;; "\\n" with "\n", "\\\n" with "\\n"
1415 ;; and so on...
1416 (replace-regexp-in-string
1417 "\\(\\\\\\\\\\)n" "\\\\"
1418 (replace-regexp-in-string
1419 "\\(?:^\\|[^\\\\]\\)\\(\\\\n\\)"
1420 hard-newline value nil nil 1)
1421 nil nil 1)
1422 restr)))))))))))
1423 (setq plist (org-combine-plists plist prop)))))))
1424 ;; 2. Standard options, as in `org-export-options-alist'.
1425 (let* ((all (append org-export-options-alist
1426 ;; Also look for back-end specific options
1427 ;; if BACKEND is defined.
1428 (and backend
1429 (let ((var
1430 (intern
1431 (format "org-%s-options-alist" backend))))
1432 (and (boundp var) (eval var))))))
1433 ;; Build alist between keyword name and property name.
1434 (alist
1435 (delq nil (mapcar
1436 (lambda (e) (when (nth 1 e) (cons (nth 1 e) (car e))))
1437 all)))
1438 ;; Build regexp matching all keywords associated to export
1439 ;; options. Note: the search is case insensitive.
1440 (opt-re (org-make-options-regexp
1441 (delq nil (mapcar (lambda (e) (nth 1 e)) all)))))
1442 (goto-char (point-min))
1443 (while (re-search-forward opt-re nil t)
1444 (let ((element (org-element-at-point)))
1445 (when (eq (org-element-type element) 'keyword)
1446 (let* ((key (org-element-property :key element))
1447 (val (org-element-property :value element))
1448 (prop (cdr (assoc key alist)))
1449 (behaviour (nth 4 (assq prop all))))
1450 (setq plist
1451 (plist-put
1452 plist prop
1453 ;; Handle value depending on specified BEHAVIOUR.
1454 (case behaviour
1455 (space
1456 (if (not (plist-get plist prop)) (org-trim val)
1457 (concat (plist-get plist prop) " " (org-trim val))))
1458 (newline
1459 (org-trim
1460 (concat (plist-get plist prop) "\n" (org-trim val))))
1461 (split
1462 `(,@(plist-get plist prop) ,@(org-split-string val)))
1463 ('t val)
1464 (otherwise (if (not (plist-member plist prop)) val
1465 (plist-get plist prop))))))))))
1466 ;; Parse keywords specified in `org-element-parsed-keywords'.
1467 (mapc
1468 (lambda (key)
1469 (let* ((prop (cdr (assoc key alist)))
1470 (value (and prop (plist-get plist prop))))
1471 (when (stringp value)
1472 (setq plist
1473 (plist-put
1474 plist prop
1475 (org-element-parse-secondary-string
1476 value (org-element-restriction 'keyword)))))))
1477 org-element-parsed-keywords))
1478 ;; 3. Return final value.
1479 plist)))
1481 (defun org-export--get-buffer-attributes ()
1482 "Return properties related to buffer attributes, as a plist."
1483 (let ((visited-file (buffer-file-name (buffer-base-buffer))))
1484 (list
1485 ;; Store full path of input file name, or nil. For internal use.
1486 :input-file visited-file
1487 :title (or (and visited-file
1488 (file-name-sans-extension
1489 (file-name-nondirectory visited-file)))
1490 (buffer-name (buffer-base-buffer)))
1491 :footnote-definition-alist
1492 ;; Footnotes definitions must be collected in the original
1493 ;; buffer, as there's no insurance that they will still be in the
1494 ;; parse tree, due to possible narrowing.
1495 (let (alist)
1496 (org-with-wide-buffer
1497 (goto-char (point-min))
1498 (while (re-search-forward org-footnote-definition-re nil t)
1499 (let ((def (org-footnote-at-definition-p)))
1500 (when def
1501 (org-skip-whitespace)
1502 (push (cons (car def)
1503 (save-restriction
1504 (narrow-to-region (point) (nth 2 def))
1505 ;; Like `org-element-parse-buffer', but
1506 ;; makes sure the definition doesn't start
1507 ;; with a section element.
1508 (org-element--parse-elements
1509 (point-min) (point-max) nil nil nil nil
1510 (list 'org-data nil))))
1511 alist))))
1512 alist))
1513 :id-alist
1514 ;; Collect id references.
1515 (let (alist)
1516 (org-with-wide-buffer
1517 (goto-char (point-min))
1518 (while (re-search-forward
1519 "\\[\\[id:\\(\\S-+?\\)\\]\\(?:\\[.*?\\]\\)?\\]" nil t)
1520 (let* ((id (org-match-string-no-properties 1))
1521 (file (org-id-find-id-file id)))
1522 (when file (push (cons id (file-relative-name file)) alist)))))
1523 alist)
1524 :macro-modification-time
1525 (and visited-file
1526 (file-exists-p visited-file)
1527 (concat "(eval (format-time-string \"$1\" '"
1528 (prin1-to-string (nth 5 (file-attributes visited-file)))
1529 "))"))
1530 ;; Store input file name as a macro.
1531 :macro-input-file (and visited-file (file-name-nondirectory visited-file))
1532 ;; `:macro-date', `:macro-time' and `:macro-property' could as
1533 ;; well be initialized as tree properties, since they don't
1534 ;; depend on buffer properties. Though, it may be more logical
1535 ;; to keep them close to other ":macro-" properties.
1536 :macro-date "(eval (format-time-string \"$1\"))"
1537 :macro-time "(eval (format-time-string \"$1\"))"
1538 :macro-property "(eval (org-entry-get nil \"$1\" 'selective))")))
1540 (defun org-export--get-global-options (&optional backend)
1541 "Return global export options as a plist.
1543 Optional argument BACKEND, if non-nil, is a symbol specifying
1544 which back-end specific export options should also be read in the
1545 process."
1546 (let ((all (append org-export-options-alist
1547 (and backend
1548 (let ((var (intern
1549 (format "org-%s-options-alist" backend))))
1550 (and (boundp var) (symbol-value var))))))
1551 ;; Output value.
1552 plist)
1553 (mapc
1554 (lambda (cell)
1555 (setq plist
1556 (plist-put
1557 plist
1558 (car cell)
1559 ;; Eval default value provided. If keyword is a member
1560 ;; of `org-element-parsed-keywords', parse it as
1561 ;; a secondary string before storing it.
1562 (let ((value (eval (nth 3 cell))))
1563 (if (not (stringp value)) value
1564 (let ((keyword (nth 1 cell)))
1565 (if (not (member keyword org-element-parsed-keywords)) value
1566 (org-element-parse-secondary-string
1567 value (org-element-restriction 'keyword)))))))))
1568 all)
1569 ;; Return value.
1570 plist))
1572 (defvar org-export--allow-BIND-local nil)
1573 (defun org-export--confirm-letbind ()
1574 "Can we use #+BIND values during export?
1575 By default this will ask for confirmation by the user, to divert
1576 possible security risks."
1577 (cond
1578 ((not org-export-allow-BIND) nil)
1579 ((eq org-export-allow-BIND t) t)
1580 ((local-variable-p 'org-export--allow-BIND-local)
1581 org-export--allow-BIND-local)
1582 (t (org-set-local 'org-export--allow-BIND-local
1583 (yes-or-no-p "Allow BIND values in this buffer? ")))))
1585 (defun org-export--install-letbind-maybe ()
1586 "Install the values from #+BIND lines as local variables.
1587 Variables must be installed before in-buffer options are
1588 retrieved."
1589 (let (letbind pair)
1590 (org-with-wide-buffer
1591 (goto-char (point-min))
1592 (while (re-search-forward (org-make-options-regexp '("BIND")) nil t)
1593 (when (org-export-confirm-letbind)
1594 (push (read (concat "(" (org-match-string-no-properties 2) ")"))
1595 letbind))))
1596 (while (setq pair (pop letbind))
1597 (org-set-local (car pair) (nth 1 pair)))))
1600 ;;;; Tree Properties
1602 ;; Tree properties are infromation extracted from parse tree. They
1603 ;; are initialized at the beginning of the transcoding process by
1604 ;; `org-export-collect-tree-properties'.
1606 ;; Dedicated functions focus on computing the value of specific tree
1607 ;; properties during initialization. Thus,
1608 ;; `org-export--populate-ignore-list' lists elements and objects that
1609 ;; should be skipped during export, `org-export--get-min-level' gets
1610 ;; the minimal exportable level, used as a basis to compute relative
1611 ;; level for headlines. Eventually
1612 ;; `org-export--collect-headline-numbering' builds an alist between
1613 ;; headlines and their numbering.
1615 (defun org-export-collect-tree-properties (data info)
1616 "Extract tree properties from parse tree.
1618 DATA is the parse tree from which information is retrieved. INFO
1619 is a list holding export options.
1621 Following tree properties are set or updated:
1623 `:exported-data' Hash table used to memoize results from
1624 `org-export-data'.
1626 `:footnote-definition-alist' List of footnotes definitions in
1627 original buffer and current parse tree.
1629 `:headline-offset' Offset between true level of headlines and
1630 local level. An offset of -1 means an headline
1631 of level 2 should be considered as a level
1632 1 headline in the context.
1634 `:headline-numbering' Alist of all headlines as key an the
1635 associated numbering as value.
1637 `:ignore-list' List of elements that should be ignored during
1638 export.
1640 `:target-list' List of all targets in the parse tree.
1642 Return updated plist."
1643 ;; Install the parse tree in the communication channel, in order to
1644 ;; use `org-export-get-genealogy' and al.
1645 (setq info (plist-put info :parse-tree data))
1646 ;; Get the list of elements and objects to ignore, and put it into
1647 ;; `:ignore-list'. Do not overwrite any user ignore that might have
1648 ;; been done during parse tree filtering.
1649 (setq info
1650 (plist-put info
1651 :ignore-list
1652 (append (org-export--populate-ignore-list data info)
1653 (plist-get info :ignore-list))))
1654 ;; Compute `:headline-offset' in order to be able to use
1655 ;; `org-export-get-relative-level'.
1656 (setq info
1657 (plist-put info
1658 :headline-offset
1659 (- 1 (org-export--get-min-level data info))))
1660 ;; Update footnotes definitions list with definitions in parse tree.
1661 ;; This is required since buffer expansion might have modified
1662 ;; boundaries of footnote definitions contained in the parse tree.
1663 ;; This way, definitions in `footnote-definition-alist' are bound to
1664 ;; match those in the parse tree.
1665 (let ((defs (plist-get info :footnote-definition-alist)))
1666 (org-element-map
1667 data 'footnote-definition
1668 (lambda (fn)
1669 (push (cons (org-element-property :label fn)
1670 `(org-data nil ,@(org-element-contents fn)))
1671 defs)))
1672 (setq info (plist-put info :footnote-definition-alist defs)))
1673 ;; Properties order doesn't matter: get the rest of the tree
1674 ;; properties.
1675 (nconc
1676 `(:target-list
1677 ,(org-element-map
1678 data '(keyword target)
1679 (lambda (blob)
1680 (when (or (eq (org-element-type blob) 'target)
1681 (string= (org-element-property :key blob) "TARGET"))
1682 blob)) info)
1683 :headline-numbering ,(org-export--collect-headline-numbering data info)
1684 :exported-data ,(make-hash-table :test 'eq :size 4001))
1685 info))
1687 (defun org-export--get-min-level (data options)
1688 "Return minimum exportable headline's level in DATA.
1689 DATA is parsed tree as returned by `org-element-parse-buffer'.
1690 OPTIONS is a plist holding export options."
1691 (catch 'exit
1692 (let ((min-level 10000))
1693 (mapc
1694 (lambda (blob)
1695 (when (and (eq (org-element-type blob) 'headline)
1696 (not (memq blob (plist-get options :ignore-list))))
1697 (setq min-level
1698 (min (org-element-property :level blob) min-level)))
1699 (when (= min-level 1) (throw 'exit 1)))
1700 (org-element-contents data))
1701 ;; If no headline was found, for the sake of consistency, set
1702 ;; minimum level to 1 nonetheless.
1703 (if (= min-level 10000) 1 min-level))))
1705 (defun org-export--collect-headline-numbering (data options)
1706 "Return numbering of all exportable headlines in a parse tree.
1708 DATA is the parse tree. OPTIONS is the plist holding export
1709 options.
1711 Return an alist whose key is an headline and value is its
1712 associated numbering \(in the shape of a list of numbers\)."
1713 (let ((numbering (make-vector org-export-max-depth 0)))
1714 (org-element-map
1715 data
1716 'headline
1717 (lambda (headline)
1718 (let ((relative-level
1719 (1- (org-export-get-relative-level headline options))))
1720 (cons
1721 headline
1722 (loop for n across numbering
1723 for idx from 0 to org-export-max-depth
1724 when (< idx relative-level) collect n
1725 when (= idx relative-level) collect (aset numbering idx (1+ n))
1726 when (> idx relative-level) do (aset numbering idx 0)))))
1727 options)))
1729 (defun org-export--populate-ignore-list (data options)
1730 "Return list of elements and objects to ignore during export.
1731 DATA is the parse tree to traverse. OPTIONS is the plist holding
1732 export options."
1733 (let* (ignore
1734 walk-data ; for byte-compiler.
1735 (walk-data
1736 (function
1737 (lambda (data options selected)
1738 ;; Collect ignored elements or objects into IGNORE-LIST.
1739 (mapc
1740 (lambda (el)
1741 (if (org-export--skip-p el options selected) (push el ignore)
1742 (let ((type (org-element-type el)))
1743 (if (and (eq (plist-get options :with-archived-trees)
1744 'headline)
1745 (eq (org-element-type el) 'headline)
1746 (org-element-property :archivedp el))
1747 ;; If headline is archived but tree below has
1748 ;; to be skipped, add it to ignore list.
1749 (mapc (lambda (e) (push e ignore))
1750 (org-element-contents el))
1751 ;; Move into recursive objects/elements.
1752 (when (org-element-contents el)
1753 (funcall walk-data el options selected))))))
1754 (org-element-contents data))))))
1755 ;; Main call. First find trees containing a select tag, if any.
1756 (funcall walk-data data options (org-export--selected-trees data options))
1757 ;; Return value.
1758 ignore))
1760 (defun org-export--selected-trees (data info)
1761 "Return list of headlines containing a select tag in their tree.
1762 DATA is parsed data as returned by `org-element-parse-buffer'.
1763 INFO is a plist holding export options."
1764 (let* (selected-trees
1765 walk-data ; for byte-compiler.
1766 (walk-data
1767 (function
1768 (lambda (data genealogy)
1769 (case (org-element-type data)
1770 (org-data (mapc (lambda (el) (funcall walk-data el genealogy))
1771 (org-element-contents data)))
1772 (headline
1773 (let ((tags (org-element-property :tags data)))
1774 (if (loop for tag in (plist-get info :select-tags)
1775 thereis (member tag tags))
1776 ;; When a select tag is found, mark full
1777 ;; genealogy and every headline within the tree
1778 ;; as acceptable.
1779 (setq selected-trees
1780 (append
1781 genealogy
1782 (org-element-map data 'headline 'identity)
1783 selected-trees))
1784 ;; Else, continue searching in tree, recursively.
1785 (mapc
1786 (lambda (el) (funcall walk-data el (cons data genealogy)))
1787 (org-element-contents data))))))))))
1788 (funcall walk-data data nil) selected-trees))
1790 (defun org-export--skip-p (blob options selected)
1791 "Non-nil when element or object BLOB should be skipped during export.
1792 OPTIONS is the plist holding export options. SELECTED, when
1793 non-nil, is a list of headlines belonging to a tree with a select
1794 tag."
1795 (case (org-element-type blob)
1796 ;; Check headline.
1797 (headline
1798 (let ((with-tasks (plist-get options :with-tasks))
1799 (todo (org-element-property :todo-keyword blob))
1800 (todo-type (org-element-property :todo-type blob))
1801 (archived (plist-get options :with-archived-trees))
1802 (tags (org-element-property :tags blob)))
1804 ;; Ignore subtrees with an exclude tag.
1805 (loop for k in (plist-get options :exclude-tags)
1806 thereis (member k tags))
1807 ;; When a select tag is present in the buffer, ignore any tree
1808 ;; without it.
1809 (and selected (not (memq blob selected)))
1810 ;; Ignore commented sub-trees.
1811 (org-element-property :commentedp blob)
1812 ;; Ignore archived subtrees if `:with-archived-trees' is nil.
1813 (and (not archived) (org-element-property :archivedp blob))
1814 ;; Ignore tasks, if specified by `:with-tasks' property.
1815 (and todo
1816 (or (not with-tasks)
1817 (and (memq with-tasks '(todo done))
1818 (not (eq todo-type with-tasks)))
1819 (and (consp with-tasks) (not (member todo with-tasks))))))))
1820 ;; Check inlinetask.
1821 (inlinetask (not (plist-get options :with-inlinetasks)))
1822 ;; Check timestamp.
1823 (timestamp
1824 (case (plist-get options :with-timestamps)
1825 ;; No timestamp allowed.
1826 ('nil t)
1827 ;; Only active timestamps allowed and the current one isn't
1828 ;; active.
1829 (active
1830 (not (memq (org-element-property :type blob)
1831 '(active active-range))))
1832 ;; Only inactive timestamps allowed and the current one isn't
1833 ;; inactive.
1834 (inactive
1835 (not (memq (org-element-property :type blob)
1836 '(inactive inactive-range))))))
1837 ;; Check drawer.
1838 (drawer
1839 (or (not (plist-get options :with-drawers))
1840 (and (consp (plist-get options :with-drawers))
1841 (not (member (org-element-property :drawer-name blob)
1842 (plist-get options :with-drawers))))))
1843 ;; Check table-row.
1844 (table-row (org-export-table-row-is-special-p blob options))
1845 ;; Check table-cell.
1846 (table-cell
1847 (and (org-export-table-has-special-column-p
1848 (org-export-get-parent-table blob))
1849 (not (org-export-get-previous-element blob options))))
1850 ;; Check clock.
1851 (clock (not (plist-get options :with-clocks)))
1852 ;; Check planning.
1853 (planning (not (plist-get options :with-plannings)))))
1857 ;;; The Transcoder
1859 ;; `org-export-data' reads a parse tree (obtained with, i.e.
1860 ;; `org-element-parse-buffer') and transcodes it into a specified
1861 ;; back-end output. It takes care of filtering out elements or
1862 ;; objects according to export options and organizing the output blank
1863 ;; lines and white space are preserved. The function memoizes its
1864 ;; results, so it is cheap to call it within translators.
1866 ;; Internally, three functions handle the filtering of objects and
1867 ;; elements during the export. In particular,
1868 ;; `org-export-ignore-element' marks an element or object so future
1869 ;; parse tree traversals skip it, `org-export--interpret-p' tells which
1870 ;; elements or objects should be seen as real Org syntax and
1871 ;; `org-export-expand' transforms the others back into their original
1872 ;; shape
1874 ;; `org-export-transcoder' is an accessor returning appropriate
1875 ;; translator function for a given element or object.
1877 (defun org-export-transcoder (blob info)
1878 "Return appropriate transcoder for BLOB.
1879 INFO is a plist containing export directives."
1880 (let ((type (org-element-type blob)))
1881 ;; Return contents only for complete parse trees.
1882 (if (eq type 'org-data) (lambda (blob contents info) contents)
1883 (let ((transcoder (cdr (assq type (plist-get info :translate-alist)))))
1884 (and (functionp transcoder) transcoder)))))
1886 (defun org-export-data (data info)
1887 "Convert DATA into current back-end format.
1889 DATA is a parse tree, an element or an object or a secondary
1890 string. INFO is a plist holding export options.
1892 Return transcoded string."
1893 (let ((memo (gethash data (plist-get info :exported-data) 'no-memo)))
1894 (if (not (eq memo 'no-memo)) memo
1895 (let* ((type (org-element-type data))
1896 (results
1897 (cond
1898 ;; Ignored element/object.
1899 ((memq data (plist-get info :ignore-list)) nil)
1900 ;; Plain text.
1901 ((eq type 'plain-text)
1902 (org-export-filter-apply-functions
1903 (plist-get info :filter-plain-text)
1904 (let ((transcoder (org-export-transcoder data info)))
1905 (if transcoder (funcall transcoder data info) data))
1906 info))
1907 ;; Uninterpreted element/object: change it back to Org
1908 ;; syntax and export again resulting raw string.
1909 ((not (org-export--interpret-p data info))
1910 (org-export-data
1911 (org-export-expand
1912 data
1913 (mapconcat (lambda (blob) (org-export-data blob info))
1914 (org-element-contents data)
1915 ""))
1916 info))
1917 ;; Secondary string.
1918 ((not type)
1919 (mapconcat (lambda (obj) (org-export-data obj info)) data ""))
1920 ;; Element/Object without contents or, as a special case,
1921 ;; headline with archive tag and archived trees restricted
1922 ;; to title only.
1923 ((or (not (org-element-contents data))
1924 (and (eq type 'headline)
1925 (eq (plist-get info :with-archived-trees) 'headline)
1926 (org-element-property :archivedp data)))
1927 (let ((transcoder (org-export-transcoder data info)))
1928 (and (functionp transcoder)
1929 (funcall transcoder data nil info))))
1930 ;; Element/Object with contents.
1932 (let ((transcoder (org-export-transcoder data info)))
1933 (when transcoder
1934 (let* ((greaterp (memq type org-element-greater-elements))
1935 (objectp
1936 (and (not greaterp)
1937 (memq type org-element-recursive-objects)))
1938 (contents
1939 (mapconcat
1940 (lambda (element) (org-export-data element info))
1941 (org-element-contents
1942 (if (or greaterp objectp) data
1943 ;; Elements directly containing objects
1944 ;; must have their indentation normalized
1945 ;; first.
1946 (org-element-normalize-contents
1947 data
1948 ;; When normalizing contents of the first
1949 ;; paragraph in an item or a footnote
1950 ;; definition, ignore first line's
1951 ;; indentation: there is none and it
1952 ;; might be misleading.
1953 (when (eq type 'paragraph)
1954 (let ((parent (org-export-get-parent data)))
1955 (and
1956 (eq (car (org-element-contents parent))
1957 data)
1958 (memq (org-element-type parent)
1959 '(footnote-definition item))))))))
1960 "")))
1961 (funcall transcoder data
1962 (if (not greaterp) contents
1963 (org-element-normalize-string contents))
1964 info))))))))
1965 ;; Final result will be memoized before being returned.
1966 (puthash
1967 data
1968 (cond
1969 ((not results) nil)
1970 ((memq type '(org-data plain-text nil)) results)
1971 ;; Append the same white space between elements or objects as in
1972 ;; the original buffer, and call appropriate filters.
1974 (let ((results
1975 (org-export-filter-apply-functions
1976 (plist-get info (intern (format ":filter-%s" type)))
1977 (let ((post-blank (or (org-element-property :post-blank data)
1978 0)))
1979 (if (memq type org-element-all-elements)
1980 (concat (org-element-normalize-string results)
1981 (make-string post-blank ?\n))
1982 (concat results (make-string post-blank ? ))))
1983 info)))
1984 results)))
1985 (plist-get info :exported-data))))))
1987 (defun org-export--interpret-p (blob info)
1988 "Non-nil if element or object BLOB should be interpreted as Org syntax.
1989 Check is done according to export options INFO, stored as
1990 a plist."
1991 (case (org-element-type blob)
1992 ;; ... entities...
1993 (entity (plist-get info :with-entities))
1994 ;; ... emphasis...
1995 (emphasis (plist-get info :with-emphasize))
1996 ;; ... fixed-width areas.
1997 (fixed-width (plist-get info :with-fixed-width))
1998 ;; ... footnotes...
1999 ((footnote-definition footnote-reference)
2000 (plist-get info :with-footnotes))
2001 ;; ... sub/superscripts...
2002 ((subscript superscript)
2003 (let ((sub/super-p (plist-get info :with-sub-superscript)))
2004 (if (eq sub/super-p '{})
2005 (org-element-property :use-brackets-p blob)
2006 sub/super-p)))
2007 ;; ... tables...
2008 (table (plist-get info :with-tables))
2009 (otherwise t)))
2011 (defun org-export-expand (blob contents)
2012 "Expand a parsed element or object to its original state.
2013 BLOB is either an element or an object. CONTENTS is its
2014 contents, as a string or nil."
2015 (funcall
2016 (intern (format "org-element-%s-interpreter" (org-element-type blob)))
2017 blob contents))
2019 (defun org-export-ignore-element (element info)
2020 "Add ELEMENT to `:ignore-list' in INFO.
2022 Any element in `:ignore-list' will be skipped when using
2023 `org-element-map'. INFO is modified by side effects."
2024 (plist-put info :ignore-list (cons element (plist-get info :ignore-list))))
2028 ;;; The Filter System
2030 ;; Filters allow end-users to tweak easily the transcoded output.
2031 ;; They are the functional counterpart of hooks, as every filter in
2032 ;; a set is applied to the return value of the previous one.
2034 ;; Every set is back-end agnostic. Although, a filter is always
2035 ;; called, in addition to the string it applies to, with the back-end
2036 ;; used as argument, so it's easy for the end-user to add back-end
2037 ;; specific filters in the set. The communication channel, as
2038 ;; a plist, is required as the third argument.
2040 ;; From the developer side, filters sets can be installed in the
2041 ;; process with the help of `org-export-define-backend', which
2042 ;; internally sets `org-BACKEND-filters-alist' variable. Each
2043 ;; association has a key among the following symbols and a function or
2044 ;; a list of functions as value.
2046 ;; - `:filter-parse-tree' applies directly on the complete parsed
2047 ;; tree. It's the only filters set that doesn't apply to a string.
2048 ;; Users can set it through `org-export-filter-parse-tree-functions'
2049 ;; variable.
2051 ;; - `:filter-final-output' applies to the final transcoded string.
2052 ;; Users can set it with `org-export-filter-final-output-functions'
2053 ;; variable
2055 ;; - `:filter-plain-text' applies to any string not recognized as Org
2056 ;; syntax. `org-export-filter-plain-text-functions' allows users to
2057 ;; configure it.
2059 ;; - `:filter-TYPE' applies on the string returned after an element or
2060 ;; object of type TYPE has been transcoded. An user can modify
2061 ;; `org-export-filter-TYPE-functions'
2063 ;; All filters sets are applied with
2064 ;; `org-export-filter-apply-functions' function. Filters in a set are
2065 ;; applied in a LIFO fashion. It allows developers to be sure that
2066 ;; their filters will be applied first.
2068 ;; Filters properties are installed in communication channel with
2069 ;; `org-export-install-filters' function.
2071 ;; Eventually, a hook (`org-export-before-parsing-hook') is run just
2072 ;; before parsing to allow for heavy structure modifications.
2075 ;;;; Before Parsing Hook
2077 (defvar org-export-before-parsing-hook nil
2078 "Hook run before parsing an export buffer.
2079 This is run after include keywords have been expanded and Babel
2080 code executed, on a copy of original buffer's area being
2081 exported. Visibility is the same as in the original one. Point
2082 is left at the beginning of the new one.")
2085 ;;;; Special Filters
2087 (defvar org-export-filter-parse-tree-functions nil
2088 "List of functions applied to the parsed tree.
2089 Each filter is called with three arguments: the parse tree, as
2090 returned by `org-element-parse-buffer', the back-end, as
2091 a symbol, and the communication channel, as a plist. It must
2092 return the modified parse tree to transcode.")
2094 (defvar org-export-filter-final-output-functions nil
2095 "List of functions applied to the transcoded string.
2096 Each filter is called with three arguments: the full transcoded
2097 string, the back-end, as a symbol, and the communication channel,
2098 as a plist. It must return a string that will be used as the
2099 final export output.")
2101 (defvar org-export-filter-plain-text-functions nil
2102 "List of functions applied to plain text.
2103 Each filter is called with three arguments: a string which
2104 contains no Org syntax, the back-end, as a symbol, and the
2105 communication channel, as a plist. It must return a string or
2106 nil.")
2109 ;;;; Elements Filters
2111 (defvar org-export-filter-center-block-functions nil
2112 "List of functions applied to a transcoded center block.
2113 Each filter is called with three arguments: the transcoded data,
2114 as a string, the back-end, as a symbol, and the communication
2115 channel, as a plist. It must return a string or nil.")
2117 (defvar org-export-filter-clock-functions nil
2118 "List of functions applied to a transcoded clock.
2119 Each filter is called with three arguments: the transcoded data,
2120 as a string, the back-end, as a symbol, and the communication
2121 channel, as a plist. It must return a string or nil.")
2123 (defvar org-export-filter-drawer-functions nil
2124 "List of functions applied to a transcoded drawer.
2125 Each filter is called with three arguments: the transcoded data,
2126 as a string, the back-end, as a symbol, and the communication
2127 channel, as a plist. It must return a string or nil.")
2129 (defvar org-export-filter-dynamic-block-functions nil
2130 "List of functions applied to a transcoded dynamic-block.
2131 Each filter is called with three arguments: the transcoded data,
2132 as a string, the back-end, as a symbol, and the communication
2133 channel, as a plist. It must return a string or nil.")
2135 (defvar org-export-filter-headline-functions nil
2136 "List of functions applied to a transcoded headline.
2137 Each filter is called with three arguments: the transcoded data,
2138 as a string, the back-end, as a symbol, and the communication
2139 channel, as a plist. It must return a string or nil.")
2141 (defvar org-export-filter-inlinetask-functions nil
2142 "List of functions applied to a transcoded inlinetask.
2143 Each filter is called with three arguments: the transcoded data,
2144 as a string, the back-end, as a symbol, and the communication
2145 channel, as a plist. It must return a string or nil.")
2147 (defvar org-export-filter-plain-list-functions nil
2148 "List of functions applied to a transcoded plain-list.
2149 Each filter is called with three arguments: the transcoded data,
2150 as a string, the back-end, as a symbol, and the communication
2151 channel, as a plist. It must return a string or nil.")
2153 (defvar org-export-filter-item-functions nil
2154 "List of functions applied to a transcoded item.
2155 Each filter is called with three arguments: the transcoded data,
2156 as a string, the back-end, as a symbol, and the communication
2157 channel, as a plist. It must return a string or nil.")
2159 (defvar org-export-filter-comment-functions nil
2160 "List of functions applied to a transcoded comment.
2161 Each filter is called with three arguments: the transcoded data,
2162 as a string, the back-end, as a symbol, and the communication
2163 channel, as a plist. It must return a string or nil.")
2165 (defvar org-export-filter-comment-block-functions nil
2166 "List of functions applied to a transcoded comment-comment.
2167 Each filter is called with three arguments: the transcoded data,
2168 as a string, the back-end, as a symbol, and the communication
2169 channel, as a plist. It must return a string or nil.")
2171 (defvar org-export-filter-example-block-functions nil
2172 "List of functions applied to a transcoded example-block.
2173 Each filter is called with three arguments: the transcoded data,
2174 as a string, the back-end, as a symbol, and the communication
2175 channel, as a plist. It must return a string or nil.")
2177 (defvar org-export-filter-export-block-functions nil
2178 "List of functions applied to a transcoded export-block.
2179 Each filter is called with three arguments: the transcoded data,
2180 as a string, the back-end, as a symbol, and the communication
2181 channel, as a plist. It must return a string or nil.")
2183 (defvar org-export-filter-fixed-width-functions nil
2184 "List of functions applied to a transcoded fixed-width.
2185 Each filter is called with three arguments: the transcoded data,
2186 as a string, the back-end, as a symbol, and the communication
2187 channel, as a plist. It must return a string or nil.")
2189 (defvar org-export-filter-footnote-definition-functions nil
2190 "List of functions applied to a transcoded footnote-definition.
2191 Each filter is called with three arguments: the transcoded data,
2192 as a string, the back-end, as a symbol, and the communication
2193 channel, as a plist. It must return a string or nil.")
2195 (defvar org-export-filter-horizontal-rule-functions nil
2196 "List of functions applied to a transcoded horizontal-rule.
2197 Each filter is called with three arguments: the transcoded data,
2198 as a string, the back-end, as a symbol, and the communication
2199 channel, as a plist. It must return a string or nil.")
2201 (defvar org-export-filter-keyword-functions nil
2202 "List of functions applied to a transcoded keyword.
2203 Each filter is called with three arguments: the transcoded data,
2204 as a string, the back-end, as a symbol, and the communication
2205 channel, as a plist. It must return a string or nil.")
2207 (defvar org-export-filter-latex-environment-functions nil
2208 "List of functions applied to a transcoded latex-environment.
2209 Each filter is called with three arguments: the transcoded data,
2210 as a string, the back-end, as a symbol, and the communication
2211 channel, as a plist. It must return a string or nil.")
2213 (defvar org-export-filter-babel-call-functions nil
2214 "List of functions applied to a transcoded babel-call.
2215 Each filter is called with three arguments: the transcoded data,
2216 as a string, the back-end, as a symbol, and the communication
2217 channel, as a plist. It must return a string or nil.")
2219 (defvar org-export-filter-paragraph-functions nil
2220 "List of functions applied to a transcoded paragraph.
2221 Each filter is called with three arguments: the transcoded data,
2222 as a string, the back-end, as a symbol, and the communication
2223 channel, as a plist. It must return a string or nil.")
2225 (defvar org-export-filter-planning-functions nil
2226 "List of functions applied to a transcoded planning.
2227 Each filter is called with three arguments: the transcoded data,
2228 as a string, the back-end, as a symbol, and the communication
2229 channel, as a plist. It must return a string or nil.")
2231 (defvar org-export-filter-property-drawer-functions nil
2232 "List of functions applied to a transcoded property-drawer.
2233 Each filter is called with three arguments: the transcoded data,
2234 as a string, the back-end, as a symbol, and the communication
2235 channel, as a plist. It must return a string or nil.")
2237 (defvar org-export-filter-quote-block-functions nil
2238 "List of functions applied to a transcoded quote block.
2239 Each filter is called with three arguments: the transcoded quote
2240 data, as a string, the back-end, as a symbol, and the
2241 communication channel, as a plist. It must return a string or
2242 nil.")
2244 (defvar org-export-filter-quote-section-functions nil
2245 "List of functions applied to a transcoded quote-section.
2246 Each filter is called with three arguments: the transcoded data,
2247 as a string, the back-end, as a symbol, and the communication
2248 channel, as a plist. It must return a string or nil.")
2250 (defvar org-export-filter-section-functions nil
2251 "List of functions applied to a transcoded section.
2252 Each filter is called with three arguments: the transcoded data,
2253 as a string, the back-end, as a symbol, and the communication
2254 channel, as a plist. It must return a string or nil.")
2256 (defvar org-export-filter-special-block-functions nil
2257 "List of functions applied to a transcoded special block.
2258 Each filter is called with three arguments: the transcoded data,
2259 as a string, the back-end, as a symbol, and the communication
2260 channel, as a plist. It must return a string or nil.")
2262 (defvar org-export-filter-src-block-functions nil
2263 "List of functions applied to a transcoded src-block.
2264 Each filter is called with three arguments: the transcoded data,
2265 as a string, the back-end, as a symbol, and the communication
2266 channel, as a plist. It must return a string or nil.")
2268 (defvar org-export-filter-table-functions nil
2269 "List of functions applied to a transcoded table.
2270 Each filter is called with three arguments: the transcoded data,
2271 as a string, the back-end, as a symbol, and the communication
2272 channel, as a plist. It must return a string or nil.")
2274 (defvar org-export-filter-table-cell-functions nil
2275 "List of functions applied to a transcoded table-cell.
2276 Each filter is called with three arguments: the transcoded data,
2277 as a string, the back-end, as a symbol, and the communication
2278 channel, as a plist. It must return a string or nil.")
2280 (defvar org-export-filter-table-row-functions nil
2281 "List of functions applied to a transcoded table-row.
2282 Each filter is called with three arguments: the transcoded data,
2283 as a string, the back-end, as a symbol, and the communication
2284 channel, as a plist. It must return a string or nil.")
2286 (defvar org-export-filter-verse-block-functions nil
2287 "List of functions applied to a transcoded verse block.
2288 Each filter is called with three arguments: the transcoded data,
2289 as a string, the back-end, as a symbol, and the communication
2290 channel, as a plist. It must return a string or nil.")
2293 ;;;; Objects Filters
2295 (defvar org-export-filter-bold-functions nil
2296 "List of functions applied to transcoded bold text.
2297 Each filter is called with three arguments: the transcoded data,
2298 as a string, the back-end, as a symbol, and the communication
2299 channel, as a plist. It must return a string or nil.")
2301 (defvar org-export-filter-code-functions nil
2302 "List of functions applied to transcoded code text.
2303 Each filter is called with three arguments: the transcoded data,
2304 as a string, the back-end, as a symbol, and the communication
2305 channel, as a plist. It must return a string or nil.")
2307 (defvar org-export-filter-entity-functions nil
2308 "List of functions applied to a transcoded entity.
2309 Each filter is called with three arguments: the transcoded data,
2310 as a string, the back-end, as a symbol, and the communication
2311 channel, as a plist. It must return a string or nil.")
2313 (defvar org-export-filter-export-snippet-functions nil
2314 "List of functions applied to a transcoded export-snippet.
2315 Each filter is called with three arguments: the transcoded data,
2316 as a string, the back-end, as a symbol, and the communication
2317 channel, as a plist. It must return a string or nil.")
2319 (defvar org-export-filter-footnote-reference-functions nil
2320 "List of functions applied to a transcoded footnote-reference.
2321 Each filter is called with three arguments: the transcoded data,
2322 as a string, the back-end, as a symbol, and the communication
2323 channel, as a plist. It must return a string or nil.")
2325 (defvar org-export-filter-inline-babel-call-functions nil
2326 "List of functions applied to a transcoded inline-babel-call.
2327 Each filter is called with three arguments: the transcoded data,
2328 as a string, the back-end, as a symbol, and the communication
2329 channel, as a plist. It must return a string or nil.")
2331 (defvar org-export-filter-inline-src-block-functions nil
2332 "List of functions applied to a transcoded inline-src-block.
2333 Each filter is called with three arguments: the transcoded data,
2334 as a string, the back-end, as a symbol, and the communication
2335 channel, as a plist. It must return a string or nil.")
2337 (defvar org-export-filter-italic-functions nil
2338 "List of functions applied to transcoded italic text.
2339 Each filter is called with three arguments: the transcoded data,
2340 as a string, the back-end, as a symbol, and the communication
2341 channel, as a plist. It must return a string or nil.")
2343 (defvar org-export-filter-latex-fragment-functions nil
2344 "List of functions applied to a transcoded latex-fragment.
2345 Each filter is called with three arguments: the transcoded data,
2346 as a string, the back-end, as a symbol, and the communication
2347 channel, as a plist. It must return a string or nil.")
2349 (defvar org-export-filter-line-break-functions nil
2350 "List of functions applied to a transcoded line-break.
2351 Each filter is called with three arguments: the transcoded data,
2352 as a string, the back-end, as a symbol, and the communication
2353 channel, as a plist. It must return a string or nil.")
2355 (defvar org-export-filter-link-functions nil
2356 "List of functions applied to a transcoded link.
2357 Each filter is called with three arguments: the transcoded data,
2358 as a string, the back-end, as a symbol, and the communication
2359 channel, as a plist. It must return a string or nil.")
2361 (defvar org-export-filter-macro-functions nil
2362 "List of functions applied to a transcoded macro.
2363 Each filter is called with three arguments: the transcoded data,
2364 as a string, the back-end, as a symbol, and the communication
2365 channel, as a plist. It must return a string or nil.")
2367 (defvar org-export-filter-radio-target-functions nil
2368 "List of functions applied to a transcoded radio-target.
2369 Each filter is called with three arguments: the transcoded data,
2370 as a string, the back-end, as a symbol, and the communication
2371 channel, as a plist. It must return a string or nil.")
2373 (defvar org-export-filter-statistics-cookie-functions nil
2374 "List of functions applied to a transcoded statistics-cookie.
2375 Each filter is called with three arguments: the transcoded data,
2376 as a string, the back-end, as a symbol, and the communication
2377 channel, as a plist. It must return a string or nil.")
2379 (defvar org-export-filter-strike-through-functions nil
2380 "List of functions applied to transcoded strike-through text.
2381 Each filter is called with three arguments: the transcoded data,
2382 as a string, the back-end, as a symbol, and the communication
2383 channel, as a plist. It must return a string or nil.")
2385 (defvar org-export-filter-subscript-functions nil
2386 "List of functions applied to a transcoded subscript.
2387 Each filter is called with three arguments: the transcoded data,
2388 as a string, the back-end, as a symbol, and the communication
2389 channel, as a plist. It must return a string or nil.")
2391 (defvar org-export-filter-superscript-functions nil
2392 "List of functions applied to a transcoded superscript.
2393 Each filter is called with three arguments: the transcoded data,
2394 as a string, the back-end, as a symbol, and the communication
2395 channel, as a plist. It must return a string or nil.")
2397 (defvar org-export-filter-target-functions nil
2398 "List of functions applied to a transcoded target.
2399 Each filter is called with three arguments: the transcoded data,
2400 as a string, the back-end, as a symbol, and the communication
2401 channel, as a plist. It must return a string or nil.")
2403 (defvar org-export-filter-timestamp-functions nil
2404 "List of functions applied to a transcoded timestamp.
2405 Each filter is called with three arguments: the transcoded data,
2406 as a string, the back-end, as a symbol, and the communication
2407 channel, as a plist. It must return a string or nil.")
2409 (defvar org-export-filter-underline-functions nil
2410 "List of functions applied to transcoded underline text.
2411 Each filter is called with three arguments: the transcoded data,
2412 as a string, the back-end, as a symbol, and the communication
2413 channel, as a plist. It must return a string or nil.")
2415 (defvar org-export-filter-verbatim-functions nil
2416 "List of functions applied to transcoded verbatim text.
2417 Each filter is called with three arguments: the transcoded data,
2418 as a string, the back-end, as a symbol, and the communication
2419 channel, as a plist. It must return a string or nil.")
2422 ;;;; Filters Tools
2424 ;; Internal function `org-export-install-filters' installs filters
2425 ;; hard-coded in back-ends (developer filters) and filters from global
2426 ;; variables (user filters) in the communication channel.
2428 ;; Internal function `org-export-filter-apply-functions' takes care
2429 ;; about applying each filter in order to a given data. It stops
2430 ;; whenever a filter returns a nil value.
2432 (defun org-export-filter-apply-functions (filters value info)
2433 "Call every function in FILTERS.
2434 Functions are called with arguments VALUE, current export
2435 back-end and INFO. Call is done in a LIFO fashion, to be sure
2436 that developer specified filters, if any, are called first."
2437 (loop for filter in filters
2438 if (not value) return nil else
2439 do (setq value (funcall filter value (plist-get info :back-end) info)))
2440 value)
2442 (defun org-export-install-filters (info)
2443 "Install filters properties in communication channel.
2445 INFO is a plist containing the current communication channel.
2447 Return the updated communication channel."
2448 (let (plist)
2449 ;; Install user defined filters with `org-export-filters-alist'.
2450 (mapc (lambda (p)
2451 (setq plist (plist-put plist (car p) (eval (cdr p)))))
2452 org-export-filters-alist)
2453 ;; Prepend back-end specific filters to that list.
2454 (let ((back-end-filters (intern (format "org-%s-filters-alist"
2455 (plist-get info :back-end)))))
2456 (when (boundp back-end-filters)
2457 (mapc (lambda (p)
2458 ;; Single values get consed, lists are prepended.
2459 (let ((key (car p)) (value (cdr p)))
2460 (when value
2461 (setq plist
2462 (plist-put
2463 plist key
2464 (if (atom value) (cons value (plist-get plist key))
2465 (append value (plist-get plist key))))))))
2466 (eval back-end-filters))))
2467 ;; Return new communication channel.
2468 (org-combine-plists info plist)))
2472 ;;; Core functions
2474 ;; This is the room for the main function, `org-export-as', along with
2475 ;; its derivatives, `org-export-to-buffer' and `org-export-to-file'.
2476 ;; They differ only by the way they output the resulting code.
2478 ;; `org-export-output-file-name' is an auxiliary function meant to be
2479 ;; used with `org-export-to-file'. With a given extension, it tries
2480 ;; to provide a canonical file name to write export output to.
2482 ;; Note that `org-export-as' doesn't really parse the current buffer,
2483 ;; but a copy of it (with the same buffer-local variables and
2484 ;; visibility), where include keywords are expanded and Babel blocks
2485 ;; are executed, if appropriate.
2486 ;; `org-export-with-current-buffer-copy' macro prepares that copy.
2488 ;; File inclusion is taken care of by
2489 ;; `org-export-expand-include-keyword' and
2490 ;; `org-export--prepare-file-contents'. Structure wise, including
2491 ;; a whole Org file in a buffer often makes little sense. For
2492 ;; example, if the file contains an headline and the include keyword
2493 ;; was within an item, the item should contain the headline. That's
2494 ;; why file inclusion should be done before any structure can be
2495 ;; associated to the file, that is before parsing.
2497 (defvar org-current-export-file) ; Dynamically scoped
2498 (defvar org-export-current-backend) ; Dynamically scoped
2499 (defun org-export-as
2500 (backend &optional subtreep visible-only body-only ext-plist noexpand)
2501 "Transcode current Org buffer into BACKEND code.
2503 If narrowing is active in the current buffer, only transcode its
2504 narrowed part.
2506 If a region is active, transcode that region.
2508 When optional argument SUBTREEP is non-nil, transcode the
2509 sub-tree at point, extracting information from the headline
2510 properties first.
2512 When optional argument VISIBLE-ONLY is non-nil, don't export
2513 contents of hidden elements.
2515 When optional argument BODY-ONLY is non-nil, only return body
2516 code, without preamble nor postamble.
2518 Optional argument EXT-PLIST, when provided, is a property list
2519 with external parameters overriding Org default settings, but
2520 still inferior to file-local settings.
2522 Optional argument NOEXPAND, when non-nil, prevents included files
2523 to be expanded and Babel code to be executed.
2525 Return code as a string."
2526 (save-excursion
2527 (save-restriction
2528 ;; Narrow buffer to an appropriate region or subtree for
2529 ;; parsing. If parsing subtree, be sure to remove main headline
2530 ;; too.
2531 (cond ((org-region-active-p)
2532 (narrow-to-region (region-beginning) (region-end)))
2533 (subtreep
2534 (org-narrow-to-subtree)
2535 (goto-char (point-min))
2536 (forward-line)
2537 (narrow-to-region (point) (point-max))))
2538 ;; 1. Get export environment from original buffer. Also install
2539 ;; user's and developer's filters.
2540 (let ((info (org-export-install-filters
2541 (org-export-get-environment backend subtreep ext-plist)))
2542 ;; 2. Get parse tree. Buffer isn't parsed directly.
2543 ;; Instead, a temporary copy is created, where include
2544 ;; keywords are expanded and code blocks are evaluated.
2545 (tree (let ((buf (or (buffer-file-name (buffer-base-buffer))
2546 (current-buffer))))
2547 (org-export-with-current-buffer-copy
2548 (unless noexpand
2549 (org-export-expand-include-keyword)
2550 ;; Setting `org-current-export-file' is
2551 ;; required by Org Babel to properly resolve
2552 ;; noweb references.
2553 (let ((org-current-export-file buf))
2554 (org-export-blocks-preprocess)))
2555 (goto-char (point-min))
2556 ;; Run hook with `org-export-current-backend' set
2557 ;; to BACKEND.
2558 (let ((org-export-current-backend backend))
2559 (run-hooks 'org-export-before-parsing-hook))
2560 ;; Eventually parse buffer.
2561 (org-element-parse-buffer nil visible-only)))))
2562 ;; 3. Call parse-tree filters to get the final tree.
2563 (setq tree
2564 (org-export-filter-apply-functions
2565 (plist-get info :filter-parse-tree) tree info))
2566 ;; 4. Now tree is complete, compute its properties and add
2567 ;; them to communication channel.
2568 (setq info
2569 (org-combine-plists
2570 info (org-export-collect-tree-properties tree info)))
2571 ;; 5. Eventually transcode TREE. Wrap the resulting string
2572 ;; into a template, if required. Eventually call
2573 ;; final-output filter.
2574 (let* ((body (org-element-normalize-string (org-export-data tree info)))
2575 (template (cdr (assq 'template
2576 (plist-get info :translate-alist))))
2577 (output (org-export-filter-apply-functions
2578 (plist-get info :filter-final-output)
2579 (if (or (not (functionp template)) body-only) body
2580 (funcall template body info))
2581 info)))
2582 ;; Maybe add final OUTPUT to kill ring, then return it.
2583 (when org-export-copy-to-kill-ring (org-kill-new output))
2584 output)))))
2586 (defun org-export-to-buffer
2587 (backend buffer &optional subtreep visible-only body-only ext-plist noexpand)
2588 "Call `org-export-as' with output to a specified buffer.
2590 BACKEND is the back-end used for transcoding, as a symbol.
2592 BUFFER is the output buffer. If it already exists, it will be
2593 erased first, otherwise, it will be created.
2595 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY, EXT-PLIST
2596 and NOEXPAND are similar to those used in `org-export-as', which
2597 see.
2599 Return buffer."
2600 (let ((out (org-export-as
2601 backend subtreep visible-only body-only ext-plist noexpand))
2602 (buffer (get-buffer-create buffer)))
2603 (with-current-buffer buffer
2604 (erase-buffer)
2605 (insert out)
2606 (goto-char (point-min)))
2607 buffer))
2609 (defun org-export-to-file
2610 (backend file &optional subtreep visible-only body-only ext-plist noexpand)
2611 "Call `org-export-as' with output to a specified file.
2613 BACKEND is the back-end used for transcoding, as a symbol. FILE
2614 is the name of the output file, as a string.
2616 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY, EXT-PLIST
2617 and NOEXPAND are similar to those used in `org-export-as', which
2618 see.
2620 Return output file's name."
2621 ;; Checks for FILE permissions. `write-file' would do the same, but
2622 ;; we'd rather avoid needless transcoding of parse tree.
2623 (unless (file-writable-p file) (error "Output file not writable"))
2624 ;; Insert contents to a temporary buffer and write it to FILE.
2625 (let ((out (org-export-as
2626 backend subtreep visible-only body-only ext-plist noexpand)))
2627 (with-temp-buffer
2628 (insert out)
2629 (let ((coding-system-for-write org-export-coding-system))
2630 (write-file file))))
2631 ;; Return full path.
2632 file)
2634 (defun org-export-output-file-name (extension &optional subtreep pub-dir)
2635 "Return output file's name according to buffer specifications.
2637 EXTENSION is a string representing the output file extension,
2638 with the leading dot.
2640 With a non-nil optional argument SUBTREEP, try to determine
2641 output file's name by looking for \"EXPORT_FILE_NAME\" property
2642 of subtree at point.
2644 When optional argument PUB-DIR is set, use it as the publishing
2645 directory.
2647 When optional argument VISIBLE-ONLY is non-nil, don't export
2648 contents of hidden elements.
2650 Return file name as a string, or nil if it couldn't be
2651 determined."
2652 (let ((base-name
2653 ;; File name may come from EXPORT_FILE_NAME subtree property,
2654 ;; assuming point is at beginning of said sub-tree.
2655 (file-name-sans-extension
2656 (or (and subtreep
2657 (org-entry-get
2658 (save-excursion
2659 (ignore-errors (org-back-to-heading) (point)))
2660 "EXPORT_FILE_NAME" t))
2661 ;; File name may be extracted from buffer's associated
2662 ;; file, if any.
2663 (buffer-file-name (buffer-base-buffer))
2664 ;; Can't determine file name on our own: Ask user.
2665 (let ((read-file-name-function
2666 (and org-completion-use-ido 'ido-read-file-name)))
2667 (read-file-name
2668 "Output file: " pub-dir nil nil nil
2669 (lambda (name)
2670 (string= (file-name-extension name t) extension))))))))
2671 ;; Build file name. Enforce EXTENSION over whatever user may have
2672 ;; come up with. PUB-DIR, if defined, always has precedence over
2673 ;; any provided path.
2674 (cond
2675 (pub-dir
2676 (concat (file-name-as-directory pub-dir)
2677 (file-name-nondirectory base-name)
2678 extension))
2679 ((string= (file-name-nondirectory base-name) base-name)
2680 (concat (file-name-as-directory ".") base-name extension))
2681 (t (concat base-name extension)))))
2683 (defmacro org-export-with-current-buffer-copy (&rest body)
2684 "Apply BODY in a copy of the current buffer.
2686 The copy preserves local variables and visibility of the original
2687 buffer.
2689 Point is at buffer's beginning when BODY is applied."
2690 (org-with-gensyms (original-buffer offset buffer-string overlays)
2691 `(let ((,original-buffer (current-buffer))
2692 (,offset (1- (point-min)))
2693 (,buffer-string (buffer-string))
2694 (,overlays (mapcar
2695 'copy-overlay (overlays-in (point-min) (point-max)))))
2696 (with-temp-buffer
2697 (let ((buffer-invisibility-spec nil))
2698 (org-clone-local-variables
2699 ,original-buffer
2700 "^\\(org-\\|orgtbl-\\|major-mode$\\|outline-\\(regexp\\|level\\)$\\)")
2701 (insert ,buffer-string)
2702 (mapc (lambda (ov)
2703 (move-overlay
2705 (- (overlay-start ov) ,offset)
2706 (- (overlay-end ov) ,offset)
2707 (current-buffer)))
2708 ,overlays)
2709 (goto-char (point-min))
2710 (progn ,@body))))))
2711 (def-edebug-spec org-export-with-current-buffer-copy (body))
2713 (defun org-export-expand-include-keyword (&optional included dir)
2714 "Expand every include keyword in buffer.
2715 Optional argument INCLUDED is a list of included file names along
2716 with their line restriction, when appropriate. It is used to
2717 avoid infinite recursion. Optional argument DIR is the current
2718 working directory. It is used to properly resolve relative
2719 paths."
2720 (let ((case-fold-search t))
2721 (goto-char (point-min))
2722 (while (re-search-forward "^[ \t]*#\\+INCLUDE: \\(.*\\)" nil t)
2723 (when (eq (org-element-type (save-match-data (org-element-at-point)))
2724 'keyword)
2725 (beginning-of-line)
2726 ;; Extract arguments from keyword's value.
2727 (let* ((value (match-string 1))
2728 (ind (org-get-indentation))
2729 (file (and (string-match "^\"\\(\\S-+\\)\"" value)
2730 (prog1 (expand-file-name (match-string 1 value) dir)
2731 (setq value (replace-match "" nil nil value)))))
2732 (lines
2733 (and (string-match
2734 ":lines +\"\\(\\(?:[0-9]+\\)?-\\(?:[0-9]+\\)?\\)\"" value)
2735 (prog1 (match-string 1 value)
2736 (setq value (replace-match "" nil nil value)))))
2737 (env (cond ((string-match "\\<example\\>" value) 'example)
2738 ((string-match "\\<src\\(?: +\\(.*\\)\\)?" value)
2739 (match-string 1 value))))
2740 ;; Minimal level of included file defaults to the child
2741 ;; level of the current headline, if any, or one. It
2742 ;; only applies is the file is meant to be included as
2743 ;; an Org one.
2744 (minlevel
2745 (and (not env)
2746 (if (string-match ":minlevel +\\([0-9]+\\)" value)
2747 (prog1 (string-to-number (match-string 1 value))
2748 (setq value (replace-match "" nil nil value)))
2749 (let ((cur (org-current-level)))
2750 (if cur (1+ (org-reduced-level cur)) 1))))))
2751 ;; Remove keyword.
2752 (delete-region (point) (progn (forward-line) (point)))
2753 (cond
2754 ((not (file-readable-p file)) (error "Cannot include file %s" file))
2755 ;; Check if files has already been parsed. Look after
2756 ;; inclusion lines too, as different parts of the same file
2757 ;; can be included too.
2758 ((member (list file lines) included)
2759 (error "Recursive file inclusion: %s" file))
2761 (cond
2762 ((eq env 'example)
2763 (insert
2764 (let ((ind-str (make-string ind ? ))
2765 (contents
2766 ;; Protect sensitive contents with commas.
2767 (replace-regexp-in-string
2768 "\\(^\\)\\([*]\\|[ \t]*#\\+\\)" ","
2769 (org-export--prepare-file-contents file lines)
2770 nil nil 1)))
2771 (format "%s#+BEGIN_EXAMPLE\n%s%s#+END_EXAMPLE\n"
2772 ind-str contents ind-str))))
2773 ((stringp env)
2774 (insert
2775 (let ((ind-str (make-string ind ? ))
2776 (contents
2777 ;; Protect sensitive contents with commas.
2778 (replace-regexp-in-string
2779 (if (string= env "org") "\\(^\\)\\(.\\)"
2780 "\\(^\\)\\([*]\\|[ \t]*#\\+\\)") ","
2781 (org-export--prepare-file-contents file lines)
2782 nil nil 1)))
2783 (format "%s#+BEGIN_SRC %s\n%s%s#+END_SRC\n"
2784 ind-str env contents ind-str))))
2786 (insert
2787 (with-temp-buffer
2788 (org-mode)
2789 (insert
2790 (org-export--prepare-file-contents file lines ind minlevel))
2791 (org-export-expand-include-keyword
2792 (cons (list file lines) included)
2793 (file-name-directory file))
2794 (buffer-string))))))))))))
2796 (defun org-export--prepare-file-contents (file &optional lines ind minlevel)
2797 "Prepare the contents of FILE for inclusion and return them as a string.
2799 When optional argument LINES is a string specifying a range of
2800 lines, include only those lines.
2802 Optional argument IND, when non-nil, is an integer specifying the
2803 global indentation of returned contents. Since its purpose is to
2804 allow an included file to stay in the same environment it was
2805 created \(i.e. a list item), it doesn't apply past the first
2806 headline encountered.
2808 Optional argument MINLEVEL, when non-nil, is an integer
2809 specifying the level that any top-level headline in the included
2810 file should have."
2811 (with-temp-buffer
2812 (insert-file-contents file)
2813 (when lines
2814 (let* ((lines (split-string lines "-"))
2815 (lbeg (string-to-number (car lines)))
2816 (lend (string-to-number (cadr lines)))
2817 (beg (if (zerop lbeg) (point-min)
2818 (goto-char (point-min))
2819 (forward-line (1- lbeg))
2820 (point)))
2821 (end (if (zerop lend) (point-max)
2822 (goto-char (point-min))
2823 (forward-line (1- lend))
2824 (point))))
2825 (narrow-to-region beg end)))
2826 ;; Remove blank lines at beginning and end of contents. The logic
2827 ;; behind that removal is that blank lines around include keyword
2828 ;; override blank lines in included file.
2829 (goto-char (point-min))
2830 (org-skip-whitespace)
2831 (beginning-of-line)
2832 (delete-region (point-min) (point))
2833 (goto-char (point-max))
2834 (skip-chars-backward " \r\t\n")
2835 (forward-line)
2836 (delete-region (point) (point-max))
2837 ;; If IND is set, preserve indentation of include keyword until
2838 ;; the first headline encountered.
2839 (when ind
2840 (unless (eq major-mode 'org-mode) (org-mode))
2841 (goto-char (point-min))
2842 (let ((ind-str (make-string ind ? )))
2843 (while (not (or (eobp) (looking-at org-outline-regexp-bol)))
2844 ;; Do not move footnote definitions out of column 0.
2845 (unless (and (looking-at org-footnote-definition-re)
2846 (eq (org-element-type (org-element-at-point))
2847 'footnote-definition))
2848 (insert ind-str))
2849 (forward-line))))
2850 ;; When MINLEVEL is specified, compute minimal level for headlines
2851 ;; in the file (CUR-MIN), and remove stars to each headline so
2852 ;; that headlines with minimal level have a level of MINLEVEL.
2853 (when minlevel
2854 (unless (eq major-mode 'org-mode) (org-mode))
2855 (let ((levels (org-map-entries
2856 (lambda () (org-reduced-level (org-current-level))))))
2857 (when levels
2858 (let ((offset (- minlevel (apply 'min levels))))
2859 (unless (zerop offset)
2860 (when org-odd-levels-only (setq offset (* offset 2)))
2861 ;; Only change stars, don't bother moving whole
2862 ;; sections.
2863 (org-map-entries
2864 (lambda () (if (< offset 0) (delete-char (abs offset))
2865 (insert (make-string offset ?*))))))))))
2866 (buffer-string)))
2869 ;;; Tools For Back-Ends
2871 ;; A whole set of tools is available to help build new exporters. Any
2872 ;; function general enough to have its use across many back-ends
2873 ;; should be added here.
2875 ;; As of now, functions operating on footnotes, headlines, links,
2876 ;; macros, references, src-blocks, tables and tables of contents are
2877 ;; implemented.
2879 ;;;; For Affiliated Keywords
2881 ;; `org-export-read-attribute' reads a property from a given element
2882 ;; as a plist. It can be used to normalize affiliated keywords'
2883 ;; syntax.
2885 (defun org-export-read-attribute (attribute element &optional property)
2886 "Turn ATTRIBUTE property from ELEMENT into a plist.
2888 When optional argument PROPERTY is non-nil, return the value of
2889 that property within attributes.
2891 This function assumes attributes are defined as \":keyword
2892 value\" pairs. It is appropriate for `:attr_html' like
2893 properties."
2894 (let ((attributes
2895 (let ((value (org-element-property attribute element)))
2896 (and value
2897 (read (format "(%s)" (mapconcat 'identity value " ")))))))
2898 (if property (plist-get attributes property) attributes)))
2901 ;;;; For Export Snippets
2903 ;; Every export snippet is transmitted to the back-end. Though, the
2904 ;; latter will only retain one type of export-snippet, ignoring
2905 ;; others, based on the former's target back-end. The function
2906 ;; `org-export-snippet-backend' returns that back-end for a given
2907 ;; export-snippet.
2909 (defun org-export-snippet-backend (export-snippet)
2910 "Return EXPORT-SNIPPET targeted back-end as a symbol.
2911 Translation, with `org-export-snippet-translation-alist', is
2912 applied."
2913 (let ((back-end (org-element-property :back-end export-snippet)))
2914 (intern
2915 (or (cdr (assoc back-end org-export-snippet-translation-alist))
2916 back-end))))
2919 ;;;; For Footnotes
2921 ;; `org-export-collect-footnote-definitions' is a tool to list
2922 ;; actually used footnotes definitions in the whole parse tree, or in
2923 ;; an headline, in order to add footnote listings throughout the
2924 ;; transcoded data.
2926 ;; `org-export-footnote-first-reference-p' is a predicate used by some
2927 ;; back-ends, when they need to attach the footnote definition only to
2928 ;; the first occurrence of the corresponding label.
2930 ;; `org-export-get-footnote-definition' and
2931 ;; `org-export-get-footnote-number' provide easier access to
2932 ;; additional information relative to a footnote reference.
2934 (defun org-export-collect-footnote-definitions (data info)
2935 "Return an alist between footnote numbers, labels and definitions.
2937 DATA is the parse tree from which definitions are collected.
2938 INFO is the plist used as a communication channel.
2940 Definitions are sorted by order of references. They either
2941 appear as Org data or as a secondary string for inlined
2942 footnotes. Unreferenced definitions are ignored."
2943 (let* (num-alist
2944 collect-fn ; for byte-compiler.
2945 (collect-fn
2946 (function
2947 (lambda (data)
2948 ;; Collect footnote number, label and definition in DATA.
2949 (org-element-map
2950 data 'footnote-reference
2951 (lambda (fn)
2952 (when (org-export-footnote-first-reference-p fn info)
2953 (let ((def (org-export-get-footnote-definition fn info)))
2954 (push
2955 (list (org-export-get-footnote-number fn info)
2956 (org-element-property :label fn)
2957 def)
2958 num-alist)
2959 ;; Also search in definition for nested footnotes.
2960 (when (eq (org-element-property :type fn) 'standard)
2961 (funcall collect-fn def)))))
2962 ;; Don't enter footnote definitions since it will happen
2963 ;; when their first reference is found.
2964 info nil 'footnote-definition)))))
2965 (funcall collect-fn (plist-get info :parse-tree))
2966 (reverse num-alist)))
2968 (defun org-export-footnote-first-reference-p (footnote-reference info)
2969 "Non-nil when a footnote reference is the first one for its label.
2971 FOOTNOTE-REFERENCE is the footnote reference being considered.
2972 INFO is the plist used as a communication channel."
2973 (let ((label (org-element-property :label footnote-reference)))
2974 ;; Anonymous footnotes are always a first reference.
2975 (if (not label) t
2976 ;; Otherwise, return the first footnote with the same LABEL and
2977 ;; test if it is equal to FOOTNOTE-REFERENCE.
2978 (let* (search-refs ; for byte-compiler.
2979 (search-refs
2980 (function
2981 (lambda (data)
2982 (org-element-map
2983 data 'footnote-reference
2984 (lambda (fn)
2985 (cond
2986 ((string= (org-element-property :label fn) label)
2987 (throw 'exit fn))
2988 ;; If FN isn't inlined, be sure to traverse its
2989 ;; definition before resuming search. See
2990 ;; comments in `org-export-get-footnote-number'
2991 ;; for more information.
2992 ((eq (org-element-property :type fn) 'standard)
2993 (funcall search-refs
2994 (org-export-get-footnote-definition fn info)))))
2995 ;; Don't enter footnote definitions since it will
2996 ;; happen when their first reference is found.
2997 info 'first-match 'footnote-definition)))))
2998 (eq (catch 'exit (funcall search-refs (plist-get info :parse-tree)))
2999 footnote-reference)))))
3001 (defun org-export-get-footnote-definition (footnote-reference info)
3002 "Return definition of FOOTNOTE-REFERENCE as parsed data.
3003 INFO is the plist used as a communication channel."
3004 (let ((label (org-element-property :label footnote-reference)))
3005 (or (org-element-property :inline-definition footnote-reference)
3006 (cdr (assoc label (plist-get info :footnote-definition-alist))))))
3008 (defun org-export-get-footnote-number (footnote info)
3009 "Return number associated to a footnote.
3011 FOOTNOTE is either a footnote reference or a footnote definition.
3012 INFO is the plist used as a communication channel."
3013 (let* ((label (org-element-property :label footnote))
3014 seen-refs
3015 search-ref ; For byte-compiler.
3016 (search-ref
3017 (function
3018 (lambda (data)
3019 ;; Search footnote references through DATA, filling
3020 ;; SEEN-REFS along the way.
3021 (org-element-map
3022 data 'footnote-reference
3023 (lambda (fn)
3024 (let ((fn-lbl (org-element-property :label fn)))
3025 (cond
3026 ;; Anonymous footnote match: return number.
3027 ((and (not fn-lbl) (eq fn footnote))
3028 (throw 'exit (1+ (length seen-refs))))
3029 ;; Labels match: return number.
3030 ((and label (string= label fn-lbl))
3031 (throw 'exit (1+ (length seen-refs))))
3032 ;; Anonymous footnote: it's always a new one. Also,
3033 ;; be sure to return nil from the `cond' so
3034 ;; `first-match' doesn't get us out of the loop.
3035 ((not fn-lbl) (push 'inline seen-refs) nil)
3036 ;; Label not seen so far: add it so SEEN-REFS.
3038 ;; Also search for subsequent references in
3039 ;; footnote definition so numbering follows reading
3040 ;; logic. Note that we don't have to care about
3041 ;; inline definitions, since `org-element-map'
3042 ;; already traverses them at the right time.
3044 ;; Once again, return nil to stay in the loop.
3045 ((not (member fn-lbl seen-refs))
3046 (push fn-lbl seen-refs)
3047 (funcall search-ref
3048 (org-export-get-footnote-definition fn info))
3049 nil))))
3050 ;; Don't enter footnote definitions since it will happen
3051 ;; when their first reference is found.
3052 info 'first-match 'footnote-definition)))))
3053 (catch 'exit (funcall search-ref (plist-get info :parse-tree)))))
3056 ;;;; For Headlines
3058 ;; `org-export-get-relative-level' is a shortcut to get headline
3059 ;; level, relatively to the lower headline level in the parsed tree.
3061 ;; `org-export-get-headline-number' returns the section number of an
3062 ;; headline, while `org-export-number-to-roman' allows to convert it
3063 ;; to roman numbers.
3065 ;; `org-export-low-level-p', `org-export-first-sibling-p' and
3066 ;; `org-export-last-sibling-p' are three useful predicates when it
3067 ;; comes to fulfill the `:headline-levels' property.
3069 (defun org-export-get-relative-level (headline info)
3070 "Return HEADLINE relative level within current parsed tree.
3071 INFO is a plist holding contextual information."
3072 (+ (org-element-property :level headline)
3073 (or (plist-get info :headline-offset) 0)))
3075 (defun org-export-low-level-p (headline info)
3076 "Non-nil when HEADLINE is considered as low level.
3078 INFO is a plist used as a communication channel.
3080 A low level headlines has a relative level greater than
3081 `:headline-levels' property value.
3083 Return value is the difference between HEADLINE relative level
3084 and the last level being considered as high enough, or nil."
3085 (let ((limit (plist-get info :headline-levels)))
3086 (when (wholenump limit)
3087 (let ((level (org-export-get-relative-level headline info)))
3088 (and (> level limit) (- level limit))))))
3090 (defun org-export-get-headline-number (headline info)
3091 "Return HEADLINE numbering as a list of numbers.
3092 INFO is a plist holding contextual information."
3093 (cdr (assoc headline (plist-get info :headline-numbering))))
3095 (defun org-export-numbered-headline-p (headline info)
3096 "Return a non-nil value if HEADLINE element should be numbered.
3097 INFO is a plist used as a communication channel."
3098 (let ((sec-num (plist-get info :section-numbers))
3099 (level (org-export-get-relative-level headline info)))
3100 (if (wholenump sec-num) (<= level sec-num) sec-num)))
3102 (defun org-export-number-to-roman (n)
3103 "Convert integer N into a roman numeral."
3104 (let ((roman '((1000 . "M") (900 . "CM") (500 . "D") (400 . "CD")
3105 ( 100 . "C") ( 90 . "XC") ( 50 . "L") ( 40 . "XL")
3106 ( 10 . "X") ( 9 . "IX") ( 5 . "V") ( 4 . "IV")
3107 ( 1 . "I")))
3108 (res ""))
3109 (if (<= n 0)
3110 (number-to-string n)
3111 (while roman
3112 (if (>= n (caar roman))
3113 (setq n (- n (caar roman))
3114 res (concat res (cdar roman)))
3115 (pop roman)))
3116 res)))
3118 (defun org-export-get-tags (element info &optional tags)
3119 "Return list of tags associated to ELEMENT.
3121 ELEMENT has either an `headline' or an `inlinetask' type. INFO
3122 is a plist used as a communication channel.
3124 Select tags (see `org-export-select-tags') and exclude tags (see
3125 `org-export-exclude-tags') are removed from the list.
3127 When non-nil, optional argument TAGS should be a list of strings.
3128 Any tag belonging to this list will also be removed."
3129 (org-remove-if (lambda (tag) (or (member tag (plist-get info :select-tags))
3130 (member tag (plist-get info :exclude-tags))
3131 (member tag tags)))
3132 (org-element-property :tags element)))
3134 (defun org-export-first-sibling-p (headline info)
3135 "Non-nil when HEADLINE is the first sibling in its sub-tree.
3136 INFO is a plist used as a communication channel."
3137 (not (eq (org-element-type (org-export-get-previous-element headline info))
3138 'headline)))
3140 (defun org-export-last-sibling-p (headline info)
3141 "Non-nil when HEADLINE is the last sibling in its sub-tree.
3142 INFO is a plist used as a communication channel."
3143 (not (org-export-get-next-element headline info)))
3146 ;;;; For Links
3148 ;; `org-export-solidify-link-text' turns a string into a safer version
3149 ;; for links, replacing most non-standard characters with hyphens.
3151 ;; `org-export-get-coderef-format' returns an appropriate format
3152 ;; string for coderefs.
3154 ;; `org-export-inline-image-p' returns a non-nil value when the link
3155 ;; provided should be considered as an inline image.
3157 ;; `org-export-resolve-fuzzy-link' searches destination of fuzzy links
3158 ;; (i.e. links with "fuzzy" as type) within the parsed tree, and
3159 ;; returns an appropriate unique identifier when found, or nil.
3161 ;; `org-export-resolve-id-link' returns the first headline with
3162 ;; specified id or custom-id in parse tree, the path to the external
3163 ;; file with the id or nil when neither was found.
3165 ;; `org-export-resolve-coderef' associates a reference to a line
3166 ;; number in the element it belongs, or returns the reference itself
3167 ;; when the element isn't numbered.
3169 (defun org-export-solidify-link-text (s)
3170 "Take link text S and make a safe target out of it."
3171 (save-match-data
3172 (mapconcat 'identity (org-split-string s "[^a-zA-Z0-9_.-]+") "-")))
3174 (defun org-export-get-coderef-format (path desc)
3175 "Return format string for code reference link.
3176 PATH is the link path. DESC is its description."
3177 (save-match-data
3178 (cond ((not desc) "%s")
3179 ((string-match (regexp-quote (concat "(" path ")")) desc)
3180 (replace-match "%s" t t desc))
3181 (t desc))))
3183 (defun org-export-inline-image-p (link &optional rules)
3184 "Non-nil if LINK object points to an inline image.
3186 Optional argument is a set of RULES defining inline images. It
3187 is an alist where associations have the following shape:
3189 \(TYPE . REGEXP)
3191 Applying a rule means apply REGEXP against LINK's path when its
3192 type is TYPE. The function will return a non-nil value if any of
3193 the provided rules is non-nil. The default rule is
3194 `org-export-default-inline-image-rule'.
3196 This only applies to links without a description."
3197 (and (not (org-element-contents link))
3198 (let ((case-fold-search t)
3199 (rules (or rules org-export-default-inline-image-rule)))
3200 (catch 'exit
3201 (mapc
3202 (lambda (rule)
3203 (and (string= (org-element-property :type link) (car rule))
3204 (string-match (cdr rule)
3205 (org-element-property :path link))
3206 (throw 'exit t)))
3207 rules)
3208 ;; Return nil if no rule matched.
3209 nil))))
3211 (defun org-export-resolve-coderef (ref info)
3212 "Resolve a code reference REF.
3214 INFO is a plist used as a communication channel.
3216 Return associated line number in source code, or REF itself,
3217 depending on src-block or example element's switches."
3218 (org-element-map
3219 (plist-get info :parse-tree) '(example-block src-block)
3220 (lambda (el)
3221 (with-temp-buffer
3222 (insert (org-trim (org-element-property :value el)))
3223 (let* ((label-fmt (regexp-quote
3224 (or (org-element-property :label-fmt el)
3225 org-coderef-label-format)))
3226 (ref-re
3227 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
3228 (replace-regexp-in-string "%s" ref label-fmt nil t))))
3229 ;; Element containing REF is found. Resolve it to either
3230 ;; a label or a line number, as needed.
3231 (when (re-search-backward ref-re nil t)
3232 (cond
3233 ((org-element-property :use-labels el) ref)
3234 ((eq (org-element-property :number-lines el) 'continued)
3235 (+ (org-export-get-loc el info) (line-number-at-pos)))
3236 (t (line-number-at-pos)))))))
3237 info 'first-match))
3239 (defun org-export-resolve-fuzzy-link (link info)
3240 "Return LINK destination.
3242 INFO is a plist holding contextual information.
3244 Return value can be an object, an element, or nil:
3246 - If LINK path matches a target object (i.e. <<path>>) or
3247 element (i.e. \"#+TARGET: path\"), return it.
3249 - If LINK path exactly matches the name affiliated keyword
3250 \(i.e. #+NAME: path) of an element, return that element.
3252 - If LINK path exactly matches any headline name, return that
3253 element. If more than one headline share that name, priority
3254 will be given to the one with the closest common ancestor, if
3255 any, or the first one in the parse tree otherwise.
3257 - Otherwise, return nil.
3259 Assume LINK type is \"fuzzy\"."
3260 (let* ((path (org-element-property :path link))
3261 (match-title-p (eq (aref path 0) ?*)))
3262 (cond
3263 ;; First try to find a matching "<<path>>" unless user specified
3264 ;; he was looking for an headline (path starts with a *
3265 ;; character).
3266 ((and (not match-title-p)
3267 (loop for target in (plist-get info :target-list)
3268 when (string= (org-element-property :value target) path)
3269 return target)))
3270 ;; Then try to find an element with a matching "#+NAME: path"
3271 ;; affiliated keyword.
3272 ((and (not match-title-p)
3273 (org-element-map
3274 (plist-get info :parse-tree) org-element-all-elements
3275 (lambda (el)
3276 (when (string= (org-element-property :name el) path) el))
3277 info 'first-match)))
3278 ;; Last case: link either points to an headline or to
3279 ;; nothingness. Try to find the source, with priority given to
3280 ;; headlines with the closest common ancestor. If such candidate
3281 ;; is found, return it, otherwise return nil.
3283 (let ((find-headline
3284 (function
3285 ;; Return first headline whose `:raw-value' property
3286 ;; is NAME in parse tree DATA, or nil.
3287 (lambda (name data)
3288 (org-element-map
3289 data 'headline
3290 (lambda (headline)
3291 (when (string=
3292 (org-element-property :raw-value headline)
3293 name)
3294 headline))
3295 info 'first-match)))))
3296 ;; Search among headlines sharing an ancestor with link,
3297 ;; from closest to farthest.
3298 (or (catch 'exit
3299 (mapc
3300 (lambda (parent)
3301 (when (eq (org-element-type parent) 'headline)
3302 (let ((foundp (funcall find-headline path parent)))
3303 (when foundp (throw 'exit foundp)))))
3304 (org-export-get-genealogy link)) nil)
3305 ;; No match with a common ancestor: try the full parse-tree.
3306 (funcall find-headline
3307 (if match-title-p (substring path 1) path)
3308 (plist-get info :parse-tree))))))))
3310 (defun org-export-resolve-id-link (link info)
3311 "Return headline referenced as LINK destination.
3313 INFO is a plist used as a communication channel.
3315 Return value can be the headline element matched in current parse
3316 tree, a file name or nil. Assume LINK type is either \"id\" or
3317 \"custom-id\"."
3318 (let ((id (org-element-property :path link)))
3319 ;; First check if id is within the current parse tree.
3320 (or (org-element-map
3321 (plist-get info :parse-tree) 'headline
3322 (lambda (headline)
3323 (when (or (string= (org-element-property :id headline) id)
3324 (string= (org-element-property :custom-id headline) id))
3325 headline))
3326 info 'first-match)
3327 ;; Otherwise, look for external files.
3328 (cdr (assoc id (plist-get info :id-alist))))))
3330 (defun org-export-resolve-radio-link (link info)
3331 "Return radio-target object referenced as LINK destination.
3333 INFO is a plist used as a communication channel.
3335 Return value can be a radio-target object or nil. Assume LINK
3336 has type \"radio\"."
3337 (let ((path (org-element-property :path link)))
3338 (org-element-map
3339 (plist-get info :parse-tree) 'radio-target
3340 (lambda (radio)
3341 (when (equal (org-element-property :value radio) path) radio))
3342 info 'first-match)))
3345 ;;;; For Macros
3347 ;; `org-export-expand-macro' simply takes care of expanding macros.
3349 (defun org-export-expand-macro (macro info)
3350 "Expand MACRO and return it as a string.
3351 INFO is a plist holding export options."
3352 (let* ((key (org-element-property :key macro))
3353 (args (org-element-property :args macro))
3354 ;; User's macros are stored in the communication channel with
3355 ;; a ":macro-" prefix. Replace arguments in VALUE. Also
3356 ;; expand recursively macros within.
3357 (value (org-export-data
3358 (mapcar
3359 (lambda (obj)
3360 (if (not (stringp obj)) (org-export-data obj info)
3361 (replace-regexp-in-string
3362 "\\$[0-9]+"
3363 (lambda (arg)
3364 (nth (1- (string-to-number (substring arg 1))) args))
3365 obj)))
3366 (plist-get info (intern (format ":macro-%s" key))))
3367 info)))
3368 ;; VALUE starts with "(eval": it is a s-exp, `eval' it.
3369 (when (string-match "\\`(eval\\>" value) (setq value (eval (read value))))
3370 ;; Return string.
3371 (format "%s" (or value ""))))
3374 ;;;; For References
3376 ;; `org-export-get-ordinal' associates a sequence number to any object
3377 ;; or element.
3379 (defun org-export-get-ordinal (element info &optional types predicate)
3380 "Return ordinal number of an element or object.
3382 ELEMENT is the element or object considered. INFO is the plist
3383 used as a communication channel.
3385 Optional argument TYPES, when non-nil, is a list of element or
3386 object types, as symbols, that should also be counted in.
3387 Otherwise, only provided element's type is considered.
3389 Optional argument PREDICATE is a function returning a non-nil
3390 value if the current element or object should be counted in. It
3391 accepts two arguments: the element or object being considered and
3392 the plist used as a communication channel. This allows to count
3393 only a certain type of objects (i.e. inline images).
3395 Return value is a list of numbers if ELEMENT is an headline or an
3396 item. It is nil for keywords. It represents the footnote number
3397 for footnote definitions and footnote references. If ELEMENT is
3398 a target, return the same value as if ELEMENT was the closest
3399 table, item or headline containing the target. In any other
3400 case, return the sequence number of ELEMENT among elements or
3401 objects of the same type."
3402 ;; A target keyword, representing an invisible target, never has
3403 ;; a sequence number.
3404 (unless (eq (org-element-type element) 'keyword)
3405 ;; Ordinal of a target object refer to the ordinal of the closest
3406 ;; table, item, or headline containing the object.
3407 (when (eq (org-element-type element) 'target)
3408 (setq element
3409 (loop for parent in (org-export-get-genealogy element)
3410 when
3411 (memq
3412 (org-element-type parent)
3413 '(footnote-definition footnote-reference headline item
3414 table))
3415 return parent)))
3416 (case (org-element-type element)
3417 ;; Special case 1: An headline returns its number as a list.
3418 (headline (org-export-get-headline-number element info))
3419 ;; Special case 2: An item returns its number as a list.
3420 (item (let ((struct (org-element-property :structure element)))
3421 (org-list-get-item-number
3422 (org-element-property :begin element)
3423 struct
3424 (org-list-prevs-alist struct)
3425 (org-list-parents-alist struct))))
3426 ((footnote-definition footnote-reference)
3427 (org-export-get-footnote-number element info))
3428 (otherwise
3429 (let ((counter 0))
3430 ;; Increment counter until ELEMENT is found again.
3431 (org-element-map
3432 (plist-get info :parse-tree) (or types (org-element-type element))
3433 (lambda (el)
3434 (cond
3435 ((eq element el) (1+ counter))
3436 ((not predicate) (incf counter) nil)
3437 ((funcall predicate el info) (incf counter) nil)))
3438 info 'first-match))))))
3441 ;;;; For Src-Blocks
3443 ;; `org-export-get-loc' counts number of code lines accumulated in
3444 ;; src-block or example-block elements with a "+n" switch until
3445 ;; a given element, excluded. Note: "-n" switches reset that count.
3447 ;; `org-export-unravel-code' extracts source code (along with a code
3448 ;; references alist) from an `element-block' or `src-block' type
3449 ;; element.
3451 ;; `org-export-format-code' applies a formatting function to each line
3452 ;; of code, providing relative line number and code reference when
3453 ;; appropriate. Since it doesn't access the original element from
3454 ;; which the source code is coming, it expects from the code calling
3455 ;; it to know if lines should be numbered and if code references
3456 ;; should appear.
3458 ;; Eventually, `org-export-format-code-default' is a higher-level
3459 ;; function (it makes use of the two previous functions) which handles
3460 ;; line numbering and code references inclusion, and returns source
3461 ;; code in a format suitable for plain text or verbatim output.
3463 (defun org-export-get-loc (element info)
3464 "Return accumulated lines of code up to ELEMENT.
3466 INFO is the plist used as a communication channel.
3468 ELEMENT is excluded from count."
3469 (let ((loc 0))
3470 (org-element-map
3471 (plist-get info :parse-tree)
3472 `(src-block example-block ,(org-element-type element))
3473 (lambda (el)
3474 (cond
3475 ;; ELEMENT is reached: Quit the loop.
3476 ((eq el element))
3477 ;; Only count lines from src-block and example-block elements
3478 ;; with a "+n" or "-n" switch. A "-n" switch resets counter.
3479 ((not (memq (org-element-type el) '(src-block example-block))) nil)
3480 ((let ((linums (org-element-property :number-lines el)))
3481 (when linums
3482 ;; Accumulate locs or reset them.
3483 (let ((lines (org-count-lines
3484 (org-trim (org-element-property :value el)))))
3485 (setq loc (if (eq linums 'new) lines (+ loc lines))))))
3486 ;; Return nil to stay in the loop.
3487 nil)))
3488 info 'first-match)
3489 ;; Return value.
3490 loc))
3492 (defun org-export-unravel-code (element)
3493 "Clean source code and extract references out of it.
3495 ELEMENT has either a `src-block' an `example-block' type.
3497 Return a cons cell whose CAR is the source code, cleaned from any
3498 reference and protective comma and CDR is an alist between
3499 relative line number (integer) and name of code reference on that
3500 line (string)."
3501 (let* ((line 0) refs
3502 ;; Get code and clean it. Remove blank lines at its
3503 ;; beginning and end. Also remove protective commas.
3504 (code (let ((c (replace-regexp-in-string
3505 "\\`\\([ \t]*\n\\)+" ""
3506 (replace-regexp-in-string
3507 "\\(:?[ \t]*\n\\)*[ \t]*\\'" "\n"
3508 (org-element-property :value element)))))
3509 ;; If appropriate, remove global indentation.
3510 (unless (or org-src-preserve-indentation
3511 (org-element-property :preserve-indent element))
3512 (setq c (org-remove-indentation c)))
3513 ;; Free up the protected lines. Note: Org blocks
3514 ;; have commas at the beginning or every line.
3515 (if (string= (org-element-property :language element) "org")
3516 (replace-regexp-in-string "^," "" c)
3517 (replace-regexp-in-string
3518 "^\\(,\\)\\(:?\\*\\|[ \t]*#\\+\\)" "" c nil nil 1))))
3519 ;; Get format used for references.
3520 (label-fmt (regexp-quote
3521 (or (org-element-property :label-fmt element)
3522 org-coderef-label-format)))
3523 ;; Build a regexp matching a loc with a reference.
3524 (with-ref-re
3525 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)[ \t]*\\)$"
3526 (replace-regexp-in-string
3527 "%s" "\\([-a-zA-Z0-9_ ]+\\)" label-fmt nil t))))
3528 ;; Return value.
3529 (cons
3530 ;; Code with references removed.
3531 (org-element-normalize-string
3532 (mapconcat
3533 (lambda (loc)
3534 (incf line)
3535 (if (not (string-match with-ref-re loc)) loc
3536 ;; Ref line: remove ref, and signal its position in REFS.
3537 (push (cons line (match-string 3 loc)) refs)
3538 (replace-match "" nil nil loc 1)))
3539 (org-split-string code "\n") "\n"))
3540 ;; Reference alist.
3541 refs)))
3543 (defun org-export-format-code (code fun &optional num-lines ref-alist)
3544 "Format CODE by applying FUN line-wise and return it.
3546 CODE is a string representing the code to format. FUN is
3547 a function. It must accept three arguments: a line of
3548 code (string), the current line number (integer) or nil and the
3549 reference associated to the current line (string) or nil.
3551 Optional argument NUM-LINES can be an integer representing the
3552 number of code lines accumulated until the current code. Line
3553 numbers passed to FUN will take it into account. If it is nil,
3554 FUN's second argument will always be nil. This number can be
3555 obtained with `org-export-get-loc' function.
3557 Optional argument REF-ALIST can be an alist between relative line
3558 number (i.e. ignoring NUM-LINES) and the name of the code
3559 reference on it. If it is nil, FUN's third argument will always
3560 be nil. It can be obtained through the use of
3561 `org-export-unravel-code' function."
3562 (let ((--locs (org-split-string code "\n"))
3563 (--line 0))
3564 (org-element-normalize-string
3565 (mapconcat
3566 (lambda (--loc)
3567 (incf --line)
3568 (let ((--ref (cdr (assq --line ref-alist))))
3569 (funcall fun --loc (and num-lines (+ num-lines --line)) --ref)))
3570 --locs "\n"))))
3572 (defun org-export-format-code-default (element info)
3573 "Return source code from ELEMENT, formatted in a standard way.
3575 ELEMENT is either a `src-block' or `example-block' element. INFO
3576 is a plist used as a communication channel.
3578 This function takes care of line numbering and code references
3579 inclusion. Line numbers, when applicable, appear at the
3580 beginning of the line, separated from the code by two white
3581 spaces. Code references, on the other hand, appear flushed to
3582 the right, separated by six white spaces from the widest line of
3583 code."
3584 ;; Extract code and references.
3585 (let* ((code-info (org-export-unravel-code element))
3586 (code (car code-info))
3587 (code-lines (org-split-string code "\n"))
3588 (refs (and (org-element-property :retain-labels element)
3589 (cdr code-info)))
3590 ;; Handle line numbering.
3591 (num-start (case (org-element-property :number-lines element)
3592 (continued (org-export-get-loc element info))
3593 (new 0)))
3594 (num-fmt
3595 (and num-start
3596 (format "%%%ds "
3597 (length (number-to-string
3598 (+ (length code-lines) num-start))))))
3599 ;; Prepare references display, if required. Any reference
3600 ;; should start six columns after the widest line of code,
3601 ;; wrapped with parenthesis.
3602 (max-width
3603 (+ (apply 'max (mapcar 'length code-lines))
3604 (if (not num-start) 0 (length (format num-fmt num-start))))))
3605 (org-export-format-code
3606 code
3607 (lambda (loc line-num ref)
3608 (let ((number-str (and num-fmt (format num-fmt line-num))))
3609 (concat
3610 number-str
3612 (and ref
3613 (concat (make-string
3614 (- (+ 6 max-width)
3615 (+ (length loc) (length number-str))) ? )
3616 (format "(%s)" ref))))))
3617 num-start refs)))
3620 ;;;; For Tables
3622 ;; `org-export-table-has-special-column-p' and and
3623 ;; `org-export-table-row-is-special-p' are predicates used to look for
3624 ;; meta-information about the table structure.
3626 ;; `org-table-has-header-p' tells when the rows before the first rule
3627 ;; should be considered as table's header.
3629 ;; `org-export-table-cell-width', `org-export-table-cell-alignment'
3630 ;; and `org-export-table-cell-borders' extract information from
3631 ;; a table-cell element.
3633 ;; `org-export-table-dimensions' gives the number on rows and columns
3634 ;; in the table, ignoring horizontal rules and special columns.
3635 ;; `org-export-table-cell-address', given a table-cell object, returns
3636 ;; the absolute address of a cell. On the other hand,
3637 ;; `org-export-get-table-cell-at' does the contrary.
3639 ;; `org-export-table-cell-starts-colgroup-p',
3640 ;; `org-export-table-cell-ends-colgroup-p',
3641 ;; `org-export-table-row-starts-rowgroup-p',
3642 ;; `org-export-table-row-ends-rowgroup-p',
3643 ;; `org-export-table-row-starts-header-p' and
3644 ;; `org-export-table-row-ends-header-p' indicate position of current
3645 ;; row or cell within the table.
3647 (defun org-export-table-has-special-column-p (table)
3648 "Non-nil when TABLE has a special column.
3649 All special columns will be ignored during export."
3650 ;; The table has a special column when every first cell of every row
3651 ;; has an empty value or contains a symbol among "/", "#", "!", "$",
3652 ;; "*" "_" and "^". Though, do not consider a first row containing
3653 ;; only empty cells as special.
3654 (let ((special-column-p 'empty))
3655 (catch 'exit
3656 (mapc
3657 (lambda (row)
3658 (when (eq (org-element-property :type row) 'standard)
3659 (let ((value (org-element-contents
3660 (car (org-element-contents row)))))
3661 (cond ((member value '(("/") ("#") ("!") ("$") ("*") ("_") ("^")))
3662 (setq special-column-p 'special))
3663 ((not value))
3664 (t (throw 'exit nil))))))
3665 (org-element-contents table))
3666 (eq special-column-p 'special))))
3668 (defun org-export-table-has-header-p (table info)
3669 "Non-nil when TABLE has an header.
3671 INFO is a plist used as a communication channel.
3673 A table has an header when it contains at least two row groups."
3674 (let ((rowgroup 1) row-flag)
3675 (org-element-map
3676 table 'table-row
3677 (lambda (row)
3678 (cond
3679 ((> rowgroup 1) t)
3680 ((and row-flag (eq (org-element-property :type row) 'rule))
3681 (incf rowgroup) (setq row-flag nil))
3682 ((and (not row-flag) (eq (org-element-property :type row) 'standard))
3683 (setq row-flag t) nil)))
3684 info)))
3686 (defun org-export-table-row-is-special-p (table-row info)
3687 "Non-nil if TABLE-ROW is considered special.
3689 INFO is a plist used as the communication channel.
3691 All special rows will be ignored during export."
3692 (when (eq (org-element-property :type table-row) 'standard)
3693 (let ((first-cell (org-element-contents
3694 (car (org-element-contents table-row)))))
3695 ;; A row is special either when...
3697 ;; ... it starts with a field only containing "/",
3698 (equal first-cell '("/"))
3699 ;; ... the table contains a special column and the row start
3700 ;; with a marking character among, "^", "_", "$" or "!",
3701 (and (org-export-table-has-special-column-p
3702 (org-export-get-parent table-row))
3703 (member first-cell '(("^") ("_") ("$") ("!"))))
3704 ;; ... it contains only alignment cookies and empty cells.
3705 (let ((special-row-p 'empty))
3706 (catch 'exit
3707 (mapc
3708 (lambda (cell)
3709 (let ((value (org-element-contents cell)))
3710 ;; Since VALUE is a secondary string, the following
3711 ;; checks avoid expanding it with `org-export-data'.
3712 (cond ((not value))
3713 ((and (not (cdr value))
3714 (stringp (car value))
3715 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'"
3716 (car value)))
3717 (setq special-row-p 'cookie))
3718 (t (throw 'exit nil)))))
3719 (org-element-contents table-row))
3720 (eq special-row-p 'cookie)))))))
3722 (defun org-export-table-row-group (table-row info)
3723 "Return TABLE-ROW's group.
3725 INFO is a plist used as the communication channel.
3727 Return value is the group number, as an integer, or nil special
3728 rows and table rules. Group 1 is also table's header."
3729 (unless (or (eq (org-element-property :type table-row) 'rule)
3730 (org-export-table-row-is-special-p table-row info))
3731 (let ((group 0) row-flag)
3732 (catch 'found
3733 (mapc
3734 (lambda (row)
3735 (cond
3736 ((and (eq (org-element-property :type row) 'standard)
3737 (not (org-export-table-row-is-special-p row info)))
3738 (unless row-flag (incf group) (setq row-flag t)))
3739 ((eq (org-element-property :type row) 'rule)
3740 (setq row-flag nil)))
3741 (when (eq table-row row) (throw 'found group)))
3742 (org-element-contents (org-export-get-parent table-row)))))))
3744 (defun org-export-table-cell-width (table-cell info)
3745 "Return TABLE-CELL contents width.
3747 INFO is a plist used as the communication channel.
3749 Return value is the width given by the last width cookie in the
3750 same column as TABLE-CELL, or nil."
3751 (let* ((row (org-export-get-parent table-cell))
3752 (column (let ((cells (org-element-contents row)))
3753 (- (length cells) (length (memq table-cell cells)))))
3754 (table (org-export-get-parent-table table-cell))
3755 cookie-width)
3756 (mapc
3757 (lambda (row)
3758 (cond
3759 ;; In a special row, try to find a width cookie at COLUMN.
3760 ((org-export-table-row-is-special-p row info)
3761 (let ((value (org-element-contents
3762 (elt (org-element-contents row) column))))
3763 ;; The following checks avoid expanding unnecessarily the
3764 ;; cell with `org-export-data'
3765 (when (and value
3766 (not (cdr value))
3767 (stringp (car value))
3768 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'" (car value))
3769 (match-string 1 (car value)))
3770 (setq cookie-width
3771 (string-to-number (match-string 1 (car value)))))))
3772 ;; Ignore table rules.
3773 ((eq (org-element-property :type row) 'rule))))
3774 (org-element-contents table))
3775 ;; Return value.
3776 cookie-width))
3778 (defun org-export-table-cell-alignment (table-cell info)
3779 "Return TABLE-CELL contents alignment.
3781 INFO is a plist used as the communication channel.
3783 Return alignment as specified by the last alignment cookie in the
3784 same column as TABLE-CELL. If no such cookie is found, a default
3785 alignment value will be deduced from fraction of numbers in the
3786 column (see `org-table-number-fraction' for more information).
3787 Possible values are `left', `right' and `center'."
3788 (let* ((row (org-export-get-parent table-cell))
3789 (column (let ((cells (org-element-contents row)))
3790 (- (length cells) (length (memq table-cell cells)))))
3791 (table (org-export-get-parent-table table-cell))
3792 (number-cells 0)
3793 (total-cells 0)
3794 cookie-align)
3795 (mapc
3796 (lambda (row)
3797 (cond
3798 ;; In a special row, try to find an alignment cookie at
3799 ;; COLUMN.
3800 ((org-export-table-row-is-special-p row info)
3801 (let ((value (org-element-contents
3802 (elt (org-element-contents row) column))))
3803 ;; Since VALUE is a secondary string, the following checks
3804 ;; avoid useless expansion through `org-export-data'.
3805 (when (and value
3806 (not (cdr value))
3807 (stringp (car value))
3808 (string-match "\\`<\\([lrc]\\)?\\([0-9]+\\)?>\\'"
3809 (car value))
3810 (match-string 1 (car value)))
3811 (setq cookie-align (match-string 1 (car value))))))
3812 ;; Ignore table rules.
3813 ((eq (org-element-property :type row) 'rule))
3814 ;; In a standard row, check if cell's contents are expressing
3815 ;; some kind of number. Increase NUMBER-CELLS accordingly.
3816 ;; Though, don't bother if an alignment cookie has already
3817 ;; defined cell's alignment.
3818 ((not cookie-align)
3819 (let ((value (org-export-data
3820 (org-element-contents
3821 (elt (org-element-contents row) column))
3822 info)))
3823 (incf total-cells)
3824 (when (string-match org-table-number-regexp value)
3825 (incf number-cells))))))
3826 (org-element-contents table))
3827 ;; Return value. Alignment specified by cookies has precedence
3828 ;; over alignment deduced from cells contents.
3829 (cond ((equal cookie-align "l") 'left)
3830 ((equal cookie-align "r") 'right)
3831 ((equal cookie-align "c") 'center)
3832 ((>= (/ (float number-cells) total-cells) org-table-number-fraction)
3833 'right)
3834 (t 'left))))
3836 (defun org-export-table-cell-borders (table-cell info)
3837 "Return TABLE-CELL borders.
3839 INFO is a plist used as a communication channel.
3841 Return value is a list of symbols, or nil. Possible values are:
3842 `top', `bottom', `above', `below', `left' and `right'. Note:
3843 `top' (resp. `bottom') only happen for a cell in the first
3844 row (resp. last row) of the table, ignoring table rules, if any.
3846 Returned borders ignore special rows."
3847 (let* ((row (org-export-get-parent table-cell))
3848 (table (org-export-get-parent-table table-cell))
3849 borders)
3850 ;; Top/above border? TABLE-CELL has a border above when a rule
3851 ;; used to demarcate row groups can be found above. Hence,
3852 ;; finding a rule isn't sufficient to push `above' in BORDERS:
3853 ;; another regular row has to be found above that rule.
3854 (let (rule-flag)
3855 (catch 'exit
3856 (mapc (lambda (row)
3857 (cond ((eq (org-element-property :type row) 'rule)
3858 (setq rule-flag t))
3859 ((not (org-export-table-row-is-special-p row info))
3860 (if rule-flag (throw 'exit (push 'above borders))
3861 (throw 'exit nil)))))
3862 ;; Look at every row before the current one.
3863 (cdr (memq row (reverse (org-element-contents table)))))
3864 ;; No rule above, or rule found starts the table (ignoring any
3865 ;; special row): TABLE-CELL is at the top of the table.
3866 (when rule-flag (push 'above borders))
3867 (push 'top borders)))
3868 ;; Bottom/below border? TABLE-CELL has a border below when next
3869 ;; non-regular row below is a rule.
3870 (let (rule-flag)
3871 (catch 'exit
3872 (mapc (lambda (row)
3873 (cond ((eq (org-element-property :type row) 'rule)
3874 (setq rule-flag t))
3875 ((not (org-export-table-row-is-special-p row info))
3876 (if rule-flag (throw 'exit (push 'below borders))
3877 (throw 'exit nil)))))
3878 ;; Look at every row after the current one.
3879 (cdr (memq row (org-element-contents table))))
3880 ;; No rule below, or rule found ends the table (modulo some
3881 ;; special row): TABLE-CELL is at the bottom of the table.
3882 (when rule-flag (push 'below borders))
3883 (push 'bottom borders)))
3884 ;; Right/left borders? They can only be specified by column
3885 ;; groups. Column groups are defined in a row starting with "/".
3886 ;; Also a column groups row only contains "<", "<>", ">" or blank
3887 ;; cells.
3888 (catch 'exit
3889 (let ((column (let ((cells (org-element-contents row)))
3890 (- (length cells) (length (memq table-cell cells))))))
3891 (mapc
3892 (lambda (row)
3893 (unless (eq (org-element-property :type row) 'rule)
3894 (when (equal (org-element-contents
3895 (car (org-element-contents row)))
3896 '("/"))
3897 (let ((column-groups
3898 (mapcar
3899 (lambda (cell)
3900 (let ((value (org-element-contents cell)))
3901 (when (member value '(("<") ("<>") (">") nil))
3902 (car value))))
3903 (org-element-contents row))))
3904 ;; There's a left border when previous cell, if
3905 ;; any, ends a group, or current one starts one.
3906 (when (or (and (not (zerop column))
3907 (member (elt column-groups (1- column))
3908 '(">" "<>")))
3909 (member (elt column-groups column) '("<" "<>")))
3910 (push 'left borders))
3911 ;; There's a right border when next cell, if any,
3912 ;; starts a group, or current one ends one.
3913 (when (or (and (/= (1+ column) (length column-groups))
3914 (member (elt column-groups (1+ column))
3915 '("<" "<>")))
3916 (member (elt column-groups column) '(">" "<>")))
3917 (push 'right borders))
3918 (throw 'exit nil)))))
3919 ;; Table rows are read in reverse order so last column groups
3920 ;; row has precedence over any previous one.
3921 (reverse (org-element-contents table)))))
3922 ;; Return value.
3923 borders))
3925 (defun org-export-table-cell-starts-colgroup-p (table-cell info)
3926 "Non-nil when TABLE-CELL is at the beginning of a row group.
3927 INFO is a plist used as a communication channel."
3928 ;; A cell starts a column group either when it is at the beginning
3929 ;; of a row (or after the special column, if any) or when it has
3930 ;; a left border.
3931 (or (eq (org-element-map
3932 (org-export-get-parent table-cell)
3933 'table-cell 'identity info 'first-match)
3934 table-cell)
3935 (memq 'left (org-export-table-cell-borders table-cell info))))
3937 (defun org-export-table-cell-ends-colgroup-p (table-cell info)
3938 "Non-nil when TABLE-CELL is at the end of a row group.
3939 INFO is a plist used as a communication channel."
3940 ;; A cell ends a column group either when it is at the end of a row
3941 ;; or when it has a right border.
3942 (or (eq (car (last (org-element-contents
3943 (org-export-get-parent table-cell))))
3944 table-cell)
3945 (memq 'right (org-export-table-cell-borders table-cell info))))
3947 (defun org-export-table-row-starts-rowgroup-p (table-row info)
3948 "Non-nil when TABLE-ROW is at the beginning of a column group.
3949 INFO is a plist used as a communication channel."
3950 (unless (or (eq (org-element-property :type table-row) 'rule)
3951 (org-export-table-row-is-special-p table-row info))
3952 (let ((borders (org-export-table-cell-borders
3953 (car (org-element-contents table-row)) info)))
3954 (or (memq 'top borders) (memq 'above borders)))))
3956 (defun org-export-table-row-ends-rowgroup-p (table-row info)
3957 "Non-nil when TABLE-ROW is at the end of a column group.
3958 INFO is a plist used as a communication channel."
3959 (unless (or (eq (org-element-property :type table-row) 'rule)
3960 (org-export-table-row-is-special-p table-row info))
3961 (let ((borders (org-export-table-cell-borders
3962 (car (org-element-contents table-row)) info)))
3963 (or (memq 'bottom borders) (memq 'below borders)))))
3965 (defun org-export-table-row-starts-header-p (table-row info)
3966 "Non-nil when TABLE-ROW is the first table header's row.
3967 INFO is a plist used as a communication channel."
3968 (and (org-export-table-has-header-p
3969 (org-export-get-parent-table table-row) info)
3970 (org-export-table-row-starts-rowgroup-p table-row info)
3971 (= (org-export-table-row-group table-row info) 1)))
3973 (defun org-export-table-row-ends-header-p (table-row info)
3974 "Non-nil when TABLE-ROW is the last table header's row.
3975 INFO is a plist used as a communication channel."
3976 (and (org-export-table-has-header-p
3977 (org-export-get-parent-table table-row) info)
3978 (org-export-table-row-ends-rowgroup-p table-row info)
3979 (= (org-export-table-row-group table-row info) 1)))
3981 (defun org-export-table-dimensions (table info)
3982 "Return TABLE dimensions.
3984 INFO is a plist used as a communication channel.
3986 Return value is a CONS like (ROWS . COLUMNS) where
3987 ROWS (resp. COLUMNS) is the number of exportable
3988 rows (resp. columns)."
3989 (let (first-row (columns 0) (rows 0))
3990 ;; Set number of rows, and extract first one.
3991 (org-element-map
3992 table 'table-row
3993 (lambda (row)
3994 (when (eq (org-element-property :type row) 'standard)
3995 (incf rows)
3996 (unless first-row (setq first-row row)))) info)
3997 ;; Set number of columns.
3998 (org-element-map first-row 'table-cell (lambda (cell) (incf columns)) info)
3999 ;; Return value.
4000 (cons rows columns)))
4002 (defun org-export-table-cell-address (table-cell info)
4003 "Return address of a regular TABLE-CELL object.
4005 TABLE-CELL is the cell considered. INFO is a plist used as
4006 a communication channel.
4008 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
4009 zero-based index. Only exportable cells are considered. The
4010 function returns nil for other cells."
4011 (let* ((table-row (org-export-get-parent table-cell))
4012 (table (org-export-get-parent-table table-cell)))
4013 ;; Ignore cells in special rows or in special column.
4014 (unless (or (org-export-table-row-is-special-p table-row info)
4015 (and (org-export-table-has-special-column-p table)
4016 (eq (car (org-element-contents table-row)) table-cell)))
4017 (cons
4018 ;; Row number.
4019 (let ((row-count 0))
4020 (org-element-map
4021 table 'table-row
4022 (lambda (row)
4023 (cond ((eq (org-element-property :type row) 'rule) nil)
4024 ((eq row table-row) row-count)
4025 (t (incf row-count) nil)))
4026 info 'first-match))
4027 ;; Column number.
4028 (let ((col-count 0))
4029 (org-element-map
4030 table-row 'table-cell
4031 (lambda (cell)
4032 (if (eq cell table-cell) col-count (incf col-count) nil))
4033 info 'first-match))))))
4035 (defun org-export-get-table-cell-at (address table info)
4036 "Return regular table-cell object at ADDRESS in TABLE.
4038 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
4039 zero-based index. TABLE is a table type element. INFO is
4040 a plist used as a communication channel.
4042 If no table-cell, among exportable cells, is found at ADDRESS,
4043 return nil."
4044 (let ((column-pos (cdr address)) (column-count 0))
4045 (org-element-map
4046 ;; Row at (car address) or nil.
4047 (let ((row-pos (car address)) (row-count 0))
4048 (org-element-map
4049 table 'table-row
4050 (lambda (row)
4051 (cond ((eq (org-element-property :type row) 'rule) nil)
4052 ((= row-count row-pos) row)
4053 (t (incf row-count) nil)))
4054 info 'first-match))
4055 'table-cell
4056 (lambda (cell)
4057 (if (= column-count column-pos) cell
4058 (incf column-count) nil))
4059 info 'first-match)))
4062 ;;;; For Tables Of Contents
4064 ;; `org-export-collect-headlines' builds a list of all exportable
4065 ;; headline elements, maybe limited to a certain depth. One can then
4066 ;; easily parse it and transcode it.
4068 ;; Building lists of tables, figures or listings is quite similar.
4069 ;; Once the generic function `org-export-collect-elements' is defined,
4070 ;; `org-export-collect-tables', `org-export-collect-figures' and
4071 ;; `org-export-collect-listings' can be derived from it.
4073 (defun org-export-collect-headlines (info &optional n)
4074 "Collect headlines in order to build a table of contents.
4076 INFO is a plist used as a communication channel.
4078 When non-nil, optional argument N must be an integer. It
4079 specifies the depth of the table of contents.
4081 Return a list of all exportable headlines as parsed elements."
4082 (org-element-map
4083 (plist-get info :parse-tree)
4084 'headline
4085 (lambda (headline)
4086 ;; Strip contents from HEADLINE.
4087 (let ((relative-level (org-export-get-relative-level headline info)))
4088 (unless (and n (> relative-level n)) headline)))
4089 info))
4091 (defun org-export-collect-elements (type info &optional predicate)
4092 "Collect referenceable elements of a determined type.
4094 TYPE can be a symbol or a list of symbols specifying element
4095 types to search. Only elements with a caption are collected.
4097 INFO is a plist used as a communication channel.
4099 When non-nil, optional argument PREDICATE is a function accepting
4100 one argument, an element of type TYPE. It returns a non-nil
4101 value when that element should be collected.
4103 Return a list of all elements found, in order of appearance."
4104 (org-element-map
4105 (plist-get info :parse-tree) type
4106 (lambda (element)
4107 (and (org-element-property :caption element)
4108 (or (not predicate) (funcall predicate element))
4109 element))
4110 info))
4112 (defun org-export-collect-tables (info)
4113 "Build a list of tables.
4114 INFO is a plist used as a communication channel.
4116 Return a list of table elements with a caption."
4117 (org-export-collect-elements 'table info))
4119 (defun org-export-collect-figures (info predicate)
4120 "Build a list of figures.
4122 INFO is a plist used as a communication channel. PREDICATE is
4123 a function which accepts one argument: a paragraph element and
4124 whose return value is non-nil when that element should be
4125 collected.
4127 A figure is a paragraph type element, with a caption, verifying
4128 PREDICATE. The latter has to be provided since a \"figure\" is
4129 a vague concept that may depend on back-end.
4131 Return a list of elements recognized as figures."
4132 (org-export-collect-elements 'paragraph info predicate))
4134 (defun org-export-collect-listings (info)
4135 "Build a list of src blocks.
4137 INFO is a plist used as a communication channel.
4139 Return a list of src-block elements with a caption."
4140 (org-export-collect-elements 'src-block info))
4143 ;;;; Topology
4145 ;; Here are various functions to retrieve information about the
4146 ;; neighbourhood of a given element or object. Neighbours of interest
4147 ;; are direct parent (`org-export-get-parent'), parent headline
4148 ;; (`org-export-get-parent-headline'), first element containing an
4149 ;; object, (`org-export-get-parent-element'), parent table
4150 ;; (`org-export-get-parent-table'), previous element or object
4151 ;; (`org-export-get-previous-element') and next element or object
4152 ;; (`org-export-get-next-element').
4154 ;; `org-export-get-genealogy' returns the full genealogy of a given
4155 ;; element or object, from closest parent to full parse tree.
4157 (defun org-export-get-parent (blob)
4158 "Return BLOB parent or nil.
4159 BLOB is the element or object considered."
4160 (org-element-property :parent blob))
4162 (defun org-export-get-genealogy (blob)
4163 "Return full genealogy relative to a given element or object.
4165 BLOB is the element or object being considered.
4167 Ancestors are returned from closest to farthest, the last one
4168 being the full parse tree."
4169 (let (genealogy (parent blob))
4170 (while (setq parent (org-element-property :parent parent))
4171 (push parent genealogy))
4172 (nreverse genealogy)))
4174 (defun org-export-get-parent-headline (blob)
4175 "Return BLOB parent headline or nil.
4176 BLOB is the element or object being considered."
4177 (let ((parent blob))
4178 (while (and (setq parent (org-element-property :parent parent))
4179 (not (eq (org-element-type parent) 'headline))))
4180 parent))
4182 (defun org-export-get-parent-element (object)
4183 "Return first element containing OBJECT or nil.
4184 OBJECT is the object to consider."
4185 (let ((parent object))
4186 (while (and (setq parent (org-element-property :parent parent))
4187 (memq (org-element-type parent) org-element-all-objects)))
4188 parent))
4190 (defun org-export-get-parent-table (object)
4191 "Return OBJECT parent table or nil.
4192 OBJECT is either a `table-cell' or `table-element' type object."
4193 (let ((parent object))
4194 (while (and (setq parent (org-element-property :parent parent))
4195 (not (eq (org-element-type parent) 'table))))
4196 parent))
4198 (defun org-export-get-previous-element (blob info)
4199 "Return previous element or object.
4200 BLOB is an element or object. INFO is a plist used as
4201 a communication channel. Return previous exportable element or
4202 object, a string, or nil."
4203 (let (prev)
4204 (catch 'exit
4205 (mapc (lambda (obj)
4206 (cond ((eq obj blob) (throw 'exit prev))
4207 ((memq obj (plist-get info :ignore-list)))
4208 (t (setq prev obj))))
4209 (org-element-contents (org-export-get-parent blob))))))
4211 (defun org-export-get-next-element (blob info)
4212 "Return next element or object.
4213 BLOB is an element or object. INFO is a plist used as
4214 a communication channel. Return next exportable element or
4215 object, a string, or nil."
4216 (catch 'found
4217 (mapc (lambda (obj)
4218 (unless (memq obj (plist-get info :ignore-list))
4219 (throw 'found obj)))
4220 (cdr (memq blob (org-element-contents (org-export-get-parent blob)))))
4221 nil))
4224 ;;;; Translation
4226 ;; `org-export-translate' translates a string according to language
4227 ;; specified by LANGUAGE keyword or `org-export-language-setup'
4228 ;; variable and a specified charset. `org-export-dictionary' contains
4229 ;; the dictionary used for the translation.
4231 (defconst org-export-dictionary
4232 '(("Author"
4233 ("fr"
4234 :ascii "Auteur"
4235 :latin1 "Auteur"
4236 :utf-8 "Auteur"))
4237 ("Date"
4238 ("fr"
4239 :ascii "Date"
4240 :latin1 "Date"
4241 :utf-8 "Date"))
4242 ("Equation")
4243 ("Figure")
4244 ("Footnotes"
4245 ("fr"
4246 :ascii "Notes de bas de page"
4247 :latin1 "Notes de bas de page"
4248 :utf-8 "Notes de bas de page"))
4249 ("List of Listings"
4250 ("fr"
4251 :ascii "Liste des programmes"
4252 :latin1 "Liste des programmes"
4253 :utf-8 "Liste des programmes"))
4254 ("List of Tables"
4255 ("fr"
4256 :ascii "Liste des tableaux"
4257 :latin1 "Liste des tableaux"
4258 :utf-8 "Liste des tableaux"))
4259 ("Listing %d:"
4260 ("fr"
4261 :ascii "Programme %d :"
4262 :latin1 "Programme %d :"
4263 :utf-8 "Programme nº %d :"))
4264 ("Listing %d: %s"
4265 ("fr"
4266 :ascii "Programme %d : %s"
4267 :latin1 "Programme %d : %s"
4268 :utf-8 "Programme nº %d : %s"))
4269 ("See section %s"
4270 ("fr"
4271 :ascii "cf. section %s"
4272 :latin1 "cf. section %s"
4273 :utf-8 "cf. section %s"))
4274 ("Table %d:"
4275 ("fr"
4276 :ascii "Tableau %d :"
4277 :latin1 "Tableau %d :"
4278 :utf-8 "Tableau nº %d :"))
4279 ("Table %d: %s"
4280 ("fr"
4281 :ascii "Tableau %d : %s"
4282 :latin1 "Tableau %d : %s"
4283 :utf-8 "Tableau nº %d : %s"))
4284 ("Table of Contents"
4285 ("fr"
4286 :ascii "Sommaire"
4287 :latin1 "Table des matières"
4288 :utf-8 "Table des matières"))
4289 ("Unknown reference"
4290 ("fr"
4291 :ascii "Destination inconnue"
4292 :latin1 "Référence inconnue"
4293 :utf-8 "Référence inconnue")))
4294 "Dictionary for export engine.
4296 Alist whose CAR is the string to translate and CDR is an alist
4297 whose CAR is the language string and CDR is a plist whose
4298 properties are possible charsets and values translated terms.
4300 It is used as a database for `org-export-translate'. Since this
4301 function returns the string as-is if no translation was found,
4302 the variable only needs to record values different from the
4303 entry.")
4305 (defun org-export-translate (s encoding info)
4306 "Translate string S according to language specification.
4308 ENCODING is a symbol among `:ascii', `:html', `:latex', `:latin1'
4309 and `:utf-8'. INFO is a plist used as a communication channel.
4311 Translation depends on `:language' property. Return the
4312 translated string. If no translation is found return S."
4313 (let ((lang (plist-get info :language))
4314 (translations (cdr (assoc s org-export-dictionary))))
4315 (or (plist-get (cdr (assoc lang translations)) encoding) s)))
4319 ;;; The Dispatcher
4321 ;; `org-export-dispatch' is the standard interactive way to start an
4322 ;; export process. It uses `org-export-dispatch-ui' as a subroutine
4323 ;; for its interface. Most commons back-ends should have an entry in
4324 ;; it.
4326 ;;;###autoload
4327 (defun org-export-dispatch ()
4328 "Export dispatcher for Org mode.
4330 It provides an access to common export related tasks in a buffer.
4331 Its interface comes in two flavours: standard and expert. While
4332 both share the same set of bindings, only the former displays the
4333 valid keys associations. Set `org-export-dispatch-use-expert-ui'
4334 to switch to one or the other.
4336 Return an error if key pressed has no associated command."
4337 (interactive)
4338 (let* ((input (org-export-dispatch-ui
4339 (if (listp org-export-initial-scope) org-export-initial-scope
4340 (list org-export-initial-scope))
4341 org-export-dispatch-use-expert-ui))
4342 (raw-key (car input))
4343 (optns (cdr input)))
4344 ;; Translate "C-a", "C-b"... into "a", "b"... Then take action
4345 ;; depending on user's key pressed.
4346 (case (if (< raw-key 27) (+ raw-key 96) raw-key)
4347 ;; Allow to quit with "q" key.
4348 (?q nil)
4349 ;; Export with `e-ascii' back-end.
4350 ((?A ?N ?U)
4351 (org-e-ascii-export-as-ascii
4352 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)
4353 `(:ascii-charset ,(case raw-key (?A 'ascii) (?N 'latin1) (t 'utf-8)))))
4354 ((?a ?n ?u)
4355 (org-e-ascii-export-to-ascii
4356 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)
4357 `(:ascii-charset ,(case raw-key (?a 'ascii) (?n 'latin1) (t 'utf-8)))))
4358 ;; Export with `e-latex' back-end.
4359 (?L (org-e-latex-export-as-latex
4360 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
4362 (org-e-latex-export-to-latex
4363 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
4365 (org-e-latex-export-to-pdf
4366 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
4368 (org-open-file
4369 (org-e-latex-export-to-pdf
4370 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))))
4371 ;; Export with `e-html' back-end.
4373 (org-e-html-export-as-html
4374 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
4376 (org-e-html-export-to-html
4377 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
4379 (org-open-file
4380 (org-e-html-export-to-html
4381 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))))
4382 ;; Export with `e-odt' back-end.
4384 (org-e-odt-export-to-odt
4385 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
4387 (org-open-file
4388 (org-e-odt-export-to-odt
4389 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))))
4390 ;; Publishing facilities
4392 (org-e-publish-current-file (memq 'force optns)))
4394 (org-e-publish-current-project (memq 'force optns)))
4396 (let ((project
4397 (assoc (org-icompleting-read
4398 "Publish project: " org-e-publish-project-alist nil t)
4399 org-e-publish-project-alist)))
4400 (org-e-publish project (memq 'force optns))))
4402 (org-e-publish-all (memq 'force optns)))
4403 ;; Undefined command.
4404 (t (error "No command associated with key %s"
4405 (char-to-string raw-key))))))
4407 (defun org-export-dispatch-ui (options expertp)
4408 "Handle interface for `org-export-dispatch'.
4410 OPTIONS is a list containing current interactive options set for
4411 export. It can contain any of the following symbols:
4412 `body' toggles a body-only export
4413 `subtree' restricts export to current subtree
4414 `visible' restricts export to visible part of buffer.
4415 `force' force publishing files.
4417 EXPERTP, when non-nil, triggers expert UI. In that case, no help
4418 buffer is provided, but indications about currently active
4419 options are given in the prompt. Moreover, \[?] allows to switch
4420 back to standard interface.
4422 Return value is a list with key pressed as CAR and a list of
4423 final interactive export options as CDR."
4424 (let ((help
4425 (format "---- (Options) -------------------------------------------
4427 \[1] Body only: %s [2] Export scope: %s
4428 \[3] Visible only: %s [4] Force publishing: %s
4431 --- (ASCII/Latin-1/UTF-8 Export) -------------------------
4433 \[a/n/u] to TXT file [A/N/U] to temporary buffer
4435 --- (HTML Export) ----------------------------------------
4437 \[h] to HTML file [b] ... and open it
4438 \[H] to temporary buffer
4440 --- (LaTeX Export) ---------------------------------------
4442 \[l] to TEX file [L] to temporary buffer
4443 \[p] to PDF file [d] ... and open it
4445 --- (ODF Export) -----------------------------------------
4447 \[o] to ODT file [O] ... and open it
4449 --- (Publish) --------------------------------------------
4451 \[F] current file [P] current project
4452 \[X] a project [E] every project"
4453 (if (memq 'body options) "On " "Off")
4454 (if (memq 'subtree options) "Subtree" "Buffer ")
4455 (if (memq 'visible options) "On " "Off")
4456 (if (memq 'force options) "On " "Off")))
4457 (standard-prompt "Export command: ")
4458 (expert-prompt (format "Export command (%s%s%s%s): "
4459 (if (memq 'body options) "b" "-")
4460 (if (memq 'subtree options) "s" "-")
4461 (if (memq 'visible options) "v" "-")
4462 (if (memq 'force options) "f" "-")))
4463 (handle-keypress
4464 (function
4465 ;; Read a character from command input, toggling interactive
4466 ;; options when applicable. PROMPT is the displayed prompt,
4467 ;; as a string.
4468 (lambda (prompt)
4469 (let ((key (read-char-exclusive prompt)))
4470 (cond
4471 ;; Ignore non-standard characters (i.e. "M-a").
4472 ((not (characterp key)) (org-export-dispatch-ui options expertp))
4473 ;; Help key: Switch back to standard interface if
4474 ;; expert UI was active.
4475 ((eq key ??) (org-export-dispatch-ui options nil))
4476 ;; Toggle export options.
4477 ((memq key '(?1 ?2 ?3 ?4))
4478 (org-export-dispatch-ui
4479 (let ((option (case key (?1 'body) (?2 'subtree) (?3 'visible)
4480 (?4 'force))))
4481 (if (memq option options) (remq option options)
4482 (cons option options)))
4483 expertp))
4484 ;; Action selected: Send key and options back to
4485 ;; `org-export-dispatch'.
4486 (t (cons key options))))))))
4487 ;; With expert UI, just read key with a fancy prompt. In standard
4488 ;; UI, display an intrusive help buffer.
4489 (if expertp (funcall handle-keypress expert-prompt)
4490 (save-window-excursion
4491 (delete-other-windows)
4492 (with-output-to-temp-buffer "*Org Export/Publishing Help*" (princ help))
4493 (org-fit-window-to-buffer
4494 (get-buffer-window "*Org Export/Publishing Help*"))
4495 (funcall handle-keypress standard-prompt)))))
4498 (provide 'org-export)
4499 ;;; org-export.el ends here