1 ;;; ox.el --- Generic Export Engine for Org Mode
3 ;; Copyright (C) 2012, 2013 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
23 ;; This library implements a generic export engine for Org, built on
24 ;; its syntactical parser: Org Elements.
26 ;; Besides that parser, the generic exporter is made of three distinct
29 ;; - The communication channel consists in a property list, which is
30 ;; created and updated during the process. Its use is to offer
31 ;; every piece of information, would it be about initial environment
32 ;; or contextual data, all in a single place. The exhaustive list
33 ;; of properties is given in "The Communication Channel" section of
36 ;; - The transcoder walks the parse tree, ignores or treat as plain
37 ;; text elements and objects according to export options, and
38 ;; eventually calls back-end specific functions to do the real
39 ;; transcoding, concatenating their return value along the way.
41 ;; - The filter system is activated at the very beginning and the very
42 ;; end of the export process, and each time an element or an object
43 ;; has been converted. It is the entry point to fine-tune standard
44 ;; output from back-end transcoders. See "The Filter System"
45 ;; section for more information.
47 ;; The core function is `org-export-as'. It returns the transcoded
48 ;; buffer as a string.
50 ;; An export back-end is defined with `org-export-define-backend',
51 ;; which defines one mandatory information: his translation table.
52 ;; Its value is an alist whose keys are elements and objects types and
53 ;; values translator functions. See function's docstring for more
54 ;; information about translators.
56 ;; Optionally, `org-export-define-backend' can also support specific
57 ;; buffer keywords, OPTION keyword's items and filters. Also refer to
58 ;; function documentation for more information.
60 ;; If the new back-end shares most properties with another one,
61 ;; `org-export-define-derived-backend' can be used to simplify the
64 ;; Any back-end can define its own variables. Among them, those
65 ;; customizable should belong to the `org-export-BACKEND' group.
67 ;; Tools for common tasks across back-ends are implemented in the
68 ;; following part of then file.
70 ;; Then, a wrapper macro for asynchronous export,
71 ;; `org-export-async-start', along with tools to display results. are
72 ;; given in the penultimate part.
74 ;; Eventually, a dispatcher (`org-export-dispatch') for standard
75 ;; back-ends is provided in the last one.
79 (eval-when-compile (require 'cl
))
80 (require 'org-element
)
84 (declare-function org-publish
"ox-publish" (project &optional force async
))
85 (declare-function org-publish-all
"ox-publish" (&optional force async
))
87 org-publish-current-file
"ox-publish" (&optional force async
))
88 (declare-function org-publish-current-project
"ox-publish"
89 (&optional force async
))
91 (defvar org-publish-project-alist
)
92 (defvar org-table-number-fraction
)
93 (defvar org-table-number-regexp
)
97 ;;; Internal Variables
99 ;; Among internal variables, the most important is
100 ;; `org-export-options-alist'. This variable define the global export
101 ;; options, shared between every exporter, and how they are acquired.
103 (defconst org-export-max-depth
19
104 "Maximum nesting depth for headlines, counting from 0.")
106 (defconst org-export-options-alist
107 '((:author
"AUTHOR" nil user-full-name t
)
108 (:creator
"CREATOR" nil org-export-creator-string
)
109 (:date
"DATE" nil nil t
)
110 (:description
"DESCRIPTION" nil nil newline
)
111 (:email
"EMAIL" nil user-mail-address t
)
112 (:exclude-tags
"EXCLUDE_TAGS" nil org-export-exclude-tags split
)
113 (:headline-levels nil
"H" org-export-headline-levels
)
114 (:keywords
"KEYWORDS" nil nil space
)
115 (:language
"LANGUAGE" nil org-export-default-language t
)
116 (:preserve-breaks nil
"\\n" org-export-preserve-breaks
)
117 (:section-numbers nil
"num" org-export-with-section-numbers
)
118 (:select-tags
"SELECT_TAGS" nil org-export-select-tags split
)
119 (:time-stamp-file nil
"timestamp" org-export-time-stamp-file
)
120 (:title
"TITLE" nil nil space
)
121 (:with-archived-trees nil
"arch" org-export-with-archived-trees
)
122 (:with-author nil
"author" org-export-with-author
)
123 (:with-clocks nil
"c" org-export-with-clocks
)
124 (:with-creator nil
"creator" org-export-with-creator
)
125 (:with-date nil
"date" org-export-with-date
)
126 (:with-drawers nil
"d" org-export-with-drawers
)
127 (:with-email nil
"email" org-export-with-email
)
128 (:with-emphasize nil
"*" org-export-with-emphasize
)
129 (:with-entities nil
"e" org-export-with-entities
)
130 (:with-fixed-width nil
":" org-export-with-fixed-width
)
131 (:with-footnotes nil
"f" org-export-with-footnotes
)
132 (:with-inlinetasks nil
"inline" org-export-with-inlinetasks
)
133 (:with-latex nil
"tex" org-export-with-latex
)
134 (:with-plannings nil
"p" org-export-with-planning
)
135 (:with-priority nil
"pri" org-export-with-priority
)
136 (:with-smart-quotes nil
"'" org-export-with-smart-quotes
)
137 (:with-special-strings nil
"-" org-export-with-special-strings
)
138 (:with-statistics-cookies nil
"stat" org-export-with-statistics-cookies
)
139 (:with-sub-superscript nil
"^" org-export-with-sub-superscripts
)
140 (:with-toc nil
"toc" org-export-with-toc
)
141 (:with-tables nil
"|" org-export-with-tables
)
142 (:with-tags nil
"tags" org-export-with-tags
)
143 (:with-tasks nil
"tasks" org-export-with-tasks
)
144 (:with-timestamps nil
"<" org-export-with-timestamps
)
145 (:with-todo-keywords nil
"todo" org-export-with-todo-keywords
))
146 "Alist between export properties and ways to set them.
148 The CAR of the alist is the property name, and the CDR is a list
149 like (KEYWORD OPTION DEFAULT BEHAVIOUR) where:
151 KEYWORD is a string representing a buffer keyword, or nil. Each
152 property defined this way can also be set, during subtree
153 export, through an headline property named after the keyword
154 with the \"EXPORT_\" prefix (i.e. DATE keyword and EXPORT_DATE
156 OPTION is a string that could be found in an #+OPTIONS: line.
157 DEFAULT is the default value for the property.
158 BEHAVIOUR determines how Org should handle multiple keywords for
159 the same property. It is a symbol among:
160 nil Keep old value and discard the new one.
161 t Replace old value with the new one.
162 `space' Concatenate the values, separating them with a space.
163 `newline' Concatenate the values, separating them with
165 `split' Split values at white spaces, and cons them to the
168 Values set through KEYWORD and OPTION have precedence over
171 All these properties should be back-end agnostic. Back-end
172 specific properties are set through `org-export-define-backend'.
173 Properties redefined there have precedence over these.")
175 (defconst org-export-special-keywords
'("FILETAGS" "SETUPFILE" "OPTIONS")
176 "List of in-buffer keywords that require special treatment.
177 These keywords are not directly associated to a property. The
178 way they are handled must be hard-coded into
179 `org-export--get-inbuffer-options' function.")
181 (defconst org-export-filters-alist
182 '((:filter-bold . org-export-filter-bold-functions
)
183 (:filter-babel-call . org-export-filter-babel-call-functions
)
184 (:filter-center-block . org-export-filter-center-block-functions
)
185 (:filter-clock . org-export-filter-clock-functions
)
186 (:filter-code . org-export-filter-code-functions
)
187 (:filter-comment . org-export-filter-comment-functions
)
188 (:filter-comment-block . org-export-filter-comment-block-functions
)
189 (:filter-diary-sexp . org-export-filter-diary-sexp-functions
)
190 (:filter-drawer . org-export-filter-drawer-functions
)
191 (:filter-dynamic-block . org-export-filter-dynamic-block-functions
)
192 (:filter-entity . org-export-filter-entity-functions
)
193 (:filter-example-block . org-export-filter-example-block-functions
)
194 (:filter-export-block . org-export-filter-export-block-functions
)
195 (:filter-export-snippet . org-export-filter-export-snippet-functions
)
196 (:filter-final-output . org-export-filter-final-output-functions
)
197 (:filter-fixed-width . org-export-filter-fixed-width-functions
)
198 (:filter-footnote-definition . org-export-filter-footnote-definition-functions
)
199 (:filter-footnote-reference . org-export-filter-footnote-reference-functions
)
200 (:filter-headline . org-export-filter-headline-functions
)
201 (:filter-horizontal-rule . org-export-filter-horizontal-rule-functions
)
202 (:filter-inline-babel-call . org-export-filter-inline-babel-call-functions
)
203 (:filter-inline-src-block . org-export-filter-inline-src-block-functions
)
204 (:filter-inlinetask . org-export-filter-inlinetask-functions
)
205 (:filter-italic . org-export-filter-italic-functions
)
206 (:filter-item . org-export-filter-item-functions
)
207 (:filter-keyword . org-export-filter-keyword-functions
)
208 (:filter-latex-environment . org-export-filter-latex-environment-functions
)
209 (:filter-latex-fragment . org-export-filter-latex-fragment-functions
)
210 (:filter-line-break . org-export-filter-line-break-functions
)
211 (:filter-link . org-export-filter-link-functions
)
212 (:filter-macro . org-export-filter-macro-functions
)
213 (:filter-node-property . org-export-filter-node-property-functions
)
214 (:filter-options . org-export-filter-options-functions
)
215 (:filter-paragraph . org-export-filter-paragraph-functions
)
216 (:filter-parse-tree . org-export-filter-parse-tree-functions
)
217 (:filter-plain-list . org-export-filter-plain-list-functions
)
218 (:filter-plain-text . org-export-filter-plain-text-functions
)
219 (:filter-planning . org-export-filter-planning-functions
)
220 (:filter-property-drawer . org-export-filter-property-drawer-functions
)
221 (:filter-quote-block . org-export-filter-quote-block-functions
)
222 (:filter-quote-section . org-export-filter-quote-section-functions
)
223 (:filter-radio-target . org-export-filter-radio-target-functions
)
224 (:filter-section . org-export-filter-section-functions
)
225 (:filter-special-block . org-export-filter-special-block-functions
)
226 (:filter-src-block . org-export-filter-src-block-functions
)
227 (:filter-statistics-cookie . org-export-filter-statistics-cookie-functions
)
228 (:filter-strike-through . org-export-filter-strike-through-functions
)
229 (:filter-subscript . org-export-filter-subscript-functions
)
230 (:filter-superscript . org-export-filter-superscript-functions
)
231 (:filter-table . org-export-filter-table-functions
)
232 (:filter-table-cell . org-export-filter-table-cell-functions
)
233 (:filter-table-row . org-export-filter-table-row-functions
)
234 (:filter-target . org-export-filter-target-functions
)
235 (:filter-timestamp . org-export-filter-timestamp-functions
)
236 (:filter-underline . org-export-filter-underline-functions
)
237 (:filter-verbatim . org-export-filter-verbatim-functions
)
238 (:filter-verse-block . org-export-filter-verse-block-functions
))
239 "Alist between filters properties and initial values.
241 The key of each association is a property name accessible through
242 the communication channel. Its value is a configurable global
243 variable defining initial filters.
245 This list is meant to install user specified filters. Back-end
246 developers may install their own filters using
247 `org-export-define-backend'. Filters defined there will always
248 be prepended to the current list, so they always get applied
251 (defconst org-export-default-inline-image-rule
255 '("png" "jpeg" "jpg" "gif" "tiff" "tif" "xbm"
256 "xpm" "pbm" "pgm" "ppm") t
))))
257 "Default rule for link matching an inline image.
258 This rule applies to links with no description. By default, it
259 will be considered as an inline image if it targets a local file
260 whose extension is either \"png\", \"jpeg\", \"jpg\", \"gif\",
261 \"tiff\", \"tif\", \"xbm\", \"xpm\", \"pbm\", \"pgm\" or \"ppm\".
262 See `org-export-inline-image-p' for more information about
265 (defvar org-export-async-debug nil
266 "Non-nil means asynchronous export process should leave data behind.
268 This data is found in the appropriate \"*Org Export Process*\"
269 buffer, and in files prefixed with \"org-export-process\" and
270 located in `temporary-file-directory'.
272 When non-nil, it will also set `debug-on-error' to a non-nil
273 value in the external process.")
275 (defvar org-export-stack-contents nil
276 "Record asynchronously generated export results and processes.
277 This is an alist: its CAR is the source of the
278 result (destination file or buffer for a finished process,
279 original buffer for a running one) and its CDR is a list
280 containing the back-end used, as a symbol, and either a process
281 or the time at which it finished. It is used to build the menu
282 from `org-export-stack'.")
284 (defvar org-export-registered-backends nil
285 "List of backends currently available in the exporter.
287 A backend is stored as a list where CAR is its name, as a symbol,
288 and CDR is a plist with the following properties:
289 `:filters-alist', `:menu-entry', `:options-alist' and
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.")
302 ;;; User-configurable Variables
304 ;; Configuration for the masses.
306 ;; They should never be accessed directly, as their value is to be
307 ;; stored in a property list (cf. `org-export-options-alist').
308 ;; Back-ends will read their value from there instead.
310 (defgroup org-export nil
311 "Options for exporting Org mode files."
315 (defgroup org-export-general nil
316 "General options for export engine."
317 :tag
"Org Export General"
320 (defcustom org-export-with-archived-trees
'headline
321 "Whether sub-trees with the ARCHIVE tag should be exported.
323 This can have three different values:
324 nil Do not export, pretend this tree is not present.
325 t Do export the entire tree.
326 `headline' Only export the headline, but skip the tree below it.
328 This option can also be set with the #+OPTIONS line,
330 :group
'org-export-general
332 (const :tag
"Not at all" nil
)
333 (const :tag
"Headline only" 'headline
)
334 (const :tag
"Entirely" t
)))
336 (defcustom org-export-with-author t
337 "Non-nil means insert author name into the exported file.
338 This option can also be set with the #+OPTIONS line,
339 e.g. \"author:nil\"."
340 :group
'org-export-general
343 (defcustom org-export-with-clocks nil
344 "Non-nil means export CLOCK keywords.
345 This option can also be set with the #+OPTIONS line,
347 :group
'org-export-general
350 (defcustom org-export-with-creator
'comment
351 "Non-nil means the postamble should contain a creator sentence.
353 The sentence can be set in `org-export-creator-string' and
354 defaults to \"Generated by Org mode XX in Emacs XXX.\".
356 If the value is `comment' insert it as a comment."
357 :group
'org-export-general
359 (const :tag
"No creator sentence" nil
)
360 (const :tag
"Sentence as a comment" 'comment
)
361 (const :tag
"Insert the sentence" t
)))
363 (defcustom org-export-with-date t
364 "Non-nil means insert date in the exported document.
365 This options can also be set with the OPTIONS keyword,
368 (defcustom org-export-creator-string
369 (format "Generated by Org mode %s in Emacs %s."
370 (if (fboundp 'org-version
) (org-version) "(Unknown)")
372 "String to insert at the end of the generated document."
373 :group
'org-export-general
374 :type
'(string :tag
"Creator string"))
376 (defcustom org-export-with-drawers
'(not "LOGBOOK")
377 "Non-nil means export contents of standard drawers.
379 When t, all drawers are exported. This may also be a list of
380 drawer names to export. If that list starts with `not', only
381 drawers with such names will be ignored.
383 This variable doesn't apply to properties drawers.
385 This option can also be set with the #+OPTIONS line,
387 :group
'org-export-general
389 (const :tag
"All drawers" t
)
390 (const :tag
"None" nil
)
391 (repeat :tag
"Selected drawers"
392 (string :tag
"Drawer name"))
393 (list :tag
"Ignored drawers"
394 (const :format
"" not
)
395 (repeat :tag
"Specify names of drawers to ignore during export"
397 (string :tag
"Drawer name")))))
399 (defcustom org-export-with-email nil
400 "Non-nil means insert author email into the exported file.
401 This option can also be set with the #+OPTIONS line,
403 :group
'org-export-general
406 (defcustom org-export-with-emphasize t
407 "Non-nil means interpret *word*, /word/, _word_ and +word+.
409 If the export target supports emphasizing text, the word will be
410 typeset in bold, italic, with an underline or strike-through,
413 This option can also be set with the #+OPTIONS line, e.g. \"*:nil\"."
414 :group
'org-export-general
417 (defcustom org-export-exclude-tags
'("noexport")
418 "Tags that exclude a tree from export.
420 All trees carrying any of these tags will be excluded from
421 export. This is without condition, so even subtrees inside that
422 carry one of the `org-export-select-tags' will be removed.
424 This option can also be set with the #+EXCLUDE_TAGS: keyword."
425 :group
'org-export-general
426 :type
'(repeat (string :tag
"Tag")))
428 (defcustom org-export-with-fixed-width t
429 "Non-nil means lines starting with \":\" will be in fixed width font.
431 This can be used to have pre-formatted text, fragments of code
433 : ;; Some Lisp examples
436 will be looking just like this in also HTML. See also the QUOTE
437 keyword. Not all export backends support this.
439 This option can also be set with the #+OPTIONS line, e.g. \"::nil\"."
440 :group
'org-export-general
443 (defcustom org-export-with-footnotes t
444 "Non-nil means Org footnotes should be exported.
445 This option can also be set with the #+OPTIONS line,
447 :group
'org-export-general
450 (defcustom org-export-with-latex t
451 "Non-nil means process LaTeX environments and fragments.
453 This option can also be set with the +OPTIONS line,
454 e.g. \"tex:verbatim\". Allowed values are:
456 nil Ignore math snippets.
457 `verbatim' Keep everything in verbatim.
458 t Allow export of math snippets."
459 :group
'org-export-general
461 (const :tag
"Do not process math in any way" nil
)
462 (const :tag
"Interpret math snippets" t
)
463 (const :tag
"Leave math verbatim" verbatim
)))
465 (defcustom org-export-headline-levels
3
466 "The last level which is still exported as a headline.
468 Inferior levels will produce itemize or enumerate lists when
471 This option can also be set with the #+OPTIONS line, e.g. \"H:2\"."
472 :group
'org-export-general
475 (defcustom org-export-default-language
"en"
476 "The default language for export and clocktable translations, as a string.
477 This may have an association in
478 `org-clock-clocktable-language-setup'."
479 :group
'org-export-general
480 :type
'(string :tag
"Language"))
482 (defcustom org-export-preserve-breaks nil
483 "Non-nil means preserve all line breaks when exporting.
484 This option can also be set with the #+OPTIONS line,
486 :group
'org-export-general
489 (defcustom org-export-with-entities t
490 "Non-nil means interpret entities when exporting.
492 For example, HTML export converts \\alpha to α and \\AA to
495 For a list of supported names, see the constant `org-entities'
496 and the user option `org-entities-user'.
498 This option can also be set with the #+OPTIONS line,
500 :group
'org-export-general
503 (defcustom org-export-with-inlinetasks t
504 "Non-nil means inlinetasks should be exported.
505 This option can also be set with the #+OPTIONS line,
506 e.g. \"inline:nil\"."
507 :group
'org-export-general
510 (defcustom org-export-with-planning nil
511 "Non-nil means include planning info in export.
512 This option can also be set with the #+OPTIONS: line,
514 :group
'org-export-general
517 (defcustom org-export-with-priority nil
518 "Non-nil means include priority cookies in export.
519 This option can also be set with the #+OPTIONS line,
521 :group
'org-export-general
524 (defcustom org-export-with-section-numbers t
525 "Non-nil means add section numbers to headlines when exporting.
527 When set to an integer n, numbering will only happen for
528 headlines whose relative level is higher or equal to n.
530 This option can also be set with the #+OPTIONS line,
532 :group
'org-export-general
535 (defcustom org-export-select-tags
'("export")
536 "Tags that select a tree for export.
538 If any such tag is found in a buffer, all trees that do not carry
539 one of these tags will be ignored during export. Inside trees
540 that are selected like this, you can still deselect a subtree by
541 tagging it with one of the `org-export-exclude-tags'.
543 This option can also be set with the #+SELECT_TAGS: keyword."
544 :group
'org-export-general
545 :type
'(repeat (string :tag
"Tag")))
547 (defcustom org-export-with-smart-quotes nil
548 "Non-nil means activate smart quotes during export.
549 This option can also be set with the #+OPTIONS: line,
551 :group
'org-export-general
554 (defcustom org-export-with-special-strings t
555 "Non-nil means interpret \"\\-\", \"--\" and \"---\" for export.
557 When this option is turned on, these strings will be exported as:
560 -----+----------+--------+-------
564 ... … \\ldots …
566 This option can also be set with the #+OPTIONS line,
568 :group
'org-export-general
571 (defcustom org-export-with-statistics-cookies t
572 "Non-nil means include statistics cookies in export.
573 This option can also be set with the #+OPTIONS: line,
575 :group
'org-export-general
578 (defcustom org-export-with-sub-superscripts t
579 "Non-nil means interpret \"_\" and \"^\" for export.
581 When this option is turned on, you can use TeX-like syntax for
582 sub- and superscripts. Several characters after \"_\" or \"^\"
583 will be considered as a single item - so grouping with {} is
584 normally not needed. For example, the following things will be
585 parsed as single sub- or superscripts.
587 10^24 or 10^tau several digits will be considered 1 item.
588 10^-12 or 10^-tau a leading sign with digits or a word
589 x^2-y^3 will be read as x^2 - y^3, because items are
590 terminated by almost any nonword/nondigit char.
591 x_{i^2} or x^(2-i) braces or parenthesis do grouping.
593 Still, ambiguity is possible - so when in doubt use {} to enclose
594 the sub/superscript. If you set this variable to the symbol
595 `{}', the braces are *required* in order to trigger
596 interpretations as sub/superscript. This can be helpful in
597 documents that need \"_\" frequently in plain text.
599 This option can also be set with the #+OPTIONS line,
601 :group
'org-export-general
603 (const :tag
"Interpret them" t
)
604 (const :tag
"Curly brackets only" {})
605 (const :tag
"Do not interpret them" nil
)))
607 (defcustom org-export-with-toc t
608 "Non-nil means create a table of contents in exported files.
610 The TOC contains headlines with levels up
611 to`org-export-headline-levels'. When an integer, include levels
612 up to N in the toc, this may then be different from
613 `org-export-headline-levels', but it will not be allowed to be
614 larger than the number of headline levels. When nil, no table of
617 This option can also be set with the #+OPTIONS line,
618 e.g. \"toc:nil\" or \"toc:3\"."
619 :group
'org-export-general
621 (const :tag
"No Table of Contents" nil
)
622 (const :tag
"Full Table of Contents" t
)
623 (integer :tag
"TOC to level")))
625 (defcustom org-export-with-tables t
626 "If non-nil, lines starting with \"|\" define a table.
629 | Name | Address | Birthday |
630 |-------------+----------+-----------|
631 | Arthur Dent | England | 29.2.2100 |
633 This option can also be set with the #+OPTIONS line, e.g. \"|:nil\"."
634 :group
'org-export-general
637 (defcustom org-export-with-tags t
638 "If nil, do not export tags, just remove them from headlines.
640 If this is the symbol `not-in-toc', tags will be removed from
641 table of contents entries, but still be shown in the headlines of
644 This option can also be set with the #+OPTIONS line,
646 :group
'org-export-general
648 (const :tag
"Off" nil
)
649 (const :tag
"Not in TOC" not-in-toc
)
650 (const :tag
"On" t
)))
652 (defcustom org-export-with-tasks t
653 "Non-nil means include TODO items for export.
655 This may have the following values:
656 t include tasks independent of state.
657 `todo' include only tasks that are not yet done.
658 `done' include only tasks that are already done.
659 nil ignore all tasks.
660 list of keywords include tasks with these keywords.
662 This option can also be set with the #+OPTIONS line,
664 :group
'org-export-general
666 (const :tag
"All tasks" t
)
667 (const :tag
"No tasks" nil
)
668 (const :tag
"Not-done tasks" todo
)
669 (const :tag
"Only done tasks" done
)
670 (repeat :tag
"Specific TODO keywords"
671 (string :tag
"Keyword"))))
673 (defcustom org-export-time-stamp-file t
674 "Non-nil means insert a time stamp into the exported file.
675 The time stamp shows when the file was created.
677 This option can also be set with the #+OPTIONS line,
678 e.g. \"timestamp:nil\"."
679 :group
'org-export-general
682 (defcustom org-export-with-timestamps t
683 "Non nil means allow timestamps in export.
685 It can be set to `active', `inactive', t or nil, in order to
686 export, respectively, only active timestamps, only inactive ones,
689 This option can also be set with the #+OPTIONS line, e.g.
691 :group
'org-export-general
693 (const :tag
"All timestamps" t
)
694 (const :tag
"Only active timestamps" active
)
695 (const :tag
"Only inactive timestamps" inactive
)
696 (const :tag
"No timestamp" nil
)))
698 (defcustom org-export-with-todo-keywords t
699 "Non-nil means include TODO keywords in export.
700 When nil, remove all these keywords from the export."
701 :group
'org-export-general
704 (defcustom org-export-allow-bind-keywords nil
705 "Non-nil means BIND keywords can define local variable values.
706 This is a potential security risk, which is why the default value
707 is nil. You can also allow them through local buffer variables."
708 :group
'org-export-general
711 (defcustom org-export-snippet-translation-alist nil
712 "Alist between export snippets back-ends and exporter back-ends.
714 This variable allows to provide shortcuts for export snippets.
716 For example, with a value of '\(\(\"h\" . \"html\"\)\), the
717 HTML back-end will recognize the contents of \"@@h:<b>@@\" as
718 HTML code while every other back-end will ignore it."
719 :group
'org-export-general
721 (cons (string :tag
"Shortcut")
722 (string :tag
"Back-end"))))
724 (defcustom org-export-coding-system nil
725 "Coding system for the exported file."
726 :group
'org-export-general
727 :type
'coding-system
)
729 (defcustom org-export-copy-to-kill-ring t
730 "Non-nil means exported stuff will also be pushed onto the kill ring."
731 :group
'org-export-general
734 (defcustom org-export-initial-scope
'buffer
735 "The initial scope when exporting with `org-export-dispatch'.
736 This variable can be either set to `buffer' or `subtree'."
737 :group
'org-export-general
739 (const :tag
"Export current buffer" 'buffer
)
740 (const :tag
"Export current subtree" 'subtree
)))
742 (defcustom org-export-show-temporary-export-buffer t
743 "Non-nil means show buffer after exporting to temp buffer.
744 When Org exports to a file, the buffer visiting that file is ever
745 shown, but remains buried. However, when exporting to
746 a temporary buffer, that buffer is popped up in a second window.
747 When this variable is nil, the buffer remains buried also in
749 :group
'org-export-general
752 (defcustom org-export-in-background nil
753 "Non-nil means export and publishing commands will run in background.
754 Results from an asynchronous export are never displayed
755 automatically. But you can retrieve them with \\[org-export-stack]."
756 :group
'org-export-general
759 (defcustom org-export-async-init-file user-init-file
760 "File used to initialize external export process.
761 Value must be an absolute file name. It defaults to user's
762 initialization file. Though, a specific configuration makes the
763 process faster and the export more portable."
764 :group
'org-export-general
765 :type
'(file :must-match t
))
767 (defcustom org-export-invisible-backends nil
768 "List of back-ends that shouldn't appear in the dispatcher.
770 Any back-end belonging to this list or derived from a back-end
771 belonging to it will not appear in the dispatcher menu.
773 Indeed, Org may require some export back-ends without notice. If
774 these modules are never to be used interactively, adding them
775 here will avoid cluttering the dispatcher menu."
776 :group
'org-export-general
777 :type
'(repeat (symbol :tag
"Back-End")))
779 (defcustom org-export-dispatch-use-expert-ui nil
780 "Non-nil means using a non-intrusive `org-export-dispatch'.
781 In that case, no help buffer is displayed. Though, an indicator
782 for current export scope is added to the prompt (\"b\" when
783 output is restricted to body only, \"s\" when it is restricted to
784 the current subtree, \"v\" when only visible elements are
785 considered for export, \"f\" when publishing functions should be
786 passed the FORCE argument and \"a\" when the export should be
787 asynchronous). Also, \[?] allows to switch back to standard
789 :group
'org-export-general
794 ;;; Defining Back-ends
796 ;; `org-export-define-backend' is the standard way to define an export
797 ;; back-end. It allows to specify translators, filters, buffer
798 ;; options and a menu entry. If the new back-end shares translators
799 ;; with another back-end, `org-export-define-derived-backend' may be
802 ;; Internally, a back-end is stored as a list, of which CAR is the
803 ;; name of the back-end, as a symbol, and CDR a plist. Accessors to
804 ;; properties of a given back-end are: `org-export-backend-filters',
805 ;; `org-export-backend-menu', `org-export-backend-options' and
806 ;; `org-export-backend-translate-table'.
808 ;; Eventually `org-export-barf-if-invalid-backend' returns an error
809 ;; when a given back-end hasn't been registered yet.
811 (defmacro org-export-define-backend
(backend translators
&rest body
)
812 "Define a new back-end BACKEND.
814 TRANSLATORS is an alist between object or element types and
815 functions handling them.
817 These functions should return a string without any trailing
818 space, or nil. They must accept three arguments: the object or
819 element itself, its contents or nil when it isn't recursive and
820 the property list used as a communication channel.
822 Contents, when not nil, are stripped from any global indentation
823 \(although the relative one is preserved). They also always end
824 with a single newline character.
826 If, for a given type, no function is found, that element or
827 object type will simply be ignored, along with any blank line or
828 white space at its end. The same will happen if the function
829 returns the nil value. If that function returns the empty
830 string, the type will be ignored, but the blank lines or white
833 In addition to element and object types, one function can be
834 associated to the `template' (or `inner-template') symbol and
835 another one to the `plain-text' symbol.
837 The former returns the final transcoded string, and can be used
838 to add a preamble and a postamble to document's body. It must
839 accept two arguments: the transcoded string and the property list
840 containing export options. A function associated to `template'
841 will not be applied if export has option \"body-only\".
842 A function associated to `inner-template' is always applied.
844 The latter, when defined, is to be called on every text not
845 recognized as an element or an object. It must accept two
846 arguments: the text string and the information channel. It is an
847 appropriate place to protect special chars relative to the
850 BODY can start with pre-defined keyword arguments. The following
851 keywords are understood:
855 String, or list of strings, representing block names that
856 will not be parsed. This is used to specify blocks that will
857 contain raw code specific to the back-end. These blocks
858 still have to be handled by the relative `export-block' type
863 Alist between filters and function, or list of functions,
864 specific to the back-end. See `org-export-filters-alist' for
865 a list of all allowed filters. Filters defined here
866 shouldn't make a back-end test, as it may prevent back-ends
867 derived from this one to behave properly.
871 Menu entry for the export dispatcher. It should be a list
874 \(KEY DESCRIPTION-OR-ORDINAL ACTION-OR-MENU)
878 KEY is a free character selecting the back-end.
880 DESCRIPTION-OR-ORDINAL is either a string or a number.
882 If it is a string, is will be used to name the back-end in
883 its menu entry. If it is a number, the following menu will
884 be displayed as a sub-menu of the back-end with the same
885 KEY. Also, the number will be used to determine in which
886 order such sub-menus will appear (lowest first).
888 ACTION-OR-MENU is either a function or an alist.
890 If it is an action, it will be called with four
891 arguments (booleans): ASYNC, SUBTREEP, VISIBLE-ONLY and
892 BODY-ONLY. See `org-export-as' for further explanations on
895 If it is an alist, associations should follow the
898 \(KEY DESCRIPTION ACTION)
900 where KEY, DESCRIPTION and ACTION are described above.
902 Valid values include:
904 \(?m \"My Special Back-end\" my-special-export-function)
908 \(?l \"Export to LaTeX\"
909 \(?p \"As PDF file\" org-latex-export-to-pdf)
910 \(?o \"As PDF file and open\"
912 \(if a (org-latex-export-to-pdf t s v b)
914 \(org-latex-export-to-pdf nil s v b)))))))
916 or the following, which will be added to the previous
920 \((?B \"As TEX buffer (Beamer)\" org-beamer-export-as-latex)
921 \(?P \"As PDF file (Beamer)\" org-beamer-export-to-pdf)))
925 Alist between back-end specific properties introduced in
926 communication channel and how their value are acquired. See
927 `org-export-options-alist' for more information about
928 structure of the values."
929 (declare (debug (&define name sexp
[&rest
[keywordp sexp
]] defbody
))
931 (let (export-block filters menu-entry options contents
)
932 (while (keywordp (car body
))
934 (:export-block
(let ((names (pop body
)))
936 (if (consp names
) (mapcar 'upcase names
)
937 (list (upcase names
))))))
938 (:filters-alist
(setq filters
(pop body
)))
939 (:menu-entry
(setq menu-entry
(pop body
)))
940 (:options-alist
(setq options
(pop body
)))
942 (setq contents
(append (list :translate-alist translators
)
943 (and filters
(list :filters-alist filters
))
944 (and options
(list :options-alist options
))
945 (and menu-entry
(list :menu-entry menu-entry
))))
947 ;; Register back-end.
948 (let ((registeredp (assq ',backend org-export-registered-backends
)))
949 (if registeredp
(setcdr registeredp
',contents
)
950 (push (cons ',backend
',contents
) org-export-registered-backends
)))
951 ;; Tell parser to not parse EXPORT-BLOCK blocks.
955 (add-to-list 'org-element-block-name-alist
956 `(,name . org-element-export-block-parser
)))
958 ;; Splice in the body, if any.
961 (defmacro org-export-define-derived-backend
(child parent
&rest body
)
962 "Create a new back-end as a variant of an existing one.
964 CHILD is the name of the derived back-end. PARENT is the name of
967 BODY can start with pre-defined keyword arguments. The following
968 keywords are understood:
972 String, or list of strings, representing block names that
973 will not be parsed. This is used to specify blocks that will
974 contain raw code specific to the back-end. These blocks
975 still have to be handled by the relative `export-block' type
980 Alist of filters that will overwrite or complete filters
981 defined in PARENT back-end. See `org-export-filters-alist'
982 for a list of allowed filters.
986 Menu entry for the export dispatcher. See
987 `org-export-define-backend' for more information about the
992 Alist of back-end specific properties that will overwrite or
993 complete those defined in PARENT back-end. Refer to
994 `org-export-options-alist' for more information about
995 structure of the values.
999 Alist of element and object types and transcoders that will
1000 overwrite or complete transcode table from PARENT back-end.
1001 Refer to `org-export-define-backend' for detailed information
1004 As an example, here is how one could define \"my-latex\" back-end
1005 as a variant of `latex' back-end with a custom template function:
1007 \(org-export-define-derived-backend my-latex latex
1008 :translate-alist ((template . my-latex-template-fun)))
1010 The back-end could then be called with, for example:
1012 \(org-export-to-buffer 'my-latex \"*Test my-latex*\")"
1013 (declare (debug (&define name sexp
[&rest
[keywordp sexp
]] def-body
))
1015 (let (export-block filters menu-entry options translators contents
)
1016 (while (keywordp (car body
))
1018 (:export-block
(let ((names (pop body
)))
1020 (if (consp names
) (mapcar 'upcase names
)
1021 (list (upcase names
))))))
1022 (:filters-alist
(setq filters
(pop body
)))
1023 (:menu-entry
(setq menu-entry
(pop body
)))
1024 (:options-alist
(setq options
(pop body
)))
1025 (:translate-alist
(setq translators
(pop body
)))
1027 (setq contents
(append
1028 (list :parent parent
)
1029 (let ((p-table (org-export-backend-translate-table parent
)))
1030 (list :translate-alist
(append translators p-table
)))
1031 (let ((p-filters (org-export-backend-filters parent
)))
1032 (list :filters-alist
(append filters p-filters
)))
1033 (let ((p-options (org-export-backend-options parent
)))
1034 (list :options-alist
(append options p-options
)))
1035 (and menu-entry
(list :menu-entry menu-entry
))))
1037 (org-export-barf-if-invalid-backend ',parent
)
1038 ;; Register back-end.
1039 (let ((registeredp (assq ',child org-export-registered-backends
)))
1040 (if registeredp
(setcdr registeredp
',contents
)
1041 (push (cons ',child
',contents
) org-export-registered-backends
)))
1042 ;; Tell parser to not parse EXPORT-BLOCK blocks.
1046 (add-to-list 'org-element-block-name-alist
1047 `(,name . org-element-export-block-parser
)))
1049 ;; Splice in the body, if any.
1052 (defun org-export-backend-parent (backend)
1053 "Return back-end from which BACKEND is derived, or nil."
1054 (plist-get (cdr (assq backend org-export-registered-backends
)) :parent
))
1056 (defun org-export-backend-filters (backend)
1057 "Return filters for BACKEND."
1058 (plist-get (cdr (assq backend org-export-registered-backends
))
1061 (defun org-export-backend-menu (backend)
1062 "Return menu entry for BACKEND."
1063 (plist-get (cdr (assq backend org-export-registered-backends
))
1066 (defun org-export-backend-options (backend)
1067 "Return export options for BACKEND."
1068 (plist-get (cdr (assq backend org-export-registered-backends
))
1071 (defun org-export-backend-translate-table (backend)
1072 "Return translate table for BACKEND."
1073 (plist-get (cdr (assq backend org-export-registered-backends
))
1076 (defun org-export-barf-if-invalid-backend (backend)
1077 "Signal an error if BACKEND isn't defined."
1078 (unless (org-export-backend-translate-table backend
)
1079 (error "Unknown \"%s\" back-end: Aborting export" backend
)))
1081 (defun org-export-derived-backend-p (backend &rest backends
)
1082 "Non-nil if BACKEND is derived from one of BACKENDS."
1083 (let ((parent backend
))
1084 (while (and (not (memq parent backends
))
1085 (setq parent
(org-export-backend-parent parent
))))
1090 ;;; The Communication Channel
1092 ;; During export process, every function has access to a number of
1093 ;; properties. They are of two types:
1095 ;; 1. Environment options are collected once at the very beginning of
1096 ;; the process, out of the original buffer and configuration.
1097 ;; Collecting them is handled by `org-export-get-environment'
1100 ;; Most environment options are defined through the
1101 ;; `org-export-options-alist' variable.
1103 ;; 2. Tree properties are extracted directly from the parsed tree,
1104 ;; just before export, by `org-export-collect-tree-properties'.
1106 ;; Here is the full list of properties available during transcode
1107 ;; process, with their category and their value type.
1109 ;; + `:author' :: Author's name.
1110 ;; - category :: option
1113 ;; + `:back-end' :: Current back-end used for transcoding.
1114 ;; - category :: tree
1117 ;; + `:creator' :: String to write as creation information.
1118 ;; - category :: option
1121 ;; + `:date' :: String to use as date.
1122 ;; - category :: option
1125 ;; + `:description' :: Description text for the current data.
1126 ;; - category :: option
1129 ;; + `:email' :: Author's email.
1130 ;; - category :: option
1133 ;; + `:exclude-tags' :: Tags for exclusion of subtrees from export
1135 ;; - category :: option
1136 ;; - type :: list of strings
1138 ;; + `:exported-data' :: Hash table used for memoizing
1139 ;; `org-export-data'.
1140 ;; - category :: tree
1141 ;; - type :: hash table
1143 ;; + `:filetags' :: List of global tags for buffer. Used by
1144 ;; `org-export-get-tags' to get tags with inheritance.
1145 ;; - category :: option
1146 ;; - type :: list of strings
1148 ;; + `:footnote-definition-alist' :: Alist between footnote labels and
1149 ;; their definition, as parsed data. Only non-inlined footnotes
1150 ;; are represented in this alist. Also, every definition isn't
1151 ;; guaranteed to be referenced in the parse tree. The purpose of
1152 ;; this property is to preserve definitions from oblivion
1153 ;; (i.e. when the parse tree comes from a part of the original
1154 ;; buffer), it isn't meant for direct use in a back-end. To
1155 ;; retrieve a definition relative to a reference, use
1156 ;; `org-export-get-footnote-definition' instead.
1157 ;; - category :: option
1158 ;; - type :: alist (STRING . LIST)
1160 ;; + `:headline-levels' :: Maximum level being exported as an
1161 ;; headline. Comparison is done with the relative level of
1162 ;; headlines in the parse tree, not necessarily with their
1164 ;; - category :: option
1165 ;; - type :: integer
1167 ;; + `:headline-offset' :: Difference between relative and real level
1168 ;; of headlines in the parse tree. For example, a value of -1
1169 ;; means a level 2 headline should be considered as level
1170 ;; 1 (cf. `org-export-get-relative-level').
1171 ;; - category :: tree
1172 ;; - type :: integer
1174 ;; + `:headline-numbering' :: Alist between headlines and their
1175 ;; numbering, as a list of numbers
1176 ;; (cf. `org-export-get-headline-number').
1177 ;; - category :: tree
1178 ;; - type :: alist (INTEGER . LIST)
1180 ;; + `:id-alist' :: Alist between ID strings and destination file's
1181 ;; path, relative to current directory. It is used by
1182 ;; `org-export-resolve-id-link' to resolve ID links targeting an
1184 ;; - category :: option
1185 ;; - type :: alist (STRING . STRING)
1187 ;; + `:ignore-list' :: List of elements and objects that should be
1188 ;; ignored during export.
1189 ;; - category :: tree
1190 ;; - type :: list of elements and objects
1192 ;; + `:input-file' :: Full path to input file, if any.
1193 ;; - category :: option
1194 ;; - type :: string or nil
1196 ;; + `:keywords' :: List of keywords attached to data.
1197 ;; - category :: option
1200 ;; + `:language' :: Default language used for translations.
1201 ;; - category :: option
1204 ;; + `:parse-tree' :: Whole parse tree, available at any time during
1206 ;; - category :: option
1207 ;; - type :: list (as returned by `org-element-parse-buffer')
1209 ;; + `:preserve-breaks' :: Non-nil means transcoding should preserve
1211 ;; - category :: option
1212 ;; - type :: symbol (nil, t)
1214 ;; + `:section-numbers' :: Non-nil means transcoding should add
1215 ;; section numbers to headlines.
1216 ;; - category :: option
1217 ;; - type :: symbol (nil, t)
1219 ;; + `:select-tags' :: List of tags enforcing inclusion of sub-trees
1220 ;; in transcoding. When such a tag is present, subtrees without
1221 ;; it are de facto excluded from the process. See
1222 ;; `use-select-tags'.
1223 ;; - category :: option
1224 ;; - type :: list of strings
1226 ;; + `:time-stamp-file' :: Non-nil means transcoding should insert
1227 ;; a time stamp in the output.
1228 ;; - category :: option
1229 ;; - type :: symbol (nil, t)
1231 ;; + `:translate-alist' :: Alist between element and object types and
1232 ;; transcoding functions relative to the current back-end.
1233 ;; Special keys `inner-template', `template' and `plain-text' are
1235 ;; - category :: option
1236 ;; - type :: alist (SYMBOL . FUNCTION)
1238 ;; + `:with-archived-trees' :: Non-nil when archived subtrees should
1239 ;; also be transcoded. If it is set to the `headline' symbol,
1240 ;; only the archived headline's name is retained.
1241 ;; - category :: option
1242 ;; - type :: symbol (nil, t, `headline')
1244 ;; + `:with-author' :: Non-nil means author's name should be included
1246 ;; - category :: option
1247 ;; - type :: symbol (nil, t)
1249 ;; + `:with-clocks' :: Non-nil means clock keywords should be exported.
1250 ;; - category :: option
1251 ;; - type :: symbol (nil, t)
1253 ;; + `:with-creator' :: Non-nil means a creation sentence should be
1254 ;; inserted at the end of the transcoded string. If the value
1255 ;; is `comment', it should be commented.
1256 ;; - category :: option
1257 ;; - type :: symbol (`comment', nil, t)
1259 ;; + `:with-date' :: Non-nil means output should contain a date.
1260 ;; - category :: option
1261 ;; - type :. symbol (nil, t)
1263 ;; + `:with-drawers' :: Non-nil means drawers should be exported. If
1264 ;; its value is a list of names, only drawers with such names
1265 ;; will be transcoded. If that list starts with `not', drawer
1266 ;; with these names will be skipped.
1267 ;; - category :: option
1268 ;; - type :: symbol (nil, t) or list of strings
1270 ;; + `:with-email' :: Non-nil means output should contain author's
1272 ;; - category :: option
1273 ;; - type :: symbol (nil, t)
1275 ;; + `:with-emphasize' :: Non-nil means emphasized text should be
1277 ;; - category :: option
1278 ;; - type :: symbol (nil, t)
1280 ;; + `:with-fixed-width' :: Non-nil if transcoder should interpret
1281 ;; strings starting with a colon as a fixed-with (verbatim) area.
1282 ;; - category :: option
1283 ;; - type :: symbol (nil, t)
1285 ;; + `:with-footnotes' :: Non-nil if transcoder should interpret
1287 ;; - category :: option
1288 ;; - type :: symbol (nil, t)
1290 ;; + `:with-latex' :: Non-nil means `latex-environment' elements and
1291 ;; `latex-fragment' objects should appear in export output. When
1292 ;; this property is set to `verbatim', they will be left as-is.
1293 ;; - category :: option
1294 ;; - type :: symbol (`verbatim', nil, t)
1296 ;; + `:with-plannings' :: Non-nil means transcoding should include
1298 ;; - category :: option
1299 ;; - type :: symbol (nil, t)
1301 ;; + `:with-priority' :: Non-nil means transcoding should include
1302 ;; priority cookies.
1303 ;; - category :: option
1304 ;; - type :: symbol (nil, t)
1306 ;; + `:with-smart-quotes' :: Non-nil means activate smart quotes in
1308 ;; - category :: option
1309 ;; - type :: symbol (nil, t)
1311 ;; + `:with-special-strings' :: Non-nil means transcoding should
1312 ;; interpret special strings in plain text.
1313 ;; - category :: option
1314 ;; - type :: symbol (nil, t)
1316 ;; + `:with-sub-superscript' :: Non-nil means transcoding should
1317 ;; interpret subscript and superscript. With a value of "{}",
1318 ;; only interpret those using curly brackets.
1319 ;; - category :: option
1320 ;; - type :: symbol (nil, {}, t)
1322 ;; + `:with-tables' :: Non-nil means transcoding should interpret
1324 ;; - category :: option
1325 ;; - type :: symbol (nil, t)
1327 ;; + `:with-tags' :: Non-nil means transcoding should keep tags in
1328 ;; headlines. A `not-in-toc' value will remove them from the
1329 ;; table of contents, if any, nonetheless.
1330 ;; - category :: option
1331 ;; - type :: symbol (nil, t, `not-in-toc')
1333 ;; + `:with-tasks' :: Non-nil means transcoding should include
1334 ;; headlines with a TODO keyword. A `todo' value will only
1335 ;; include headlines with a todo type keyword while a `done'
1336 ;; value will do the contrary. If a list of strings is provided,
1337 ;; only tasks with keywords belonging to that list will be kept.
1338 ;; - category :: option
1339 ;; - type :: symbol (t, todo, done, nil) or list of strings
1341 ;; + `:with-timestamps' :: Non-nil means transcoding should include
1342 ;; time stamps. Special value `active' (resp. `inactive') ask to
1343 ;; export only active (resp. inactive) timestamps. Otherwise,
1344 ;; completely remove them.
1345 ;; - category :: option
1346 ;; - type :: symbol: (`active', `inactive', t, nil)
1348 ;; + `:with-toc' :: Non-nil means that a table of contents has to be
1349 ;; added to the output. An integer value limits its depth.
1350 ;; - category :: option
1351 ;; - type :: symbol (nil, t or integer)
1353 ;; + `:with-todo-keywords' :: Non-nil means transcoding should
1354 ;; include TODO keywords.
1355 ;; - category :: option
1356 ;; - type :: symbol (nil, t)
1359 ;;;; Environment Options
1361 ;; Environment options encompass all parameters defined outside the
1362 ;; scope of the parsed data. They come from five sources, in
1363 ;; increasing precedence order:
1365 ;; - Global variables,
1366 ;; - Buffer's attributes,
1367 ;; - Options keyword symbols,
1368 ;; - Buffer keywords,
1369 ;; - Subtree properties.
1371 ;; The central internal function with regards to environment options
1372 ;; is `org-export-get-environment'. It updates global variables with
1373 ;; "#+BIND:" keywords, then retrieve and prioritize properties from
1374 ;; the different sources.
1376 ;; The internal functions doing the retrieval are:
1377 ;; `org-export--get-global-options',
1378 ;; `org-export--get-buffer-attributes',
1379 ;; `org-export--parse-option-keyword',
1380 ;; `org-export--get-subtree-options' and
1381 ;; `org-export--get-inbuffer-options'
1383 ;; Also, `org-export--install-letbind-maybe' takes care of the part
1384 ;; relative to "#+BIND:" keywords.
1386 (defun org-export-get-environment (&optional backend subtreep ext-plist
)
1387 "Collect export options from the current buffer.
1389 Optional argument BACKEND is a symbol specifying which back-end
1390 specific options to read, if any.
1392 When optional argument SUBTREEP is non-nil, assume the export is
1393 done against the current sub-tree.
1395 Third optional argument EXT-PLIST is a property list with
1396 external parameters overriding Org default settings, but still
1397 inferior to file-local settings."
1398 ;; First install #+BIND variables.
1399 (org-export--install-letbind-maybe)
1400 ;; Get and prioritize export options...
1402 ;; ... from global variables...
1403 (org-export--get-global-options backend
)
1404 ;; ... from an external property list...
1406 ;; ... from in-buffer settings...
1407 (org-export--get-inbuffer-options
1409 (and buffer-file-name
(org-remove-double-quotes buffer-file-name
)))
1410 ;; ... and from subtree, when appropriate.
1411 (and subtreep
(org-export--get-subtree-options backend
))
1412 ;; Eventually add misc. properties.
1417 (org-export-backend-translate-table backend
)
1418 :footnote-definition-alist
1419 ;; Footnotes definitions must be collected in the original
1420 ;; buffer, as there's no insurance that they will still be in
1421 ;; the parse tree, due to possible narrowing.
1423 (org-with-wide-buffer
1424 (goto-char (point-min))
1425 (while (re-search-forward org-footnote-definition-re nil t
)
1426 (let ((def (save-match-data (org-element-at-point))))
1427 (when (eq (org-element-type def
) 'footnote-definition
)
1429 (cons (org-element-property :label def
)
1430 (let ((cbeg (org-element-property :contents-begin def
)))
1432 (org-element--parse-elements
1433 cbeg
(org-element-property :contents-end def
)
1434 nil nil nil nil
(list 'org-data nil
)))))
1438 ;; Collect id references.
1440 (org-with-wide-buffer
1441 (goto-char (point-min))
1442 (while (re-search-forward "\\[\\[id:\\S-+?\\]" nil t
)
1443 (let ((link (org-element-context)))
1444 (when (eq (org-element-type link
) 'link
)
1445 (let* ((id (org-element-property :path link
))
1446 (file (org-id-find-id-file id
)))
1448 (push (cons id
(file-relative-name file
)) alist
)))))))
1451 (defun org-export--parse-option-keyword (options &optional backend
)
1452 "Parse an OPTIONS line and return values as a plist.
1453 Optional argument BACKEND is a symbol specifying which back-end
1454 specific items to read, if any."
1456 ;; Priority is given to back-end specific options.
1457 (append (and backend
(org-export-backend-options backend
))
1458 org-export-options-alist
))
1460 (dolist (option all
)
1461 (let ((property (car option
))
1462 (item (nth 2 option
)))
1464 (not (plist-member plist property
))
1465 (string-match (concat "\\(\\`\\|[ \t]\\)"
1467 ":\\(([^)\n]+)\\|[^ \t\n\r;,.]*\\)")
1469 (setq plist
(plist-put plist
1471 (car (read-from-string
1472 (match-string 2 options
))))))))
1475 (defun org-export--get-subtree-options (&optional backend
)
1476 "Get export options in subtree at point.
1477 Optional argument BACKEND is a symbol specifying back-end used
1478 for export. Return options as a plist."
1479 ;; For each buffer keyword, create an headline property setting the
1480 ;; same property in communication channel. The name for the property
1481 ;; is the keyword with "EXPORT_" appended to it.
1482 (org-with-wide-buffer
1484 ;; Make sure point is at an heading.
1485 (if (org-at-heading-p) (org-up-heading-safe) (org-back-to-heading t
))
1486 ;; Take care of EXPORT_TITLE. If it isn't defined, use headline's
1487 ;; title as its fallback value.
1488 (when (setq prop
(or (org-entry-get (point) "EXPORT_TITLE")
1489 (progn (looking-at org-todo-line-regexp
)
1490 (org-match-string-no-properties 3))))
1494 (org-element-parse-secondary-string
1495 prop
(org-element-restriction 'keyword
)))))
1496 ;; EXPORT_OPTIONS are parsed in a non-standard way.
1497 (when (setq prop
(org-entry-get (point) "EXPORT_OPTIONS"))
1499 (nconc plist
(org-export--parse-option-keyword prop backend
))))
1500 ;; Handle other keywords. TITLE keyword is excluded as it has
1501 ;; been handled already.
1502 (let ((seen '("TITLE")))
1505 (let ((property (car option
))
1506 (keyword (nth 1 option
)))
1507 (when (and keyword
(not (member keyword seen
)))
1508 (let* ((subtree-prop (concat "EXPORT_" keyword
))
1509 ;; Export properties are not case-sensitive.
1510 (value (let ((case-fold-search t
))
1511 (org-entry-get (point) subtree-prop
))))
1513 (when (and value
(not (plist-member plist property
)))
1519 ;; Parse VALUE if required.
1520 ((member keyword org-element-document-properties
)
1521 (org-element-parse-secondary-string
1522 value
(org-element-restriction 'keyword
)))
1523 ;; If BEHAVIOUR is `split' expected value is
1524 ;; a list of strings, not a string.
1525 ((eq (nth 4 option
) 'split
) (org-split-string value
))
1527 ;; Look for both general keywords and back-end specific
1528 ;; options, with priority given to the latter.
1529 (append (and backend
(org-export-backend-options backend
))
1530 org-export-options-alist
)))
1534 (defun org-export--get-inbuffer-options (&optional backend files
)
1535 "Return current buffer export options, as a plist.
1537 Optional argument BACKEND, when non-nil, is a symbol specifying
1538 which back-end specific options should also be read in the
1541 Optional argument FILES is a list of setup files names read so
1542 far, used to avoid circular dependencies.
1544 Assume buffer is in Org mode. Narrowing, if any, is ignored."
1545 (org-with-wide-buffer
1546 (goto-char (point-min))
1547 (let ((case-fold-search t
) plist
)
1548 ;; 1. Special keywords, as in `org-export-special-keywords'.
1550 (format "^[ \t]*#\\+%s:" (regexp-opt org-export-special-keywords
))))
1551 (while (re-search-forward special-re nil t
)
1552 (let ((element (org-element-at-point)))
1553 (when (eq (org-element-type element
) 'keyword
)
1554 (let* ((key (org-element-property :key element
))
1555 (val (org-element-property :value element
))
1558 ((equal key
"SETUPFILE")
1561 (org-remove-double-quotes (org-trim val
)))))
1562 ;; Avoid circular dependencies.
1563 (unless (member file files
)
1565 (insert (org-file-contents file
'noerror
))
1567 (org-export--get-inbuffer-options
1568 backend
(cons file files
))))))
1569 ((equal key
"OPTIONS")
1570 (org-export--parse-option-keyword val backend
))
1571 ((equal key
"FILETAGS")
1574 (append (org-split-string val
":")
1575 (plist-get plist
:filetags
))))))))
1576 (setq plist
(org-combine-plists plist prop
)))))))
1577 ;; 2. Standard options, as in `org-export-options-alist'.
1579 ;; Priority is given to back-end specific options.
1580 (and backend
(org-export-backend-options backend
))
1581 org-export-options-alist
)))
1582 (dolist (option all
)
1583 (let ((prop (car option
)))
1584 (when (and (nth 1 option
) (not (plist-member plist prop
)))
1585 (goto-char (point-min))
1586 (let ((opt-re (format "^[ \t]*#\\+%s:" (nth 1 option
)))
1587 (behaviour (nth 4 option
)))
1588 (while (re-search-forward opt-re nil t
)
1589 (let ((element (org-element-at-point)))
1590 (when (eq (org-element-type element
) 'keyword
)
1591 (let((key (org-element-property :key element
))
1592 (val (org-element-property :value element
)))
1596 ;; Handle value depending on specified
1600 (if (not (plist-get plist prop
)) (org-trim val
)
1601 (concat (plist-get plist prop
)
1605 (org-trim (concat (plist-get plist prop
)
1608 (split `(,@(plist-get plist prop
)
1609 ,@(org-split-string val
)))
1612 (if (not (plist-member plist prop
)) val
1613 (plist-get plist prop
))))))))))))))
1614 ;; Parse keywords specified in
1615 ;; `org-element-document-properties'.
1618 ;; Find the property associated to the keyword.
1619 (let* ((prop (catch 'found
1620 (mapc (lambda (option)
1621 (when (equal (nth 1 option
) key
)
1622 (throw 'found
(car option
))))
1624 (value (and prop
(plist-get plist prop
))))
1625 (when (stringp value
)
1629 (org-element-parse-secondary-string
1630 value
(org-element-restriction 'keyword
)))))))
1631 org-element-document-properties
))
1632 ;; 3. Return final value.
1635 (defun org-export--get-buffer-attributes ()
1636 "Return properties related to buffer attributes, as a plist."
1637 (let ((visited-file (buffer-file-name (buffer-base-buffer))))
1639 ;; Store full path of input file name, or nil. For internal use.
1640 :input-file visited-file
1641 :title
(or (and visited-file
1642 (file-name-sans-extension
1643 (file-name-nondirectory visited-file
)))
1644 (buffer-name (buffer-base-buffer))))))
1646 (defun org-export--get-global-options (&optional backend
)
1647 "Return global export options as a plist.
1649 Optional argument BACKEND, if non-nil, is a symbol specifying
1650 which back-end specific export options should also be read in the
1653 ;; Priority is given to back-end specific options.
1654 (append (and backend
(org-export-backend-options backend
))
1655 org-export-options-alist
))
1659 (let ((prop (car cell
)))
1660 (unless (plist-member plist prop
)
1665 ;; Eval default value provided. If keyword is a member
1666 ;; of `org-element-document-properties', parse it as
1667 ;; a secondary string before storing it.
1668 (let ((value (eval (nth 3 cell
))))
1669 (if (not (stringp value
)) value
1670 (let ((keyword (nth 1 cell
)))
1671 (if (not (member keyword org-element-document-properties
))
1673 (org-element-parse-secondary-string
1674 value
(org-element-restriction 'keyword
)))))))))))
1679 (defun org-export--install-letbind-maybe ()
1680 "Install the values from #+BIND lines as local variables.
1681 Variables must be installed before in-buffer options are
1683 (when org-export-allow-bind-keywords
1684 (let ((case-fold-search t
) letbind
)
1685 (org-with-wide-buffer
1686 (goto-char (point-min))
1687 (while (re-search-forward "^[ \t]*#\\+BIND:" nil t
)
1688 (let* ((element (org-element-at-point))
1689 (value (org-element-property :value element
)))
1690 (when (and (eq (org-element-type element
) 'keyword
)
1691 (not (equal value
"")))
1692 (push (read (format "(%s)" value
)) letbind
)))))
1693 (dolist (pair (nreverse letbind
))
1694 (org-set-local (car pair
) (nth 1 pair
))))))
1697 ;;;; Tree Properties
1699 ;; Tree properties are information extracted from parse tree. They
1700 ;; are initialized at the beginning of the transcoding process by
1701 ;; `org-export-collect-tree-properties'.
1703 ;; Dedicated functions focus on computing the value of specific tree
1704 ;; properties during initialization. Thus,
1705 ;; `org-export--populate-ignore-list' lists elements and objects that
1706 ;; should be skipped during export, `org-export--get-min-level' gets
1707 ;; the minimal exportable level, used as a basis to compute relative
1708 ;; level for headlines. Eventually
1709 ;; `org-export--collect-headline-numbering' builds an alist between
1710 ;; headlines and their numbering.
1712 (defun org-export-collect-tree-properties (data info
)
1713 "Extract tree properties from parse tree.
1715 DATA is the parse tree from which information is retrieved. INFO
1716 is a list holding export options.
1718 Following tree properties are set or updated:
1720 `:exported-data' Hash table used to memoize results from
1723 `:footnote-definition-alist' List of footnotes definitions in
1724 original buffer and current parse tree.
1726 `:headline-offset' Offset between true level of headlines and
1727 local level. An offset of -1 means an headline
1728 of level 2 should be considered as a level
1729 1 headline in the context.
1731 `:headline-numbering' Alist of all headlines as key an the
1732 associated numbering as value.
1734 `:ignore-list' List of elements that should be ignored during
1737 Return updated plist."
1738 ;; Install the parse tree in the communication channel, in order to
1739 ;; use `org-export-get-genealogy' and al.
1740 (setq info
(plist-put info
:parse-tree data
))
1741 ;; Get the list of elements and objects to ignore, and put it into
1742 ;; `:ignore-list'. Do not overwrite any user ignore that might have
1743 ;; been done during parse tree filtering.
1747 (append (org-export--populate-ignore-list data info
)
1748 (plist-get info
:ignore-list
))))
1749 ;; Compute `:headline-offset' in order to be able to use
1750 ;; `org-export-get-relative-level'.
1754 (- 1 (org-export--get-min-level data info
))))
1755 ;; Update footnotes definitions list with definitions in parse tree.
1756 ;; This is required since buffer expansion might have modified
1757 ;; boundaries of footnote definitions contained in the parse tree.
1758 ;; This way, definitions in `footnote-definition-alist' are bound to
1759 ;; match those in the parse tree.
1760 (let ((defs (plist-get info
:footnote-definition-alist
)))
1761 (org-element-map data
'footnote-definition
1763 (push (cons (org-element-property :label fn
)
1764 `(org-data nil
,@(org-element-contents fn
)))
1766 (setq info
(plist-put info
:footnote-definition-alist defs
)))
1767 ;; Properties order doesn't matter: get the rest of the tree
1770 `(:headline-numbering
,(org-export--collect-headline-numbering data info
)
1771 :exported-data
,(make-hash-table :test
'eq
:size
4001))
1774 (defun org-export--get-min-level (data options
)
1775 "Return minimum exportable headline's level in DATA.
1776 DATA is parsed tree as returned by `org-element-parse-buffer'.
1777 OPTIONS is a plist holding export options."
1779 (let ((min-level 10000))
1782 (when (and (eq (org-element-type blob
) 'headline
)
1783 (not (memq blob
(plist-get options
:ignore-list
))))
1785 (min (org-element-property :level blob
) min-level
)))
1786 (when (= min-level
1) (throw 'exit
1)))
1787 (org-element-contents data
))
1788 ;; If no headline was found, for the sake of consistency, set
1789 ;; minimum level to 1 nonetheless.
1790 (if (= min-level
10000) 1 min-level
))))
1792 (defun org-export--collect-headline-numbering (data options
)
1793 "Return numbering of all exportable headlines in a parse tree.
1795 DATA is the parse tree. OPTIONS is the plist holding export
1798 Return an alist whose key is an headline and value is its
1799 associated numbering \(in the shape of a list of numbers\) or nil
1800 for a footnotes section."
1801 (let ((numbering (make-vector org-export-max-depth
0)))
1802 (org-element-map data
'headline
1804 (unless (org-element-property :footnote-section-p headline
)
1805 (let ((relative-level
1806 (1- (org-export-get-relative-level headline options
))))
1809 (loop for n across numbering
1810 for idx from
0 to org-export-max-depth
1811 when
(< idx relative-level
) collect n
1812 when
(= idx relative-level
) collect
(aset numbering idx
(1+ n
))
1813 when
(> idx relative-level
) do
(aset numbering idx
0))))))
1816 (defun org-export--populate-ignore-list (data options
)
1817 "Return list of elements and objects to ignore during export.
1818 DATA is the parse tree to traverse. OPTIONS is the plist holding
1822 ;; First find trees containing a select tag, if any.
1823 (selected (org-export--selected-trees data options
))
1826 ;; Collect ignored elements or objects into IGNORE-LIST.
1827 (let ((type (org-element-type data
)))
1828 (if (org-export--skip-p data options selected
) (push data ignore
)
1829 (if (and (eq type
'headline
)
1830 (eq (plist-get options
:with-archived-trees
) 'headline
)
1831 (org-element-property :archivedp data
))
1832 ;; If headline is archived but tree below has
1833 ;; to be skipped, add it to ignore list.
1834 (mapc (lambda (e) (push e ignore
))
1835 (org-element-contents data
))
1836 ;; Move into secondary string, if any.
1838 (cdr (assq type org-element-secondary-value-alist
))))
1840 (mapc walk-data
(org-element-property sec-prop data
))))
1841 ;; Move into recursive objects/elements.
1842 (mapc walk-data
(org-element-contents data
))))))))
1844 (funcall walk-data data
)
1848 (defun org-export--selected-trees (data info
)
1849 "Return list of headlines containing a select tag in their tree.
1850 DATA is parsed data as returned by `org-element-parse-buffer'.
1851 INFO is a plist holding export options."
1852 (let* (selected-trees
1853 walk-data
; for byte-compiler.
1856 (lambda (data genealogy
)
1857 (case (org-element-type data
)
1858 (org-data (mapc (lambda (el) (funcall walk-data el genealogy
))
1859 (org-element-contents data
)))
1861 (let ((tags (org-element-property :tags data
)))
1862 (if (loop for tag in
(plist-get info
:select-tags
)
1863 thereis
(member tag tags
))
1864 ;; When a select tag is found, mark full
1865 ;; genealogy and every headline within the tree
1867 (setq selected-trees
1870 (org-element-map data
'headline
'identity
)
1872 ;; Else, continue searching in tree, recursively.
1874 (lambda (el) (funcall walk-data el
(cons data genealogy
)))
1875 (org-element-contents data
))))))))))
1876 (funcall walk-data data nil
) selected-trees
))
1878 (defun org-export--skip-p (blob options selected
)
1879 "Non-nil when element or object BLOB should be skipped during export.
1880 OPTIONS is the plist holding export options. SELECTED, when
1881 non-nil, is a list of headlines belonging to a tree with a select
1883 (case (org-element-type blob
)
1884 (clock (not (plist-get options
:with-clocks
)))
1886 (let ((with-drawers-p (plist-get options
:with-drawers
)))
1887 (or (not with-drawers-p
)
1888 (and (consp with-drawers-p
)
1889 ;; If `:with-drawers' value starts with `not', ignore
1890 ;; every drawer whose name belong to that list.
1891 ;; Otherwise, ignore drawers whose name isn't in that
1893 (let ((name (org-element-property :drawer-name blob
)))
1894 (if (eq (car with-drawers-p
) 'not
)
1895 (member-ignore-case name
(cdr with-drawers-p
))
1896 (not (member-ignore-case name with-drawers-p
))))))))
1898 (let ((with-tasks (plist-get options
:with-tasks
))
1899 (todo (org-element-property :todo-keyword blob
))
1900 (todo-type (org-element-property :todo-type blob
))
1901 (archived (plist-get options
:with-archived-trees
))
1902 (tags (org-element-property :tags blob
)))
1904 ;; Ignore subtrees with an exclude tag.
1905 (loop for k in
(plist-get options
:exclude-tags
)
1906 thereis
(member k tags
))
1907 ;; When a select tag is present in the buffer, ignore any tree
1909 (and selected
(not (memq blob selected
)))
1910 ;; Ignore commented sub-trees.
1911 (org-element-property :commentedp blob
)
1912 ;; Ignore archived subtrees if `:with-archived-trees' is nil.
1913 (and (not archived
) (org-element-property :archivedp blob
))
1914 ;; Ignore tasks, if specified by `:with-tasks' property.
1916 (or (not with-tasks
)
1917 (and (memq with-tasks
'(todo done
))
1918 (not (eq todo-type with-tasks
)))
1919 (and (consp with-tasks
) (not (member todo with-tasks
))))))))
1920 (inlinetask (not (plist-get options
:with-inlinetasks
)))
1921 ((latex-environment latex-fragment
) (not (plist-get options
:with-latex
)))
1922 (planning (not (plist-get options
:with-plannings
)))
1923 (statistics-cookie (not (plist-get options
:with-statistics-cookies
)))
1925 (and (org-export-table-has-special-column-p
1926 (org-export-get-parent-table blob
))
1927 (not (org-export-get-previous-element blob options
))))
1928 (table-row (org-export-table-row-is-special-p blob options
))
1930 (case (plist-get options
:with-timestamps
)
1931 ;; No timestamp allowed.
1933 ;; Only active timestamps allowed and the current one isn't
1936 (not (memq (org-element-property :type blob
)
1937 '(active active-range
))))
1938 ;; Only inactive timestamps allowed and the current one isn't
1941 (not (memq (org-element-property :type blob
)
1942 '(inactive inactive-range
))))))))
1948 ;; `org-export-data' reads a parse tree (obtained with, i.e.
1949 ;; `org-element-parse-buffer') and transcodes it into a specified
1950 ;; back-end output. It takes care of filtering out elements or
1951 ;; objects according to export options and organizing the output blank
1952 ;; lines and white space are preserved. The function memoizes its
1953 ;; results, so it is cheap to call it within translators.
1955 ;; It is possible to modify locally the back-end used by
1956 ;; `org-export-data' or even use a temporary back-end by using
1957 ;; `org-export-data-with-translations' and
1958 ;; `org-export-data-with-backend'.
1960 ;; Internally, three functions handle the filtering of objects and
1961 ;; elements during the export. In particular,
1962 ;; `org-export-ignore-element' marks an element or object so future
1963 ;; parse tree traversals skip it, `org-export--interpret-p' tells which
1964 ;; elements or objects should be seen as real Org syntax and
1965 ;; `org-export-expand' transforms the others back into their original
1968 ;; `org-export-transcoder' is an accessor returning appropriate
1969 ;; translator function for a given element or object.
1971 (defun org-export-transcoder (blob info
)
1972 "Return appropriate transcoder for BLOB.
1973 INFO is a plist containing export directives."
1974 (let ((type (org-element-type blob
)))
1975 ;; Return contents only for complete parse trees.
1976 (if (eq type
'org-data
) (lambda (blob contents info
) contents
)
1977 (let ((transcoder (cdr (assq type
(plist-get info
:translate-alist
)))))
1978 (and (functionp transcoder
) transcoder
)))))
1980 (defun org-export-data (data info
)
1981 "Convert DATA into current back-end format.
1983 DATA is a parse tree, an element or an object or a secondary
1984 string. INFO is a plist holding export options.
1986 Return transcoded string."
1987 (let ((memo (gethash data
(plist-get info
:exported-data
) 'no-memo
)))
1988 (if (not (eq memo
'no-memo
)) memo
1989 (let* ((type (org-element-type data
))
1992 ;; Ignored element/object.
1993 ((memq data
(plist-get info
:ignore-list
)) nil
)
1995 ((eq type
'plain-text
)
1996 (org-export-filter-apply-functions
1997 (plist-get info
:filter-plain-text
)
1998 (let ((transcoder (org-export-transcoder data info
)))
1999 (if transcoder
(funcall transcoder data info
) data
))
2001 ;; Uninterpreted element/object: change it back to Org
2002 ;; syntax and export again resulting raw string.
2003 ((not (org-export--interpret-p data info
))
2007 (mapconcat (lambda (blob) (org-export-data blob info
))
2008 (org-element-contents data
)
2011 ;; Secondary string.
2013 (mapconcat (lambda (obj) (org-export-data obj info
)) data
""))
2014 ;; Element/Object without contents or, as a special case,
2015 ;; headline with archive tag and archived trees restricted
2017 ((or (not (org-element-contents data
))
2018 (and (eq type
'headline
)
2019 (eq (plist-get info
:with-archived-trees
) 'headline
)
2020 (org-element-property :archivedp data
)))
2021 (let ((transcoder (org-export-transcoder data info
)))
2022 (and (functionp transcoder
)
2023 (funcall transcoder data nil info
))))
2024 ;; Element/Object with contents.
2026 (let ((transcoder (org-export-transcoder data info
)))
2028 (let* ((greaterp (memq type org-element-greater-elements
))
2031 (memq type org-element-recursive-objects
)))
2034 (lambda (element) (org-export-data element info
))
2035 (org-element-contents
2036 (if (or greaterp objectp
) data
2037 ;; Elements directly containing objects
2038 ;; must have their indentation normalized
2040 (org-element-normalize-contents
2042 ;; When normalizing contents of the first
2043 ;; paragraph in an item or a footnote
2044 ;; definition, ignore first line's
2045 ;; indentation: there is none and it
2046 ;; might be misleading.
2047 (when (eq type
'paragraph
)
2048 (let ((parent (org-export-get-parent data
)))
2050 (eq (car (org-element-contents parent
))
2052 (memq (org-element-type parent
)
2053 '(footnote-definition item
))))))))
2055 (funcall transcoder data
2056 (if (not greaterp
) contents
2057 (org-element-normalize-string contents
))
2059 ;; Final result will be memoized before being returned.
2064 ((memq type
'(org-data plain-text nil
)) results
)
2065 ;; Append the same white space between elements or objects as in
2066 ;; the original buffer, and call appropriate filters.
2069 (org-export-filter-apply-functions
2070 (plist-get info
(intern (format ":filter-%s" type
)))
2071 (let ((post-blank (or (org-element-property :post-blank data
)
2073 (if (memq type org-element-all-elements
)
2074 (concat (org-element-normalize-string results
)
2075 (make-string post-blank ?
\n))
2076 (concat results
(make-string post-blank ?
))))
2079 (plist-get info
:exported-data
))))))
2081 (defun org-export-data-with-translations (data translations info
)
2082 "Convert DATA into another format using a given translation table.
2083 DATA is an element, an object, a secondary string or a string.
2084 TRANSLATIONS is an alist between element or object types and
2085 a functions handling them. See `org-export-define-backend' for
2086 more information. INFO is a plist used as a communication
2090 ;; Set-up a new communication channel with TRANSLATIONS as the
2091 ;; translate table and a new hash table for memoization.
2094 (list :translate-alist translations
2095 ;; Size of the hash table is reduced since this function
2096 ;; will probably be used on short trees.
2097 :exported-data
(make-hash-table :test
'eq
:size
401)))))
2099 (defun org-export-data-with-backend (data backend info
)
2100 "Convert DATA into BACKEND format.
2102 DATA is an element, an object, a secondary string or a string.
2103 BACKEND is a symbol. INFO is a plist used as a communication
2106 Unlike to `org-export-with-backend', this function will
2107 recursively convert DATA using BACKEND translation table."
2108 (org-export-barf-if-invalid-backend backend
)
2109 (org-export-data-with-translations
2110 data
(org-export-backend-translate-table backend
) info
))
2112 (defun org-export--interpret-p (blob info
)
2113 "Non-nil if element or object BLOB should be interpreted during export.
2114 If nil, BLOB will appear as raw Org syntax. Check is done
2115 according to export options INFO, stored as a plist."
2116 (case (org-element-type blob
)
2118 (entity (plist-get info
:with-entities
))
2120 ((bold italic strike-through underline
)
2121 (plist-get info
:with-emphasize
))
2122 ;; ... fixed-width areas.
2123 (fixed-width (plist-get info
:with-fixed-width
))
2125 ((footnote-definition footnote-reference
)
2126 (plist-get info
:with-footnotes
))
2127 ;; ... LaTeX environments and fragments...
2128 ((latex-environment latex-fragment
)
2129 (let ((with-latex-p (plist-get info
:with-latex
)))
2130 (and with-latex-p
(not (eq with-latex-p
'verbatim
)))))
2131 ;; ... sub/superscripts...
2132 ((subscript superscript
)
2133 (let ((sub/super-p
(plist-get info
:with-sub-superscript
)))
2134 (if (eq sub
/super-p
'{})
2135 (org-element-property :use-brackets-p blob
)
2138 (table (plist-get info
:with-tables
))
2141 (defun org-export-expand (blob contents
)
2142 "Expand a parsed element or object to its original state.
2143 BLOB is either an element or an object. CONTENTS is its
2144 contents, as a string or nil."
2146 (intern (format "org-element-%s-interpreter" (org-element-type blob
)))
2149 (defun org-export-ignore-element (element info
)
2150 "Add ELEMENT to `:ignore-list' in INFO.
2152 Any element in `:ignore-list' will be skipped when using
2153 `org-element-map'. INFO is modified by side effects."
2154 (plist-put info
:ignore-list
(cons element
(plist-get info
:ignore-list
))))
2158 ;;; The Filter System
2160 ;; Filters allow end-users to tweak easily the transcoded output.
2161 ;; They are the functional counterpart of hooks, as every filter in
2162 ;; a set is applied to the return value of the previous one.
2164 ;; Every set is back-end agnostic. Although, a filter is always
2165 ;; called, in addition to the string it applies to, with the back-end
2166 ;; used as argument, so it's easy for the end-user to add back-end
2167 ;; specific filters in the set. The communication channel, as
2168 ;; a plist, is required as the third argument.
2170 ;; From the developer side, filters sets can be installed in the
2171 ;; process with the help of `org-export-define-backend', which
2172 ;; internally stores filters as an alist. Each association has a key
2173 ;; among the following symbols and a function or a list of functions
2176 ;; - `:filter-options' applies to the property list containing export
2177 ;; options. Unlike to other filters, functions in this list accept
2178 ;; two arguments instead of three: the property list containing
2179 ;; export options and the back-end. Users can set its value through
2180 ;; `org-export-filter-options-functions' variable.
2182 ;; - `:filter-parse-tree' applies directly to the complete parsed
2183 ;; tree. Users can set it through
2184 ;; `org-export-filter-parse-tree-functions' variable.
2186 ;; - `:filter-final-output' applies to the final transcoded string.
2187 ;; Users can set it with `org-export-filter-final-output-functions'
2190 ;; - `:filter-plain-text' applies to any string not recognized as Org
2191 ;; syntax. `org-export-filter-plain-text-functions' allows users to
2194 ;; - `:filter-TYPE' applies on the string returned after an element or
2195 ;; object of type TYPE has been transcoded. An user can modify
2196 ;; `org-export-filter-TYPE-functions'
2198 ;; All filters sets are applied with
2199 ;; `org-export-filter-apply-functions' function. Filters in a set are
2200 ;; applied in a LIFO fashion. It allows developers to be sure that
2201 ;; their filters will be applied first.
2203 ;; Filters properties are installed in communication channel with
2204 ;; `org-export-install-filters' function.
2206 ;; Eventually, two hooks (`org-export-before-processing-hook' and
2207 ;; `org-export-before-parsing-hook') are run at the beginning of the
2208 ;; export process and just before parsing to allow for heavy structure
2214 (defvar org-export-before-processing-hook nil
2215 "Hook run at the beginning of the export process.
2217 This is run before include keywords and macros are expanded and
2218 Babel code blocks executed, on a copy of the original buffer
2219 being exported. Visibility and narrowing are preserved. Point
2220 is at the beginning of the buffer.
2222 Every function in this hook will be called with one argument: the
2223 back-end currently used, as a symbol.")
2225 (defvar org-export-before-parsing-hook nil
2226 "Hook run before parsing an export buffer.
2228 This is run after include keywords and macros have been expanded
2229 and Babel code blocks executed, on a copy of the original buffer
2230 being exported. Visibility and narrowing are preserved. Point
2231 is at the beginning of the buffer.
2233 Every function in this hook will be called with one argument: the
2234 back-end currently used, as a symbol.")
2237 ;;;; Special Filters
2239 (defvar org-export-filter-options-functions nil
2240 "List of functions applied to the export options.
2241 Each filter is called with two arguments: the export options, as
2242 a plist, and the back-end, as a symbol. It must return
2243 a property list containing export options.")
2245 (defvar org-export-filter-parse-tree-functions nil
2246 "List of functions applied to the parsed tree.
2247 Each filter is called with three arguments: the parse tree, as
2248 returned by `org-element-parse-buffer', the back-end, as
2249 a symbol, and the communication channel, as a plist. It must
2250 return the modified parse tree to transcode.")
2252 (defvar org-export-filter-plain-text-functions nil
2253 "List of functions applied to plain text.
2254 Each filter is called with three arguments: a string which
2255 contains no Org syntax, the back-end, as a symbol, and the
2256 communication channel, as a plist. It must return a string or
2259 (defvar org-export-filter-final-output-functions nil
2260 "List of functions applied to the transcoded string.
2261 Each filter is called with three arguments: the full transcoded
2262 string, the back-end, as a symbol, and the communication channel,
2263 as a plist. It must return a string that will be used as the
2264 final export output.")
2267 ;;;; Elements Filters
2269 (defvar org-export-filter-babel-call-functions nil
2270 "List of functions applied to a transcoded babel-call.
2271 Each filter is called with three arguments: the transcoded data,
2272 as a string, the back-end, as a symbol, and the communication
2273 channel, as a plist. It must return a string or nil.")
2275 (defvar org-export-filter-center-block-functions nil
2276 "List of functions applied to a transcoded center block.
2277 Each filter is called with three arguments: the transcoded data,
2278 as a string, the back-end, as a symbol, and the communication
2279 channel, as a plist. It must return a string or nil.")
2281 (defvar org-export-filter-clock-functions nil
2282 "List of functions applied to a transcoded clock.
2283 Each filter is called with three arguments: the transcoded data,
2284 as a string, the back-end, as a symbol, and the communication
2285 channel, as a plist. It must return a string or nil.")
2287 (defvar org-export-filter-comment-functions nil
2288 "List of functions applied to a transcoded comment.
2289 Each filter is called with three arguments: the transcoded data,
2290 as a string, the back-end, as a symbol, and the communication
2291 channel, as a plist. It must return a string or nil.")
2293 (defvar org-export-filter-comment-block-functions nil
2294 "List of functions applied to a transcoded comment-block.
2295 Each filter is called with three arguments: the transcoded data,
2296 as a string, the back-end, as a symbol, and the communication
2297 channel, as a plist. It must return a string or nil.")
2299 (defvar org-export-filter-diary-sexp-functions nil
2300 "List of functions applied to a transcoded diary-sexp.
2301 Each filter is called with three arguments: the transcoded data,
2302 as a string, the back-end, as a symbol, and the communication
2303 channel, as a plist. It must return a string or nil.")
2305 (defvar org-export-filter-drawer-functions nil
2306 "List of functions applied to a transcoded drawer.
2307 Each filter is called with three arguments: the transcoded data,
2308 as a string, the back-end, as a symbol, and the communication
2309 channel, as a plist. It must return a string or nil.")
2311 (defvar org-export-filter-dynamic-block-functions nil
2312 "List of functions applied to a transcoded dynamic-block.
2313 Each filter is called with three arguments: the transcoded data,
2314 as a string, the back-end, as a symbol, and the communication
2315 channel, as a plist. It must return a string or nil.")
2317 (defvar org-export-filter-example-block-functions nil
2318 "List of functions applied to a transcoded example-block.
2319 Each filter is called with three arguments: the transcoded data,
2320 as a string, the back-end, as a symbol, and the communication
2321 channel, as a plist. It must return a string or nil.")
2323 (defvar org-export-filter-export-block-functions nil
2324 "List of functions applied to a transcoded export-block.
2325 Each filter is called with three arguments: the transcoded data,
2326 as a string, the back-end, as a symbol, and the communication
2327 channel, as a plist. It must return a string or nil.")
2329 (defvar org-export-filter-fixed-width-functions nil
2330 "List of functions applied to a transcoded fixed-width.
2331 Each filter is called with three arguments: the transcoded data,
2332 as a string, the back-end, as a symbol, and the communication
2333 channel, as a plist. It must return a string or nil.")
2335 (defvar org-export-filter-footnote-definition-functions nil
2336 "List of functions applied to a transcoded footnote-definition.
2337 Each filter is called with three arguments: the transcoded data,
2338 as a string, the back-end, as a symbol, and the communication
2339 channel, as a plist. It must return a string or nil.")
2341 (defvar org-export-filter-headline-functions nil
2342 "List of functions applied to a transcoded headline.
2343 Each filter is called with three arguments: the transcoded data,
2344 as a string, the back-end, as a symbol, and the communication
2345 channel, as a plist. It must return a string or nil.")
2347 (defvar org-export-filter-horizontal-rule-functions nil
2348 "List of functions applied to a transcoded horizontal-rule.
2349 Each filter is called with three arguments: the transcoded data,
2350 as a string, the back-end, as a symbol, and the communication
2351 channel, as a plist. It must return a string or nil.")
2353 (defvar org-export-filter-inlinetask-functions nil
2354 "List of functions applied to a transcoded inlinetask.
2355 Each filter is called with three arguments: the transcoded data,
2356 as a string, the back-end, as a symbol, and the communication
2357 channel, as a plist. It must return a string or nil.")
2359 (defvar org-export-filter-item-functions nil
2360 "List of functions applied to a transcoded item.
2361 Each filter is called with three arguments: the transcoded data,
2362 as a string, the back-end, as a symbol, and the communication
2363 channel, as a plist. It must return a string or nil.")
2365 (defvar org-export-filter-keyword-functions nil
2366 "List of functions applied to a transcoded keyword.
2367 Each filter is called with three arguments: the transcoded data,
2368 as a string, the back-end, as a symbol, and the communication
2369 channel, as a plist. It must return a string or nil.")
2371 (defvar org-export-filter-latex-environment-functions nil
2372 "List of functions applied to a transcoded latex-environment.
2373 Each filter is called with three arguments: the transcoded data,
2374 as a string, the back-end, as a symbol, and the communication
2375 channel, as a plist. It must return a string or nil.")
2377 (defvar org-export-filter-node-property-functions nil
2378 "List of functions applied to a transcoded node-property.
2379 Each filter is called with three arguments: the transcoded data,
2380 as a string, the back-end, as a symbol, and the communication
2381 channel, as a plist. It must return a string or nil.")
2383 (defvar org-export-filter-paragraph-functions nil
2384 "List of functions applied to a transcoded paragraph.
2385 Each filter is called with three arguments: the transcoded data,
2386 as a string, the back-end, as a symbol, and the communication
2387 channel, as a plist. It must return a string or nil.")
2389 (defvar org-export-filter-plain-list-functions nil
2390 "List of functions applied to a transcoded plain-list.
2391 Each filter is called with three arguments: the transcoded data,
2392 as a string, the back-end, as a symbol, and the communication
2393 channel, as a plist. It must return a string or nil.")
2395 (defvar org-export-filter-planning-functions nil
2396 "List of functions applied to a transcoded planning.
2397 Each filter is called with three arguments: the transcoded data,
2398 as a string, the back-end, as a symbol, and the communication
2399 channel, as a plist. It must return a string or nil.")
2401 (defvar org-export-filter-property-drawer-functions nil
2402 "List of functions applied to a transcoded property-drawer.
2403 Each filter is called with three arguments: the transcoded data,
2404 as a string, the back-end, as a symbol, and the communication
2405 channel, as a plist. It must return a string or nil.")
2407 (defvar org-export-filter-quote-block-functions nil
2408 "List of functions applied to a transcoded quote block.
2409 Each filter is called with three arguments: the transcoded quote
2410 data, as a string, the back-end, as a symbol, and the
2411 communication channel, as a plist. It must return a string or
2414 (defvar org-export-filter-quote-section-functions nil
2415 "List of functions applied to a transcoded quote-section.
2416 Each filter is called with three arguments: the transcoded data,
2417 as a string, the back-end, as a symbol, and the communication
2418 channel, as a plist. It must return a string or nil.")
2420 (defvar org-export-filter-section-functions nil
2421 "List of functions applied to a transcoded section.
2422 Each filter is called with three arguments: the transcoded data,
2423 as a string, the back-end, as a symbol, and the communication
2424 channel, as a plist. It must return a string or nil.")
2426 (defvar org-export-filter-special-block-functions nil
2427 "List of functions applied to a transcoded special block.
2428 Each filter is called with three arguments: the transcoded data,
2429 as a string, the back-end, as a symbol, and the communication
2430 channel, as a plist. It must return a string or nil.")
2432 (defvar org-export-filter-src-block-functions nil
2433 "List of functions applied to a transcoded src-block.
2434 Each filter is called with three arguments: the transcoded data,
2435 as a string, the back-end, as a symbol, and the communication
2436 channel, as a plist. It must return a string or nil.")
2438 (defvar org-export-filter-table-functions nil
2439 "List of functions applied to a transcoded table.
2440 Each filter is called with three arguments: the transcoded data,
2441 as a string, the back-end, as a symbol, and the communication
2442 channel, as a plist. It must return a string or nil.")
2444 (defvar org-export-filter-table-cell-functions nil
2445 "List of functions applied to a transcoded table-cell.
2446 Each filter is called with three arguments: the transcoded data,
2447 as a string, the back-end, as a symbol, and the communication
2448 channel, as a plist. It must return a string or nil.")
2450 (defvar org-export-filter-table-row-functions nil
2451 "List of functions applied to a transcoded table-row.
2452 Each filter is called with three arguments: the transcoded data,
2453 as a string, the back-end, as a symbol, and the communication
2454 channel, as a plist. It must return a string or nil.")
2456 (defvar org-export-filter-verse-block-functions nil
2457 "List of functions applied to a transcoded verse block.
2458 Each filter is called with three arguments: the transcoded data,
2459 as a string, the back-end, as a symbol, and the communication
2460 channel, as a plist. It must return a string or nil.")
2463 ;;;; Objects Filters
2465 (defvar org-export-filter-bold-functions nil
2466 "List of functions applied to transcoded bold text.
2467 Each filter is called with three arguments: the transcoded data,
2468 as a string, the back-end, as a symbol, and the communication
2469 channel, as a plist. It must return a string or nil.")
2471 (defvar org-export-filter-code-functions nil
2472 "List of functions applied to transcoded code text.
2473 Each filter is called with three arguments: the transcoded data,
2474 as a string, the back-end, as a symbol, and the communication
2475 channel, as a plist. It must return a string or nil.")
2477 (defvar org-export-filter-entity-functions nil
2478 "List of functions applied to a transcoded entity.
2479 Each filter is called with three arguments: the transcoded data,
2480 as a string, the back-end, as a symbol, and the communication
2481 channel, as a plist. It must return a string or nil.")
2483 (defvar org-export-filter-export-snippet-functions nil
2484 "List of functions applied to a transcoded export-snippet.
2485 Each filter is called with three arguments: the transcoded data,
2486 as a string, the back-end, as a symbol, and the communication
2487 channel, as a plist. It must return a string or nil.")
2489 (defvar org-export-filter-footnote-reference-functions nil
2490 "List of functions applied to a transcoded footnote-reference.
2491 Each filter is called with three arguments: the transcoded data,
2492 as a string, the back-end, as a symbol, and the communication
2493 channel, as a plist. It must return a string or nil.")
2495 (defvar org-export-filter-inline-babel-call-functions nil
2496 "List of functions applied to a transcoded inline-babel-call.
2497 Each filter is called with three arguments: the transcoded data,
2498 as a string, the back-end, as a symbol, and the communication
2499 channel, as a plist. It must return a string or nil.")
2501 (defvar org-export-filter-inline-src-block-functions nil
2502 "List of functions applied to a transcoded inline-src-block.
2503 Each filter is called with three arguments: the transcoded data,
2504 as a string, the back-end, as a symbol, and the communication
2505 channel, as a plist. It must return a string or nil.")
2507 (defvar org-export-filter-italic-functions nil
2508 "List of functions applied to transcoded italic text.
2509 Each filter is called with three arguments: the transcoded data,
2510 as a string, the back-end, as a symbol, and the communication
2511 channel, as a plist. It must return a string or nil.")
2513 (defvar org-export-filter-latex-fragment-functions nil
2514 "List of functions applied to a transcoded latex-fragment.
2515 Each filter is called with three arguments: the transcoded data,
2516 as a string, the back-end, as a symbol, and the communication
2517 channel, as a plist. It must return a string or nil.")
2519 (defvar org-export-filter-line-break-functions nil
2520 "List of functions applied to a transcoded line-break.
2521 Each filter is called with three arguments: the transcoded data,
2522 as a string, the back-end, as a symbol, and the communication
2523 channel, as a plist. It must return a string or nil.")
2525 (defvar org-export-filter-link-functions nil
2526 "List of functions applied to a transcoded link.
2527 Each filter is called with three arguments: the transcoded data,
2528 as a string, the back-end, as a symbol, and the communication
2529 channel, as a plist. It must return a string or nil.")
2531 (defvar org-export-filter-macro-functions nil
2532 "List of functions applied to a transcoded macro.
2533 Each filter is called with three arguments: the transcoded data,
2534 as a string, the back-end, as a symbol, and the communication
2535 channel, as a plist. It must return a string or nil.")
2537 (defvar org-export-filter-radio-target-functions nil
2538 "List of functions applied to a transcoded radio-target.
2539 Each filter is called with three arguments: the transcoded data,
2540 as a string, the back-end, as a symbol, and the communication
2541 channel, as a plist. It must return a string or nil.")
2543 (defvar org-export-filter-statistics-cookie-functions nil
2544 "List of functions applied to a transcoded statistics-cookie.
2545 Each filter is called with three arguments: the transcoded data,
2546 as a string, the back-end, as a symbol, and the communication
2547 channel, as a plist. It must return a string or nil.")
2549 (defvar org-export-filter-strike-through-functions nil
2550 "List of functions applied to transcoded strike-through text.
2551 Each filter is called with three arguments: the transcoded data,
2552 as a string, the back-end, as a symbol, and the communication
2553 channel, as a plist. It must return a string or nil.")
2555 (defvar org-export-filter-subscript-functions nil
2556 "List of functions applied to a transcoded subscript.
2557 Each filter is called with three arguments: the transcoded data,
2558 as a string, the back-end, as a symbol, and the communication
2559 channel, as a plist. It must return a string or nil.")
2561 (defvar org-export-filter-superscript-functions nil
2562 "List of functions applied to a transcoded superscript.
2563 Each filter is called with three arguments: the transcoded data,
2564 as a string, the back-end, as a symbol, and the communication
2565 channel, as a plist. It must return a string or nil.")
2567 (defvar org-export-filter-target-functions nil
2568 "List of functions applied to a transcoded target.
2569 Each filter is called with three arguments: the transcoded data,
2570 as a string, the back-end, as a symbol, and the communication
2571 channel, as a plist. It must return a string or nil.")
2573 (defvar org-export-filter-timestamp-functions nil
2574 "List of functions applied to a transcoded timestamp.
2575 Each filter is called with three arguments: the transcoded data,
2576 as a string, the back-end, as a symbol, and the communication
2577 channel, as a plist. It must return a string or nil.")
2579 (defvar org-export-filter-underline-functions nil
2580 "List of functions applied to transcoded underline text.
2581 Each filter is called with three arguments: the transcoded data,
2582 as a string, the back-end, as a symbol, and the communication
2583 channel, as a plist. It must return a string or nil.")
2585 (defvar org-export-filter-verbatim-functions nil
2586 "List of functions applied to transcoded verbatim text.
2587 Each filter is called with three arguments: the transcoded data,
2588 as a string, the back-end, as a symbol, and the communication
2589 channel, as a plist. It must return a string or nil.")
2594 ;; Internal function `org-export-install-filters' installs filters
2595 ;; hard-coded in back-ends (developer filters) and filters from global
2596 ;; variables (user filters) in the communication channel.
2598 ;; Internal function `org-export-filter-apply-functions' takes care
2599 ;; about applying each filter in order to a given data. It ignores
2600 ;; filters returning a nil value but stops whenever a filter returns
2603 (defun org-export-filter-apply-functions (filters value info
)
2604 "Call every function in FILTERS.
2606 Functions are called with arguments VALUE, current export
2607 back-end and INFO. A function returning a nil value will be
2608 skipped. If it returns the empty string, the process ends and
2611 Call is done in a LIFO fashion, to be sure that developer
2612 specified filters, if any, are called first."
2614 (dolist (filter filters value
)
2615 (let ((result (funcall filter value
(plist-get info
:back-end
) info
)))
2616 (cond ((not result
) value
)
2617 ((equal value
"") (throw 'exit nil
))
2618 (t (setq value result
)))))))
2620 (defun org-export-install-filters (info)
2621 "Install filters properties in communication channel.
2622 INFO is a plist containing the current communication channel.
2623 Return the updated communication channel."
2625 ;; Install user defined filters with `org-export-filters-alist'.
2627 (setq plist
(plist-put plist
(car p
) (eval (cdr p
)))))
2628 org-export-filters-alist
)
2629 ;; Prepend back-end specific filters to that list.
2631 ;; Single values get consed, lists are prepended.
2632 (let ((key (car p
)) (value (cdr p
)))
2637 (if (atom value
) (cons value
(plist-get plist key
))
2638 (append value
(plist-get plist key
))))))))
2639 (org-export-backend-filters (plist-get info
:back-end
)))
2640 ;; Return new communication channel.
2641 (org-combine-plists info plist
)))
2647 ;; This is the room for the main function, `org-export-as', along with
2648 ;; its derivatives, `org-export-to-buffer', `org-export-to-file' and
2649 ;; `org-export-string-as'. They differ either by the way they output
2650 ;; the resulting code (for the first two) or by the input type (for
2653 ;; `org-export-output-file-name' is an auxiliary function meant to be
2654 ;; used with `org-export-to-file'. With a given extension, it tries
2655 ;; to provide a canonical file name to write export output to.
2657 ;; Note that `org-export-as' doesn't really parse the current buffer,
2658 ;; but a copy of it (with the same buffer-local variables and
2659 ;; visibility), where macros and include keywords are expanded and
2660 ;; Babel blocks are executed, if appropriate.
2661 ;; `org-export-with-buffer-copy' macro prepares that copy.
2663 ;; File inclusion is taken care of by
2664 ;; `org-export-expand-include-keyword' and
2665 ;; `org-export--prepare-file-contents'. Structure wise, including
2666 ;; a whole Org file in a buffer often makes little sense. For
2667 ;; example, if the file contains an headline and the include keyword
2668 ;; was within an item, the item should contain the headline. That's
2669 ;; why file inclusion should be done before any structure can be
2670 ;; associated to the file, that is before parsing.
2672 (defun org-export-copy-buffer ()
2673 "Return a copy of the current buffer.
2674 The copy preserves Org buffer-local variables, visibility and
2676 (let ((copy-buffer-fun (org-export--generate-copy-script (current-buffer)))
2677 (new-buf (generate-new-buffer (buffer-name))))
2678 (with-current-buffer new-buf
2679 (funcall copy-buffer-fun
)
2680 (set-buffer-modified-p nil
))
2683 (defmacro org-export-with-buffer-copy
(&rest body
)
2684 "Apply BODY in a copy of the current buffer.
2685 The copy preserves local variables, visibility and contents of
2686 the original buffer. Point is at the beginning of the buffer
2687 when BODY is applied."
2689 (org-with-gensyms (buf-copy)
2690 `(let ((,buf-copy
(org-export-copy-buffer)))
2692 (with-current-buffer ,buf-copy
2693 (goto-char (point-min))
2695 (and (buffer-live-p ,buf-copy
)
2696 ;; Kill copy without confirmation.
2697 (progn (with-current-buffer ,buf-copy
2698 (restore-buffer-modified-p nil
))
2699 (kill-buffer ,buf-copy
)))))))
2701 (defun org-export--generate-copy-script (buffer)
2702 "Generate a function duplicating BUFFER.
2704 The copy will preserve local variables, visibility, contents and
2705 narrowing of the original buffer. If a region was active in
2706 BUFFER, contents will be narrowed to that region instead.
2708 The resulting function can be eval'ed at a later time, from
2709 another buffer, effectively cloning the original buffer there."
2710 (with-current-buffer buffer
2712 (let ((inhibit-modification-hooks t
))
2713 ;; Buffer local variables.
2718 (let ((var (car entry
))
2720 (and (not (eq var
'org-font-lock-keywords
))
2722 '(major-mode default-directory
2723 buffer-file-name outline-level
2725 buffer-invisibility-spec
))
2726 (string-match "^\\(org-\\|orgtbl-\\)"
2728 ;; Skip unreadable values, as they cannot be
2729 ;; sent to external process.
2730 (or (not val
) (ignore-errors (read (format "%S" val
))))
2731 (push `(set (make-local-variable (quote ,var
))
2734 (buffer-local-variables (buffer-base-buffer)))
2736 ;; Whole buffer contents.
2738 ,(org-with-wide-buffer
2739 (buffer-substring-no-properties
2740 (point-min) (point-max))))
2742 ,(if (org-region-active-p)
2743 `(narrow-to-region ,(region-beginning) ,(region-end))
2744 `(narrow-to-region ,(point-min) ,(point-max)))
2745 ;; Current position of point.
2746 (goto-char ,(point))
2747 ;; Overlays with invisible property.
2751 (let ((invis-prop (overlay-get ov
'invisible
)))
2754 (make-overlay ,(overlay-start ov
)
2756 'invisible
(quote ,invis-prop
))
2758 (overlays-in (point-min) (point-max)))
2762 (defun org-export-as
2763 (backend &optional subtreep visible-only body-only ext-plist
)
2764 "Transcode current Org buffer into BACKEND code.
2766 If narrowing is active in the current buffer, only transcode its
2769 If a region is active, transcode that region.
2771 When optional argument SUBTREEP is non-nil, transcode the
2772 sub-tree at point, extracting information from the headline
2775 When optional argument VISIBLE-ONLY is non-nil, don't export
2776 contents of hidden elements.
2778 When optional argument BODY-ONLY is non-nil, only return body
2779 code, without surrounding template.
2781 Optional argument EXT-PLIST, when provided, is a property list
2782 with external parameters overriding Org default settings, but
2783 still inferior to file-local settings.
2785 Return code as a string."
2786 (org-export-barf-if-invalid-backend backend
)
2789 ;; Narrow buffer to an appropriate region or subtree for
2790 ;; parsing. If parsing subtree, be sure to remove main headline
2792 (cond ((org-region-active-p)
2793 (narrow-to-region (region-beginning) (region-end)))
2795 (org-narrow-to-subtree)
2796 (goto-char (point-min))
2798 (narrow-to-region (point) (point-max))))
2799 ;; Initialize communication channel with original buffer
2800 ;; attributes, unavailable in its copy.
2801 (let ((info (org-export--get-buffer-attributes)) tree
)
2802 ;; Update communication channel and get parse tree. Buffer
2803 ;; isn't parsed directly. Instead, a temporary copy is
2804 ;; created, where include keywords, macros are expanded and
2805 ;; code blocks are evaluated.
2806 (org-export-with-buffer-copy
2807 ;; Run first hook with current back-end as argument.
2808 (run-hook-with-args 'org-export-before-processing-hook backend
)
2809 (org-export-expand-include-keyword)
2810 ;; Update macro templates since #+INCLUDE keywords might have
2811 ;; added some new ones.
2812 (org-macro-initialize-templates)
2813 (org-macro-replace-all org-macro-templates
)
2814 (when org-export-babel-evaluate
(org-export-execute-babel-code))
2815 ;; Update radio targets since keyword inclusion might have
2817 (org-update-radio-target-regexp)
2818 ;; Run last hook with current back-end as argument.
2819 (goto-char (point-min))
2820 (run-hook-with-args 'org-export-before-parsing-hook backend
)
2821 ;; Update communication channel with environment. Also
2822 ;; install user's and developer's filters.
2824 (org-export-install-filters
2826 info
(org-export-get-environment backend subtreep ext-plist
))))
2827 ;; Expand export-specific set of macros: {{{author}}},
2828 ;; {{{date}}}, {{{email}}} and {{{title}}}. It must be done
2829 ;; once regular macros have been expanded, since document
2830 ;; keywords may contain one of them.
2831 (org-macro-replace-all
2832 (list (cons "author"
2833 (org-element-interpret-data (plist-get info
:author
)))
2835 (org-element-interpret-data (plist-get info
:date
)))
2836 ;; EMAIL is not a parsed keyword: store it as-is.
2837 (cons "email" (or (plist-get info
:email
) ""))
2839 (org-element-interpret-data (plist-get info
:title
)))))
2840 ;; Call options filters and update export options. We do not
2841 ;; use `org-export-filter-apply-functions' here since the
2842 ;; arity of such filters is different.
2843 (dolist (filter (plist-get info
:filter-options
))
2844 (let ((result (funcall filter info backend
)))
2845 (when result
(setq info result
))))
2846 ;; Parse buffer and call parse-tree filter on it.
2848 (org-export-filter-apply-functions
2849 (plist-get info
:filter-parse-tree
)
2850 (org-element-parse-buffer nil visible-only
) info
))
2851 ;; Now tree is complete, compute its properties and add them
2852 ;; to communication channel.
2855 info
(org-export-collect-tree-properties tree info
)))
2856 ;; Eventually transcode TREE. Wrap the resulting string into
2858 (let* ((body (org-element-normalize-string
2859 (or (org-export-data tree info
) "")))
2860 (inner-template (cdr (assq 'inner-template
2861 (plist-get info
:translate-alist
))))
2862 (full-body (if (not (functionp inner-template
)) body
2863 (funcall inner-template body info
)))
2864 (template (cdr (assq 'template
2865 (plist-get info
:translate-alist
)))))
2866 ;; Remove all text properties since they cannot be
2867 ;; retrieved from an external process. Finally call
2868 ;; final-output filter and return result.
2870 (org-export-filter-apply-functions
2871 (plist-get info
:filter-final-output
)
2872 (if (or (not (functionp template
)) body-only
) full-body
2873 (funcall template full-body info
))
2877 (defun org-export-to-buffer
2878 (backend buffer
&optional subtreep visible-only body-only ext-plist
)
2879 "Call `org-export-as' with output to a specified buffer.
2881 BACKEND is the back-end used for transcoding, as a symbol.
2883 BUFFER is the output buffer. If it already exists, it will be
2884 erased first, otherwise, it will be created.
2886 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and
2887 EXT-PLIST are similar to those used in `org-export-as', which
2890 If `org-export-copy-to-kill-ring' is non-nil, add buffer contents
2891 to kill ring. Return buffer."
2892 (let ((out (org-export-as backend subtreep visible-only body-only ext-plist
))
2893 (buffer (get-buffer-create buffer
)))
2894 (with-current-buffer buffer
2897 (goto-char (point-min)))
2898 ;; Maybe add buffer contents to kill ring.
2899 (when (and org-export-copy-to-kill-ring
(org-string-nw-p out
))
2905 (defun org-export-to-file
2906 (backend file
&optional subtreep visible-only body-only ext-plist
)
2907 "Call `org-export-as' with output to a specified file.
2909 BACKEND is the back-end used for transcoding, as a symbol. FILE
2910 is the name of the output file, as a string.
2912 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and
2913 EXT-PLIST are similar to those used in `org-export-as', which
2916 If `org-export-copy-to-kill-ring' is non-nil, add file contents
2917 to kill ring. Return output file's name."
2918 ;; Checks for FILE permissions. `write-file' would do the same, but
2919 ;; we'd rather avoid needless transcoding of parse tree.
2920 (unless (file-writable-p file
) (error "Output file not writable"))
2921 ;; Insert contents to a temporary buffer and write it to FILE.
2922 (let ((out (org-export-as backend subtreep visible-only body-only ext-plist
)))
2925 (let ((coding-system-for-write org-export-coding-system
))
2927 ;; Maybe add file contents to kill ring.
2928 (when (and org-export-copy-to-kill-ring
(org-string-nw-p out
))
2929 (org-kill-new out
)))
2930 ;; Return full path.
2934 (defun org-export-string-as (string backend
&optional body-only ext-plist
)
2935 "Transcode STRING into BACKEND code.
2937 When optional argument BODY-ONLY is non-nil, only return body
2938 code, without preamble nor postamble.
2940 Optional argument EXT-PLIST, when provided, is a property list
2941 with external parameters overriding Org default settings, but
2942 still inferior to file-local settings.
2944 Return code as a string."
2948 (org-export-as backend nil nil body-only ext-plist
)))
2950 (defun org-export-output-file-name (extension &optional subtreep pub-dir
)
2951 "Return output file's name according to buffer specifications.
2953 EXTENSION is a string representing the output file extension,
2954 with the leading dot.
2956 With a non-nil optional argument SUBTREEP, try to determine
2957 output file's name by looking for \"EXPORT_FILE_NAME\" property
2958 of subtree at point.
2960 When optional argument PUB-DIR is set, use it as the publishing
2963 When optional argument VISIBLE-ONLY is non-nil, don't export
2964 contents of hidden elements.
2966 Return file name as a string."
2967 (let* ((visited-file (buffer-file-name (buffer-base-buffer)))
2969 ;; File name may come from EXPORT_FILE_NAME subtree
2970 ;; property, assuming point is at beginning of said
2972 (file-name-sans-extension
2976 (ignore-errors (org-back-to-heading) (point)))
2977 "EXPORT_FILE_NAME" t
))
2978 ;; File name may be extracted from buffer's associated
2980 (and visited-file
(file-name-nondirectory visited-file
))
2981 ;; Can't determine file name on our own: Ask user.
2982 (let ((read-file-name-function
2983 (and org-completion-use-ido
'ido-read-file-name
)))
2985 "Output file: " pub-dir nil nil nil
2987 (string= (file-name-extension name t
) extension
)))))))
2989 ;; Build file name. Enforce EXTENSION over whatever user
2990 ;; may have come up with. PUB-DIR, if defined, always has
2991 ;; precedence over any provided path.
2994 (concat (file-name-as-directory pub-dir
)
2995 (file-name-nondirectory base-name
)
2997 ((file-name-absolute-p base-name
) (concat base-name extension
))
2998 (t (concat (file-name-as-directory ".") base-name extension
)))))
2999 ;; If writing to OUTPUT-FILE would overwrite original file, append
3000 ;; EXTENSION another time to final name.
3001 (if (and visited-file
(file-equal-p visited-file output-file
))
3002 (concat output-file extension
)
3005 (defun org-export-expand-include-keyword (&optional included dir
)
3006 "Expand every include keyword in buffer.
3007 Optional argument INCLUDED is a list of included file names along
3008 with their line restriction, when appropriate. It is used to
3009 avoid infinite recursion. Optional argument DIR is the current
3010 working directory. It is used to properly resolve relative
3012 (let ((case-fold-search t
))
3013 (goto-char (point-min))
3014 (while (re-search-forward "^[ \t]*#\\+INCLUDE: +\\(.*\\)[ \t]*$" nil t
)
3015 (when (eq (org-element-type (save-match-data (org-element-at-point)))
3018 ;; Extract arguments from keyword's value.
3019 (let* ((value (match-string 1))
3020 (ind (org-get-indentation))
3021 (file (and (string-match "^\"\\(\\S-+\\)\"" value
)
3022 (prog1 (expand-file-name (match-string 1 value
) dir
)
3023 (setq value
(replace-match "" nil nil value
)))))
3026 ":lines +\"\\(\\(?:[0-9]+\\)?-\\(?:[0-9]+\\)?\\)\"" value
)
3027 (prog1 (match-string 1 value
)
3028 (setq value
(replace-match "" nil nil value
)))))
3029 (env (cond ((string-match "\\<example\\>" value
) 'example
)
3030 ((string-match "\\<src\\(?: +\\(.*\\)\\)?" value
)
3031 (match-string 1 value
))))
3032 ;; Minimal level of included file defaults to the child
3033 ;; level of the current headline, if any, or one. It
3034 ;; only applies is the file is meant to be included as
3038 (if (string-match ":minlevel +\\([0-9]+\\)" value
)
3039 (prog1 (string-to-number (match-string 1 value
))
3040 (setq value
(replace-match "" nil nil value
)))
3041 (let ((cur (org-current-level)))
3042 (if cur
(1+ (org-reduced-level cur
)) 1))))))
3044 (delete-region (point) (progn (forward-line) (point)))
3046 ((not file
) (error "Invalid syntax in INCLUDE keyword"))
3047 ((not (file-readable-p file
)) (error "Cannot include file %s" file
))
3048 ;; Check if files has already been parsed. Look after
3049 ;; inclusion lines too, as different parts of the same file
3050 ;; can be included too.
3051 ((member (list file lines
) included
)
3052 (error "Recursive file inclusion: %s" file
))
3057 (let ((ind-str (make-string ind ?
))
3059 (org-escape-code-in-string
3060 (org-export--prepare-file-contents file lines
))))
3061 (format "%s#+BEGIN_EXAMPLE\n%s%s#+END_EXAMPLE\n"
3062 ind-str contents ind-str
))))
3065 (let ((ind-str (make-string ind ?
))
3067 (org-escape-code-in-string
3068 (org-export--prepare-file-contents file lines
))))
3069 (format "%s#+BEGIN_SRC %s\n%s%s#+END_SRC\n"
3070 ind-str env contents ind-str
))))
3076 (org-export--prepare-file-contents file lines ind minlevel
))
3077 (org-export-expand-include-keyword
3078 (cons (list file lines
) included
)
3079 (file-name-directory file
))
3080 (buffer-string))))))))))))
3082 (defun org-export--prepare-file-contents (file &optional lines ind minlevel
)
3083 "Prepare the contents of FILE for inclusion and return them as a string.
3085 When optional argument LINES is a string specifying a range of
3086 lines, include only those lines.
3088 Optional argument IND, when non-nil, is an integer specifying the
3089 global indentation of returned contents. Since its purpose is to
3090 allow an included file to stay in the same environment it was
3091 created \(i.e. a list item), it doesn't apply past the first
3092 headline encountered.
3094 Optional argument MINLEVEL, when non-nil, is an integer
3095 specifying the level that any top-level headline in the included
3098 (insert-file-contents file
)
3100 (let* ((lines (split-string lines
"-"))
3101 (lbeg (string-to-number (car lines
)))
3102 (lend (string-to-number (cadr lines
)))
3103 (beg (if (zerop lbeg
) (point-min)
3104 (goto-char (point-min))
3105 (forward-line (1- lbeg
))
3107 (end (if (zerop lend
) (point-max)
3108 (goto-char (point-min))
3109 (forward-line (1- lend
))
3111 (narrow-to-region beg end
)))
3112 ;; Remove blank lines at beginning and end of contents. The logic
3113 ;; behind that removal is that blank lines around include keyword
3114 ;; override blank lines in included file.
3115 (goto-char (point-min))
3116 (org-skip-whitespace)
3118 (delete-region (point-min) (point))
3119 (goto-char (point-max))
3120 (skip-chars-backward " \r\t\n")
3122 (delete-region (point) (point-max))
3123 ;; If IND is set, preserve indentation of include keyword until
3124 ;; the first headline encountered.
3126 (unless (eq major-mode
'org-mode
) (org-mode))
3127 (goto-char (point-min))
3128 (let ((ind-str (make-string ind ?
)))
3129 (while (not (or (eobp) (looking-at org-outline-regexp-bol
)))
3130 ;; Do not move footnote definitions out of column 0.
3131 (unless (and (looking-at org-footnote-definition-re
)
3132 (eq (org-element-type (org-element-at-point))
3133 'footnote-definition
))
3136 ;; When MINLEVEL is specified, compute minimal level for headlines
3137 ;; in the file (CUR-MIN), and remove stars to each headline so
3138 ;; that headlines with minimal level have a level of MINLEVEL.
3140 (unless (eq major-mode
'org-mode
) (org-mode))
3141 (org-with-limited-levels
3142 (let ((levels (org-map-entries
3143 (lambda () (org-reduced-level (org-current-level))))))
3145 (let ((offset (- minlevel
(apply 'min levels
))))
3146 (unless (zerop offset
)
3147 (when org-odd-levels-only
(setq offset
(* offset
2)))
3148 ;; Only change stars, don't bother moving whole
3151 (lambda () (if (< offset
0) (delete-char (abs offset
))
3152 (insert (make-string offset ?
*)))))))))))
3153 (org-element-normalize-string (buffer-string))))
3155 (defun org-export-execute-babel-code ()
3156 "Execute every Babel code in the visible part of current buffer."
3157 ;; Get a pristine copy of current buffer so Babel references can be
3158 ;; properly resolved.
3159 (let ((reference (org-export-copy-buffer)))
3160 (unwind-protect (let ((org-current-export-file reference
))
3161 (org-babel-exp-process-buffer))
3162 (kill-buffer reference
))))
3166 ;;; Tools For Back-Ends
3168 ;; A whole set of tools is available to help build new exporters. Any
3169 ;; function general enough to have its use across many back-ends
3170 ;; should be added here.
3172 ;;;; For Affiliated Keywords
3174 ;; `org-export-read-attribute' reads a property from a given element
3175 ;; as a plist. It can be used to normalize affiliated keywords'
3178 ;; Since captions can span over multiple lines and accept dual values,
3179 ;; their internal representation is a bit tricky. Therefore,
3180 ;; `org-export-get-caption' transparently returns a given element's
3181 ;; caption as a secondary string.
3183 (defun org-export-read-attribute (attribute element
&optional property
)
3184 "Turn ATTRIBUTE property from ELEMENT into a plist.
3186 When optional argument PROPERTY is non-nil, return the value of
3187 that property within attributes.
3189 This function assumes attributes are defined as \":keyword
3190 value\" pairs. It is appropriate for `:attr_html' like
3193 (let ((value (org-element-property attribute element
)))
3195 (read (format "(%s)" (mapconcat 'identity value
" ")))))))
3196 (if property
(plist-get attributes property
) attributes
)))
3198 (defun org-export-get-caption (element &optional shortp
)
3199 "Return caption from ELEMENT as a secondary string.
3201 When optional argument SHORTP is non-nil, return short caption,
3202 as a secondary string, instead.
3204 Caption lines are separated by a white space."
3205 (let ((full-caption (org-element-property :caption element
)) caption
)
3206 (dolist (line full-caption
(cdr caption
))
3207 (let ((cap (funcall (if shortp
'cdr
'car
) line
)))
3209 (setq caption
(nconc (list " ") (copy-sequence cap
) caption
)))))))
3212 ;;;; For Derived Back-ends
3214 ;; `org-export-with-backend' is a function allowing to locally use
3215 ;; another back-end to transcode some object or element. In a derived
3216 ;; back-end, it may be used as a fall-back function once all specific
3217 ;; cases have been treated.
3219 (defun org-export-with-backend (back-end data
&optional contents info
)
3220 "Call a transcoder from BACK-END on DATA.
3221 CONTENTS, when non-nil, is the transcoded contents of DATA
3222 element, as a string. INFO, when non-nil, is the communication
3223 channel used for export, as a plist.."
3224 (org-export-barf-if-invalid-backend back-end
)
3225 (let ((type (org-element-type data
)))
3226 (if (memq type
'(nil org-data
)) (error "No foreign transcoder available")
3228 (cdr (assq type
(org-export-backend-translate-table back-end
)))))
3229 (if (functionp transcoder
) (funcall transcoder data contents info
)
3230 (error "No foreign transcoder available"))))))
3233 ;;;; For Export Snippets
3235 ;; Every export snippet is transmitted to the back-end. Though, the
3236 ;; latter will only retain one type of export-snippet, ignoring
3237 ;; others, based on the former's target back-end. The function
3238 ;; `org-export-snippet-backend' returns that back-end for a given
3241 (defun org-export-snippet-backend (export-snippet)
3242 "Return EXPORT-SNIPPET targeted back-end as a symbol.
3243 Translation, with `org-export-snippet-translation-alist', is
3245 (let ((back-end (org-element-property :back-end export-snippet
)))
3247 (or (cdr (assoc back-end org-export-snippet-translation-alist
))
3253 ;; `org-export-collect-footnote-definitions' is a tool to list
3254 ;; actually used footnotes definitions in the whole parse tree, or in
3255 ;; an headline, in order to add footnote listings throughout the
3258 ;; `org-export-footnote-first-reference-p' is a predicate used by some
3259 ;; back-ends, when they need to attach the footnote definition only to
3260 ;; the first occurrence of the corresponding label.
3262 ;; `org-export-get-footnote-definition' and
3263 ;; `org-export-get-footnote-number' provide easier access to
3264 ;; additional information relative to a footnote reference.
3266 (defun org-export-collect-footnote-definitions (data info
)
3267 "Return an alist between footnote numbers, labels and definitions.
3269 DATA is the parse tree from which definitions are collected.
3270 INFO is the plist used as a communication channel.
3272 Definitions are sorted by order of references. They either
3273 appear as Org data or as a secondary string for inlined
3274 footnotes. Unreferenced definitions are ignored."
3276 collect-fn
; for byte-compiler.
3280 ;; Collect footnote number, label and definition in DATA.
3281 (org-element-map data
'footnote-reference
3283 (when (org-export-footnote-first-reference-p fn info
)
3284 (let ((def (org-export-get-footnote-definition fn info
)))
3286 (list (org-export-get-footnote-number fn info
)
3287 (org-element-property :label fn
)
3290 ;; Also search in definition for nested footnotes.
3291 (when (eq (org-element-property :type fn
) 'standard
)
3292 (funcall collect-fn def
)))))
3293 ;; Don't enter footnote definitions since it will happen
3294 ;; when their first reference is found.
3295 info nil
'footnote-definition
)))))
3296 (funcall collect-fn
(plist-get info
:parse-tree
))
3297 (reverse num-alist
)))
3299 (defun org-export-footnote-first-reference-p (footnote-reference info
)
3300 "Non-nil when a footnote reference is the first one for its label.
3302 FOOTNOTE-REFERENCE is the footnote reference being considered.
3303 INFO is the plist used as a communication channel."
3304 (let ((label (org-element-property :label footnote-reference
)))
3305 ;; Anonymous footnotes are always a first reference.
3307 ;; Otherwise, return the first footnote with the same LABEL and
3308 ;; test if it is equal to FOOTNOTE-REFERENCE.
3309 (let* (search-refs ; for byte-compiler.
3313 (org-element-map data
'footnote-reference
3316 ((string= (org-element-property :label fn
) label
)
3318 ;; If FN isn't inlined, be sure to traverse its
3319 ;; definition before resuming search. See
3320 ;; comments in `org-export-get-footnote-number'
3321 ;; for more information.
3322 ((eq (org-element-property :type fn
) 'standard
)
3323 (funcall search-refs
3324 (org-export-get-footnote-definition fn info
)))))
3325 ;; Don't enter footnote definitions since it will
3326 ;; happen when their first reference is found.
3327 info
'first-match
'footnote-definition
)))))
3328 (eq (catch 'exit
(funcall search-refs
(plist-get info
:parse-tree
)))
3329 footnote-reference
)))))
3331 (defun org-export-get-footnote-definition (footnote-reference info
)
3332 "Return definition of FOOTNOTE-REFERENCE as parsed data.
3333 INFO is the plist used as a communication channel. If no such
3334 definition can be found, return the \"DEFINITION NOT FOUND\"
3336 (let ((label (org-element-property :label footnote-reference
)))
3337 (or (org-element-property :inline-definition footnote-reference
)
3338 (cdr (assoc label
(plist-get info
:footnote-definition-alist
)))
3339 "DEFINITION NOT FOUND.")))
3341 (defun org-export-get-footnote-number (footnote info
)
3342 "Return number associated to a footnote.
3344 FOOTNOTE is either a footnote reference or a footnote definition.
3345 INFO is the plist used as a communication channel."
3346 (let* ((label (org-element-property :label footnote
))
3348 search-ref
; For byte-compiler.
3352 ;; Search footnote references through DATA, filling
3353 ;; SEEN-REFS along the way.
3354 (org-element-map data
'footnote-reference
3356 (let ((fn-lbl (org-element-property :label fn
)))
3358 ;; Anonymous footnote match: return number.
3359 ((and (not fn-lbl
) (eq fn footnote
))
3360 (throw 'exit
(1+ (length seen-refs
))))
3361 ;; Labels match: return number.
3362 ((and label
(string= label fn-lbl
))
3363 (throw 'exit
(1+ (length seen-refs
))))
3364 ;; Anonymous footnote: it's always a new one.
3365 ;; Also, be sure to return nil from the `cond' so
3366 ;; `first-match' doesn't get us out of the loop.
3367 ((not fn-lbl
) (push 'inline seen-refs
) nil
)
3368 ;; Label not seen so far: add it so SEEN-REFS.
3370 ;; Also search for subsequent references in
3371 ;; footnote definition so numbering follows
3372 ;; reading logic. Note that we don't have to care
3373 ;; about inline definitions, since
3374 ;; `org-element-map' already traverses them at the
3377 ;; Once again, return nil to stay in the loop.
3378 ((not (member fn-lbl seen-refs
))
3379 (push fn-lbl seen-refs
)
3381 (org-export-get-footnote-definition fn info
))
3383 ;; Don't enter footnote definitions since it will
3384 ;; happen when their first reference is found.
3385 info
'first-match
'footnote-definition
)))))
3386 (catch 'exit
(funcall search-ref
(plist-get info
:parse-tree
)))))
3391 ;; `org-export-get-relative-level' is a shortcut to get headline
3392 ;; level, relatively to the lower headline level in the parsed tree.
3394 ;; `org-export-get-headline-number' returns the section number of an
3395 ;; headline, while `org-export-number-to-roman' allows to convert it
3396 ;; to roman numbers.
3398 ;; `org-export-low-level-p', `org-export-first-sibling-p' and
3399 ;; `org-export-last-sibling-p' are three useful predicates when it
3400 ;; comes to fulfill the `:headline-levels' property.
3402 ;; `org-export-get-tags', `org-export-get-category' and
3403 ;; `org-export-get-node-property' extract useful information from an
3404 ;; headline or a parent headline. They all handle inheritance.
3406 (defun org-export-get-relative-level (headline info
)
3407 "Return HEADLINE relative level within current parsed tree.
3408 INFO is a plist holding contextual information."
3409 (+ (org-element-property :level headline
)
3410 (or (plist-get info
:headline-offset
) 0)))
3412 (defun org-export-low-level-p (headline info
)
3413 "Non-nil when HEADLINE is considered as low level.
3415 INFO is a plist used as a communication channel.
3417 A low level headlines has a relative level greater than
3418 `:headline-levels' property value.
3420 Return value is the difference between HEADLINE relative level
3421 and the last level being considered as high enough, or nil."
3422 (let ((limit (plist-get info
:headline-levels
)))
3423 (when (wholenump limit
)
3424 (let ((level (org-export-get-relative-level headline info
)))
3425 (and (> level limit
) (- level limit
))))))
3427 (defun org-export-get-headline-number (headline info
)
3428 "Return HEADLINE numbering as a list of numbers.
3429 INFO is a plist holding contextual information."
3430 (cdr (assoc headline
(plist-get info
:headline-numbering
))))
3432 (defun org-export-numbered-headline-p (headline info
)
3433 "Return a non-nil value if HEADLINE element should be numbered.
3434 INFO is a plist used as a communication channel."
3435 (let ((sec-num (plist-get info
:section-numbers
))
3436 (level (org-export-get-relative-level headline info
)))
3437 (if (wholenump sec-num
) (<= level sec-num
) sec-num
)))
3439 (defun org-export-number-to-roman (n)
3440 "Convert integer N into a roman numeral."
3441 (let ((roman '((1000 .
"M") (900 .
"CM") (500 .
"D") (400 .
"CD")
3442 ( 100 .
"C") ( 90 .
"XC") ( 50 .
"L") ( 40 .
"XL")
3443 ( 10 .
"X") ( 9 .
"IX") ( 5 .
"V") ( 4 .
"IV")
3447 (number-to-string n
)
3449 (if (>= n
(caar roman
))
3450 (setq n
(- n
(caar roman
))
3451 res
(concat res
(cdar roman
)))
3455 (defun org-export-get-tags (element info
&optional tags inherited
)
3456 "Return list of tags associated to ELEMENT.
3458 ELEMENT has either an `headline' or an `inlinetask' type. INFO
3459 is a plist used as a communication channel.
3461 Select tags (see `org-export-select-tags') and exclude tags (see
3462 `org-export-exclude-tags') are removed from the list.
3464 When non-nil, optional argument TAGS should be a list of strings.
3465 Any tag belonging to this list will also be removed.
3467 When optional argument INHERITED is non-nil, tags can also be
3468 inherited from parent headlines and FILETAGS keywords."
3470 (lambda (tag) (or (member tag
(plist-get info
:select-tags
))
3471 (member tag
(plist-get info
:exclude-tags
))
3473 (if (not inherited
) (org-element-property :tags element
)
3474 ;; Build complete list of inherited tags.
3475 (let ((current-tag-list (org-element-property :tags element
)))
3480 (when (and (memq (org-element-type parent
) '(headline inlinetask
))
3481 (not (member tag current-tag-list
)))
3482 (push tag current-tag-list
)))
3483 (org-element-property :tags parent
)))
3484 (org-export-get-genealogy element
))
3485 ;; Add FILETAGS keywords and return results.
3486 (org-uniquify (append (plist-get info
:filetags
) current-tag-list
))))))
3488 (defun org-export-get-node-property (property blob
&optional inherited
)
3489 "Return node PROPERTY value for BLOB.
3491 PROPERTY is normalized symbol (i.e. `:cookie-data'). BLOB is an
3494 If optional argument INHERITED is non-nil, the value can be
3495 inherited from a parent headline.
3497 Return value is a string or nil."
3498 (let ((headline (if (eq (org-element-type blob
) 'headline
) blob
3499 (org-export-get-parent-headline blob
))))
3500 (if (not inherited
) (org-element-property property blob
)
3501 (let ((parent headline
) value
)
3504 (when (plist-member (nth 1 parent
) property
)
3505 (throw 'found
(org-element-property property parent
)))
3506 (setq parent
(org-element-property :parent parent
))))))))
3508 (defun org-export-get-category (blob info
)
3509 "Return category for element or object BLOB.
3511 INFO is a plist used as a communication channel.
3513 CATEGORY is automatically inherited from a parent headline, from
3514 #+CATEGORY: keyword or created out of original file name. If all
3515 fail, the fall-back value is \"???\"."
3516 (or (let ((headline (if (eq (org-element-type blob
) 'headline
) blob
3517 (org-export-get-parent-headline blob
))))
3518 ;; Almost like `org-export-node-property', but we cannot trust
3519 ;; `plist-member' as every headline has a `:category'
3520 ;; property, would it be nil or equal to "???" (which has the
3522 (let ((parent headline
) value
)
3525 (let ((category (org-element-property :category parent
)))
3526 (and category
(not (equal "???" category
))
3527 (throw 'found category
)))
3528 (setq parent
(org-element-property :parent parent
))))))
3529 (org-element-map (plist-get info
:parse-tree
) 'keyword
3531 (when (equal (org-element-property :key kwd
) "CATEGORY")
3532 (org-element-property :value kwd
)))
3534 (let ((file (plist-get info
:input-file
)))
3535 (and file
(file-name-sans-extension (file-name-nondirectory file
))))
3538 (defun org-export-first-sibling-p (headline info
)
3539 "Non-nil when HEADLINE is the first sibling in its sub-tree.
3540 INFO is a plist used as a communication channel."
3541 (not (eq (org-element-type (org-export-get-previous-element headline info
))
3544 (defun org-export-last-sibling-p (headline info
)
3545 "Non-nil when HEADLINE is the last sibling in its sub-tree.
3546 INFO is a plist used as a communication channel."
3547 (not (org-export-get-next-element headline info
)))
3552 ;; `org-export-solidify-link-text' turns a string into a safer version
3553 ;; for links, replacing most non-standard characters with hyphens.
3555 ;; `org-export-get-coderef-format' returns an appropriate format
3556 ;; string for coderefs.
3558 ;; `org-export-inline-image-p' returns a non-nil value when the link
3559 ;; provided should be considered as an inline image.
3561 ;; `org-export-resolve-fuzzy-link' searches destination of fuzzy links
3562 ;; (i.e. links with "fuzzy" as type) within the parsed tree, and
3563 ;; returns an appropriate unique identifier when found, or nil.
3565 ;; `org-export-resolve-id-link' returns the first headline with
3566 ;; specified id or custom-id in parse tree, the path to the external
3567 ;; file with the id or nil when neither was found.
3569 ;; `org-export-resolve-coderef' associates a reference to a line
3570 ;; number in the element it belongs, or returns the reference itself
3571 ;; when the element isn't numbered.
3573 (defun org-export-solidify-link-text (s)
3574 "Take link text S and make a safe target out of it."
3576 (mapconcat 'identity
(org-split-string s
"[^a-zA-Z0-9_.-:]+") "-")))
3578 (defun org-export-get-coderef-format (path desc
)
3579 "Return format string for code reference link.
3580 PATH is the link path. DESC is its description."
3582 (cond ((not desc
) "%s")
3583 ((string-match (regexp-quote (concat "(" path
")")) desc
)
3584 (replace-match "%s" t t desc
))
3587 (defun org-export-inline-image-p (link &optional rules
)
3588 "Non-nil if LINK object points to an inline image.
3590 Optional argument is a set of RULES defining inline images. It
3591 is an alist where associations have the following shape:
3595 Applying a rule means apply REGEXP against LINK's path when its
3596 type is TYPE. The function will return a non-nil value if any of
3597 the provided rules is non-nil. The default rule is
3598 `org-export-default-inline-image-rule'.
3600 This only applies to links without a description."
3601 (and (not (org-element-contents link
))
3602 (let ((case-fold-search t
)
3603 (rules (or rules org-export-default-inline-image-rule
)))
3607 (and (string= (org-element-property :type link
) (car rule
))
3608 (string-match (cdr rule
)
3609 (org-element-property :path link
))
3612 ;; Return nil if no rule matched.
3615 (defun org-export-resolve-coderef (ref info
)
3616 "Resolve a code reference REF.
3618 INFO is a plist used as a communication channel.
3620 Return associated line number in source code, or REF itself,
3621 depending on src-block or example element's switches."
3622 (org-element-map (plist-get info
:parse-tree
) '(example-block src-block
)
3625 (insert (org-trim (org-element-property :value el
)))
3626 (let* ((label-fmt (regexp-quote
3627 (or (org-element-property :label-fmt el
)
3628 org-coderef-label-format
)))
3630 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
3631 (replace-regexp-in-string "%s" ref label-fmt nil t
))))
3632 ;; Element containing REF is found. Resolve it to either
3633 ;; a label or a line number, as needed.
3634 (when (re-search-backward ref-re nil t
)
3636 ((org-element-property :use-labels el
) ref
)
3637 ((eq (org-element-property :number-lines el
) 'continued
)
3638 (+ (org-export-get-loc el info
) (line-number-at-pos)))
3639 (t (line-number-at-pos)))))))
3642 (defun org-export-resolve-fuzzy-link (link info
)
3643 "Return LINK destination.
3645 INFO is a plist holding contextual information.
3647 Return value can be an object, an element, or nil:
3649 - If LINK path matches a target object (i.e. <<path>>) or
3650 element (i.e. \"#+TARGET: path\"), return it.
3652 - If LINK path exactly matches the name affiliated keyword
3653 \(i.e. #+NAME: path) of an element, return that element.
3655 - If LINK path exactly matches any headline name, return that
3656 element. If more than one headline share that name, priority
3657 will be given to the one with the closest common ancestor, if
3658 any, or the first one in the parse tree otherwise.
3660 - Otherwise, return nil.
3662 Assume LINK type is \"fuzzy\". White spaces are not
3664 (let* ((raw-path (org-element-property :path link
))
3665 (match-title-p (eq (aref raw-path
0) ?
*))
3666 ;; Split PATH at white spaces so matches are space
3668 (path (org-split-string
3669 (if match-title-p
(substring raw-path
1) raw-path
))))
3671 ;; First try to find a matching "<<path>>" unless user specified
3672 ;; he was looking for an headline (path starts with a *
3674 ((and (not match-title-p
)
3675 (org-element-map (plist-get info
:parse-tree
) '(keyword target
)
3677 (and (or (eq (org-element-type blob
) 'target
)
3678 (equal (org-element-property :key blob
) "TARGET"))
3679 (equal (org-split-string (org-element-property :value blob
))
3683 ;; Then try to find an element with a matching "#+NAME: path"
3684 ;; affiliated keyword.
3685 ((and (not match-title-p
)
3686 (org-element-map (plist-get info
:parse-tree
)
3687 org-element-all-elements
3689 (let ((name (org-element-property :name el
)))
3690 (when (and name
(equal (org-split-string name
) path
)) el
)))
3691 info
'first-match
)))
3692 ;; Last case: link either points to an headline or to
3693 ;; nothingness. Try to find the source, with priority given to
3694 ;; headlines with the closest common ancestor. If such candidate
3695 ;; is found, return it, otherwise return nil.
3697 (let ((find-headline
3699 ;; Return first headline whose `:raw-value' property is
3700 ;; NAME in parse tree DATA, or nil. Statistics cookies
3703 (org-element-map data
'headline
3705 (when (equal (org-split-string
3706 (replace-regexp-in-string
3707 "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
3708 (org-element-property :raw-value headline
)))
3711 info
'first-match
)))))
3712 ;; Search among headlines sharing an ancestor with link, from
3713 ;; closest to farthest.
3717 (when (eq (org-element-type parent
) 'headline
)
3718 (let ((foundp (funcall find-headline path parent
)))
3719 (when foundp
(throw 'exit foundp
)))))
3720 (org-export-get-genealogy link
)) nil
)
3721 ;; No match with a common ancestor: try full parse-tree.
3722 (funcall find-headline path
(plist-get info
:parse-tree
))))))))
3724 (defun org-export-resolve-id-link (link info
)
3725 "Return headline referenced as LINK destination.
3727 INFO is a plist used as a communication channel.
3729 Return value can be the headline element matched in current parse
3730 tree, a file name or nil. Assume LINK type is either \"id\" or
3732 (let ((id (org-element-property :path link
)))
3733 ;; First check if id is within the current parse tree.
3734 (or (org-element-map (plist-get info
:parse-tree
) 'headline
3736 (when (or (string= (org-element-property :id headline
) id
)
3737 (string= (org-element-property :custom-id headline
) id
))
3740 ;; Otherwise, look for external files.
3741 (cdr (assoc id
(plist-get info
:id-alist
))))))
3743 (defun org-export-resolve-radio-link (link info
)
3744 "Return radio-target object referenced as LINK destination.
3746 INFO is a plist used as a communication channel.
3748 Return value can be a radio-target object or nil. Assume LINK
3749 has type \"radio\"."
3750 (let ((path (org-element-property :path link
)))
3751 (org-element-map (plist-get info
:parse-tree
) 'radio-target
3753 (and (compare-strings
3754 (org-element-property :value radio
) 0 nil path
0 nil t
)
3756 info
'first-match
)))
3761 ;; `org-export-get-ordinal' associates a sequence number to any object
3764 (defun org-export-get-ordinal (element info
&optional types predicate
)
3765 "Return ordinal number of an element or object.
3767 ELEMENT is the element or object considered. INFO is the plist
3768 used as a communication channel.
3770 Optional argument TYPES, when non-nil, is a list of element or
3771 object types, as symbols, that should also be counted in.
3772 Otherwise, only provided element's type is considered.
3774 Optional argument PREDICATE is a function returning a non-nil
3775 value if the current element or object should be counted in. It
3776 accepts two arguments: the element or object being considered and
3777 the plist used as a communication channel. This allows to count
3778 only a certain type of objects (i.e. inline images).
3780 Return value is a list of numbers if ELEMENT is an headline or an
3781 item. It is nil for keywords. It represents the footnote number
3782 for footnote definitions and footnote references. If ELEMENT is
3783 a target, return the same value as if ELEMENT was the closest
3784 table, item or headline containing the target. In any other
3785 case, return the sequence number of ELEMENT among elements or
3786 objects of the same type."
3787 ;; A target keyword, representing an invisible target, never has
3788 ;; a sequence number.
3789 (unless (eq (org-element-type element
) 'keyword
)
3790 ;; Ordinal of a target object refer to the ordinal of the closest
3791 ;; table, item, or headline containing the object.
3792 (when (eq (org-element-type element
) 'target
)
3794 (loop for parent in
(org-export-get-genealogy element
)
3797 (org-element-type parent
)
3798 '(footnote-definition footnote-reference headline item
3801 (case (org-element-type element
)
3802 ;; Special case 1: An headline returns its number as a list.
3803 (headline (org-export-get-headline-number element info
))
3804 ;; Special case 2: An item returns its number as a list.
3805 (item (let ((struct (org-element-property :structure element
)))
3806 (org-list-get-item-number
3807 (org-element-property :begin element
)
3809 (org-list-prevs-alist struct
)
3810 (org-list-parents-alist struct
))))
3811 ((footnote-definition footnote-reference
)
3812 (org-export-get-footnote-number element info
))
3815 ;; Increment counter until ELEMENT is found again.
3816 (org-element-map (plist-get info
:parse-tree
)
3817 (or types
(org-element-type element
))
3820 ((eq element el
) (1+ counter
))
3821 ((not predicate
) (incf counter
) nil
)
3822 ((funcall predicate el info
) (incf counter
) nil
)))
3823 info
'first-match
))))))
3828 ;; `org-export-get-loc' counts number of code lines accumulated in
3829 ;; src-block or example-block elements with a "+n" switch until
3830 ;; a given element, excluded. Note: "-n" switches reset that count.
3832 ;; `org-export-unravel-code' extracts source code (along with a code
3833 ;; references alist) from an `element-block' or `src-block' type
3836 ;; `org-export-format-code' applies a formatting function to each line
3837 ;; of code, providing relative line number and code reference when
3838 ;; appropriate. Since it doesn't access the original element from
3839 ;; which the source code is coming, it expects from the code calling
3840 ;; it to know if lines should be numbered and if code references
3843 ;; Eventually, `org-export-format-code-default' is a higher-level
3844 ;; function (it makes use of the two previous functions) which handles
3845 ;; line numbering and code references inclusion, and returns source
3846 ;; code in a format suitable for plain text or verbatim output.
3848 (defun org-export-get-loc (element info
)
3849 "Return accumulated lines of code up to ELEMENT.
3851 INFO is the plist used as a communication channel.
3853 ELEMENT is excluded from count."
3855 (org-element-map (plist-get info
:parse-tree
)
3856 `(src-block example-block
,(org-element-type element
))
3859 ;; ELEMENT is reached: Quit the loop.
3861 ;; Only count lines from src-block and example-block elements
3862 ;; with a "+n" or "-n" switch. A "-n" switch resets counter.
3863 ((not (memq (org-element-type el
) '(src-block example-block
))) nil
)
3864 ((let ((linums (org-element-property :number-lines el
)))
3866 ;; Accumulate locs or reset them.
3867 (let ((lines (org-count-lines
3868 (org-trim (org-element-property :value el
)))))
3869 (setq loc
(if (eq linums
'new
) lines
(+ loc lines
))))))
3870 ;; Return nil to stay in the loop.
3876 (defun org-export-unravel-code (element)
3877 "Clean source code and extract references out of it.
3879 ELEMENT has either a `src-block' an `example-block' type.
3881 Return a cons cell whose CAR is the source code, cleaned from any
3882 reference and protective comma and CDR is an alist between
3883 relative line number (integer) and name of code reference on that
3885 (let* ((line 0) refs
3886 ;; Get code and clean it. Remove blank lines at its
3887 ;; beginning and end.
3888 (code (let ((c (replace-regexp-in-string
3889 "\\`\\([ \t]*\n\\)+" ""
3890 (replace-regexp-in-string
3891 "\\([ \t]*\n\\)*[ \t]*\\'" "\n"
3892 (org-element-property :value element
)))))
3893 ;; If appropriate, remove global indentation.
3894 (if (or org-src-preserve-indentation
3895 (org-element-property :preserve-indent element
))
3897 (org-remove-indentation c
))))
3898 ;; Get format used for references.
3899 (label-fmt (regexp-quote
3900 (or (org-element-property :label-fmt element
)
3901 org-coderef-label-format
)))
3902 ;; Build a regexp matching a loc with a reference.
3904 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)[ \t]*\\)$"
3905 (replace-regexp-in-string
3906 "%s" "\\([-a-zA-Z0-9_ ]+\\)" label-fmt nil t
))))
3909 ;; Code with references removed.
3910 (org-element-normalize-string
3914 (if (not (string-match with-ref-re loc
)) loc
3915 ;; Ref line: remove ref, and signal its position in REFS.
3916 (push (cons line
(match-string 3 loc
)) refs
)
3917 (replace-match "" nil nil loc
1)))
3918 (org-split-string code
"\n") "\n"))
3922 (defun org-export-format-code (code fun
&optional num-lines ref-alist
)
3923 "Format CODE by applying FUN line-wise and return it.
3925 CODE is a string representing the code to format. FUN is
3926 a function. It must accept three arguments: a line of
3927 code (string), the current line number (integer) or nil and the
3928 reference associated to the current line (string) or nil.
3930 Optional argument NUM-LINES can be an integer representing the
3931 number of code lines accumulated until the current code. Line
3932 numbers passed to FUN will take it into account. If it is nil,
3933 FUN's second argument will always be nil. This number can be
3934 obtained with `org-export-get-loc' function.
3936 Optional argument REF-ALIST can be an alist between relative line
3937 number (i.e. ignoring NUM-LINES) and the name of the code
3938 reference on it. If it is nil, FUN's third argument will always
3939 be nil. It can be obtained through the use of
3940 `org-export-unravel-code' function."
3941 (let ((--locs (org-split-string code
"\n"))
3943 (org-element-normalize-string
3947 (let ((--ref (cdr (assq --line ref-alist
))))
3948 (funcall fun --loc
(and num-lines
(+ num-lines --line
)) --ref
)))
3951 (defun org-export-format-code-default (element info
)
3952 "Return source code from ELEMENT, formatted in a standard way.
3954 ELEMENT is either a `src-block' or `example-block' element. INFO
3955 is a plist used as a communication channel.
3957 This function takes care of line numbering and code references
3958 inclusion. Line numbers, when applicable, appear at the
3959 beginning of the line, separated from the code by two white
3960 spaces. Code references, on the other hand, appear flushed to
3961 the right, separated by six white spaces from the widest line of
3963 ;; Extract code and references.
3964 (let* ((code-info (org-export-unravel-code element
))
3965 (code (car code-info
))
3966 (code-lines (org-split-string code
"\n"))
3967 (refs (and (org-element-property :retain-labels element
)
3969 ;; Handle line numbering.
3970 (num-start (case (org-element-property :number-lines element
)
3971 (continued (org-export-get-loc element info
))
3976 (length (number-to-string
3977 (+ (length code-lines
) num-start
))))))
3978 ;; Prepare references display, if required. Any reference
3979 ;; should start six columns after the widest line of code,
3980 ;; wrapped with parenthesis.
3982 (+ (apply 'max
(mapcar 'length code-lines
))
3983 (if (not num-start
) 0 (length (format num-fmt num-start
))))))
3984 (org-export-format-code
3986 (lambda (loc line-num ref
)
3987 (let ((number-str (and num-fmt
(format num-fmt line-num
))))
3992 (concat (make-string
3994 (+ (length loc
) (length number-str
))) ?
)
3995 (format "(%s)" ref
))))))
4001 ;; `org-export-table-has-special-column-p' and and
4002 ;; `org-export-table-row-is-special-p' are predicates used to look for
4003 ;; meta-information about the table structure.
4005 ;; `org-table-has-header-p' tells when the rows before the first rule
4006 ;; should be considered as table's header.
4008 ;; `org-export-table-cell-width', `org-export-table-cell-alignment'
4009 ;; and `org-export-table-cell-borders' extract information from
4010 ;; a table-cell element.
4012 ;; `org-export-table-dimensions' gives the number on rows and columns
4013 ;; in the table, ignoring horizontal rules and special columns.
4014 ;; `org-export-table-cell-address', given a table-cell object, returns
4015 ;; the absolute address of a cell. On the other hand,
4016 ;; `org-export-get-table-cell-at' does the contrary.
4018 ;; `org-export-table-cell-starts-colgroup-p',
4019 ;; `org-export-table-cell-ends-colgroup-p',
4020 ;; `org-export-table-row-starts-rowgroup-p',
4021 ;; `org-export-table-row-ends-rowgroup-p',
4022 ;; `org-export-table-row-starts-header-p' and
4023 ;; `org-export-table-row-ends-header-p' indicate position of current
4024 ;; row or cell within the table.
4026 (defun org-export-table-has-special-column-p (table)
4027 "Non-nil when TABLE has a special column.
4028 All special columns will be ignored during export."
4029 ;; The table has a special column when every first cell of every row
4030 ;; has an empty value or contains a symbol among "/", "#", "!", "$",
4031 ;; "*" "_" and "^". Though, do not consider a first row containing
4032 ;; only empty cells as special.
4033 (let ((special-column-p 'empty
))
4037 (when (eq (org-element-property :type row
) 'standard
)
4038 (let ((value (org-element-contents
4039 (car (org-element-contents row
)))))
4040 (cond ((member value
'(("/") ("#") ("!") ("$") ("*") ("_") ("^")))
4041 (setq special-column-p
'special
))
4043 (t (throw 'exit nil
))))))
4044 (org-element-contents table
))
4045 (eq special-column-p
'special
))))
4047 (defun org-export-table-has-header-p (table info
)
4048 "Non-nil when TABLE has an header.
4050 INFO is a plist used as a communication channel.
4052 A table has an header when it contains at least two row groups."
4053 (let ((rowgroup 1) row-flag
)
4054 (org-element-map table
'table-row
4058 ((and row-flag
(eq (org-element-property :type row
) 'rule
))
4059 (incf rowgroup
) (setq row-flag nil
))
4060 ((and (not row-flag
) (eq (org-element-property :type row
) 'standard
))
4061 (setq row-flag t
) nil
)))
4064 (defun org-export-table-row-is-special-p (table-row info
)
4065 "Non-nil if TABLE-ROW is considered special.
4067 INFO is a plist used as the communication channel.
4069 All special rows will be ignored during export."
4070 (when (eq (org-element-property :type table-row
) 'standard
)
4071 (let ((first-cell (org-element-contents
4072 (car (org-element-contents table-row
)))))
4073 ;; A row is special either when...
4075 ;; ... it starts with a field only containing "/",
4076 (equal first-cell
'("/"))
4077 ;; ... the table contains a special column and the row start
4078 ;; with a marking character among, "^", "_", "$" or "!",
4079 (and (org-export-table-has-special-column-p
4080 (org-export-get-parent table-row
))
4081 (member first-cell
'(("^") ("_") ("$") ("!"))))
4082 ;; ... it contains only alignment cookies and empty cells.
4083 (let ((special-row-p 'empty
))
4087 (let ((value (org-element-contents cell
)))
4088 ;; Since VALUE is a secondary string, the following
4089 ;; checks avoid expanding it with `org-export-data'.
4091 ((and (not (cdr value
))
4092 (stringp (car value
))
4093 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'"
4095 (setq special-row-p
'cookie
))
4096 (t (throw 'exit nil
)))))
4097 (org-element-contents table-row
))
4098 (eq special-row-p
'cookie
)))))))
4100 (defun org-export-table-row-group (table-row info
)
4101 "Return TABLE-ROW's group.
4103 INFO is a plist used as the communication channel.
4105 Return value is the group number, as an integer, or nil special
4106 rows and table rules. Group 1 is also table's header."
4107 (unless (or (eq (org-element-property :type table-row
) 'rule
)
4108 (org-export-table-row-is-special-p table-row info
))
4109 (let ((group 0) row-flag
)
4114 ((and (eq (org-element-property :type row
) 'standard
)
4115 (not (org-export-table-row-is-special-p row info
)))
4116 (unless row-flag
(incf group
) (setq row-flag t
)))
4117 ((eq (org-element-property :type row
) 'rule
)
4118 (setq row-flag nil
)))
4119 (when (eq table-row row
) (throw 'found group
)))
4120 (org-element-contents (org-export-get-parent table-row
)))))))
4122 (defun org-export-table-cell-width (table-cell info
)
4123 "Return TABLE-CELL contents width.
4125 INFO is a plist used as the communication channel.
4127 Return value is the width given by the last width cookie in the
4128 same column as TABLE-CELL, or nil."
4129 (let* ((row (org-export-get-parent table-cell
))
4130 (column (let ((cells (org-element-contents row
)))
4131 (- (length cells
) (length (memq table-cell cells
)))))
4132 (table (org-export-get-parent-table table-cell
))
4137 ;; In a special row, try to find a width cookie at COLUMN.
4138 ((org-export-table-row-is-special-p row info
)
4139 (let ((value (org-element-contents
4140 (elt (org-element-contents row
) column
))))
4141 ;; The following checks avoid expanding unnecessarily the
4142 ;; cell with `org-export-data'
4145 (stringp (car value
))
4146 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'" (car value
))
4147 (match-string 1 (car value
)))
4149 (string-to-number (match-string 1 (car value
)))))))
4150 ;; Ignore table rules.
4151 ((eq (org-element-property :type row
) 'rule
))))
4152 (org-element-contents table
))
4156 (defun org-export-table-cell-alignment (table-cell info
)
4157 "Return TABLE-CELL contents alignment.
4159 INFO is a plist used as the communication channel.
4161 Return alignment as specified by the last alignment cookie in the
4162 same column as TABLE-CELL. If no such cookie is found, a default
4163 alignment value will be deduced from fraction of numbers in the
4164 column (see `org-table-number-fraction' for more information).
4165 Possible values are `left', `right' and `center'."
4166 (let* ((row (org-export-get-parent table-cell
))
4167 (column (let ((cells (org-element-contents row
)))
4168 (- (length cells
) (length (memq table-cell cells
)))))
4169 (table (org-export-get-parent-table table-cell
))
4176 ;; In a special row, try to find an alignment cookie at
4178 ((org-export-table-row-is-special-p row info
)
4179 (let ((value (org-element-contents
4180 (elt (org-element-contents row
) column
))))
4181 ;; Since VALUE is a secondary string, the following checks
4182 ;; avoid useless expansion through `org-export-data'.
4185 (stringp (car value
))
4186 (string-match "\\`<\\([lrc]\\)?\\([0-9]+\\)?>\\'"
4188 (match-string 1 (car value
)))
4189 (setq cookie-align
(match-string 1 (car value
))))))
4190 ;; Ignore table rules.
4191 ((eq (org-element-property :type row
) 'rule
))
4192 ;; In a standard row, check if cell's contents are expressing
4193 ;; some kind of number. Increase NUMBER-CELLS accordingly.
4194 ;; Though, don't bother if an alignment cookie has already
4195 ;; defined cell's alignment.
4197 (let ((value (org-export-data
4198 (org-element-contents
4199 (elt (org-element-contents row
) column
))
4202 (when (string-match org-table-number-regexp value
)
4203 (incf number-cells
))))))
4204 (org-element-contents table
))
4205 ;; Return value. Alignment specified by cookies has precedence
4206 ;; over alignment deduced from cells contents.
4207 (cond ((equal cookie-align
"l") 'left
)
4208 ((equal cookie-align
"r") 'right
)
4209 ((equal cookie-align
"c") 'center
)
4210 ((>= (/ (float number-cells
) total-cells
) org-table-number-fraction
)
4214 (defun org-export-table-cell-borders (table-cell info
)
4215 "Return TABLE-CELL borders.
4217 INFO is a plist used as a communication channel.
4219 Return value is a list of symbols, or nil. Possible values are:
4220 `top', `bottom', `above', `below', `left' and `right'. Note:
4221 `top' (resp. `bottom') only happen for a cell in the first
4222 row (resp. last row) of the table, ignoring table rules, if any.
4224 Returned borders ignore special rows."
4225 (let* ((row (org-export-get-parent table-cell
))
4226 (table (org-export-get-parent-table table-cell
))
4228 ;; Top/above border? TABLE-CELL has a border above when a rule
4229 ;; used to demarcate row groups can be found above. Hence,
4230 ;; finding a rule isn't sufficient to push `above' in BORDERS:
4231 ;; another regular row has to be found above that rule.
4235 (cond ((eq (org-element-property :type row
) 'rule
)
4237 ((not (org-export-table-row-is-special-p row info
))
4238 (if rule-flag
(throw 'exit
(push 'above borders
))
4239 (throw 'exit nil
)))))
4240 ;; Look at every row before the current one.
4241 (cdr (memq row
(reverse (org-element-contents table
)))))
4242 ;; No rule above, or rule found starts the table (ignoring any
4243 ;; special row): TABLE-CELL is at the top of the table.
4244 (when rule-flag
(push 'above borders
))
4245 (push 'top borders
)))
4246 ;; Bottom/below border? TABLE-CELL has a border below when next
4247 ;; non-regular row below is a rule.
4251 (cond ((eq (org-element-property :type row
) 'rule
)
4253 ((not (org-export-table-row-is-special-p row info
))
4254 (if rule-flag
(throw 'exit
(push 'below borders
))
4255 (throw 'exit nil
)))))
4256 ;; Look at every row after the current one.
4257 (cdr (memq row
(org-element-contents table
))))
4258 ;; No rule below, or rule found ends the table (modulo some
4259 ;; special row): TABLE-CELL is at the bottom of the table.
4260 (when rule-flag
(push 'below borders
))
4261 (push 'bottom borders
)))
4262 ;; Right/left borders? They can only be specified by column
4263 ;; groups. Column groups are defined in a row starting with "/".
4264 ;; Also a column groups row only contains "<", "<>", ">" or blank
4267 (let ((column (let ((cells (org-element-contents row
)))
4268 (- (length cells
) (length (memq table-cell cells
))))))
4271 (unless (eq (org-element-property :type row
) 'rule
)
4272 (when (equal (org-element-contents
4273 (car (org-element-contents row
)))
4275 (let ((column-groups
4278 (let ((value (org-element-contents cell
)))
4279 (when (member value
'(("<") ("<>") (">") nil
))
4281 (org-element-contents row
))))
4282 ;; There's a left border when previous cell, if
4283 ;; any, ends a group, or current one starts one.
4284 (when (or (and (not (zerop column
))
4285 (member (elt column-groups
(1- column
))
4287 (member (elt column-groups column
) '("<" "<>")))
4288 (push 'left borders
))
4289 ;; There's a right border when next cell, if any,
4290 ;; starts a group, or current one ends one.
4291 (when (or (and (/= (1+ column
) (length column-groups
))
4292 (member (elt column-groups
(1+ column
))
4294 (member (elt column-groups column
) '(">" "<>")))
4295 (push 'right borders
))
4296 (throw 'exit nil
)))))
4297 ;; Table rows are read in reverse order so last column groups
4298 ;; row has precedence over any previous one.
4299 (reverse (org-element-contents table
)))))
4303 (defun org-export-table-cell-starts-colgroup-p (table-cell info
)
4304 "Non-nil when TABLE-CELL is at the beginning of a row group.
4305 INFO is a plist used as a communication channel."
4306 ;; A cell starts a column group either when it is at the beginning
4307 ;; of a row (or after the special column, if any) or when it has
4309 (or (eq (org-element-map (org-export-get-parent table-cell
) 'table-cell
4310 'identity info
'first-match
)
4312 (memq 'left
(org-export-table-cell-borders table-cell info
))))
4314 (defun org-export-table-cell-ends-colgroup-p (table-cell info
)
4315 "Non-nil when TABLE-CELL is at the end of a row group.
4316 INFO is a plist used as a communication channel."
4317 ;; A cell ends a column group either when it is at the end of a row
4318 ;; or when it has a right border.
4319 (or (eq (car (last (org-element-contents
4320 (org-export-get-parent table-cell
))))
4322 (memq 'right
(org-export-table-cell-borders table-cell info
))))
4324 (defun org-export-table-row-starts-rowgroup-p (table-row info
)
4325 "Non-nil when TABLE-ROW is at the beginning of a column group.
4326 INFO is a plist used as a communication channel."
4327 (unless (or (eq (org-element-property :type table-row
) 'rule
)
4328 (org-export-table-row-is-special-p table-row info
))
4329 (let ((borders (org-export-table-cell-borders
4330 (car (org-element-contents table-row
)) info
)))
4331 (or (memq 'top borders
) (memq 'above borders
)))))
4333 (defun org-export-table-row-ends-rowgroup-p (table-row info
)
4334 "Non-nil when TABLE-ROW is at the end of a column group.
4335 INFO is a plist used as a communication channel."
4336 (unless (or (eq (org-element-property :type table-row
) 'rule
)
4337 (org-export-table-row-is-special-p table-row info
))
4338 (let ((borders (org-export-table-cell-borders
4339 (car (org-element-contents table-row
)) info
)))
4340 (or (memq 'bottom borders
) (memq 'below borders
)))))
4342 (defun org-export-table-row-starts-header-p (table-row info
)
4343 "Non-nil when TABLE-ROW is the first table header's row.
4344 INFO is a plist used as a communication channel."
4345 (and (org-export-table-has-header-p
4346 (org-export-get-parent-table table-row
) info
)
4347 (org-export-table-row-starts-rowgroup-p table-row info
)
4348 (= (org-export-table-row-group table-row info
) 1)))
4350 (defun org-export-table-row-ends-header-p (table-row info
)
4351 "Non-nil when TABLE-ROW is the last table header's row.
4352 INFO is a plist used as a communication channel."
4353 (and (org-export-table-has-header-p
4354 (org-export-get-parent-table table-row
) info
)
4355 (org-export-table-row-ends-rowgroup-p table-row info
)
4356 (= (org-export-table-row-group table-row info
) 1)))
4358 (defun org-export-table-dimensions (table info
)
4359 "Return TABLE dimensions.
4361 INFO is a plist used as a communication channel.
4363 Return value is a CONS like (ROWS . COLUMNS) where
4364 ROWS (resp. COLUMNS) is the number of exportable
4365 rows (resp. columns)."
4366 (let (first-row (columns 0) (rows 0))
4367 ;; Set number of rows, and extract first one.
4368 (org-element-map table
'table-row
4370 (when (eq (org-element-property :type row
) 'standard
)
4372 (unless first-row
(setq first-row row
)))) info
)
4373 ;; Set number of columns.
4374 (org-element-map first-row
'table-cell
(lambda (cell) (incf columns
)) info
)
4376 (cons rows columns
)))
4378 (defun org-export-table-cell-address (table-cell info
)
4379 "Return address of a regular TABLE-CELL object.
4381 TABLE-CELL is the cell considered. INFO is a plist used as
4382 a communication channel.
4384 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
4385 zero-based index. Only exportable cells are considered. The
4386 function returns nil for other cells."
4387 (let* ((table-row (org-export-get-parent table-cell
))
4388 (table (org-export-get-parent-table table-cell
)))
4389 ;; Ignore cells in special rows or in special column.
4390 (unless (or (org-export-table-row-is-special-p table-row info
)
4391 (and (org-export-table-has-special-column-p table
)
4392 (eq (car (org-element-contents table-row
)) table-cell
)))
4395 (let ((row-count 0))
4396 (org-element-map table
'table-row
4398 (cond ((eq (org-element-property :type row
) 'rule
) nil
)
4399 ((eq row table-row
) row-count
)
4400 (t (incf row-count
) nil
)))
4403 (let ((col-count 0))
4404 (org-element-map table-row
'table-cell
4406 (if (eq cell table-cell
) col-count
(incf col-count
) nil
))
4407 info
'first-match
))))))
4409 (defun org-export-get-table-cell-at (address table info
)
4410 "Return regular table-cell object at ADDRESS in TABLE.
4412 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
4413 zero-based index. TABLE is a table type element. INFO is
4414 a plist used as a communication channel.
4416 If no table-cell, among exportable cells, is found at ADDRESS,
4418 (let ((column-pos (cdr address
)) (column-count 0))
4420 ;; Row at (car address) or nil.
4421 (let ((row-pos (car address
)) (row-count 0))
4422 (org-element-map table
'table-row
4424 (cond ((eq (org-element-property :type row
) 'rule
) nil
)
4425 ((= row-count row-pos
) row
)
4426 (t (incf row-count
) nil
)))
4430 (if (= column-count column-pos
) cell
4431 (incf column-count
) nil
))
4432 info
'first-match
)))
4435 ;;;; For Tables Of Contents
4437 ;; `org-export-collect-headlines' builds a list of all exportable
4438 ;; headline elements, maybe limited to a certain depth. One can then
4439 ;; easily parse it and transcode it.
4441 ;; Building lists of tables, figures or listings is quite similar.
4442 ;; Once the generic function `org-export-collect-elements' is defined,
4443 ;; `org-export-collect-tables', `org-export-collect-figures' and
4444 ;; `org-export-collect-listings' can be derived from it.
4446 (defun org-export-collect-headlines (info &optional n
)
4447 "Collect headlines in order to build a table of contents.
4449 INFO is a plist used as a communication channel.
4451 When optional argument N is an integer, it specifies the depth of
4452 the table of contents. Otherwise, it is set to the value of the
4453 last headline level. See `org-export-headline-levels' for more
4456 Return a list of all exportable headlines as parsed elements.
4457 Footnote sections, if any, will be ignored."
4458 (unless (wholenump n
) (setq n
(plist-get info
:headline-levels
)))
4459 (org-element-map (plist-get info
:parse-tree
) 'headline
4461 (unless (org-element-property :footnote-section-p headline
)
4462 ;; Strip contents from HEADLINE.
4463 (let ((relative-level (org-export-get-relative-level headline info
)))
4464 (unless (> relative-level n
) headline
))))
4467 (defun org-export-collect-elements (type info
&optional predicate
)
4468 "Collect referenceable elements of a determined type.
4470 TYPE can be a symbol or a list of symbols specifying element
4471 types to search. Only elements with a caption are collected.
4473 INFO is a plist used as a communication channel.
4475 When non-nil, optional argument PREDICATE is a function accepting
4476 one argument, an element of type TYPE. It returns a non-nil
4477 value when that element should be collected.
4479 Return a list of all elements found, in order of appearance."
4480 (org-element-map (plist-get info
:parse-tree
) type
4482 (and (org-element-property :caption element
)
4483 (or (not predicate
) (funcall predicate element
))
4487 (defun org-export-collect-tables (info)
4488 "Build a list of tables.
4489 INFO is a plist used as a communication channel.
4491 Return a list of table elements with a caption."
4492 (org-export-collect-elements 'table info
))
4494 (defun org-export-collect-figures (info predicate
)
4495 "Build a list of figures.
4497 INFO is a plist used as a communication channel. PREDICATE is
4498 a function which accepts one argument: a paragraph element and
4499 whose return value is non-nil when that element should be
4502 A figure is a paragraph type element, with a caption, verifying
4503 PREDICATE. The latter has to be provided since a \"figure\" is
4504 a vague concept that may depend on back-end.
4506 Return a list of elements recognized as figures."
4507 (org-export-collect-elements 'paragraph info predicate
))
4509 (defun org-export-collect-listings (info)
4510 "Build a list of src blocks.
4512 INFO is a plist used as a communication channel.
4514 Return a list of src-block elements with a caption."
4515 (org-export-collect-elements 'src-block info
))
4520 ;; The main function for the smart quotes sub-system is
4521 ;; `org-export-activate-smart-quotes', which replaces every quote in
4522 ;; a given string from the parse tree with its "smart" counterpart.
4524 ;; Dictionary for smart quotes is stored in
4525 ;; `org-export-smart-quotes-alist'.
4527 ;; Internally, regexps matching potential smart quotes (checks at
4528 ;; string boundaries are also necessary) are defined in
4529 ;; `org-export-smart-quotes-regexps'.
4531 (defconst org-export-smart-quotes-alist
4533 (opening-double-quote :utf-8
"„" :html
"„" :latex
"\"`"
4534 :texinfo
"@quotedblbase{}")
4535 (closing-double-quote :utf-8
"“" :html
"“" :latex
"\"'"
4536 :texinfo
"@quotedblleft{}")
4537 (opening-single-quote :utf-8
"‚" :html
"‚" :latex
"\\glq{}"
4538 :texinfo
"@quotesinglbase{}")
4539 (closing-single-quote :utf-8
"‘" :html
"‘" :latex
"\\grq{}"
4540 :texinfo
"@quoteleft{}")
4541 (apostrophe :utf-8
"’" :html
"’"))
4543 (opening-double-quote :utf-8
"“" :html
"“" :latex
"``" :texinfo
"``")
4544 (closing-double-quote :utf-8
"”" :html
"”" :latex
"''" :texinfo
"''")
4545 (opening-single-quote :utf-8
"‘" :html
"‘" :latex
"`" :texinfo
"`")
4546 (closing-single-quote :utf-8
"’" :html
"’" :latex
"'" :texinfo
"'")
4547 (apostrophe :utf-8
"’" :html
"’"))
4549 (opening-double-quote :utf-8
"«" :html
"«" :latex
"\\guillemotleft{}"
4550 :texinfo
"@guillemetleft{}")
4551 (closing-double-quote :utf-8
"»" :html
"»" :latex
"\\guillemotright{}"
4552 :texinfo
"@guillemetright{}")
4553 (opening-single-quote :utf-8
"“" :html
"“" :latex
"``" :texinfo
"``")
4554 (closing-single-quote :utf-8
"”" :html
"”" :latex
"''" :texinfo
"''")
4555 (apostrophe :utf-8
"’" :html
"’"))
4557 (opening-double-quote :utf-8
"« " :html
"« " :latex
"\\og "
4558 :texinfo
"@guillemetleft{}@tie{}")
4559 (closing-double-quote :utf-8
" »" :html
" »" :latex
"\\fg{}"
4560 :texinfo
"@tie{}@guillemetright{}")
4561 (opening-single-quote :utf-8
"« " :html
"« " :latex
"\\og "
4562 :texinfo
"@guillemetleft{}@tie{}")
4563 (closing-single-quote :utf-8
" »" :html
" »" :latex
"\\fg{}"
4564 :texinfo
"@tie{}@guillemetright{}")
4565 (apostrophe :utf-8
"’" :html
"’")))
4566 "Smart quotes translations.
4568 Alist whose CAR is a language string and CDR is an alist with
4569 quote type as key and a plist associating various encodings to
4570 their translation as value.
4572 A quote type can be any symbol among `opening-double-quote',
4573 `closing-double-quote', `opening-single-quote',
4574 `closing-single-quote' and `apostrophe'.
4576 Valid encodings include `:utf-8', `:html', `:latex' and
4579 If no translation is found, the quote character is left as-is.")
4581 (defconst org-export-smart-quotes-regexps
4583 ;; Possible opening quote at beginning of string.
4584 "\\`\\([\"']\\)\\(\\w\\|\\s.\\|\\s_\\)"
4585 ;; Possible closing quote at beginning of string.
4586 "\\`\\([\"']\\)\\(\\s-\\|\\s)\\|\\s.\\)"
4587 ;; Possible apostrophe at beginning of string.
4589 ;; Opening single and double quotes.
4590 "\\(?:\\s-\\|\\s(\\)\\([\"']\\)\\(?:\\w\\|\\s.\\|\\s_\\)"
4591 ;; Closing single and double quotes.
4592 "\\(?:\\w\\|\\s.\\|\\s_\\)\\([\"']\\)\\(?:\\s-\\|\\s)\\|\\s.\\)"
4595 ;; Possible opening quote at end of string.
4596 "\\(?:\\s-\\|\\s(\\)\\([\"']\\)\\'"
4597 ;; Possible closing quote at end of string.
4598 "\\(?:\\w\\|\\s.\\|\\s_\\)\\([\"']\\)\\'"
4599 ;; Possible apostrophe at end of string.
4601 "List of regexps matching a quote or an apostrophe.
4602 In every regexp, quote or apostrophe matched is put in group 1.")
4604 (defun org-export-activate-smart-quotes (s encoding info
&optional original
)
4605 "Replace regular quotes with \"smart\" quotes in string S.
4607 ENCODING is a symbol among `:html', `:latex', `:texinfo' and
4608 `:utf-8'. INFO is a plist used as a communication channel.
4610 The function has to retrieve information about string
4611 surroundings in parse tree. It can only happen with an
4612 unmodified string. Thus, if S has already been through another
4613 process, a non-nil ORIGINAL optional argument will provide that
4616 Return the new string."
4618 (let* ((prev (org-export-get-previous-element (or original s
) info
))
4619 ;; Try to be flexible when computing number of blanks
4620 ;; before object. The previous object may be a string
4621 ;; introduced by the back-end and not completely parsed.
4622 (pre-blank (and prev
4623 (or (org-element-property :post-blank prev
)
4624 ;; A string with missing `:post-blank'
4627 (string-match " *\\'" prev
)
4628 (length (match-string 0 prev
)))
4631 (next (org-export-get-next-element (or original s
) info
))
4634 ;; Return smart quote associated to a give quote Q, as
4635 ;; a string. TYPE is a symbol among `open', `close' and
4637 (let ((key (case type
4638 (apostrophe 'apostrophe
)
4639 (open (if (equal "'" q
) 'opening-single-quote
4640 'opening-double-quote
))
4641 (otherwise (if (equal "'" q
) 'closing-single-quote
4642 'closing-double-quote
)))))
4645 (cdr (assoc (plist-get info
:language
)
4646 org-export-smart-quotes-alist
))))
4649 (if (or (equal "\"" s
) (equal "'" s
))
4650 ;; Only a quote: no regexp can match. We have to check both
4651 ;; sides and decide what to do.
4652 (cond ((and (not prev
) (not next
)) s
)
4653 ((not prev
) (funcall get-smart-quote s
'open
))
4654 ((and (not next
) (zerop pre-blank
))
4655 (funcall get-smart-quote s
'close
))
4657 ((zerop pre-blank
) (funcall get-smart-quote s
'apostrophe
))
4658 (t (funcall get-smart-quote
'open
)))
4659 ;; 1. Replace quote character at the beginning of S.
4662 ((and prev
(zerop pre-blank
)
4663 (string-match (nth 2 org-export-smart-quotes-regexps
) s
))
4664 (setq s
(replace-match
4665 (funcall get-smart-quote
(match-string 1 s
) 'apostrophe
)
4668 ((and prev
(zerop pre-blank
)
4669 (string-match (nth 1 org-export-smart-quotes-regexps
) s
))
4670 (setq s
(replace-match
4671 (funcall get-smart-quote
(match-string 1 s
) 'close
)
4674 ((and (or (not prev
) (> pre-blank
0))
4675 (string-match (nth 0 org-export-smart-quotes-regexps
) s
))
4676 (setq s
(replace-match
4677 (funcall get-smart-quote
(match-string 1 s
) 'open
)
4679 ;; 2. Replace quotes in the middle of the string.
4680 (setq s
(replace-regexp-in-string
4682 (nth 3 org-export-smart-quotes-regexps
)
4684 (funcall get-smart-quote
(match-string 1 text
) 'open
))
4686 (setq s
(replace-regexp-in-string
4688 (nth 4 org-export-smart-quotes-regexps
)
4690 (funcall get-smart-quote
(match-string 1 text
) 'close
))
4692 (setq s
(replace-regexp-in-string
4694 (nth 5 org-export-smart-quotes-regexps
)
4696 (funcall get-smart-quote
(match-string 1 text
) 'apostrophe
))
4698 ;; 3. Replace quote character at the end of S.
4701 ((and next
(string-match (nth 8 org-export-smart-quotes-regexps
) s
))
4702 (setq s
(replace-match
4703 (funcall get-smart-quote
(match-string 1 s
) 'apostrophe
)
4707 (string-match (nth 7 org-export-smart-quotes-regexps
) s
))
4708 (setq s
(replace-match
4709 (funcall get-smart-quote
(match-string 1 s
) 'close
)
4712 ((and next
(string-match (nth 6 org-export-smart-quotes-regexps
) s
))
4713 (setq s
(replace-match
4714 (funcall get-smart-quote
(match-string 1 s
) 'open
)
4716 ;; Return string with smart quotes.
4721 ;; Here are various functions to retrieve information about the
4722 ;; neighbourhood of a given element or object. Neighbours of interest
4723 ;; are direct parent (`org-export-get-parent'), parent headline
4724 ;; (`org-export-get-parent-headline'), first element containing an
4725 ;; object, (`org-export-get-parent-element'), parent table
4726 ;; (`org-export-get-parent-table'), previous element or object
4727 ;; (`org-export-get-previous-element') and next element or object
4728 ;; (`org-export-get-next-element').
4730 ;; `org-export-get-genealogy' returns the full genealogy of a given
4731 ;; element or object, from closest parent to full parse tree.
4733 (defun org-export-get-parent (blob)
4734 "Return BLOB parent or nil.
4735 BLOB is the element or object considered."
4736 (org-element-property :parent blob
))
4738 (defun org-export-get-genealogy (blob)
4739 "Return full genealogy relative to a given element or object.
4741 BLOB is the element or object being considered.
4743 Ancestors are returned from closest to farthest, the last one
4744 being the full parse tree."
4745 (let (genealogy (parent blob
))
4746 (while (setq parent
(org-element-property :parent parent
))
4747 (push parent genealogy
))
4748 (nreverse genealogy
)))
4750 (defun org-export-get-parent-headline (blob)
4751 "Return BLOB parent headline or nil.
4752 BLOB is the element or object being considered."
4753 (let ((parent blob
))
4754 (while (and (setq parent
(org-element-property :parent parent
))
4755 (not (eq (org-element-type parent
) 'headline
))))
4758 (defun org-export-get-parent-element (object)
4759 "Return first element containing OBJECT or nil.
4760 OBJECT is the object to consider."
4761 (let ((parent object
))
4762 (while (and (setq parent
(org-element-property :parent parent
))
4763 (memq (org-element-type parent
) org-element-all-objects
)))
4766 (defun org-export-get-parent-table (object)
4767 "Return OBJECT parent table or nil.
4768 OBJECT is either a `table-cell' or `table-element' type object."
4769 (let ((parent object
))
4770 (while (and (setq parent
(org-element-property :parent parent
))
4771 (not (eq (org-element-type parent
) 'table
))))
4774 (defun org-export-get-previous-element (blob info
&optional n
)
4775 "Return previous element or object.
4777 BLOB is an element or object. INFO is a plist used as
4778 a communication channel. Return previous exportable element or
4779 object, a string, or nil.
4781 When optional argument N is a positive integer, return a list
4782 containing up to N siblings before BLOB, from closest to
4783 farthest. With any other non-nil value, return a list containing
4786 ;; An object can belong to the contents of its parent or
4787 ;; to a secondary string. We check the latter option
4789 (let ((parent (org-export-get-parent blob
)))
4790 (or (and (not (memq (org-element-type blob
)
4791 org-element-all-elements
))
4793 (org-element-property
4794 (cdr (assq (org-element-type parent
)
4795 org-element-secondary-value-alist
))
4797 (and (memq blob sec-value
) sec-value
)))
4798 (org-element-contents parent
))))
4802 (cond ((memq obj
(plist-get info
:ignore-list
)))
4803 ((null n
) (throw 'exit obj
))
4804 ((not (wholenump n
)) (push obj prev
))
4805 ((zerop n
) (throw 'exit
(nreverse prev
)))
4806 (t (decf n
) (push obj prev
))))
4807 (cdr (memq blob
(reverse siblings
))))
4810 (defun org-export-get-next-element (blob info
&optional n
)
4811 "Return next element or object.
4813 BLOB is an element or object. INFO is a plist used as
4814 a communication channel. Return next exportable element or
4815 object, a string, or nil.
4817 When optional argument N is a positive integer, return a list
4818 containing up to N siblings after BLOB, from closest to farthest.
4819 With any other non-nil value, return a list containing all of
4822 ;; An object can belong to the contents of its parent or to
4823 ;; a secondary string. We check the latter option first.
4824 (let ((parent (org-export-get-parent blob
)))
4825 (or (and (not (memq (org-element-type blob
)
4826 org-element-all-objects
))
4828 (org-element-property
4829 (cdr (assq (org-element-type parent
)
4830 org-element-secondary-value-alist
))
4832 (cdr (memq blob sec-value
))))
4833 (cdr (memq blob
(org-element-contents parent
))))))
4837 (cond ((memq obj
(plist-get info
:ignore-list
)))
4838 ((null n
) (throw 'exit obj
))
4839 ((not (wholenump n
)) (push obj next
))
4840 ((zerop n
) (throw 'exit
(nreverse next
)))
4841 (t (decf n
) (push obj next
))))
4848 ;; `org-export-translate' translates a string according to language
4849 ;; specified by LANGUAGE keyword or `org-export-language-setup'
4850 ;; variable and a specified charset. `org-export-dictionary' contains
4851 ;; the dictionary used for the translation.
4853 (defconst org-export-dictionary
4855 ("ca" :default
"Autor")
4856 ("cs" :default
"Autor")
4857 ("da" :default
"Ophavsmand")
4858 ("de" :default
"Autor")
4859 ("eo" :html
"Aŭtoro")
4860 ("es" :default
"Autor")
4861 ("fi" :html
"Tekijä")
4862 ("fr" :default
"Auteur")
4863 ("hu" :default
"Szerzõ")
4864 ("is" :html
"Höfundur")
4865 ("it" :default
"Autore")
4866 ("ja" :html
"著者" :utf-8
"著者")
4867 ("nl" :default
"Auteur")
4868 ("no" :default
"Forfatter")
4869 ("nb" :default
"Forfatter")
4870 ("nn" :default
"Forfattar")
4871 ("pl" :default
"Autor")
4872 ("ru" :html
"Автор" :utf-8
"Автор")
4873 ("sv" :html
"Författare")
4874 ("uk" :html
"Автор" :utf-8
"Автор")
4875 ("zh-CN" :html
"作者" :utf-8
"作者")
4876 ("zh-TW" :html
"作者" :utf-8
"作者"))
4878 ("ca" :default
"Data")
4879 ("cs" :default
"Datum")
4880 ("da" :default
"Dato")
4881 ("de" :default
"Datum")
4882 ("eo" :default
"Dato")
4883 ("es" :default
"Fecha")
4884 ("fi" :html
"Päivämäärä")
4885 ("hu" :html
"Dátum")
4886 ("is" :default
"Dagsetning")
4887 ("it" :default
"Data")
4888 ("ja" :html
"日付" :utf-8
"日付")
4889 ("nl" :default
"Datum")
4890 ("no" :default
"Dato")
4891 ("nb" :default
"Dato")
4892 ("nn" :default
"Dato")
4893 ("pl" :default
"Data")
4894 ("ru" :html
"Дата" :utf-8
"Дата")
4895 ("sv" :default
"Datum")
4896 ("uk" :html
"Дата" :utf-8
"Дата")
4897 ("zh-CN" :html
"日期" :utf-8
"日期")
4898 ("zh-TW" :html
"日期" :utf-8
"日期"))
4900 ("fr" :ascii
"Equation" :default
"Équation"))
4903 ("ca" :html
"Peus de pàgina")
4904 ("cs" :default
"Pozn\xe1mky pod carou")
4905 ("da" :default
"Fodnoter")
4906 ("de" :html
"Fußnoten")
4907 ("eo" :default
"Piednotoj")
4908 ("es" :html
"Pies de página")
4909 ("fi" :default
"Alaviitteet")
4910 ("fr" :default
"Notes de bas de page")
4911 ("hu" :html
"Lábjegyzet")
4912 ("is" :html
"Aftanmálsgreinar")
4913 ("it" :html
"Note a piè di pagina")
4914 ("ja" :html
"脚注" :utf-8
"脚注")
4915 ("nl" :default
"Voetnoten")
4916 ("no" :default
"Fotnoter")
4917 ("nb" :default
"Fotnoter")
4918 ("nn" :default
"Fotnotar")
4919 ("pl" :default
"Przypis")
4920 ("ru" :html
"Сноски" :utf-8
"Сноски")
4921 ("sv" :default
"Fotnoter")
4922 ("uk" :html
"Примітки"
4924 ("zh-CN" :html
"脚注" :utf-8
"脚注")
4925 ("zh-TW" :html
"腳註" :utf-8
"腳註"))
4927 ("fr" :default
"Liste des programmes"))
4929 ("fr" :default
"Liste des tableaux"))
4932 :ascii
"Programme %d :" :default
"Programme nº %d :"
4933 :latin1
"Programme %d :"))
4936 :ascii
"Programme %d : %s" :default
"Programme nº %d : %s"
4937 :latin1
"Programme %d : %s"))
4939 ("fr" :default
"cf. section %s"))
4942 :ascii
"Tableau %d :" :default
"Tableau nº %d :" :latin1
"Tableau %d :"))
4945 :ascii
"Tableau %d : %s" :default
"Tableau nº %d : %s"
4946 :latin1
"Tableau %d : %s"))
4947 ("Table of Contents"
4948 ("ca" :html
"Índex")
4949 ("cs" :default
"Obsah")
4950 ("da" :default
"Indhold")
4951 ("de" :default
"Inhaltsverzeichnis")
4952 ("eo" :default
"Enhavo")
4953 ("es" :html
"Índice")
4954 ("fi" :html
"Sisällysluettelo")
4955 ("fr" :ascii
"Sommaire" :default
"Table des matières")
4956 ("hu" :html
"Tartalomjegyzék")
4957 ("is" :default
"Efnisyfirlit")
4958 ("it" :default
"Indice")
4959 ("ja" :html
"目次" :utf-8
"目次")
4960 ("nl" :default
"Inhoudsopgave")
4961 ("no" :default
"Innhold")
4962 ("nb" :default
"Innhold")
4963 ("nn" :default
"Innhald")
4964 ("pl" :html
"Spis treści")
4965 ("ru" :html
"Содержание"
4966 :utf-8
"Содержание")
4967 ("sv" :html
"Innehåll")
4968 ("uk" :html
"Зміст" :utf-8
"Зміст")
4969 ("zh-CN" :html
"目录" :utf-8
"目录")
4970 ("zh-TW" :html
"目錄" :utf-8
"目錄"))
4971 ("Unknown reference"
4972 ("fr" :ascii
"Destination inconnue" :default
"Référence inconnue")))
4973 "Dictionary for export engine.
4975 Alist whose CAR is the string to translate and CDR is an alist
4976 whose CAR is the language string and CDR is a plist whose
4977 properties are possible charsets and values translated terms.
4979 It is used as a database for `org-export-translate'. Since this
4980 function returns the string as-is if no translation was found,
4981 the variable only needs to record values different from the
4984 (defun org-export-translate (s encoding info
)
4985 "Translate string S according to language specification.
4987 ENCODING is a symbol among `:ascii', `:html', `:latex', `:latin1'
4988 and `:utf-8'. INFO is a plist used as a communication channel.
4990 Translation depends on `:language' property. Return the
4991 translated string. If no translation is found, try to fall back
4992 to `:default' encoding. If it fails, return S."
4993 (let* ((lang (plist-get info
:language
))
4994 (translations (cdr (assoc lang
4995 (cdr (assoc s org-export-dictionary
))))))
4996 (or (plist-get translations encoding
)
4997 (plist-get translations
:default
)
5002 ;;; Asynchronous Export
5004 ;; `org-export-async-start' is the entry point for asynchronous
5005 ;; export. It recreates current buffer (including visibility,
5006 ;; narrowing and visited file) in an external Emacs process, and
5007 ;; evaluates a command there. It then applies a function on the
5008 ;; returned results in the current process.
5010 ;; Asynchronously generated results are never displayed directly.
5011 ;; Instead, they are stored in `org-export-stack-contents'. They can
5012 ;; then be retrieved by calling `org-export-stack'.
5014 ;; Export Stack is viewed through a dedicated major mode
5015 ;;`org-export-stack-mode' and tools: `org-export-stack-refresh',
5016 ;;`org-export-stack-delete', `org-export-stack-view' and
5017 ;;`org-export-stack-clear'.
5019 ;; For back-ends, `org-export-add-to-stack' add a new source to stack.
5020 ;; It should used whenever `org-export-async-start' is called.
5022 (defmacro org-export-async-start
(fun &rest body
)
5023 "Call function FUN on the results returned by BODY evaluation.
5025 BODY evaluation happens in an asynchronous process, from a buffer
5026 which is an exact copy of the current one.
5028 Use `org-export-add-to-stack' in FUN in order to register results
5029 in the stack. Examples for, respectively a temporary buffer and
5032 \(org-export-async-start
5034 \(with-current-buffer (get-buffer-create \"*Org BACKEND Export*\")
5037 \(goto-char (point-min))
5038 \(org-export-add-to-stack (current-buffer) 'backend)))
5039 `(org-export-as 'backend ,subtreep ,visible-only ,body-only ',ext-plist))
5043 \(org-export-async-start
5044 \(lambda (f) (org-export-add-to-stack f 'backend))
5046 \(org-export-to-file
5047 'backend ,outfile ,subtreep ,visible-only ,body-only ',ext-plist)))"
5048 (declare (indent 1) (debug t
))
5049 (org-with-gensyms (process temp-file copy-fun proc-buffer handler
)
5050 ;; Write the full sexp evaluating BODY in a copy of the current
5051 ;; buffer to a temporary file, as it may be too long for program
5052 ;; args in `start-process'.
5053 `(with-temp-message "Initializing asynchronous export process"
5054 (let ((,copy-fun
(org-export--generate-copy-script (current-buffer)))
5055 (,temp-file
(make-temp-file "org-export-process")))
5056 (with-temp-file ,temp-file
5061 ,(when org-export-async-debug
'(setq debug-on-error t
))
5062 ;; Initialize `org-mode' and export framework in the
5063 ;; external process.
5066 ;; Re-create current buffer there.
5067 (funcall ,,copy-fun
)
5068 (restore-buffer-modified-p nil
)
5069 ;; Sexp to evaluate in the buffer.
5070 (print (progn ,,@body
))))))
5071 ;; Start external process.
5072 (let* ((process-connection-type nil
)
5073 (,proc-buffer
(generate-new-buffer-name "*Org Export Process*"))
5076 "org-export-process" ,proc-buffer
5077 (expand-file-name invocation-name invocation-directory
)
5079 "-l" org-export-async-init-file
5081 ;; Register running process in stack.
5082 (org-export-add-to-stack (get-buffer ,proc-buffer
) nil
,process
)
5083 ;; Set-up sentinel in order to catch results.
5084 (set-process-sentinel
5086 (let ((handler ',fun
))
5088 (let ((proc-buffer (process-buffer p
)))
5089 (when (eq (process-status p
) 'exit
)
5091 (if (zerop (process-exit-status p
))
5094 (with-current-buffer proc-buffer
5095 (goto-char (point-max))
5097 (read (current-buffer)))))
5098 (funcall ,handler results
))
5099 (unless org-export-async-debug
5100 (and (get-buffer proc-buffer
)
5101 (kill-buffer proc-buffer
))))
5102 (org-export-add-to-stack proc-buffer nil p
)
5104 (message "Process '%s' exited abnormally" p
))
5105 (unless org-export-async-debug
5106 (delete-file ,,temp-file
)))))))))))))
5108 (defun org-export-add-to-stack (source backend
&optional process
)
5109 "Add a new result to export stack if not present already.
5111 SOURCE is a buffer or a file name containing export results.
5112 BACKEND is a symbol representing export back-end used to generate
5115 Entries already pointing to SOURCE and unavailable entries are
5116 removed beforehand. Return the new stack."
5117 (setq org-export-stack-contents
5118 (cons (list source backend
(or process
(current-time)))
5119 (org-export-stack-remove source
))))
5121 (defun org-export-stack ()
5122 "Menu for asynchronous export results and running processes."
5124 (let ((buffer (get-buffer-create "*Org Export Stack*")))
5126 (when (zerop (buffer-size)) (org-export-stack-mode))
5127 (org-export-stack-refresh)
5128 (pop-to-buffer buffer
))
5129 (message "Type \"q\" to quit, \"?\" for help"))
5131 (defun org-export--stack-source-at-point ()
5132 "Return source from export results at point in stack."
5133 (let ((source (car (nth (1- (org-current-line)) org-export-stack-contents
))))
5134 (if (not source
) (error "Source unavailable, please refresh buffer")
5135 (let ((source-name (if (stringp source
) source
(buffer-name source
))))
5138 (looking-at (concat ".* +" (regexp-quote source-name
) "$")))
5140 ;; SOURCE is not consistent with current line. The stack
5141 ;; view is outdated.
5142 (error "Source unavailable; type `g' to update buffer"))))))
5144 (defun org-export-stack-clear ()
5145 "Remove all entries from export stack."
5147 (setq org-export-stack-contents nil
))
5149 (defun org-export-stack-refresh (&rest dummy
)
5150 "Refresh the asynchronous export stack.
5151 DUMMY is ignored. Unavailable sources are removed from the list.
5152 Return the new stack."
5153 (let ((inhibit-read-only t
))
5160 (let ((proc-p (processp (nth 2 entry
))))
5163 (format " %-12s " (or (nth 1 entry
) ""))
5165 (let ((data (nth 2 entry
)))
5166 (if proc-p
(format " %6s " (process-status data
))
5167 ;; Compute age of the results.
5170 (float-time (time-since data
)))))
5173 (let ((source (car entry
)))
5174 (if (stringp source
) source
5175 (buffer-name source
)))))))
5176 ;; Clear stack from exited processes, dead buffers or
5177 ;; non-existent files.
5178 (setq org-export-stack-contents
5181 (if (processp (nth 2 el
))
5182 (buffer-live-p (process-buffer (nth 2 el
)))
5183 (let ((source (car el
)))
5184 (if (bufferp source
) (buffer-live-p source
)
5185 (file-exists-p source
)))))
5186 org-export-stack-contents
)) "\n")))))))
5188 (defun org-export-stack-remove (&optional source
)
5189 "Remove export results at point from stack.
5190 If optional argument SOURCE is non-nil, remove it instead."
5192 (let ((source (or source
(org-export--stack-source-at-point))))
5193 (setq org-export-stack-contents
5194 (org-remove-if (lambda (el) (equal (car el
) source
))
5195 org-export-stack-contents
))))
5197 (defun org-export-stack-view (&optional in-emacs
)
5198 "View export results at point in stack.
5199 With an optional prefix argument IN-EMACS, force viewing files
5202 (let ((source (org-export--stack-source-at-point)))
5203 (cond ((processp source
)
5204 (org-switch-to-buffer-other-window (process-buffer source
)))
5205 ((bufferp source
) (org-switch-to-buffer-other-window source
))
5206 (t (org-open-file source in-emacs
)))))
5208 (defconst org-export-stack-mode-map
5209 (let ((km (make-sparse-keymap)))
5210 (define-key km
" " 'next-line
)
5211 (define-key km
"n" 'next-line
)
5212 (define-key km
"\C-n" 'next-line
)
5213 (define-key km
[down] 'next-line)
5214 (define-key km "p" 'previous-line)
5215 (define-key km "\C-p" 'previous-line)
5216 (define-key km "\C-?" 'previous-line)
5217 (define-key km [up] 'previous-line)
5218 (define-key km "C" 'org-export-stack-clear)
5219 (define-key km "v" 'org-export-stack-view)
5220 (define-key km (kbd "RET") 'org-export-stack-view)
5221 (define-key km "d" 'org-export-stack-remove)
5223 "Keymap for Org Export Stack.")
5225 (define-derived-mode org-export-stack-mode special-mode "Org-Stack"
5226 "Mode for displaying asynchronous export stack.
5228 Type \\[org-export-stack] to visualize the asynchronous export
5231 In an Org Export Stack buffer, use \\<org-export-stack-mode-map>\\[org-export-stack-view] to view export output
5232 on current line, \\[org-export-stack-remove] to remove it from the stack and \\[org-export-stack-clear] to clear
5235 Removing entries in an Org Export Stack buffer doesn't affect
5236 files or buffers, only the display.
5238 \\{org-export-stack-mode-map}"
5241 (setq buffer-read-only t
5246 (format " %-12s | %6s | %s" "Back-End" "Age" "Source")))
5247 (add-hook 'post-command-hook 'org-export-stack-refresh nil t)
5248 (set (make-local-variable 'revert-buffer-function)
5249 'org-export-stack-refresh))
5255 ;; `org-export-dispatch' is the standard interactive way to start an
5256 ;; export process. It uses `org-export--dispatch-ui' as a subroutine
5257 ;; for its interface, which, in turn, delegates response to key
5258 ;; pressed to `org-export--dispatch-action'.
5261 (defun org-export-dispatch (&optional arg)
5262 "Export dispatcher for Org mode.
5264 It provides an access to common export related tasks in a buffer.
5265 Its interface comes in two flavours: standard and expert.
5267 While both share the same set of bindings, only the former
5268 displays the valid keys associations in a dedicated buffer.
5269 Scrolling (resp. line-wise motion) in this buffer is done with
5270 SPC and DEL (resp. C-n and C-p) keys.
5272 Set variable `org-export-dispatch-use-expert-ui' to switch to one
5273 flavour or the other.
5275 When ARG is \\[universal-argument], repeat the last export action, with the same set
5276 of options used back then, on the current buffer.
5278 When ARG is \\[universal-argument] \\[universal-argument], display the asynchronous export stack."
5281 (cond ((equal arg '(16)) '(stack))
5282 ((and arg org-export-dispatch-last-action))
5283 (t (save-window-excursion
5285 ;; Store this export command.
5286 (setq org-export-dispatch-last-action
5287 (org-export--dispatch-ui
5288 (list org-export-initial-scope
5289 (and org-export-in-background 'async))
5291 org-export-dispatch-use-expert-ui))
5292 (and (get-buffer "*Org Export Dispatcher*")
5293 (kill-buffer "*Org Export Dispatcher*")))))))
5294 (action (car input))
5295 (optns (cdr input)))
5297 ;; First handle special hard-coded actions.
5298 (stack (org-export-stack))
5299 (publish-current-file
5300 (org-publish-current-file (memq 'force optns) (memq 'async optns)))
5301 (publish-current-project
5302 (org-publish-current-project (memq 'force optns) (memq 'async optns)))
5303 (publish-choose-project
5304 (org-publish (assoc (org-icompleting-read
5306 org-publish-project-alist nil t)
5307 org-publish-project-alist)
5309 (memq 'async optns)))
5310 (publish-all (org-publish-all (memq 'force optns) (memq 'async optns)))
5311 (otherwise (funcall action
5312 ;; Return a symbol instead of a list to ease
5313 ;; asynchronous export macro use.
5314 (and (memq 'async optns) t)
5315 (and (memq 'subtree optns) t)
5316 (and (memq 'visible optns) t)
5317 (and (memq 'body optns) t))))))
5319 (defun org-export--dispatch-ui (options first-key expertp)
5320 "Handle interface for `org-export-dispatch'.
5322 OPTIONS is a list containing current interactive options set for
5323 export. It can contain any of the following symbols:
5324 `body' toggles a body-only export
5325 `subtree' restricts export to current subtree
5326 `visible' restricts export to visible part of buffer.
5327 `force' force publishing files.
5328 `async' use asynchronous export process
5330 FIRST-KEY is the key pressed to select the first level menu. It
5331 is nil when this menu hasn't been selected yet.
5333 EXPERTP, when non-nil, triggers expert UI. In that case, no help
5334 buffer is provided, but indications about currently active
5335 options are given in the prompt. Moreover, \[?] allows to switch
5336 back to standard interface."
5338 (lambda (key &optional access-key)
5339 ;; Fontify KEY string. Optional argument ACCESS-KEY, when
5340 ;; non-nil is the required first-level key to activate
5341 ;; KEY. When its value is t, activate KEY independently
5342 ;; on the first key, if any. A nil value means KEY will
5343 ;; only be activated at first level.
5344 (if (or (eq access-key t) (eq access-key first-key))
5345 (org-propertize key 'face 'org-warning)
5349 ;; Fontify VALUE string.
5350 (org-propertize value 'face 'font-lock-variable-name-face)))
5351 ;; Prepare menu entries by extracting them from
5352 ;; `org-export-registered-backends', and sorting them by
5353 ;; access key and by ordinal, if any.
5360 (let ((name (car b)))
5362 ;; Ignore any back-end belonging to
5363 ;; `org-export-invisible-backends' or derived
5364 ;; from one of them.
5365 (dolist (ignored org-export-invisible-backends)
5366 (when (org-export-derived-backend-p name ignored)
5367 (throw 'ignored nil)))
5368 (org-export-backend-menu name))))
5369 org-export-registered-backends))
5371 (let ((key-a (nth 1 a))
5373 (cond ((and (numberp key-a) (numberp key-b))
5375 ((numberp key-b) t)))))
5376 (lambda (a b) (< (car a) (car b)))))
5377 ;; Compute a list of allowed keys based on the first key
5378 ;; pressed, if any. Some keys (?^B, ?^V, ?^S, ?^F, ?^A
5379 ;; and ?q) are always available.
5381 (nconc (list ?\x02 ?\x16 ?\x13 ?\x06 ?\x01)
5382 (if (not first-key) (org-uniquify (mapcar 'car backends))
5384 (dolist (backend backends (sort (mapcar 'car sub-menu) '<))
5385 (when (eq (car backend) first-key)
5386 (setq sub-menu (append (nth 2 backend) sub-menu))))))
5387 (cond ((eq first-key ?P) (list ?f ?p ?x ?a))
5388 ((not first-key) (list ?P)))
5390 (when expertp (list ??))
5392 ;; Build the help menu for standard UI.
5396 ;; Options are hard-coded.
5398 [%s] Body only: %s [%s] Visible only: %s
5399 [%s] Export scope: %s [%s] Force publishing: %s
5400 [%s] Async export: %s\n"
5401 (funcall fontify-key "C-b" t)
5402 (funcall fontify-value
5403 (if (memq 'body options) "On " "Off"))
5404 (funcall fontify-key "C-v" t)
5405 (funcall fontify-value
5406 (if (memq 'visible options) "On " "Off"))
5407 (funcall fontify-key "C-s" t)
5408 (funcall fontify-value
5409 (if (memq 'subtree options) "Subtree" "Buffer "))
5410 (funcall fontify-key "C-f" t)
5411 (funcall fontify-value
5412 (if (memq 'force options) "On " "Off"))
5413 (funcall fontify-key "C-a" t)
5414 (funcall fontify-value
5415 (if (memq 'async options) "On " "Off")))
5416 ;; Display registered back-end entries. When a key
5417 ;; appears for the second time, do not create another
5418 ;; entry, but append its sub-menu to existing menu.
5422 (let ((top-key (car entry)))
5424 (unless (eq top-key last-key)
5425 (setq last-key top-key)
5426 (format "\n[%s] %s\n"
5427 (funcall fontify-key (char-to-string top-key))
5429 (let ((sub-menu (nth 2 entry)))
5430 (unless (functionp sub-menu)
5431 ;; Split sub-menu into two columns.
5438 (if (zerop (mod index 2)) " [%s] %-26s"
5440 (funcall fontify-key
5441 (char-to-string (car sub-entry))
5445 (when (zerop (mod index 2)) "\n"))))))))
5447 ;; Publishing menu is hard-coded.
5448 (format "\n[%s] Publish
5449 [%s] Current file [%s] Current project
5450 [%s] Choose project [%s] All projects\n\n"
5451 (funcall fontify-key "P")
5452 (funcall fontify-key "f" ?P)
5453 (funcall fontify-key "p" ?P)
5454 (funcall fontify-key "x" ?P)
5455 (funcall fontify-key "a" ?P))
5456 (format "\[%s] Export stack\n" (funcall fontify-key "&" t))
5458 (funcall fontify-key "q" t)
5459 (if first-key "Main menu" "Exit")))))
5460 ;; Build prompts for both standard and expert UI.
5461 (standard-prompt (unless expertp "Export command: "))
5465 "Export command (C-%s%s%s%s%s) [%s]: "
5466 (if (memq 'body options) (funcall fontify-key "b" t) "b")
5467 (if (memq 'visible options) (funcall fontify-key "v" t) "v")
5468 (if (memq 'subtree options) (funcall fontify-key "s" t) "s")
5469 (if (memq 'force options) (funcall fontify-key "f" t) "f")
5470 (if (memq 'async options) (funcall fontify-key "a" t) "a")
5471 (mapconcat (lambda (k)
5472 ;; Strip control characters.
5473 (unless (< k 27) (char-to-string k)))
5474 allowed-keys "")))))
5475 ;; With expert UI, just read key with a fancy prompt. In standard
5476 ;; UI, display an intrusive help buffer.
5478 (org-export--dispatch-action
5479 expert-prompt allowed-keys backends options first-key expertp)
5480 ;; At first call, create frame layout in order to display menu.
5481 (unless (get-buffer "*Org Export Dispatcher*")
5482 (delete-other-windows)
5483 (org-switch-to-buffer-other-window
5484 (get-buffer-create "*Org Export Dispatcher*"))
5485 (setq cursor-type nil
5486 header-line-format "Use SPC, DEL, C-n or C-p to navigate.")
5487 ;; Prevent square brackets from being highlighted when point
5489 (modify-syntax-entry ?\[ "w"))
5490 ;; At this point, the buffer containing the menu exists and is
5491 ;; visible in the current window. So, refresh it.
5492 (with-current-buffer "*Org Export Dispatcher*"
5493 ;; Refresh help. Maintain display continuity by re-visiting
5494 ;; previous window position.
5495 (let ((pos (window-start)))
5498 (set-window-start nil pos)))
5499 (org-fit-window-to-buffer)
5500 (org-export--dispatch-action
5501 standard-prompt allowed-keys backends options first-key expertp))))
5503 (defun org-export--dispatch-action
5504 (prompt allowed-keys backends options first-key expertp)
5505 "Read a character from command input and act accordingly.
5507 PROMPT is the displayed prompt, as a string. ALLOWED-KEYS is
5508 a list of characters available at a given step in the process.
5509 BACKENDS is a list of menu entries. OPTIONS, FIRST-KEY and
5510 EXPERTP are the same as defined in `org-export--dispatch-ui',
5513 Toggle export options when required. Otherwise, return value is
5514 a list with action as CAR and a list of interactive export
5517 ;; Scrolling: when in non-expert mode, act on motion keys (C-n,
5519 (while (and (setq key (read-char-exclusive prompt))
5521 (memq key '(?\x0e ?\x10 ?\s ?\d)))
5523 (?\x0e (if (not (pos-visible-in-window-p (point-max)))
5524 (ignore-errors (scroll-up-line))
5525 (message "End of buffer")
5527 (?\x10 (if (not (pos-visible-in-window-p (point-min)))
5528 (ignore-errors (scroll-down-line))
5529 (message "Beginning of buffer")
5531 (?\s (if (not (pos-visible-in-window-p (point-max)))
5533 (message "End of buffer")
5535 (?\d (if (not (pos-visible-in-window-p (point-min)))
5537 (message "Beginning of buffer")
5540 ;; Ignore undefined associations.
5541 ((not (memq key allowed-keys))
5543 (unless expertp (message "Invalid key") (sit-for 1))
5544 (org-export--dispatch-ui options first-key expertp))
5545 ;; q key at first level aborts export. At second
5546 ;; level, cancel first key instead.
5547 ((eq key ?q) (if (not first-key) (error "Export aborted")
5548 (org-export--dispatch-ui options nil expertp)))
5549 ;; Help key: Switch back to standard interface if
5550 ;; expert UI was active.
5551 ((eq key ??) (org-export--dispatch-ui options first-key nil))
5552 ;; Switch to asynchronous export stack.
5553 ((eq key ?&) '(stack))
5554 ;; Toggle export options.
5555 ((memq key '(?\x02 ?\x16 ?\x13 ?\x06 ?\x01))
5556 (org-export--dispatch-ui
5557 (let ((option (case key (?\x02 'body) (?\x16 'visible) (?\x13 'subtree)
5558 (?\x06 'force) (?\x01 'async))))
5559 (if (memq option options) (remq option options)
5560 (cons option options)))
5562 ;; Action selected: Send key and options back to
5563 ;; `org-export-dispatch'.
5564 ((or first-key (functionp (nth 2 (assq key backends))))
5566 ((not first-key) (nth 2 (assq key backends)))
5567 ;; Publishing actions are hard-coded. Send a special
5568 ;; signal to `org-export-dispatch'.
5571 (?f 'publish-current-file)
5572 (?p 'publish-current-project)
5573 (?x 'publish-choose-project)
5575 ;; Return first action associated to FIRST-KEY + KEY
5576 ;; path. Indeed, derived backends can share the same
5579 (mapc (lambda (backend)
5580 (let ((match (assq key (nth 2 backend))))
5581 (when match (throw 'found (nth 2 match)))))
5582 (member (assq first-key backends) backends)))))
5584 ;; Otherwise, enter sub-menu.
5585 (t (org-export--dispatch-ui options key expertp)))))
5592 ;; generated-autoload-file: "org-loaddefs.el"