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