Revert "Merge export and special blocks within back-ends"
[org-mode/org-tableheadings.git] / lisp / ox.el
blob050b054373355c969f17828ccdbbffba45e2a529
1 ;;; ox.el --- Generic Export Engine for Org Mode
3 ;; Copyright (C) 2012-2014 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; This library implements a generic export engine for Org, built on
26 ;; its syntactical parser: Org Elements.
28 ;; Besides that parser, the generic exporter is made of three distinct
29 ;; parts:
31 ;; - The communication channel consists in a property list, which is
32 ;; created and updated during the process. Its use is to offer
33 ;; every piece of information, would it be about initial environment
34 ;; or contextual data, all in a single place. The exhaustive list
35 ;; of properties is given in "The Communication Channel" section of
36 ;; this file.
38 ;; - The transcoder walks the parse tree, ignores or treat as plain
39 ;; text elements and objects according to export options, and
40 ;; eventually calls back-end specific functions to do the real
41 ;; transcoding, concatenating their return value along the way.
43 ;; - The filter system is activated at the very beginning and the very
44 ;; end of the export process, and each time an element or an object
45 ;; has been converted. It is the entry point to fine-tune standard
46 ;; output from back-end transcoders. See "The Filter System"
47 ;; section for more information.
49 ;; The core function is `org-export-as'. It returns the transcoded
50 ;; buffer as a string.
52 ;; An export back-end is defined with `org-export-define-backend'.
53 ;; This function can also support specific buffer keywords, OPTION
54 ;; keyword's items and filters. Refer to function's documentation for
55 ;; more information.
57 ;; If the new back-end shares most properties with another one,
58 ;; `org-export-define-derived-backend' can be used to simplify the
59 ;; process.
61 ;; Any back-end can define its own variables. Among them, those
62 ;; customizable should belong to the `org-export-BACKEND' group.
64 ;; Tools for common tasks across back-ends are implemented in the
65 ;; following part of the file.
67 ;; Then, a wrapper macro for asynchronous export,
68 ;; `org-export-async-start', along with tools to display results. are
69 ;; given in the penultimate part.
71 ;; Eventually, a dispatcher (`org-export-dispatch') for standard
72 ;; back-ends is provided in the last one.
74 ;;; Code:
76 (eval-when-compile (require 'cl))
77 (require 'org-element)
78 (require 'org-macro)
79 (require 'ob-exp)
81 (declare-function org-publish "ox-publish" (project &optional force async))
82 (declare-function org-publish-all "ox-publish" (&optional force async))
83 (declare-function
84 org-publish-current-file "ox-publish" (&optional force async))
85 (declare-function org-publish-current-project "ox-publish"
86 (&optional force async))
88 (defvar org-publish-project-alist)
89 (defvar org-table-number-fraction)
90 (defvar org-table-number-regexp)
93 ;;; Internal Variables
95 ;; Among internal variables, the most important is
96 ;; `org-export-options-alist'. This variable define the global export
97 ;; options, shared between every exporter, and how they are acquired.
99 (defconst org-export-max-depth 19
100 "Maximum nesting depth for headlines, counting from 0.")
102 (defconst org-export-options-alist
103 '((:title "TITLE" nil nil space)
104 (:date "DATE" nil nil t)
105 (:author "AUTHOR" nil user-full-name t)
106 (:email "EMAIL" nil user-mail-address t)
107 (:description "DESCRIPTION" nil nil newline)
108 (:keywords "KEYWORDS" nil nil space)
109 (:language "LANGUAGE" nil org-export-default-language t)
110 (:select-tags "SELECT_TAGS" nil org-export-select-tags split)
111 (:exclude-tags "EXCLUDE_TAGS" nil org-export-exclude-tags split)
112 (:creator "CREATOR" nil org-export-creator-string)
113 (:headline-levels nil "H" org-export-headline-levels)
114 (:preserve-breaks nil "\\n" org-export-preserve-breaks)
115 (:section-numbers nil "num" org-export-with-section-numbers)
116 (:time-stamp-file nil "timestamp" org-export-time-stamp-file)
117 (:with-archived-trees nil "arch" org-export-with-archived-trees)
118 (:with-author nil "author" org-export-with-author)
119 (:with-clocks nil "c" org-export-with-clocks)
120 (:with-creator nil "creator" org-export-with-creator)
121 (:with-date nil "date" org-export-with-date)
122 (:with-drawers nil "d" org-export-with-drawers)
123 (:with-email nil "email" org-export-with-email)
124 (:with-emphasize nil "*" org-export-with-emphasize)
125 (:with-entities nil "e" org-export-with-entities)
126 (:with-fixed-width nil ":" org-export-with-fixed-width)
127 (:with-footnotes nil "f" org-export-with-footnotes)
128 (:with-inlinetasks nil "inline" org-export-with-inlinetasks)
129 (:with-latex nil "tex" org-export-with-latex)
130 (:with-planning nil "p" org-export-with-planning)
131 (:with-priority nil "pri" org-export-with-priority)
132 (:with-properties nil "prop" org-export-with-properties)
133 (:with-smart-quotes nil "'" org-export-with-smart-quotes)
134 (:with-special-strings nil "-" org-export-with-special-strings)
135 (:with-statistics-cookies nil "stat" org-export-with-statistics-cookies)
136 (:with-sub-superscript nil "^" org-export-with-sub-superscripts)
137 (:with-toc nil "toc" org-export-with-toc)
138 (:with-tables nil "|" org-export-with-tables)
139 (:with-tags nil "tags" org-export-with-tags)
140 (:with-tasks nil "tasks" org-export-with-tasks)
141 (:with-timestamps nil "<" org-export-with-timestamps)
142 (:with-todo-keywords nil "todo" org-export-with-todo-keywords))
143 "Alist between export properties and ways to set them.
145 The CAR of the alist is the property name, and the CDR is a list
146 like (KEYWORD OPTION DEFAULT BEHAVIOR) where:
148 KEYWORD is a string representing a buffer keyword, or nil. Each
149 property defined this way can also be set, during subtree
150 export, through a headline property named after the keyword
151 with the \"EXPORT_\" prefix (i.e. DATE keyword and EXPORT_DATE
152 property).
153 OPTION is a string that could be found in an #+OPTIONS: line.
154 DEFAULT is the default value for the property.
155 BEHAVIOR determines how Org should handle multiple keywords for
156 the same property. It is a symbol among:
157 nil Keep old value and discard the new one.
158 t Replace old value with the new one.
159 `space' Concatenate the values, separating them with a space.
160 `newline' Concatenate the values, separating them with
161 a newline.
162 `split' Split values at white spaces, and cons them to the
163 previous list.
165 Values set through KEYWORD and OPTION have precedence over
166 DEFAULT.
168 All these properties should be back-end agnostic. Back-end
169 specific properties are set through `org-export-define-backend'.
170 Properties redefined there have precedence over these.")
172 (defconst org-export-special-keywords '("FILETAGS" "SETUPFILE" "OPTIONS")
173 "List of in-buffer keywords that require special treatment.
174 These keywords are not directly associated to a property. The
175 way they are handled must be hard-coded into
176 `org-export--get-inbuffer-options' function.")
178 (defconst org-export-filters-alist
179 '((:filter-body . org-export-filter-body-functions)
180 (:filter-bold . org-export-filter-bold-functions)
181 (:filter-babel-call . org-export-filter-babel-call-functions)
182 (:filter-center-block . org-export-filter-center-block-functions)
183 (:filter-clock . org-export-filter-clock-functions)
184 (:filter-code . org-export-filter-code-functions)
185 (:filter-comment . org-export-filter-comment-functions)
186 (:filter-comment-block . org-export-filter-comment-block-functions)
187 (:filter-diary-sexp . org-export-filter-diary-sexp-functions)
188 (:filter-drawer . org-export-filter-drawer-functions)
189 (:filter-dynamic-block . org-export-filter-dynamic-block-functions)
190 (:filter-entity . org-export-filter-entity-functions)
191 (:filter-example-block . org-export-filter-example-block-functions)
192 (:filter-export-block . org-export-filter-export-block-functions)
193 (:filter-export-snippet . org-export-filter-export-snippet-functions)
194 (:filter-final-output . org-export-filter-final-output-functions)
195 (:filter-fixed-width . org-export-filter-fixed-width-functions)
196 (:filter-footnote-definition . org-export-filter-footnote-definition-functions)
197 (:filter-footnote-reference . org-export-filter-footnote-reference-functions)
198 (:filter-headline . org-export-filter-headline-functions)
199 (:filter-horizontal-rule . org-export-filter-horizontal-rule-functions)
200 (:filter-inline-babel-call . org-export-filter-inline-babel-call-functions)
201 (:filter-inline-src-block . org-export-filter-inline-src-block-functions)
202 (:filter-inlinetask . org-export-filter-inlinetask-functions)
203 (:filter-italic . org-export-filter-italic-functions)
204 (:filter-item . org-export-filter-item-functions)
205 (:filter-keyword . org-export-filter-keyword-functions)
206 (:filter-latex-environment . org-export-filter-latex-environment-functions)
207 (:filter-latex-fragment . org-export-filter-latex-fragment-functions)
208 (:filter-line-break . org-export-filter-line-break-functions)
209 (:filter-link . org-export-filter-link-functions)
210 (:filter-node-property . org-export-filter-node-property-functions)
211 (:filter-options . org-export-filter-options-functions)
212 (:filter-paragraph . org-export-filter-paragraph-functions)
213 (:filter-parse-tree . org-export-filter-parse-tree-functions)
214 (:filter-plain-list . org-export-filter-plain-list-functions)
215 (:filter-plain-text . org-export-filter-plain-text-functions)
216 (:filter-planning . org-export-filter-planning-functions)
217 (:filter-property-drawer . org-export-filter-property-drawer-functions)
218 (:filter-quote-block . org-export-filter-quote-block-functions)
219 (:filter-radio-target . org-export-filter-radio-target-functions)
220 (:filter-section . org-export-filter-section-functions)
221 (:filter-special-block . org-export-filter-special-block-functions)
222 (:filter-src-block . org-export-filter-src-block-functions)
223 (:filter-statistics-cookie . org-export-filter-statistics-cookie-functions)
224 (:filter-strike-through . org-export-filter-strike-through-functions)
225 (:filter-subscript . org-export-filter-subscript-functions)
226 (:filter-superscript . org-export-filter-superscript-functions)
227 (:filter-table . org-export-filter-table-functions)
228 (:filter-table-cell . org-export-filter-table-cell-functions)
229 (:filter-table-row . org-export-filter-table-row-functions)
230 (:filter-target . org-export-filter-target-functions)
231 (:filter-timestamp . org-export-filter-timestamp-functions)
232 (:filter-underline . org-export-filter-underline-functions)
233 (:filter-verbatim . org-export-filter-verbatim-functions)
234 (:filter-verse-block . org-export-filter-verse-block-functions))
235 "Alist between filters properties and initial values.
237 The key of each association is a property name accessible through
238 the communication channel. Its value is a configurable global
239 variable defining initial filters.
241 This list is meant to install user specified filters. Back-end
242 developers may install their own filters using
243 `org-export-define-backend'. Filters defined there will always
244 be prepended to the current list, so they always get applied
245 first.")
247 (defconst org-export-default-inline-image-rule
248 `(("file" .
249 ,(format "\\.%s\\'"
250 (regexp-opt
251 '("png" "jpeg" "jpg" "gif" "tiff" "tif" "xbm"
252 "xpm" "pbm" "pgm" "ppm") t))))
253 "Default rule for link matching an inline image.
254 This rule applies to links with no description. By default, it
255 will be considered as an inline image if it targets a local file
256 whose extension is either \"png\", \"jpeg\", \"jpg\", \"gif\",
257 \"tiff\", \"tif\", \"xbm\", \"xpm\", \"pbm\", \"pgm\" or \"ppm\".
258 See `org-export-inline-image-p' for more information about
259 rules.")
261 (defconst org-export-ignored-local-variables
262 '(org-font-lock-keywords
263 org-element--cache org-element--cache-objects org-element--cache-sync-keys
264 org-element--cache-sync-requests org-element--cache-sync-timer)
265 "List of variables not copied through upon buffer duplication.
266 Export process takes place on a copy of the original buffer.
267 When this copy is created, all Org related local variables not in
268 this list are copied to the new buffer. Variables with an
269 unreadable value are also ignored.")
271 (defvar org-export-async-debug nil
272 "Non-nil means asynchronous export process should leave data behind.
274 This data is found in the appropriate \"*Org Export Process*\"
275 buffer, and in files prefixed with \"org-export-process\" and
276 located in `temporary-file-directory'.
278 When non-nil, it will also set `debug-on-error' to a non-nil
279 value in the external process.")
281 (defvar org-export-stack-contents nil
282 "Record asynchronously generated export results and processes.
283 This is an alist: its CAR is the source of the
284 result (destination file or buffer for a finished process,
285 original buffer for a running one) and its CDR is a list
286 containing the back-end used, as a symbol, and either a process
287 or the time at which it finished. It is used to build the menu
288 from `org-export-stack'.")
290 (defvar org-export--registered-backends nil
291 "List of backends currently available in the exporter.
292 This variable is set with `org-export-define-backend' and
293 `org-export-define-derived-backend' functions.")
295 (defvar org-export-dispatch-last-action nil
296 "Last command called from the dispatcher.
297 The value should be a list. Its CAR is the action, as a symbol,
298 and its CDR is a list of export options.")
300 (defvar org-export-dispatch-last-position (make-marker)
301 "The position where the last export command was created using the dispatcher.
302 This marker will be used with `C-u C-c C-e' to make sure export repetition
303 uses the same subtree if the previous command was restricted to a subtree.")
305 ;; For compatibility with Org < 8
306 (defvar org-export-current-backend nil
307 "Name, if any, of the back-end used during an export process.
309 Its value is a symbol such as `html', `latex', `ascii', or nil if
310 the back-end is anonymous (see `org-export-create-backend') or if
311 there is no export process in progress.
313 It can be used to teach Babel blocks how to act differently
314 according to the back-end used.")
318 ;;; User-configurable Variables
320 ;; Configuration for the masses.
322 ;; They should never be accessed directly, as their value is to be
323 ;; stored in a property list (cf. `org-export-options-alist').
324 ;; Back-ends will read their value from there instead.
326 (defgroup org-export nil
327 "Options for exporting Org mode files."
328 :tag "Org Export"
329 :group 'org)
331 (defgroup org-export-general nil
332 "General options for export engine."
333 :tag "Org Export General"
334 :group 'org-export)
336 (defcustom org-export-with-archived-trees 'headline
337 "Whether sub-trees with the ARCHIVE tag should be exported.
339 This can have three different values:
340 nil Do not export, pretend this tree is not present.
341 t Do export the entire tree.
342 `headline' Only export the headline, but skip the tree below it.
344 This option can also be set with the OPTIONS keyword,
345 e.g. \"arch:nil\"."
346 :group 'org-export-general
347 :type '(choice
348 (const :tag "Not at all" nil)
349 (const :tag "Headline only" headline)
350 (const :tag "Entirely" t)))
352 (defcustom org-export-with-author t
353 "Non-nil means insert author name into the exported file.
354 This option can also be set with the OPTIONS keyword,
355 e.g. \"author:nil\"."
356 :group 'org-export-general
357 :type 'boolean)
359 (defcustom org-export-with-clocks nil
360 "Non-nil means export CLOCK keywords.
361 This option can also be set with the OPTIONS keyword,
362 e.g. \"c:t\"."
363 :group 'org-export-general
364 :type 'boolean)
366 (defcustom org-export-with-creator 'comment
367 "Non-nil means the postamble should contain a creator sentence.
369 The sentence can be set in `org-export-creator-string' and
370 defaults to \"Generated by Org mode XX in Emacs XXX.\".
372 If the value is `comment' insert it as a comment."
373 :group 'org-export-general
374 :type '(choice
375 (const :tag "No creator sentence" nil)
376 (const :tag "Sentence as a comment" comment)
377 (const :tag "Insert the sentence" t)))
379 (defcustom org-export-with-date t
380 "Non-nil means insert date in the exported document.
381 This option can also be set with the OPTIONS keyword,
382 e.g. \"date:nil\"."
383 :group 'org-export-general
384 :type 'boolean)
386 (defcustom org-export-date-timestamp-format nil
387 "Time-stamp format string to use for DATE keyword.
389 The format string, when specified, only applies if date consists
390 in a single time-stamp. Otherwise its value will be ignored.
392 See `format-time-string' for details on how to build this
393 string."
394 :group 'org-export-general
395 :type '(choice
396 (string :tag "Time-stamp format string")
397 (const :tag "No format string" nil)))
399 (defcustom org-export-creator-string
400 (format "Emacs %s (Org mode %s)"
401 emacs-version
402 (if (fboundp 'org-version) (org-version) "unknown version"))
403 "Information about the creator of the document.
404 This option can also be set on with the CREATOR keyword."
405 :group 'org-export-general
406 :type '(string :tag "Creator string"))
408 (defcustom org-export-with-drawers '(not "LOGBOOK")
409 "Non-nil means export contents of standard drawers.
411 When t, all drawers are exported. This may also be a list of
412 drawer names to export, as strings. If that list starts with
413 `not', only drawers with such names will be ignored.
415 This variable doesn't apply to properties drawers. See
416 `org-export-with-properties' instead.
418 This option can also be set with the OPTIONS keyword,
419 e.g. \"d:nil\"."
420 :group 'org-export-general
421 :version "24.4"
422 :package-version '(Org . "8.0")
423 :type '(choice
424 (const :tag "All drawers" t)
425 (const :tag "None" nil)
426 (repeat :tag "Selected drawers"
427 (string :tag "Drawer name"))
428 (list :tag "Ignored drawers"
429 (const :format "" not)
430 (repeat :tag "Specify names of drawers to ignore during export"
431 :inline t
432 (string :tag "Drawer name")))))
434 (defcustom org-export-with-email nil
435 "Non-nil means insert author email into the exported file.
436 This option can also be set with the OPTIONS keyword,
437 e.g. \"email:t\"."
438 :group 'org-export-general
439 :type 'boolean)
441 (defcustom org-export-with-emphasize t
442 "Non-nil means interpret *word*, /word/, _word_ and +word+.
444 If the export target supports emphasizing text, the word will be
445 typeset in bold, italic, with an underline or strike-through,
446 respectively.
448 This option can also be set with the OPTIONS keyword,
449 e.g. \"*:nil\"."
450 :group 'org-export-general
451 :type 'boolean)
453 (defcustom org-export-exclude-tags '("noexport")
454 "Tags that exclude a tree from export.
456 All trees carrying any of these tags will be excluded from
457 export. This is without condition, so even subtrees inside that
458 carry one of the `org-export-select-tags' will be removed.
460 This option can also be set with the EXCLUDE_TAGS keyword."
461 :group 'org-export-general
462 :type '(repeat (string :tag "Tag")))
464 (defcustom org-export-with-fixed-width t
465 "Non-nil means export lines starting with \":\".
466 This option can also be set with the OPTIONS keyword,
467 e.g. \"::nil\"."
468 :group 'org-export-general
469 :version "24.4"
470 :package-version '(Org . "8.0")
471 :type 'boolean)
473 (defcustom org-export-with-footnotes t
474 "Non-nil means Org footnotes should be exported.
475 This option can also be set with the OPTIONS keyword,
476 e.g. \"f:nil\"."
477 :group 'org-export-general
478 :type 'boolean)
480 (defcustom org-export-with-latex t
481 "Non-nil means process LaTeX environments and fragments.
483 This option can also be set with the OPTIONS line,
484 e.g. \"tex:verbatim\". Allowed values are:
486 nil Ignore math snippets.
487 `verbatim' Keep everything in verbatim.
488 t Allow export of math snippets."
489 :group 'org-export-general
490 :version "24.4"
491 :package-version '(Org . "8.0")
492 :type '(choice
493 (const :tag "Do not process math in any way" nil)
494 (const :tag "Interpret math snippets" t)
495 (const :tag "Leave math verbatim" verbatim)))
497 (defcustom org-export-headline-levels 3
498 "The last level which is still exported as a headline.
500 Inferior levels will usually produce itemize or enumerate lists
501 when exported, but back-end behaviour may differ.
503 This option can also be set with the OPTIONS keyword,
504 e.g. \"H:2\"."
505 :group 'org-export-general
506 :type 'integer)
508 (defcustom org-export-default-language "en"
509 "The default language for export and clocktable translations, as a string.
510 This may have an association in
511 `org-clock-clocktable-language-setup',
512 `org-export-smart-quotes-alist' and `org-export-dictionary'.
513 This option can also be set with the LANGUAGE keyword."
514 :group 'org-export-general
515 :type '(string :tag "Language"))
517 (defcustom org-export-preserve-breaks nil
518 "Non-nil means preserve all line breaks when exporting.
519 This option can also be set with the OPTIONS keyword,
520 e.g. \"\\n:t\"."
521 :group 'org-export-general
522 :type 'boolean)
524 (defcustom org-export-with-entities t
525 "Non-nil means interpret entities when exporting.
527 For example, HTML export converts \\alpha to &alpha; and \\AA to
528 &Aring;.
530 For a list of supported names, see the constant `org-entities'
531 and the user option `org-entities-user'.
533 This option can also be set with the OPTIONS keyword,
534 e.g. \"e:nil\"."
535 :group 'org-export-general
536 :type 'boolean)
538 (defcustom org-export-with-inlinetasks t
539 "Non-nil means inlinetasks should be exported.
540 This option can also be set with the OPTIONS keyword,
541 e.g. \"inline:nil\"."
542 :group 'org-export-general
543 :version "24.4"
544 :package-version '(Org . "8.0")
545 :type 'boolean)
547 (defcustom org-export-with-planning nil
548 "Non-nil means include planning info in export.
550 Planning info is the line containing either SCHEDULED:,
551 DEADLINE:, CLOSED: time-stamps, or a combination of them.
553 This option can also be set with the OPTIONS keyword,
554 e.g. \"p:t\"."
555 :group 'org-export-general
556 :version "24.4"
557 :package-version '(Org . "8.0")
558 :type 'boolean)
560 (defcustom org-export-with-priority nil
561 "Non-nil means include priority cookies in export.
562 This option can also be set with the OPTIONS keyword,
563 e.g. \"pri:t\"."
564 :group 'org-export-general
565 :type 'boolean)
567 (defcustom org-export-with-properties nil
568 "Non-nil means export contents of properties drawers.
570 When t, all properties are exported. This may also be a list of
571 properties to export, as strings.
573 This option can also be set with the OPTIONS keyword,
574 e.g. \"prop:t\"."
575 :group 'org-export-general
576 :version "24.4"
577 :package-version '(Org . "8.3")
578 :type '(choice
579 (const :tag "All properties" t)
580 (const :tag "None" nil)
581 (repeat :tag "Selected properties"
582 (string :tag "Property name"))))
584 (defcustom org-export-with-section-numbers t
585 "Non-nil means add section numbers to headlines when exporting.
587 When set to an integer n, numbering will only happen for
588 headlines whose relative level is higher or equal to n.
590 This option can also be set with the OPTIONS keyword,
591 e.g. \"num:t\"."
592 :group 'org-export-general
593 :type 'boolean)
595 (defcustom org-export-select-tags '("export")
596 "Tags that select a tree for export.
598 If any such tag is found in a buffer, all trees that do not carry
599 one of these tags will be ignored during export. Inside trees
600 that are selected like this, you can still deselect a subtree by
601 tagging it with one of the `org-export-exclude-tags'.
603 This option can also be set with the SELECT_TAGS keyword."
604 :group 'org-export-general
605 :type '(repeat (string :tag "Tag")))
607 (defcustom org-export-with-smart-quotes nil
608 "Non-nil means activate smart quotes during export.
609 This option can also be set with the OPTIONS keyword,
610 e.g., \"':t\".
612 When setting this to non-nil, you need to take care of
613 using the correct Babel package when exporting to LaTeX.
614 E.g., you can load Babel for french like this:
616 #+LATEX_HEADER: \\usepackage[french]{babel}"
617 :group 'org-export-general
618 :version "24.4"
619 :package-version '(Org . "8.0")
620 :type 'boolean)
622 (defcustom org-export-with-special-strings t
623 "Non-nil means interpret \"\\-\", \"--\" and \"---\" for export.
625 When this option is turned on, these strings will be exported as:
627 Org HTML LaTeX UTF-8
628 -----+----------+--------+-------
629 \\- &shy; \\-
630 -- &ndash; -- –
631 --- &mdash; --- —
632 ... &hellip; \\ldots …
634 This option can also be set with the OPTIONS keyword,
635 e.g. \"-:nil\"."
636 :group 'org-export-general
637 :type 'boolean)
639 (defcustom org-export-with-statistics-cookies t
640 "Non-nil means include statistics cookies in export.
641 This option can also be set with the OPTIONS keyword,
642 e.g. \"stat:nil\""
643 :group 'org-export-general
644 :version "24.4"
645 :package-version '(Org . "8.0")
646 :type 'boolean)
648 (defcustom org-export-with-sub-superscripts t
649 "Non-nil means interpret \"_\" and \"^\" for export.
651 If you want to control how Org displays those characters, see
652 `org-use-sub-superscripts'. `org-export-with-sub-superscripts'
653 used to be an alias for `org-use-sub-superscripts' in Org <8.0,
654 it is not anymore.
656 When this option is turned on, you can use TeX-like syntax for
657 sub- and superscripts and see them exported correctly.
659 You can also set the option with #+OPTIONS: ^:t
661 Several characters after \"_\" or \"^\" will be considered as a
662 single item - so grouping with {} is normally not needed. For
663 example, the following things will be parsed as single sub- or
664 superscripts:
666 10^24 or 10^tau several digits will be considered 1 item.
667 10^-12 or 10^-tau a leading sign with digits or a word
668 x^2-y^3 will be read as x^2 - y^3, because items are
669 terminated by almost any nonword/nondigit char.
670 x_{i^2} or x^(2-i) braces or parenthesis do grouping.
672 Still, ambiguity is possible. So when in doubt, use {} to enclose
673 the sub/superscript. If you set this variable to the symbol `{}',
674 the braces are *required* in order to trigger interpretations as
675 sub/superscript. This can be helpful in documents that need \"_\"
676 frequently in plain text."
677 :group 'org-export-general
678 :version "24.4"
679 :package-version '(Org . "8.0")
680 :type '(choice
681 (const :tag "Interpret them" t)
682 (const :tag "Curly brackets only" {})
683 (const :tag "Do not interpret them" nil)))
685 (defcustom org-export-with-toc t
686 "Non-nil means create a table of contents in exported files.
688 The TOC contains headlines with levels up
689 to`org-export-headline-levels'. When an integer, include levels
690 up to N in the toc, this may then be different from
691 `org-export-headline-levels', but it will not be allowed to be
692 larger than the number of headline levels. When nil, no table of
693 contents is made.
695 This option can also be set with the OPTIONS keyword,
696 e.g. \"toc:nil\" or \"toc:3\"."
697 :group 'org-export-general
698 :type '(choice
699 (const :tag "No Table of Contents" nil)
700 (const :tag "Full Table of Contents" t)
701 (integer :tag "TOC to level")))
703 (defcustom org-export-with-tables t
704 "Non-nil means export tables.
705 This option can also be set with the OPTIONS keyword,
706 e.g. \"|:nil\"."
707 :group 'org-export-general
708 :version "24.4"
709 :package-version '(Org . "8.0")
710 :type 'boolean)
712 (defcustom org-export-with-tags t
713 "If nil, do not export tags, just remove them from headlines.
715 If this is the symbol `not-in-toc', tags will be removed from
716 table of contents entries, but still be shown in the headlines of
717 the document.
719 This option can also be set with the OPTIONS keyword,
720 e.g. \"tags:nil\"."
721 :group 'org-export-general
722 :type '(choice
723 (const :tag "Off" nil)
724 (const :tag "Not in TOC" not-in-toc)
725 (const :tag "On" t)))
727 (defcustom org-export-with-tasks t
728 "Non-nil means include TODO items for export.
730 This may have the following values:
731 t include tasks independent of state.
732 `todo' include only tasks that are not yet done.
733 `done' include only tasks that are already done.
734 nil ignore all tasks.
735 list of keywords include tasks with these keywords.
737 This option can also be set with the OPTIONS keyword,
738 e.g. \"tasks:nil\"."
739 :group 'org-export-general
740 :type '(choice
741 (const :tag "All tasks" t)
742 (const :tag "No tasks" nil)
743 (const :tag "Not-done tasks" todo)
744 (const :tag "Only done tasks" done)
745 (repeat :tag "Specific TODO keywords"
746 (string :tag "Keyword"))))
748 (defcustom org-export-time-stamp-file t
749 "Non-nil means insert a time stamp into the exported file.
750 The time stamp shows when the file was created. This option can
751 also be set with the OPTIONS keyword, e.g. \"timestamp:nil\"."
752 :group 'org-export-general
753 :type 'boolean)
755 (defcustom org-export-with-timestamps t
756 "Non nil means allow timestamps in export.
758 It can be set to any of the following values:
759 t export all timestamps.
760 `active' export active timestamps only.
761 `inactive' export inactive timestamps only.
762 nil do not export timestamps
764 This only applies to timestamps isolated in a paragraph
765 containing only timestamps. Other timestamps are always
766 exported.
768 This option can also be set with the OPTIONS keyword, e.g.
769 \"<:nil\"."
770 :group 'org-export-general
771 :type '(choice
772 (const :tag "All timestamps" t)
773 (const :tag "Only active timestamps" active)
774 (const :tag "Only inactive timestamps" inactive)
775 (const :tag "No timestamp" nil)))
777 (defcustom org-export-with-todo-keywords t
778 "Non-nil means include TODO keywords in export.
779 When nil, remove all these keywords from the export. This option
780 can also be set with the OPTIONS keyword, e.g. \"todo:nil\"."
781 :group 'org-export-general
782 :type 'boolean)
784 (defcustom org-export-allow-bind-keywords nil
785 "Non-nil means BIND keywords can define local variable values.
786 This is a potential security risk, which is why the default value
787 is nil. You can also allow them through local buffer variables."
788 :group 'org-export-general
789 :version "24.4"
790 :package-version '(Org . "8.0")
791 :type 'boolean)
793 (defcustom org-export-snippet-translation-alist nil
794 "Alist between export snippets back-ends and exporter back-ends.
796 This variable allows to provide shortcuts for export snippets.
798 For example, with a value of '\(\(\"h\" . \"html\"\)\), the
799 HTML back-end will recognize the contents of \"@@h:<b>@@\" as
800 HTML code while every other back-end will ignore it."
801 :group 'org-export-general
802 :version "24.4"
803 :package-version '(Org . "8.0")
804 :type '(repeat
805 (cons (string :tag "Shortcut")
806 (string :tag "Back-end"))))
808 (defcustom org-export-coding-system nil
809 "Coding system for the exported file."
810 :group 'org-export-general
811 :version "24.4"
812 :package-version '(Org . "8.0")
813 :type 'coding-system)
815 (defcustom org-export-copy-to-kill-ring nil
816 "Non-nil means pushing export output to the kill ring.
817 This variable is ignored during asynchronous export."
818 :group 'org-export-general
819 :version "25.1"
820 :package-version '(Org . "8.3")
821 :type '(choice
822 (const :tag "Always" t)
823 (const :tag "When export is done interactively" if-interactive)
824 (const :tag "Never" nil)))
826 (defcustom org-export-initial-scope 'buffer
827 "The initial scope when exporting with `org-export-dispatch'.
828 This variable can be either set to `buffer' or `subtree'."
829 :group 'org-export-general
830 :type '(choice
831 (const :tag "Export current buffer" buffer)
832 (const :tag "Export current subtree" subtree)))
834 (defcustom org-export-show-temporary-export-buffer t
835 "Non-nil means show buffer after exporting to temp buffer.
836 When Org exports to a file, the buffer visiting that file is ever
837 shown, but remains buried. However, when exporting to
838 a temporary buffer, that buffer is popped up in a second window.
839 When this variable is nil, the buffer remains buried also in
840 these cases."
841 :group 'org-export-general
842 :type 'boolean)
844 (defcustom org-export-in-background nil
845 "Non-nil means export and publishing commands will run in background.
846 Results from an asynchronous export are never displayed
847 automatically. But you can retrieve them with \\[org-export-stack]."
848 :group 'org-export-general
849 :version "24.4"
850 :package-version '(Org . "8.0")
851 :type 'boolean)
853 (defcustom org-export-async-init-file nil
854 "File used to initialize external export process.
856 Value must be either nil or an absolute file name. When nil, the
857 external process is launched like a regular Emacs session,
858 loading user's initialization file and any site specific
859 configuration. If a file is provided, it, and only it, is loaded
860 at start-up.
862 Therefore, using a specific configuration makes the process to
863 load faster and the export more portable."
864 :group 'org-export-general
865 :version "24.4"
866 :package-version '(Org . "8.0")
867 :type '(choice
868 (const :tag "Regular startup" nil)
869 (file :tag "Specific start-up file" :must-match t)))
871 (defcustom org-export-dispatch-use-expert-ui nil
872 "Non-nil means using a non-intrusive `org-export-dispatch'.
873 In that case, no help buffer is displayed. Though, an indicator
874 for current export scope is added to the prompt (\"b\" when
875 output is restricted to body only, \"s\" when it is restricted to
876 the current subtree, \"v\" when only visible elements are
877 considered for export, \"f\" when publishing functions should be
878 passed the FORCE argument and \"a\" when the export should be
879 asynchronous). Also, \[?] allows to switch back to standard
880 mode."
881 :group 'org-export-general
882 :version "24.4"
883 :package-version '(Org . "8.0")
884 :type 'boolean)
888 ;;; Defining Back-ends
890 ;; An export back-end is a structure with `org-export-backend' type
891 ;; and `name', `parent', `transcoders', `options', `filters', `blocks'
892 ;; and `menu' slots.
894 ;; At the lowest level, a back-end is created with
895 ;; `org-export-create-backend' function.
897 ;; A named back-end can be registered with
898 ;; `org-export-register-backend' function. A registered back-end can
899 ;; later be referred to by its name, with `org-export-get-backend'
900 ;; function. Also, such a back-end can become the parent of a derived
901 ;; back-end from which slot values will be inherited by default.
902 ;; `org-export-derived-backend-p' can check if a given back-end is
903 ;; derived from a list of back-end names.
905 ;; `org-export-get-all-transcoders', `org-export-get-all-options' and
906 ;; `org-export-get-all-filters' return the full alist of transcoders,
907 ;; options and filters, including those inherited from ancestors.
909 ;; At a higher level, `org-export-define-backend' is the standard way
910 ;; to define an export back-end. If the new back-end is similar to
911 ;; a registered back-end, `org-export-define-derived-backend' may be
912 ;; used instead.
914 ;; Eventually `org-export-barf-if-invalid-backend' returns an error
915 ;; when a given back-end hasn't been registered yet.
917 (defstruct (org-export-backend (:constructor org-export-create-backend)
918 (:copier nil))
919 name parent transcoders options filters blocks menu)
921 (defun org-export-get-backend (name)
922 "Return export back-end named after NAME.
923 NAME is a symbol. Return nil if no such back-end is found."
924 (catch 'found
925 (dolist (b org-export--registered-backends)
926 (when (eq (org-export-backend-name b) name)
927 (throw 'found b)))))
929 (defun org-export-register-backend (backend)
930 "Register BACKEND as a known export back-end.
931 BACKEND is a structure with `org-export-backend' type."
932 ;; Refuse to register an unnamed back-end.
933 (unless (org-export-backend-name backend)
934 (error "Cannot register a unnamed export back-end"))
935 ;; Refuse to register a back-end with an unknown parent.
936 (let ((parent (org-export-backend-parent backend)))
937 (when (and parent (not (org-export-get-backend parent)))
938 (error "Cannot use unknown \"%s\" back-end as a parent" parent)))
939 ;; Register dedicated export blocks in the parser.
940 (dolist (name (org-export-backend-blocks backend))
941 (add-to-list 'org-element-block-name-alist
942 (cons name 'org-element-export-block-parser)))
943 ;; If a back-end with the same name as BACKEND is already
944 ;; registered, replace it with BACKEND. Otherwise, simply add
945 ;; BACKEND to the list of registered back-ends.
946 (let ((old (org-export-get-backend (org-export-backend-name backend))))
947 (if old (setcar (memq old org-export--registered-backends) backend)
948 (push backend org-export--registered-backends))))
950 (defun org-export-barf-if-invalid-backend (backend)
951 "Signal an error if BACKEND isn't defined."
952 (unless (org-export-backend-p backend)
953 (error "Unknown \"%s\" back-end: Aborting export" backend)))
955 (defun org-export-derived-backend-p (backend &rest backends)
956 "Non-nil if BACKEND is derived from one of BACKENDS.
957 BACKEND is an export back-end, as returned by, e.g.,
958 `org-export-create-backend', or a symbol referring to
959 a registered back-end. BACKENDS is constituted of symbols."
960 (when (symbolp backend) (setq backend (org-export-get-backend backend)))
961 (when backend
962 (catch 'exit
963 (while (org-export-backend-parent backend)
964 (when (memq (org-export-backend-name backend) backends)
965 (throw 'exit t))
966 (setq backend
967 (org-export-get-backend (org-export-backend-parent backend))))
968 (memq (org-export-backend-name backend) backends))))
970 (defun org-export-get-all-transcoders (backend)
971 "Return full translation table for BACKEND.
973 BACKEND is an export back-end, as return by, e.g,,
974 `org-export-create-backend'. Return value is an alist where
975 keys are element or object types, as symbols, and values are
976 transcoders.
978 Unlike to `org-export-backend-transcoders', this function
979 also returns transcoders inherited from parent back-ends,
980 if any."
981 (when (symbolp backend) (setq backend (org-export-get-backend backend)))
982 (when backend
983 (let ((transcoders (org-export-backend-transcoders backend))
984 parent)
985 (while (setq parent (org-export-backend-parent backend))
986 (setq backend (org-export-get-backend parent))
987 (setq transcoders
988 (append transcoders (org-export-backend-transcoders backend))))
989 transcoders)))
991 (defun org-export-get-all-options (backend)
992 "Return export options for BACKEND.
994 BACKEND is an export back-end, as return by, e.g,,
995 `org-export-create-backend'. See `org-export-options-alist'
996 for the shape of the return value.
998 Unlike to `org-export-backend-options', this function also
999 returns options inherited from parent back-ends, if any."
1000 (when (symbolp backend) (setq backend (org-export-get-backend backend)))
1001 (when backend
1002 (let ((options (org-export-backend-options backend))
1003 parent)
1004 (while (setq parent (org-export-backend-parent backend))
1005 (setq backend (org-export-get-backend parent))
1006 (setq options (append options (org-export-backend-options backend))))
1007 options)))
1009 (defun org-export-get-all-filters (backend)
1010 "Return complete list of filters for BACKEND.
1012 BACKEND is an export back-end, as return by, e.g,,
1013 `org-export-create-backend'. Return value is an alist where
1014 keys are symbols and values lists of functions.
1016 Unlike to `org-export-backend-filters', this function also
1017 returns filters inherited from parent back-ends, if any."
1018 (when (symbolp backend) (setq backend (org-export-get-backend backend)))
1019 (when backend
1020 (let ((filters (org-export-backend-filters backend))
1021 parent)
1022 (while (setq parent (org-export-backend-parent backend))
1023 (setq backend (org-export-get-backend parent))
1024 (setq filters (append filters (org-export-backend-filters backend))))
1025 filters)))
1027 (defun org-export-define-backend (backend transcoders &rest body)
1028 "Define a new back-end BACKEND.
1030 TRANSCODERS is an alist between object or element types and
1031 functions handling them.
1033 These functions should return a string without any trailing
1034 space, or nil. They must accept three arguments: the object or
1035 element itself, its contents or nil when it isn't recursive and
1036 the property list used as a communication channel.
1038 Contents, when not nil, are stripped from any global indentation
1039 \(although the relative one is preserved). They also always end
1040 with a single newline character.
1042 If, for a given type, no function is found, that element or
1043 object type will simply be ignored, along with any blank line or
1044 white space at its end. The same will happen if the function
1045 returns the nil value. If that function returns the empty
1046 string, the type will be ignored, but the blank lines or white
1047 spaces will be kept.
1049 In addition to element and object types, one function can be
1050 associated to the `template' (or `inner-template') symbol and
1051 another one to the `plain-text' symbol.
1053 The former returns the final transcoded string, and can be used
1054 to add a preamble and a postamble to document's body. It must
1055 accept two arguments: the transcoded string and the property list
1056 containing export options. A function associated to `template'
1057 will not be applied if export has option \"body-only\".
1058 A function associated to `inner-template' is always applied.
1060 The latter, when defined, is to be called on every text not
1061 recognized as an element or an object. It must accept two
1062 arguments: the text string and the information channel. It is an
1063 appropriate place to protect special chars relative to the
1064 back-end.
1066 BODY can start with pre-defined keyword arguments. The following
1067 keywords are understood:
1069 :export-block
1071 String, or list of strings, representing block names that
1072 will not be parsed. This is used to specify blocks that will
1073 contain raw code specific to the back-end. These blocks
1074 still have to be handled by the relative `export-block' type
1075 translator.
1077 :filters-alist
1079 Alist between filters and function, or list of functions,
1080 specific to the back-end. See `org-export-filters-alist' for
1081 a list of all allowed filters. Filters defined here
1082 shouldn't make a back-end test, as it may prevent back-ends
1083 derived from this one to behave properly.
1085 :menu-entry
1087 Menu entry for the export dispatcher. It should be a list
1088 like:
1090 '(KEY DESCRIPTION-OR-ORDINAL ACTION-OR-MENU)
1092 where :
1094 KEY is a free character selecting the back-end.
1096 DESCRIPTION-OR-ORDINAL is either a string or a number.
1098 If it is a string, is will be used to name the back-end in
1099 its menu entry. If it is a number, the following menu will
1100 be displayed as a sub-menu of the back-end with the same
1101 KEY. Also, the number will be used to determine in which
1102 order such sub-menus will appear (lowest first).
1104 ACTION-OR-MENU is either a function or an alist.
1106 If it is an action, it will be called with four
1107 arguments (booleans): ASYNC, SUBTREEP, VISIBLE-ONLY and
1108 BODY-ONLY. See `org-export-as' for further explanations on
1109 some of them.
1111 If it is an alist, associations should follow the
1112 pattern:
1114 '(KEY DESCRIPTION ACTION)
1116 where KEY, DESCRIPTION and ACTION are described above.
1118 Valid values include:
1120 '(?m \"My Special Back-end\" my-special-export-function)
1124 '(?l \"Export to LaTeX\"
1125 \(?p \"As PDF file\" org-latex-export-to-pdf)
1126 \(?o \"As PDF file and open\"
1127 \(lambda (a s v b)
1128 \(if a (org-latex-export-to-pdf t s v b)
1129 \(org-open-file
1130 \(org-latex-export-to-pdf nil s v b)))))))
1132 or the following, which will be added to the previous
1133 sub-menu,
1135 '(?l 1
1136 \((?B \"As TEX buffer (Beamer)\" org-beamer-export-as-latex)
1137 \(?P \"As PDF file (Beamer)\" org-beamer-export-to-pdf)))
1139 :options-alist
1141 Alist between back-end specific properties introduced in
1142 communication channel and how their value are acquired. See
1143 `org-export-options-alist' for more information about
1144 structure of the values."
1145 (declare (indent 1))
1146 (let (blocks filters menu-entry options contents)
1147 (while (keywordp (car body))
1148 (let ((keyword (pop body)))
1149 (case keyword
1150 (:export-block (let ((names (pop body)))
1151 (setq blocks (if (consp names) (mapcar 'upcase names)
1152 (list (upcase names))))))
1153 (:filters-alist (setq filters (pop body)))
1154 (:menu-entry (setq menu-entry (pop body)))
1155 (:options-alist (setq options (pop body)))
1156 (t (error "Unknown keyword: %s" keyword)))))
1157 (org-export-register-backend
1158 (org-export-create-backend :name backend
1159 :transcoders transcoders
1160 :options options
1161 :filters filters
1162 :blocks blocks
1163 :menu menu-entry))))
1165 (defun org-export-define-derived-backend (child parent &rest body)
1166 "Create a new back-end as a variant of an existing one.
1168 CHILD is the name of the derived back-end. PARENT is the name of
1169 the parent back-end.
1171 BODY can start with pre-defined keyword arguments. The following
1172 keywords are understood:
1174 :export-block
1176 String, or list of strings, representing block names that
1177 will not be parsed. This is used to specify blocks that will
1178 contain raw code specific to the back-end. These blocks
1179 still have to be handled by the relative `export-block' type
1180 translator.
1182 :filters-alist
1184 Alist of filters that will overwrite or complete filters
1185 defined in PARENT back-end. See `org-export-filters-alist'
1186 for a list of allowed filters.
1188 :menu-entry
1190 Menu entry for the export dispatcher. See
1191 `org-export-define-backend' for more information about the
1192 expected value.
1194 :options-alist
1196 Alist of back-end specific properties that will overwrite or
1197 complete those defined in PARENT back-end. Refer to
1198 `org-export-options-alist' for more information about
1199 structure of the values.
1201 :translate-alist
1203 Alist of element and object types and transcoders that will
1204 overwrite or complete transcode table from PARENT back-end.
1205 Refer to `org-export-define-backend' for detailed information
1206 about transcoders.
1208 As an example, here is how one could define \"my-latex\" back-end
1209 as a variant of `latex' back-end with a custom template function:
1211 \(org-export-define-derived-backend 'my-latex 'latex
1212 :translate-alist '((template . my-latex-template-fun)))
1214 The back-end could then be called with, for example:
1216 \(org-export-to-buffer 'my-latex \"*Test my-latex*\")"
1217 (declare (indent 2))
1218 (let (blocks filters menu-entry options transcoders contents)
1219 (while (keywordp (car body))
1220 (let ((keyword (pop body)))
1221 (case keyword
1222 (:export-block (let ((names (pop body)))
1223 (setq blocks (if (consp names) (mapcar 'upcase names)
1224 (list (upcase names))))))
1225 (:filters-alist (setq filters (pop body)))
1226 (:menu-entry (setq menu-entry (pop body)))
1227 (:options-alist (setq options (pop body)))
1228 (:translate-alist (setq transcoders (pop body)))
1229 (t (error "Unknown keyword: %s" keyword)))))
1230 (org-export-register-backend
1231 (org-export-create-backend :name child
1232 :parent parent
1233 :transcoders transcoders
1234 :options options
1235 :filters filters
1236 :blocks blocks
1237 :menu menu-entry))))
1241 ;;; The Communication Channel
1243 ;; During export process, every function has access to a number of
1244 ;; properties. They are of two types:
1246 ;; 1. Environment options are collected once at the very beginning of
1247 ;; the process, out of the original buffer and configuration.
1248 ;; Collecting them is handled by `org-export-get-environment'
1249 ;; function.
1251 ;; Most environment options are defined through the
1252 ;; `org-export-options-alist' variable.
1254 ;; 2. Tree properties are extracted directly from the parsed tree,
1255 ;; just before export, by `org-export-collect-tree-properties'.
1257 ;; Here is the full list of properties available during transcode
1258 ;; process, with their category and their value type.
1260 ;; + `:author' :: Author's name.
1261 ;; - category :: option
1262 ;; - type :: string
1264 ;; + `:back-end' :: Current back-end used for transcoding.
1265 ;; - category :: tree
1266 ;; - type :: structure
1268 ;; + `:creator' :: String to write as creation information.
1269 ;; - category :: option
1270 ;; - type :: string
1272 ;; + `:date' :: String to use as date.
1273 ;; - category :: option
1274 ;; - type :: string
1276 ;; + `:description' :: Description text for the current data.
1277 ;; - category :: option
1278 ;; - type :: string
1280 ;; + `:email' :: Author's email.
1281 ;; - category :: option
1282 ;; - type :: string
1284 ;; + `:exclude-tags' :: Tags for exclusion of subtrees from export
1285 ;; process.
1286 ;; - category :: option
1287 ;; - type :: list of strings
1289 ;; + `:export-options' :: List of export options available for current
1290 ;; process.
1291 ;; - category :: none
1292 ;; - type :: list of symbols, among `subtree', `body-only' and
1293 ;; `visible-only'.
1295 ;; + `:exported-data' :: Hash table used for memoizing
1296 ;; `org-export-data'.
1297 ;; - category :: tree
1298 ;; - type :: hash table
1300 ;; + `:filetags' :: List of global tags for buffer. Used by
1301 ;; `org-export-get-tags' to get tags with inheritance.
1302 ;; - category :: option
1303 ;; - type :: list of strings
1305 ;; + `:footnote-definition-alist' :: Alist between footnote labels and
1306 ;; their definition, as parsed data. Only non-inlined footnotes
1307 ;; are represented in this alist. Also, every definition isn't
1308 ;; guaranteed to be referenced in the parse tree. The purpose of
1309 ;; this property is to preserve definitions from oblivion
1310 ;; (i.e. when the parse tree comes from a part of the original
1311 ;; buffer), it isn't meant for direct use in a back-end. To
1312 ;; retrieve a definition relative to a reference, use
1313 ;; `org-export-get-footnote-definition' instead.
1314 ;; - category :: option
1315 ;; - type :: alist (STRING . LIST)
1317 ;; + `:headline-levels' :: Maximum level being exported as an
1318 ;; headline. Comparison is done with the relative level of
1319 ;; headlines in the parse tree, not necessarily with their
1320 ;; actual level.
1321 ;; - category :: option
1322 ;; - type :: integer
1324 ;; + `:headline-offset' :: Difference between relative and real level
1325 ;; of headlines in the parse tree. For example, a value of -1
1326 ;; means a level 2 headline should be considered as level
1327 ;; 1 (cf. `org-export-get-relative-level').
1328 ;; - category :: tree
1329 ;; - type :: integer
1331 ;; + `:headline-numbering' :: Alist between headlines and their
1332 ;; numbering, as a list of numbers
1333 ;; (cf. `org-export-get-headline-number').
1334 ;; - category :: tree
1335 ;; - type :: alist (INTEGER . LIST)
1337 ;; + `:id-alist' :: Alist between ID strings and destination file's
1338 ;; path, relative to current directory. It is used by
1339 ;; `org-export-resolve-id-link' to resolve ID links targeting an
1340 ;; external file.
1341 ;; - category :: option
1342 ;; - type :: alist (STRING . STRING)
1344 ;; + `:ignore-list' :: List of elements and objects that should be
1345 ;; ignored during export.
1346 ;; - category :: tree
1347 ;; - type :: list of elements and objects
1349 ;; + `:input-file' :: Full path to input file, if any.
1350 ;; - category :: option
1351 ;; - type :: string or nil
1353 ;; + `:keywords' :: List of keywords attached to data.
1354 ;; - category :: option
1355 ;; - type :: string
1357 ;; + `:language' :: Default language used for translations.
1358 ;; - category :: option
1359 ;; - type :: string
1361 ;; + `:output-file' :: Full path to output file, if any.
1362 ;; - category :: option
1363 ;; - type :: string or nil
1365 ;; + `:parse-tree' :: Whole parse tree, available at any time during
1366 ;; transcoding.
1367 ;; - category :: option
1368 ;; - type :: list (as returned by `org-element-parse-buffer')
1370 ;; + `:preserve-breaks' :: Non-nil means transcoding should preserve
1371 ;; all line breaks.
1372 ;; - category :: option
1373 ;; - type :: symbol (nil, t)
1375 ;; + `:section-numbers' :: Non-nil means transcoding should add
1376 ;; section numbers to headlines.
1377 ;; - category :: option
1378 ;; - type :: symbol (nil, t)
1380 ;; + `:select-tags' :: List of tags enforcing inclusion of sub-trees
1381 ;; in transcoding. When such a tag is present, subtrees without
1382 ;; it are de facto excluded from the process. See
1383 ;; `use-select-tags'.
1384 ;; - category :: option
1385 ;; - type :: list of strings
1387 ;; + `:time-stamp-file' :: Non-nil means transcoding should insert
1388 ;; a time stamp in the output.
1389 ;; - category :: option
1390 ;; - type :: symbol (nil, t)
1392 ;; + `:translate-alist' :: Alist between element and object types and
1393 ;; transcoding functions relative to the current back-end.
1394 ;; Special keys `inner-template', `template' and `plain-text' are
1395 ;; also possible.
1396 ;; - category :: option
1397 ;; - type :: alist (SYMBOL . FUNCTION)
1399 ;; + `:with-archived-trees' :: Non-nil when archived subtrees should
1400 ;; also be transcoded. If it is set to the `headline' symbol,
1401 ;; only the archived headline's name is retained.
1402 ;; - category :: option
1403 ;; - type :: symbol (nil, t, `headline')
1405 ;; + `:with-author' :: Non-nil means author's name should be included
1406 ;; in the output.
1407 ;; - category :: option
1408 ;; - type :: symbol (nil, t)
1410 ;; + `:with-clocks' :: Non-nil means clock keywords should be exported.
1411 ;; - category :: option
1412 ;; - type :: symbol (nil, t)
1414 ;; + `:with-creator' :: Non-nil means a creation sentence should be
1415 ;; inserted at the end of the transcoded string. If the value
1416 ;; is `comment', it should be commented.
1417 ;; - category :: option
1418 ;; - type :: symbol (`comment', nil, t)
1420 ;; + `:with-date' :: Non-nil means output should contain a date.
1421 ;; - category :: option
1422 ;; - type :. symbol (nil, t)
1424 ;; + `:with-drawers' :: Non-nil means drawers should be exported. If
1425 ;; its value is a list of names, only drawers with such names
1426 ;; will be transcoded. If that list starts with `not', drawer
1427 ;; with these names will be skipped.
1428 ;; - category :: option
1429 ;; - type :: symbol (nil, t) or list of strings
1431 ;; + `:with-email' :: Non-nil means output should contain author's
1432 ;; email.
1433 ;; - category :: option
1434 ;; - type :: symbol (nil, t)
1436 ;; + `:with-emphasize' :: Non-nil means emphasized text should be
1437 ;; interpreted.
1438 ;; - category :: option
1439 ;; - type :: symbol (nil, t)
1441 ;; + `:with-fixed-width' :: Non-nil if transcoder should export
1442 ;; strings starting with a colon as a fixed-with (verbatim) area.
1443 ;; - category :: option
1444 ;; - type :: symbol (nil, t)
1446 ;; + `:with-footnotes' :: Non-nil if transcoder should interpret
1447 ;; footnotes.
1448 ;; - category :: option
1449 ;; - type :: symbol (nil, t)
1451 ;; + `:with-latex' :: Non-nil means `latex-environment' elements and
1452 ;; `latex-fragment' objects should appear in export output. When
1453 ;; this property is set to `verbatim', they will be left as-is.
1454 ;; - category :: option
1455 ;; - type :: symbol (`verbatim', nil, t)
1457 ;; + `:with-planning' :: Non-nil means transcoding should include
1458 ;; planning info.
1459 ;; - category :: option
1460 ;; - type :: symbol (nil, t)
1462 ;; + `:with-priority' :: Non-nil means transcoding should include
1463 ;; priority cookies.
1464 ;; - category :: option
1465 ;; - type :: symbol (nil, t)
1467 ;; + `:with-smart-quotes' :: Non-nil means activate smart quotes in
1468 ;; plain text.
1469 ;; - category :: option
1470 ;; - type :: symbol (nil, t)
1472 ;; + `:with-special-strings' :: Non-nil means transcoding should
1473 ;; interpret special strings in plain text.
1474 ;; - category :: option
1475 ;; - type :: symbol (nil, t)
1477 ;; + `:with-sub-superscript' :: Non-nil means transcoding should
1478 ;; interpret subscript and superscript. With a value of "{}",
1479 ;; only interpret those using curly brackets.
1480 ;; - category :: option
1481 ;; - type :: symbol (nil, {}, t)
1483 ;; + `:with-tables' :: Non-nil means transcoding should export tables.
1484 ;; - category :: option
1485 ;; - type :: symbol (nil, t)
1487 ;; + `:with-tags' :: Non-nil means transcoding should keep tags in
1488 ;; headlines. A `not-in-toc' value will remove them from the
1489 ;; table of contents, if any, nonetheless.
1490 ;; - category :: option
1491 ;; - type :: symbol (nil, t, `not-in-toc')
1493 ;; + `:with-tasks' :: Non-nil means transcoding should include
1494 ;; headlines with a TODO keyword. A `todo' value will only
1495 ;; include headlines with a todo type keyword while a `done'
1496 ;; value will do the contrary. If a list of strings is provided,
1497 ;; only tasks with keywords belonging to that list will be kept.
1498 ;; - category :: option
1499 ;; - type :: symbol (t, todo, done, nil) or list of strings
1501 ;; + `:with-timestamps' :: Non-nil means transcoding should include
1502 ;; time stamps. Special value `active' (resp. `inactive') ask to
1503 ;; export only active (resp. inactive) timestamps. Otherwise,
1504 ;; completely remove them.
1505 ;; - category :: option
1506 ;; - type :: symbol: (`active', `inactive', t, nil)
1508 ;; + `:with-toc' :: Non-nil means that a table of contents has to be
1509 ;; added to the output. An integer value limits its depth.
1510 ;; - category :: option
1511 ;; - type :: symbol (nil, t or integer)
1513 ;; + `:with-todo-keywords' :: Non-nil means transcoding should
1514 ;; include TODO keywords.
1515 ;; - category :: option
1516 ;; - type :: symbol (nil, t)
1519 ;;;; Environment Options
1521 ;; Environment options encompass all parameters defined outside the
1522 ;; scope of the parsed data. They come from five sources, in
1523 ;; increasing precedence order:
1525 ;; - Global variables,
1526 ;; - Buffer's attributes,
1527 ;; - Options keyword symbols,
1528 ;; - Buffer keywords,
1529 ;; - Subtree properties.
1531 ;; The central internal function with regards to environment options
1532 ;; is `org-export-get-environment'. It updates global variables with
1533 ;; "#+BIND:" keywords, then retrieve and prioritize properties from
1534 ;; the different sources.
1536 ;; The internal functions doing the retrieval are:
1537 ;; `org-export--get-global-options',
1538 ;; `org-export--get-buffer-attributes',
1539 ;; `org-export--parse-option-keyword',
1540 ;; `org-export--get-subtree-options' and
1541 ;; `org-export--get-inbuffer-options'
1543 ;; Also, `org-export--list-bound-variables' collects bound variables
1544 ;; along with their value in order to set them as buffer local
1545 ;; variables later in the process.
1547 (defun org-export-get-environment (&optional backend subtreep ext-plist)
1548 "Collect export options from the current buffer.
1550 Optional argument BACKEND is an export back-end, as returned by
1551 `org-export-create-backend'.
1553 When optional argument SUBTREEP is non-nil, assume the export is
1554 done against the current sub-tree.
1556 Third optional argument EXT-PLIST is a property list with
1557 external parameters overriding Org default settings, but still
1558 inferior to file-local settings."
1559 ;; First install #+BIND variables since these must be set before
1560 ;; global options are read.
1561 (dolist (pair (org-export--list-bound-variables))
1562 (org-set-local (car pair) (nth 1 pair)))
1563 ;; Get and prioritize export options...
1564 (org-combine-plists
1565 ;; ... from global variables...
1566 (org-export--get-global-options backend)
1567 ;; ... from an external property list...
1568 ext-plist
1569 ;; ... from in-buffer settings...
1570 (org-export--get-inbuffer-options backend)
1571 ;; ... and from subtree, when appropriate.
1572 (and subtreep (org-export--get-subtree-options backend))
1573 ;; Eventually add misc. properties.
1574 (list
1575 :back-end
1576 backend
1577 :translate-alist (org-export-get-all-transcoders backend)
1578 :footnote-definition-alist
1579 ;; Footnotes definitions must be collected in the original
1580 ;; buffer, as there's no insurance that they will still be in
1581 ;; the parse tree, due to possible narrowing.
1582 (let (alist)
1583 (org-with-wide-buffer
1584 (goto-char (point-min))
1585 (while (re-search-forward org-footnote-re nil t)
1586 (backward-char)
1587 (let ((fn (save-match-data (org-element-context))))
1588 (case (org-element-type fn)
1589 (footnote-definition
1590 (push
1591 (cons (org-element-property :label fn)
1592 (let ((cbeg (org-element-property :contents-begin fn)))
1593 (when cbeg
1594 (org-element--parse-elements
1595 cbeg (org-element-property :contents-end fn)
1596 nil nil nil nil (list 'org-data nil)))))
1597 alist))
1598 (footnote-reference
1599 (let ((label (org-element-property :label fn))
1600 (cbeg (org-element-property :contents-begin fn)))
1601 (when (and label cbeg
1602 (eq (org-element-property :type fn) 'inline))
1603 (push
1604 (cons label
1605 (org-element-parse-secondary-string
1606 (buffer-substring
1607 cbeg (org-element-property :contents-end fn))
1608 (org-element-restriction 'footnote-reference)))
1609 alist)))))))
1610 alist))
1611 :id-alist
1612 ;; Collect id references.
1613 (let (alist)
1614 (org-with-wide-buffer
1615 (goto-char (point-min))
1616 (while (re-search-forward "\\[\\[id:\\S-+?\\]" nil t)
1617 (let ((link (org-element-context)))
1618 (when (eq (org-element-type link) 'link)
1619 (let* ((id (org-element-property :path link))
1620 (file (org-id-find-id-file id)))
1621 (when file
1622 (push (cons id (file-relative-name file)) alist)))))))
1623 alist))))
1625 (defun org-export--parse-option-keyword (options &optional backend)
1626 "Parse an OPTIONS line and return values as a plist.
1627 Optional argument BACKEND is an export back-end, as returned by,
1628 e.g., `org-export-create-backend'. It specifies which back-end
1629 specific items to read, if any."
1630 (let* ((all
1631 ;; Priority is given to back-end specific options.
1632 (append (and backend (org-export-get-all-options backend))
1633 org-export-options-alist))
1634 plist)
1635 (dolist (option all)
1636 (let ((property (car option))
1637 (item (nth 2 option)))
1638 (when (and item
1639 (not (plist-member plist property))
1640 (string-match (concat "\\(\\`\\|[ \t]\\)"
1641 (regexp-quote item)
1642 ":\\(([^)\n]+)\\|[^ \t\n\r;,.]*\\)")
1643 options))
1644 (setq plist (plist-put plist
1645 property
1646 (car (read-from-string
1647 (match-string 2 options))))))))
1648 plist))
1650 (defun org-export--get-subtree-options (&optional backend)
1651 "Get export options in subtree at point.
1652 Optional argument BACKEND is an export back-end, as returned by,
1653 e.g., `org-export-create-backend'. It specifies back-end used
1654 for export. Return options as a plist."
1655 ;; For each buffer keyword, create a headline property setting the
1656 ;; same property in communication channel. The name for the property
1657 ;; is the keyword with "EXPORT_" appended to it.
1658 (org-with-wide-buffer
1659 (let (prop plist)
1660 ;; Make sure point is at a heading.
1661 (if (org-at-heading-p) (org-up-heading-safe) (org-back-to-heading t))
1662 ;; Take care of EXPORT_TITLE. If it isn't defined, use headline's
1663 ;; title (with no todo keyword, priority cookie or tag) as its
1664 ;; fallback value.
1665 (when (setq prop (or (org-entry-get (point) "EXPORT_TITLE")
1666 (progn (looking-at org-complex-heading-regexp)
1667 (org-match-string-no-properties 4))))
1668 (setq plist
1669 (plist-put
1670 plist :title
1671 (org-element-parse-secondary-string
1672 prop (org-element-restriction 'keyword)))))
1673 ;; EXPORT_OPTIONS are parsed in a non-standard way.
1674 (when (setq prop (org-entry-get (point) "EXPORT_OPTIONS"))
1675 (setq plist
1676 (nconc plist (org-export--parse-option-keyword prop backend))))
1677 ;; Handle other keywords. TITLE keyword is excluded as it has
1678 ;; been handled already.
1679 (let ((seen '("TITLE")))
1680 (mapc
1681 (lambda (option)
1682 (let ((property (car option))
1683 (keyword (nth 1 option)))
1684 (when (and keyword (not (member keyword seen)))
1685 (let* ((subtree-prop (concat "EXPORT_" keyword))
1686 ;; Export properties are not case-sensitive.
1687 (value (let ((case-fold-search t))
1688 (org-entry-get (point) subtree-prop))))
1689 (push keyword seen)
1690 (when (and value (not (plist-member plist property)))
1691 (setq plist
1692 (plist-put
1693 plist
1694 property
1695 (cond
1696 ;; Parse VALUE if required.
1697 ((member keyword org-element-document-properties)
1698 (org-element-parse-secondary-string
1699 value (org-element-restriction 'keyword)))
1700 ;; If BEHAVIOR is `split' expected value is
1701 ;; a list of strings, not a string.
1702 ((eq (nth 4 option) 'split) (org-split-string value))
1703 (t value)))))))))
1704 ;; Look for both general keywords and back-end specific
1705 ;; options, with priority given to the latter.
1706 (append (and backend (org-export-get-all-options backend))
1707 org-export-options-alist)))
1708 ;; Return value.
1709 plist)))
1711 (defun org-export--get-inbuffer-options (&optional backend)
1712 "Return current buffer export options, as a plist.
1714 Optional argument BACKEND, when non-nil, is an export back-end,
1715 as returned by, e.g., `org-export-create-backend'. It specifies
1716 which back-end specific options should also be read in the
1717 process.
1719 Assume buffer is in Org mode. Narrowing, if any, is ignored."
1720 (let* (plist
1721 get-options ; For byte-compiler.
1722 (case-fold-search t)
1723 (options (append
1724 ;; Priority is given to back-end specific options.
1725 (and backend (org-export-get-all-options backend))
1726 org-export-options-alist))
1727 (regexp (format "^[ \t]*#\\+%s:"
1728 (regexp-opt (nconc (delq nil (mapcar 'cadr options))
1729 org-export-special-keywords))))
1730 (find-properties
1731 (lambda (keyword)
1732 ;; Return all properties associated to KEYWORD.
1733 (let (properties)
1734 (dolist (option options properties)
1735 (when (equal (nth 1 option) keyword)
1736 (pushnew (car option) properties))))))
1737 (get-options
1738 (lambda (&optional files plist)
1739 ;; Recursively read keywords in buffer. FILES is a list
1740 ;; of files read so far. PLIST is the current property
1741 ;; list obtained.
1742 (org-with-wide-buffer
1743 (goto-char (point-min))
1744 (while (re-search-forward regexp nil t)
1745 (let ((element (org-element-at-point)))
1746 (when (eq (org-element-type element) 'keyword)
1747 (let ((key (org-element-property :key element))
1748 (val (org-element-property :value element)))
1749 (cond
1750 ;; Options in `org-export-special-keywords'.
1751 ((equal key "SETUPFILE")
1752 (let ((file (expand-file-name
1753 (org-remove-double-quotes (org-trim val)))))
1754 ;; Avoid circular dependencies.
1755 (unless (member file files)
1756 (with-temp-buffer
1757 (insert (org-file-contents file 'noerror))
1758 (let ((org-inhibit-startup t)) (org-mode))
1759 (setq plist (funcall get-options
1760 (cons file files) plist))))))
1761 ((equal key "OPTIONS")
1762 (setq plist
1763 (org-combine-plists
1764 plist
1765 (org-export--parse-option-keyword val backend))))
1766 ((equal key "FILETAGS")
1767 (setq plist
1768 (org-combine-plists
1769 plist
1770 (list :filetags
1771 (org-uniquify
1772 (append (org-split-string val ":")
1773 (plist-get plist :filetags)))))))
1775 ;; Options in `org-export-options-alist'.
1776 (dolist (property (funcall find-properties key))
1777 (let ((behaviour (nth 4 (assq property options))))
1778 (setq plist
1779 (plist-put
1780 plist property
1781 ;; Handle value depending on specified
1782 ;; BEHAVIOR.
1783 (case behaviour
1784 (space
1785 (if (not (plist-get plist property))
1786 (org-trim val)
1787 (concat (plist-get plist property)
1789 (org-trim val))))
1790 (newline
1791 (org-trim
1792 (concat (plist-get plist property)
1793 "\n"
1794 (org-trim val))))
1795 (split `(,@(plist-get plist property)
1796 ,@(org-split-string val)))
1797 ('t val)
1798 (otherwise
1799 (if (not (plist-member plist property)) val
1800 (plist-get plist property))))))))))))))
1801 ;; Return final value.
1802 plist))))
1803 ;; Read options in the current buffer.
1804 (setq plist (funcall get-options
1805 (and buffer-file-name (list buffer-file-name)) nil))
1806 ;; Parse keywords specified in `org-element-document-properties'
1807 ;; and return PLIST.
1808 (dolist (keyword org-element-document-properties plist)
1809 (dolist (property (funcall find-properties keyword))
1810 (let ((value (plist-get plist property)))
1811 (when (stringp value)
1812 (setq plist
1813 (plist-put plist property
1814 (org-element-parse-secondary-string
1815 value (org-element-restriction 'keyword))))))))))
1817 (defun org-export--get-buffer-attributes ()
1818 "Return properties related to buffer attributes, as a plist."
1819 ;; Store full path of input file name, or nil. For internal use.
1820 (list :input-file (buffer-file-name (buffer-base-buffer))))
1822 (defun org-export--get-global-options (&optional backend)
1823 "Return global export options as a plist.
1824 Optional argument BACKEND, if non-nil, is an export back-end, as
1825 returned by, e.g., `org-export-create-backend'. It specifies
1826 which back-end specific export options should also be read in the
1827 process."
1828 (let (plist
1829 ;; Priority is given to back-end specific options.
1830 (all (append (and backend (org-export-get-all-options backend))
1831 org-export-options-alist)))
1832 (dolist (cell all plist)
1833 (let ((prop (car cell)))
1834 (unless (plist-member plist prop)
1835 (setq plist
1836 (plist-put
1837 plist
1838 prop
1839 ;; Evaluate default value provided. If keyword is
1840 ;; a member of `org-element-document-properties',
1841 ;; parse it as a secondary string before storing it.
1842 (let ((value (eval (nth 3 cell))))
1843 (if (and (stringp value)
1844 (member (nth 1 cell)
1845 org-element-document-properties))
1846 (org-element-parse-secondary-string
1847 value (org-element-restriction 'keyword))
1848 value)))))))))
1850 (defun org-export--list-bound-variables ()
1851 "Return variables bound from BIND keywords in current buffer.
1852 Also look for BIND keywords in setup files. The return value is
1853 an alist where associations are (VARIABLE-NAME VALUE)."
1854 (when org-export-allow-bind-keywords
1855 (let* (collect-bind ; For byte-compiler.
1856 (collect-bind
1857 (lambda (files alist)
1858 ;; Return an alist between variable names and their
1859 ;; value. FILES is a list of setup files names read so
1860 ;; far, used to avoid circular dependencies. ALIST is
1861 ;; the alist collected so far.
1862 (let ((case-fold-search t))
1863 (org-with-wide-buffer
1864 (goto-char (point-min))
1865 (while (re-search-forward
1866 "^[ \t]*#\\+\\(BIND\\|SETUPFILE\\):" nil t)
1867 (let ((element (org-element-at-point)))
1868 (when (eq (org-element-type element) 'keyword)
1869 (let ((val (org-element-property :value element)))
1870 (if (equal (org-element-property :key element) "BIND")
1871 (push (read (format "(%s)" val)) alist)
1872 ;; Enter setup file.
1873 (let ((file (expand-file-name
1874 (org-remove-double-quotes val))))
1875 (unless (member file files)
1876 (with-temp-buffer
1877 (let ((org-inhibit-startup t)) (org-mode))
1878 (insert (org-file-contents file 'noerror))
1879 (setq alist
1880 (funcall collect-bind
1881 (cons file files)
1882 alist))))))))))
1883 alist)))))
1884 ;; Return value in appropriate order of appearance.
1885 (nreverse (funcall collect-bind nil nil)))))
1887 ;; defsubst org-export-get-parent must be defined before first use,
1888 ;; was originally defined in the topology section
1890 (defsubst org-export-get-parent (blob)
1891 "Return BLOB parent or nil.
1892 BLOB is the element or object considered."
1893 (org-element-property :parent blob))
1895 ;;;; Tree Properties
1897 ;; Tree properties are information extracted from parse tree. They
1898 ;; are initialized at the beginning of the transcoding process by
1899 ;; `org-export-collect-tree-properties'.
1901 ;; Dedicated functions focus on computing the value of specific tree
1902 ;; properties during initialization. Thus,
1903 ;; `org-export--populate-ignore-list' lists elements and objects that
1904 ;; should be skipped during export, `org-export--get-min-level' gets
1905 ;; the minimal exportable level, used as a basis to compute relative
1906 ;; level for headlines. Eventually
1907 ;; `org-export--collect-headline-numbering' builds an alist between
1908 ;; headlines and their numbering.
1910 (defun org-export-collect-tree-properties (data info)
1911 "Extract tree properties from parse tree.
1913 DATA is the parse tree from which information is retrieved. INFO
1914 is a list holding export options.
1916 Following tree properties are set or updated:
1918 `:exported-data' Hash table used to memoize results from
1919 `org-export-data'.
1921 `:footnote-definition-alist' List of footnotes definitions in
1922 original buffer and current parse tree.
1924 `:headline-offset' Offset between true level of headlines and
1925 local level. An offset of -1 means a headline
1926 of level 2 should be considered as a level
1927 1 headline in the context.
1929 `:headline-numbering' Alist of all headlines as key an the
1930 associated numbering as value.
1932 `:ignore-list' List of elements that should be ignored during
1933 export.
1935 Return updated plist."
1936 ;; Install the parse tree in the communication channel, in order to
1937 ;; use `org-export-get-genealogy' and al.
1938 (setq info (plist-put info :parse-tree data))
1939 ;; Get the list of elements and objects to ignore, and put it into
1940 ;; `:ignore-list'. Do not overwrite any user ignore that might have
1941 ;; been done during parse tree filtering.
1942 (setq info
1943 (plist-put info
1944 :ignore-list
1945 (append (org-export--populate-ignore-list data info)
1946 (plist-get info :ignore-list))))
1947 ;; Compute `:headline-offset' in order to be able to use
1948 ;; `org-export-get-relative-level'.
1949 (setq info
1950 (plist-put info
1951 :headline-offset
1952 (- 1 (org-export--get-min-level data info))))
1953 ;; Footnote definitions in parse tree override those stored in
1954 ;; `:footnote-definition-alist'. This way, any change to
1955 ;; a definition in the parse tree (e.g., through a parse tree
1956 ;; filter) propagates into the alist.
1957 (let ((defs (plist-get info :footnote-definition-alist)))
1958 (org-element-map data '(footnote-definition footnote-reference)
1959 (lambda (fn)
1960 (cond ((eq (org-element-type fn) 'footnote-definition)
1961 (push (cons (org-element-property :label fn)
1962 (append '(org-data nil) (org-element-contents fn)))
1963 defs))
1964 ((eq (org-element-property :type fn) 'inline)
1965 (push (cons (org-element-property :label fn)
1966 (org-element-contents fn))
1967 defs)))))
1968 (setq info (plist-put info :footnote-definition-alist defs)))
1969 ;; Properties order doesn't matter: get the rest of the tree
1970 ;; properties.
1971 (nconc
1972 `(:headline-numbering ,(org-export--collect-headline-numbering data info)
1973 :exported-data ,(make-hash-table :test 'eq :size 4001))
1974 info))
1976 (defun org-export--get-min-level (data options)
1977 "Return minimum exportable headline's level in DATA.
1978 DATA is parsed tree as returned by `org-element-parse-buffer'.
1979 OPTIONS is a plist holding export options."
1980 (catch 'exit
1981 (let ((min-level 10000))
1982 (mapc
1983 (lambda (blob)
1984 (when (and (eq (org-element-type blob) 'headline)
1985 (not (org-element-property :footnote-section-p blob))
1986 (not (memq blob (plist-get options :ignore-list))))
1987 (setq min-level (min (org-element-property :level blob) min-level)))
1988 (when (= min-level 1) (throw 'exit 1)))
1989 (org-element-contents data))
1990 ;; If no headline was found, for the sake of consistency, set
1991 ;; minimum level to 1 nonetheless.
1992 (if (= min-level 10000) 1 min-level))))
1994 (defun org-export--collect-headline-numbering (data options)
1995 "Return numbering of all exportable headlines in a parse tree.
1997 DATA is the parse tree. OPTIONS is the plist holding export
1998 options.
2000 Return an alist whose key is a headline and value is its
2001 associated numbering \(in the shape of a list of numbers\) or nil
2002 for a footnotes section."
2003 (let ((numbering (make-vector org-export-max-depth 0)))
2004 (org-element-map data 'headline
2005 (lambda (headline)
2006 (unless (org-element-property :footnote-section-p headline)
2007 (let ((relative-level
2008 (1- (org-export-get-relative-level headline options))))
2009 (cons
2010 headline
2011 (loop for n across numbering
2012 for idx from 0 to org-export-max-depth
2013 when (< idx relative-level) collect n
2014 when (= idx relative-level) collect (aset numbering idx (1+ n))
2015 when (> idx relative-level) do (aset numbering idx 0))))))
2016 options)))
2018 (defun org-export--populate-ignore-list (data options)
2019 "Return list of elements and objects to ignore during export.
2020 DATA is the parse tree to traverse. OPTIONS is the plist holding
2021 export options."
2022 (let* (walk-data
2023 ;; First find trees containing a select tag, if any.
2024 (selected (org-export--selected-trees data options))
2025 ;; If a select tag is active, also ignore the section before
2026 ;; the first headline, if any.
2027 (ignore (and selected
2028 (let ((first-element (car (org-element-contents data))))
2029 (and (eq (org-element-type first-element) 'section)
2030 first-element))))
2031 (walk-data
2032 (lambda (data)
2033 ;; Collect ignored elements or objects into IGNORE-LIST.
2034 (let ((type (org-element-type data)))
2035 (if (org-export--skip-p data options selected) (push data ignore)
2036 (if (and (eq type 'headline)
2037 (eq (plist-get options :with-archived-trees) 'headline)
2038 (org-element-property :archivedp data))
2039 ;; If headline is archived but tree below has
2040 ;; to be skipped, add it to ignore list.
2041 (dolist (element (org-element-contents data))
2042 (push element ignore))
2043 ;; Move into secondary string, if any.
2044 (let ((sec-prop
2045 (cdr (assq type org-element-secondary-value-alist))))
2046 (when sec-prop
2047 (mapc walk-data (org-element-property sec-prop data))))
2048 ;; Move into recursive objects/elements.
2049 (mapc walk-data (org-element-contents data))))))))
2050 ;; Main call.
2051 (funcall walk-data data)
2052 ;; Return value.
2053 ignore))
2055 (defun org-export--selected-trees (data info)
2056 "Return list of headlines and inlinetasks with a select tag in their tree.
2057 DATA is parsed data as returned by `org-element-parse-buffer'.
2058 INFO is a plist holding export options."
2059 (let* (selected-trees
2060 walk-data ; For byte-compiler.
2061 (walk-data
2062 (function
2063 (lambda (data genealogy)
2064 (let ((type (org-element-type data)))
2065 (cond
2066 ((memq type '(headline inlinetask))
2067 (let ((tags (org-element-property :tags data)))
2068 (if (loop for tag in (plist-get info :select-tags)
2069 thereis (member tag tags))
2070 ;; When a select tag is found, mark full
2071 ;; genealogy and every headline within the tree
2072 ;; as acceptable.
2073 (setq selected-trees
2074 (append
2075 genealogy
2076 (org-element-map data '(headline inlinetask)
2077 'identity)
2078 selected-trees))
2079 ;; If at a headline, continue searching in tree,
2080 ;; recursively.
2081 (when (eq type 'headline)
2082 (mapc (lambda (el)
2083 (funcall walk-data el (cons data genealogy)))
2084 (org-element-contents data))))))
2085 ((or (eq type 'org-data)
2086 (memq type org-element-greater-elements))
2087 (mapc (lambda (el) (funcall walk-data el genealogy))
2088 (org-element-contents data)))))))))
2089 (funcall walk-data data nil)
2090 selected-trees))
2092 (defun org-export--skip-p (blob options selected)
2093 "Non-nil when element or object BLOB should be skipped during export.
2094 OPTIONS is the plist holding export options. SELECTED, when
2095 non-nil, is a list of headlines or inlinetasks belonging to
2096 a tree with a select tag."
2097 (case (org-element-type blob)
2098 (clock (not (plist-get options :with-clocks)))
2099 (drawer
2100 (let ((with-drawers-p (plist-get options :with-drawers)))
2101 (or (not with-drawers-p)
2102 (and (consp with-drawers-p)
2103 ;; If `:with-drawers' value starts with `not', ignore
2104 ;; every drawer whose name belong to that list.
2105 ;; Otherwise, ignore drawers whose name isn't in that
2106 ;; list.
2107 (let ((name (org-element-property :drawer-name blob)))
2108 (if (eq (car with-drawers-p) 'not)
2109 (member-ignore-case name (cdr with-drawers-p))
2110 (not (member-ignore-case name with-drawers-p))))))))
2111 (fixed-width (not (plist-get options :with-fixed-width)))
2112 ((footnote-definition footnote-reference)
2113 (not (plist-get options :with-footnotes)))
2114 ((headline inlinetask)
2115 (let ((with-tasks (plist-get options :with-tasks))
2116 (todo (org-element-property :todo-keyword blob))
2117 (todo-type (org-element-property :todo-type blob))
2118 (archived (plist-get options :with-archived-trees))
2119 (tags (org-element-property :tags blob)))
2121 (and (eq (org-element-type blob) 'inlinetask)
2122 (not (plist-get options :with-inlinetasks)))
2123 ;; Ignore subtrees with an exclude tag.
2124 (loop for k in (plist-get options :exclude-tags)
2125 thereis (member k tags))
2126 ;; When a select tag is present in the buffer, ignore any tree
2127 ;; without it.
2128 (and selected (not (memq blob selected)))
2129 ;; Ignore commented sub-trees.
2130 (org-element-property :commentedp blob)
2131 ;; Ignore archived subtrees if `:with-archived-trees' is nil.
2132 (and (not archived) (org-element-property :archivedp blob))
2133 ;; Ignore tasks, if specified by `:with-tasks' property.
2134 (and todo
2135 (or (not with-tasks)
2136 (and (memq with-tasks '(todo done))
2137 (not (eq todo-type with-tasks)))
2138 (and (consp with-tasks) (not (member todo with-tasks))))))))
2139 ((latex-environment latex-fragment) (not (plist-get options :with-latex)))
2140 (node-property
2141 (let ((properties-set (plist-get options :with-properties)))
2142 (cond ((null properties-set) t)
2143 ((consp properties-set)
2144 (not (member-ignore-case (org-element-property :key blob)
2145 properties-set))))))
2146 (planning (not (plist-get options :with-planning)))
2147 (property-drawer (not (plist-get options :with-properties)))
2148 (statistics-cookie (not (plist-get options :with-statistics-cookies)))
2149 (table (not (plist-get options :with-tables)))
2150 (table-cell
2151 (and (org-export-table-has-special-column-p
2152 (org-export-get-parent-table blob))
2153 (not (org-export-get-previous-element blob options))))
2154 (table-row (org-export-table-row-is-special-p blob options))
2155 (timestamp
2156 ;; `:with-timestamps' only applies to isolated timestamps
2157 ;; objects, i.e. timestamp objects in a paragraph containing only
2158 ;; timestamps and whitespaces.
2159 (when (let ((parent (org-export-get-parent-element blob)))
2160 (and (memq (org-element-type parent) '(paragraph verse-block))
2161 (not (org-element-map parent
2162 (cons 'plain-text
2163 (remq 'timestamp org-element-all-objects))
2164 (lambda (obj)
2165 (or (not (stringp obj)) (org-string-nw-p obj)))
2166 options t))))
2167 (case (plist-get options :with-timestamps)
2168 ('nil t)
2169 (active
2170 (not (memq (org-element-property :type blob) '(active active-range))))
2171 (inactive
2172 (not (memq (org-element-property :type blob)
2173 '(inactive inactive-range)))))))))
2176 ;;; The Transcoder
2178 ;; `org-export-data' reads a parse tree (obtained with, i.e.
2179 ;; `org-element-parse-buffer') and transcodes it into a specified
2180 ;; back-end output. It takes care of filtering out elements or
2181 ;; objects according to export options and organizing the output blank
2182 ;; lines and white space are preserved. The function memoizes its
2183 ;; results, so it is cheap to call it within transcoders.
2185 ;; It is possible to modify locally the back-end used by
2186 ;; `org-export-data' or even use a temporary back-end by using
2187 ;; `org-export-data-with-backend'.
2189 ;; Internally, three functions handle the filtering of objects and
2190 ;; elements during the export. In particular,
2191 ;; `org-export-ignore-element' marks an element or object so future
2192 ;; parse tree traversals skip it and `org-export-expand' transforms
2193 ;; the others back into their original shape.
2195 ;; `org-export-transcoder' is an accessor returning appropriate
2196 ;; translator function for a given element or object.
2198 (defun org-export-transcoder (blob info)
2199 "Return appropriate transcoder for BLOB.
2200 INFO is a plist containing export directives."
2201 (let ((type (org-element-type blob)))
2202 ;; Return contents only for complete parse trees.
2203 (if (eq type 'org-data) (lambda (blob contents info) contents)
2204 (let ((transcoder (cdr (assq type (plist-get info :translate-alist)))))
2205 (and (functionp transcoder) transcoder)))))
2207 (defun org-export-data (data info)
2208 "Convert DATA into current back-end format.
2210 DATA is a parse tree, an element or an object or a secondary
2211 string. INFO is a plist holding export options.
2213 Return a string."
2214 (or (gethash data (plist-get info :exported-data))
2215 (let* ((type (org-element-type data))
2216 (results
2217 (cond
2218 ;; Ignored element/object.
2219 ((memq data (plist-get info :ignore-list)) nil)
2220 ;; Plain text.
2221 ((eq type 'plain-text)
2222 (org-export-filter-apply-functions
2223 (plist-get info :filter-plain-text)
2224 (let ((transcoder (org-export-transcoder data info)))
2225 (if transcoder (funcall transcoder data info) data))
2226 info))
2227 ;; Secondary string.
2228 ((not type)
2229 (mapconcat (lambda (obj) (org-export-data obj info)) data ""))
2230 ;; Element/Object without contents or, as a special
2231 ;; case, headline with archive tag and archived trees
2232 ;; restricted to title only.
2233 ((or (not (org-element-contents data))
2234 (and (eq type 'headline)
2235 (eq (plist-get info :with-archived-trees) 'headline)
2236 (org-element-property :archivedp data)))
2237 (let ((transcoder (org-export-transcoder data info)))
2238 (or (and (functionp transcoder)
2239 (funcall transcoder data nil info))
2240 ;; Export snippets never return a nil value so
2241 ;; that white spaces following them are never
2242 ;; ignored.
2243 (and (eq type 'export-snippet) ""))))
2244 ;; Element/Object with contents.
2246 (let ((transcoder (org-export-transcoder data info)))
2247 (when transcoder
2248 (let* ((greaterp (memq type org-element-greater-elements))
2249 (objectp
2250 (and (not greaterp)
2251 (memq type org-element-recursive-objects)))
2252 (contents
2253 (mapconcat
2254 (lambda (element) (org-export-data element info))
2255 (org-element-contents
2256 (if (or greaterp objectp) data
2257 ;; Elements directly containing
2258 ;; objects must have their indentation
2259 ;; normalized first.
2260 (org-element-normalize-contents
2261 data
2262 ;; When normalizing contents of the
2263 ;; first paragraph in an item or
2264 ;; a footnote definition, ignore
2265 ;; first line's indentation: there is
2266 ;; none and it might be misleading.
2267 (when (eq type 'paragraph)
2268 (let ((parent (org-export-get-parent data)))
2269 (and
2270 (eq (car (org-element-contents parent))
2271 data)
2272 (memq (org-element-type parent)
2273 '(footnote-definition item))))))))
2274 "")))
2275 (funcall transcoder data
2276 (if (not greaterp) contents
2277 (org-element-normalize-string contents))
2278 info))))))))
2279 ;; Final result will be memoized before being returned.
2280 (puthash
2281 data
2282 (cond
2283 ((not results) "")
2284 ((memq type '(org-data plain-text nil)) results)
2285 ;; Append the same white space between elements or objects
2286 ;; as in the original buffer, and call appropriate filters.
2288 (let ((results
2289 (org-export-filter-apply-functions
2290 (plist-get info (intern (format ":filter-%s" type)))
2291 (let ((post-blank (or (org-element-property :post-blank data)
2292 0)))
2293 (if (memq type org-element-all-elements)
2294 (concat (org-element-normalize-string results)
2295 (make-string post-blank ?\n))
2296 (concat results (make-string post-blank ?\s))))
2297 info)))
2298 results)))
2299 (plist-get info :exported-data)))))
2301 (defun org-export-data-with-backend (data backend info)
2302 "Convert DATA into BACKEND format.
2304 DATA is an element, an object, a secondary string or a string.
2305 BACKEND is a symbol. INFO is a plist used as a communication
2306 channel.
2308 Unlike to `org-export-with-backend', this function will
2309 recursively convert DATA using BACKEND translation table."
2310 (when (symbolp backend) (setq backend (org-export-get-backend backend)))
2311 (org-export-data
2312 data
2313 ;; Set-up a new communication channel with translations defined in
2314 ;; BACKEND as the translate table and a new hash table for
2315 ;; memoization.
2316 (org-combine-plists
2317 info
2318 (list :back-end backend
2319 :translate-alist (org-export-get-all-transcoders backend)
2320 ;; Size of the hash table is reduced since this function
2321 ;; will probably be used on small trees.
2322 :exported-data (make-hash-table :test 'eq :size 401)))))
2324 (defun org-export-remove-uninterpreted-data (data info)
2325 "Change uninterpreted elements back into Org syntax.
2326 DATA is the parse tree. INFO is a plist containing export
2327 options. Each uninterpreted element or object is changed back
2328 into a string. Contents, if any, are not modified. The parse
2329 tree is modified by side effect and returned by the function."
2330 (org-export--remove-uninterpreted-data-1 data info)
2331 (dolist (prop '(:author :date :title))
2332 (plist-put info
2333 prop
2334 (org-export--remove-uninterpreted-data-1
2335 (plist-get info prop)
2336 info))))
2338 (defun org-export--remove-uninterpreted-data-1 (data info)
2339 "Change uninterpreted elements back into Org syntax.
2340 DATA is a parse tree or a secondary string. INFO is a plist
2341 containing export options. It is modified by side effect and
2342 returned by the function."
2343 (org-element-map data
2344 '(entity bold italic latex-environment latex-fragment strike-through
2345 subscript superscript underline)
2346 #'(lambda (blob)
2347 (let ((new
2348 (case (org-element-type blob)
2349 ;; ... entities...
2350 (entity
2351 (and (not (plist-get info :with-entities))
2352 (list (concat
2353 (org-export-expand blob nil)
2354 (make-string
2355 (or (org-element-property :post-blank blob) 0)
2356 ?\s)))))
2357 ;; ... emphasis...
2358 ((bold italic strike-through underline)
2359 (and (not (plist-get info :with-emphasize))
2360 (let ((marker (case (org-element-type blob)
2361 (bold "*")
2362 (italic "/")
2363 (strike-through "+")
2364 (underline "_"))))
2365 (append
2366 (list marker)
2367 (org-element-contents blob)
2368 (list (concat
2369 marker
2370 (make-string
2371 (or (org-element-property :post-blank blob)
2373 ?\s)))))))
2374 ;; ... LaTeX environments and fragments...
2375 ((latex-environment latex-fragment)
2376 (and (eq (plist-get info :with-latex) 'verbatim)
2377 (list (org-export-expand blob nil))))
2378 ;; ... sub/superscripts...
2379 ((subscript superscript)
2380 (let ((sub/super-p (plist-get info :with-sub-superscript))
2381 (bracketp (org-element-property :use-brackets-p blob)))
2382 (and (or (not sub/super-p)
2383 (and (eq sub/super-p '{}) (not bracketp)))
2384 (append
2385 (list (concat
2386 (if (eq (org-element-type blob) 'subscript)
2388 "^")
2389 (and bracketp "{")))
2390 (org-element-contents blob)
2391 (list (concat
2392 (and bracketp "}")
2393 (and (org-element-property :post-blank blob)
2394 (make-string
2395 (org-element-property :post-blank blob)
2396 ?\s)))))))))))
2397 (when new
2398 ;; Splice NEW at BLOB location in parse tree.
2399 (dolist (e new (org-element-extract-element blob))
2400 (unless (string= e "") (org-element-insert-before e blob))))))
2401 info)
2402 ;; Return modified parse tree.
2403 data)
2405 (defun org-export-expand (blob contents &optional with-affiliated)
2406 "Expand a parsed element or object to its original state.
2408 BLOB is either an element or an object. CONTENTS is its
2409 contents, as a string or nil.
2411 When optional argument WITH-AFFILIATED is non-nil, add affiliated
2412 keywords before output."
2413 (let ((type (org-element-type blob)))
2414 (concat (and with-affiliated (memq type org-element-all-elements)
2415 (org-element--interpret-affiliated-keywords blob))
2416 (funcall (intern (format "org-element-%s-interpreter" type))
2417 blob contents))))
2419 (defun org-export-ignore-element (element info)
2420 "Add ELEMENT to `:ignore-list' in INFO.
2422 Any element in `:ignore-list' will be skipped when using
2423 `org-element-map'. INFO is modified by side effects."
2424 (plist-put info :ignore-list (cons element (plist-get info :ignore-list))))
2428 ;;; The Filter System
2430 ;; Filters allow end-users to tweak easily the transcoded output.
2431 ;; They are the functional counterpart of hooks, as every filter in
2432 ;; a set is applied to the return value of the previous one.
2434 ;; Every set is back-end agnostic. Although, a filter is always
2435 ;; called, in addition to the string it applies to, with the back-end
2436 ;; used as argument, so it's easy for the end-user to add back-end
2437 ;; specific filters in the set. The communication channel, as
2438 ;; a plist, is required as the third argument.
2440 ;; From the developer side, filters sets can be installed in the
2441 ;; process with the help of `org-export-define-backend', which
2442 ;; internally stores filters as an alist. Each association has a key
2443 ;; among the following symbols and a function or a list of functions
2444 ;; as value.
2446 ;; - `:filter-options' applies to the property list containing export
2447 ;; options. Unlike to other filters, functions in this list accept
2448 ;; two arguments instead of three: the property list containing
2449 ;; export options and the back-end. Users can set its value through
2450 ;; `org-export-filter-options-functions' variable.
2452 ;; - `:filter-parse-tree' applies directly to the complete parsed
2453 ;; tree. Users can set it through
2454 ;; `org-export-filter-parse-tree-functions' variable.
2456 ;; - `:filter-body' applies to the body of the output, before template
2457 ;; translator chimes in. Users can set it through
2458 ;; `org-export-filter-body-functions' variable.
2460 ;; - `:filter-final-output' applies to the final transcoded string.
2461 ;; Users can set it with `org-export-filter-final-output-functions'
2462 ;; variable.
2464 ;; - `:filter-plain-text' applies to any string not recognized as Org
2465 ;; syntax. `org-export-filter-plain-text-functions' allows users to
2466 ;; configure it.
2468 ;; - `:filter-TYPE' applies on the string returned after an element or
2469 ;; object of type TYPE has been transcoded. A user can modify
2470 ;; `org-export-filter-TYPE-functions' to install these filters.
2472 ;; All filters sets are applied with
2473 ;; `org-export-filter-apply-functions' function. Filters in a set are
2474 ;; applied in a LIFO fashion. It allows developers to be sure that
2475 ;; their filters will be applied first.
2477 ;; Filters properties are installed in communication channel with
2478 ;; `org-export-install-filters' function.
2480 ;; Eventually, two hooks (`org-export-before-processing-hook' and
2481 ;; `org-export-before-parsing-hook') are run at the beginning of the
2482 ;; export process and just before parsing to allow for heavy structure
2483 ;; modifications.
2486 ;;;; Hooks
2488 (defvar org-export-before-processing-hook nil
2489 "Hook run at the beginning of the export process.
2491 This is run before include keywords and macros are expanded and
2492 Babel code blocks executed, on a copy of the original buffer
2493 being exported. Visibility and narrowing are preserved. Point
2494 is at the beginning of the buffer.
2496 Every function in this hook will be called with one argument: the
2497 back-end currently used, as a symbol.")
2499 (defvar org-export-before-parsing-hook nil
2500 "Hook run before parsing an export buffer.
2502 This is run after include keywords and macros have been expanded
2503 and Babel code blocks executed, on a copy of the original buffer
2504 being exported. Visibility and narrowing are preserved. Point
2505 is at the beginning of the buffer.
2507 Every function in this hook will be called with one argument: the
2508 back-end currently used, as a symbol.")
2511 ;;;; Special Filters
2513 (defvar org-export-filter-options-functions nil
2514 "List of functions applied to the export options.
2515 Each filter is called with two arguments: the export options, as
2516 a plist, and the back-end, as a symbol. It must return
2517 a property list containing export options.")
2519 (defvar org-export-filter-parse-tree-functions nil
2520 "List of functions applied to the parsed tree.
2521 Each filter is called with three arguments: the parse tree, as
2522 returned by `org-element-parse-buffer', the back-end, as
2523 a symbol, and the communication channel, as a plist. It must
2524 return the modified parse tree to transcode.")
2526 (defvar org-export-filter-plain-text-functions nil
2527 "List of functions applied to plain text.
2528 Each filter is called with three arguments: a string which
2529 contains no Org syntax, the back-end, as a symbol, and the
2530 communication channel, as a plist. It must return a string or
2531 nil.")
2533 (defvar org-export-filter-body-functions nil
2534 "List of functions applied to transcoded body.
2535 Each filter is called with three arguments: a string which
2536 contains no Org syntax, the back-end, as a symbol, and the
2537 communication channel, as a plist. It must return a string or
2538 nil.")
2540 (defvar org-export-filter-final-output-functions nil
2541 "List of functions applied to the transcoded string.
2542 Each filter is called with three arguments: the full transcoded
2543 string, the back-end, as a symbol, and the communication channel,
2544 as a plist. It must return a string that will be used as the
2545 final export output.")
2548 ;;;; Elements Filters
2550 (defvar org-export-filter-babel-call-functions nil
2551 "List of functions applied to a transcoded babel-call.
2552 Each filter is called with three arguments: the transcoded data,
2553 as a string, the back-end, as a symbol, and the communication
2554 channel, as a plist. It must return a string or nil.")
2556 (defvar org-export-filter-center-block-functions nil
2557 "List of functions applied to a transcoded center block.
2558 Each filter is called with three arguments: the transcoded data,
2559 as a string, the back-end, as a symbol, and the communication
2560 channel, as a plist. It must return a string or nil.")
2562 (defvar org-export-filter-clock-functions nil
2563 "List of functions applied to a transcoded clock.
2564 Each filter is called with three arguments: the transcoded data,
2565 as a string, the back-end, as a symbol, and the communication
2566 channel, as a plist. It must return a string or nil.")
2568 (defvar org-export-filter-comment-functions nil
2569 "List of functions applied to a transcoded comment.
2570 Each filter is called with three arguments: the transcoded data,
2571 as a string, the back-end, as a symbol, and the communication
2572 channel, as a plist. It must return a string or nil.")
2574 (defvar org-export-filter-comment-block-functions nil
2575 "List of functions applied to a transcoded comment-block.
2576 Each filter is called with three arguments: the transcoded data,
2577 as a string, the back-end, as a symbol, and the communication
2578 channel, as a plist. It must return a string or nil.")
2580 (defvar org-export-filter-diary-sexp-functions nil
2581 "List of functions applied to a transcoded diary-sexp.
2582 Each filter is called with three arguments: the transcoded data,
2583 as a string, the back-end, as a symbol, and the communication
2584 channel, as a plist. It must return a string or nil.")
2586 (defvar org-export-filter-drawer-functions nil
2587 "List of functions applied to a transcoded drawer.
2588 Each filter is called with three arguments: the transcoded data,
2589 as a string, the back-end, as a symbol, and the communication
2590 channel, as a plist. It must return a string or nil.")
2592 (defvar org-export-filter-dynamic-block-functions nil
2593 "List of functions applied to a transcoded dynamic-block.
2594 Each filter is called with three arguments: the transcoded data,
2595 as a string, the back-end, as a symbol, and the communication
2596 channel, as a plist. It must return a string or nil.")
2598 (defvar org-export-filter-example-block-functions nil
2599 "List of functions applied to a transcoded example-block.
2600 Each filter is called with three arguments: the transcoded data,
2601 as a string, the back-end, as a symbol, and the communication
2602 channel, as a plist. It must return a string or nil.")
2604 (defvar org-export-filter-export-block-functions nil
2605 "List of functions applied to a transcoded export-block.
2606 Each filter is called with three arguments: the transcoded data,
2607 as a string, the back-end, as a symbol, and the communication
2608 channel, as a plist. It must return a string or nil.")
2610 (defvar org-export-filter-fixed-width-functions nil
2611 "List of functions applied to a transcoded fixed-width.
2612 Each filter is called with three arguments: the transcoded data,
2613 as a string, the back-end, as a symbol, and the communication
2614 channel, as a plist. It must return a string or nil.")
2616 (defvar org-export-filter-footnote-definition-functions nil
2617 "List of functions applied to a transcoded footnote-definition.
2618 Each filter is called with three arguments: the transcoded data,
2619 as a string, the back-end, as a symbol, and the communication
2620 channel, as a plist. It must return a string or nil.")
2622 (defvar org-export-filter-headline-functions nil
2623 "List of functions applied to a transcoded headline.
2624 Each filter is called with three arguments: the transcoded data,
2625 as a string, the back-end, as a symbol, and the communication
2626 channel, as a plist. It must return a string or nil.")
2628 (defvar org-export-filter-horizontal-rule-functions nil
2629 "List of functions applied to a transcoded horizontal-rule.
2630 Each filter is called with three arguments: the transcoded data,
2631 as a string, the back-end, as a symbol, and the communication
2632 channel, as a plist. It must return a string or nil.")
2634 (defvar org-export-filter-inlinetask-functions nil
2635 "List of functions applied to a transcoded inlinetask.
2636 Each filter is called with three arguments: the transcoded data,
2637 as a string, the back-end, as a symbol, and the communication
2638 channel, as a plist. It must return a string or nil.")
2640 (defvar org-export-filter-item-functions nil
2641 "List of functions applied to a transcoded item.
2642 Each filter is called with three arguments: the transcoded data,
2643 as a string, the back-end, as a symbol, and the communication
2644 channel, as a plist. It must return a string or nil.")
2646 (defvar org-export-filter-keyword-functions nil
2647 "List of functions applied to a transcoded keyword.
2648 Each filter is called with three arguments: the transcoded data,
2649 as a string, the back-end, as a symbol, and the communication
2650 channel, as a plist. It must return a string or nil.")
2652 (defvar org-export-filter-latex-environment-functions nil
2653 "List of functions applied to a transcoded latex-environment.
2654 Each filter is called with three arguments: the transcoded data,
2655 as a string, the back-end, as a symbol, and the communication
2656 channel, as a plist. It must return a string or nil.")
2658 (defvar org-export-filter-node-property-functions nil
2659 "List of functions applied to a transcoded node-property.
2660 Each filter is called with three arguments: the transcoded data,
2661 as a string, the back-end, as a symbol, and the communication
2662 channel, as a plist. It must return a string or nil.")
2664 (defvar org-export-filter-paragraph-functions nil
2665 "List of functions applied to a transcoded paragraph.
2666 Each filter is called with three arguments: the transcoded data,
2667 as a string, the back-end, as a symbol, and the communication
2668 channel, as a plist. It must return a string or nil.")
2670 (defvar org-export-filter-plain-list-functions nil
2671 "List of functions applied to a transcoded plain-list.
2672 Each filter is called with three arguments: the transcoded data,
2673 as a string, the back-end, as a symbol, and the communication
2674 channel, as a plist. It must return a string or nil.")
2676 (defvar org-export-filter-planning-functions nil
2677 "List of functions applied to a transcoded planning.
2678 Each filter is called with three arguments: the transcoded data,
2679 as a string, the back-end, as a symbol, and the communication
2680 channel, as a plist. It must return a string or nil.")
2682 (defvar org-export-filter-property-drawer-functions nil
2683 "List of functions applied to a transcoded property-drawer.
2684 Each filter is called with three arguments: the transcoded data,
2685 as a string, the back-end, as a symbol, and the communication
2686 channel, as a plist. It must return a string or nil.")
2688 (defvar org-export-filter-quote-block-functions nil
2689 "List of functions applied to a transcoded quote block.
2690 Each filter is called with three arguments: the transcoded quote
2691 data, as a string, the back-end, as a symbol, and the
2692 communication channel, as a plist. It must return a string or
2693 nil.")
2695 (defvar org-export-filter-section-functions nil
2696 "List of functions applied to a transcoded section.
2697 Each filter is called with three arguments: the transcoded data,
2698 as a string, the back-end, as a symbol, and the communication
2699 channel, as a plist. It must return a string or nil.")
2701 (defvar org-export-filter-special-block-functions nil
2702 "List of functions applied to a transcoded special block.
2703 Each filter is called with three arguments: the transcoded data,
2704 as a string, the back-end, as a symbol, and the communication
2705 channel, as a plist. It must return a string or nil.")
2707 (defvar org-export-filter-src-block-functions nil
2708 "List of functions applied to a transcoded src-block.
2709 Each filter is called with three arguments: the transcoded data,
2710 as a string, the back-end, as a symbol, and the communication
2711 channel, as a plist. It must return a string or nil.")
2713 (defvar org-export-filter-table-functions nil
2714 "List of functions applied to a transcoded table.
2715 Each filter is called with three arguments: the transcoded data,
2716 as a string, the back-end, as a symbol, and the communication
2717 channel, as a plist. It must return a string or nil.")
2719 (defvar org-export-filter-table-cell-functions nil
2720 "List of functions applied to a transcoded table-cell.
2721 Each filter is called with three arguments: the transcoded data,
2722 as a string, the back-end, as a symbol, and the communication
2723 channel, as a plist. It must return a string or nil.")
2725 (defvar org-export-filter-table-row-functions nil
2726 "List of functions applied to a transcoded table-row.
2727 Each filter is called with three arguments: the transcoded data,
2728 as a string, the back-end, as a symbol, and the communication
2729 channel, as a plist. It must return a string or nil.")
2731 (defvar org-export-filter-verse-block-functions nil
2732 "List of functions applied to a transcoded verse block.
2733 Each filter is called with three arguments: the transcoded data,
2734 as a string, the back-end, as a symbol, and the communication
2735 channel, as a plist. It must return a string or nil.")
2738 ;;;; Objects Filters
2740 (defvar org-export-filter-bold-functions nil
2741 "List of functions applied to transcoded bold text.
2742 Each filter is called with three arguments: the transcoded data,
2743 as a string, the back-end, as a symbol, and the communication
2744 channel, as a plist. It must return a string or nil.")
2746 (defvar org-export-filter-code-functions nil
2747 "List of functions applied to transcoded code text.
2748 Each filter is called with three arguments: the transcoded data,
2749 as a string, the back-end, as a symbol, and the communication
2750 channel, as a plist. It must return a string or nil.")
2752 (defvar org-export-filter-entity-functions nil
2753 "List of functions applied to a transcoded entity.
2754 Each filter is called with three arguments: the transcoded data,
2755 as a string, the back-end, as a symbol, and the communication
2756 channel, as a plist. It must return a string or nil.")
2758 (defvar org-export-filter-export-snippet-functions nil
2759 "List of functions applied to a transcoded export-snippet.
2760 Each filter is called with three arguments: the transcoded data,
2761 as a string, the back-end, as a symbol, and the communication
2762 channel, as a plist. It must return a string or nil.")
2764 (defvar org-export-filter-footnote-reference-functions nil
2765 "List of functions applied to a transcoded footnote-reference.
2766 Each filter is called with three arguments: the transcoded data,
2767 as a string, the back-end, as a symbol, and the communication
2768 channel, as a plist. It must return a string or nil.")
2770 (defvar org-export-filter-inline-babel-call-functions nil
2771 "List of functions applied to a transcoded inline-babel-call.
2772 Each filter is called with three arguments: the transcoded data,
2773 as a string, the back-end, as a symbol, and the communication
2774 channel, as a plist. It must return a string or nil.")
2776 (defvar org-export-filter-inline-src-block-functions nil
2777 "List of functions applied to a transcoded inline-src-block.
2778 Each filter is called with three arguments: the transcoded data,
2779 as a string, the back-end, as a symbol, and the communication
2780 channel, as a plist. It must return a string or nil.")
2782 (defvar org-export-filter-italic-functions nil
2783 "List of functions applied to transcoded italic text.
2784 Each filter is called with three arguments: the transcoded data,
2785 as a string, the back-end, as a symbol, and the communication
2786 channel, as a plist. It must return a string or nil.")
2788 (defvar org-export-filter-latex-fragment-functions nil
2789 "List of functions applied to a transcoded latex-fragment.
2790 Each filter is called with three arguments: the transcoded data,
2791 as a string, the back-end, as a symbol, and the communication
2792 channel, as a plist. It must return a string or nil.")
2794 (defvar org-export-filter-line-break-functions nil
2795 "List of functions applied to a transcoded line-break.
2796 Each filter is called with three arguments: the transcoded data,
2797 as a string, the back-end, as a symbol, and the communication
2798 channel, as a plist. It must return a string or nil.")
2800 (defvar org-export-filter-link-functions nil
2801 "List of functions applied to a transcoded link.
2802 Each filter is called with three arguments: the transcoded data,
2803 as a string, the back-end, as a symbol, and the communication
2804 channel, as a plist. It must return a string or nil.")
2806 (defvar org-export-filter-radio-target-functions nil
2807 "List of functions applied to a transcoded radio-target.
2808 Each filter is called with three arguments: the transcoded data,
2809 as a string, the back-end, as a symbol, and the communication
2810 channel, as a plist. It must return a string or nil.")
2812 (defvar org-export-filter-statistics-cookie-functions nil
2813 "List of functions applied to a transcoded statistics-cookie.
2814 Each filter is called with three arguments: the transcoded data,
2815 as a string, the back-end, as a symbol, and the communication
2816 channel, as a plist. It must return a string or nil.")
2818 (defvar org-export-filter-strike-through-functions nil
2819 "List of functions applied to transcoded strike-through text.
2820 Each filter is called with three arguments: the transcoded data,
2821 as a string, the back-end, as a symbol, and the communication
2822 channel, as a plist. It must return a string or nil.")
2824 (defvar org-export-filter-subscript-functions nil
2825 "List of functions applied to a transcoded subscript.
2826 Each filter is called with three arguments: the transcoded data,
2827 as a string, the back-end, as a symbol, and the communication
2828 channel, as a plist. It must return a string or nil.")
2830 (defvar org-export-filter-superscript-functions nil
2831 "List of functions applied to a transcoded superscript.
2832 Each filter is called with three arguments: the transcoded data,
2833 as a string, the back-end, as a symbol, and the communication
2834 channel, as a plist. It must return a string or nil.")
2836 (defvar org-export-filter-target-functions nil
2837 "List of functions applied to a transcoded target.
2838 Each filter is called with three arguments: the transcoded data,
2839 as a string, the back-end, as a symbol, and the communication
2840 channel, as a plist. It must return a string or nil.")
2842 (defvar org-export-filter-timestamp-functions nil
2843 "List of functions applied to a transcoded timestamp.
2844 Each filter is called with three arguments: the transcoded data,
2845 as a string, the back-end, as a symbol, and the communication
2846 channel, as a plist. It must return a string or nil.")
2848 (defvar org-export-filter-underline-functions nil
2849 "List of functions applied to transcoded underline text.
2850 Each filter is called with three arguments: the transcoded data,
2851 as a string, the back-end, as a symbol, and the communication
2852 channel, as a plist. It must return a string or nil.")
2854 (defvar org-export-filter-verbatim-functions nil
2855 "List of functions applied to transcoded verbatim text.
2856 Each filter is called with three arguments: the transcoded data,
2857 as a string, the back-end, as a symbol, and the communication
2858 channel, as a plist. It must return a string or nil.")
2861 ;;;; Filters Tools
2863 ;; Internal function `org-export-install-filters' installs filters
2864 ;; hard-coded in back-ends (developer filters) and filters from global
2865 ;; variables (user filters) in the communication channel.
2867 ;; Internal function `org-export-filter-apply-functions' takes care
2868 ;; about applying each filter in order to a given data. It ignores
2869 ;; filters returning a nil value but stops whenever a filter returns
2870 ;; an empty string.
2872 (defun org-export-filter-apply-functions (filters value info)
2873 "Call every function in FILTERS.
2875 Functions are called with arguments VALUE, current export
2876 back-end's name and INFO. A function returning a nil value will
2877 be skipped. If it returns the empty string, the process ends and
2878 VALUE is ignored.
2880 Call is done in a LIFO fashion, to be sure that developer
2881 specified filters, if any, are called first."
2882 (catch 'exit
2883 (let* ((backend (plist-get info :back-end))
2884 (backend-name (and backend (org-export-backend-name backend))))
2885 (dolist (filter filters value)
2886 (let ((result (funcall filter value backend-name info)))
2887 (cond ((not result) value)
2888 ((equal value "") (throw 'exit nil))
2889 (t (setq value result))))))))
2891 (defun org-export-install-filters (info)
2892 "Install filters properties in communication channel.
2893 INFO is a plist containing the current communication channel.
2894 Return the updated communication channel."
2895 (let (plist)
2896 ;; Install user-defined filters with `org-export-filters-alist'
2897 ;; and filters already in INFO (through ext-plist mechanism).
2898 (mapc (lambda (p)
2899 (let* ((prop (car p))
2900 (info-value (plist-get info prop))
2901 (default-value (symbol-value (cdr p))))
2902 (setq plist
2903 (plist-put plist prop
2904 ;; Filters in INFO will be called
2905 ;; before those user provided.
2906 (append (if (listp info-value) info-value
2907 (list info-value))
2908 default-value)))))
2909 org-export-filters-alist)
2910 ;; Prepend back-end specific filters to that list.
2911 (mapc (lambda (p)
2912 ;; Single values get consed, lists are appended.
2913 (let ((key (car p)) (value (cdr p)))
2914 (when value
2915 (setq plist
2916 (plist-put
2917 plist key
2918 (if (atom value) (cons value (plist-get plist key))
2919 (append value (plist-get plist key))))))))
2920 (org-export-get-all-filters (plist-get info :back-end)))
2921 ;; Return new communication channel.
2922 (org-combine-plists info plist)))
2926 ;;; Core functions
2928 ;; This is the room for the main function, `org-export-as', along with
2929 ;; its derivative, `org-export-string-as'.
2930 ;; `org-export--copy-to-kill-ring-p' determines if output of these
2931 ;; function should be added to kill ring.
2933 ;; Note that `org-export-as' doesn't really parse the current buffer,
2934 ;; but a copy of it (with the same buffer-local variables and
2935 ;; visibility), where macros and include keywords are expanded and
2936 ;; Babel blocks are executed, if appropriate.
2937 ;; `org-export-with-buffer-copy' macro prepares that copy.
2939 ;; File inclusion is taken care of by
2940 ;; `org-export-expand-include-keyword' and
2941 ;; `org-export--prepare-file-contents'. Structure wise, including
2942 ;; a whole Org file in a buffer often makes little sense. For
2943 ;; example, if the file contains a headline and the include keyword
2944 ;; was within an item, the item should contain the headline. That's
2945 ;; why file inclusion should be done before any structure can be
2946 ;; associated to the file, that is before parsing.
2948 ;; `org-export-insert-default-template' is a command to insert
2949 ;; a default template (or a back-end specific template) at point or in
2950 ;; current subtree.
2952 (defun org-export-copy-buffer ()
2953 "Return a copy of the current buffer.
2954 The copy preserves Org buffer-local variables, visibility and
2955 narrowing."
2956 (let ((copy-buffer-fun (org-export--generate-copy-script (current-buffer)))
2957 (new-buf (generate-new-buffer (buffer-name))))
2958 (with-current-buffer new-buf
2959 (funcall copy-buffer-fun)
2960 (set-buffer-modified-p nil))
2961 new-buf))
2963 (defmacro org-export-with-buffer-copy (&rest body)
2964 "Apply BODY in a copy of the current buffer.
2965 The copy preserves local variables, visibility and contents of
2966 the original buffer. Point is at the beginning of the buffer
2967 when BODY is applied."
2968 (declare (debug t))
2969 (org-with-gensyms (buf-copy)
2970 `(let ((,buf-copy (org-export-copy-buffer)))
2971 (unwind-protect
2972 (with-current-buffer ,buf-copy
2973 (goto-char (point-min))
2974 (progn ,@body))
2975 (and (buffer-live-p ,buf-copy)
2976 ;; Kill copy without confirmation.
2977 (progn (with-current-buffer ,buf-copy
2978 (restore-buffer-modified-p nil))
2979 (kill-buffer ,buf-copy)))))))
2981 (defun org-export--generate-copy-script (buffer)
2982 "Generate a function duplicating BUFFER.
2984 The copy will preserve local variables, visibility, contents and
2985 narrowing of the original buffer. If a region was active in
2986 BUFFER, contents will be narrowed to that region instead.
2988 The resulting function can be evaluated at a later time, from
2989 another buffer, effectively cloning the original buffer there.
2991 The function assumes BUFFER's major mode is `org-mode'."
2992 (with-current-buffer buffer
2993 `(lambda ()
2994 (let ((inhibit-modification-hooks t))
2995 ;; Set major mode. Ignore `org-mode-hook' as it has been run
2996 ;; already in BUFFER.
2997 (let ((org-mode-hook nil) (org-inhibit-startup t)) (org-mode))
2998 ;; Copy specific buffer local variables and variables set
2999 ;; through BIND keywords.
3000 ,@(let ((bound-variables (org-export--list-bound-variables))
3001 vars)
3002 (dolist (entry (buffer-local-variables (buffer-base-buffer)) vars)
3003 (when (consp entry)
3004 (let ((var (car entry))
3005 (val (cdr entry)))
3006 (and (not (memq var org-export-ignored-local-variables))
3007 (or (memq var
3008 '(default-directory
3009 buffer-file-name
3010 buffer-file-coding-system))
3011 (assq var bound-variables)
3012 (string-match "^\\(org-\\|orgtbl-\\)"
3013 (symbol-name var)))
3014 ;; Skip unreadable values, as they cannot be
3015 ;; sent to external process.
3016 (or (not val) (ignore-errors (read (format "%S" val))))
3017 (push `(set (make-local-variable (quote ,var))
3018 (quote ,val))
3019 vars))))))
3020 ;; Whole buffer contents.
3021 (insert
3022 ,(org-with-wide-buffer
3023 (buffer-substring-no-properties
3024 (point-min) (point-max))))
3025 ;; Narrowing.
3026 ,(if (org-region-active-p)
3027 `(narrow-to-region ,(region-beginning) ,(region-end))
3028 `(narrow-to-region ,(point-min) ,(point-max)))
3029 ;; Current position of point.
3030 (goto-char ,(point))
3031 ;; Overlays with invisible property.
3032 ,@(let (ov-set)
3033 (mapc
3034 (lambda (ov)
3035 (let ((invis-prop (overlay-get ov 'invisible)))
3036 (when invis-prop
3037 (push `(overlay-put
3038 (make-overlay ,(overlay-start ov)
3039 ,(overlay-end ov))
3040 'invisible (quote ,invis-prop))
3041 ov-set))))
3042 (overlays-in (point-min) (point-max)))
3043 ov-set)))))
3045 ;;;###autoload
3046 (defun org-export-as
3047 (backend &optional subtreep visible-only body-only ext-plist)
3048 "Transcode current Org buffer into BACKEND code.
3050 BACKEND is either an export back-end, as returned by, e.g.,
3051 `org-export-create-backend', or a symbol referring to
3052 a registered back-end.
3054 If narrowing is active in the current buffer, only transcode its
3055 narrowed part.
3057 If a region is active, transcode that region.
3059 When optional argument SUBTREEP is non-nil, transcode the
3060 sub-tree at point, extracting information from the headline
3061 properties first.
3063 When optional argument VISIBLE-ONLY is non-nil, don't export
3064 contents of hidden elements.
3066 When optional argument BODY-ONLY is non-nil, only return body
3067 code, without surrounding template.
3069 Optional argument EXT-PLIST, when provided, is a property list
3070 with external parameters overriding Org default settings, but
3071 still inferior to file-local settings.
3073 Return code as a string."
3074 (when (symbolp backend) (setq backend (org-export-get-backend backend)))
3075 (org-export-barf-if-invalid-backend backend)
3076 (save-excursion
3077 (save-restriction
3078 ;; Narrow buffer to an appropriate region or subtree for
3079 ;; parsing. If parsing subtree, be sure to remove main headline
3080 ;; too.
3081 (cond ((org-region-active-p)
3082 (narrow-to-region (region-beginning) (region-end)))
3083 (subtreep
3084 (org-narrow-to-subtree)
3085 (goto-char (point-min))
3086 (forward-line)
3087 (narrow-to-region (point) (point-max))))
3088 ;; Initialize communication channel with original buffer
3089 ;; attributes, unavailable in its copy.
3090 (let* ((org-export-current-backend (org-export-backend-name backend))
3091 (info (org-combine-plists
3092 (list :export-options
3093 (delq nil
3094 (list (and subtreep 'subtree)
3095 (and visible-only 'visible-only)
3096 (and body-only 'body-only))))
3097 (org-export--get-buffer-attributes)))
3098 tree)
3099 ;; Update communication channel and get parse tree. Buffer
3100 ;; isn't parsed directly. Instead, a temporary copy is
3101 ;; created, where include keywords, macros are expanded and
3102 ;; code blocks are evaluated.
3103 (org-export-with-buffer-copy
3104 ;; Run first hook with current back-end's name as argument.
3105 (run-hook-with-args 'org-export-before-processing-hook
3106 (org-export-backend-name backend))
3107 (org-export-expand-include-keyword)
3108 ;; Update macro templates since #+INCLUDE keywords might have
3109 ;; added some new ones.
3110 (org-macro-initialize-templates)
3111 (org-macro-replace-all org-macro-templates)
3112 (org-export-execute-babel-code)
3113 ;; Update radio targets since keyword inclusion might have
3114 ;; added some more.
3115 (org-update-radio-target-regexp)
3116 ;; Run last hook with current back-end's name as argument.
3117 (goto-char (point-min))
3118 (save-excursion
3119 (run-hook-with-args 'org-export-before-parsing-hook
3120 (org-export-backend-name backend)))
3121 ;; Update communication channel with environment. Also
3122 ;; install user's and developer's filters.
3123 (setq info
3124 (org-export-install-filters
3125 (org-combine-plists
3126 info (org-export-get-environment backend subtreep ext-plist))))
3127 ;; Expand export-specific set of macros: {{{author}}},
3128 ;; {{{date}}}, {{{email}}} and {{{title}}}. It must be done
3129 ;; once regular macros have been expanded, since document
3130 ;; keywords may contain one of them.
3131 (org-macro-replace-all
3132 (list (cons "author"
3133 (org-element-interpret-data (plist-get info :author)))
3134 (cons "date"
3135 (org-element-interpret-data (plist-get info :date)))
3136 ;; EMAIL is not a parsed keyword: store it as-is.
3137 (cons "email" (or (plist-get info :email) ""))
3138 (cons "title"
3139 (org-element-interpret-data (plist-get info :title)))))
3140 ;; Parse buffer.
3141 (setq tree (org-element-parse-buffer nil visible-only))
3142 ;; Handle left-over uninterpreted elements or objects in
3143 ;; parse tree and communication channel.
3144 (org-export-remove-uninterpreted-data tree info)
3145 ;; Call options filters and update export options. We do not
3146 ;; use `org-export-filter-apply-functions' here since the
3147 ;; arity of such filters is different.
3148 (let ((backend-name (org-export-backend-name backend)))
3149 (dolist (filter (plist-get info :filter-options))
3150 (let ((result (funcall filter info backend-name)))
3151 (when result (setq info result)))))
3152 ;; Parse buffer, handle uninterpreted elements or objects,
3153 ;; then call parse-tree filters.
3154 (setq tree
3155 (org-export-filter-apply-functions
3156 (plist-get info :filter-parse-tree) tree info))
3157 ;; Now tree is complete, compute its properties and add them
3158 ;; to communication channel.
3159 (setq info
3160 (org-combine-plists
3161 info (org-export-collect-tree-properties tree info)))
3162 ;; Eventually transcode TREE. Wrap the resulting string into
3163 ;; a template.
3164 (let* ((body (org-element-normalize-string
3165 (or (org-export-data tree info) "")))
3166 (inner-template (cdr (assq 'inner-template
3167 (plist-get info :translate-alist))))
3168 (full-body (org-export-filter-apply-functions
3169 (plist-get info :filter-body)
3170 (if (not (functionp inner-template)) body
3171 (funcall inner-template body info))
3172 info))
3173 (template (cdr (assq 'template
3174 (plist-get info :translate-alist)))))
3175 ;; Remove all text properties since they cannot be
3176 ;; retrieved from an external process. Finally call
3177 ;; final-output filter and return result.
3178 (org-no-properties
3179 (org-export-filter-apply-functions
3180 (plist-get info :filter-final-output)
3181 (if (or (not (functionp template)) body-only) full-body
3182 (funcall template full-body info))
3183 info))))))))
3185 ;;;###autoload
3186 (defun org-export-string-as (string backend &optional body-only ext-plist)
3187 "Transcode STRING into BACKEND code.
3189 BACKEND is either an export back-end, as returned by, e.g.,
3190 `org-export-create-backend', or a symbol referring to
3191 a registered back-end.
3193 When optional argument BODY-ONLY is non-nil, only return body
3194 code, without preamble nor postamble.
3196 Optional argument EXT-PLIST, when provided, is a property list
3197 with external parameters overriding Org default settings, but
3198 still inferior to file-local settings.
3200 Return code as a string."
3201 (with-temp-buffer
3202 (insert string)
3203 (let ((org-inhibit-startup t)) (org-mode))
3204 (org-export-as backend nil nil body-only ext-plist)))
3206 ;;;###autoload
3207 (defun org-export-replace-region-by (backend)
3208 "Replace the active region by its export to BACKEND.
3209 BACKEND is either an export back-end, as returned by, e.g.,
3210 `org-export-create-backend', or a symbol referring to
3211 a registered back-end."
3212 (if (not (org-region-active-p))
3213 (user-error "No active region to replace")
3214 (let* ((beg (region-beginning))
3215 (end (region-end))
3216 (str (buffer-substring beg end)) rpl)
3217 (setq rpl (org-export-string-as str backend t))
3218 (delete-region beg end)
3219 (insert rpl))))
3221 ;;;###autoload
3222 (defun org-export-insert-default-template (&optional backend subtreep)
3223 "Insert all export keywords with default values at beginning of line.
3225 BACKEND is a symbol referring to the name of a registered export
3226 back-end, for which specific export options should be added to
3227 the template, or `default' for default template. When it is nil,
3228 the user will be prompted for a category.
3230 If SUBTREEP is non-nil, export configuration will be set up
3231 locally for the subtree through node properties."
3232 (interactive)
3233 (unless (derived-mode-p 'org-mode) (user-error "Not in an Org mode buffer"))
3234 (when (and subtreep (org-before-first-heading-p))
3235 (user-error "No subtree to set export options for"))
3236 (let ((node (and subtreep (save-excursion (org-back-to-heading t) (point))))
3237 (backend
3238 (or backend
3239 (intern
3240 (org-completing-read
3241 "Options category: "
3242 (cons "default"
3243 (mapcar #'(lambda (b)
3244 (symbol-name (org-export-backend-name b)))
3245 org-export--registered-backends))
3246 nil t))))
3247 options keywords)
3248 ;; Populate OPTIONS and KEYWORDS.
3249 (dolist (entry (cond ((eq backend 'default) org-export-options-alist)
3250 ((org-export-backend-p backend)
3251 (org-export-backend-options backend))
3252 (t (org-export-backend-options
3253 (org-export-get-backend backend)))))
3254 (let ((keyword (nth 1 entry))
3255 (option (nth 2 entry)))
3256 (cond
3257 (keyword (unless (assoc keyword keywords)
3258 (let ((value
3259 (if (eq (nth 4 entry) 'split)
3260 (mapconcat #'identity (eval (nth 3 entry)) " ")
3261 (eval (nth 3 entry)))))
3262 (push (cons keyword value) keywords))))
3263 (option (unless (assoc option options)
3264 (push (cons option (eval (nth 3 entry))) options))))))
3265 ;; Move to an appropriate location in order to insert options.
3266 (unless subtreep (beginning-of-line))
3267 ;; First (multiple) OPTIONS lines. Never go past fill-column.
3268 (when options
3269 (let ((items
3270 (mapcar
3271 #'(lambda (opt) (format "%s:%S" (car opt) (cdr opt)))
3272 (sort options (lambda (k1 k2) (string< (car k1) (car k2)))))))
3273 (if subtreep
3274 (org-entry-put
3275 node "EXPORT_OPTIONS" (mapconcat 'identity items " "))
3276 (while items
3277 (insert "#+OPTIONS:")
3278 (let ((width 10))
3279 (while (and items
3280 (< (+ width (length (car items)) 1) fill-column))
3281 (let ((item (pop items)))
3282 (insert " " item)
3283 (incf width (1+ (length item))))))
3284 (insert "\n")))))
3285 ;; Then the rest of keywords, in the order specified in either
3286 ;; `org-export-options-alist' or respective export back-ends.
3287 (dolist (key (nreverse keywords))
3288 (let ((val (cond ((equal (car key) "DATE")
3289 (or (cdr key)
3290 (with-temp-buffer
3291 (org-insert-time-stamp (current-time)))))
3292 ((equal (car key) "TITLE")
3293 (or (let ((visited-file
3294 (buffer-file-name (buffer-base-buffer))))
3295 (and visited-file
3296 (file-name-sans-extension
3297 (file-name-nondirectory visited-file))))
3298 (buffer-name (buffer-base-buffer))))
3299 (t (cdr key)))))
3300 (if subtreep (org-entry-put node (concat "EXPORT_" (car key)) val)
3301 (insert
3302 (format "#+%s:%s\n"
3303 (car key)
3304 (if (org-string-nw-p val) (format " %s" val) ""))))))))
3306 (defun org-export-expand-include-keyword (&optional included dir)
3307 "Expand every include keyword in buffer.
3308 Optional argument INCLUDED is a list of included file names along
3309 with their line restriction, when appropriate. It is used to
3310 avoid infinite recursion. Optional argument DIR is the current
3311 working directory. It is used to properly resolve relative
3312 paths."
3313 (let ((case-fold-search t)
3314 (file-prefix (make-hash-table :test #'equal))
3315 (current-prefix 0))
3316 (goto-char (point-min))
3317 (while (re-search-forward "^[ \t]*#\\+INCLUDE:" nil t)
3318 (let ((element (save-match-data (org-element-at-point))))
3319 (when (eq (org-element-type element) 'keyword)
3320 (beginning-of-line)
3321 ;; Extract arguments from keyword's value.
3322 (let* ((value (org-element-property :value element))
3323 (ind (org-get-indentation))
3324 (file (and (string-match
3325 "^\\(\".+?\"\\|\\S-+\\)\\(?:\\s-+\\|$\\)" value)
3326 (prog1 (expand-file-name
3327 (org-remove-double-quotes
3328 (match-string 1 value))
3329 dir)
3330 (setq value (replace-match "" nil nil value)))))
3331 (lines
3332 (and (string-match
3333 ":lines +\"\\(\\(?:[0-9]+\\)?-\\(?:[0-9]+\\)?\\)\""
3334 value)
3335 (prog1 (match-string 1 value)
3336 (setq value (replace-match "" nil nil value)))))
3337 (env (cond ((string-match "\\<example\\>" value)
3338 'literal)
3339 ((string-match "\\<src\\(?: +\\(.*\\)\\)?" value)
3340 'literal)))
3341 ;; Minimal level of included file defaults to the child
3342 ;; level of the current headline, if any, or one. It
3343 ;; only applies is the file is meant to be included as
3344 ;; an Org one.
3345 (minlevel
3346 (and (not env)
3347 (if (string-match ":minlevel +\\([0-9]+\\)" value)
3348 (prog1 (string-to-number (match-string 1 value))
3349 (setq value (replace-match "" nil nil value)))
3350 (let ((cur (org-current-level)))
3351 (if cur (1+ (org-reduced-level cur)) 1)))))
3352 (src-args (and (eq env 'literal)
3353 (match-string 1 value)))
3354 (block (and (string-match "\\<\\(\\S-+\\)\\>" value)
3355 (match-string 1 value))))
3356 ;; Remove keyword.
3357 (delete-region (point) (progn (forward-line) (point)))
3358 (cond
3359 ((not file) nil)
3360 ((not (file-readable-p file))
3361 (error "Cannot include file %s" file))
3362 ;; Check if files has already been parsed. Look after
3363 ;; inclusion lines too, as different parts of the same file
3364 ;; can be included too.
3365 ((member (list file lines) included)
3366 (error "Recursive file inclusion: %s" file))
3368 (cond
3369 ((eq env 'literal)
3370 (insert
3371 (let ((ind-str (make-string ind ? ))
3372 (arg-str (if (stringp src-args)
3373 (format " %s" src-args)
3374 ""))
3375 (contents
3376 (org-escape-code-in-string
3377 (org-export--prepare-file-contents file lines))))
3378 (format "%s#+BEGIN_%s%s\n%s%s#+END_%s\n"
3379 ind-str block arg-str contents ind-str block))))
3380 ((stringp block)
3381 (insert
3382 (let ((ind-str (make-string ind ? ))
3383 (contents
3384 (org-export--prepare-file-contents file lines)))
3385 (format "%s#+BEGIN_%s\n%s%s#+END_%s\n"
3386 ind-str block contents ind-str block))))
3388 (insert
3389 (with-temp-buffer
3390 (let ((org-inhibit-startup t)) (org-mode))
3391 (insert
3392 (org-export--prepare-file-contents
3393 file lines ind minlevel
3394 (or (gethash file file-prefix)
3395 (puthash file (incf current-prefix) file-prefix))))
3396 (org-export-expand-include-keyword
3397 (cons (list file lines) included)
3398 (file-name-directory file))
3399 (buffer-string)))))))))))))
3401 (defun org-export--prepare-file-contents (file &optional lines ind minlevel id)
3402 "Prepare the contents of FILE for inclusion and return them as a string.
3404 When optional argument LINES is a string specifying a range of
3405 lines, include only those lines.
3407 Optional argument IND, when non-nil, is an integer specifying the
3408 global indentation of returned contents. Since its purpose is to
3409 allow an included file to stay in the same environment it was
3410 created \(i.e. a list item), it doesn't apply past the first
3411 headline encountered.
3413 Optional argument MINLEVEL, when non-nil, is an integer
3414 specifying the level that any top-level headline in the included
3415 file should have.
3417 Optional argument ID is an integer that will be inserted before
3418 each footnote definition and reference if FILE is an Org file.
3419 This is useful to avoid conflicts when more than one Org file
3420 with footnotes is included in a document."
3421 (with-temp-buffer
3422 (insert-file-contents file)
3423 (when lines
3424 (let* ((lines (split-string lines "-"))
3425 (lbeg (string-to-number (car lines)))
3426 (lend (string-to-number (cadr lines)))
3427 (beg (if (zerop lbeg) (point-min)
3428 (goto-char (point-min))
3429 (forward-line (1- lbeg))
3430 (point)))
3431 (end (if (zerop lend) (point-max)
3432 (goto-char (point-min))
3433 (forward-line (1- lend))
3434 (point))))
3435 (narrow-to-region beg end)))
3436 ;; Remove blank lines at beginning and end of contents. The logic
3437 ;; behind that removal is that blank lines around include keyword
3438 ;; override blank lines in included file.
3439 (goto-char (point-min))
3440 (org-skip-whitespace)
3441 (beginning-of-line)
3442 (delete-region (point-min) (point))
3443 (goto-char (point-max))
3444 (skip-chars-backward " \r\t\n")
3445 (forward-line)
3446 (delete-region (point) (point-max))
3447 ;; If IND is set, preserve indentation of include keyword until
3448 ;; the first headline encountered.
3449 (when ind
3450 (unless (eq major-mode 'org-mode)
3451 (let ((org-inhibit-startup t)) (org-mode)))
3452 (goto-char (point-min))
3453 (let ((ind-str (make-string ind ? )))
3454 (while (not (or (eobp) (looking-at org-outline-regexp-bol)))
3455 ;; Do not move footnote definitions out of column 0.
3456 (unless (and (looking-at org-footnote-definition-re)
3457 (eq (org-element-type (org-element-at-point))
3458 'footnote-definition))
3459 (insert ind-str))
3460 (forward-line))))
3461 ;; When MINLEVEL is specified, compute minimal level for headlines
3462 ;; in the file (CUR-MIN), and remove stars to each headline so
3463 ;; that headlines with minimal level have a level of MINLEVEL.
3464 (when minlevel
3465 (unless (eq major-mode 'org-mode)
3466 (let ((org-inhibit-startup t)) (org-mode)))
3467 (org-with-limited-levels
3468 (let ((levels (org-map-entries
3469 (lambda () (org-reduced-level (org-current-level))))))
3470 (when levels
3471 (let ((offset (- minlevel (apply 'min levels))))
3472 (unless (zerop offset)
3473 (when org-odd-levels-only (setq offset (* offset 2)))
3474 ;; Only change stars, don't bother moving whole
3475 ;; sections.
3476 (org-map-entries
3477 (lambda () (if (< offset 0) (delete-char (abs offset))
3478 (insert (make-string offset ?*)))))))))))
3479 ;; Append ID to all footnote references and definitions, so they
3480 ;; become file specific and cannot collide with footnotes in other
3481 ;; included files.
3482 (when id
3483 (goto-char (point-min))
3484 (while (re-search-forward org-footnote-re nil t)
3485 (let ((reference (org-element-context)))
3486 (when (memq (org-element-type reference)
3487 '(footnote-reference footnote-definition))
3488 (goto-char (org-element-property :begin reference))
3489 (forward-char)
3490 (let ((label (org-element-property :label reference)))
3491 (cond ((not label))
3492 ((org-string-match-p "\\`[0-9]+\\'" label)
3493 (insert (format "fn:%d-" id)))
3494 (t (forward-char 3) (insert (format "%d-" id)))))))))
3495 (org-element-normalize-string (buffer-string))))
3497 (defun org-export-execute-babel-code ()
3498 "Execute every Babel code in the visible part of current buffer."
3499 ;; Get a pristine copy of current buffer so Babel references can be
3500 ;; properly resolved.
3501 (let ((reference (org-export-copy-buffer)))
3502 (unwind-protect (org-babel-exp-process-buffer reference)
3503 (kill-buffer reference))))
3505 (defun org-export--copy-to-kill-ring-p ()
3506 "Return a non-nil value when output should be added to the kill ring.
3507 See also `org-export-copy-to-kill-ring'."
3508 (if (eq org-export-copy-to-kill-ring 'if-interactive)
3509 (not (or executing-kbd-macro noninteractive))
3510 (eq org-export-copy-to-kill-ring t)))
3514 ;;; Tools For Back-Ends
3516 ;; A whole set of tools is available to help build new exporters. Any
3517 ;; function general enough to have its use across many back-ends
3518 ;; should be added here.
3520 ;;;; For Affiliated Keywords
3522 ;; `org-export-read-attribute' reads a property from a given element
3523 ;; as a plist. It can be used to normalize affiliated keywords'
3524 ;; syntax.
3526 ;; Since captions can span over multiple lines and accept dual values,
3527 ;; their internal representation is a bit tricky. Therefore,
3528 ;; `org-export-get-caption' transparently returns a given element's
3529 ;; caption as a secondary string.
3531 (defun org-export-read-attribute (attribute element &optional property)
3532 "Turn ATTRIBUTE property from ELEMENT into a plist.
3534 When optional argument PROPERTY is non-nil, return the value of
3535 that property within attributes.
3537 This function assumes attributes are defined as \":keyword
3538 value\" pairs. It is appropriate for `:attr_html' like
3539 properties.
3541 All values will become strings except the empty string and
3542 \"nil\", which will become nil. Also, values containing only
3543 double quotes will be read as-is, which means that \"\" value
3544 will become the empty string."
3545 (let* ((prepare-value
3546 (lambda (str)
3547 (save-match-data
3548 (cond ((member str '(nil "" "nil")) nil)
3549 ((string-match "^\"\\(\"+\\)?\"$" str)
3550 (or (match-string 1 str) ""))
3551 (t str)))))
3552 (attributes
3553 (let ((value (org-element-property attribute element)))
3554 (when value
3555 (let ((s (mapconcat 'identity value " ")) result)
3556 (while (string-match
3557 "\\(?:^\\|[ \t]+\\)\\(:[-a-zA-Z0-9_]+\\)\\([ \t]+\\|$\\)"
3559 (let ((value (substring s 0 (match-beginning 0))))
3560 (push (funcall prepare-value value) result))
3561 (push (intern (match-string 1 s)) result)
3562 (setq s (substring s (match-end 0))))
3563 ;; Ignore any string before first property with `cdr'.
3564 (cdr (nreverse (cons (funcall prepare-value s) result))))))))
3565 (if property (plist-get attributes property) attributes)))
3567 (defun org-export-get-caption (element &optional shortp)
3568 "Return caption from ELEMENT as a secondary string.
3570 When optional argument SHORTP is non-nil, return short caption,
3571 as a secondary string, instead.
3573 Caption lines are separated by a white space."
3574 (let ((full-caption (org-element-property :caption element)) caption)
3575 (dolist (line full-caption (cdr caption))
3576 (let ((cap (funcall (if shortp 'cdr 'car) line)))
3577 (when cap
3578 (setq caption (nconc (list " ") (copy-sequence cap) caption)))))))
3581 ;;;; For Derived Back-ends
3583 ;; `org-export-with-backend' is a function allowing to locally use
3584 ;; another back-end to transcode some object or element. In a derived
3585 ;; back-end, it may be used as a fall-back function once all specific
3586 ;; cases have been treated.
3588 (defun org-export-with-backend (backend data &optional contents info)
3589 "Call a transcoder from BACKEND on DATA.
3590 BACKEND is an export back-end, as returned by, e.g.,
3591 `org-export-create-backend', or a symbol referring to
3592 a registered back-end. DATA is an Org element, object, secondary
3593 string or string. CONTENTS, when non-nil, is the transcoded
3594 contents of DATA element, as a string. INFO, when non-nil, is
3595 the communication channel used for export, as a plist."
3596 (when (symbolp backend) (setq backend (org-export-get-backend backend)))
3597 (org-export-barf-if-invalid-backend backend)
3598 (let ((type (org-element-type data)))
3599 (if (memq type '(nil org-data)) (error "No foreign transcoder available")
3600 (let* ((all-transcoders (org-export-get-all-transcoders backend))
3601 (transcoder (cdr (assq type all-transcoders))))
3602 (if (not (functionp transcoder))
3603 (error "No foreign transcoder available")
3604 (funcall
3605 transcoder data contents
3606 (org-combine-plists
3607 info (list :back-end backend
3608 :translate-alist all-transcoders
3609 :exported-data (make-hash-table :test 'eq :size 401)))))))))
3612 ;;;; For Export Snippets
3614 ;; Every export snippet is transmitted to the back-end. Though, the
3615 ;; latter will only retain one type of export-snippet, ignoring
3616 ;; others, based on the former's target back-end. The function
3617 ;; `org-export-snippet-backend' returns that back-end for a given
3618 ;; export-snippet.
3620 (defun org-export-snippet-backend (export-snippet)
3621 "Return EXPORT-SNIPPET targeted back-end as a symbol.
3622 Translation, with `org-export-snippet-translation-alist', is
3623 applied."
3624 (let ((back-end (org-element-property :back-end export-snippet)))
3625 (intern
3626 (or (cdr (assoc back-end org-export-snippet-translation-alist))
3627 back-end))))
3630 ;;;; For Footnotes
3632 ;; `org-export-collect-footnote-definitions' is a tool to list
3633 ;; actually used footnotes definitions in the whole parse tree, or in
3634 ;; a headline, in order to add footnote listings throughout the
3635 ;; transcoded data.
3637 ;; `org-export-footnote-first-reference-p' is a predicate used by some
3638 ;; back-ends, when they need to attach the footnote definition only to
3639 ;; the first occurrence of the corresponding label.
3641 ;; `org-export-get-footnote-definition' and
3642 ;; `org-export-get-footnote-number' provide easier access to
3643 ;; additional information relative to a footnote reference.
3645 (defun org-export-collect-footnote-definitions (data info)
3646 "Return an alist between footnote numbers, labels and definitions.
3648 DATA is the parse tree from which definitions are collected.
3649 INFO is the plist used as a communication channel.
3651 Definitions are sorted by order of references. They either
3652 appear as Org data or as a secondary string for inlined
3653 footnotes. Unreferenced definitions are ignored."
3654 (let* (num-alist
3655 collect-fn ; for byte-compiler.
3656 (collect-fn
3657 (function
3658 (lambda (data)
3659 ;; Collect footnote number, label and definition in DATA.
3660 (org-element-map data 'footnote-reference
3661 (lambda (fn)
3662 (when (org-export-footnote-first-reference-p fn info)
3663 (let ((def (org-export-get-footnote-definition fn info)))
3664 (push
3665 (list (org-export-get-footnote-number fn info)
3666 (org-element-property :label fn)
3667 def)
3668 num-alist)
3669 ;; Also search in definition for nested footnotes.
3670 (when (eq (org-element-property :type fn) 'standard)
3671 (funcall collect-fn def)))))
3672 ;; Don't enter footnote definitions since it will happen
3673 ;; when their first reference is found.
3674 info nil 'footnote-definition)))))
3675 (funcall collect-fn (plist-get info :parse-tree))
3676 (reverse num-alist)))
3678 (defun org-export-footnote-first-reference-p (footnote-reference info)
3679 "Non-nil when a footnote reference is the first one for its label.
3681 FOOTNOTE-REFERENCE is the footnote reference being considered.
3682 INFO is the plist used as a communication channel."
3683 (let ((label (org-element-property :label footnote-reference)))
3684 ;; Anonymous footnotes are always a first reference.
3685 (if (not label) t
3686 ;; Otherwise, return the first footnote with the same LABEL and
3687 ;; test if it is equal to FOOTNOTE-REFERENCE.
3688 (let* (search-refs ; for byte-compiler.
3689 (search-refs
3690 (function
3691 (lambda (data)
3692 (org-element-map data 'footnote-reference
3693 (lambda (fn)
3694 (cond
3695 ((string= (org-element-property :label fn) label)
3696 (throw 'exit fn))
3697 ;; If FN isn't inlined, be sure to traverse its
3698 ;; definition before resuming search. See
3699 ;; comments in `org-export-get-footnote-number'
3700 ;; for more information.
3701 ((eq (org-element-property :type fn) 'standard)
3702 (funcall search-refs
3703 (org-export-get-footnote-definition fn info)))))
3704 ;; Don't enter footnote definitions since it will
3705 ;; happen when their first reference is found.
3706 info 'first-match 'footnote-definition)))))
3707 (eq (catch 'exit (funcall search-refs (plist-get info :parse-tree)))
3708 footnote-reference)))))
3710 (defun org-export-get-footnote-definition (footnote-reference info)
3711 "Return definition of FOOTNOTE-REFERENCE as parsed data.
3712 INFO is the plist used as a communication channel. If no such
3713 definition can be found, return \"DEFINITION NOT FOUND\"."
3714 (let ((label (org-element-property :label footnote-reference)))
3715 (or (if label
3716 (cdr (assoc label (plist-get info :footnote-definition-alist)))
3717 (org-element-contents footnote-reference))
3718 "DEFINITION NOT FOUND.")))
3720 (defun org-export-get-footnote-number (footnote info)
3721 "Return number associated to a footnote.
3723 FOOTNOTE is either a footnote reference or a footnote definition.
3724 INFO is the plist used as a communication channel."
3725 (let* ((label (org-element-property :label footnote))
3726 seen-refs
3727 search-ref ; For byte-compiler.
3728 (search-ref
3729 (function
3730 (lambda (data)
3731 ;; Search footnote references through DATA, filling
3732 ;; SEEN-REFS along the way.
3733 (org-element-map data 'footnote-reference
3734 (lambda (fn)
3735 (let ((fn-lbl (org-element-property :label fn)))
3736 (cond
3737 ;; Anonymous footnote match: return number.
3738 ((and (not fn-lbl) (eq fn footnote))
3739 (throw 'exit (1+ (length seen-refs))))
3740 ;; Labels match: return number.
3741 ((and label (string= label fn-lbl))
3742 (throw 'exit (1+ (length seen-refs))))
3743 ;; Anonymous footnote: it's always a new one.
3744 ;; Also, be sure to return nil from the `cond' so
3745 ;; `first-match' doesn't get us out of the loop.
3746 ((not fn-lbl) (push 'inline seen-refs) nil)
3747 ;; Label not seen so far: add it so SEEN-REFS.
3749 ;; Also search for subsequent references in
3750 ;; footnote definition so numbering follows
3751 ;; reading logic. Note that we don't have to care
3752 ;; about inline definitions, since
3753 ;; `org-element-map' already traverses them at the
3754 ;; right time.
3756 ;; Once again, return nil to stay in the loop.
3757 ((not (member fn-lbl seen-refs))
3758 (push fn-lbl seen-refs)
3759 (funcall search-ref
3760 (org-export-get-footnote-definition fn info))
3761 nil))))
3762 ;; Don't enter footnote definitions since it will
3763 ;; happen when their first reference is found.
3764 info 'first-match 'footnote-definition)))))
3765 (catch 'exit (funcall search-ref (plist-get info :parse-tree)))))
3768 ;;;; For Headlines
3770 ;; `org-export-get-relative-level' is a shortcut to get headline
3771 ;; level, relatively to the lower headline level in the parsed tree.
3773 ;; `org-export-get-headline-number' returns the section number of an
3774 ;; headline, while `org-export-number-to-roman' allows to convert it
3775 ;; to roman numbers.
3777 ;; `org-export-low-level-p', `org-export-first-sibling-p' and
3778 ;; `org-export-last-sibling-p' are three useful predicates when it
3779 ;; comes to fulfill the `:headline-levels' property.
3781 ;; `org-export-get-tags', `org-export-get-category' and
3782 ;; `org-export-get-node-property' extract useful information from an
3783 ;; headline or a parent headline. They all handle inheritance.
3785 ;; `org-export-get-alt-title' tries to retrieve an alternative title,
3786 ;; as a secondary string, suitable for table of contents. It falls
3787 ;; back onto default title.
3789 (defun org-export-get-relative-level (headline info)
3790 "Return HEADLINE relative level within current parsed tree.
3791 INFO is a plist holding contextual information."
3792 (+ (org-element-property :level headline)
3793 (or (plist-get info :headline-offset) 0)))
3795 (defun org-export-low-level-p (headline info)
3796 "Non-nil when HEADLINE is considered as low level.
3798 INFO is a plist used as a communication channel.
3800 A low level headlines has a relative level greater than
3801 `:headline-levels' property value.
3803 Return value is the difference between HEADLINE relative level
3804 and the last level being considered as high enough, or nil."
3805 (let ((limit (plist-get info :headline-levels)))
3806 (when (wholenump limit)
3807 (let ((level (org-export-get-relative-level headline info)))
3808 (and (> level limit) (- level limit))))))
3810 (defun org-export-get-headline-number (headline info)
3811 "Return HEADLINE numbering as a list of numbers.
3812 INFO is a plist holding contextual information."
3813 (cdr (assoc headline (plist-get info :headline-numbering))))
3815 (defun org-export-numbered-headline-p (headline info)
3816 "Return a non-nil value if HEADLINE element should be numbered.
3817 INFO is a plist used as a communication channel."
3818 (let ((sec-num (plist-get info :section-numbers))
3819 (level (org-export-get-relative-level headline info)))
3820 (if (wholenump sec-num) (<= level sec-num) sec-num)))
3822 (defun org-export-number-to-roman (n)
3823 "Convert integer N into a roman numeral."
3824 (let ((roman '((1000 . "M") (900 . "CM") (500 . "D") (400 . "CD")
3825 ( 100 . "C") ( 90 . "XC") ( 50 . "L") ( 40 . "XL")
3826 ( 10 . "X") ( 9 . "IX") ( 5 . "V") ( 4 . "IV")
3827 ( 1 . "I")))
3828 (res ""))
3829 (if (<= n 0)
3830 (number-to-string n)
3831 (while roman
3832 (if (>= n (caar roman))
3833 (setq n (- n (caar roman))
3834 res (concat res (cdar roman)))
3835 (pop roman)))
3836 res)))
3838 (defun org-export-get-tags (element info &optional tags inherited)
3839 "Return list of tags associated to ELEMENT.
3841 ELEMENT has either an `headline' or an `inlinetask' type. INFO
3842 is a plist used as a communication channel.
3844 Select tags (see `org-export-select-tags') and exclude tags (see
3845 `org-export-exclude-tags') are removed from the list.
3847 When non-nil, optional argument TAGS should be a list of strings.
3848 Any tag belonging to this list will also be removed.
3850 When optional argument INHERITED is non-nil, tags can also be
3851 inherited from parent headlines and FILETAGS keywords."
3852 (org-remove-if
3853 (lambda (tag) (or (member tag (plist-get info :select-tags))
3854 (member tag (plist-get info :exclude-tags))
3855 (member tag tags)))
3856 (if (not inherited) (org-element-property :tags element)
3857 ;; Build complete list of inherited tags.
3858 (let ((current-tag-list (org-element-property :tags element)))
3859 (mapc
3860 (lambda (parent)
3861 (mapc
3862 (lambda (tag)
3863 (when (and (memq (org-element-type parent) '(headline inlinetask))
3864 (not (member tag current-tag-list)))
3865 (push tag current-tag-list)))
3866 (org-element-property :tags parent)))
3867 (org-export-get-genealogy element))
3868 ;; Add FILETAGS keywords and return results.
3869 (org-uniquify (append (plist-get info :filetags) current-tag-list))))))
3871 (defun org-export-get-node-property (property blob &optional inherited)
3872 "Return node PROPERTY value for BLOB.
3874 PROPERTY is an upcase symbol (i.e. `:COOKIE_DATA'). BLOB is an
3875 element or object.
3877 If optional argument INHERITED is non-nil, the value can be
3878 inherited from a parent headline.
3880 Return value is a string or nil."
3881 (let ((headline (if (eq (org-element-type blob) 'headline) blob
3882 (org-export-get-parent-headline blob))))
3883 (if (not inherited) (org-element-property property blob)
3884 (let ((parent headline) value)
3885 (catch 'found
3886 (while parent
3887 (when (plist-member (nth 1 parent) property)
3888 (throw 'found (org-element-property property parent)))
3889 (setq parent (org-element-property :parent parent))))))))
3891 (defun org-export-get-category (blob info)
3892 "Return category for element or object BLOB.
3894 INFO is a plist used as a communication channel.
3896 CATEGORY is automatically inherited from a parent headline, from
3897 #+CATEGORY: keyword or created out of original file name. If all
3898 fail, the fall-back value is \"???\"."
3899 (or (org-export-get-node-property :CATEGORY blob t)
3900 (org-element-map (plist-get info :parse-tree) 'keyword
3901 (lambda (kwd)
3902 (when (equal (org-element-property :key kwd) "CATEGORY")
3903 (org-element-property :value kwd)))
3904 info 'first-match)
3905 (let ((file (plist-get info :input-file)))
3906 (and file (file-name-sans-extension (file-name-nondirectory file))))
3907 "???"))
3909 (defun org-export-get-alt-title (headline info)
3910 "Return alternative title for HEADLINE, as a secondary string.
3911 INFO is a plist used as a communication channel. If no optional
3912 title is defined, fall-back to the regular title."
3913 (or (org-element-property :alt-title headline)
3914 (org-element-property :title headline)))
3916 (defun org-export-first-sibling-p (blob info)
3917 "Non-nil when BLOB is the first sibling in its parent.
3918 BLOB is an element or an object. If BLOB is a headline, non-nil
3919 means it is the first sibling in the sub-tree. INFO is a plist
3920 used as a communication channel."
3921 (memq (org-element-type (org-export-get-previous-element blob info))
3922 '(nil section)))
3924 (defun org-export-last-sibling-p (blob info)
3925 "Non-nil when BLOB is the last sibling in its parent.
3926 BLOB is an element or an object. INFO is a plist used as
3927 a communication channel."
3928 (not (org-export-get-next-element blob info)))
3931 ;;;; For Keywords
3933 ;; `org-export-get-date' returns a date appropriate for the document
3934 ;; to about to be exported. In particular, it takes care of
3935 ;; `org-export-date-timestamp-format'.
3937 (defun org-export-get-date (info &optional fmt)
3938 "Return date value for the current document.
3940 INFO is a plist used as a communication channel. FMT, when
3941 non-nil, is a time format string that will be applied on the date
3942 if it consists in a single timestamp object. It defaults to
3943 `org-export-date-timestamp-format' when nil.
3945 A proper date can be a secondary string, a string or nil. It is
3946 meant to be translated with `org-export-data' or alike."
3947 (let ((date (plist-get info :date))
3948 (fmt (or fmt org-export-date-timestamp-format)))
3949 (cond ((not date) nil)
3950 ((and fmt
3951 (not (cdr date))
3952 (eq (org-element-type (car date)) 'timestamp))
3953 (org-timestamp-format (car date) fmt))
3954 (t date))))
3957 ;;;; For Links
3959 ;; `org-export-solidify-link-text' turns a string into a safer version
3960 ;; for links, replacing most non-standard characters with hyphens.
3962 ;; `org-export-get-coderef-format' returns an appropriate format
3963 ;; string for coderefs.
3965 ;; `org-export-inline-image-p' returns a non-nil value when the link
3966 ;; provided should be considered as an inline image.
3968 ;; `org-export-resolve-fuzzy-link' searches destination of fuzzy links
3969 ;; (i.e. links with "fuzzy" as type) within the parsed tree, and
3970 ;; returns an appropriate unique identifier when found, or nil.
3972 ;; `org-export-resolve-id-link' returns the first headline with
3973 ;; specified id or custom-id in parse tree, the path to the external
3974 ;; file with the id or nil when neither was found.
3976 ;; `org-export-resolve-coderef' associates a reference to a line
3977 ;; number in the element it belongs, or returns the reference itself
3978 ;; when the element isn't numbered.
3980 (defun org-export-solidify-link-text (s)
3981 "Take link text S and make a safe target out of it."
3982 (save-match-data
3983 (mapconcat 'identity (org-split-string s "[^a-zA-Z0-9_.-:]+") "-")))
3985 (defun org-export-get-coderef-format (path desc)
3986 "Return format string for code reference link.
3987 PATH is the link path. DESC is its description."
3988 (save-match-data
3989 (cond ((not desc) "%s")
3990 ((string-match (regexp-quote (concat "(" path ")")) desc)
3991 (replace-match "%s" t t desc))
3992 (t desc))))
3994 (defun org-export-inline-image-p (link &optional rules)
3995 "Non-nil if LINK object points to an inline image.
3997 Optional argument is a set of RULES defining inline images. It
3998 is an alist where associations have the following shape:
4000 \(TYPE . REGEXP)
4002 Applying a rule means apply REGEXP against LINK's path when its
4003 type is TYPE. The function will return a non-nil value if any of
4004 the provided rules is non-nil. The default rule is
4005 `org-export-default-inline-image-rule'.
4007 This only applies to links without a description."
4008 (and (not (org-element-contents link))
4009 (let ((case-fold-search t)
4010 (rules (or rules org-export-default-inline-image-rule)))
4011 (catch 'exit
4012 (mapc
4013 (lambda (rule)
4014 (and (string= (org-element-property :type link) (car rule))
4015 (string-match (cdr rule)
4016 (org-element-property :path link))
4017 (throw 'exit t)))
4018 rules)
4019 ;; Return nil if no rule matched.
4020 nil))))
4022 (defun org-export-resolve-coderef (ref info)
4023 "Resolve a code reference REF.
4025 INFO is a plist used as a communication channel.
4027 Return associated line number in source code, or REF itself,
4028 depending on src-block or example element's switches."
4029 (org-element-map (plist-get info :parse-tree) '(example-block src-block)
4030 (lambda (el)
4031 (with-temp-buffer
4032 (insert (org-trim (org-element-property :value el)))
4033 (let* ((label-fmt (regexp-quote
4034 (or (org-element-property :label-fmt el)
4035 org-coderef-label-format)))
4036 (ref-re
4037 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
4038 (replace-regexp-in-string "%s" ref label-fmt nil t))))
4039 ;; Element containing REF is found. Resolve it to either
4040 ;; a label or a line number, as needed.
4041 (when (re-search-backward ref-re nil t)
4042 (cond
4043 ((org-element-property :use-labels el) ref)
4044 ((eq (org-element-property :number-lines el) 'continued)
4045 (+ (org-export-get-loc el info) (line-number-at-pos)))
4046 (t (line-number-at-pos)))))))
4047 info 'first-match))
4049 (defun org-export-resolve-fuzzy-link (link info)
4050 "Return LINK destination.
4052 INFO is a plist holding contextual information.
4054 Return value can be an object, an element, or nil:
4056 - If LINK path matches a target object (i.e. <<path>>) return it.
4058 - If LINK path exactly matches the name affiliated keyword
4059 \(i.e. #+NAME: path) of an element, return that element.
4061 - If LINK path exactly matches any headline name, return that
4062 element. If more than one headline share that name, priority
4063 will be given to the one with the closest common ancestor, if
4064 any, or the first one in the parse tree otherwise.
4066 - Otherwise, return nil.
4068 Assume LINK type is \"fuzzy\". White spaces are not
4069 significant."
4070 (let* ((raw-path (org-element-property :path link))
4071 (match-title-p (eq (aref raw-path 0) ?*))
4072 ;; Split PATH at white spaces so matches are space
4073 ;; insensitive.
4074 (path (org-split-string
4075 (if match-title-p (substring raw-path 1) raw-path)))
4076 ;; Cache for destinations that are not position dependent.
4077 (link-cache
4078 (or (plist-get info :resolve-fuzzy-link-cache)
4079 (plist-get (setq info (plist-put info :resolve-fuzzy-link-cache
4080 (make-hash-table :test 'equal)))
4081 :resolve-fuzzy-link-cache)))
4082 (cached (gethash path link-cache 'not-found)))
4083 (cond
4084 ;; Destination is not position dependent: use cached value.
4085 ((and (not match-title-p) (not (eq cached 'not-found))) cached)
4086 ;; First try to find a matching "<<path>>" unless user specified
4087 ;; he was looking for a headline (path starts with a "*"
4088 ;; character).
4089 ((and (not match-title-p)
4090 (let ((match (org-element-map (plist-get info :parse-tree) 'target
4091 (lambda (blob)
4092 (and (equal (org-split-string
4093 (org-element-property :value blob))
4094 path)
4095 blob))
4096 info 'first-match)))
4097 (and match (puthash path match link-cache)))))
4098 ;; Then try to find an element with a matching "#+NAME: path"
4099 ;; affiliated keyword.
4100 ((and (not match-title-p)
4101 (let ((match (org-element-map (plist-get info :parse-tree)
4102 org-element-all-elements
4103 (lambda (el)
4104 (let ((name (org-element-property :name el)))
4105 (when (and name
4106 (equal (org-split-string name) path))
4107 el)))
4108 info 'first-match)))
4109 (and match (puthash path match link-cache)))))
4110 ;; Last case: link either points to a headline or to nothingness.
4111 ;; Try to find the source, with priority given to headlines with
4112 ;; the closest common ancestor. If such candidate is found,
4113 ;; return it, otherwise return nil.
4115 (let ((find-headline
4116 (function
4117 ;; Return first headline whose `:raw-value' property is
4118 ;; NAME in parse tree DATA, or nil. Statistics cookies
4119 ;; are ignored.
4120 (lambda (name data)
4121 (org-element-map data 'headline
4122 (lambda (headline)
4123 (when (equal (org-split-string
4124 (replace-regexp-in-string
4125 "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
4126 (org-element-property :raw-value headline)))
4127 name)
4128 headline))
4129 info 'first-match)))))
4130 ;; Search among headlines sharing an ancestor with link, from
4131 ;; closest to farthest.
4132 (catch 'exit
4133 (mapc
4134 (lambda (parent)
4135 (let ((foundp (funcall find-headline path parent)))
4136 (when foundp (throw 'exit foundp))))
4137 (let ((parent-hl (org-export-get-parent-headline link)))
4138 (if (not parent-hl) (list (plist-get info :parse-tree))
4139 (cons parent-hl (org-export-get-genealogy parent-hl)))))
4140 ;; No destination found: return nil.
4141 (and (not match-title-p) (puthash path nil link-cache))))))))
4143 (defun org-export-resolve-id-link (link info)
4144 "Return headline referenced as LINK destination.
4146 INFO is a plist used as a communication channel.
4148 Return value can be the headline element matched in current parse
4149 tree, a file name or nil. Assume LINK type is either \"id\" or
4150 \"custom-id\"."
4151 (let ((id (org-element-property :path link)))
4152 ;; First check if id is within the current parse tree.
4153 (or (org-element-map (plist-get info :parse-tree) 'headline
4154 (lambda (headline)
4155 (when (or (string= (org-element-property :ID headline) id)
4156 (string= (org-element-property :CUSTOM_ID headline) id))
4157 headline))
4158 info 'first-match)
4159 ;; Otherwise, look for external files.
4160 (cdr (assoc id (plist-get info :id-alist))))))
4162 (defun org-export-resolve-radio-link (link info)
4163 "Return radio-target object referenced as LINK destination.
4165 INFO is a plist used as a communication channel.
4167 Return value can be a radio-target object or nil. Assume LINK
4168 has type \"radio\"."
4169 (let ((path (replace-regexp-in-string
4170 "[ \r\t\n]+" " " (org-element-property :path link))))
4171 (org-element-map (plist-get info :parse-tree) 'radio-target
4172 (lambda (radio)
4173 (and (eq (compare-strings
4174 (replace-regexp-in-string
4175 "[ \r\t\n]+" " " (org-element-property :value radio))
4176 nil nil path nil nil t)
4178 radio))
4179 info 'first-match)))
4182 ;;;; For References
4184 ;; `org-export-get-ordinal' associates a sequence number to any object
4185 ;; or element.
4187 (defun org-export-get-ordinal (element info &optional types predicate)
4188 "Return ordinal number of an element or object.
4190 ELEMENT is the element or object considered. INFO is the plist
4191 used as a communication channel.
4193 Optional argument TYPES, when non-nil, is a list of element or
4194 object types, as symbols, that should also be counted in.
4195 Otherwise, only provided element's type is considered.
4197 Optional argument PREDICATE is a function returning a non-nil
4198 value if the current element or object should be counted in. It
4199 accepts two arguments: the element or object being considered and
4200 the plist used as a communication channel. This allows to count
4201 only a certain type of objects (i.e. inline images).
4203 Return value is a list of numbers if ELEMENT is a headline or an
4204 item. It is nil for keywords. It represents the footnote number
4205 for footnote definitions and footnote references. If ELEMENT is
4206 a target, return the same value as if ELEMENT was the closest
4207 table, item or headline containing the target. In any other
4208 case, return the sequence number of ELEMENT among elements or
4209 objects of the same type."
4210 ;; Ordinal of a target object refer to the ordinal of the closest
4211 ;; table, item, or headline containing the object.
4212 (when (eq (org-element-type element) 'target)
4213 (setq element
4214 (loop for parent in (org-export-get-genealogy element)
4215 when
4216 (memq
4217 (org-element-type parent)
4218 '(footnote-definition footnote-reference headline item
4219 table))
4220 return parent)))
4221 (case (org-element-type element)
4222 ;; Special case 1: A headline returns its number as a list.
4223 (headline (org-export-get-headline-number element info))
4224 ;; Special case 2: An item returns its number as a list.
4225 (item (let ((struct (org-element-property :structure element)))
4226 (org-list-get-item-number
4227 (org-element-property :begin element)
4228 struct
4229 (org-list-prevs-alist struct)
4230 (org-list-parents-alist struct))))
4231 ((footnote-definition footnote-reference)
4232 (org-export-get-footnote-number element info))
4233 (otherwise
4234 (let ((counter 0))
4235 ;; Increment counter until ELEMENT is found again.
4236 (org-element-map (plist-get info :parse-tree)
4237 (or types (org-element-type element))
4238 (lambda (el)
4239 (cond
4240 ((eq element el) (1+ counter))
4241 ((not predicate) (incf counter) nil)
4242 ((funcall predicate el info) (incf counter) nil)))
4243 info 'first-match)))))
4245 ;;;; For Special Blocks
4247 ;; `org-export-raw-special-block-p' check if current special block is
4248 ;; an "export block", i.e., a block whose contents should be inserted
4249 ;; as-is in the output. This should generally be the first check to
4250 ;; do when handling special blocks in the export back-end.
4252 (defun org-export-raw-special-block-p (element info &optional no-inheritance)
4253 "Non-nil if ELEMENT is an export block relatively to current back-end.
4254 An export block is a special block whose contents should be
4255 included as-is in the final output. Such blocks are defined
4256 through :export-block property in `org-export-define-backend',
4257 which see."
4258 (and (eq (org-element-type element) 'special-block)
4259 (let ((type (org-element-property :type element))
4260 (b (plist-get info :back-end)))
4261 (if no-inheritance (member type (org-export-backend-blocks b))
4262 (while (and b (not (member type (org-export-backend-blocks b))))
4263 (setq b (org-export-get-backend (org-export-backend-parent b))))
4264 b))))
4267 ;;;; For Src Blocks
4269 ;; `org-export-get-loc' counts number of code lines accumulated in
4270 ;; src-block or example-block elements with a "+n" switch until
4271 ;; a given element, excluded. Note: "-n" switches reset that count.
4273 ;; `org-export-unravel-code' extracts source code (along with a code
4274 ;; references alist) from an `element-block' or `src-block' type
4275 ;; element.
4277 ;; `org-export-format-code' applies a formatting function to each line
4278 ;; of code, providing relative line number and code reference when
4279 ;; appropriate. Since it doesn't access the original element from
4280 ;; which the source code is coming, it expects from the code calling
4281 ;; it to know if lines should be numbered and if code references
4282 ;; should appear.
4284 ;; Eventually, `org-export-format-code-default' is a higher-level
4285 ;; function (it makes use of the two previous functions) which handles
4286 ;; line numbering and code references inclusion, and returns source
4287 ;; code in a format suitable for plain text or verbatim output.
4289 (defun org-export-get-loc (element info)
4290 "Return accumulated lines of code up to ELEMENT.
4292 INFO is the plist used as a communication channel.
4294 ELEMENT is excluded from count."
4295 (let ((loc 0))
4296 (org-element-map (plist-get info :parse-tree)
4297 `(src-block example-block ,(org-element-type element))
4298 (lambda (el)
4299 (cond
4300 ;; ELEMENT is reached: Quit the loop.
4301 ((eq el element))
4302 ;; Only count lines from src-block and example-block elements
4303 ;; with a "+n" or "-n" switch. A "-n" switch resets counter.
4304 ((not (memq (org-element-type el) '(src-block example-block))) nil)
4305 ((let ((linums (org-element-property :number-lines el)))
4306 (when linums
4307 ;; Accumulate locs or reset them.
4308 (let ((lines (org-count-lines
4309 (org-trim (org-element-property :value el)))))
4310 (setq loc (if (eq linums 'new) lines (+ loc lines))))))
4311 ;; Return nil to stay in the loop.
4312 nil)))
4313 info 'first-match)
4314 ;; Return value.
4315 loc))
4317 (defun org-export-unravel-code (element)
4318 "Clean source code and extract references out of it.
4320 ELEMENT has either a `src-block' an `example-block' type.
4322 Return a cons cell whose CAR is the source code, cleaned from any
4323 reference, protective commas and spurious indentation, and CDR is
4324 an alist between relative line number (integer) and name of code
4325 reference on that line (string)."
4326 (let* ((line 0) refs
4327 (value (org-element-property :value element))
4328 ;; Get code and clean it. Remove blank lines at its
4329 ;; beginning and end.
4330 (code (replace-regexp-in-string
4331 "\\`\\([ \t]*\n\\)+" ""
4332 (replace-regexp-in-string
4333 "\\([ \t]*\n\\)*[ \t]*\\'" "\n"
4334 (if (or org-src-preserve-indentation
4335 (org-element-property :preserve-indent element))
4336 value
4337 (org-element-remove-indentation value)))))
4338 ;; Get format used for references.
4339 (label-fmt (regexp-quote
4340 (or (org-element-property :label-fmt element)
4341 org-coderef-label-format)))
4342 ;; Build a regexp matching a loc with a reference.
4343 (with-ref-re
4344 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)[ \t]*\\)$"
4345 (replace-regexp-in-string
4346 "%s" "\\([-a-zA-Z0-9_ ]+\\)" label-fmt nil t))))
4347 ;; Return value.
4348 (cons
4349 ;; Code with references removed.
4350 (org-element-normalize-string
4351 (mapconcat
4352 (lambda (loc)
4353 (incf line)
4354 (if (not (string-match with-ref-re loc)) loc
4355 ;; Ref line: remove ref, and signal its position in REFS.
4356 (push (cons line (match-string 3 loc)) refs)
4357 (replace-match "" nil nil loc 1)))
4358 (org-split-string code "\n") "\n"))
4359 ;; Reference alist.
4360 refs)))
4362 (defun org-export-format-code (code fun &optional num-lines ref-alist)
4363 "Format CODE by applying FUN line-wise and return it.
4365 CODE is a string representing the code to format. FUN is
4366 a function. It must accept three arguments: a line of
4367 code (string), the current line number (integer) or nil and the
4368 reference associated to the current line (string) or nil.
4370 Optional argument NUM-LINES can be an integer representing the
4371 number of code lines accumulated until the current code. Line
4372 numbers passed to FUN will take it into account. If it is nil,
4373 FUN's second argument will always be nil. This number can be
4374 obtained with `org-export-get-loc' function.
4376 Optional argument REF-ALIST can be an alist between relative line
4377 number (i.e. ignoring NUM-LINES) and the name of the code
4378 reference on it. If it is nil, FUN's third argument will always
4379 be nil. It can be obtained through the use of
4380 `org-export-unravel-code' function."
4381 (let ((--locs (org-split-string code "\n"))
4382 (--line 0))
4383 (org-element-normalize-string
4384 (mapconcat
4385 (lambda (--loc)
4386 (incf --line)
4387 (let ((--ref (cdr (assq --line ref-alist))))
4388 (funcall fun --loc (and num-lines (+ num-lines --line)) --ref)))
4389 --locs "\n"))))
4391 (defun org-export-format-code-default (element info)
4392 "Return source code from ELEMENT, formatted in a standard way.
4394 ELEMENT is either a `src-block' or `example-block' element. INFO
4395 is a plist used as a communication channel.
4397 This function takes care of line numbering and code references
4398 inclusion. Line numbers, when applicable, appear at the
4399 beginning of the line, separated from the code by two white
4400 spaces. Code references, on the other hand, appear flushed to
4401 the right, separated by six white spaces from the widest line of
4402 code."
4403 ;; Extract code and references.
4404 (let* ((code-info (org-export-unravel-code element))
4405 (code (car code-info))
4406 (code-lines (org-split-string code "\n")))
4407 (if (null code-lines) ""
4408 (let* ((refs (and (org-element-property :retain-labels element)
4409 (cdr code-info)))
4410 ;; Handle line numbering.
4411 (num-start (case (org-element-property :number-lines element)
4412 (continued (org-export-get-loc element info))
4413 (new 0)))
4414 (num-fmt
4415 (and num-start
4416 (format "%%%ds "
4417 (length (number-to-string
4418 (+ (length code-lines) num-start))))))
4419 ;; Prepare references display, if required. Any reference
4420 ;; should start six columns after the widest line of code,
4421 ;; wrapped with parenthesis.
4422 (max-width
4423 (+ (apply 'max (mapcar 'length code-lines))
4424 (if (not num-start) 0 (length (format num-fmt num-start))))))
4425 (org-export-format-code
4426 code
4427 (lambda (loc line-num ref)
4428 (let ((number-str (and num-fmt (format num-fmt line-num))))
4429 (concat
4430 number-str
4432 (and ref
4433 (concat (make-string
4434 (- (+ 6 max-width)
4435 (+ (length loc) (length number-str))) ? )
4436 (format "(%s)" ref))))))
4437 num-start refs)))))
4440 ;;;; For Tables
4442 ;; `org-export-table-has-special-column-p' and and
4443 ;; `org-export-table-row-is-special-p' are predicates used to look for
4444 ;; meta-information about the table structure.
4446 ;; `org-table-has-header-p' tells when the rows before the first rule
4447 ;; should be considered as table's header.
4449 ;; `org-export-table-cell-width', `org-export-table-cell-alignment'
4450 ;; and `org-export-table-cell-borders' extract information from
4451 ;; a table-cell element.
4453 ;; `org-export-table-dimensions' gives the number on rows and columns
4454 ;; in the table, ignoring horizontal rules and special columns.
4455 ;; `org-export-table-cell-address', given a table-cell object, returns
4456 ;; the absolute address of a cell. On the other hand,
4457 ;; `org-export-get-table-cell-at' does the contrary.
4459 ;; `org-export-table-cell-starts-colgroup-p',
4460 ;; `org-export-table-cell-ends-colgroup-p',
4461 ;; `org-export-table-row-starts-rowgroup-p',
4462 ;; `org-export-table-row-ends-rowgroup-p',
4463 ;; `org-export-table-row-starts-header-p',
4464 ;; `org-export-table-row-ends-header-p' and
4465 ;; `org-export-table-row-in-header-p' indicate position of current row
4466 ;; or cell within the table.
4468 (defun org-export-table-has-special-column-p (table)
4469 "Non-nil when TABLE has a special column.
4470 All special columns will be ignored during export."
4471 ;; The table has a special column when every first cell of every row
4472 ;; has an empty value or contains a symbol among "/", "#", "!", "$",
4473 ;; "*" "_" and "^". Though, do not consider a first row containing
4474 ;; only empty cells as special.
4475 (let ((special-column-p 'empty))
4476 (catch 'exit
4477 (mapc
4478 (lambda (row)
4479 (when (eq (org-element-property :type row) 'standard)
4480 (let ((value (org-element-contents
4481 (car (org-element-contents row)))))
4482 (cond ((member value '(("/") ("#") ("!") ("$") ("*") ("_") ("^")))
4483 (setq special-column-p 'special))
4484 ((not value))
4485 (t (throw 'exit nil))))))
4486 (org-element-contents table))
4487 (eq special-column-p 'special))))
4489 (defun org-export-table-has-header-p (table info)
4490 "Non-nil when TABLE has a header.
4492 INFO is a plist used as a communication channel.
4494 A table has a header when it contains at least two row groups."
4495 (let ((cache (or (plist-get info :table-header-cache)
4496 (plist-get (setq info
4497 (plist-put info :table-header-cache
4498 (make-hash-table :test 'eq)))
4499 :table-header-cache))))
4500 (or (gethash table cache)
4501 (let ((rowgroup 1) row-flag)
4502 (puthash
4503 table
4504 (org-element-map table 'table-row
4505 (lambda (row)
4506 (cond
4507 ((> rowgroup 1) t)
4508 ((and row-flag (eq (org-element-property :type row) 'rule))
4509 (incf rowgroup) (setq row-flag nil))
4510 ((and (not row-flag) (eq (org-element-property :type row)
4511 'standard))
4512 (setq row-flag t) nil)))
4513 info 'first-match)
4514 cache)))))
4516 (defun org-export-table-row-is-special-p (table-row info)
4517 "Non-nil if TABLE-ROW is considered special.
4519 INFO is a plist used as the communication channel.
4521 All special rows will be ignored during export."
4522 (when (eq (org-element-property :type table-row) 'standard)
4523 (let ((first-cell (org-element-contents
4524 (car (org-element-contents table-row)))))
4525 ;; A row is special either when...
4527 ;; ... it starts with a field only containing "/",
4528 (equal first-cell '("/"))
4529 ;; ... the table contains a special column and the row start
4530 ;; with a marking character among, "^", "_", "$" or "!",
4531 (and (org-export-table-has-special-column-p
4532 (org-export-get-parent table-row))
4533 (member first-cell '(("^") ("_") ("$") ("!"))))
4534 ;; ... it contains only alignment cookies and empty cells.
4535 (let ((special-row-p 'empty))
4536 (catch 'exit
4537 (mapc
4538 (lambda (cell)
4539 (let ((value (org-element-contents cell)))
4540 ;; Since VALUE is a secondary string, the following
4541 ;; checks avoid expanding it with `org-export-data'.
4542 (cond ((not value))
4543 ((and (not (cdr value))
4544 (stringp (car value))
4545 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'"
4546 (car value)))
4547 (setq special-row-p 'cookie))
4548 (t (throw 'exit nil)))))
4549 (org-element-contents table-row))
4550 (eq special-row-p 'cookie)))))))
4552 (defun org-export-table-row-group (table-row info)
4553 "Return TABLE-ROW's group number, as an integer.
4555 INFO is a plist used as the communication channel.
4557 Return value is the group number, as an integer, or nil for
4558 special rows and rows separators. First group is also table's
4559 header."
4560 (let ((cache (or (plist-get info :table-row-group-cache)
4561 (plist-get (setq info
4562 (plist-put info :table-row-group-cache
4563 (make-hash-table :test 'eq)))
4564 :table-row-group-cache))))
4565 (cond ((gethash table-row cache))
4566 ((eq (org-element-property :type table-row) 'rule) nil)
4567 (t (let ((group 0) row-flag)
4568 (org-element-map (org-export-get-parent table-row) 'table-row
4569 (lambda (row)
4570 (if (eq (org-element-property :type row) 'rule)
4571 (setq row-flag nil)
4572 (unless row-flag (incf group) (setq row-flag t)))
4573 (when (eq table-row row) (puthash table-row group cache)))
4574 info 'first-match))))))
4576 (defun org-export-table-cell-width (table-cell info)
4577 "Return TABLE-CELL contents width.
4579 INFO is a plist used as the communication channel.
4581 Return value is the width given by the last width cookie in the
4582 same column as TABLE-CELL, or nil."
4583 (let* ((row (org-export-get-parent table-cell))
4584 (table (org-export-get-parent row))
4585 (cells (org-element-contents row))
4586 (columns (length cells))
4587 (column (- columns (length (memq table-cell cells))))
4588 (cache (or (plist-get info :table-cell-width-cache)
4589 (plist-get (setq info
4590 (plist-put info :table-cell-width-cache
4591 (make-hash-table :test 'eq)))
4592 :table-cell-width-cache)))
4593 (width-vector (or (gethash table cache)
4594 (puthash table (make-vector columns 'empty) cache)))
4595 (value (aref width-vector column)))
4596 (if (not (eq value 'empty)) value
4597 (let (cookie-width)
4598 (dolist (row (org-element-contents table)
4599 (aset width-vector column cookie-width))
4600 (when (org-export-table-row-is-special-p row info)
4601 ;; In a special row, try to find a width cookie at COLUMN.
4602 (let* ((value (org-element-contents
4603 (elt (org-element-contents row) column)))
4604 (cookie (car value)))
4605 ;; The following checks avoid expanding unnecessarily
4606 ;; the cell with `org-export-data'.
4607 (when (and value
4608 (not (cdr value))
4609 (stringp cookie)
4610 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'" cookie)
4611 (match-string 1 cookie))
4612 (setq cookie-width
4613 (string-to-number (match-string 1 cookie)))))))))))
4615 (defun org-export-table-cell-alignment (table-cell info)
4616 "Return TABLE-CELL contents alignment.
4618 INFO is a plist used as the communication channel.
4620 Return alignment as specified by the last alignment cookie in the
4621 same column as TABLE-CELL. If no such cookie is found, a default
4622 alignment value will be deduced from fraction of numbers in the
4623 column (see `org-table-number-fraction' for more information).
4624 Possible values are `left', `right' and `center'."
4625 ;; Load `org-table-number-fraction' and `org-table-number-regexp'.
4626 (require 'org-table)
4627 (let* ((row (org-export-get-parent table-cell))
4628 (table (org-export-get-parent row))
4629 (cells (org-element-contents row))
4630 (columns (length cells))
4631 (column (- columns (length (memq table-cell cells))))
4632 (cache (or (plist-get info :table-cell-alignment-cache)
4633 (plist-get (setq info
4634 (plist-put info :table-cell-alignment-cache
4635 (make-hash-table :test 'eq)))
4636 :table-cell-alignment-cache)))
4637 (align-vector (or (gethash table cache)
4638 (puthash table (make-vector columns nil) cache))))
4639 (or (aref align-vector column)
4640 (let ((number-cells 0)
4641 (total-cells 0)
4642 cookie-align
4643 previous-cell-number-p)
4644 (dolist (row (org-element-contents (org-export-get-parent row)))
4645 (cond
4646 ;; In a special row, try to find an alignment cookie at
4647 ;; COLUMN.
4648 ((org-export-table-row-is-special-p row info)
4649 (let ((value (org-element-contents
4650 (elt (org-element-contents row) column))))
4651 ;; Since VALUE is a secondary string, the following
4652 ;; checks avoid useless expansion through
4653 ;; `org-export-data'.
4654 (when (and value
4655 (not (cdr value))
4656 (stringp (car value))
4657 (string-match "\\`<\\([lrc]\\)?\\([0-9]+\\)?>\\'"
4658 (car value))
4659 (match-string 1 (car value)))
4660 (setq cookie-align (match-string 1 (car value))))))
4661 ;; Ignore table rules.
4662 ((eq (org-element-property :type row) 'rule))
4663 ;; In a standard row, check if cell's contents are
4664 ;; expressing some kind of number. Increase NUMBER-CELLS
4665 ;; accordingly. Though, don't bother if an alignment
4666 ;; cookie has already defined cell's alignment.
4667 ((not cookie-align)
4668 (let ((value (org-export-data
4669 (org-element-contents
4670 (elt (org-element-contents row) column))
4671 info)))
4672 (incf total-cells)
4673 ;; Treat an empty cell as a number if it follows
4674 ;; a number.
4675 (if (not (or (string-match org-table-number-regexp value)
4676 (and (string= value "") previous-cell-number-p)))
4677 (setq previous-cell-number-p nil)
4678 (setq previous-cell-number-p t)
4679 (incf number-cells))))))
4680 ;; Return value. Alignment specified by cookies has
4681 ;; precedence over alignment deduced from cell's contents.
4682 (aset align-vector
4683 column
4684 (cond ((equal cookie-align "l") 'left)
4685 ((equal cookie-align "r") 'right)
4686 ((equal cookie-align "c") 'center)
4687 ((>= (/ (float number-cells) total-cells)
4688 org-table-number-fraction)
4689 'right)
4690 (t 'left)))))))
4692 (defun org-export-table-cell-borders (table-cell info)
4693 "Return TABLE-CELL borders.
4695 INFO is a plist used as a communication channel.
4697 Return value is a list of symbols, or nil. Possible values are:
4698 `top', `bottom', `above', `below', `left' and `right'. Note:
4699 `top' (resp. `bottom') only happen for a cell in the first
4700 row (resp. last row) of the table, ignoring table rules, if any.
4702 Returned borders ignore special rows."
4703 (let* ((row (org-export-get-parent table-cell))
4704 (table (org-export-get-parent-table table-cell))
4705 borders)
4706 ;; Top/above border? TABLE-CELL has a border above when a rule
4707 ;; used to demarcate row groups can be found above. Hence,
4708 ;; finding a rule isn't sufficient to push `above' in BORDERS:
4709 ;; another regular row has to be found above that rule.
4710 (let (rule-flag)
4711 (catch 'exit
4712 (mapc (lambda (row)
4713 (cond ((eq (org-element-property :type row) 'rule)
4714 (setq rule-flag t))
4715 ((not (org-export-table-row-is-special-p row info))
4716 (if rule-flag (throw 'exit (push 'above borders))
4717 (throw 'exit nil)))))
4718 ;; Look at every row before the current one.
4719 (cdr (memq row (reverse (org-element-contents table)))))
4720 ;; No rule above, or rule found starts the table (ignoring any
4721 ;; special row): TABLE-CELL is at the top of the table.
4722 (when rule-flag (push 'above borders))
4723 (push 'top borders)))
4724 ;; Bottom/below border? TABLE-CELL has a border below when next
4725 ;; non-regular row below is a rule.
4726 (let (rule-flag)
4727 (catch 'exit
4728 (mapc (lambda (row)
4729 (cond ((eq (org-element-property :type row) 'rule)
4730 (setq rule-flag t))
4731 ((not (org-export-table-row-is-special-p row info))
4732 (if rule-flag (throw 'exit (push 'below borders))
4733 (throw 'exit nil)))))
4734 ;; Look at every row after the current one.
4735 (cdr (memq row (org-element-contents table))))
4736 ;; No rule below, or rule found ends the table (modulo some
4737 ;; special row): TABLE-CELL is at the bottom of the table.
4738 (when rule-flag (push 'below borders))
4739 (push 'bottom borders)))
4740 ;; Right/left borders? They can only be specified by column
4741 ;; groups. Column groups are defined in a row starting with "/".
4742 ;; Also a column groups row only contains "<", "<>", ">" or blank
4743 ;; cells.
4744 (catch 'exit
4745 (let ((column (let ((cells (org-element-contents row)))
4746 (- (length cells) (length (memq table-cell cells))))))
4747 (mapc
4748 (lambda (row)
4749 (unless (eq (org-element-property :type row) 'rule)
4750 (when (equal (org-element-contents
4751 (car (org-element-contents row)))
4752 '("/"))
4753 (let ((column-groups
4754 (mapcar
4755 (lambda (cell)
4756 (let ((value (org-element-contents cell)))
4757 (when (member value '(("<") ("<>") (">") nil))
4758 (car value))))
4759 (org-element-contents row))))
4760 ;; There's a left border when previous cell, if
4761 ;; any, ends a group, or current one starts one.
4762 (when (or (and (not (zerop column))
4763 (member (elt column-groups (1- column))
4764 '(">" "<>")))
4765 (member (elt column-groups column) '("<" "<>")))
4766 (push 'left borders))
4767 ;; There's a right border when next cell, if any,
4768 ;; starts a group, or current one ends one.
4769 (when (or (and (/= (1+ column) (length column-groups))
4770 (member (elt column-groups (1+ column))
4771 '("<" "<>")))
4772 (member (elt column-groups column) '(">" "<>")))
4773 (push 'right borders))
4774 (throw 'exit nil)))))
4775 ;; Table rows are read in reverse order so last column groups
4776 ;; row has precedence over any previous one.
4777 (reverse (org-element-contents table)))))
4778 ;; Return value.
4779 borders))
4781 (defun org-export-table-cell-starts-colgroup-p (table-cell info)
4782 "Non-nil when TABLE-CELL is at the beginning of a column group.
4783 INFO is a plist used as a communication channel."
4784 ;; A cell starts a column group either when it is at the beginning
4785 ;; of a row (or after the special column, if any) or when it has
4786 ;; a left border.
4787 (or (eq (org-element-map (org-export-get-parent table-cell) 'table-cell
4788 'identity info 'first-match)
4789 table-cell)
4790 (memq 'left (org-export-table-cell-borders table-cell info))))
4792 (defun org-export-table-cell-ends-colgroup-p (table-cell info)
4793 "Non-nil when TABLE-CELL is at the end of a column group.
4794 INFO is a plist used as a communication channel."
4795 ;; A cell ends a column group either when it is at the end of a row
4796 ;; or when it has a right border.
4797 (or (eq (car (last (org-element-contents
4798 (org-export-get-parent table-cell))))
4799 table-cell)
4800 (memq 'right (org-export-table-cell-borders table-cell info))))
4802 (defun org-export-table-row-starts-rowgroup-p (table-row info)
4803 "Non-nil when TABLE-ROW is at the beginning of a row group.
4804 INFO is a plist used as a communication channel."
4805 (unless (or (eq (org-element-property :type table-row) 'rule)
4806 (org-export-table-row-is-special-p table-row info))
4807 (let ((borders (org-export-table-cell-borders
4808 (car (org-element-contents table-row)) info)))
4809 (or (memq 'top borders) (memq 'above borders)))))
4811 (defun org-export-table-row-ends-rowgroup-p (table-row info)
4812 "Non-nil when TABLE-ROW is at the end of a row group.
4813 INFO is a plist used as a communication channel."
4814 (unless (or (eq (org-element-property :type table-row) 'rule)
4815 (org-export-table-row-is-special-p table-row info))
4816 (let ((borders (org-export-table-cell-borders
4817 (car (org-element-contents table-row)) info)))
4818 (or (memq 'bottom borders) (memq 'below borders)))))
4820 (defun org-export-table-row-in-header-p (table-row info)
4821 "Non-nil when TABLE-ROW is located within table's header.
4822 INFO is a plist used as a communication channel. Always return
4823 nil for special rows and rows separators."
4824 (and (org-export-table-has-header-p
4825 (org-export-get-parent-table table-row) info)
4826 (eql (org-export-table-row-group table-row info) 1)))
4828 (defun org-export-table-row-starts-header-p (table-row info)
4829 "Non-nil when TABLE-ROW is the first table header's row.
4830 INFO is a plist used as a communication channel."
4831 (and (org-export-table-row-in-header-p table-row info)
4832 (org-export-table-row-starts-rowgroup-p table-row info)))
4834 (defun org-export-table-row-ends-header-p (table-row info)
4835 "Non-nil when TABLE-ROW is the last table header's row.
4836 INFO is a plist used as a communication channel."
4837 (and (org-export-table-row-in-header-p table-row info)
4838 (org-export-table-row-ends-rowgroup-p table-row info)))
4840 (defun org-export-table-row-number (table-row info)
4841 "Return TABLE-ROW number.
4842 INFO is a plist used as a communication channel. Return value is
4843 zero-based and ignores separators. The function returns nil for
4844 special columns and separators."
4845 (when (and (eq (org-element-property :type table-row) 'standard)
4846 (not (org-export-table-row-is-special-p table-row info)))
4847 (let ((number 0))
4848 (org-element-map (org-export-get-parent-table table-row) 'table-row
4849 (lambda (row)
4850 (cond ((eq row table-row) number)
4851 ((eq (org-element-property :type row) 'standard)
4852 (incf number) nil)))
4853 info 'first-match))))
4855 (defun org-export-table-dimensions (table info)
4856 "Return TABLE dimensions.
4858 INFO is a plist used as a communication channel.
4860 Return value is a CONS like (ROWS . COLUMNS) where
4861 ROWS (resp. COLUMNS) is the number of exportable
4862 rows (resp. columns)."
4863 (let (first-row (columns 0) (rows 0))
4864 ;; Set number of rows, and extract first one.
4865 (org-element-map table 'table-row
4866 (lambda (row)
4867 (when (eq (org-element-property :type row) 'standard)
4868 (incf rows)
4869 (unless first-row (setq first-row row)))) info)
4870 ;; Set number of columns.
4871 (org-element-map first-row 'table-cell (lambda (cell) (incf columns)) info)
4872 ;; Return value.
4873 (cons rows columns)))
4875 (defun org-export-table-cell-address (table-cell info)
4876 "Return address of a regular TABLE-CELL object.
4878 TABLE-CELL is the cell considered. INFO is a plist used as
4879 a communication channel.
4881 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
4882 zero-based index. Only exportable cells are considered. The
4883 function returns nil for other cells."
4884 (let* ((table-row (org-export-get-parent table-cell))
4885 (row-number (org-export-table-row-number table-row info)))
4886 (when row-number
4887 (cons row-number
4888 (let ((col-count 0))
4889 (org-element-map table-row 'table-cell
4890 (lambda (cell)
4891 (if (eq cell table-cell) col-count (incf col-count) nil))
4892 info 'first-match))))))
4894 (defun org-export-get-table-cell-at (address table info)
4895 "Return regular table-cell object at ADDRESS in TABLE.
4897 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
4898 zero-based index. TABLE is a table type element. INFO is
4899 a plist used as a communication channel.
4901 If no table-cell, among exportable cells, is found at ADDRESS,
4902 return nil."
4903 (let ((column-pos (cdr address)) (column-count 0))
4904 (org-element-map
4905 ;; Row at (car address) or nil.
4906 (let ((row-pos (car address)) (row-count 0))
4907 (org-element-map table 'table-row
4908 (lambda (row)
4909 (cond ((eq (org-element-property :type row) 'rule) nil)
4910 ((= row-count row-pos) row)
4911 (t (incf row-count) nil)))
4912 info 'first-match))
4913 'table-cell
4914 (lambda (cell)
4915 (if (= column-count column-pos) cell
4916 (incf column-count) nil))
4917 info 'first-match)))
4920 ;;;; For Tables Of Contents
4922 ;; `org-export-collect-headlines' builds a list of all exportable
4923 ;; headline elements, maybe limited to a certain depth. One can then
4924 ;; easily parse it and transcode it.
4926 ;; Building lists of tables, figures or listings is quite similar.
4927 ;; Once the generic function `org-export-collect-elements' is defined,
4928 ;; `org-export-collect-tables', `org-export-collect-figures' and
4929 ;; `org-export-collect-listings' can be derived from it.
4931 (defun org-export-collect-headlines (info &optional n)
4932 "Collect headlines in order to build a table of contents.
4934 INFO is a plist used as a communication channel.
4936 When optional argument N is an integer, it specifies the depth of
4937 the table of contents. Otherwise, it is set to the value of the
4938 last headline level. See `org-export-headline-levels' for more
4939 information.
4941 Return a list of all exportable headlines as parsed elements.
4942 Footnote sections, if any, will be ignored."
4943 (let ((limit (plist-get info :headline-levels)))
4944 (setq n (if (wholenump n) (min n limit) limit))
4945 (org-element-map (plist-get info :parse-tree) 'headline
4946 #'(lambda (headline)
4947 (unless (org-element-property :footnote-section-p headline)
4948 (let ((level (org-export-get-relative-level headline info)))
4949 (and (<= level n) headline))))
4950 info)))
4952 (defun org-export-collect-elements (type info &optional predicate)
4953 "Collect referenceable elements of a determined type.
4955 TYPE can be a symbol or a list of symbols specifying element
4956 types to search. Only elements with a caption are collected.
4958 INFO is a plist used as a communication channel.
4960 When non-nil, optional argument PREDICATE is a function accepting
4961 one argument, an element of type TYPE. It returns a non-nil
4962 value when that element should be collected.
4964 Return a list of all elements found, in order of appearance."
4965 (org-element-map (plist-get info :parse-tree) type
4966 (lambda (element)
4967 (and (org-element-property :caption element)
4968 (or (not predicate) (funcall predicate element))
4969 element))
4970 info))
4972 (defun org-export-collect-tables (info)
4973 "Build a list of tables.
4974 INFO is a plist used as a communication channel.
4976 Return a list of table elements with a caption."
4977 (org-export-collect-elements 'table info))
4979 (defun org-export-collect-figures (info predicate)
4980 "Build a list of figures.
4982 INFO is a plist used as a communication channel. PREDICATE is
4983 a function which accepts one argument: a paragraph element and
4984 whose return value is non-nil when that element should be
4985 collected.
4987 A figure is a paragraph type element, with a caption, verifying
4988 PREDICATE. The latter has to be provided since a \"figure\" is
4989 a vague concept that may depend on back-end.
4991 Return a list of elements recognized as figures."
4992 (org-export-collect-elements 'paragraph info predicate))
4994 (defun org-export-collect-listings (info)
4995 "Build a list of src blocks.
4997 INFO is a plist used as a communication channel.
4999 Return a list of src-block elements with a caption."
5000 (org-export-collect-elements 'src-block info))
5003 ;;;; Smart Quotes
5005 ;; The main function for the smart quotes sub-system is
5006 ;; `org-export-activate-smart-quotes', which replaces every quote in
5007 ;; a given string from the parse tree with its "smart" counterpart.
5009 ;; Dictionary for smart quotes is stored in
5010 ;; `org-export-smart-quotes-alist'.
5012 ;; Internally, regexps matching potential smart quotes (checks at
5013 ;; string boundaries are also necessary) are defined in
5014 ;; `org-export-smart-quotes-regexps'.
5016 (defconst org-export-smart-quotes-alist
5017 '(("da"
5018 ;; one may use: »...«, "...", ›...‹, or '...'.
5019 ;; http://sproget.dk/raad-og-regler/retskrivningsregler/retskrivningsregler/a7-40-60/a7-58-anforselstegn/
5020 ;; LaTeX quotes require Babel!
5021 (opening-double-quote :utf-8 "»" :html "&raquo;" :latex ">>"
5022 :texinfo "@guillemetright{}")
5023 (closing-double-quote :utf-8 "«" :html "&laquo;" :latex "<<"
5024 :texinfo "@guillemetleft{}")
5025 (opening-single-quote :utf-8 "›" :html "&rsaquo;" :latex "\\frq{}"
5026 :texinfo "@guilsinglright{}")
5027 (closing-single-quote :utf-8 "‹" :html "&lsaquo;" :latex "\\flq{}"
5028 :texinfo "@guilsingleft{}")
5029 (apostrophe :utf-8 "’" :html "&rsquo;"))
5030 ("de"
5031 (opening-double-quote :utf-8 "„" :html "&bdquo;" :latex "\"`"
5032 :texinfo "@quotedblbase{}")
5033 (closing-double-quote :utf-8 "“" :html "&ldquo;" :latex "\"'"
5034 :texinfo "@quotedblleft{}")
5035 (opening-single-quote :utf-8 "‚" :html "&sbquo;" :latex "\\glq{}"
5036 :texinfo "@quotesinglbase{}")
5037 (closing-single-quote :utf-8 "‘" :html "&lsquo;" :latex "\\grq{}"
5038 :texinfo "@quoteleft{}")
5039 (apostrophe :utf-8 "’" :html "&rsquo;"))
5040 ("en"
5041 (opening-double-quote :utf-8 "“" :html "&ldquo;" :latex "``" :texinfo "``")
5042 (closing-double-quote :utf-8 "”" :html "&rdquo;" :latex "''" :texinfo "''")
5043 (opening-single-quote :utf-8 "‘" :html "&lsquo;" :latex "`" :texinfo "`")
5044 (closing-single-quote :utf-8 "’" :html "&rsquo;" :latex "'" :texinfo "'")
5045 (apostrophe :utf-8 "’" :html "&rsquo;"))
5046 ("es"
5047 (opening-double-quote :utf-8 "«" :html "&laquo;" :latex "\\guillemotleft{}"
5048 :texinfo "@guillemetleft{}")
5049 (closing-double-quote :utf-8 "»" :html "&raquo;" :latex "\\guillemotright{}"
5050 :texinfo "@guillemetright{}")
5051 (opening-single-quote :utf-8 "“" :html "&ldquo;" :latex "``" :texinfo "``")
5052 (closing-single-quote :utf-8 "”" :html "&rdquo;" :latex "''" :texinfo "''")
5053 (apostrophe :utf-8 "’" :html "&rsquo;"))
5054 ("fr"
5055 (opening-double-quote :utf-8 "« " :html "&laquo;&nbsp;" :latex "\\og "
5056 :texinfo "@guillemetleft{}@tie{}")
5057 (closing-double-quote :utf-8 " »" :html "&nbsp;&raquo;" :latex "\\fg{}"
5058 :texinfo "@tie{}@guillemetright{}")
5059 (opening-single-quote :utf-8 "« " :html "&laquo;&nbsp;" :latex "\\og "
5060 :texinfo "@guillemetleft{}@tie{}")
5061 (closing-single-quote :utf-8 " »" :html "&nbsp;&raquo;" :latex "\\fg{}"
5062 :texinfo "@tie{}@guillemetright{}")
5063 (apostrophe :utf-8 "’" :html "&rsquo;"))
5064 ("no"
5065 ;; https://nn.wikipedia.org/wiki/Sitatteikn
5066 (opening-double-quote :utf-8 "«" :html "&laquo;" :latex "\\guillemotleft{}"
5067 :texinfo "@guillemetleft{}")
5068 (closing-double-quote :utf-8 "»" :html "&raquo;" :latex "\\guillemotright{}"
5069 :texinfo "@guillemetright{}")
5070 (opening-single-quote :utf-8 "‘" :html "&lsquo;" :latex "`" :texinfo "`")
5071 (closing-single-quote :utf-8 "’" :html "&rsquo;" :latex "'" :texinfo "'")
5072 (apostrophe :utf-8 "’" :html "&rsquo;"))
5073 ("nb"
5074 ;; https://nn.wikipedia.org/wiki/Sitatteikn
5075 (opening-double-quote :utf-8 "«" :html "&laquo;" :latex "\\guillemotleft{}"
5076 :texinfo "@guillemetleft{}")
5077 (closing-double-quote :utf-8 "»" :html "&raquo;" :latex "\\guillemotright{}"
5078 :texinfo "@guillemetright{}")
5079 (opening-single-quote :utf-8 "‘" :html "&lsquo;" :latex "`" :texinfo "`")
5080 (closing-single-quote :utf-8 "’" :html "&rsquo;" :latex "'" :texinfo "'")
5081 (apostrophe :utf-8 "’" :html "&rsquo;"))
5082 ("nn"
5083 ;; https://nn.wikipedia.org/wiki/Sitatteikn
5084 (opening-double-quote :utf-8 "«" :html "&laquo;" :latex "\\guillemotleft{}"
5085 :texinfo "@guillemetleft{}")
5086 (closing-double-quote :utf-8 "»" :html "&raquo;" :latex "\\guillemotright{}"
5087 :texinfo "@guillemetright{}")
5088 (opening-single-quote :utf-8 "‘" :html "&lsquo;" :latex "`" :texinfo "`")
5089 (closing-single-quote :utf-8 "’" :html "&rsquo;" :latex "'" :texinfo "'")
5090 (apostrophe :utf-8 "’" :html "&rsquo;"))
5091 ("ru"
5092 ;; http://ru.wikipedia.org/wiki/%D0%9A%D0%B0%D0%B2%D1%8B%D1%87%D0%BA%D0%B8#.D0.9A.D0.B0.D0.B2.D1.8B.D1.87.D0.BA.D0.B8.2C_.D0.B8.D1.81.D0.BF.D0.BE.D0.BB.D1.8C.D0.B7.D1.83.D0.B5.D0.BC.D1.8B.D0.B5_.D0.B2_.D1.80.D1.83.D1.81.D1.81.D0.BA.D0.BE.D0.BC_.D1.8F.D0.B7.D1.8B.D0.BA.D0.B5
5093 ;; http://www.artlebedev.ru/kovodstvo/sections/104/
5094 (opening-double-quote :utf-8 "«" :html "&laquo;" :latex "{}<<"
5095 :texinfo "@guillemetleft{}")
5096 (closing-double-quote :utf-8 "»" :html "&raquo;" :latex ">>{}"
5097 :texinfo "@guillemetright{}")
5098 (opening-single-quote :utf-8 "„" :html "&bdquo;" :latex "\\glqq{}"
5099 :texinfo "@quotedblbase{}")
5100 (closing-single-quote :utf-8 "“" :html "&ldquo;" :latex "\\grqq{}"
5101 :texinfo "@quotedblleft{}")
5102 (apostrophe :utf-8 "’" :html: "&#39;"))
5103 ("sv"
5104 ;; based on https://sv.wikipedia.org/wiki/Citattecken
5105 (opening-double-quote :utf-8 "”" :html "&rdquo;" :latex "’’" :texinfo "’’")
5106 (closing-double-quote :utf-8 "”" :html "&rdquo;" :latex "’’" :texinfo "’’")
5107 (opening-single-quote :utf-8 "’" :html "&rsquo;" :latex "’" :texinfo "`")
5108 (closing-single-quote :utf-8 "’" :html "&rsquo;" :latex "’" :texinfo "'")
5109 (apostrophe :utf-8 "’" :html "&rsquo;"))
5111 "Smart quotes translations.
5113 Alist whose CAR is a language string and CDR is an alist with
5114 quote type as key and a plist associating various encodings to
5115 their translation as value.
5117 A quote type can be any symbol among `opening-double-quote',
5118 `closing-double-quote', `opening-single-quote',
5119 `closing-single-quote' and `apostrophe'.
5121 Valid encodings include `:utf-8', `:html', `:latex' and
5122 `:texinfo'.
5124 If no translation is found, the quote character is left as-is.")
5126 (defconst org-export-smart-quotes-regexps
5127 (list
5128 ;; Possible opening quote at beginning of string.
5129 "\\`\\([\"']\\)\\(\\w\\|\\s.\\|\\s_\\|\\s(\\)"
5130 ;; Possible closing quote at beginning of string.
5131 "\\`\\([\"']\\)\\(\\s-\\|\\s)\\|\\s.\\)"
5132 ;; Possible apostrophe at beginning of string.
5133 "\\`\\('\\)\\S-"
5134 ;; Opening single and double quotes.
5135 "\\(?:\\s-\\|\\s(\\)\\([\"']\\)\\(?:\\w\\|\\s.\\|\\s_\\)"
5136 ;; Closing single and double quotes.
5137 "\\(?:\\w\\|\\s.\\|\\s_\\)\\([\"']\\)\\(?:\\s-\\|\\s)\\|\\s.\\)"
5138 ;; Apostrophe.
5139 "\\S-\\('\\)\\S-"
5140 ;; Possible opening quote at end of string.
5141 "\\(?:\\s-\\|\\s(\\)\\([\"']\\)\\'"
5142 ;; Possible closing quote at end of string.
5143 "\\(?:\\w\\|\\s.\\|\\s_\\)\\([\"']\\)\\'"
5144 ;; Possible apostrophe at end of string.
5145 "\\S-\\('\\)\\'")
5146 "List of regexps matching a quote or an apostrophe.
5147 In every regexp, quote or apostrophe matched is put in group 1.")
5149 (defun org-export-activate-smart-quotes (s encoding info &optional original)
5150 "Replace regular quotes with \"smart\" quotes in string S.
5152 ENCODING is a symbol among `:html', `:latex', `:texinfo' and
5153 `:utf-8'. INFO is a plist used as a communication channel.
5155 The function has to retrieve information about string
5156 surroundings in parse tree. It can only happen with an
5157 unmodified string. Thus, if S has already been through another
5158 process, a non-nil ORIGINAL optional argument will provide that
5159 original string.
5161 Return the new string."
5162 (if (equal s "") ""
5163 (let* ((prev (org-export-get-previous-element (or original s) info))
5164 ;; Try to be flexible when computing number of blanks
5165 ;; before object. The previous object may be a string
5166 ;; introduced by the back-end and not completely parsed.
5167 (pre-blank (and prev
5168 (or (org-element-property :post-blank prev)
5169 ;; A string with missing `:post-blank'
5170 ;; property.
5171 (and (stringp prev)
5172 (string-match " *\\'" prev)
5173 (length (match-string 0 prev)))
5174 ;; Fallback value.
5175 0)))
5176 (next (org-export-get-next-element (or original s) info))
5177 (get-smart-quote
5178 (lambda (q type)
5179 ;; Return smart quote associated to a give quote Q, as
5180 ;; a string. TYPE is a symbol among `open', `close' and
5181 ;; `apostrophe'.
5182 (let ((key (case type
5183 (apostrophe 'apostrophe)
5184 (open (if (equal "'" q) 'opening-single-quote
5185 'opening-double-quote))
5186 (otherwise (if (equal "'" q) 'closing-single-quote
5187 'closing-double-quote)))))
5188 (or (plist-get
5189 (cdr (assq key
5190 (cdr (assoc (plist-get info :language)
5191 org-export-smart-quotes-alist))))
5192 encoding)
5193 q)))))
5194 (if (or (equal "\"" s) (equal "'" s))
5195 ;; Only a quote: no regexp can match. We have to check both
5196 ;; sides and decide what to do.
5197 (cond ((and (not prev) (not next)) s)
5198 ((not prev) (funcall get-smart-quote s 'open))
5199 ((and (not next) (zerop pre-blank))
5200 (funcall get-smart-quote s 'close))
5201 ((not next) s)
5202 ((zerop pre-blank) (funcall get-smart-quote s 'apostrophe))
5203 (t (funcall get-smart-quote 'open)))
5204 ;; 1. Replace quote character at the beginning of S.
5205 (cond
5206 ;; Apostrophe?
5207 ((and prev (zerop pre-blank)
5208 (string-match (nth 2 org-export-smart-quotes-regexps) s))
5209 (setq s (replace-match
5210 (funcall get-smart-quote (match-string 1 s) 'apostrophe)
5211 nil t s 1)))
5212 ;; Closing quote?
5213 ((and prev (zerop pre-blank)
5214 (string-match (nth 1 org-export-smart-quotes-regexps) s))
5215 (setq s (replace-match
5216 (funcall get-smart-quote (match-string 1 s) 'close)
5217 nil t s 1)))
5218 ;; Opening quote?
5219 ((and (or (not prev) (> pre-blank 0))
5220 (string-match (nth 0 org-export-smart-quotes-regexps) s))
5221 (setq s (replace-match
5222 (funcall get-smart-quote (match-string 1 s) 'open)
5223 nil t s 1))))
5224 ;; 2. Replace quotes in the middle of the string.
5225 (setq s (replace-regexp-in-string
5226 ;; Opening quotes.
5227 (nth 3 org-export-smart-quotes-regexps)
5228 (lambda (text)
5229 (funcall get-smart-quote (match-string 1 text) 'open))
5230 s nil t 1))
5231 (setq s (replace-regexp-in-string
5232 ;; Closing quotes.
5233 (nth 4 org-export-smart-quotes-regexps)
5234 (lambda (text)
5235 (funcall get-smart-quote (match-string 1 text) 'close))
5236 s nil t 1))
5237 (setq s (replace-regexp-in-string
5238 ;; Apostrophes.
5239 (nth 5 org-export-smart-quotes-regexps)
5240 (lambda (text)
5241 (funcall get-smart-quote (match-string 1 text) 'apostrophe))
5242 s nil t 1))
5243 ;; 3. Replace quote character at the end of S.
5244 (cond
5245 ;; Apostrophe?
5246 ((and next (string-match (nth 8 org-export-smart-quotes-regexps) s))
5247 (setq s (replace-match
5248 (funcall get-smart-quote (match-string 1 s) 'apostrophe)
5249 nil t s 1)))
5250 ;; Closing quote?
5251 ((and (not next)
5252 (string-match (nth 7 org-export-smart-quotes-regexps) s))
5253 (setq s (replace-match
5254 (funcall get-smart-quote (match-string 1 s) 'close)
5255 nil t s 1)))
5256 ;; Opening quote?
5257 ((and next (string-match (nth 6 org-export-smart-quotes-regexps) s))
5258 (setq s (replace-match
5259 (funcall get-smart-quote (match-string 1 s) 'open)
5260 nil t s 1))))
5261 ;; Return string with smart quotes.
5262 s))))
5264 ;;;; Topology
5266 ;; Here are various functions to retrieve information about the
5267 ;; neighborhood of a given element or object. Neighbors of interest
5268 ;; are direct parent (`org-export-get-parent'), parent headline
5269 ;; (`org-export-get-parent-headline'), first element containing an
5270 ;; object, (`org-export-get-parent-element'), parent table
5271 ;; (`org-export-get-parent-table'), previous element or object
5272 ;; (`org-export-get-previous-element') and next element or object
5273 ;; (`org-export-get-next-element').
5275 ;; `org-export-get-genealogy' returns the full genealogy of a given
5276 ;; element or object, from closest parent to full parse tree.
5278 ;; defsubst org-export-get-parent must be defined before first use
5280 (defun org-export-get-genealogy (blob)
5281 "Return full genealogy relative to a given element or object.
5283 BLOB is the element or object being considered.
5285 Ancestors are returned from closest to farthest, the last one
5286 being the full parse tree."
5287 (let (genealogy (parent blob))
5288 (while (setq parent (org-element-property :parent parent))
5289 (push parent genealogy))
5290 (nreverse genealogy)))
5292 (defun org-export-get-parent-headline (blob)
5293 "Return BLOB parent headline or nil.
5294 BLOB is the element or object being considered."
5295 (let ((parent blob))
5296 (while (and (setq parent (org-element-property :parent parent))
5297 (not (eq (org-element-type parent) 'headline))))
5298 parent))
5300 (defun org-export-get-parent-element (object)
5301 "Return first element containing OBJECT or nil.
5302 OBJECT is the object to consider."
5303 (let ((parent object))
5304 (while (and (setq parent (org-element-property :parent parent))
5305 (memq (org-element-type parent) org-element-all-objects)))
5306 parent))
5308 (defun org-export-get-parent-table (object)
5309 "Return OBJECT parent table or nil.
5310 OBJECT is either a `table-cell' or `table-element' type object."
5311 (let ((parent object))
5312 (while (and (setq parent (org-element-property :parent parent))
5313 (not (eq (org-element-type parent) 'table))))
5314 parent))
5316 (defun org-export-get-previous-element (blob info &optional n)
5317 "Return previous element or object.
5319 BLOB is an element or object. INFO is a plist used as
5320 a communication channel. Return previous exportable element or
5321 object, a string, or nil.
5323 When optional argument N is a positive integer, return a list
5324 containing up to N siblings before BLOB, from farthest to
5325 closest. With any other non-nil value, return a list containing
5326 all of them."
5327 (let* ((secondary (org-element-secondary-p blob))
5328 (parent (org-export-get-parent blob))
5329 (siblings
5330 (if secondary (org-element-property secondary parent)
5331 (org-element-contents parent)))
5332 prev)
5333 (catch 'exit
5334 (dolist (obj (cdr (memq blob (reverse siblings))) prev)
5335 (cond ((memq obj (plist-get info :ignore-list)))
5336 ((null n) (throw 'exit obj))
5337 ((not (wholenump n)) (push obj prev))
5338 ((zerop n) (throw 'exit prev))
5339 (t (decf n) (push obj prev)))))))
5341 (defun org-export-get-next-element (blob info &optional n)
5342 "Return next element or object.
5344 BLOB is an element or object. INFO is a plist used as
5345 a communication channel. Return next exportable element or
5346 object, a string, or nil.
5348 When optional argument N is a positive integer, return a list
5349 containing up to N siblings after BLOB, from closest to farthest.
5350 With any other non-nil value, return a list containing all of
5351 them."
5352 (let* ((secondary (org-element-secondary-p blob))
5353 (parent (org-export-get-parent blob))
5354 (siblings
5355 (cdr (memq blob
5356 (if secondary (org-element-property secondary parent)
5357 (org-element-contents parent)))))
5358 next)
5359 (catch 'exit
5360 (dolist (obj siblings (nreverse next))
5361 (cond ((memq obj (plist-get info :ignore-list)))
5362 ((null n) (throw 'exit obj))
5363 ((not (wholenump n)) (push obj next))
5364 ((zerop n) (throw 'exit (nreverse next)))
5365 (t (decf n) (push obj next)))))))
5368 ;;;; Translation
5370 ;; `org-export-translate' translates a string according to the language
5371 ;; specified by the LANGUAGE keyword. `org-export-dictionary' contains
5372 ;; the dictionary used for the translation.
5374 (defconst org-export-dictionary
5375 '(("%e %n: %c"
5376 ("fr" :default "%e %n : %c" :html "%e&nbsp;%n&nbsp;: %c"))
5377 ("Author"
5378 ("ca" :default "Autor")
5379 ("cs" :default "Autor")
5380 ("da" :default "Forfatter")
5381 ("de" :default "Autor")
5382 ("eo" :html "A&#365;toro")
5383 ("es" :default "Autor")
5384 ("et" :default "Autor")
5385 ("fi" :html "Tekij&auml;")
5386 ("fr" :default "Auteur")
5387 ("hu" :default "Szerz&otilde;")
5388 ("is" :html "H&ouml;fundur")
5389 ("it" :default "Autore")
5390 ("ja" :default "著者" :html "&#33879;&#32773;")
5391 ("nl" :default "Auteur")
5392 ("no" :default "Forfatter")
5393 ("nb" :default "Forfatter")
5394 ("nn" :default "Forfattar")
5395 ("pl" :default "Autor")
5396 ("ru" :html "&#1040;&#1074;&#1090;&#1086;&#1088;" :utf-8 "Автор")
5397 ("sv" :html "F&ouml;rfattare")
5398 ("uk" :html "&#1040;&#1074;&#1090;&#1086;&#1088;" :utf-8 "Автор")
5399 ("zh-CN" :html "&#20316;&#32773;" :utf-8 "作者")
5400 ("zh-TW" :html "&#20316;&#32773;" :utf-8 "作者"))
5401 ("Continued from previous page"
5402 ("de" :default "Fortsetzung von vorheriger Seite")
5403 ("es" :default "Continúa de la página anterior")
5404 ("fr" :default "Suite de la page précédente")
5405 ("it" :default "Continua da pagina precedente")
5406 ("ja" :default "前ページからの続き")
5407 ("nl" :default "Vervolg van vorige pagina")
5408 ("pt" :default "Continuação da página anterior")
5409 ("ru" :html "(&#1055;&#1088;&#1086;&#1076;&#1086;&#1083;&#1078;&#1077;&#1085;&#1080;&#1077;)"
5410 :utf-8 "(Продолжение)"))
5411 ("Continued on next page"
5412 ("de" :default "Fortsetzung nächste Seite")
5413 ("es" :default "Continúa en la siguiente página")
5414 ("fr" :default "Suite page suivante")
5415 ("it" :default "Continua alla pagina successiva")
5416 ("ja" :default "次ページに続く")
5417 ("nl" :default "Vervolg op volgende pagina")
5418 ("pt" :default "Continua na página seguinte")
5419 ("ru" :html "(&#1055;&#1088;&#1086;&#1076;&#1086;&#1083;&#1078;&#1077;&#1085;&#1080;&#1077; &#1089;&#1083;&#1077;&#1076;&#1091;&#1077;&#1090;)"
5420 :utf-8 "(Продолжение следует)"))
5421 ("Date"
5422 ("ca" :default "Data")
5423 ("cs" :default "Datum")
5424 ("da" :default "Dato")
5425 ("de" :default "Datum")
5426 ("eo" :default "Dato")
5427 ("es" :default "Fecha")
5428 ("et" :html "Kuup&#228;ev" :utf-8 "Kuupäev")
5429 ("fi" :html "P&auml;iv&auml;m&auml;&auml;r&auml;")
5430 ("hu" :html "D&aacute;tum")
5431 ("is" :default "Dagsetning")
5432 ("it" :default "Data")
5433 ("ja" :default "日付" :html "&#26085;&#20184;")
5434 ("nl" :default "Datum")
5435 ("no" :default "Dato")
5436 ("nb" :default "Dato")
5437 ("nn" :default "Dato")
5438 ("pl" :default "Data")
5439 ("ru" :html "&#1044;&#1072;&#1090;&#1072;" :utf-8 "Дата")
5440 ("sv" :default "Datum")
5441 ("uk" :html "&#1044;&#1072;&#1090;&#1072;" :utf-8 "Дата")
5442 ("zh-CN" :html "&#26085;&#26399;" :utf-8 "日期")
5443 ("zh-TW" :html "&#26085;&#26399;" :utf-8 "日期"))
5444 ("Equation"
5445 ("da" :default "Ligning")
5446 ("de" :default "Gleichung")
5447 ("es" :html "Ecuaci&oacute;n" :default "Ecuación")
5448 ("et" :html "V&#245;rrand" :utf-8 "Võrrand")
5449 ("fr" :ascii "Equation" :default "Équation")
5450 ("ja" :default "方程式")
5451 ("no" :default "Ligning")
5452 ("nb" :default "Ligning")
5453 ("nn" :default "Likning")
5454 ("ru" :html "&#1059;&#1088;&#1072;&#1074;&#1085;&#1077;&#1085;&#1080;&#1077;"
5455 :utf-8 "Уравнение")
5456 ("sv" :default "Ekvation")
5457 ("zh-CN" :html "&#26041;&#31243;" :utf-8 "方程"))
5458 ("Figure"
5459 ("da" :default "Figur")
5460 ("de" :default "Abbildung")
5461 ("es" :default "Figura")
5462 ("et" :default "Joonis")
5463 ("ja" :default "図" :html "&#22259;")
5464 ("no" :default "Illustrasjon")
5465 ("nb" :default "Illustrasjon")
5466 ("nn" :default "Illustrasjon")
5467 ("ru" :html "&#1056;&#1080;&#1089;&#1091;&#1085;&#1086;&#1082;" :utf-8 "Рисунок")
5468 ("sv" :default "Illustration")
5469 ("zh-CN" :html "&#22270;" :utf-8 "图"))
5470 ("Figure %d:"
5471 ("da" :default "Figur %d")
5472 ("de" :default "Abbildung %d:")
5473 ("es" :default "Figura %d:")
5474 ("et" :default "Joonis %d:")
5475 ("fr" :default "Figure %d :" :html "Figure&nbsp;%d&nbsp;:")
5476 ("ja" :default "図%d: " :html "&#22259;%d: ")
5477 ("no" :default "Illustrasjon %d")
5478 ("nb" :default "Illustrasjon %d")
5479 ("nn" :default "Illustrasjon %d")
5480 ("ru" :html "&#1056;&#1080;&#1089;. %d.:" :utf-8 "Рис. %d.:")
5481 ("sv" :default "Illustration %d")
5482 ("zh-CN" :html "&#22270;%d&nbsp;" :utf-8 "图%d "))
5483 ("Footnotes"
5484 ("ca" :html "Peus de p&agrave;gina")
5485 ("cs" :default "Pozn\xe1mky pod carou")
5486 ("da" :default "Fodnoter")
5487 ("de" :html "Fu&szlig;noten" :default "Fußnoten")
5488 ("eo" :default "Piednotoj")
5489 ("es" :html "Nota al pie de p&aacute;gina" :default "Nota al pie de página")
5490 ("et" :html "Allm&#228;rkused" :utf-8 "Allmärkused")
5491 ("fi" :default "Alaviitteet")
5492 ("fr" :default "Notes de bas de page")
5493 ("hu" :html "L&aacute;bjegyzet")
5494 ("is" :html "Aftanm&aacute;lsgreinar")
5495 ("it" :html "Note a pi&egrave; di pagina")
5496 ("ja" :default "脚注" :html "&#33050;&#27880;")
5497 ("nl" :default "Voetnoten")
5498 ("no" :default "Fotnoter")
5499 ("nb" :default "Fotnoter")
5500 ("nn" :default "Fotnotar")
5501 ("pl" :default "Przypis")
5502 ("ru" :html "&#1057;&#1085;&#1086;&#1089;&#1082;&#1080;" :utf-8 "Сноски")
5503 ("sv" :default "Fotnoter")
5504 ("uk" :html "&#1055;&#1088;&#1080;&#1084;&#1110;&#1090;&#1082;&#1080;"
5505 :utf-8 "Примітки")
5506 ("zh-CN" :html "&#33050;&#27880;" :utf-8 "脚注")
5507 ("zh-TW" :html "&#33139;&#35387;" :utf-8 "腳註"))
5508 ("List of Listings"
5509 ("da" :default "Programmer")
5510 ("de" :default "Programmauflistungsverzeichnis")
5511 ("es" :default "Indice de Listados de programas")
5512 ("et" :default "Loendite nimekiri")
5513 ("fr" :default "Liste des programmes")
5514 ("ja" :default "ソースコード目次")
5515 ("no" :default "Dataprogrammer")
5516 ("nb" :default "Dataprogrammer")
5517 ("ru" :html "&#1057;&#1087;&#1080;&#1089;&#1086;&#1082; &#1088;&#1072;&#1089;&#1087;&#1077;&#1095;&#1072;&#1090;&#1086;&#1082;"
5518 :utf-8 "Список распечаток")
5519 ("zh-CN" :html "&#20195;&#30721;&#30446;&#24405;" :utf-8 "代码目录"))
5520 ("List of Tables"
5521 ("da" :default "Tabeller")
5522 ("de" :default "Tabellenverzeichnis")
5523 ("es" :default "Indice de tablas")
5524 ("et" :default "Tabelite nimekiri")
5525 ("fr" :default "Liste des tableaux")
5526 ("ja" :default "表目次")
5527 ("no" :default "Tabeller")
5528 ("nb" :default "Tabeller")
5529 ("nn" :default "Tabeller")
5530 ("ru" :html "&#1057;&#1087;&#1080;&#1089;&#1086;&#1082; &#1090;&#1072;&#1073;&#1083;&#1080;&#1094;"
5531 :utf-8 "Список таблиц")
5532 ("sv" :default "Tabeller")
5533 ("zh-CN" :html "&#34920;&#26684;&#30446;&#24405;" :utf-8 "表格目录"))
5534 ("Listing %d:"
5535 ("da" :default "Program %d")
5536 ("de" :default "Programmlisting %d")
5537 ("es" :default "Listado de programa %d")
5538 ("et" :default "Loend %d")
5539 ("fr" :default "Programme %d :" :html "Programme&nbsp;%d&nbsp;:")
5540 ("ja" :default "ソースコード%d:")
5541 ("no" :default "Dataprogram %d")
5542 ("nb" :default "Dataprogram %d")
5543 ("ru" :html "&#1056;&#1072;&#1089;&#1087;&#1077;&#1095;&#1072;&#1090;&#1082;&#1072; %d.:"
5544 :utf-8 "Распечатка %d.:")
5545 ("zh-CN" :html "&#20195;&#30721;%d&nbsp;" :utf-8 "代码%d "))
5546 ("References"
5547 ("fr" :ascii "References" :default "Références")
5548 ("de" :default "Quellen"))
5549 ("See section %s"
5550 ("da" :default "jævnfør afsnit %s")
5551 ("de" :default "siehe Abschnitt %s")
5552 ("es" :default "vea seccion %s")
5553 ("et" :html "Vaata peat&#252;kki %s" :utf-8 "Vaata peatükki %s")
5554 ("fr" :default "cf. section %s")
5555 ("ja" :default "セクション %s を参照")
5556 ("ru" :html "&#1057;&#1084;. &#1088;&#1072;&#1079;&#1076;&#1077;&#1083; %s"
5557 :utf-8 "См. раздел %s")
5558 ("zh-CN" :html "&#21442;&#35265;&#31532;%s&#33410;" :utf-8 "参见第%s节"))
5559 ("Table"
5560 ("de" :default "Tabelle")
5561 ("es" :default "Tabla")
5562 ("et" :default "Tabel")
5563 ("fr" :default "Tableau")
5564 ("ja" :default "表" :html "&#34920;")
5565 ("ru" :html "&#1058;&#1072;&#1073;&#1083;&#1080;&#1094;&#1072;" :utf-8 "Таблица")
5566 ("zh-CN" :html "&#34920;" :utf-8 "表"))
5567 ("Table %d:"
5568 ("da" :default "Tabel %d")
5569 ("de" :default "Tabelle %d")
5570 ("es" :default "Tabla %d")
5571 ("et" :default "Tabel %d")
5572 ("fr" :default "Tableau %d :")
5573 ("ja" :default "表%d:" :html "&#34920;%d:")
5574 ("no" :default "Tabell %d")
5575 ("nb" :default "Tabell %d")
5576 ("nn" :default "Tabell %d")
5577 ("ru" :html "&#1058;&#1072;&#1073;&#1083;&#1080;&#1094;&#1072; %d.:"
5578 :utf-8 "Таблица %d.:")
5579 ("sv" :default "Tabell %d")
5580 ("zh-CN" :html "&#34920;%d&nbsp;" :utf-8 "表%d "))
5581 ("Table of Contents"
5582 ("ca" :html "&Iacute;ndex")
5583 ("cs" :default "Obsah")
5584 ("da" :default "Indhold")
5585 ("de" :default "Inhaltsverzeichnis")
5586 ("eo" :default "Enhavo")
5587 ("es" :html "&Iacute;ndice")
5588 ("et" :default "Sisukord")
5589 ("fi" :html "Sis&auml;llysluettelo")
5590 ("fr" :ascii "Sommaire" :default "Table des matières")
5591 ("hu" :html "Tartalomjegyz&eacute;k")
5592 ("is" :default "Efnisyfirlit")
5593 ("it" :default "Indice")
5594 ("ja" :default "目次" :html "&#30446;&#27425;")
5595 ("nl" :default "Inhoudsopgave")
5596 ("no" :default "Innhold")
5597 ("nb" :default "Innhold")
5598 ("nn" :default "Innhald")
5599 ("pl" :html "Spis tre&#x015b;ci")
5600 ("ru" :html "&#1057;&#1086;&#1076;&#1077;&#1088;&#1078;&#1072;&#1085;&#1080;&#1077;"
5601 :utf-8 "Содержание")
5602 ("sv" :html "Inneh&aring;ll")
5603 ("uk" :html "&#1047;&#1084;&#1110;&#1089;&#1090;" :utf-8 "Зміст")
5604 ("zh-CN" :html "&#30446;&#24405;" :utf-8 "目录")
5605 ("zh-TW" :html "&#30446;&#37636;" :utf-8 "目錄"))
5606 ("Unknown reference"
5607 ("da" :default "ukendt reference")
5608 ("de" :default "Unbekannter Verweis")
5609 ("es" :default "referencia desconocida")
5610 ("et" :default "Tundmatu viide")
5611 ("fr" :ascii "Destination inconnue" :default "Référence inconnue")
5612 ("ja" :default "不明な参照先")
5613 ("ru" :html "&#1053;&#1077;&#1080;&#1079;&#1074;&#1077;&#1089;&#1090;&#1085;&#1072;&#1103; &#1089;&#1089;&#1099;&#1083;&#1082;&#1072;"
5614 :utf-8 "Неизвестная ссылка")
5615 ("zh-CN" :html "&#26410;&#30693;&#24341;&#29992;" :utf-8 "未知引用")))
5616 "Dictionary for export engine.
5618 Alist whose CAR is the string to translate and CDR is an alist
5619 whose CAR is the language string and CDR is a plist whose
5620 properties are possible charsets and values translated terms.
5622 It is used as a database for `org-export-translate'. Since this
5623 function returns the string as-is if no translation was found,
5624 the variable only needs to record values different from the
5625 entry.")
5627 (defun org-export-translate (s encoding info)
5628 "Translate string S according to language specification.
5630 ENCODING is a symbol among `:ascii', `:html', `:latex', `:latin1'
5631 and `:utf-8'. INFO is a plist used as a communication channel.
5633 Translation depends on `:language' property. Return the
5634 translated string. If no translation is found, try to fall back
5635 to `:default' encoding. If it fails, return S."
5636 (let* ((lang (plist-get info :language))
5637 (translations (cdr (assoc lang
5638 (cdr (assoc s org-export-dictionary))))))
5639 (or (plist-get translations encoding)
5640 (plist-get translations :default)
5641 s)))
5645 ;;; Asynchronous Export
5647 ;; `org-export-async-start' is the entry point for asynchronous
5648 ;; export. It recreates current buffer (including visibility,
5649 ;; narrowing and visited file) in an external Emacs process, and
5650 ;; evaluates a command there. It then applies a function on the
5651 ;; returned results in the current process.
5653 ;; At a higher level, `org-export-to-buffer' and `org-export-to-file'
5654 ;; allow to export to a buffer or a file, asynchronously or not.
5656 ;; `org-export-output-file-name' is an auxiliary function meant to be
5657 ;; used with `org-export-to-file'. With a given extension, it tries
5658 ;; to provide a canonical file name to write export output to.
5660 ;; Asynchronously generated results are never displayed directly.
5661 ;; Instead, they are stored in `org-export-stack-contents'. They can
5662 ;; then be retrieved by calling `org-export-stack'.
5664 ;; Export Stack is viewed through a dedicated major mode
5665 ;;`org-export-stack-mode' and tools: `org-export-stack-refresh',
5666 ;;`org-export-stack-delete', `org-export-stack-view' and
5667 ;;`org-export-stack-clear'.
5669 ;; For back-ends, `org-export-add-to-stack' add a new source to stack.
5670 ;; It should be used whenever `org-export-async-start' is called.
5672 (defmacro org-export-async-start (fun &rest body)
5673 "Call function FUN on the results returned by BODY evaluation.
5675 BODY evaluation happens in an asynchronous process, from a buffer
5676 which is an exact copy of the current one.
5678 Use `org-export-add-to-stack' in FUN in order to register results
5679 in the stack.
5681 This is a low level function. See also `org-export-to-buffer'
5682 and `org-export-to-file' for more specialized functions."
5683 (declare (indent 1) (debug t))
5684 (org-with-gensyms (process temp-file copy-fun proc-buffer coding)
5685 ;; Write the full sexp evaluating BODY in a copy of the current
5686 ;; buffer to a temporary file, as it may be too long for program
5687 ;; args in `start-process'.
5688 `(with-temp-message "Initializing asynchronous export process"
5689 (let ((,copy-fun (org-export--generate-copy-script (current-buffer)))
5690 (,temp-file (make-temp-file "org-export-process"))
5691 (,coding buffer-file-coding-system))
5692 (with-temp-file ,temp-file
5693 (insert
5694 ;; Null characters (from variable values) are inserted
5695 ;; within the file. As a consequence, coding system for
5696 ;; buffer contents will not be recognized properly. So,
5697 ;; we make sure it is the same as the one used to display
5698 ;; the original buffer.
5699 (format ";; -*- coding: %s; -*-\n%S"
5700 ,coding
5701 `(with-temp-buffer
5702 (when org-export-async-debug '(setq debug-on-error t))
5703 ;; Ignore `kill-emacs-hook' and code evaluation
5704 ;; queries from Babel as we need a truly
5705 ;; non-interactive process.
5706 (setq kill-emacs-hook nil
5707 org-babel-confirm-evaluate-answer-no t)
5708 ;; Initialize export framework.
5709 (require 'ox)
5710 ;; Re-create current buffer there.
5711 (funcall ,,copy-fun)
5712 (restore-buffer-modified-p nil)
5713 ;; Sexp to evaluate in the buffer.
5714 (print (progn ,,@body))))))
5715 ;; Start external process.
5716 (let* ((process-connection-type nil)
5717 (,proc-buffer (generate-new-buffer-name "*Org Export Process*"))
5718 (,process
5719 (apply
5720 #'start-process
5721 (append
5722 (list "org-export-process"
5723 ,proc-buffer
5724 (expand-file-name invocation-name invocation-directory)
5725 "--batch")
5726 (if org-export-async-init-file
5727 (list "-Q" "-l" org-export-async-init-file)
5728 (list "-l" user-init-file))
5729 (list "-l" ,temp-file)))))
5730 ;; Register running process in stack.
5731 (org-export-add-to-stack (get-buffer ,proc-buffer) nil ,process)
5732 ;; Set-up sentinel in order to catch results.
5733 (let ((handler ,fun))
5734 (set-process-sentinel
5735 ,process
5736 `(lambda (p status)
5737 (let ((proc-buffer (process-buffer p)))
5738 (when (eq (process-status p) 'exit)
5739 (unwind-protect
5740 (if (zerop (process-exit-status p))
5741 (unwind-protect
5742 (let ((results
5743 (with-current-buffer proc-buffer
5744 (goto-char (point-max))
5745 (backward-sexp)
5746 (read (current-buffer)))))
5747 (funcall ,handler results))
5748 (unless org-export-async-debug
5749 (and (get-buffer proc-buffer)
5750 (kill-buffer proc-buffer))))
5751 (org-export-add-to-stack proc-buffer nil p)
5752 (ding)
5753 (message "Process '%s' exited abnormally" p))
5754 (unless org-export-async-debug
5755 (delete-file ,,temp-file)))))))))))))
5757 ;;;###autoload
5758 (defun org-export-to-buffer
5759 (backend buffer
5760 &optional async subtreep visible-only body-only ext-plist
5761 post-process)
5762 "Call `org-export-as' with output to a specified buffer.
5764 BACKEND is either an export back-end, as returned by, e.g.,
5765 `org-export-create-backend', or a symbol referring to
5766 a registered back-end.
5768 BUFFER is the name of the output buffer. If it already exists,
5769 it will be erased first, otherwise, it will be created.
5771 A non-nil optional argument ASYNC means the process should happen
5772 asynchronously. The resulting buffer should then be accessible
5773 through the `org-export-stack' interface. When ASYNC is nil, the
5774 buffer is displayed if `org-export-show-temporary-export-buffer'
5775 is non-nil.
5777 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and
5778 EXT-PLIST are similar to those used in `org-export-as', which
5779 see.
5781 Optional argument POST-PROCESS is a function which should accept
5782 no argument. It is always called within the current process,
5783 from BUFFER, with point at its beginning. Export back-ends can
5784 use it to set a major mode there, e.g,
5786 \(defun org-latex-export-as-latex
5787 \(&optional async subtreep visible-only body-only ext-plist)
5788 \(interactive)
5789 \(org-export-to-buffer 'latex \"*Org LATEX Export*\"
5790 async subtreep visible-only body-only ext-plist (lambda () (LaTeX-mode))))
5792 This function returns BUFFER."
5793 (declare (indent 2))
5794 (if async
5795 (org-export-async-start
5796 `(lambda (output)
5797 (with-current-buffer (get-buffer-create ,buffer)
5798 (erase-buffer)
5799 (setq buffer-file-coding-system ',buffer-file-coding-system)
5800 (insert output)
5801 (goto-char (point-min))
5802 (org-export-add-to-stack (current-buffer) ',backend)
5803 (ignore-errors (funcall ,post-process))))
5804 `(org-export-as
5805 ',backend ,subtreep ,visible-only ,body-only ',ext-plist))
5806 (let ((output
5807 (org-export-as backend subtreep visible-only body-only ext-plist))
5808 (buffer (get-buffer-create buffer))
5809 (encoding buffer-file-coding-system))
5810 (when (and (org-string-nw-p output) (org-export--copy-to-kill-ring-p))
5811 (org-kill-new output))
5812 (with-current-buffer buffer
5813 (erase-buffer)
5814 (setq buffer-file-coding-system encoding)
5815 (insert output)
5816 (goto-char (point-min))
5817 (and (functionp post-process) (funcall post-process)))
5818 (when org-export-show-temporary-export-buffer
5819 (switch-to-buffer-other-window buffer))
5820 buffer)))
5822 ;;;###autoload
5823 (defun org-export-to-file
5824 (backend file &optional async subtreep visible-only body-only ext-plist
5825 post-process)
5826 "Call `org-export-as' with output to a specified file.
5828 BACKEND is either an export back-end, as returned by, e.g.,
5829 `org-export-create-backend', or a symbol referring to
5830 a registered back-end. FILE is the name of the output file, as
5831 a string.
5833 A non-nil optional argument ASYNC means the process should happen
5834 asynchronously. The resulting buffer will then be accessible
5835 through the `org-export-stack' interface.
5837 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and
5838 EXT-PLIST are similar to those used in `org-export-as', which
5839 see.
5841 Optional argument POST-PROCESS is called with FILE as its
5842 argument and happens asynchronously when ASYNC is non-nil. It
5843 has to return a file name, or nil. Export back-ends can use this
5844 to send the output file through additional processing, e.g,
5846 \(defun org-latex-export-to-latex
5847 \(&optional async subtreep visible-only body-only ext-plist)
5848 \(interactive)
5849 \(let ((outfile (org-export-output-file-name \".tex\" subtreep)))
5850 \(org-export-to-file 'latex outfile
5851 async subtreep visible-only body-only ext-plist
5852 \(lambda (file) (org-latex-compile file)))
5854 The function returns either a file name returned by POST-PROCESS,
5855 or FILE."
5856 (declare (indent 2))
5857 (if (not (file-writable-p file)) (error "Output file not writable")
5858 (let ((ext-plist (org-combine-plists `(:output-file ,file) ext-plist))
5859 (encoding (or org-export-coding-system buffer-file-coding-system)))
5860 (if async
5861 (org-export-async-start
5862 `(lambda (file)
5863 (org-export-add-to-stack (expand-file-name file) ',backend))
5864 `(let ((output
5865 (org-export-as
5866 ',backend ,subtreep ,visible-only ,body-only
5867 ',ext-plist)))
5868 (with-temp-buffer
5869 (insert output)
5870 (let ((coding-system-for-write ',encoding))
5871 (write-file ,file)))
5872 (or (ignore-errors (funcall ',post-process ,file)) ,file)))
5873 (let ((output (org-export-as
5874 backend subtreep visible-only body-only ext-plist)))
5875 (with-temp-buffer
5876 (insert output)
5877 (let ((coding-system-for-write encoding))
5878 (write-file file)))
5879 (when (and (org-export--copy-to-kill-ring-p) (org-string-nw-p output))
5880 (org-kill-new output))
5881 ;; Get proper return value.
5882 (or (and (functionp post-process) (funcall post-process file))
5883 file))))))
5885 (defun org-export-output-file-name (extension &optional subtreep pub-dir)
5886 "Return output file's name according to buffer specifications.
5888 EXTENSION is a string representing the output file extension,
5889 with the leading dot.
5891 With a non-nil optional argument SUBTREEP, try to determine
5892 output file's name by looking for \"EXPORT_FILE_NAME\" property
5893 of subtree at point.
5895 When optional argument PUB-DIR is set, use it as the publishing
5896 directory.
5898 When optional argument VISIBLE-ONLY is non-nil, don't export
5899 contents of hidden elements.
5901 Return file name as a string."
5902 (let* ((visited-file (buffer-file-name (buffer-base-buffer)))
5903 (base-name
5904 ;; File name may come from EXPORT_FILE_NAME subtree
5905 ;; property, assuming point is at beginning of said
5906 ;; sub-tree.
5907 (file-name-sans-extension
5908 (or (and subtreep
5909 (org-entry-get
5910 (save-excursion
5911 (ignore-errors (org-back-to-heading) (point)))
5912 "EXPORT_FILE_NAME" t))
5913 ;; File name may be extracted from buffer's associated
5914 ;; file, if any.
5915 (and visited-file (file-name-nondirectory visited-file))
5916 ;; Can't determine file name on our own: Ask user.
5917 (let ((read-file-name-function
5918 (and org-completion-use-ido 'ido-read-file-name)))
5919 (read-file-name
5920 "Output file: " pub-dir nil nil nil
5921 (lambda (name)
5922 (string= (file-name-extension name t) extension)))))))
5923 (output-file
5924 ;; Build file name. Enforce EXTENSION over whatever user
5925 ;; may have come up with. PUB-DIR, if defined, always has
5926 ;; precedence over any provided path.
5927 (cond
5928 (pub-dir
5929 (concat (file-name-as-directory pub-dir)
5930 (file-name-nondirectory base-name)
5931 extension))
5932 ((file-name-absolute-p base-name) (concat base-name extension))
5933 (t (concat (file-name-as-directory ".") base-name extension)))))
5934 ;; If writing to OUTPUT-FILE would overwrite original file, append
5935 ;; EXTENSION another time to final name.
5936 (if (and visited-file (org-file-equal-p visited-file output-file))
5937 (concat output-file extension)
5938 output-file)))
5940 (defun org-export-add-to-stack (source backend &optional process)
5941 "Add a new result to export stack if not present already.
5943 SOURCE is a buffer or a file name containing export results.
5944 BACKEND is a symbol representing export back-end used to generate
5947 Entries already pointing to SOURCE and unavailable entries are
5948 removed beforehand. Return the new stack."
5949 (setq org-export-stack-contents
5950 (cons (list source backend (or process (current-time)))
5951 (org-export-stack-remove source))))
5953 (defun org-export-stack ()
5954 "Menu for asynchronous export results and running processes."
5955 (interactive)
5956 (let ((buffer (get-buffer-create "*Org Export Stack*")))
5957 (set-buffer buffer)
5958 (when (zerop (buffer-size)) (org-export-stack-mode))
5959 (org-export-stack-refresh)
5960 (pop-to-buffer buffer))
5961 (message "Type \"q\" to quit, \"?\" for help"))
5963 (defun org-export--stack-source-at-point ()
5964 "Return source from export results at point in stack."
5965 (let ((source (car (nth (1- (org-current-line)) org-export-stack-contents))))
5966 (if (not source) (error "Source unavailable, please refresh buffer")
5967 (let ((source-name (if (stringp source) source (buffer-name source))))
5968 (if (save-excursion
5969 (beginning-of-line)
5970 (looking-at (concat ".* +" (regexp-quote source-name) "$")))
5971 source
5972 ;; SOURCE is not consistent with current line. The stack
5973 ;; view is outdated.
5974 (error "Source unavailable; type `g' to update buffer"))))))
5976 (defun org-export-stack-clear ()
5977 "Remove all entries from export stack."
5978 (interactive)
5979 (setq org-export-stack-contents nil))
5981 (defun org-export-stack-refresh (&rest dummy)
5982 "Refresh the asynchronous export stack.
5983 DUMMY is ignored. Unavailable sources are removed from the list.
5984 Return the new stack."
5985 (let ((inhibit-read-only t))
5986 (org-preserve-lc
5987 (erase-buffer)
5988 (insert (concat
5989 (let ((counter 0))
5990 (mapconcat
5991 (lambda (entry)
5992 (let ((proc-p (processp (nth 2 entry))))
5993 (concat
5994 ;; Back-end.
5995 (format " %-12s " (or (nth 1 entry) ""))
5996 ;; Age.
5997 (let ((data (nth 2 entry)))
5998 (if proc-p (format " %6s " (process-status data))
5999 ;; Compute age of the results.
6000 (org-format-seconds
6001 "%4h:%.2m "
6002 (float-time (time-since data)))))
6003 ;; Source.
6004 (format " %s"
6005 (let ((source (car entry)))
6006 (if (stringp source) source
6007 (buffer-name source)))))))
6008 ;; Clear stack from exited processes, dead buffers or
6009 ;; non-existent files.
6010 (setq org-export-stack-contents
6011 (org-remove-if-not
6012 (lambda (el)
6013 (if (processp (nth 2 el))
6014 (buffer-live-p (process-buffer (nth 2 el)))
6015 (let ((source (car el)))
6016 (if (bufferp source) (buffer-live-p source)
6017 (file-exists-p source)))))
6018 org-export-stack-contents)) "\n")))))))
6020 (defun org-export-stack-remove (&optional source)
6021 "Remove export results at point from stack.
6022 If optional argument SOURCE is non-nil, remove it instead."
6023 (interactive)
6024 (let ((source (or source (org-export--stack-source-at-point))))
6025 (setq org-export-stack-contents
6026 (org-remove-if (lambda (el) (equal (car el) source))
6027 org-export-stack-contents))))
6029 (defun org-export-stack-view (&optional in-emacs)
6030 "View export results at point in stack.
6031 With an optional prefix argument IN-EMACS, force viewing files
6032 within Emacs."
6033 (interactive "P")
6034 (let ((source (org-export--stack-source-at-point)))
6035 (cond ((processp source)
6036 (org-switch-to-buffer-other-window (process-buffer source)))
6037 ((bufferp source) (org-switch-to-buffer-other-window source))
6038 (t (org-open-file source in-emacs)))))
6040 (defvar org-export-stack-mode-map
6041 (let ((km (make-sparse-keymap)))
6042 (define-key km " " 'next-line)
6043 (define-key km "n" 'next-line)
6044 (define-key km "\C-n" 'next-line)
6045 (define-key km [down] 'next-line)
6046 (define-key km "p" 'previous-line)
6047 (define-key km "\C-p" 'previous-line)
6048 (define-key km "\C-?" 'previous-line)
6049 (define-key km [up] 'previous-line)
6050 (define-key km "C" 'org-export-stack-clear)
6051 (define-key km "v" 'org-export-stack-view)
6052 (define-key km (kbd "RET") 'org-export-stack-view)
6053 (define-key km "d" 'org-export-stack-remove)
6055 "Keymap for Org Export Stack.")
6057 (define-derived-mode org-export-stack-mode special-mode "Org-Stack"
6058 "Mode for displaying asynchronous export stack.
6060 Type \\[org-export-stack] to visualize the asynchronous export
6061 stack.
6063 In an Org Export Stack buffer, use \\<org-export-stack-mode-map>\\[org-export-stack-view] to view export output
6064 on current line, \\[org-export-stack-remove] to remove it from the stack and \\[org-export-stack-clear] to clear
6065 stack completely.
6067 Removing entries in an Org Export Stack buffer doesn't affect
6068 files or buffers, only the display.
6070 \\{org-export-stack-mode-map}"
6071 (abbrev-mode 0)
6072 (auto-fill-mode 0)
6073 (setq buffer-read-only t
6074 buffer-undo-list t
6075 truncate-lines t
6076 header-line-format
6077 '(:eval
6078 (format " %-12s | %6s | %s" "Back-End" "Age" "Source")))
6079 (org-add-hook 'post-command-hook 'org-export-stack-refresh nil t)
6080 (set (make-local-variable 'revert-buffer-function)
6081 'org-export-stack-refresh))
6085 ;;; The Dispatcher
6087 ;; `org-export-dispatch' is the standard interactive way to start an
6088 ;; export process. It uses `org-export--dispatch-ui' as a subroutine
6089 ;; for its interface, which, in turn, delegates response to key
6090 ;; pressed to `org-export--dispatch-action'.
6092 ;;;###autoload
6093 (defun org-export-dispatch (&optional arg)
6094 "Export dispatcher for Org mode.
6096 It provides an access to common export related tasks in a buffer.
6097 Its interface comes in two flavors: standard and expert.
6099 While both share the same set of bindings, only the former
6100 displays the valid keys associations in a dedicated buffer.
6101 Scrolling (resp. line-wise motion) in this buffer is done with
6102 SPC and DEL (resp. C-n and C-p) keys.
6104 Set variable `org-export-dispatch-use-expert-ui' to switch to one
6105 flavor or the other.
6107 When ARG is \\[universal-argument], repeat the last export action, with the same set
6108 of options used back then, on the current buffer.
6110 When ARG is \\[universal-argument] \\[universal-argument], display the asynchronous export stack."
6111 (interactive "P")
6112 (let* ((input
6113 (cond ((equal arg '(16)) '(stack))
6114 ((and arg org-export-dispatch-last-action))
6115 (t (save-window-excursion
6116 (unwind-protect
6117 (progn
6118 ;; Remember where we are
6119 (move-marker org-export-dispatch-last-position
6120 (point)
6121 (org-base-buffer (current-buffer)))
6122 ;; Get and store an export command
6123 (setq org-export-dispatch-last-action
6124 (org-export--dispatch-ui
6125 (list org-export-initial-scope
6126 (and org-export-in-background 'async))
6128 org-export-dispatch-use-expert-ui)))
6129 (and (get-buffer "*Org Export Dispatcher*")
6130 (kill-buffer "*Org Export Dispatcher*")))))))
6131 (action (car input))
6132 (optns (cdr input)))
6133 (unless (memq 'subtree optns)
6134 (move-marker org-export-dispatch-last-position nil))
6135 (case action
6136 ;; First handle special hard-coded actions.
6137 (template (org-export-insert-default-template nil optns))
6138 (stack (org-export-stack))
6139 (publish-current-file
6140 (org-publish-current-file (memq 'force optns) (memq 'async optns)))
6141 (publish-current-project
6142 (org-publish-current-project (memq 'force optns) (memq 'async optns)))
6143 (publish-choose-project
6144 (org-publish (assoc (org-icompleting-read
6145 "Publish project: "
6146 org-publish-project-alist nil t)
6147 org-publish-project-alist)
6148 (memq 'force optns)
6149 (memq 'async optns)))
6150 (publish-all (org-publish-all (memq 'force optns) (memq 'async optns)))
6151 (otherwise
6152 (save-excursion
6153 (when arg
6154 ;; Repeating command, maybe move cursor to restore subtree
6155 ;; context.
6156 (if (eq (marker-buffer org-export-dispatch-last-position)
6157 (org-base-buffer (current-buffer)))
6158 (goto-char org-export-dispatch-last-position)
6159 ;; We are in a different buffer, forget position.
6160 (move-marker org-export-dispatch-last-position nil)))
6161 (funcall action
6162 ;; Return a symbol instead of a list to ease
6163 ;; asynchronous export macro use.
6164 (and (memq 'async optns) t)
6165 (and (memq 'subtree optns) t)
6166 (and (memq 'visible optns) t)
6167 (and (memq 'body optns) t)))))))
6169 (defun org-export--dispatch-ui (options first-key expertp)
6170 "Handle interface for `org-export-dispatch'.
6172 OPTIONS is a list containing current interactive options set for
6173 export. It can contain any of the following symbols:
6174 `body' toggles a body-only export
6175 `subtree' restricts export to current subtree
6176 `visible' restricts export to visible part of buffer.
6177 `force' force publishing files.
6178 `async' use asynchronous export process
6180 FIRST-KEY is the key pressed to select the first level menu. It
6181 is nil when this menu hasn't been selected yet.
6183 EXPERTP, when non-nil, triggers expert UI. In that case, no help
6184 buffer is provided, but indications about currently active
6185 options are given in the prompt. Moreover, \[?] allows to switch
6186 back to standard interface."
6187 (let* ((fontify-key
6188 (lambda (key &optional access-key)
6189 ;; Fontify KEY string. Optional argument ACCESS-KEY, when
6190 ;; non-nil is the required first-level key to activate
6191 ;; KEY. When its value is t, activate KEY independently
6192 ;; on the first key, if any. A nil value means KEY will
6193 ;; only be activated at first level.
6194 (if (or (eq access-key t) (eq access-key first-key))
6195 (org-propertize key 'face 'org-warning)
6196 key)))
6197 (fontify-value
6198 (lambda (value)
6199 ;; Fontify VALUE string.
6200 (org-propertize value 'face 'font-lock-variable-name-face)))
6201 ;; Prepare menu entries by extracting them from registered
6202 ;; back-ends and sorting them by access key and by ordinal,
6203 ;; if any.
6204 (entries
6205 (sort (sort (delq nil
6206 (mapcar 'org-export-backend-menu
6207 org-export--registered-backends))
6208 (lambda (a b)
6209 (let ((key-a (nth 1 a))
6210 (key-b (nth 1 b)))
6211 (cond ((and (numberp key-a) (numberp key-b))
6212 (< key-a key-b))
6213 ((numberp key-b) t)))))
6214 'car-less-than-car))
6215 ;; Compute a list of allowed keys based on the first key
6216 ;; pressed, if any. Some keys
6217 ;; (?^B, ?^V, ?^S, ?^F, ?^A, ?&, ?# and ?q) are always
6218 ;; available.
6219 (allowed-keys
6220 (nconc (list 2 22 19 6 1)
6221 (if (not first-key) (org-uniquify (mapcar 'car entries))
6222 (let (sub-menu)
6223 (dolist (entry entries (sort (mapcar 'car sub-menu) '<))
6224 (when (eq (car entry) first-key)
6225 (setq sub-menu (append (nth 2 entry) sub-menu))))))
6226 (cond ((eq first-key ?P) (list ?f ?p ?x ?a))
6227 ((not first-key) (list ?P)))
6228 (list ?& ?#)
6229 (when expertp (list ??))
6230 (list ?q)))
6231 ;; Build the help menu for standard UI.
6232 (help
6233 (unless expertp
6234 (concat
6235 ;; Options are hard-coded.
6236 (format "[%s] Body only: %s [%s] Visible only: %s
6237 \[%s] Export scope: %s [%s] Force publishing: %s
6238 \[%s] Async export: %s\n\n"
6239 (funcall fontify-key "C-b" t)
6240 (funcall fontify-value
6241 (if (memq 'body options) "On " "Off"))
6242 (funcall fontify-key "C-v" t)
6243 (funcall fontify-value
6244 (if (memq 'visible options) "On " "Off"))
6245 (funcall fontify-key "C-s" t)
6246 (funcall fontify-value
6247 (if (memq 'subtree options) "Subtree" "Buffer "))
6248 (funcall fontify-key "C-f" t)
6249 (funcall fontify-value
6250 (if (memq 'force options) "On " "Off"))
6251 (funcall fontify-key "C-a" t)
6252 (funcall fontify-value
6253 (if (memq 'async options) "On " "Off")))
6254 ;; Display registered back-end entries. When a key
6255 ;; appears for the second time, do not create another
6256 ;; entry, but append its sub-menu to existing menu.
6257 (let (last-key)
6258 (mapconcat
6259 (lambda (entry)
6260 (let ((top-key (car entry)))
6261 (concat
6262 (unless (eq top-key last-key)
6263 (setq last-key top-key)
6264 (format "\n[%s] %s\n"
6265 (funcall fontify-key (char-to-string top-key))
6266 (nth 1 entry)))
6267 (let ((sub-menu (nth 2 entry)))
6268 (unless (functionp sub-menu)
6269 ;; Split sub-menu into two columns.
6270 (let ((index -1))
6271 (concat
6272 (mapconcat
6273 (lambda (sub-entry)
6274 (incf index)
6275 (format
6276 (if (zerop (mod index 2)) " [%s] %-26s"
6277 "[%s] %s\n")
6278 (funcall fontify-key
6279 (char-to-string (car sub-entry))
6280 top-key)
6281 (nth 1 sub-entry)))
6282 sub-menu "")
6283 (when (zerop (mod index 2)) "\n"))))))))
6284 entries ""))
6285 ;; Publishing menu is hard-coded.
6286 (format "\n[%s] Publish
6287 [%s] Current file [%s] Current project
6288 [%s] Choose project [%s] All projects\n\n\n"
6289 (funcall fontify-key "P")
6290 (funcall fontify-key "f" ?P)
6291 (funcall fontify-key "p" ?P)
6292 (funcall fontify-key "x" ?P)
6293 (funcall fontify-key "a" ?P))
6294 (format "[%s] Export stack [%s] Insert template\n"
6295 (funcall fontify-key "&" t)
6296 (funcall fontify-key "#" t))
6297 (format "[%s] %s"
6298 (funcall fontify-key "q" t)
6299 (if first-key "Main menu" "Exit")))))
6300 ;; Build prompts for both standard and expert UI.
6301 (standard-prompt (unless expertp "Export command: "))
6302 (expert-prompt
6303 (when expertp
6304 (format
6305 "Export command (C-%s%s%s%s%s) [%s]: "
6306 (if (memq 'body options) (funcall fontify-key "b" t) "b")
6307 (if (memq 'visible options) (funcall fontify-key "v" t) "v")
6308 (if (memq 'subtree options) (funcall fontify-key "s" t) "s")
6309 (if (memq 'force options) (funcall fontify-key "f" t) "f")
6310 (if (memq 'async options) (funcall fontify-key "a" t) "a")
6311 (mapconcat (lambda (k)
6312 ;; Strip control characters.
6313 (unless (< k 27) (char-to-string k)))
6314 allowed-keys "")))))
6315 ;; With expert UI, just read key with a fancy prompt. In standard
6316 ;; UI, display an intrusive help buffer.
6317 (if expertp
6318 (org-export--dispatch-action
6319 expert-prompt allowed-keys entries options first-key expertp)
6320 ;; At first call, create frame layout in order to display menu.
6321 (unless (get-buffer "*Org Export Dispatcher*")
6322 (delete-other-windows)
6323 (org-switch-to-buffer-other-window
6324 (get-buffer-create "*Org Export Dispatcher*"))
6325 (setq cursor-type nil
6326 header-line-format "Use SPC, DEL, C-n or C-p to navigate.")
6327 ;; Make sure that invisible cursor will not highlight square
6328 ;; brackets.
6329 (set-syntax-table (copy-syntax-table))
6330 (modify-syntax-entry ?\[ "w"))
6331 ;; At this point, the buffer containing the menu exists and is
6332 ;; visible in the current window. So, refresh it.
6333 (with-current-buffer "*Org Export Dispatcher*"
6334 ;; Refresh help. Maintain display continuity by re-visiting
6335 ;; previous window position.
6336 (let ((pos (window-start)))
6337 (erase-buffer)
6338 (insert help)
6339 (set-window-start nil pos)))
6340 (org-fit-window-to-buffer)
6341 (org-export--dispatch-action
6342 standard-prompt allowed-keys entries options first-key expertp))))
6344 (defun org-export--dispatch-action
6345 (prompt allowed-keys entries options first-key expertp)
6346 "Read a character from command input and act accordingly.
6348 PROMPT is the displayed prompt, as a string. ALLOWED-KEYS is
6349 a list of characters available at a given step in the process.
6350 ENTRIES is a list of menu entries. OPTIONS, FIRST-KEY and
6351 EXPERTP are the same as defined in `org-export--dispatch-ui',
6352 which see.
6354 Toggle export options when required. Otherwise, return value is
6355 a list with action as CAR and a list of interactive export
6356 options as CDR."
6357 (let (key)
6358 ;; Scrolling: when in non-expert mode, act on motion keys (C-n,
6359 ;; C-p, SPC, DEL).
6360 (while (and (setq key (read-char-exclusive prompt))
6361 (not expertp)
6362 (memq key '(14 16 ?\s ?\d)))
6363 (case key
6364 (14 (if (not (pos-visible-in-window-p (point-max)))
6365 (ignore-errors (scroll-up 1))
6366 (message "End of buffer")
6367 (sit-for 1)))
6368 (16 (if (not (pos-visible-in-window-p (point-min)))
6369 (ignore-errors (scroll-down 1))
6370 (message "Beginning of buffer")
6371 (sit-for 1)))
6372 (?\s (if (not (pos-visible-in-window-p (point-max)))
6373 (scroll-up nil)
6374 (message "End of buffer")
6375 (sit-for 1)))
6376 (?\d (if (not (pos-visible-in-window-p (point-min)))
6377 (scroll-down nil)
6378 (message "Beginning of buffer")
6379 (sit-for 1)))))
6380 (cond
6381 ;; Ignore undefined associations.
6382 ((not (memq key allowed-keys))
6383 (ding)
6384 (unless expertp (message "Invalid key") (sit-for 1))
6385 (org-export--dispatch-ui options first-key expertp))
6386 ;; q key at first level aborts export. At second level, cancel
6387 ;; first key instead.
6388 ((eq key ?q) (if (not first-key) (error "Export aborted")
6389 (org-export--dispatch-ui options nil expertp)))
6390 ;; Help key: Switch back to standard interface if expert UI was
6391 ;; active.
6392 ((eq key ??) (org-export--dispatch-ui options first-key nil))
6393 ;; Send request for template insertion along with export scope.
6394 ((eq key ?#) (cons 'template (memq 'subtree options)))
6395 ;; Switch to asynchronous export stack.
6396 ((eq key ?&) '(stack))
6397 ;; Toggle options: C-b (2) C-v (22) C-s (19) C-f (6) C-a (1).
6398 ((memq key '(2 22 19 6 1))
6399 (org-export--dispatch-ui
6400 (let ((option (case key (2 'body) (22 'visible) (19 'subtree)
6401 (6 'force) (1 'async))))
6402 (if (memq option options) (remq option options)
6403 (cons option options)))
6404 first-key expertp))
6405 ;; Action selected: Send key and options back to
6406 ;; `org-export-dispatch'.
6407 ((or first-key (functionp (nth 2 (assq key entries))))
6408 (cons (cond
6409 ((not first-key) (nth 2 (assq key entries)))
6410 ;; Publishing actions are hard-coded. Send a special
6411 ;; signal to `org-export-dispatch'.
6412 ((eq first-key ?P)
6413 (case key
6414 (?f 'publish-current-file)
6415 (?p 'publish-current-project)
6416 (?x 'publish-choose-project)
6417 (?a 'publish-all)))
6418 ;; Return first action associated to FIRST-KEY + KEY
6419 ;; path. Indeed, derived backends can share the same
6420 ;; FIRST-KEY.
6421 (t (catch 'found
6422 (mapc (lambda (entry)
6423 (let ((match (assq key (nth 2 entry))))
6424 (when match (throw 'found (nth 2 match)))))
6425 (member (assq first-key entries) entries)))))
6426 options))
6427 ;; Otherwise, enter sub-menu.
6428 (t (org-export--dispatch-ui options key expertp)))))
6432 (provide 'ox)
6434 ;; Local variables:
6435 ;; generated-autoload-file: "org-loaddefs.el"
6436 ;; End:
6438 ;;; ox.el ends here