fix test: BEGIN_ORG / END_ORG has been replaced by BEGIN_SRC org / END_SRC
[org-mode.git] / contrib / lisp / org-export.el
blob4f01b7ee48d0302833bab415c0e10bcfa0d44123
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. Filters defined here
749 shouldn't make a back-end test, as it may prevent back-ends
750 derived from this one to behave properly.
752 :options-alist
754 Alist between back-end specific properties introduced in
755 communication channel and how their value are acquired. See
756 `org-export-options-alist' for more information about
757 structure of the values.
759 As an example, here is how the `e-ascii' back-end is defined:
761 \(org-export-define-backend e-ascii
762 \((bold . org-e-ascii-bold)
763 \(center-block . org-e-ascii-center-block)
764 \(clock . org-e-ascii-clock)
765 \(code . org-e-ascii-code)
766 \(drawer . org-e-ascii-drawer)
767 \(dynamic-block . org-e-ascii-dynamic-block)
768 \(entity . org-e-ascii-entity)
769 \(example-block . org-e-ascii-example-block)
770 \(export-block . org-e-ascii-export-block)
771 \(export-snippet . org-e-ascii-export-snippet)
772 \(fixed-width . org-e-ascii-fixed-width)
773 \(footnote-definition . org-e-ascii-footnote-definition)
774 \(footnote-reference . org-e-ascii-footnote-reference)
775 \(headline . org-e-ascii-headline)
776 \(horizontal-rule . org-e-ascii-horizontal-rule)
777 \(inline-src-block . org-e-ascii-inline-src-block)
778 \(inlinetask . org-e-ascii-inlinetask)
779 \(italic . org-e-ascii-italic)
780 \(item . org-e-ascii-item)
781 \(keyword . org-e-ascii-keyword)
782 \(latex-environment . org-e-ascii-latex-environment)
783 \(latex-fragment . org-e-ascii-latex-fragment)
784 \(line-break . org-e-ascii-line-break)
785 \(link . org-e-ascii-link)
786 \(macro . org-e-ascii-macro)
787 \(paragraph . org-e-ascii-paragraph)
788 \(plain-list . org-e-ascii-plain-list)
789 \(plain-text . org-e-ascii-plain-text)
790 \(planning . org-e-ascii-planning)
791 \(property-drawer . org-e-ascii-property-drawer)
792 \(quote-block . org-e-ascii-quote-block)
793 \(quote-section . org-e-ascii-quote-section)
794 \(radio-target . org-e-ascii-radio-target)
795 \(section . org-e-ascii-section)
796 \(special-block . org-e-ascii-special-block)
797 \(src-block . org-e-ascii-src-block)
798 \(statistics-cookie . org-e-ascii-statistics-cookie)
799 \(strike-through . org-e-ascii-strike-through)
800 \(subscript . org-e-ascii-subscript)
801 \(superscript . org-e-ascii-superscript)
802 \(table . org-e-ascii-table)
803 \(table-cell . org-e-ascii-table-cell)
804 \(table-row . org-e-ascii-table-row)
805 \(target . org-e-ascii-target)
806 \(template . org-e-ascii-template)
807 \(timestamp . org-e-ascii-timestamp)
808 \(underline . org-e-ascii-underline)
809 \(verbatim . org-e-ascii-verbatim)
810 \(verse-block . org-e-ascii-verse-block))
811 :export-block \"ASCII\"
812 :filters-alist ((:filter-headline . org-e-ascii-filter-headline-blank-lines)
813 \(:filter-section . org-e-ascii-filter-headline-blank-lines))
814 :options-alist ((:ascii-charset nil nil org-e-ascii-charset)))"
815 (declare (debug (&define name sexp [&rest [keywordp sexp]] defbody))
816 (indent 1))
817 (let (filters options export-block)
818 (while (keywordp (car body))
819 (case (pop body)
820 (:export-block (let ((names (pop body)))
821 (setq export-block
822 (if (consp names) (mapcar 'upcase names)
823 (list (upcase names))))))
824 (:filters-alist (setq filters (pop body)))
825 (:options-alist (setq options (pop body)))
826 (t (pop body))))
827 `(progn
828 ;; Define translators.
829 (defvar ,(intern (format "org-%s-translate-alist" backend)) ',translators
830 "Alist between element or object types and translators.")
831 ;; Define options.
832 ,(when options
833 `(defconst ,(intern (format "org-%s-options-alist" backend)) ',options
834 ,(format "Alist between %s export properties and ways to set them.
835 See `org-export-options-alist' for more information on the
836 structure of the values."
837 backend)))
838 ;; Define filters.
839 ,(when filters
840 `(defconst ,(intern (format "org-%s-filters-alist" backend)) ',filters
841 "Alist between filters keywords and back-end specific filters.
842 See `org-export-filters-alist' for more information."))
843 ;; Tell parser to not parse EXPORT-BLOCK blocks.
844 ,(when export-block
845 `(mapc
846 (lambda (name)
847 (add-to-list 'org-element-block-name-alist
848 `(,name . org-element-export-block-parser)))
849 ',export-block))
850 ;; Splice in the body, if any.
851 ,@body)))
853 (defmacro org-export-define-derived-backend (child parent &rest body)
854 "Create a new back-end as a variant of an existing one.
856 CHILD is the name of the derived back-end. PARENT is the name of
857 the parent back-end.
859 BODY can start with pre-defined keyword arguments. The following
860 keywords are understood:
862 :export-block
864 String, or list of strings, representing block names that
865 will not be parsed. This is used to specify blocks that will
866 contain raw code specific to the back-end. These blocks
867 still have to be handled by the relative `export-block' type
868 translator.
870 :filters-alist
872 Alist of filters that will overwrite or complete filters
873 defined in PARENT back-end. See `org-export-filters-alist'
874 for more a list of allowed filters.
876 :options-alist
878 Alist of back-end specific properties that will overwrite or
879 complete those defined in PARENT back-end. Refer to
880 `org-export-options-alist' for more information about
881 structure of the values.
883 :translate-alist
885 Alist of element and object types and transcoders that will
886 overwrite or complete transcode table from PARENT back-end.
887 Refer to `org-export-define-backend' for detailed information
888 about transcoders.
890 As an example, here is how one could define \"my-latex\" back-end
891 as a variant of `e-latex' back-end with a custom template
892 function:
894 \(org-export-define-derived-backend my-latex e-latex
895 :translate-alist ((template . my-latex-template-fun)))
897 The back-end could then be called with, for example:
899 \(org-export-to-buffer 'my-latex \"*Test my-latex*\")"
900 (declare (debug (&define name sexp [&rest [keywordp sexp]] def-body))
901 (indent 2))
902 (let (filters options translate export-block)
903 (while (keywordp (car body))
904 (case (pop body)
905 (:export-block (let ((names (pop body)))
906 (setq export-block
907 (if (consp names) (mapcar 'upcase names)
908 (list (upcase names))))))
909 (:filters-alist (setq filters (pop body)))
910 (:options-alist (setq options (pop body)))
911 (:translate-alist (setq translate (pop body)))
912 (t (pop body))))
913 `(progn
914 ;; Tell parser to not parse EXPORT-BLOCK blocks.
915 ,(when export-block
916 `(mapc
917 (lambda (name)
918 (add-to-list 'org-element-block-name-alist
919 `(,name . org-element-export-block-parser)))
920 ',export-block))
921 ;; Define filters.
922 ,(let ((parent-filters (intern (format "org-%s-filters-alist" parent))))
923 (when (or (boundp parent-filters) filters)
924 `(defconst ,(intern (format "org-%s-filters-alist" child))
925 ',(append filters
926 (and (boundp parent-filters)
927 (copy-sequence (symbol-value parent-filters))))
928 "Alist between filters keywords and back-end specific filters.
929 See `org-export-filters-alist' for more information.")))
930 ;; Define options.
931 ,(let ((parent-options (intern (format "org-%s-options-alist" parent))))
932 (when (or (boundp parent-options) options)
933 `(defconst ,(intern (format "org-%s-options-alist" child))
934 ',(append options
935 (and (boundp parent-options)
936 (copy-sequence (symbol-value parent-options))))
937 ,(format "Alist between %s export properties and ways to set them.
938 See `org-export-options-alist' for more information on the
939 structure of the values."
940 child))))
941 ;; Define translators.
942 (defvar ,(intern (format "org-%s-translate-alist" child))
943 ',(append translate
944 (copy-sequence
945 (symbol-value
946 (intern (format "org-%s-translate-alist" parent)))))
947 "Alist between element or object types and translators.")
948 ;; Splice in the body, if any.
949 ,@body)))
953 ;;; The Communication Channel
955 ;; During export process, every function has access to a number of
956 ;; properties. They are of two types:
958 ;; 1. Environment options are collected once at the very beginning of
959 ;; the process, out of the original buffer and configuration.
960 ;; Collecting them is handled by `org-export-get-environment'
961 ;; function.
963 ;; Most environment options are defined through the
964 ;; `org-export-options-alist' variable.
966 ;; 2. Tree properties are extracted directly from the parsed tree,
967 ;; just before export, by `org-export-collect-tree-properties'.
969 ;; Here is the full list of properties available during transcode
970 ;; process, with their category (option, tree or local) and their
971 ;; value type.
973 ;; + `:author' :: Author's name.
974 ;; - category :: option
975 ;; - type :: string
977 ;; + `:back-end' :: Current back-end used for transcoding.
978 ;; - category :: tree
979 ;; - type :: symbol
981 ;; + `:creator' :: String to write as creation information.
982 ;; - category :: option
983 ;; - type :: string
985 ;; + `:date' :: String to use as date.
986 ;; - category :: option
987 ;; - type :: string
989 ;; + `:description' :: Description text for the current data.
990 ;; - category :: option
991 ;; - type :: string
993 ;; + `:email' :: Author's email.
994 ;; - category :: option
995 ;; - type :: string
997 ;; + `:exclude-tags' :: Tags for exclusion of subtrees from export
998 ;; process.
999 ;; - category :: option
1000 ;; - type :: list of strings
1002 ;; + `:exported-data' :: Hash table used for memoizing
1003 ;; `org-export-data'.
1004 ;; - category :: tree
1005 ;; - type :: hash table
1007 ;; + `:footnote-definition-alist' :: Alist between footnote labels and
1008 ;; their definition, as parsed data. Only non-inlined footnotes
1009 ;; are represented in this alist. Also, every definition isn't
1010 ;; guaranteed to be referenced in the parse tree. The purpose of
1011 ;; this property is to preserve definitions from oblivion
1012 ;; (i.e. when the parse tree comes from a part of the original
1013 ;; buffer), it isn't meant for direct use in a back-end. To
1014 ;; retrieve a definition relative to a reference, use
1015 ;; `org-export-get-footnote-definition' instead.
1016 ;; - category :: option
1017 ;; - type :: alist (STRING . LIST)
1019 ;; + `:headline-levels' :: Maximum level being exported as an
1020 ;; headline. Comparison is done with the relative level of
1021 ;; headlines in the parse tree, not necessarily with their
1022 ;; actual level.
1023 ;; - category :: option
1024 ;; - type :: integer
1026 ;; + `:headline-offset' :: Difference between relative and real level
1027 ;; of headlines in the parse tree. For example, a value of -1
1028 ;; means a level 2 headline should be considered as level
1029 ;; 1 (cf. `org-export-get-relative-level').
1030 ;; - category :: tree
1031 ;; - type :: integer
1033 ;; + `:headline-numbering' :: Alist between headlines and their
1034 ;; numbering, as a list of numbers
1035 ;; (cf. `org-export-get-headline-number').
1036 ;; - category :: tree
1037 ;; - type :: alist (INTEGER . LIST)
1039 ;; + `:id-alist' :: Alist between ID strings and destination file's
1040 ;; path, relative to current directory. It is used by
1041 ;; `org-export-resolve-id-link' to resolve ID links targeting an
1042 ;; external file.
1043 ;; - category :: option
1044 ;; - type :: alist (STRING . STRING)
1046 ;; + `:ignore-list' :: List of elements and objects that should be
1047 ;; ignored during export.
1048 ;; - category :: tree
1049 ;; - type :: list of elements and objects
1051 ;; + `:input-file' :: Full path to input file, if any.
1052 ;; - category :: option
1053 ;; - type :: string or nil
1055 ;; + `:keywords' :: List of keywords attached to data.
1056 ;; - category :: option
1057 ;; - type :: string
1059 ;; + `:language' :: Default language used for translations.
1060 ;; - category :: option
1061 ;; - type :: string
1063 ;; + `:parse-tree' :: Whole parse tree, available at any time during
1064 ;; transcoding.
1065 ;; - category :: option
1066 ;; - type :: list (as returned by `org-element-parse-buffer')
1068 ;; + `:preserve-breaks' :: Non-nil means transcoding should preserve
1069 ;; all line breaks.
1070 ;; - category :: option
1071 ;; - type :: symbol (nil, t)
1073 ;; + `:section-numbers' :: Non-nil means transcoding should add
1074 ;; section numbers to headlines.
1075 ;; - category :: option
1076 ;; - type :: symbol (nil, t)
1078 ;; + `:select-tags' :: List of tags enforcing inclusion of sub-trees
1079 ;; in transcoding. When such a tag is present, subtrees without
1080 ;; it are de facto excluded from the process. See
1081 ;; `use-select-tags'.
1082 ;; - category :: option
1083 ;; - type :: list of strings
1085 ;; + `:target-list' :: List of targets encountered in the parse tree.
1086 ;; This is used to partly resolve "fuzzy" links
1087 ;; (cf. `org-export-resolve-fuzzy-link').
1088 ;; - category :: tree
1089 ;; - type :: list of strings
1091 ;; + `:time-stamp-file' :: Non-nil means transcoding should insert
1092 ;; a time stamp in the output.
1093 ;; - category :: option
1094 ;; - type :: symbol (nil, t)
1096 ;; + `:translate-alist' :: Alist between element and object types and
1097 ;; transcoding functions relative to the current back-end.
1098 ;; Special keys `template' and `plain-text' are also possible.
1099 ;; - category :: option
1100 ;; - type :: alist (SYMBOL . FUNCTION)
1102 ;; + `:with-archived-trees' :: Non-nil when archived subtrees should
1103 ;; also be transcoded. If it is set to the `headline' symbol,
1104 ;; only the archived headline's name is retained.
1105 ;; - category :: option
1106 ;; - type :: symbol (nil, t, `headline')
1108 ;; + `:with-author' :: Non-nil means author's name should be included
1109 ;; in the output.
1110 ;; - category :: option
1111 ;; - type :: symbol (nil, t)
1113 ;; + `:with-clocks' :: Non-nild means clock keywords should be exported.
1114 ;; - category :: option
1115 ;; - type :: symbol (nil, t)
1117 ;; + `:with-creator' :: Non-nild means a creation sentence should be
1118 ;; inserted at the end of the transcoded string. If the value
1119 ;; is `comment', it should be commented.
1120 ;; - category :: option
1121 ;; - type :: symbol (`comment', nil, t)
1123 ;; + `:with-drawers' :: Non-nil means drawers should be exported. If
1124 ;; its value is a list of names, only drawers with such names
1125 ;; will be transcoded.
1126 ;; - category :: option
1127 ;; - type :: symbol (nil, t) or list of strings
1129 ;; + `:with-email' :: Non-nil means output should contain author's
1130 ;; email.
1131 ;; - category :: option
1132 ;; - type :: symbol (nil, t)
1134 ;; + `:with-emphasize' :: Non-nil means emphasized text should be
1135 ;; interpreted.
1136 ;; - category :: option
1137 ;; - type :: symbol (nil, t)
1139 ;; + `:with-fixed-width' :: Non-nil if transcoder should interpret
1140 ;; strings starting with a colon as a fixed-with (verbatim) area.
1141 ;; - category :: option
1142 ;; - type :: symbol (nil, t)
1144 ;; + `:with-footnotes' :: Non-nil if transcoder should interpret
1145 ;; footnotes.
1146 ;; - category :: option
1147 ;; - type :: symbol (nil, t)
1149 ;; + `:with-plannings' :: Non-nil means transcoding should include
1150 ;; planning info.
1151 ;; - category :: option
1152 ;; - type :: symbol (nil, t)
1154 ;; + `:with-priority' :: Non-nil means transcoding should include
1155 ;; priority cookies.
1156 ;; - category :: option
1157 ;; - type :: symbol (nil, t)
1159 ;; + `:with-special-strings' :: Non-nil means transcoding should
1160 ;; interpret special strings in plain text.
1161 ;; - category :: option
1162 ;; - type :: symbol (nil, t)
1164 ;; + `:with-sub-superscript' :: Non-nil means transcoding should
1165 ;; interpret subscript and superscript. With a value of "{}",
1166 ;; only interpret those using curly brackets.
1167 ;; - category :: option
1168 ;; - type :: symbol (nil, {}, t)
1170 ;; + `:with-tables' :: Non-nil means transcoding should interpret
1171 ;; tables.
1172 ;; - category :: option
1173 ;; - type :: symbol (nil, t)
1175 ;; + `:with-tags' :: Non-nil means transcoding should keep tags in
1176 ;; headlines. A `not-in-toc' value will remove them from the
1177 ;; table of contents, if any, nonetheless.
1178 ;; - category :: option
1179 ;; - type :: symbol (nil, t, `not-in-toc')
1181 ;; + `:with-tasks' :: Non-nil means transcoding should include
1182 ;; headlines with a TODO keyword. A `todo' value will only
1183 ;; include headlines with a todo type keyword while a `done'
1184 ;; value will do the contrary. If a list of strings is provided,
1185 ;; only tasks with keywords belonging to that list will be kept.
1186 ;; - category :: option
1187 ;; - type :: symbol (t, todo, done, nil) or list of strings
1189 ;; + `:with-timestamps' :: Non-nil means transcoding should include
1190 ;; time stamps. Special value `active' (resp. `inactive') ask to
1191 ;; export only active (resp. inactive) timestamps. Otherwise,
1192 ;; completely remove them.
1193 ;; - category :: option
1194 ;; - type :: symbol: (`active', `inactive', t, nil)
1196 ;; + `:with-toc' :: Non-nil means that a table of contents has to be
1197 ;; added to the output. An integer value limits its depth.
1198 ;; - category :: option
1199 ;; - type :: symbol (nil, t or integer)
1201 ;; + `:with-todo-keywords' :: Non-nil means transcoding should
1202 ;; include TODO keywords.
1203 ;; - category :: option
1204 ;; - type :: symbol (nil, t)
1207 ;;;; Environment Options
1209 ;; Environment options encompass all parameters defined outside the
1210 ;; scope of the parsed data. They come from five sources, in
1211 ;; increasing precedence order:
1213 ;; - Global variables,
1214 ;; - Buffer's attributes,
1215 ;; - Options keyword symbols,
1216 ;; - Buffer keywords,
1217 ;; - Subtree properties.
1219 ;; The central internal function with regards to environment options
1220 ;; is `org-export-get-environment'. It updates global variables with
1221 ;; "#+BIND:" keywords, then retrieve and prioritize properties from
1222 ;; the different sources.
1224 ;; The internal functions doing the retrieval are:
1225 ;; `org-export--get-global-options',
1226 ;; `org-export--get-buffer-attributes',
1227 ;; `org-export--parse-option-keyword',
1228 ;; `org-export--get-subtree-options' and
1229 ;; `org-export--get-inbuffer-options'
1231 ;; Also, `org-export--confirm-letbind' and `org-export--install-letbind'
1232 ;; take care of the part relative to "#+BIND:" keywords.
1234 (defun org-export-get-environment (&optional backend subtreep ext-plist)
1235 "Collect export options from the current buffer.
1237 Optional argument BACKEND is a symbol specifying which back-end
1238 specific options to read, if any.
1240 When optional argument SUBTREEP is non-nil, assume the export is
1241 done against the current sub-tree.
1243 Third optional argument EXT-PLIST is a property list with
1244 external parameters overriding Org default settings, but still
1245 inferior to file-local settings."
1246 ;; First install #+BIND variables.
1247 (org-export--install-letbind-maybe)
1248 ;; Get and prioritize export options...
1249 (org-combine-plists
1250 ;; ... from global variables...
1251 (org-export--get-global-options backend)
1252 ;; ... from buffer's attributes...
1253 (org-export--get-buffer-attributes)
1254 ;; ... from an external property list...
1255 ext-plist
1256 ;; ... from in-buffer settings...
1257 (org-export--get-inbuffer-options
1258 backend
1259 (and buffer-file-name (org-remove-double-quotes buffer-file-name)))
1260 ;; ... and from subtree, when appropriate.
1261 (and subtreep (org-export--get-subtree-options backend))
1262 ;; Eventually install back-end symbol and its translation table.
1263 `(:back-end
1264 ,backend
1265 :translate-alist
1266 ,(let ((trans-alist (intern (format "org-%s-translate-alist" backend))))
1267 (when (boundp trans-alist) (symbol-value trans-alist))))))
1269 (defun org-export--parse-option-keyword (options &optional backend)
1270 "Parse an OPTIONS line and return values as a plist.
1271 Optional argument BACKEND is a symbol specifying which back-end
1272 specific items to read, if any."
1273 (let* ((all
1274 (append org-export-options-alist
1275 (and backend
1276 (let ((var (intern
1277 (format "org-%s-options-alist" backend))))
1278 (and (boundp var) (eval var))))))
1279 ;; Build an alist between #+OPTION: item and property-name.
1280 (alist (delq nil
1281 (mapcar (lambda (e)
1282 (when (nth 2 e) (cons (regexp-quote (nth 2 e))
1283 (car e))))
1284 all)))
1285 plist)
1286 (mapc (lambda (e)
1287 (when (string-match (concat "\\(\\`\\|[ \t]\\)"
1288 (car e)
1289 ":\\(([^)\n]+)\\|[^ \t\n\r;,.]*\\)")
1290 options)
1291 (setq plist (plist-put plist
1292 (cdr e)
1293 (car (read-from-string
1294 (match-string 2 options)))))))
1295 alist)
1296 plist))
1298 (defun org-export--get-subtree-options (&optional backend)
1299 "Get export options in subtree at point.
1300 Optional argument BACKEND is a symbol specifying back-end used
1301 for export. Return options as a plist."
1302 ;; For each buffer keyword, create an headline property setting the
1303 ;; same property in communication channel. The name for the property
1304 ;; is the keyword with "EXPORT_" appended to it.
1305 (org-with-wide-buffer
1306 (let (prop plist)
1307 ;; Make sure point is at an heading.
1308 (unless (org-at-heading-p) (org-back-to-heading t))
1309 ;; Take care of EXPORT_TITLE. If it isn't defined, use headline's
1310 ;; title as its fallback value.
1311 (when (setq prop (progn (looking-at org-todo-line-regexp)
1312 (or (save-match-data
1313 (org-entry-get (point) "EXPORT_TITLE"))
1314 (org-match-string-no-properties 3))))
1315 (setq plist
1316 (plist-put
1317 plist :title
1318 (org-element-parse-secondary-string
1319 prop (org-element-restriction 'keyword)))))
1320 ;; EXPORT_OPTIONS are parsed in a non-standard way.
1321 (when (setq prop (org-entry-get (point) "EXPORT_OPTIONS"))
1322 (setq plist
1323 (nconc plist (org-export--parse-option-keyword prop backend))))
1324 ;; Handle other keywords.
1325 (let ((seen '("TITLE")))
1326 (mapc
1327 (lambda (option)
1328 (let ((property (nth 1 option)))
1329 (when (and property (not (member property seen)))
1330 (let* ((subtree-prop (concat "EXPORT_" property))
1331 ;; Export properties are not case-sensitive.
1332 (value (let ((case-fold-search t))
1333 (org-entry-get (point) subtree-prop))))
1334 (push property seen)
1335 (when value
1336 (setq plist
1337 (plist-put
1338 plist
1339 (car option)
1340 ;; Parse VALUE if required.
1341 (if (member property org-element-parsed-keywords)
1342 (org-element-parse-secondary-string
1343 value (org-element-restriction 'keyword))
1344 value))))))))
1345 ;; Also look for both general keywords and back-end specific
1346 ;; options if BACKEND is provided.
1347 (append (and backend
1348 (let ((var (intern
1349 (format "org-%s-options-alist" backend))))
1350 (and (boundp var) (symbol-value var))))
1351 org-export-options-alist)))
1352 ;; Return value.
1353 plist)))
1355 (defun org-export--get-inbuffer-options (&optional backend files)
1356 "Return current buffer export options, as a plist.
1358 Optional argument BACKEND, when non-nil, is a symbol specifying
1359 which back-end specific options should also be read in the
1360 process.
1362 Optional argument FILES is a list of setup files names read so
1363 far, used to avoid circular dependencies.
1365 Assume buffer is in Org mode. Narrowing, if any, is ignored."
1366 (org-with-wide-buffer
1367 (goto-char (point-min))
1368 (let ((case-fold-search t) plist)
1369 ;; 1. Special keywords, as in `org-export-special-keywords'.
1370 (let ((special-re (org-make-options-regexp org-export-special-keywords)))
1371 (while (re-search-forward special-re nil t)
1372 (let ((element (org-element-at-point)))
1373 (when (eq (org-element-type element) 'keyword)
1374 (let* ((key (org-element-property :key element))
1375 (val (org-element-property :value element))
1376 (prop
1377 (cond
1378 ((string= key "SETUP_FILE")
1379 (let ((file
1380 (expand-file-name
1381 (org-remove-double-quotes (org-trim val)))))
1382 ;; Avoid circular dependencies.
1383 (unless (member file files)
1384 (with-temp-buffer
1385 (insert (org-file-contents file 'noerror))
1386 (org-mode)
1387 (org-export--get-inbuffer-options
1388 backend (cons file files))))))
1389 ((string= key "OPTIONS")
1390 (org-export--parse-option-keyword val backend))
1391 ((string= key "MACRO")
1392 (when (string-match
1393 "^\\([-a-zA-Z0-9_]+\\)\\(?:[ \t]+\\(.*?\\)[ \t]*$\\)?"
1394 val)
1395 (let ((key
1396 (intern
1397 (concat ":macro-"
1398 (downcase (match-string 1 val)))))
1399 (value (org-match-string-no-properties 2 val)))
1400 (cond
1401 ((not value) nil)
1402 ;; Value will be evaled: do not parse it.
1403 ((string-match "\\`(eval\\>" value)
1404 (list key (list value)))
1405 ;; Value has to be parsed for nested
1406 ;; macros.
1408 (list
1410 (let ((restr (org-element-restriction 'macro)))
1411 (org-element-parse-secondary-string
1412 ;; If user explicitly asks for
1413 ;; a newline, be sure to preserve it
1414 ;; from further filling with
1415 ;; `hard-newline'. Also replace
1416 ;; "\\n" with "\n", "\\\n" with "\\n"
1417 ;; and so on...
1418 (replace-regexp-in-string
1419 "\\(\\\\\\\\\\)n" "\\\\"
1420 (replace-regexp-in-string
1421 "\\(?:^\\|[^\\\\]\\)\\(\\\\n\\)"
1422 hard-newline value nil nil 1)
1423 nil nil 1)
1424 restr)))))))))))
1425 (setq plist (org-combine-plists plist prop)))))))
1426 ;; 2. Standard options, as in `org-export-options-alist'.
1427 (let* ((all (append org-export-options-alist
1428 ;; Also look for back-end specific options
1429 ;; if BACKEND is defined.
1430 (and backend
1431 (let ((var
1432 (intern
1433 (format "org-%s-options-alist" backend))))
1434 (and (boundp var) (eval var))))))
1435 ;; Build alist between keyword name and property name.
1436 (alist
1437 (delq nil (mapcar
1438 (lambda (e) (when (nth 1 e) (cons (nth 1 e) (car e))))
1439 all)))
1440 ;; Build regexp matching all keywords associated to export
1441 ;; options. Note: the search is case insensitive.
1442 (opt-re (org-make-options-regexp
1443 (delq nil (mapcar (lambda (e) (nth 1 e)) all)))))
1444 (goto-char (point-min))
1445 (while (re-search-forward opt-re nil t)
1446 (let ((element (org-element-at-point)))
1447 (when (eq (org-element-type element) 'keyword)
1448 (let* ((key (org-element-property :key element))
1449 (val (org-element-property :value element))
1450 (prop (cdr (assoc key alist)))
1451 (behaviour (nth 4 (assq prop all))))
1452 (setq plist
1453 (plist-put
1454 plist prop
1455 ;; Handle value depending on specified BEHAVIOUR.
1456 (case behaviour
1457 (space
1458 (if (not (plist-get plist prop)) (org-trim val)
1459 (concat (plist-get plist prop) " " (org-trim val))))
1460 (newline
1461 (org-trim
1462 (concat (plist-get plist prop) "\n" (org-trim val))))
1463 (split
1464 `(,@(plist-get plist prop) ,@(org-split-string val)))
1465 ('t val)
1466 (otherwise (if (not (plist-member plist prop)) val
1467 (plist-get plist prop))))))))))
1468 ;; Parse keywords specified in `org-element-parsed-keywords'.
1469 (mapc
1470 (lambda (key)
1471 (let* ((prop (cdr (assoc key alist)))
1472 (value (and prop (plist-get plist prop))))
1473 (when (stringp value)
1474 (setq plist
1475 (plist-put
1476 plist prop
1477 (org-element-parse-secondary-string
1478 value (org-element-restriction 'keyword)))))))
1479 org-element-parsed-keywords))
1480 ;; 3. Return final value.
1481 plist)))
1483 (defun org-export--get-buffer-attributes ()
1484 "Return properties related to buffer attributes, as a plist."
1485 (let ((visited-file (buffer-file-name (buffer-base-buffer))))
1486 (list
1487 ;; Store full path of input file name, or nil. For internal use.
1488 :input-file visited-file
1489 :title (or (and visited-file
1490 (file-name-sans-extension
1491 (file-name-nondirectory visited-file)))
1492 (buffer-name (buffer-base-buffer)))
1493 :footnote-definition-alist
1494 ;; Footnotes definitions must be collected in the original
1495 ;; buffer, as there's no insurance that they will still be in the
1496 ;; parse tree, due to possible narrowing.
1497 (let (alist)
1498 (org-with-wide-buffer
1499 (goto-char (point-min))
1500 (while (re-search-forward org-footnote-definition-re nil t)
1501 (let ((def (org-footnote-at-definition-p)))
1502 (when def
1503 (org-skip-whitespace)
1504 (push (cons (car def)
1505 (save-restriction
1506 (narrow-to-region (point) (nth 2 def))
1507 ;; Like `org-element-parse-buffer', but
1508 ;; makes sure the definition doesn't start
1509 ;; with a section element.
1510 (org-element--parse-elements
1511 (point-min) (point-max) nil nil nil nil
1512 (list 'org-data nil))))
1513 alist))))
1514 alist))
1515 :id-alist
1516 ;; Collect id references.
1517 (let (alist)
1518 (org-with-wide-buffer
1519 (goto-char (point-min))
1520 (while (re-search-forward
1521 "\\[\\[id:\\(\\S-+?\\)\\]\\(?:\\[.*?\\]\\)?\\]" nil t)
1522 (let* ((id (org-match-string-no-properties 1))
1523 (file (org-id-find-id-file id)))
1524 (when file (push (cons id (file-relative-name file)) alist)))))
1525 alist)
1526 :macro-modification-time
1527 (and visited-file
1528 (file-exists-p visited-file)
1529 (concat "(eval (format-time-string \"$1\" '"
1530 (prin1-to-string (nth 5 (file-attributes visited-file)))
1531 "))"))
1532 ;; Store input file name as a macro.
1533 :macro-input-file (and visited-file (file-name-nondirectory visited-file))
1534 ;; `:macro-date', `:macro-time' and `:macro-property' could as
1535 ;; well be initialized as tree properties, since they don't
1536 ;; depend on buffer properties. Though, it may be more logical
1537 ;; to keep them close to other ":macro-" properties.
1538 :macro-date "(eval (format-time-string \"$1\"))"
1539 :macro-time "(eval (format-time-string \"$1\"))"
1540 :macro-property "(eval (org-entry-get nil \"$1\" 'selective))")))
1542 (defun org-export--get-global-options (&optional backend)
1543 "Return global export options as a plist.
1545 Optional argument BACKEND, if non-nil, is a symbol specifying
1546 which back-end specific export options should also be read in the
1547 process."
1548 (let ((all (append org-export-options-alist
1549 (and backend
1550 (let ((var (intern
1551 (format "org-%s-options-alist" backend))))
1552 (and (boundp var) (symbol-value var))))))
1553 ;; Output value.
1554 plist)
1555 (mapc
1556 (lambda (cell)
1557 (setq plist
1558 (plist-put
1559 plist
1560 (car cell)
1561 ;; Eval default value provided. If keyword is a member
1562 ;; of `org-element-parsed-keywords', parse it as
1563 ;; a secondary string before storing it.
1564 (let ((value (eval (nth 3 cell))))
1565 (if (not (stringp value)) value
1566 (let ((keyword (nth 1 cell)))
1567 (if (not (member keyword org-element-parsed-keywords)) value
1568 (org-element-parse-secondary-string
1569 value (org-element-restriction 'keyword)))))))))
1570 all)
1571 ;; Return value.
1572 plist))
1574 (defvar org-export--allow-BIND-local nil)
1575 (defun org-export--confirm-letbind ()
1576 "Can we use #+BIND values during export?
1577 By default this will ask for confirmation by the user, to divert
1578 possible security risks."
1579 (cond
1580 ((not org-export-allow-BIND) nil)
1581 ((eq org-export-allow-BIND t) t)
1582 ((local-variable-p 'org-export--allow-BIND-local)
1583 org-export--allow-BIND-local)
1584 (t (org-set-local 'org-export--allow-BIND-local
1585 (yes-or-no-p "Allow BIND values in this buffer? ")))))
1587 (defun org-export--install-letbind-maybe ()
1588 "Install the values from #+BIND lines as local variables.
1589 Variables must be installed before in-buffer options are
1590 retrieved."
1591 (let (letbind pair)
1592 (org-with-wide-buffer
1593 (goto-char (point-min))
1594 (while (re-search-forward (org-make-options-regexp '("BIND")) nil t)
1595 (when (org-export-confirm-letbind)
1596 (push (read (concat "(" (org-match-string-no-properties 2) ")"))
1597 letbind))))
1598 (while (setq pair (pop letbind))
1599 (org-set-local (car pair) (nth 1 pair)))))
1602 ;;;; Tree Properties
1604 ;; Tree properties are infromation extracted from parse tree. They
1605 ;; are initialized at the beginning of the transcoding process by
1606 ;; `org-export-collect-tree-properties'.
1608 ;; Dedicated functions focus on computing the value of specific tree
1609 ;; properties during initialization. Thus,
1610 ;; `org-export--populate-ignore-list' lists elements and objects that
1611 ;; should be skipped during export, `org-export--get-min-level' gets
1612 ;; the minimal exportable level, used as a basis to compute relative
1613 ;; level for headlines. Eventually
1614 ;; `org-export--collect-headline-numbering' builds an alist between
1615 ;; headlines and their numbering.
1617 (defun org-export-collect-tree-properties (data info)
1618 "Extract tree properties from parse tree.
1620 DATA is the parse tree from which information is retrieved. INFO
1621 is a list holding export options.
1623 Following tree properties are set or updated:
1625 `:exported-data' Hash table used to memoize results from
1626 `org-export-data'.
1628 `:footnote-definition-alist' List of footnotes definitions in
1629 original buffer and current parse tree.
1631 `:headline-offset' Offset between true level of headlines and
1632 local level. An offset of -1 means an headline
1633 of level 2 should be considered as a level
1634 1 headline in the context.
1636 `:headline-numbering' Alist of all headlines as key an the
1637 associated numbering as value.
1639 `:ignore-list' List of elements that should be ignored during
1640 export.
1642 `:target-list' List of all targets in the parse tree.
1644 Return updated plist."
1645 ;; Install the parse tree in the communication channel, in order to
1646 ;; use `org-export-get-genealogy' and al.
1647 (setq info (plist-put info :parse-tree data))
1648 ;; Get the list of elements and objects to ignore, and put it into
1649 ;; `:ignore-list'. Do not overwrite any user ignore that might have
1650 ;; been done during parse tree filtering.
1651 (setq info
1652 (plist-put info
1653 :ignore-list
1654 (append (org-export--populate-ignore-list data info)
1655 (plist-get info :ignore-list))))
1656 ;; Compute `:headline-offset' in order to be able to use
1657 ;; `org-export-get-relative-level'.
1658 (setq info
1659 (plist-put info
1660 :headline-offset
1661 (- 1 (org-export--get-min-level data info))))
1662 ;; Update footnotes definitions list with definitions in parse tree.
1663 ;; This is required since buffer expansion might have modified
1664 ;; boundaries of footnote definitions contained in the parse tree.
1665 ;; This way, definitions in `footnote-definition-alist' are bound to
1666 ;; match those in the parse tree.
1667 (let ((defs (plist-get info :footnote-definition-alist)))
1668 (org-element-map
1669 data 'footnote-definition
1670 (lambda (fn)
1671 (push (cons (org-element-property :label fn)
1672 `(org-data nil ,@(org-element-contents fn)))
1673 defs)))
1674 (setq info (plist-put info :footnote-definition-alist defs)))
1675 ;; Properties order doesn't matter: get the rest of the tree
1676 ;; properties.
1677 (nconc
1678 `(:target-list
1679 ,(org-element-map
1680 data '(keyword target)
1681 (lambda (blob)
1682 (when (or (eq (org-element-type blob) 'target)
1683 (string= (org-element-property :key blob) "TARGET"))
1684 blob)) info)
1685 :headline-numbering ,(org-export--collect-headline-numbering data info)
1686 :exported-data ,(make-hash-table :test 'eq :size 4001))
1687 info))
1689 (defun org-export--get-min-level (data options)
1690 "Return minimum exportable headline's level in DATA.
1691 DATA is parsed tree as returned by `org-element-parse-buffer'.
1692 OPTIONS is a plist holding export options."
1693 (catch 'exit
1694 (let ((min-level 10000))
1695 (mapc
1696 (lambda (blob)
1697 (when (and (eq (org-element-type blob) 'headline)
1698 (not (memq blob (plist-get options :ignore-list))))
1699 (setq min-level
1700 (min (org-element-property :level blob) min-level)))
1701 (when (= min-level 1) (throw 'exit 1)))
1702 (org-element-contents data))
1703 ;; If no headline was found, for the sake of consistency, set
1704 ;; minimum level to 1 nonetheless.
1705 (if (= min-level 10000) 1 min-level))))
1707 (defun org-export--collect-headline-numbering (data options)
1708 "Return numbering of all exportable headlines in a parse tree.
1710 DATA is the parse tree. OPTIONS is the plist holding export
1711 options.
1713 Return an alist whose key is an headline and value is its
1714 associated numbering \(in the shape of a list of numbers\)."
1715 (let ((numbering (make-vector org-export-max-depth 0)))
1716 (org-element-map
1717 data
1718 'headline
1719 (lambda (headline)
1720 (let ((relative-level
1721 (1- (org-export-get-relative-level headline options))))
1722 (cons
1723 headline
1724 (loop for n across numbering
1725 for idx from 0 to org-export-max-depth
1726 when (< idx relative-level) collect n
1727 when (= idx relative-level) collect (aset numbering idx (1+ n))
1728 when (> idx relative-level) do (aset numbering idx 0)))))
1729 options)))
1731 (defun org-export--populate-ignore-list (data options)
1732 "Return list of elements and objects to ignore during export.
1733 DATA is the parse tree to traverse. OPTIONS is the plist holding
1734 export options."
1735 (let* (ignore
1736 walk-data ; for byte-compiler.
1737 (walk-data
1738 (function
1739 (lambda (data options selected)
1740 ;; Collect ignored elements or objects into IGNORE-LIST.
1741 (mapc
1742 (lambda (el)
1743 (if (org-export--skip-p el options selected) (push el ignore)
1744 (let ((type (org-element-type el)))
1745 (if (and (eq (plist-get options :with-archived-trees)
1746 'headline)
1747 (eq (org-element-type el) 'headline)
1748 (org-element-property :archivedp el))
1749 ;; If headline is archived but tree below has
1750 ;; to be skipped, add it to ignore list.
1751 (mapc (lambda (e) (push e ignore))
1752 (org-element-contents el))
1753 ;; Move into recursive objects/elements.
1754 (when (org-element-contents el)
1755 (funcall walk-data el options selected))))))
1756 (org-element-contents data))))))
1757 ;; Main call. First find trees containing a select tag, if any.
1758 (funcall walk-data data options (org-export--selected-trees data options))
1759 ;; Return value.
1760 ignore))
1762 (defun org-export--selected-trees (data info)
1763 "Return list of headlines containing a select tag in their tree.
1764 DATA is parsed data as returned by `org-element-parse-buffer'.
1765 INFO is a plist holding export options."
1766 (let* (selected-trees
1767 walk-data ; for byte-compiler.
1768 (walk-data
1769 (function
1770 (lambda (data genealogy)
1771 (case (org-element-type data)
1772 (org-data (mapc (lambda (el) (funcall walk-data el genealogy))
1773 (org-element-contents data)))
1774 (headline
1775 (let ((tags (org-element-property :tags data)))
1776 (if (loop for tag in (plist-get info :select-tags)
1777 thereis (member tag tags))
1778 ;; When a select tag is found, mark full
1779 ;; genealogy and every headline within the tree
1780 ;; as acceptable.
1781 (setq selected-trees
1782 (append
1783 genealogy
1784 (org-element-map data 'headline 'identity)
1785 selected-trees))
1786 ;; Else, continue searching in tree, recursively.
1787 (mapc
1788 (lambda (el) (funcall walk-data el (cons data genealogy)))
1789 (org-element-contents data))))))))))
1790 (funcall walk-data data nil) selected-trees))
1792 (defun org-export--skip-p (blob options selected)
1793 "Non-nil when element or object BLOB should be skipped during export.
1794 OPTIONS is the plist holding export options. SELECTED, when
1795 non-nil, is a list of headlines belonging to a tree with a select
1796 tag."
1797 (case (org-element-type blob)
1798 ;; Check headline.
1799 (headline
1800 (let ((with-tasks (plist-get options :with-tasks))
1801 (todo (org-element-property :todo-keyword blob))
1802 (todo-type (org-element-property :todo-type blob))
1803 (archived (plist-get options :with-archived-trees))
1804 (tags (org-element-property :tags blob)))
1806 ;; Ignore subtrees with an exclude tag.
1807 (loop for k in (plist-get options :exclude-tags)
1808 thereis (member k tags))
1809 ;; When a select tag is present in the buffer, ignore any tree
1810 ;; without it.
1811 (and selected (not (memq blob selected)))
1812 ;; Ignore commented sub-trees.
1813 (org-element-property :commentedp blob)
1814 ;; Ignore archived subtrees if `:with-archived-trees' is nil.
1815 (and (not archived) (org-element-property :archivedp blob))
1816 ;; Ignore tasks, if specified by `:with-tasks' property.
1817 (and todo
1818 (or (not with-tasks)
1819 (and (memq with-tasks '(todo done))
1820 (not (eq todo-type with-tasks)))
1821 (and (consp with-tasks) (not (member todo with-tasks))))))))
1822 ;; Check inlinetask.
1823 (inlinetask (not (plist-get options :with-inlinetasks)))
1824 ;; Check timestamp.
1825 (timestamp
1826 (case (plist-get options :with-timestamps)
1827 ;; No timestamp allowed.
1828 ('nil t)
1829 ;; Only active timestamps allowed and the current one isn't
1830 ;; active.
1831 (active
1832 (not (memq (org-element-property :type blob)
1833 '(active active-range))))
1834 ;; Only inactive timestamps allowed and the current one isn't
1835 ;; inactive.
1836 (inactive
1837 (not (memq (org-element-property :type blob)
1838 '(inactive inactive-range))))))
1839 ;; Check drawer.
1840 (drawer
1841 (or (not (plist-get options :with-drawers))
1842 (and (consp (plist-get options :with-drawers))
1843 (not (member (org-element-property :drawer-name blob)
1844 (plist-get options :with-drawers))))))
1845 ;; Check table-row.
1846 (table-row (org-export-table-row-is-special-p blob options))
1847 ;; Check table-cell.
1848 (table-cell
1849 (and (org-export-table-has-special-column-p
1850 (org-export-get-parent-table blob))
1851 (not (org-export-get-previous-element blob options))))
1852 ;; Check clock.
1853 (clock (not (plist-get options :with-clocks)))
1854 ;; Check planning.
1855 (planning (not (plist-get options :with-plannings)))))
1859 ;;; The Transcoder
1861 ;; `org-export-data' reads a parse tree (obtained with, i.e.
1862 ;; `org-element-parse-buffer') and transcodes it into a specified
1863 ;; back-end output. It takes care of filtering out elements or
1864 ;; objects according to export options and organizing the output blank
1865 ;; lines and white space are preserved. The function memoizes its
1866 ;; results, so it is cheap to call it within translators.
1868 ;; Internally, three functions handle the filtering of objects and
1869 ;; elements during the export. In particular,
1870 ;; `org-export-ignore-element' marks an element or object so future
1871 ;; parse tree traversals skip it, `org-export--interpret-p' tells which
1872 ;; elements or objects should be seen as real Org syntax and
1873 ;; `org-export-expand' transforms the others back into their original
1874 ;; shape
1876 ;; `org-export-transcoder' is an accessor returning appropriate
1877 ;; translator function for a given element or object.
1879 (defun org-export-transcoder (blob info)
1880 "Return appropriate transcoder for BLOB.
1881 INFO is a plist containing export directives."
1882 (let ((type (org-element-type blob)))
1883 ;; Return contents only for complete parse trees.
1884 (if (eq type 'org-data) (lambda (blob contents info) contents)
1885 (let ((transcoder (cdr (assq type (plist-get info :translate-alist)))))
1886 (and (functionp transcoder) transcoder)))))
1888 (defun org-export-data (data info)
1889 "Convert DATA into current back-end format.
1891 DATA is a parse tree, an element or an object or a secondary
1892 string. INFO is a plist holding export options.
1894 Return transcoded string."
1895 (let ((memo (gethash data (plist-get info :exported-data) 'no-memo)))
1896 (if (not (eq memo 'no-memo)) memo
1897 (let* ((type (org-element-type data))
1898 (results
1899 (cond
1900 ;; Ignored element/object.
1901 ((memq data (plist-get info :ignore-list)) nil)
1902 ;; Plain text.
1903 ((eq type 'plain-text)
1904 (org-export-filter-apply-functions
1905 (plist-get info :filter-plain-text)
1906 (let ((transcoder (org-export-transcoder data info)))
1907 (if transcoder (funcall transcoder data info) data))
1908 info))
1909 ;; Uninterpreted element/object: change it back to Org
1910 ;; syntax and export again resulting raw string.
1911 ((not (org-export--interpret-p data info))
1912 (org-export-data
1913 (org-export-expand
1914 data
1915 (mapconcat (lambda (blob) (org-export-data blob info))
1916 (org-element-contents data)
1917 ""))
1918 info))
1919 ;; Secondary string.
1920 ((not type)
1921 (mapconcat (lambda (obj) (org-export-data obj info)) data ""))
1922 ;; Element/Object without contents or, as a special case,
1923 ;; headline with archive tag and archived trees restricted
1924 ;; to title only.
1925 ((or (not (org-element-contents data))
1926 (and (eq type 'headline)
1927 (eq (plist-get info :with-archived-trees) 'headline)
1928 (org-element-property :archivedp data)))
1929 (let ((transcoder (org-export-transcoder data info)))
1930 (and (functionp transcoder)
1931 (funcall transcoder data nil info))))
1932 ;; Element/Object with contents.
1934 (let ((transcoder (org-export-transcoder data info)))
1935 (when transcoder
1936 (let* ((greaterp (memq type org-element-greater-elements))
1937 (objectp
1938 (and (not greaterp)
1939 (memq type org-element-recursive-objects)))
1940 (contents
1941 (mapconcat
1942 (lambda (element) (org-export-data element info))
1943 (org-element-contents
1944 (if (or greaterp objectp) data
1945 ;; Elements directly containing objects
1946 ;; must have their indentation normalized
1947 ;; first.
1948 (org-element-normalize-contents
1949 data
1950 ;; When normalizing contents of the first
1951 ;; paragraph in an item or a footnote
1952 ;; definition, ignore first line's
1953 ;; indentation: there is none and it
1954 ;; might be misleading.
1955 (when (eq type 'paragraph)
1956 (let ((parent (org-export-get-parent data)))
1957 (and
1958 (eq (car (org-element-contents parent))
1959 data)
1960 (memq (org-element-type parent)
1961 '(footnote-definition item))))))))
1962 "")))
1963 (funcall transcoder data
1964 (if (not greaterp) contents
1965 (org-element-normalize-string contents))
1966 info))))))))
1967 ;; Final result will be memoized before being returned.
1968 (puthash
1969 data
1970 (cond
1971 ((not results) nil)
1972 ((memq type '(org-data plain-text nil)) results)
1973 ;; Append the same white space between elements or objects as in
1974 ;; the original buffer, and call appropriate filters.
1976 (let ((results
1977 (org-export-filter-apply-functions
1978 (plist-get info (intern (format ":filter-%s" type)))
1979 (let ((post-blank (or (org-element-property :post-blank data)
1980 0)))
1981 (if (memq type org-element-all-elements)
1982 (concat (org-element-normalize-string results)
1983 (make-string post-blank ?\n))
1984 (concat results (make-string post-blank ? ))))
1985 info)))
1986 results)))
1987 (plist-get info :exported-data))))))
1989 (defun org-export--interpret-p (blob info)
1990 "Non-nil if element or object BLOB should be interpreted as Org syntax.
1991 Check is done according to export options INFO, stored as
1992 a plist."
1993 (case (org-element-type blob)
1994 ;; ... entities...
1995 (entity (plist-get info :with-entities))
1996 ;; ... emphasis...
1997 (emphasis (plist-get info :with-emphasize))
1998 ;; ... fixed-width areas.
1999 (fixed-width (plist-get info :with-fixed-width))
2000 ;; ... footnotes...
2001 ((footnote-definition footnote-reference)
2002 (plist-get info :with-footnotes))
2003 ;; ... sub/superscripts...
2004 ((subscript superscript)
2005 (let ((sub/super-p (plist-get info :with-sub-superscript)))
2006 (if (eq sub/super-p '{})
2007 (org-element-property :use-brackets-p blob)
2008 sub/super-p)))
2009 ;; ... tables...
2010 (table (plist-get info :with-tables))
2011 (otherwise t)))
2013 (defun org-export-expand (blob contents)
2014 "Expand a parsed element or object to its original state.
2015 BLOB is either an element or an object. CONTENTS is its
2016 contents, as a string or nil."
2017 (funcall
2018 (intern (format "org-element-%s-interpreter" (org-element-type blob)))
2019 blob contents))
2021 (defun org-export-ignore-element (element info)
2022 "Add ELEMENT to `:ignore-list' in INFO.
2024 Any element in `:ignore-list' will be skipped when using
2025 `org-element-map'. INFO is modified by side effects."
2026 (plist-put info :ignore-list (cons element (plist-get info :ignore-list))))
2030 ;;; The Filter System
2032 ;; Filters allow end-users to tweak easily the transcoded output.
2033 ;; They are the functional counterpart of hooks, as every filter in
2034 ;; a set is applied to the return value of the previous one.
2036 ;; Every set is back-end agnostic. Although, a filter is always
2037 ;; called, in addition to the string it applies to, with the back-end
2038 ;; used as argument, so it's easy for the end-user to add back-end
2039 ;; specific filters in the set. The communication channel, as
2040 ;; a plist, is required as the third argument.
2042 ;; From the developer side, filters sets can be installed in the
2043 ;; process with the help of `org-export-define-backend', which
2044 ;; internally sets `org-BACKEND-filters-alist' variable. Each
2045 ;; association has a key among the following symbols and a function or
2046 ;; a list of functions as value.
2048 ;; - `:filter-parse-tree' applies directly on the complete parsed
2049 ;; tree. It's the only filters set that doesn't apply to a string.
2050 ;; Users can set it through `org-export-filter-parse-tree-functions'
2051 ;; variable.
2053 ;; - `:filter-final-output' applies to the final transcoded string.
2054 ;; Users can set it with `org-export-filter-final-output-functions'
2055 ;; variable
2057 ;; - `:filter-plain-text' applies to any string not recognized as Org
2058 ;; syntax. `org-export-filter-plain-text-functions' allows users to
2059 ;; configure it.
2061 ;; - `:filter-TYPE' applies on the string returned after an element or
2062 ;; object of type TYPE has been transcoded. An user can modify
2063 ;; `org-export-filter-TYPE-functions'
2065 ;; All filters sets are applied with
2066 ;; `org-export-filter-apply-functions' function. Filters in a set are
2067 ;; applied in a LIFO fashion. It allows developers to be sure that
2068 ;; their filters will be applied first.
2070 ;; Filters properties are installed in communication channel with
2071 ;; `org-export-install-filters' function.
2073 ;; Eventually, a hook (`org-export-before-parsing-hook') is run just
2074 ;; before parsing to allow for heavy structure modifications.
2077 ;;;; Before Parsing Hook
2079 (defvar org-export-before-parsing-hook nil
2080 "Hook run before parsing an export buffer.
2082 This is run after include keywords have been expanded and Babel
2083 code executed, on a copy of original buffer's area being
2084 exported. Visibility is the same as in the original one. Point
2085 is left at the beginning of the new one.
2087 Every function in this hook will be called with one argument: the
2088 back-end currently used, as a symbol.")
2091 ;;;; Special Filters
2093 (defvar org-export-filter-parse-tree-functions nil
2094 "List of functions applied to the parsed tree.
2095 Each filter is called with three arguments: the parse tree, as
2096 returned by `org-element-parse-buffer', the back-end, as
2097 a symbol, and the communication channel, as a plist. It must
2098 return the modified parse tree to transcode.")
2100 (defvar org-export-filter-final-output-functions nil
2101 "List of functions applied to the transcoded string.
2102 Each filter is called with three arguments: the full transcoded
2103 string, the back-end, as a symbol, and the communication channel,
2104 as a plist. It must return a string that will be used as the
2105 final export output.")
2107 (defvar org-export-filter-plain-text-functions nil
2108 "List of functions applied to plain text.
2109 Each filter is called with three arguments: a string which
2110 contains no Org syntax, the back-end, as a symbol, and the
2111 communication channel, as a plist. It must return a string or
2112 nil.")
2115 ;;;; Elements Filters
2117 (defvar org-export-filter-center-block-functions nil
2118 "List of functions applied to a transcoded center block.
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-clock-functions nil
2124 "List of functions applied to a transcoded clock.
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-drawer-functions nil
2130 "List of functions applied to a transcoded drawer.
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-dynamic-block-functions nil
2136 "List of functions applied to a transcoded dynamic-block.
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-headline-functions nil
2142 "List of functions applied to a transcoded headline.
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-inlinetask-functions nil
2148 "List of functions applied to a transcoded inlinetask.
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-plain-list-functions nil
2154 "List of functions applied to a transcoded plain-list.
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-item-functions nil
2160 "List of functions applied to a transcoded item.
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-functions nil
2166 "List of functions applied to a transcoded 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-comment-block-functions nil
2172 "List of functions applied to a transcoded comment-comment.
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-example-block-functions nil
2178 "List of functions applied to a transcoded example-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-export-block-functions nil
2184 "List of functions applied to a transcoded export-block.
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-fixed-width-functions nil
2190 "List of functions applied to a transcoded fixed-width.
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-footnote-definition-functions nil
2196 "List of functions applied to a transcoded footnote-definition.
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-horizontal-rule-functions nil
2202 "List of functions applied to a transcoded horizontal-rule.
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-keyword-functions nil
2208 "List of functions applied to a transcoded keyword.
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-latex-environment-functions nil
2214 "List of functions applied to a transcoded latex-environment.
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-babel-call-functions nil
2220 "List of functions applied to a transcoded babel-call.
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-paragraph-functions nil
2226 "List of functions applied to a transcoded paragraph.
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-planning-functions nil
2232 "List of functions applied to a transcoded planning.
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-property-drawer-functions nil
2238 "List of functions applied to a transcoded property-drawer.
2239 Each filter is called with three arguments: the transcoded data,
2240 as a string, the back-end, as a symbol, and the communication
2241 channel, as a plist. It must return a string or nil.")
2243 (defvar org-export-filter-quote-block-functions nil
2244 "List of functions applied to a transcoded quote block.
2245 Each filter is called with three arguments: the transcoded quote
2246 data, as a string, the back-end, as a symbol, and the
2247 communication channel, as a plist. It must return a string or
2248 nil.")
2250 (defvar org-export-filter-quote-section-functions nil
2251 "List of functions applied to a transcoded quote-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-section-functions nil
2257 "List of functions applied to a transcoded section.
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-special-block-functions nil
2263 "List of functions applied to a transcoded special 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-src-block-functions nil
2269 "List of functions applied to a transcoded src-block.
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-functions nil
2275 "List of functions applied to a transcoded table.
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-cell-functions nil
2281 "List of functions applied to a transcoded table-cell.
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-table-row-functions nil
2287 "List of functions applied to a transcoded table-row.
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.")
2292 (defvar org-export-filter-verse-block-functions nil
2293 "List of functions applied to a transcoded verse block.
2294 Each filter is called with three arguments: the transcoded data,
2295 as a string, the back-end, as a symbol, and the communication
2296 channel, as a plist. It must return a string or nil.")
2299 ;;;; Objects Filters
2301 (defvar org-export-filter-bold-functions nil
2302 "List of functions applied to transcoded bold 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-code-functions nil
2308 "List of functions applied to transcoded code text.
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-entity-functions nil
2314 "List of functions applied to a transcoded entity.
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-export-snippet-functions nil
2320 "List of functions applied to a transcoded export-snippet.
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-footnote-reference-functions nil
2326 "List of functions applied to a transcoded footnote-reference.
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-babel-call-functions nil
2332 "List of functions applied to a transcoded inline-babel-call.
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-inline-src-block-functions nil
2338 "List of functions applied to a transcoded inline-src-block.
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-italic-functions nil
2344 "List of functions applied to transcoded italic text.
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-latex-fragment-functions nil
2350 "List of functions applied to a transcoded latex-fragment.
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-line-break-functions nil
2356 "List of functions applied to a transcoded line-break.
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-link-functions nil
2362 "List of functions applied to a transcoded link.
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-macro-functions nil
2368 "List of functions applied to a transcoded macro.
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-radio-target-functions nil
2374 "List of functions applied to a transcoded radio-target.
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-statistics-cookie-functions nil
2380 "List of functions applied to a transcoded statistics-cookie.
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-strike-through-functions nil
2386 "List of functions applied to transcoded strike-through text.
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-subscript-functions nil
2392 "List of functions applied to a transcoded subscript.
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-superscript-functions nil
2398 "List of functions applied to a transcoded superscript.
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-target-functions nil
2404 "List of functions applied to a transcoded target.
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-timestamp-functions nil
2410 "List of functions applied to a transcoded timestamp.
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-underline-functions nil
2416 "List of functions applied to transcoded underline 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.")
2421 (defvar org-export-filter-verbatim-functions nil
2422 "List of functions applied to transcoded verbatim text.
2423 Each filter is called with three arguments: the transcoded data,
2424 as a string, the back-end, as a symbol, and the communication
2425 channel, as a plist. It must return a string or nil.")
2428 ;;;; Filters Tools
2430 ;; Internal function `org-export-install-filters' installs filters
2431 ;; hard-coded in back-ends (developer filters) and filters from global
2432 ;; variables (user filters) in the communication channel.
2434 ;; Internal function `org-export-filter-apply-functions' takes care
2435 ;; about applying each filter in order to a given data. It ignores
2436 ;; filters returning a nil value but stops whenever a filter returns
2437 ;; an empty string.
2439 (defun org-export-filter-apply-functions (filters value info)
2440 "Call every function in FILTERS.
2442 Functions are called with arguments VALUE, current export
2443 back-end and INFO. A function returning a nil value will be
2444 skipped. If it returns the empty string, the process ends and
2445 VALUE is ignored.
2447 Call is done in a LIFO fashion, to be sure that developer
2448 specified filters, if any, are called first."
2449 (catch 'exit
2450 (dolist (filter filters value)
2451 (let ((result (funcall filter value (plist-get info :back-end) info)))
2452 (cond ((not value))
2453 ((equal value "") (throw 'exit nil))
2454 (t (setq value result)))))))
2456 (defun org-export-install-filters (info)
2457 "Install filters properties in communication channel.
2459 INFO is a plist containing the current communication channel.
2461 Return the updated communication channel."
2462 (let (plist)
2463 ;; Install user defined filters with `org-export-filters-alist'.
2464 (mapc (lambda (p)
2465 (setq plist (plist-put plist (car p) (eval (cdr p)))))
2466 org-export-filters-alist)
2467 ;; Prepend back-end specific filters to that list.
2468 (let ((back-end-filters (intern (format "org-%s-filters-alist"
2469 (plist-get info :back-end)))))
2470 (when (boundp back-end-filters)
2471 (mapc (lambda (p)
2472 ;; Single values get consed, lists are prepended.
2473 (let ((key (car p)) (value (cdr p)))
2474 (when value
2475 (setq plist
2476 (plist-put
2477 plist key
2478 (if (atom value) (cons value (plist-get plist key))
2479 (append value (plist-get plist key))))))))
2480 (eval back-end-filters))))
2481 ;; Return new communication channel.
2482 (org-combine-plists info plist)))
2486 ;;; Core functions
2488 ;; This is the room for the main function, `org-export-as', along with
2489 ;; its derivatives, `org-export-to-buffer' and `org-export-to-file'.
2490 ;; They differ only by the way they output the resulting code.
2492 ;; `org-export-output-file-name' is an auxiliary function meant to be
2493 ;; used with `org-export-to-file'. With a given extension, it tries
2494 ;; to provide a canonical file name to write export output to.
2496 ;; Note that `org-export-as' doesn't really parse the current buffer,
2497 ;; but a copy of it (with the same buffer-local variables and
2498 ;; visibility), where include keywords are expanded and Babel blocks
2499 ;; are executed, if appropriate.
2500 ;; `org-export-with-current-buffer-copy' macro prepares that copy.
2502 ;; File inclusion is taken care of by
2503 ;; `org-export-expand-include-keyword' and
2504 ;; `org-export--prepare-file-contents'. Structure wise, including
2505 ;; a whole Org file in a buffer often makes little sense. For
2506 ;; example, if the file contains an headline and the include keyword
2507 ;; was within an item, the item should contain the headline. That's
2508 ;; why file inclusion should be done before any structure can be
2509 ;; associated to the file, that is before parsing.
2511 (defun org-export-as
2512 (backend &optional subtreep visible-only body-only ext-plist noexpand)
2513 "Transcode current Org buffer into BACKEND code.
2515 If narrowing is active in the current buffer, only transcode its
2516 narrowed part.
2518 If a region is active, transcode that region.
2520 When optional argument SUBTREEP is non-nil, transcode the
2521 sub-tree at point, extracting information from the headline
2522 properties first.
2524 When optional argument VISIBLE-ONLY is non-nil, don't export
2525 contents of hidden elements.
2527 When optional argument BODY-ONLY is non-nil, only return body
2528 code, without preamble nor postamble.
2530 Optional argument EXT-PLIST, when provided, is a property list
2531 with external parameters overriding Org default settings, but
2532 still inferior to file-local settings.
2534 Optional argument NOEXPAND, when non-nil, prevents included files
2535 to be expanded and Babel code to be executed.
2537 Return code as a string."
2538 (save-excursion
2539 (save-restriction
2540 ;; Narrow buffer to an appropriate region or subtree for
2541 ;; parsing. If parsing subtree, be sure to remove main headline
2542 ;; too.
2543 (cond ((org-region-active-p)
2544 (narrow-to-region (region-beginning) (region-end)))
2545 (subtreep
2546 (org-narrow-to-subtree)
2547 (goto-char (point-min))
2548 (forward-line)
2549 (narrow-to-region (point) (point-max))))
2550 ;; 1. Get export environment from original buffer. Also install
2551 ;; user's and developer's filters.
2552 (let ((info (org-export-install-filters
2553 (org-export-get-environment backend subtreep ext-plist)))
2554 ;; 2. Get parse tree. Buffer isn't parsed directly.
2555 ;; Instead, a temporary copy is created, where include
2556 ;; keywords are expanded and code blocks are evaluated.
2557 (tree (let ((buf (or (buffer-file-name (buffer-base-buffer))
2558 (current-buffer))))
2559 (org-export-with-current-buffer-copy
2560 (unless noexpand
2561 (org-export-expand-include-keyword)
2562 ;; TODO: Setting `org-current-export-file' is
2563 ;; required by Org Babel to properly resolve
2564 ;; noweb references. Once "org-exp.el" is
2565 ;; removed, modify
2566 ;; `org-export-blocks-preprocess' so it accepts
2567 ;; the value as an argument instead.
2568 (let ((org-current-export-file buf))
2569 (org-export-blocks-preprocess)))
2570 (goto-char (point-min))
2571 ;; Run hook
2572 ;; `org-export-before-parsing-hook'. with current
2573 ;; back-end as argument.
2574 (run-hook-with-args
2575 'org-export-before-parsing-hook backend)
2576 ;; Eventually parse buffer.
2577 (org-element-parse-buffer nil visible-only)))))
2578 ;; 3. Call parse-tree filters to get the final tree.
2579 (setq tree
2580 (org-export-filter-apply-functions
2581 (plist-get info :filter-parse-tree) tree info))
2582 ;; 4. Now tree is complete, compute its properties and add
2583 ;; them to communication channel.
2584 (setq info
2585 (org-combine-plists
2586 info (org-export-collect-tree-properties tree info)))
2587 ;; 5. Eventually transcode TREE. Wrap the resulting string
2588 ;; into a template, if required. Eventually call
2589 ;; final-output filter.
2590 (let* ((body (org-element-normalize-string (org-export-data tree info)))
2591 (template (cdr (assq 'template
2592 (plist-get info :translate-alist))))
2593 (output (org-export-filter-apply-functions
2594 (plist-get info :filter-final-output)
2595 (if (or (not (functionp template)) body-only) body
2596 (funcall template body info))
2597 info)))
2598 ;; Maybe add final OUTPUT to kill ring, then return it.
2599 (when org-export-copy-to-kill-ring (org-kill-new output))
2600 output)))))
2602 (defun org-export-to-buffer
2603 (backend buffer &optional subtreep visible-only body-only ext-plist noexpand)
2604 "Call `org-export-as' with output to a specified buffer.
2606 BACKEND is the back-end used for transcoding, as a symbol.
2608 BUFFER is the output buffer. If it already exists, it will be
2609 erased first, otherwise, it will be created.
2611 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY, EXT-PLIST
2612 and NOEXPAND are similar to those used in `org-export-as', which
2613 see.
2615 Return buffer."
2616 (let ((out (org-export-as
2617 backend subtreep visible-only body-only ext-plist noexpand))
2618 (buffer (get-buffer-create buffer)))
2619 (with-current-buffer buffer
2620 (erase-buffer)
2621 (insert out)
2622 (goto-char (point-min)))
2623 buffer))
2625 (defun org-export-to-file
2626 (backend file &optional subtreep visible-only body-only ext-plist noexpand)
2627 "Call `org-export-as' with output to a specified file.
2629 BACKEND is the back-end used for transcoding, as a symbol. FILE
2630 is the name of the output file, as a string.
2632 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY, EXT-PLIST
2633 and NOEXPAND are similar to those used in `org-export-as', which
2634 see.
2636 Return output file's name."
2637 ;; Checks for FILE permissions. `write-file' would do the same, but
2638 ;; we'd rather avoid needless transcoding of parse tree.
2639 (unless (file-writable-p file) (error "Output file not writable"))
2640 ;; Insert contents to a temporary buffer and write it to FILE.
2641 (let ((out (org-export-as
2642 backend subtreep visible-only body-only ext-plist noexpand)))
2643 (with-temp-buffer
2644 (insert out)
2645 (let ((coding-system-for-write org-export-coding-system))
2646 (write-file file))))
2647 ;; Return full path.
2648 file)
2650 (defun org-export-output-file-name (extension &optional subtreep pub-dir)
2651 "Return output file's name according to buffer specifications.
2653 EXTENSION is a string representing the output file extension,
2654 with the leading dot.
2656 With a non-nil optional argument SUBTREEP, try to determine
2657 output file's name by looking for \"EXPORT_FILE_NAME\" property
2658 of subtree at point.
2660 When optional argument PUB-DIR is set, use it as the publishing
2661 directory.
2663 When optional argument VISIBLE-ONLY is non-nil, don't export
2664 contents of hidden elements.
2666 Return file name as a string, or nil if it couldn't be
2667 determined."
2668 (let ((base-name
2669 ;; File name may come from EXPORT_FILE_NAME subtree property,
2670 ;; assuming point is at beginning of said sub-tree.
2671 (file-name-sans-extension
2672 (or (and subtreep
2673 (org-entry-get
2674 (save-excursion
2675 (ignore-errors (org-back-to-heading) (point)))
2676 "EXPORT_FILE_NAME" t))
2677 ;; File name may be extracted from buffer's associated
2678 ;; file, if any.
2679 (buffer-file-name (buffer-base-buffer))
2680 ;; Can't determine file name on our own: Ask user.
2681 (let ((read-file-name-function
2682 (and org-completion-use-ido 'ido-read-file-name)))
2683 (read-file-name
2684 "Output file: " pub-dir nil nil nil
2685 (lambda (name)
2686 (string= (file-name-extension name t) extension))))))))
2687 ;; Build file name. Enforce EXTENSION over whatever user may have
2688 ;; come up with. PUB-DIR, if defined, always has precedence over
2689 ;; any provided path.
2690 (cond
2691 (pub-dir
2692 (concat (file-name-as-directory pub-dir)
2693 (file-name-nondirectory base-name)
2694 extension))
2695 ((string= (file-name-nondirectory base-name) base-name)
2696 (concat (file-name-as-directory ".") base-name extension))
2697 (t (concat base-name extension)))))
2699 (defmacro org-export-with-current-buffer-copy (&rest body)
2700 "Apply BODY in a copy of the current buffer.
2702 The copy preserves local variables and visibility of the original
2703 buffer.
2705 Point is at buffer's beginning when BODY is applied."
2706 (org-with-gensyms (original-buffer offset buffer-string overlays)
2707 `(let ((,original-buffer (current-buffer))
2708 (,offset (1- (point-min)))
2709 (,buffer-string (buffer-string))
2710 (,overlays (mapcar
2711 'copy-overlay (overlays-in (point-min) (point-max)))))
2712 (with-temp-buffer
2713 (let ((buffer-invisibility-spec nil))
2714 (org-clone-local-variables
2715 ,original-buffer
2716 "^\\(org-\\|orgtbl-\\|major-mode$\\|outline-\\(regexp\\|level\\)$\\)")
2717 (insert ,buffer-string)
2718 (mapc (lambda (ov)
2719 (move-overlay
2721 (- (overlay-start ov) ,offset)
2722 (- (overlay-end ov) ,offset)
2723 (current-buffer)))
2724 ,overlays)
2725 (goto-char (point-min))
2726 (progn ,@body))))))
2727 (def-edebug-spec org-export-with-current-buffer-copy (body))
2729 (defun org-export-expand-include-keyword (&optional included dir)
2730 "Expand every include keyword in buffer.
2731 Optional argument INCLUDED is a list of included file names along
2732 with their line restriction, when appropriate. It is used to
2733 avoid infinite recursion. Optional argument DIR is the current
2734 working directory. It is used to properly resolve relative
2735 paths."
2736 (let ((case-fold-search t))
2737 (goto-char (point-min))
2738 (while (re-search-forward "^[ \t]*#\\+INCLUDE: \\(.*\\)" nil t)
2739 (when (eq (org-element-type (save-match-data (org-element-at-point)))
2740 'keyword)
2741 (beginning-of-line)
2742 ;; Extract arguments from keyword's value.
2743 (let* ((value (match-string 1))
2744 (ind (org-get-indentation))
2745 (file (and (string-match "^\"\\(\\S-+\\)\"" value)
2746 (prog1 (expand-file-name (match-string 1 value) dir)
2747 (setq value (replace-match "" nil nil value)))))
2748 (lines
2749 (and (string-match
2750 ":lines +\"\\(\\(?:[0-9]+\\)?-\\(?:[0-9]+\\)?\\)\"" value)
2751 (prog1 (match-string 1 value)
2752 (setq value (replace-match "" nil nil value)))))
2753 (env (cond ((string-match "\\<example\\>" value) 'example)
2754 ((string-match "\\<src\\(?: +\\(.*\\)\\)?" value)
2755 (match-string 1 value))))
2756 ;; Minimal level of included file defaults to the child
2757 ;; level of the current headline, if any, or one. It
2758 ;; only applies is the file is meant to be included as
2759 ;; an Org one.
2760 (minlevel
2761 (and (not env)
2762 (if (string-match ":minlevel +\\([0-9]+\\)" value)
2763 (prog1 (string-to-number (match-string 1 value))
2764 (setq value (replace-match "" nil nil value)))
2765 (let ((cur (org-current-level)))
2766 (if cur (1+ (org-reduced-level cur)) 1))))))
2767 ;; Remove keyword.
2768 (delete-region (point) (progn (forward-line) (point)))
2769 (cond
2770 ((not (file-readable-p file)) (error "Cannot include file %s" file))
2771 ;; Check if files has already been parsed. Look after
2772 ;; inclusion lines too, as different parts of the same file
2773 ;; can be included too.
2774 ((member (list file lines) included)
2775 (error "Recursive file inclusion: %s" file))
2777 (cond
2778 ((eq env 'example)
2779 (insert
2780 (let ((ind-str (make-string ind ? ))
2781 (contents
2782 ;; Protect sensitive contents with commas.
2783 (replace-regexp-in-string
2784 "\\(^\\)\\([*]\\|[ \t]*#\\+\\)" ","
2785 (org-export--prepare-file-contents file lines)
2786 nil nil 1)))
2787 (format "%s#+BEGIN_EXAMPLE\n%s%s#+END_EXAMPLE\n"
2788 ind-str contents ind-str))))
2789 ((stringp env)
2790 (insert
2791 (let ((ind-str (make-string ind ? ))
2792 (contents
2793 ;; Protect sensitive contents with commas.
2794 (replace-regexp-in-string
2795 (if (string= env "org") "\\(^\\)\\(.\\)"
2796 "\\(^\\)\\([*]\\|[ \t]*#\\+\\)") ","
2797 (org-export--prepare-file-contents file lines)
2798 nil nil 1)))
2799 (format "%s#+BEGIN_SRC %s\n%s%s#+END_SRC\n"
2800 ind-str env contents ind-str))))
2802 (insert
2803 (with-temp-buffer
2804 (org-mode)
2805 (insert
2806 (org-export--prepare-file-contents file lines ind minlevel))
2807 (org-export-expand-include-keyword
2808 (cons (list file lines) included)
2809 (file-name-directory file))
2810 (buffer-string))))))))))))
2812 (defun org-export--prepare-file-contents (file &optional lines ind minlevel)
2813 "Prepare the contents of FILE for inclusion and return them as a string.
2815 When optional argument LINES is a string specifying a range of
2816 lines, include only those lines.
2818 Optional argument IND, when non-nil, is an integer specifying the
2819 global indentation of returned contents. Since its purpose is to
2820 allow an included file to stay in the same environment it was
2821 created \(i.e. a list item), it doesn't apply past the first
2822 headline encountered.
2824 Optional argument MINLEVEL, when non-nil, is an integer
2825 specifying the level that any top-level headline in the included
2826 file should have."
2827 (with-temp-buffer
2828 (insert-file-contents file)
2829 (when lines
2830 (let* ((lines (split-string lines "-"))
2831 (lbeg (string-to-number (car lines)))
2832 (lend (string-to-number (cadr lines)))
2833 (beg (if (zerop lbeg) (point-min)
2834 (goto-char (point-min))
2835 (forward-line (1- lbeg))
2836 (point)))
2837 (end (if (zerop lend) (point-max)
2838 (goto-char (point-min))
2839 (forward-line (1- lend))
2840 (point))))
2841 (narrow-to-region beg end)))
2842 ;; Remove blank lines at beginning and end of contents. The logic
2843 ;; behind that removal is that blank lines around include keyword
2844 ;; override blank lines in included file.
2845 (goto-char (point-min))
2846 (org-skip-whitespace)
2847 (beginning-of-line)
2848 (delete-region (point-min) (point))
2849 (goto-char (point-max))
2850 (skip-chars-backward " \r\t\n")
2851 (forward-line)
2852 (delete-region (point) (point-max))
2853 ;; If IND is set, preserve indentation of include keyword until
2854 ;; the first headline encountered.
2855 (when ind
2856 (unless (eq major-mode 'org-mode) (org-mode))
2857 (goto-char (point-min))
2858 (let ((ind-str (make-string ind ? )))
2859 (while (not (or (eobp) (looking-at org-outline-regexp-bol)))
2860 ;; Do not move footnote definitions out of column 0.
2861 (unless (and (looking-at org-footnote-definition-re)
2862 (eq (org-element-type (org-element-at-point))
2863 'footnote-definition))
2864 (insert ind-str))
2865 (forward-line))))
2866 ;; When MINLEVEL is specified, compute minimal level for headlines
2867 ;; in the file (CUR-MIN), and remove stars to each headline so
2868 ;; that headlines with minimal level have a level of MINLEVEL.
2869 (when minlevel
2870 (unless (eq major-mode 'org-mode) (org-mode))
2871 (let ((levels (org-map-entries
2872 (lambda () (org-reduced-level (org-current-level))))))
2873 (when levels
2874 (let ((offset (- minlevel (apply 'min levels))))
2875 (unless (zerop offset)
2876 (when org-odd-levels-only (setq offset (* offset 2)))
2877 ;; Only change stars, don't bother moving whole
2878 ;; sections.
2879 (org-map-entries
2880 (lambda () (if (< offset 0) (delete-char (abs offset))
2881 (insert (make-string offset ?*))))))))))
2882 (buffer-string)))
2885 ;;; Tools For Back-Ends
2887 ;; A whole set of tools is available to help build new exporters. Any
2888 ;; function general enough to have its use across many back-ends
2889 ;; should be added here.
2891 ;; As of now, functions operating on footnotes, headlines, links,
2892 ;; macros, references, src-blocks, tables and tables of contents are
2893 ;; implemented.
2895 ;;;; For Affiliated Keywords
2897 ;; `org-export-read-attribute' reads a property from a given element
2898 ;; as a plist. It can be used to normalize affiliated keywords'
2899 ;; syntax.
2901 (defun org-export-read-attribute (attribute element &optional property)
2902 "Turn ATTRIBUTE property from ELEMENT into a plist.
2904 When optional argument PROPERTY is non-nil, return the value of
2905 that property within attributes.
2907 This function assumes attributes are defined as \":keyword
2908 value\" pairs. It is appropriate for `:attr_html' like
2909 properties."
2910 (let ((attributes
2911 (let ((value (org-element-property attribute element)))
2912 (and value
2913 (read (format "(%s)" (mapconcat 'identity value " ")))))))
2914 (if property (plist-get attributes property) attributes)))
2917 ;;;; For Export Snippets
2919 ;; Every export snippet is transmitted to the back-end. Though, the
2920 ;; latter will only retain one type of export-snippet, ignoring
2921 ;; others, based on the former's target back-end. The function
2922 ;; `org-export-snippet-backend' returns that back-end for a given
2923 ;; export-snippet.
2925 (defun org-export-snippet-backend (export-snippet)
2926 "Return EXPORT-SNIPPET targeted back-end as a symbol.
2927 Translation, with `org-export-snippet-translation-alist', is
2928 applied."
2929 (let ((back-end (org-element-property :back-end export-snippet)))
2930 (intern
2931 (or (cdr (assoc back-end org-export-snippet-translation-alist))
2932 back-end))))
2935 ;;;; For Footnotes
2937 ;; `org-export-collect-footnote-definitions' is a tool to list
2938 ;; actually used footnotes definitions in the whole parse tree, or in
2939 ;; an headline, in order to add footnote listings throughout the
2940 ;; transcoded data.
2942 ;; `org-export-footnote-first-reference-p' is a predicate used by some
2943 ;; back-ends, when they need to attach the footnote definition only to
2944 ;; the first occurrence of the corresponding label.
2946 ;; `org-export-get-footnote-definition' and
2947 ;; `org-export-get-footnote-number' provide easier access to
2948 ;; additional information relative to a footnote reference.
2950 (defun org-export-collect-footnote-definitions (data info)
2951 "Return an alist between footnote numbers, labels and definitions.
2953 DATA is the parse tree from which definitions are collected.
2954 INFO is the plist used as a communication channel.
2956 Definitions are sorted by order of references. They either
2957 appear as Org data or as a secondary string for inlined
2958 footnotes. Unreferenced definitions are ignored."
2959 (let* (num-alist
2960 collect-fn ; for byte-compiler.
2961 (collect-fn
2962 (function
2963 (lambda (data)
2964 ;; Collect footnote number, label and definition in DATA.
2965 (org-element-map
2966 data 'footnote-reference
2967 (lambda (fn)
2968 (when (org-export-footnote-first-reference-p fn info)
2969 (let ((def (org-export-get-footnote-definition fn info)))
2970 (push
2971 (list (org-export-get-footnote-number fn info)
2972 (org-element-property :label fn)
2973 def)
2974 num-alist)
2975 ;; Also search in definition for nested footnotes.
2976 (when (eq (org-element-property :type fn) 'standard)
2977 (funcall collect-fn def)))))
2978 ;; Don't enter footnote definitions since it will happen
2979 ;; when their first reference is found.
2980 info nil 'footnote-definition)))))
2981 (funcall collect-fn (plist-get info :parse-tree))
2982 (reverse num-alist)))
2984 (defun org-export-footnote-first-reference-p (footnote-reference info)
2985 "Non-nil when a footnote reference is the first one for its label.
2987 FOOTNOTE-REFERENCE is the footnote reference being considered.
2988 INFO is the plist used as a communication channel."
2989 (let ((label (org-element-property :label footnote-reference)))
2990 ;; Anonymous footnotes are always a first reference.
2991 (if (not label) t
2992 ;; Otherwise, return the first footnote with the same LABEL and
2993 ;; test if it is equal to FOOTNOTE-REFERENCE.
2994 (let* (search-refs ; for byte-compiler.
2995 (search-refs
2996 (function
2997 (lambda (data)
2998 (org-element-map
2999 data 'footnote-reference
3000 (lambda (fn)
3001 (cond
3002 ((string= (org-element-property :label fn) label)
3003 (throw 'exit fn))
3004 ;; If FN isn't inlined, be sure to traverse its
3005 ;; definition before resuming search. See
3006 ;; comments in `org-export-get-footnote-number'
3007 ;; for more information.
3008 ((eq (org-element-property :type fn) 'standard)
3009 (funcall search-refs
3010 (org-export-get-footnote-definition fn info)))))
3011 ;; Don't enter footnote definitions since it will
3012 ;; happen when their first reference is found.
3013 info 'first-match 'footnote-definition)))))
3014 (eq (catch 'exit (funcall search-refs (plist-get info :parse-tree)))
3015 footnote-reference)))))
3017 (defun org-export-get-footnote-definition (footnote-reference info)
3018 "Return definition of FOOTNOTE-REFERENCE as parsed data.
3019 INFO is the plist used as a communication channel."
3020 (let ((label (org-element-property :label footnote-reference)))
3021 (or (org-element-property :inline-definition footnote-reference)
3022 (cdr (assoc label (plist-get info :footnote-definition-alist))))))
3024 (defun org-export-get-footnote-number (footnote info)
3025 "Return number associated to a footnote.
3027 FOOTNOTE is either a footnote reference or a footnote definition.
3028 INFO is the plist used as a communication channel."
3029 (let* ((label (org-element-property :label footnote))
3030 seen-refs
3031 search-ref ; For byte-compiler.
3032 (search-ref
3033 (function
3034 (lambda (data)
3035 ;; Search footnote references through DATA, filling
3036 ;; SEEN-REFS along the way.
3037 (org-element-map
3038 data 'footnote-reference
3039 (lambda (fn)
3040 (let ((fn-lbl (org-element-property :label fn)))
3041 (cond
3042 ;; Anonymous footnote match: return number.
3043 ((and (not fn-lbl) (eq fn footnote))
3044 (throw 'exit (1+ (length seen-refs))))
3045 ;; Labels match: return number.
3046 ((and label (string= label fn-lbl))
3047 (throw 'exit (1+ (length seen-refs))))
3048 ;; Anonymous footnote: it's always a new one. Also,
3049 ;; be sure to return nil from the `cond' so
3050 ;; `first-match' doesn't get us out of the loop.
3051 ((not fn-lbl) (push 'inline seen-refs) nil)
3052 ;; Label not seen so far: add it so SEEN-REFS.
3054 ;; Also search for subsequent references in
3055 ;; footnote definition so numbering follows reading
3056 ;; logic. Note that we don't have to care about
3057 ;; inline definitions, since `org-element-map'
3058 ;; already traverses them at the right time.
3060 ;; Once again, return nil to stay in the loop.
3061 ((not (member fn-lbl seen-refs))
3062 (push fn-lbl seen-refs)
3063 (funcall search-ref
3064 (org-export-get-footnote-definition fn info))
3065 nil))))
3066 ;; Don't enter footnote definitions since it will happen
3067 ;; when their first reference is found.
3068 info 'first-match 'footnote-definition)))))
3069 (catch 'exit (funcall search-ref (plist-get info :parse-tree)))))
3072 ;;;; For Headlines
3074 ;; `org-export-get-relative-level' is a shortcut to get headline
3075 ;; level, relatively to the lower headline level in the parsed tree.
3077 ;; `org-export-get-headline-number' returns the section number of an
3078 ;; headline, while `org-export-number-to-roman' allows to convert it
3079 ;; to roman numbers.
3081 ;; `org-export-low-level-p', `org-export-first-sibling-p' and
3082 ;; `org-export-last-sibling-p' are three useful predicates when it
3083 ;; comes to fulfill the `:headline-levels' property.
3085 (defun org-export-get-relative-level (headline info)
3086 "Return HEADLINE relative level within current parsed tree.
3087 INFO is a plist holding contextual information."
3088 (+ (org-element-property :level headline)
3089 (or (plist-get info :headline-offset) 0)))
3091 (defun org-export-low-level-p (headline info)
3092 "Non-nil when HEADLINE is considered as low level.
3094 INFO is a plist used as a communication channel.
3096 A low level headlines has a relative level greater than
3097 `:headline-levels' property value.
3099 Return value is the difference between HEADLINE relative level
3100 and the last level being considered as high enough, or nil."
3101 (let ((limit (plist-get info :headline-levels)))
3102 (when (wholenump limit)
3103 (let ((level (org-export-get-relative-level headline info)))
3104 (and (> level limit) (- level limit))))))
3106 (defun org-export-get-headline-number (headline info)
3107 "Return HEADLINE numbering as a list of numbers.
3108 INFO is a plist holding contextual information."
3109 (cdr (assoc headline (plist-get info :headline-numbering))))
3111 (defun org-export-numbered-headline-p (headline info)
3112 "Return a non-nil value if HEADLINE element should be numbered.
3113 INFO is a plist used as a communication channel."
3114 (let ((sec-num (plist-get info :section-numbers))
3115 (level (org-export-get-relative-level headline info)))
3116 (if (wholenump sec-num) (<= level sec-num) sec-num)))
3118 (defun org-export-number-to-roman (n)
3119 "Convert integer N into a roman numeral."
3120 (let ((roman '((1000 . "M") (900 . "CM") (500 . "D") (400 . "CD")
3121 ( 100 . "C") ( 90 . "XC") ( 50 . "L") ( 40 . "XL")
3122 ( 10 . "X") ( 9 . "IX") ( 5 . "V") ( 4 . "IV")
3123 ( 1 . "I")))
3124 (res ""))
3125 (if (<= n 0)
3126 (number-to-string n)
3127 (while roman
3128 (if (>= n (caar roman))
3129 (setq n (- n (caar roman))
3130 res (concat res (cdar roman)))
3131 (pop roman)))
3132 res)))
3134 (defun org-export-get-tags (element info &optional tags)
3135 "Return list of tags associated to ELEMENT.
3137 ELEMENT has either an `headline' or an `inlinetask' type. INFO
3138 is a plist used as a communication channel.
3140 Select tags (see `org-export-select-tags') and exclude tags (see
3141 `org-export-exclude-tags') are removed from the list.
3143 When non-nil, optional argument TAGS should be a list of strings.
3144 Any tag belonging to this list will also be removed."
3145 (org-remove-if (lambda (tag) (or (member tag (plist-get info :select-tags))
3146 (member tag (plist-get info :exclude-tags))
3147 (member tag tags)))
3148 (org-element-property :tags element)))
3150 (defun org-export-first-sibling-p (headline info)
3151 "Non-nil when HEADLINE is the first sibling in its sub-tree.
3152 INFO is a plist used as a communication channel."
3153 (not (eq (org-element-type (org-export-get-previous-element headline info))
3154 'headline)))
3156 (defun org-export-last-sibling-p (headline info)
3157 "Non-nil when HEADLINE is the last sibling in its sub-tree.
3158 INFO is a plist used as a communication channel."
3159 (not (org-export-get-next-element headline info)))
3162 ;;;; For Links
3164 ;; `org-export-solidify-link-text' turns a string into a safer version
3165 ;; for links, replacing most non-standard characters with hyphens.
3167 ;; `org-export-get-coderef-format' returns an appropriate format
3168 ;; string for coderefs.
3170 ;; `org-export-inline-image-p' returns a non-nil value when the link
3171 ;; provided should be considered as an inline image.
3173 ;; `org-export-resolve-fuzzy-link' searches destination of fuzzy links
3174 ;; (i.e. links with "fuzzy" as type) within the parsed tree, and
3175 ;; returns an appropriate unique identifier when found, or nil.
3177 ;; `org-export-resolve-id-link' returns the first headline with
3178 ;; specified id or custom-id in parse tree, the path to the external
3179 ;; file with the id or nil when neither was found.
3181 ;; `org-export-resolve-coderef' associates a reference to a line
3182 ;; number in the element it belongs, or returns the reference itself
3183 ;; when the element isn't numbered.
3185 (defun org-export-solidify-link-text (s)
3186 "Take link text S and make a safe target out of it."
3187 (save-match-data
3188 (mapconcat 'identity (org-split-string s "[^a-zA-Z0-9_.-]+") "-")))
3190 (defun org-export-get-coderef-format (path desc)
3191 "Return format string for code reference link.
3192 PATH is the link path. DESC is its description."
3193 (save-match-data
3194 (cond ((not desc) "%s")
3195 ((string-match (regexp-quote (concat "(" path ")")) desc)
3196 (replace-match "%s" t t desc))
3197 (t desc))))
3199 (defun org-export-inline-image-p (link &optional rules)
3200 "Non-nil if LINK object points to an inline image.
3202 Optional argument is a set of RULES defining inline images. It
3203 is an alist where associations have the following shape:
3205 \(TYPE . REGEXP)
3207 Applying a rule means apply REGEXP against LINK's path when its
3208 type is TYPE. The function will return a non-nil value if any of
3209 the provided rules is non-nil. The default rule is
3210 `org-export-default-inline-image-rule'.
3212 This only applies to links without a description."
3213 (and (not (org-element-contents link))
3214 (let ((case-fold-search t)
3215 (rules (or rules org-export-default-inline-image-rule)))
3216 (catch 'exit
3217 (mapc
3218 (lambda (rule)
3219 (and (string= (org-element-property :type link) (car rule))
3220 (string-match (cdr rule)
3221 (org-element-property :path link))
3222 (throw 'exit t)))
3223 rules)
3224 ;; Return nil if no rule matched.
3225 nil))))
3227 (defun org-export-resolve-coderef (ref info)
3228 "Resolve a code reference REF.
3230 INFO is a plist used as a communication channel.
3232 Return associated line number in source code, or REF itself,
3233 depending on src-block or example element's switches."
3234 (org-element-map
3235 (plist-get info :parse-tree) '(example-block src-block)
3236 (lambda (el)
3237 (with-temp-buffer
3238 (insert (org-trim (org-element-property :value el)))
3239 (let* ((label-fmt (regexp-quote
3240 (or (org-element-property :label-fmt el)
3241 org-coderef-label-format)))
3242 (ref-re
3243 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
3244 (replace-regexp-in-string "%s" ref label-fmt nil t))))
3245 ;; Element containing REF is found. Resolve it to either
3246 ;; a label or a line number, as needed.
3247 (when (re-search-backward ref-re nil t)
3248 (cond
3249 ((org-element-property :use-labels el) ref)
3250 ((eq (org-element-property :number-lines el) 'continued)
3251 (+ (org-export-get-loc el info) (line-number-at-pos)))
3252 (t (line-number-at-pos)))))))
3253 info 'first-match))
3255 (defun org-export-resolve-fuzzy-link (link info)
3256 "Return LINK destination.
3258 INFO is a plist holding contextual information.
3260 Return value can be an object, an element, or nil:
3262 - If LINK path matches a target object (i.e. <<path>>) or
3263 element (i.e. \"#+TARGET: path\"), return it.
3265 - If LINK path exactly matches the name affiliated keyword
3266 \(i.e. #+NAME: path) of an element, return that element.
3268 - If LINK path exactly matches any headline name, return that
3269 element. If more than one headline share that name, priority
3270 will be given to the one with the closest common ancestor, if
3271 any, or the first one in the parse tree otherwise.
3273 - Otherwise, return nil.
3275 Assume LINK type is \"fuzzy\"."
3276 (let* ((path (org-element-property :path link))
3277 (match-title-p (eq (aref path 0) ?*)))
3278 (cond
3279 ;; First try to find a matching "<<path>>" unless user specified
3280 ;; he was looking for an headline (path starts with a *
3281 ;; character).
3282 ((and (not match-title-p)
3283 (loop for target in (plist-get info :target-list)
3284 when (string= (org-element-property :value target) path)
3285 return target)))
3286 ;; Then try to find an element with a matching "#+NAME: path"
3287 ;; affiliated keyword.
3288 ((and (not match-title-p)
3289 (org-element-map
3290 (plist-get info :parse-tree) org-element-all-elements
3291 (lambda (el)
3292 (when (string= (org-element-property :name el) path) el))
3293 info 'first-match)))
3294 ;; Last case: link either points to an headline or to
3295 ;; nothingness. Try to find the source, with priority given to
3296 ;; headlines with the closest common ancestor. If such candidate
3297 ;; is found, return it, otherwise return nil.
3299 (let ((find-headline
3300 (function
3301 ;; Return first headline whose `:raw-value' property
3302 ;; is NAME in parse tree DATA, or nil.
3303 (lambda (name data)
3304 (org-element-map
3305 data 'headline
3306 (lambda (headline)
3307 (when (string=
3308 (org-element-property :raw-value headline)
3309 name)
3310 headline))
3311 info 'first-match)))))
3312 ;; Search among headlines sharing an ancestor with link,
3313 ;; from closest to farthest.
3314 (or (catch 'exit
3315 (mapc
3316 (lambda (parent)
3317 (when (eq (org-element-type parent) 'headline)
3318 (let ((foundp (funcall find-headline path parent)))
3319 (when foundp (throw 'exit foundp)))))
3320 (org-export-get-genealogy link)) nil)
3321 ;; No match with a common ancestor: try the full parse-tree.
3322 (funcall find-headline
3323 (if match-title-p (substring path 1) path)
3324 (plist-get info :parse-tree))))))))
3326 (defun org-export-resolve-id-link (link info)
3327 "Return headline referenced as LINK destination.
3329 INFO is a plist used as a communication channel.
3331 Return value can be the headline element matched in current parse
3332 tree, a file name or nil. Assume LINK type is either \"id\" or
3333 \"custom-id\"."
3334 (let ((id (org-element-property :path link)))
3335 ;; First check if id is within the current parse tree.
3336 (or (org-element-map
3337 (plist-get info :parse-tree) 'headline
3338 (lambda (headline)
3339 (when (or (string= (org-element-property :id headline) id)
3340 (string= (org-element-property :custom-id headline) id))
3341 headline))
3342 info 'first-match)
3343 ;; Otherwise, look for external files.
3344 (cdr (assoc id (plist-get info :id-alist))))))
3346 (defun org-export-resolve-radio-link (link info)
3347 "Return radio-target object referenced as LINK destination.
3349 INFO is a plist used as a communication channel.
3351 Return value can be a radio-target object or nil. Assume LINK
3352 has type \"radio\"."
3353 (let ((path (org-element-property :path link)))
3354 (org-element-map
3355 (plist-get info :parse-tree) 'radio-target
3356 (lambda (radio)
3357 (when (equal (org-element-property :value radio) path) radio))
3358 info 'first-match)))
3361 ;;;; For Macros
3363 ;; `org-export-expand-macro' simply takes care of expanding macros.
3365 (defun org-export-expand-macro (macro info)
3366 "Expand MACRO and return it as a string.
3367 INFO is a plist holding export options."
3368 (let* ((key (org-element-property :key macro))
3369 (args (org-element-property :args macro))
3370 ;; User's macros are stored in the communication channel with
3371 ;; a ":macro-" prefix. Replace arguments in VALUE. Also
3372 ;; expand recursively macros within.
3373 (value (org-export-data
3374 (mapcar
3375 (lambda (obj)
3376 (if (not (stringp obj)) (org-export-data obj info)
3377 (replace-regexp-in-string
3378 "\\$[0-9]+"
3379 (lambda (arg)
3380 (nth (1- (string-to-number (substring arg 1))) args))
3381 obj)))
3382 (plist-get info (intern (format ":macro-%s" key))))
3383 info)))
3384 ;; VALUE starts with "(eval": it is a s-exp, `eval' it.
3385 (when (string-match "\\`(eval\\>" value) (setq value (eval (read value))))
3386 ;; Return string.
3387 (format "%s" (or value ""))))
3390 ;;;; For References
3392 ;; `org-export-get-ordinal' associates a sequence number to any object
3393 ;; or element.
3395 (defun org-export-get-ordinal (element info &optional types predicate)
3396 "Return ordinal number of an element or object.
3398 ELEMENT is the element or object considered. INFO is the plist
3399 used as a communication channel.
3401 Optional argument TYPES, when non-nil, is a list of element or
3402 object types, as symbols, that should also be counted in.
3403 Otherwise, only provided element's type is considered.
3405 Optional argument PREDICATE is a function returning a non-nil
3406 value if the current element or object should be counted in. It
3407 accepts two arguments: the element or object being considered and
3408 the plist used as a communication channel. This allows to count
3409 only a certain type of objects (i.e. inline images).
3411 Return value is a list of numbers if ELEMENT is an headline or an
3412 item. It is nil for keywords. It represents the footnote number
3413 for footnote definitions and footnote references. If ELEMENT is
3414 a target, return the same value as if ELEMENT was the closest
3415 table, item or headline containing the target. In any other
3416 case, return the sequence number of ELEMENT among elements or
3417 objects of the same type."
3418 ;; A target keyword, representing an invisible target, never has
3419 ;; a sequence number.
3420 (unless (eq (org-element-type element) 'keyword)
3421 ;; Ordinal of a target object refer to the ordinal of the closest
3422 ;; table, item, or headline containing the object.
3423 (when (eq (org-element-type element) 'target)
3424 (setq element
3425 (loop for parent in (org-export-get-genealogy element)
3426 when
3427 (memq
3428 (org-element-type parent)
3429 '(footnote-definition footnote-reference headline item
3430 table))
3431 return parent)))
3432 (case (org-element-type element)
3433 ;; Special case 1: An headline returns its number as a list.
3434 (headline (org-export-get-headline-number element info))
3435 ;; Special case 2: An item returns its number as a list.
3436 (item (let ((struct (org-element-property :structure element)))
3437 (org-list-get-item-number
3438 (org-element-property :begin element)
3439 struct
3440 (org-list-prevs-alist struct)
3441 (org-list-parents-alist struct))))
3442 ((footnote-definition footnote-reference)
3443 (org-export-get-footnote-number element info))
3444 (otherwise
3445 (let ((counter 0))
3446 ;; Increment counter until ELEMENT is found again.
3447 (org-element-map
3448 (plist-get info :parse-tree) (or types (org-element-type element))
3449 (lambda (el)
3450 (cond
3451 ((eq element el) (1+ counter))
3452 ((not predicate) (incf counter) nil)
3453 ((funcall predicate el info) (incf counter) nil)))
3454 info 'first-match))))))
3457 ;;;; For Src-Blocks
3459 ;; `org-export-get-loc' counts number of code lines accumulated in
3460 ;; src-block or example-block elements with a "+n" switch until
3461 ;; a given element, excluded. Note: "-n" switches reset that count.
3463 ;; `org-export-unravel-code' extracts source code (along with a code
3464 ;; references alist) from an `element-block' or `src-block' type
3465 ;; element.
3467 ;; `org-export-format-code' applies a formatting function to each line
3468 ;; of code, providing relative line number and code reference when
3469 ;; appropriate. Since it doesn't access the original element from
3470 ;; which the source code is coming, it expects from the code calling
3471 ;; it to know if lines should be numbered and if code references
3472 ;; should appear.
3474 ;; Eventually, `org-export-format-code-default' is a higher-level
3475 ;; function (it makes use of the two previous functions) which handles
3476 ;; line numbering and code references inclusion, and returns source
3477 ;; code in a format suitable for plain text or verbatim output.
3479 (defun org-export-get-loc (element info)
3480 "Return accumulated lines of code up to ELEMENT.
3482 INFO is the plist used as a communication channel.
3484 ELEMENT is excluded from count."
3485 (let ((loc 0))
3486 (org-element-map
3487 (plist-get info :parse-tree)
3488 `(src-block example-block ,(org-element-type element))
3489 (lambda (el)
3490 (cond
3491 ;; ELEMENT is reached: Quit the loop.
3492 ((eq el element))
3493 ;; Only count lines from src-block and example-block elements
3494 ;; with a "+n" or "-n" switch. A "-n" switch resets counter.
3495 ((not (memq (org-element-type el) '(src-block example-block))) nil)
3496 ((let ((linums (org-element-property :number-lines el)))
3497 (when linums
3498 ;; Accumulate locs or reset them.
3499 (let ((lines (org-count-lines
3500 (org-trim (org-element-property :value el)))))
3501 (setq loc (if (eq linums 'new) lines (+ loc lines))))))
3502 ;; Return nil to stay in the loop.
3503 nil)))
3504 info 'first-match)
3505 ;; Return value.
3506 loc))
3508 (defun org-export-unravel-code (element)
3509 "Clean source code and extract references out of it.
3511 ELEMENT has either a `src-block' an `example-block' type.
3513 Return a cons cell whose CAR is the source code, cleaned from any
3514 reference and protective comma and CDR is an alist between
3515 relative line number (integer) and name of code reference on that
3516 line (string)."
3517 (let* ((line 0) refs
3518 ;; Get code and clean it. Remove blank lines at its
3519 ;; beginning and end. Also remove protective commas.
3520 (code (let ((c (replace-regexp-in-string
3521 "\\`\\([ \t]*\n\\)+" ""
3522 (replace-regexp-in-string
3523 "\\(:?[ \t]*\n\\)*[ \t]*\\'" "\n"
3524 (org-element-property :value element)))))
3525 ;; If appropriate, remove global indentation.
3526 (unless (or org-src-preserve-indentation
3527 (org-element-property :preserve-indent element))
3528 (setq c (org-remove-indentation c)))
3529 ;; Free up the protected lines. Note: Org blocks
3530 ;; have commas at the beginning or every line.
3531 (if (string= (org-element-property :language element) "org")
3532 (replace-regexp-in-string "^," "" c)
3533 (replace-regexp-in-string
3534 "^\\(,\\)\\(:?\\*\\|[ \t]*#\\+\\)" "" c nil nil 1))))
3535 ;; Get format used for references.
3536 (label-fmt (regexp-quote
3537 (or (org-element-property :label-fmt element)
3538 org-coderef-label-format)))
3539 ;; Build a regexp matching a loc with a reference.
3540 (with-ref-re
3541 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)[ \t]*\\)$"
3542 (replace-regexp-in-string
3543 "%s" "\\([-a-zA-Z0-9_ ]+\\)" label-fmt nil t))))
3544 ;; Return value.
3545 (cons
3546 ;; Code with references removed.
3547 (org-element-normalize-string
3548 (mapconcat
3549 (lambda (loc)
3550 (incf line)
3551 (if (not (string-match with-ref-re loc)) loc
3552 ;; Ref line: remove ref, and signal its position in REFS.
3553 (push (cons line (match-string 3 loc)) refs)
3554 (replace-match "" nil nil loc 1)))
3555 (org-split-string code "\n") "\n"))
3556 ;; Reference alist.
3557 refs)))
3559 (defun org-export-format-code (code fun &optional num-lines ref-alist)
3560 "Format CODE by applying FUN line-wise and return it.
3562 CODE is a string representing the code to format. FUN is
3563 a function. It must accept three arguments: a line of
3564 code (string), the current line number (integer) or nil and the
3565 reference associated to the current line (string) or nil.
3567 Optional argument NUM-LINES can be an integer representing the
3568 number of code lines accumulated until the current code. Line
3569 numbers passed to FUN will take it into account. If it is nil,
3570 FUN's second argument will always be nil. This number can be
3571 obtained with `org-export-get-loc' function.
3573 Optional argument REF-ALIST can be an alist between relative line
3574 number (i.e. ignoring NUM-LINES) and the name of the code
3575 reference on it. If it is nil, FUN's third argument will always
3576 be nil. It can be obtained through the use of
3577 `org-export-unravel-code' function."
3578 (let ((--locs (org-split-string code "\n"))
3579 (--line 0))
3580 (org-element-normalize-string
3581 (mapconcat
3582 (lambda (--loc)
3583 (incf --line)
3584 (let ((--ref (cdr (assq --line ref-alist))))
3585 (funcall fun --loc (and num-lines (+ num-lines --line)) --ref)))
3586 --locs "\n"))))
3588 (defun org-export-format-code-default (element info)
3589 "Return source code from ELEMENT, formatted in a standard way.
3591 ELEMENT is either a `src-block' or `example-block' element. INFO
3592 is a plist used as a communication channel.
3594 This function takes care of line numbering and code references
3595 inclusion. Line numbers, when applicable, appear at the
3596 beginning of the line, separated from the code by two white
3597 spaces. Code references, on the other hand, appear flushed to
3598 the right, separated by six white spaces from the widest line of
3599 code."
3600 ;; Extract code and references.
3601 (let* ((code-info (org-export-unravel-code element))
3602 (code (car code-info))
3603 (code-lines (org-split-string code "\n"))
3604 (refs (and (org-element-property :retain-labels element)
3605 (cdr code-info)))
3606 ;; Handle line numbering.
3607 (num-start (case (org-element-property :number-lines element)
3608 (continued (org-export-get-loc element info))
3609 (new 0)))
3610 (num-fmt
3611 (and num-start
3612 (format "%%%ds "
3613 (length (number-to-string
3614 (+ (length code-lines) num-start))))))
3615 ;; Prepare references display, if required. Any reference
3616 ;; should start six columns after the widest line of code,
3617 ;; wrapped with parenthesis.
3618 (max-width
3619 (+ (apply 'max (mapcar 'length code-lines))
3620 (if (not num-start) 0 (length (format num-fmt num-start))))))
3621 (org-export-format-code
3622 code
3623 (lambda (loc line-num ref)
3624 (let ((number-str (and num-fmt (format num-fmt line-num))))
3625 (concat
3626 number-str
3628 (and ref
3629 (concat (make-string
3630 (- (+ 6 max-width)
3631 (+ (length loc) (length number-str))) ? )
3632 (format "(%s)" ref))))))
3633 num-start refs)))
3636 ;;;; For Tables
3638 ;; `org-export-table-has-special-column-p' and and
3639 ;; `org-export-table-row-is-special-p' are predicates used to look for
3640 ;; meta-information about the table structure.
3642 ;; `org-table-has-header-p' tells when the rows before the first rule
3643 ;; should be considered as table's header.
3645 ;; `org-export-table-cell-width', `org-export-table-cell-alignment'
3646 ;; and `org-export-table-cell-borders' extract information from
3647 ;; a table-cell element.
3649 ;; `org-export-table-dimensions' gives the number on rows and columns
3650 ;; in the table, ignoring horizontal rules and special columns.
3651 ;; `org-export-table-cell-address', given a table-cell object, returns
3652 ;; the absolute address of a cell. On the other hand,
3653 ;; `org-export-get-table-cell-at' does the contrary.
3655 ;; `org-export-table-cell-starts-colgroup-p',
3656 ;; `org-export-table-cell-ends-colgroup-p',
3657 ;; `org-export-table-row-starts-rowgroup-p',
3658 ;; `org-export-table-row-ends-rowgroup-p',
3659 ;; `org-export-table-row-starts-header-p' and
3660 ;; `org-export-table-row-ends-header-p' indicate position of current
3661 ;; row or cell within the table.
3663 (defun org-export-table-has-special-column-p (table)
3664 "Non-nil when TABLE has a special column.
3665 All special columns will be ignored during export."
3666 ;; The table has a special column when every first cell of every row
3667 ;; has an empty value or contains a symbol among "/", "#", "!", "$",
3668 ;; "*" "_" and "^". Though, do not consider a first row containing
3669 ;; only empty cells as special.
3670 (let ((special-column-p 'empty))
3671 (catch 'exit
3672 (mapc
3673 (lambda (row)
3674 (when (eq (org-element-property :type row) 'standard)
3675 (let ((value (org-element-contents
3676 (car (org-element-contents row)))))
3677 (cond ((member value '(("/") ("#") ("!") ("$") ("*") ("_") ("^")))
3678 (setq special-column-p 'special))
3679 ((not value))
3680 (t (throw 'exit nil))))))
3681 (org-element-contents table))
3682 (eq special-column-p 'special))))
3684 (defun org-export-table-has-header-p (table info)
3685 "Non-nil when TABLE has an header.
3687 INFO is a plist used as a communication channel.
3689 A table has an header when it contains at least two row groups."
3690 (let ((rowgroup 1) row-flag)
3691 (org-element-map
3692 table 'table-row
3693 (lambda (row)
3694 (cond
3695 ((> rowgroup 1) t)
3696 ((and row-flag (eq (org-element-property :type row) 'rule))
3697 (incf rowgroup) (setq row-flag nil))
3698 ((and (not row-flag) (eq (org-element-property :type row) 'standard))
3699 (setq row-flag t) nil)))
3700 info)))
3702 (defun org-export-table-row-is-special-p (table-row info)
3703 "Non-nil if TABLE-ROW is considered special.
3705 INFO is a plist used as the communication channel.
3707 All special rows will be ignored during export."
3708 (when (eq (org-element-property :type table-row) 'standard)
3709 (let ((first-cell (org-element-contents
3710 (car (org-element-contents table-row)))))
3711 ;; A row is special either when...
3713 ;; ... it starts with a field only containing "/",
3714 (equal first-cell '("/"))
3715 ;; ... the table contains a special column and the row start
3716 ;; with a marking character among, "^", "_", "$" or "!",
3717 (and (org-export-table-has-special-column-p
3718 (org-export-get-parent table-row))
3719 (member first-cell '(("^") ("_") ("$") ("!"))))
3720 ;; ... it contains only alignment cookies and empty cells.
3721 (let ((special-row-p 'empty))
3722 (catch 'exit
3723 (mapc
3724 (lambda (cell)
3725 (let ((value (org-element-contents cell)))
3726 ;; Since VALUE is a secondary string, the following
3727 ;; checks avoid expanding it with `org-export-data'.
3728 (cond ((not value))
3729 ((and (not (cdr value))
3730 (stringp (car value))
3731 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'"
3732 (car value)))
3733 (setq special-row-p 'cookie))
3734 (t (throw 'exit nil)))))
3735 (org-element-contents table-row))
3736 (eq special-row-p 'cookie)))))))
3738 (defun org-export-table-row-group (table-row info)
3739 "Return TABLE-ROW's group.
3741 INFO is a plist used as the communication channel.
3743 Return value is the group number, as an integer, or nil special
3744 rows and table rules. Group 1 is also table's header."
3745 (unless (or (eq (org-element-property :type table-row) 'rule)
3746 (org-export-table-row-is-special-p table-row info))
3747 (let ((group 0) row-flag)
3748 (catch 'found
3749 (mapc
3750 (lambda (row)
3751 (cond
3752 ((and (eq (org-element-property :type row) 'standard)
3753 (not (org-export-table-row-is-special-p row info)))
3754 (unless row-flag (incf group) (setq row-flag t)))
3755 ((eq (org-element-property :type row) 'rule)
3756 (setq row-flag nil)))
3757 (when (eq table-row row) (throw 'found group)))
3758 (org-element-contents (org-export-get-parent table-row)))))))
3760 (defun org-export-table-cell-width (table-cell info)
3761 "Return TABLE-CELL contents width.
3763 INFO is a plist used as the communication channel.
3765 Return value is the width given by the last width cookie in the
3766 same column as TABLE-CELL, or nil."
3767 (let* ((row (org-export-get-parent table-cell))
3768 (column (let ((cells (org-element-contents row)))
3769 (- (length cells) (length (memq table-cell cells)))))
3770 (table (org-export-get-parent-table table-cell))
3771 cookie-width)
3772 (mapc
3773 (lambda (row)
3774 (cond
3775 ;; In a special row, try to find a width cookie at COLUMN.
3776 ((org-export-table-row-is-special-p row info)
3777 (let ((value (org-element-contents
3778 (elt (org-element-contents row) column))))
3779 ;; The following checks avoid expanding unnecessarily the
3780 ;; cell with `org-export-data'
3781 (when (and value
3782 (not (cdr value))
3783 (stringp (car value))
3784 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'" (car value))
3785 (match-string 1 (car value)))
3786 (setq cookie-width
3787 (string-to-number (match-string 1 (car value)))))))
3788 ;; Ignore table rules.
3789 ((eq (org-element-property :type row) 'rule))))
3790 (org-element-contents table))
3791 ;; Return value.
3792 cookie-width))
3794 (defun org-export-table-cell-alignment (table-cell info)
3795 "Return TABLE-CELL contents alignment.
3797 INFO is a plist used as the communication channel.
3799 Return alignment as specified by the last alignment cookie in the
3800 same column as TABLE-CELL. If no such cookie is found, a default
3801 alignment value will be deduced from fraction of numbers in the
3802 column (see `org-table-number-fraction' for more information).
3803 Possible values are `left', `right' and `center'."
3804 (let* ((row (org-export-get-parent table-cell))
3805 (column (let ((cells (org-element-contents row)))
3806 (- (length cells) (length (memq table-cell cells)))))
3807 (table (org-export-get-parent-table table-cell))
3808 (number-cells 0)
3809 (total-cells 0)
3810 cookie-align)
3811 (mapc
3812 (lambda (row)
3813 (cond
3814 ;; In a special row, try to find an alignment cookie at
3815 ;; COLUMN.
3816 ((org-export-table-row-is-special-p row info)
3817 (let ((value (org-element-contents
3818 (elt (org-element-contents row) column))))
3819 ;; Since VALUE is a secondary string, the following checks
3820 ;; avoid useless expansion through `org-export-data'.
3821 (when (and value
3822 (not (cdr value))
3823 (stringp (car value))
3824 (string-match "\\`<\\([lrc]\\)?\\([0-9]+\\)?>\\'"
3825 (car value))
3826 (match-string 1 (car value)))
3827 (setq cookie-align (match-string 1 (car value))))))
3828 ;; Ignore table rules.
3829 ((eq (org-element-property :type row) 'rule))
3830 ;; In a standard row, check if cell's contents are expressing
3831 ;; some kind of number. Increase NUMBER-CELLS accordingly.
3832 ;; Though, don't bother if an alignment cookie has already
3833 ;; defined cell's alignment.
3834 ((not cookie-align)
3835 (let ((value (org-export-data
3836 (org-element-contents
3837 (elt (org-element-contents row) column))
3838 info)))
3839 (incf total-cells)
3840 (when (string-match org-table-number-regexp value)
3841 (incf number-cells))))))
3842 (org-element-contents table))
3843 ;; Return value. Alignment specified by cookies has precedence
3844 ;; over alignment deduced from cells contents.
3845 (cond ((equal cookie-align "l") 'left)
3846 ((equal cookie-align "r") 'right)
3847 ((equal cookie-align "c") 'center)
3848 ((>= (/ (float number-cells) total-cells) org-table-number-fraction)
3849 'right)
3850 (t 'left))))
3852 (defun org-export-table-cell-borders (table-cell info)
3853 "Return TABLE-CELL borders.
3855 INFO is a plist used as a communication channel.
3857 Return value is a list of symbols, or nil. Possible values are:
3858 `top', `bottom', `above', `below', `left' and `right'. Note:
3859 `top' (resp. `bottom') only happen for a cell in the first
3860 row (resp. last row) of the table, ignoring table rules, if any.
3862 Returned borders ignore special rows."
3863 (let* ((row (org-export-get-parent table-cell))
3864 (table (org-export-get-parent-table table-cell))
3865 borders)
3866 ;; Top/above border? TABLE-CELL has a border above when a rule
3867 ;; used to demarcate row groups can be found above. Hence,
3868 ;; finding a rule isn't sufficient to push `above' in BORDERS:
3869 ;; another regular row has to be found above that 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 'above borders))
3877 (throw 'exit nil)))))
3878 ;; Look at every row before the current one.
3879 (cdr (memq row (reverse (org-element-contents table)))))
3880 ;; No rule above, or rule found starts the table (ignoring any
3881 ;; special row): TABLE-CELL is at the top of the table.
3882 (when rule-flag (push 'above borders))
3883 (push 'top borders)))
3884 ;; Bottom/below border? TABLE-CELL has a border below when next
3885 ;; non-regular row below is a rule.
3886 (let (rule-flag)
3887 (catch 'exit
3888 (mapc (lambda (row)
3889 (cond ((eq (org-element-property :type row) 'rule)
3890 (setq rule-flag t))
3891 ((not (org-export-table-row-is-special-p row info))
3892 (if rule-flag (throw 'exit (push 'below borders))
3893 (throw 'exit nil)))))
3894 ;; Look at every row after the current one.
3895 (cdr (memq row (org-element-contents table))))
3896 ;; No rule below, or rule found ends the table (modulo some
3897 ;; special row): TABLE-CELL is at the bottom of the table.
3898 (when rule-flag (push 'below borders))
3899 (push 'bottom borders)))
3900 ;; Right/left borders? They can only be specified by column
3901 ;; groups. Column groups are defined in a row starting with "/".
3902 ;; Also a column groups row only contains "<", "<>", ">" or blank
3903 ;; cells.
3904 (catch 'exit
3905 (let ((column (let ((cells (org-element-contents row)))
3906 (- (length cells) (length (memq table-cell cells))))))
3907 (mapc
3908 (lambda (row)
3909 (unless (eq (org-element-property :type row) 'rule)
3910 (when (equal (org-element-contents
3911 (car (org-element-contents row)))
3912 '("/"))
3913 (let ((column-groups
3914 (mapcar
3915 (lambda (cell)
3916 (let ((value (org-element-contents cell)))
3917 (when (member value '(("<") ("<>") (">") nil))
3918 (car value))))
3919 (org-element-contents row))))
3920 ;; There's a left border when previous cell, if
3921 ;; any, ends a group, or current one starts one.
3922 (when (or (and (not (zerop column))
3923 (member (elt column-groups (1- column))
3924 '(">" "<>")))
3925 (member (elt column-groups column) '("<" "<>")))
3926 (push 'left borders))
3927 ;; There's a right border when next cell, if any,
3928 ;; starts a group, or current one ends one.
3929 (when (or (and (/= (1+ column) (length column-groups))
3930 (member (elt column-groups (1+ column))
3931 '("<" "<>")))
3932 (member (elt column-groups column) '(">" "<>")))
3933 (push 'right borders))
3934 (throw 'exit nil)))))
3935 ;; Table rows are read in reverse order so last column groups
3936 ;; row has precedence over any previous one.
3937 (reverse (org-element-contents table)))))
3938 ;; Return value.
3939 borders))
3941 (defun org-export-table-cell-starts-colgroup-p (table-cell info)
3942 "Non-nil when TABLE-CELL is at the beginning of a row group.
3943 INFO is a plist used as a communication channel."
3944 ;; A cell starts a column group either when it is at the beginning
3945 ;; of a row (or after the special column, if any) or when it has
3946 ;; a left border.
3947 (or (eq (org-element-map
3948 (org-export-get-parent table-cell)
3949 'table-cell 'identity info 'first-match)
3950 table-cell)
3951 (memq 'left (org-export-table-cell-borders table-cell info))))
3953 (defun org-export-table-cell-ends-colgroup-p (table-cell info)
3954 "Non-nil when TABLE-CELL is at the end of a row group.
3955 INFO is a plist used as a communication channel."
3956 ;; A cell ends a column group either when it is at the end of a row
3957 ;; or when it has a right border.
3958 (or (eq (car (last (org-element-contents
3959 (org-export-get-parent table-cell))))
3960 table-cell)
3961 (memq 'right (org-export-table-cell-borders table-cell info))))
3963 (defun org-export-table-row-starts-rowgroup-p (table-row info)
3964 "Non-nil when TABLE-ROW is at the beginning of a column group.
3965 INFO is a plist used as a communication channel."
3966 (unless (or (eq (org-element-property :type table-row) 'rule)
3967 (org-export-table-row-is-special-p table-row info))
3968 (let ((borders (org-export-table-cell-borders
3969 (car (org-element-contents table-row)) info)))
3970 (or (memq 'top borders) (memq 'above borders)))))
3972 (defun org-export-table-row-ends-rowgroup-p (table-row info)
3973 "Non-nil when TABLE-ROW is at the end of a column group.
3974 INFO is a plist used as a communication channel."
3975 (unless (or (eq (org-element-property :type table-row) 'rule)
3976 (org-export-table-row-is-special-p table-row info))
3977 (let ((borders (org-export-table-cell-borders
3978 (car (org-element-contents table-row)) info)))
3979 (or (memq 'bottom borders) (memq 'below borders)))))
3981 (defun org-export-table-row-starts-header-p (table-row info)
3982 "Non-nil when TABLE-ROW is the first table header's row.
3983 INFO is a plist used as a communication channel."
3984 (and (org-export-table-has-header-p
3985 (org-export-get-parent-table table-row) info)
3986 (org-export-table-row-starts-rowgroup-p table-row info)
3987 (= (org-export-table-row-group table-row info) 1)))
3989 (defun org-export-table-row-ends-header-p (table-row info)
3990 "Non-nil when TABLE-ROW is the last table header's row.
3991 INFO is a plist used as a communication channel."
3992 (and (org-export-table-has-header-p
3993 (org-export-get-parent-table table-row) info)
3994 (org-export-table-row-ends-rowgroup-p table-row info)
3995 (= (org-export-table-row-group table-row info) 1)))
3997 (defun org-export-table-dimensions (table info)
3998 "Return TABLE dimensions.
4000 INFO is a plist used as a communication channel.
4002 Return value is a CONS like (ROWS . COLUMNS) where
4003 ROWS (resp. COLUMNS) is the number of exportable
4004 rows (resp. columns)."
4005 (let (first-row (columns 0) (rows 0))
4006 ;; Set number of rows, and extract first one.
4007 (org-element-map
4008 table 'table-row
4009 (lambda (row)
4010 (when (eq (org-element-property :type row) 'standard)
4011 (incf rows)
4012 (unless first-row (setq first-row row)))) info)
4013 ;; Set number of columns.
4014 (org-element-map first-row 'table-cell (lambda (cell) (incf columns)) info)
4015 ;; Return value.
4016 (cons rows columns)))
4018 (defun org-export-table-cell-address (table-cell info)
4019 "Return address of a regular TABLE-CELL object.
4021 TABLE-CELL is the cell considered. INFO is a plist used as
4022 a communication channel.
4024 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
4025 zero-based index. Only exportable cells are considered. The
4026 function returns nil for other cells."
4027 (let* ((table-row (org-export-get-parent table-cell))
4028 (table (org-export-get-parent-table table-cell)))
4029 ;; Ignore cells in special rows or in special column.
4030 (unless (or (org-export-table-row-is-special-p table-row info)
4031 (and (org-export-table-has-special-column-p table)
4032 (eq (car (org-element-contents table-row)) table-cell)))
4033 (cons
4034 ;; Row number.
4035 (let ((row-count 0))
4036 (org-element-map
4037 table 'table-row
4038 (lambda (row)
4039 (cond ((eq (org-element-property :type row) 'rule) nil)
4040 ((eq row table-row) row-count)
4041 (t (incf row-count) nil)))
4042 info 'first-match))
4043 ;; Column number.
4044 (let ((col-count 0))
4045 (org-element-map
4046 table-row 'table-cell
4047 (lambda (cell)
4048 (if (eq cell table-cell) col-count (incf col-count) nil))
4049 info 'first-match))))))
4051 (defun org-export-get-table-cell-at (address table info)
4052 "Return regular table-cell object at ADDRESS in TABLE.
4054 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
4055 zero-based index. TABLE is a table type element. INFO is
4056 a plist used as a communication channel.
4058 If no table-cell, among exportable cells, is found at ADDRESS,
4059 return nil."
4060 (let ((column-pos (cdr address)) (column-count 0))
4061 (org-element-map
4062 ;; Row at (car address) or nil.
4063 (let ((row-pos (car address)) (row-count 0))
4064 (org-element-map
4065 table 'table-row
4066 (lambda (row)
4067 (cond ((eq (org-element-property :type row) 'rule) nil)
4068 ((= row-count row-pos) row)
4069 (t (incf row-count) nil)))
4070 info 'first-match))
4071 'table-cell
4072 (lambda (cell)
4073 (if (= column-count column-pos) cell
4074 (incf column-count) nil))
4075 info 'first-match)))
4078 ;;;; For Tables Of Contents
4080 ;; `org-export-collect-headlines' builds a list of all exportable
4081 ;; headline elements, maybe limited to a certain depth. One can then
4082 ;; easily parse it and transcode it.
4084 ;; Building lists of tables, figures or listings is quite similar.
4085 ;; Once the generic function `org-export-collect-elements' is defined,
4086 ;; `org-export-collect-tables', `org-export-collect-figures' and
4087 ;; `org-export-collect-listings' can be derived from it.
4089 (defun org-export-collect-headlines (info &optional n)
4090 "Collect headlines in order to build a table of contents.
4092 INFO is a plist used as a communication channel.
4094 When optional argument N is an integer, it specifies the depth of
4095 the table of contents. Otherwise, it is set to the value of the
4096 last headline level. See `org-export-headline-levels' for more
4097 information.
4099 Return a list of all exportable headlines as parsed elements."
4100 (unless (wholenump n) (setq n (plist-get info :headline-levels)))
4101 (org-element-map
4102 (plist-get info :parse-tree)
4103 'headline
4104 (lambda (headline)
4105 ;; Strip contents from HEADLINE.
4106 (let ((relative-level (org-export-get-relative-level headline info)))
4107 (unless (> relative-level n) headline)))
4108 info))
4110 (defun org-export-collect-elements (type info &optional predicate)
4111 "Collect referenceable elements of a determined type.
4113 TYPE can be a symbol or a list of symbols specifying element
4114 types to search. Only elements with a caption are collected.
4116 INFO is a plist used as a communication channel.
4118 When non-nil, optional argument PREDICATE is a function accepting
4119 one argument, an element of type TYPE. It returns a non-nil
4120 value when that element should be collected.
4122 Return a list of all elements found, in order of appearance."
4123 (org-element-map
4124 (plist-get info :parse-tree) type
4125 (lambda (element)
4126 (and (org-element-property :caption element)
4127 (or (not predicate) (funcall predicate element))
4128 element))
4129 info))
4131 (defun org-export-collect-tables (info)
4132 "Build a list of tables.
4133 INFO is a plist used as a communication channel.
4135 Return a list of table elements with a caption."
4136 (org-export-collect-elements 'table info))
4138 (defun org-export-collect-figures (info predicate)
4139 "Build a list of figures.
4141 INFO is a plist used as a communication channel. PREDICATE is
4142 a function which accepts one argument: a paragraph element and
4143 whose return value is non-nil when that element should be
4144 collected.
4146 A figure is a paragraph type element, with a caption, verifying
4147 PREDICATE. The latter has to be provided since a \"figure\" is
4148 a vague concept that may depend on back-end.
4150 Return a list of elements recognized as figures."
4151 (org-export-collect-elements 'paragraph info predicate))
4153 (defun org-export-collect-listings (info)
4154 "Build a list of src blocks.
4156 INFO is a plist used as a communication channel.
4158 Return a list of src-block elements with a caption."
4159 (org-export-collect-elements 'src-block info))
4162 ;;;; Topology
4164 ;; Here are various functions to retrieve information about the
4165 ;; neighbourhood of a given element or object. Neighbours of interest
4166 ;; are direct parent (`org-export-get-parent'), parent headline
4167 ;; (`org-export-get-parent-headline'), first element containing an
4168 ;; object, (`org-export-get-parent-element'), parent table
4169 ;; (`org-export-get-parent-table'), previous element or object
4170 ;; (`org-export-get-previous-element') and next element or object
4171 ;; (`org-export-get-next-element').
4173 ;; `org-export-get-genealogy' returns the full genealogy of a given
4174 ;; element or object, from closest parent to full parse tree.
4176 (defun org-export-get-parent (blob)
4177 "Return BLOB parent or nil.
4178 BLOB is the element or object considered."
4179 (org-element-property :parent blob))
4181 (defun org-export-get-genealogy (blob)
4182 "Return full genealogy relative to a given element or object.
4184 BLOB is the element or object being considered.
4186 Ancestors are returned from closest to farthest, the last one
4187 being the full parse tree."
4188 (let (genealogy (parent blob))
4189 (while (setq parent (org-element-property :parent parent))
4190 (push parent genealogy))
4191 (nreverse genealogy)))
4193 (defun org-export-get-parent-headline (blob)
4194 "Return BLOB parent headline or nil.
4195 BLOB is the element or object being considered."
4196 (let ((parent blob))
4197 (while (and (setq parent (org-element-property :parent parent))
4198 (not (eq (org-element-type parent) 'headline))))
4199 parent))
4201 (defun org-export-get-parent-element (object)
4202 "Return first element containing OBJECT or nil.
4203 OBJECT is the object to consider."
4204 (let ((parent object))
4205 (while (and (setq parent (org-element-property :parent parent))
4206 (memq (org-element-type parent) org-element-all-objects)))
4207 parent))
4209 (defun org-export-get-parent-table (object)
4210 "Return OBJECT parent table or nil.
4211 OBJECT is either a `table-cell' or `table-element' type object."
4212 (let ((parent object))
4213 (while (and (setq parent (org-element-property :parent parent))
4214 (not (eq (org-element-type parent) 'table))))
4215 parent))
4217 (defun org-export-get-previous-element (blob info)
4218 "Return previous element or object.
4219 BLOB is an element or object. INFO is a plist used as
4220 a communication channel. Return previous exportable element or
4221 object, a string, or nil."
4222 (let (prev)
4223 (catch 'exit
4224 (mapc (lambda (obj)
4225 (cond ((eq obj blob) (throw 'exit prev))
4226 ((memq obj (plist-get info :ignore-list)))
4227 (t (setq prev obj))))
4228 (org-element-contents (org-export-get-parent blob))))))
4230 (defun org-export-get-next-element (blob info)
4231 "Return next element or object.
4232 BLOB is an element or object. INFO is a plist used as
4233 a communication channel. Return next exportable element or
4234 object, a string, or nil."
4235 (catch 'found
4236 (mapc (lambda (obj)
4237 (unless (memq obj (plist-get info :ignore-list))
4238 (throw 'found obj)))
4239 (cdr (memq blob (org-element-contents (org-export-get-parent blob)))))
4240 nil))
4243 ;;;; Translation
4245 ;; `org-export-translate' translates a string according to language
4246 ;; specified by LANGUAGE keyword or `org-export-language-setup'
4247 ;; variable and a specified charset. `org-export-dictionary' contains
4248 ;; the dictionary used for the translation.
4250 (defconst org-export-dictionary
4251 '(("Author"
4252 ("fr"
4253 :ascii "Auteur"
4254 :latin1 "Auteur"
4255 :utf-8 "Auteur"))
4256 ("Date"
4257 ("fr"
4258 :ascii "Date"
4259 :latin1 "Date"
4260 :utf-8 "Date"))
4261 ("Equation")
4262 ("Figure")
4263 ("Footnotes"
4264 ("fr"
4265 :ascii "Notes de bas de page"
4266 :latin1 "Notes de bas de page"
4267 :utf-8 "Notes de bas de page"))
4268 ("List of Listings"
4269 ("fr"
4270 :ascii "Liste des programmes"
4271 :latin1 "Liste des programmes"
4272 :utf-8 "Liste des programmes"))
4273 ("List of Tables"
4274 ("fr"
4275 :ascii "Liste des tableaux"
4276 :latin1 "Liste des tableaux"
4277 :utf-8 "Liste des tableaux"))
4278 ("Listing %d:"
4279 ("fr"
4280 :ascii "Programme %d :"
4281 :latin1 "Programme %d :"
4282 :utf-8 "Programme nº %d :"))
4283 ("Listing %d: %s"
4284 ("fr"
4285 :ascii "Programme %d : %s"
4286 :latin1 "Programme %d : %s"
4287 :utf-8 "Programme nº %d : %s"))
4288 ("See section %s"
4289 ("fr"
4290 :ascii "cf. section %s"
4291 :latin1 "cf. section %s"
4292 :utf-8 "cf. section %s"))
4293 ("Table %d:"
4294 ("fr"
4295 :ascii "Tableau %d :"
4296 :latin1 "Tableau %d :"
4297 :utf-8 "Tableau nº %d :"))
4298 ("Table %d: %s"
4299 ("fr"
4300 :ascii "Tableau %d : %s"
4301 :latin1 "Tableau %d : %s"
4302 :utf-8 "Tableau nº %d : %s"))
4303 ("Table of Contents"
4304 ("fr"
4305 :ascii "Sommaire"
4306 :latin1 "Table des matières"
4307 :utf-8 "Table des matières"))
4308 ("Unknown reference"
4309 ("fr"
4310 :ascii "Destination inconnue"
4311 :latin1 "Référence inconnue"
4312 :utf-8 "Référence inconnue")))
4313 "Dictionary for export engine.
4315 Alist whose CAR is the string to translate and CDR is an alist
4316 whose CAR is the language string and CDR is a plist whose
4317 properties are possible charsets and values translated terms.
4319 It is used as a database for `org-export-translate'. Since this
4320 function returns the string as-is if no translation was found,
4321 the variable only needs to record values different from the
4322 entry.")
4324 (defun org-export-translate (s encoding info)
4325 "Translate string S according to language specification.
4327 ENCODING is a symbol among `:ascii', `:html', `:latex', `:latin1'
4328 and `:utf-8'. INFO is a plist used as a communication channel.
4330 Translation depends on `:language' property. Return the
4331 translated string. If no translation is found return S."
4332 (let ((lang (plist-get info :language))
4333 (translations (cdr (assoc s org-export-dictionary))))
4334 (or (plist-get (cdr (assoc lang translations)) encoding) s)))
4338 ;;; The Dispatcher
4340 ;; `org-export-dispatch' is the standard interactive way to start an
4341 ;; export process. It uses `org-export-dispatch-ui' as a subroutine
4342 ;; for its interface. Most commons back-ends should have an entry in
4343 ;; it.
4345 ;;;###autoload
4346 (defun org-export-dispatch ()
4347 "Export dispatcher for Org mode.
4349 It provides an access to common export related tasks in a buffer.
4350 Its interface comes in two flavours: standard and expert. While
4351 both share the same set of bindings, only the former displays the
4352 valid keys associations. Set `org-export-dispatch-use-expert-ui'
4353 to switch to one or the other.
4355 Return an error if key pressed has no associated command."
4356 (interactive)
4357 (let* ((input (org-export-dispatch-ui
4358 (if (listp org-export-initial-scope) org-export-initial-scope
4359 (list org-export-initial-scope))
4360 org-export-dispatch-use-expert-ui))
4361 (raw-key (car input))
4362 (optns (cdr input)))
4363 ;; Translate "C-a", "C-b"... into "a", "b"... Then take action
4364 ;; depending on user's key pressed.
4365 (case (if (< raw-key 27) (+ raw-key 96) raw-key)
4366 ;; Allow to quit with "q" key.
4367 (?q nil)
4368 ;; Export with `e-ascii' back-end.
4369 ((?A ?N ?U)
4370 (org-e-ascii-export-as-ascii
4371 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)
4372 `(:ascii-charset ,(case raw-key (?A 'ascii) (?N 'latin1) (t 'utf-8)))))
4373 ((?a ?n ?u)
4374 (org-e-ascii-export-to-ascii
4375 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)
4376 `(:ascii-charset ,(case raw-key (?a 'ascii) (?n 'latin1) (t 'utf-8)))))
4377 ;; Export with `e-latex' back-end.
4378 (?L (org-e-latex-export-as-latex
4379 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
4381 (org-e-latex-export-to-latex
4382 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
4384 (org-e-latex-export-to-pdf
4385 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
4387 (org-open-file
4388 (org-e-latex-export-to-pdf
4389 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))))
4390 ;; Export with `e-html' back-end.
4392 (org-e-html-export-as-html
4393 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
4395 (org-e-html-export-to-html
4396 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
4398 (org-open-file
4399 (org-e-html-export-to-html
4400 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))))
4401 ;; Export with `e-odt' back-end.
4403 (org-e-odt-export-to-odt
4404 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
4406 (org-open-file
4407 (org-e-odt-export-to-odt
4408 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))))
4409 ;; Publishing facilities
4411 (org-e-publish-current-file (memq 'force optns)))
4413 (org-e-publish-current-project (memq 'force optns)))
4415 (let ((project
4416 (assoc (org-icompleting-read
4417 "Publish project: " org-e-publish-project-alist nil t)
4418 org-e-publish-project-alist)))
4419 (org-e-publish project (memq 'force optns))))
4421 (org-e-publish-all (memq 'force optns)))
4422 ;; Undefined command.
4423 (t (error "No command associated with key %s"
4424 (char-to-string raw-key))))))
4426 (defun org-export-dispatch-ui (options expertp)
4427 "Handle interface for `org-export-dispatch'.
4429 OPTIONS is a list containing current interactive options set for
4430 export. It can contain any of the following symbols:
4431 `body' toggles a body-only export
4432 `subtree' restricts export to current subtree
4433 `visible' restricts export to visible part of buffer.
4434 `force' force publishing files.
4436 EXPERTP, when non-nil, triggers expert UI. In that case, no help
4437 buffer is provided, but indications about currently active
4438 options are given in the prompt. Moreover, \[?] allows to switch
4439 back to standard interface.
4441 Return value is a list with key pressed as CAR and a list of
4442 final interactive export options as CDR."
4443 (let ((help
4444 (format "---- (Options) -------------------------------------------
4446 \[1] Body only: %s [2] Export scope: %s
4447 \[3] Visible only: %s [4] Force publishing: %s
4450 --- (ASCII/Latin-1/UTF-8 Export) -------------------------
4452 \[a/n/u] to TXT file [A/N/U] to temporary buffer
4454 --- (HTML Export) ----------------------------------------
4456 \[h] to HTML file [b] ... and open it
4457 \[H] to temporary buffer
4459 --- (LaTeX Export) ---------------------------------------
4461 \[l] to TEX file [L] to temporary buffer
4462 \[p] to PDF file [d] ... and open it
4464 --- (ODF Export) -----------------------------------------
4466 \[o] to ODT file [O] ... and open it
4468 --- (Publish) --------------------------------------------
4470 \[F] current file [P] current project
4471 \[X] a project [E] every project"
4472 (if (memq 'body options) "On " "Off")
4473 (if (memq 'subtree options) "Subtree" "Buffer ")
4474 (if (memq 'visible options) "On " "Off")
4475 (if (memq 'force options) "On " "Off")))
4476 (standard-prompt "Export command: ")
4477 (expert-prompt (format "Export command (%s%s%s%s): "
4478 (if (memq 'body options) "b" "-")
4479 (if (memq 'subtree options) "s" "-")
4480 (if (memq 'visible options) "v" "-")
4481 (if (memq 'force options) "f" "-")))
4482 (handle-keypress
4483 (function
4484 ;; Read a character from command input, toggling interactive
4485 ;; options when applicable. PROMPT is the displayed prompt,
4486 ;; as a string.
4487 (lambda (prompt)
4488 (let ((key (read-char-exclusive prompt)))
4489 (cond
4490 ;; Ignore non-standard characters (i.e. "M-a").
4491 ((not (characterp key)) (org-export-dispatch-ui options expertp))
4492 ;; Help key: Switch back to standard interface if
4493 ;; expert UI was active.
4494 ((eq key ??) (org-export-dispatch-ui options nil))
4495 ;; Toggle export options.
4496 ((memq key '(?1 ?2 ?3 ?4))
4497 (org-export-dispatch-ui
4498 (let ((option (case key (?1 'body) (?2 'subtree) (?3 'visible)
4499 (?4 'force))))
4500 (if (memq option options) (remq option options)
4501 (cons option options)))
4502 expertp))
4503 ;; Action selected: Send key and options back to
4504 ;; `org-export-dispatch'.
4505 (t (cons key options))))))))
4506 ;; With expert UI, just read key with a fancy prompt. In standard
4507 ;; UI, display an intrusive help buffer.
4508 (if expertp (funcall handle-keypress expert-prompt)
4509 (save-window-excursion
4510 (delete-other-windows)
4511 (with-output-to-temp-buffer "*Org Export/Publishing Help*" (princ help))
4512 (org-fit-window-to-buffer
4513 (get-buffer-window "*Org Export/Publishing Help*"))
4514 (funcall handle-keypress standard-prompt)))))
4517 (provide 'org-export)
4518 ;;; org-export.el ends here