Bug fix for removal of comments during export.
[org-mode.git] / lisp / org-exp.el
blobdd9495ca4133f687ecf0aff974614ef7ff514500
1 ;;; org-exp.el --- ASCII, HTML, XOXO and iCalendar export for Org-mode
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
8 ;; Version: 6.04b
9 ;;
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
26 ;;; Commentary:
28 (require 'org)
29 (require 'org-agenda)
30 (eval-and-compile
31 (require 'cl))
33 (declare-function org-export-latex-preprocess "org-export-latex" ())
34 (declare-function org-agenda-skip "org-agenda" ())
35 (declare-function org-infojs-options-inbuffer-template "org-jsinfo" ())
36 (declare-function htmlize-region "ext:htmlize" (beg end))
37 (defvar htmlize-buffer-places) ; from htmlize.el
39 (defgroup org-export nil
40 "Options for exporting org-listings."
41 :tag "Org Export"
42 :group 'org)
44 (defgroup org-export-general nil
45 "General options for exporting Org-mode files."
46 :tag "Org Export General"
47 :group 'org-export)
49 ;; FIXME
50 (defvar org-export-publishing-directory nil)
52 (defcustom org-export-run-in-background nil
53 "Non-nil means export and publishing commands will run in background.
54 This works by starting up a separate Emacs process visiting the same file
55 and doing the export from there.
56 Not all export commands are affected by this - only the ones which
57 actually write to a file, and that do not depend on the buffer state.
59 If this option is nil, you can still get background export by calling
60 `org-export' with a double prefix arg: `C-u C-u C-c C-e'.
62 If this option is t, the double prefix can be used to exceptionally
63 force an export command into the current process."
64 :group 'org-export-general
65 :type 'boolean)
67 (defcustom org-export-with-special-strings t
68 "Non-nil means, interpret \"\-\", \"--\" and \"---\" for export.
69 When this option is turned on, these strings will be exported as:
71 Org HTML LaTeX
72 -----+----------+--------
73 \\- &shy; \\-
74 -- &ndash; --
75 --- &mdash; ---
76 ... &hellip; \ldots
78 This option can also be set with the +OPTIONS line, e.g. \"-:nil\"."
79 :group 'org-export-translation
80 :type 'boolean)
82 (defcustom org-export-language-setup
83 '(("en" "Author" "Date" "Table of Contents")
84 ("cs" "Autor" "Datum" "Obsah")
85 ("da" "Ophavsmand" "Dato" "Indhold")
86 ("de" "Autor" "Datum" "Inhaltsverzeichnis")
87 ("es" "Autor" "Fecha" "\xcdndice")
88 ("fr" "Auteur" "Date" "Table des mati\xe8res")
89 ("it" "Autore" "Data" "Indice")
90 ("nl" "Auteur" "Datum" "Inhoudsopgave")
91 ("no" "Forfatter" "Dato" "Innhold")
92 ("nb" "Forfatter" "Dato" "Innhold") ;; nb = Norsk (bokm.l)
93 ("nn" "Forfattar" "Dato" "Innhald") ;; nn = Norsk (nynorsk)
94 ("sv" "F\xf6rfattarens" "Datum" "Inneh\xe5ll"))
95 "Terms used in export text, translated to different languages.
96 Use the variable `org-export-default-language' to set the language,
97 or use the +OPTION lines for a per-file setting."
98 :group 'org-export-general
99 :type '(repeat
100 (list
101 (string :tag "HTML language tag")
102 (string :tag "Author")
103 (string :tag "Date")
104 (string :tag "Table of Contents"))))
106 (defcustom org-export-default-language "en"
107 "The default language of HTML export, as a string.
108 This should have an association in `org-export-language-setup'."
109 :group 'org-export-general
110 :type 'string)
112 (defcustom org-export-skip-text-before-1st-heading nil
113 "Non-nil means, skip all text before the first headline when exporting.
114 When nil, that text is exported as well."
115 :group 'org-export-general
116 :type 'boolean)
118 (defcustom org-export-headline-levels 3
119 "The last level which is still exported as a headline.
120 Inferior levels will produce itemize lists when exported.
121 Note that a numeric prefix argument to an exporter function overrides
122 this setting.
124 This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
125 :group 'org-export-general
126 :type 'number)
128 (defcustom org-export-with-section-numbers t
129 "Non-nil means, add section numbers to headlines when exporting.
131 This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
132 :group 'org-export-general
133 :type 'boolean)
135 (defcustom org-export-with-toc t
136 "Non-nil means, create a table of contents in exported files.
137 The TOC contains headlines with levels up to`org-export-headline-levels'.
138 When an integer, include levels up to N in the toc, this may then be
139 different from `org-export-headline-levels', but it will not be allowed
140 to be larger than the number of headline levels.
141 When nil, no table of contents is made.
143 Headlines which contain any TODO items will be marked with \"(*)\" in
144 ASCII export, and with red color in HTML output, if the option
145 `org-export-mark-todo-in-toc' is set.
147 In HTML output, the TOC will be clickable.
149 This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"
150 or \"toc:3\"."
151 :group 'org-export-general
152 :type '(choice
153 (const :tag "No Table of Contents" nil)
154 (const :tag "Full Table of Contents" t)
155 (integer :tag "TOC to level")))
157 (defcustom org-export-mark-todo-in-toc nil
158 "Non-nil means, mark TOC lines that contain any open TODO items."
159 :group 'org-export-general
160 :type 'boolean)
162 (defcustom org-export-preserve-breaks nil
163 "Non-nil means, preserve all line breaks when exporting.
164 Normally, in HTML output paragraphs will be reformatted. In ASCII
165 export, line breaks will always be preserved, regardless of this variable.
167 This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
168 :group 'org-export-general
169 :type 'boolean)
171 (defcustom org-export-with-archived-trees 'headline
172 "Whether subtrees with the ARCHIVE tag should be exported.
173 This can have three different values
174 nil Do not export, pretend this tree is not present
175 t Do export the entire tree
176 headline Only export the headline, but skip the tree below it."
177 :group 'org-export-general
178 :group 'org-archive
179 :type '(choice
180 (const :tag "not at all" nil)
181 (const :tag "headline only" 'headline)
182 (const :tag "entirely" t)))
184 (defcustom org-export-author-info t
185 "Non-nil means, insert author name and email into the exported file.
187 This option can also be set with the +OPTIONS line,
188 e.g. \"author-info:nil\"."
189 :group 'org-export-general
190 :type 'boolean)
192 (defcustom org-export-time-stamp-file t
193 "Non-nil means, insert a time stamp into the exported file.
194 The time stamp shows when the file was created.
196 This option can also be set with the +OPTIONS line,
197 e.g. \"timestamp:nil\"."
198 :group 'org-export-general
199 :type 'boolean)
201 (defcustom org-export-with-timestamps t
202 "If nil, do not export time stamps and associated keywords."
203 :group 'org-export-general
204 :type 'boolean)
206 (defcustom org-export-remove-timestamps-from-toc t
207 "If nil, remove timestamps from the table of contents entries."
208 :group 'org-export-general
209 :type 'boolean)
211 (defcustom org-export-with-tags 'not-in-toc
212 "If nil, do not export tags, just remove them from headlines.
213 If this is the symbol `not-in-toc', tags will be removed from table of
214 contents entries, but still be shown in the headlines of the document.
216 This option can also be set with the +OPTIONS line, e.g. \"tags:nil\"."
217 :group 'org-export-general
218 :type '(choice
219 (const :tag "Off" nil)
220 (const :tag "Not in TOC" not-in-toc)
221 (const :tag "On" t)))
223 (defcustom org-export-with-drawers nil
224 "Non-nil means, export with drawers like the property drawer.
225 When t, all drawers are exported. This may also be a list of
226 drawer names to export."
227 :group 'org-export-general
228 :type '(choice
229 (const :tag "All drawers" t)
230 (const :tag "None" nil)
231 (repeat :tag "Selected drawers"
232 (string :tag "Drawer name"))))
234 (defvar org-export-preprocess-hook nil
235 "Hook for preprocessing an export buffer.
236 Pretty much the first thing when exporting is running this hook.")
238 (defgroup org-export-translation nil
239 "Options for translating special ascii sequences for the export backends."
240 :tag "Org Export Translation"
241 :group 'org-export)
243 (defcustom org-export-with-emphasize t
244 "Non-nil means, interpret *word*, /word/, and _word_ as emphasized text.
245 If the export target supports emphasizing text, the word will be
246 typeset in bold, italic, or underlined, respectively. Works only for
247 single words, but you can say: I *really* *mean* *this*.
248 Not all export backends support this.
250 This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
251 :group 'org-export-translation
252 :type 'boolean)
254 (defcustom org-export-with-footnotes t
255 "If nil, export [1] as a footnote marker.
256 Lines starting with [1] will be formatted as footnotes.
258 This option can also be set with the +OPTIONS line, e.g. \"f:nil\"."
259 :group 'org-export-translation
260 :type 'boolean)
262 (defcustom org-export-with-sub-superscripts t
263 "Non-nil means, interpret \"_\" and \"^\" for export.
264 When this option is turned on, you can use TeX-like syntax for sub- and
265 superscripts. Several characters after \"_\" or \"^\" will be
266 considered as a single item - so grouping with {} is normally not
267 needed. For example, the following things will be parsed as single
268 sub- or superscripts.
270 10^24 or 10^tau several digits will be considered 1 item.
271 10^-12 or 10^-tau a leading sign with digits or a word
272 x^2-y^3 will be read as x^2 - y^3, because items are
273 terminated by almost any nonword/nondigit char.
274 x_{i^2} or x^(2-i) braces or parenthesis do grouping.
276 Still, ambiguity is possible - so when in doubt use {} to enclose the
277 sub/superscript. If you set this variable to the symbol `{}',
278 the braces are *required* in order to trigger interpretations as
279 sub/superscript. This can be helpful in documents that need \"_\"
280 frequently in plain text.
282 Not all export backends support this, but HTML does.
284 This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
285 :group 'org-export-translation
286 :type '(choice
287 (const :tag "Always interpret" t)
288 (const :tag "Only with braces" {})
289 (const :tag "Never interpret" nil)))
291 (defcustom org-export-with-special-strings t
292 "Non-nil means, interpret \"\-\", \"--\" and \"---\" for export.
293 When this option is turned on, these strings will be exported as:
295 \\- : &shy;
296 -- : &ndash;
297 --- : &mdash;
299 Not all export backends support this, but HTML does.
301 This option can also be set with the +OPTIONS line, e.g. \"-:nil\"."
302 :group 'org-export-translation
303 :type 'boolean)
305 (defcustom org-export-with-TeX-macros t
306 "Non-nil means, interpret simple TeX-like macros when exporting.
307 For example, HTML export converts \\alpha to &alpha; and \\AA to &Aring;.
308 No only real TeX macros will work here, but the standard HTML entities
309 for math can be used as macro names as well. For a list of supported
310 names in HTML export, see the constant `org-html-entities'.
311 Not all export backends support this.
313 This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
314 :group 'org-export-translation
315 :group 'org-export-latex
316 :type 'boolean)
318 (defcustom org-export-with-LaTeX-fragments nil
319 "Non-nil means, convert LaTeX fragments to images when exporting to HTML.
320 When set, the exporter will find LaTeX environments if the \\begin line is
321 the first non-white thing on a line. It will also find the math delimiters
322 like $a=b$ and \\( a=b \\) for inline math, $$a=b$$ and \\[ a=b \\] for
323 display math.
325 This option can also be set with the +OPTIONS line, e.g. \"LaTeX:t\"."
326 :group 'org-export-translation
327 :group 'org-export-latex
328 :type 'boolean)
330 (defcustom org-export-with-fixed-width t
331 "Non-nil means, lines starting with \":\" will be in fixed width font.
332 This can be used to have pre-formatted text, fragments of code etc. For
333 example:
334 : ;; Some Lisp examples
335 : (while (defc cnt)
336 : (ding))
337 will be looking just like this in also HTML. See also the QUOTE keyword.
338 Not all export backends support this.
340 This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
341 :group 'org-export-translation
342 :type 'boolean)
344 (defcustom org-match-sexp-depth 3
345 "Number of stacked braces for sub/superscript matching.
346 This has to be set before loading org.el to be effective."
347 :group 'org-export-translation
348 :type 'integer)
350 (defgroup org-export-tables nil
351 "Options for exporting tables in Org-mode."
352 :tag "Org Export Tables"
353 :group 'org-export)
355 (defcustom org-export-with-tables t
356 "If non-nil, lines starting with \"|\" define a table.
357 For example:
359 | Name | Address | Birthday |
360 |-------------+----------+-----------|
361 | Arthur Dent | England | 29.2.2100 |
363 Not all export backends support this.
365 This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
366 :group 'org-export-tables
367 :type 'boolean)
369 (defcustom org-export-highlight-first-table-line t
370 "Non-nil means, highlight the first table line.
371 In HTML export, this means use <th> instead of <td>.
372 In tables created with table.el, this applies to the first table line.
373 In Org-mode tables, all lines before the first horizontal separator
374 line will be formatted with <th> tags."
375 :group 'org-export-tables
376 :type 'boolean)
378 (defcustom org-export-table-remove-special-lines t
379 "Remove special lines and marking characters in calculating tables.
380 This removes the special marking character column from tables that are set
381 up for spreadsheet calculations. It also removes the entire lines
382 marked with `!', `_', or `^'. The lines with `$' are kept, because
383 the values of constants may be useful to have."
384 :group 'org-export-tables
385 :type 'boolean)
387 (defcustom org-export-prefer-native-exporter-for-tables nil
388 "Non-nil means, always export tables created with table.el natively.
389 Natively means, use the HTML code generator in table.el.
390 When nil, Org-mode's own HTML generator is used when possible (i.e. if
391 the table does not use row- or column-spanning). This has the
392 advantage, that the automatic HTML conversions for math symbols and
393 sub/superscripts can be applied. Org-mode's HTML generator is also
394 much faster."
395 :group 'org-export-tables
396 :type 'boolean)
398 (defgroup org-export-ascii nil
399 "Options specific for ASCII export of Org-mode files."
400 :tag "Org Export ASCII"
401 :group 'org-export)
403 (defcustom org-export-ascii-underline '(?\$ ?\# ?^ ?\~ ?\= ?\-)
404 "Characters for underlining headings in ASCII export.
405 In the given sequence, these characters will be used for level 1, 2, ..."
406 :group 'org-export-ascii
407 :type '(repeat character))
409 (defcustom org-export-ascii-bullets '(?* ?+ ?-)
410 "Bullet characters for headlines converted to lists in ASCII export.
411 The first character is used for the first lest level generated in this
412 way, and so on. If there are more levels than characters given here,
413 the list will be repeated.
414 Note that plain lists will keep the same bullets as the have in the
415 Org-mode file."
416 :group 'org-export-ascii
417 :type '(repeat character))
419 (defgroup org-export-xml nil
420 "Options specific for XML export of Org-mode files."
421 :tag "Org Export XML"
422 :group 'org-export)
424 (defgroup org-export-html nil
425 "Options specific for HTML export of Org-mode files."
426 :tag "Org Export HTML"
427 :group 'org-export)
429 (defcustom org-export-html-coding-system nil
430 "Coding system for HTML export, defaults to buffer-file-coding-system."
431 :group 'org-export-html
432 :type 'coding-system)
434 (defcustom org-export-html-extension "html"
435 "The extension for exported HTML files."
436 :group 'org-export-html
437 :type 'string)
439 (defcustom org-export-html-link-up ""
440 "Where should the \"UP\" link of exported HTML pages lead?"
441 :group 'org-export-html
442 :type '(string :tag "File or URL"))
444 (defcustom org-export-html-link-home ""
445 "Where should the \"HOME\" link of exported HTML pages lead?"
446 :group 'org-export-html
447 :type '(string :tag "File or URL"))
449 (defcustom org-export-html-style
450 "<style type=\"text/css\">
451 html {
452 font-family: Times, serif;
453 font-size: 12pt;
455 .title { text-align: center; }
456 .todo { color: red; }
457 .done { color: green; }
458 .timestamp { color: grey }
459 .timestamp-kwd { color: CadetBlue }
460 .tag { background-color:lightblue; font-weight:normal }
461 .target { }
462 pre {
463 border: 1pt solid #AEBDCC;
464 background-color: #F3F5F7;
465 padding: 5pt;
466 font-family: courier, monospace;
468 table { border-collapse: collapse; }
469 td, th {
470 vertical-align: top;
471 <!--border: 1pt solid #ADB9CC;-->
473 dt { font-weight: bold; }
474 </style>"
475 "The default style specification for exported HTML files.
476 Since there are different ways of setting style information, this variable
477 needs to contain the full HTML structure to provide a style, including the
478 surrounding HTML tags. The style specifications should include definitions
479 for new classes todo, done, title, and deadline. For example, valid values
480 would be:
482 <style type=\"text/css\">
483 p { font-weight: normal; color: gray; }
484 h1 { color: black; }
485 .title { text-align: center; }
486 .todo, .deadline { color: red; }
487 .done { color: green; }
488 </style>
490 or, if you want to keep the style in a file,
492 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
494 As the value of this option simply gets inserted into the HTML <head> header,
495 you can \"misuse\" it to add arbitrary text to the header."
496 :group 'org-export-html
497 :type 'string)
500 (defcustom org-export-html-title-format "<h1 class=\"title\">%s</h1>\n"
501 "Format for typesetting the document title in HTML export."
502 :group 'org-export-html
503 :type 'string)
505 (defcustom org-export-html-toplevel-hlevel 2
506 "The <H> level for level 1 headings in HTML export."
507 :group 'org-export-html
508 :type 'string)
510 (defcustom org-export-html-link-org-files-as-html t
511 "Non-nil means, make file links to `file.org' point to `file.html'.
512 When org-mode is exporting an org-mode file to HTML, links to
513 non-html files are directly put into a href tag in HTML.
514 However, links to other Org-mode files (recognized by the
515 extension `.org.) should become links to the corresponding html
516 file, assuming that the linked org-mode file will also be
517 converted to HTML.
518 When nil, the links still point to the plain `.org' file."
519 :group 'org-export-html
520 :type 'boolean)
522 (defcustom org-export-html-inline-images 'maybe
523 "Non-nil means, inline images into exported HTML pages.
524 This is done using an <img> tag. When nil, an anchor with href is used to
525 link to the image. If this option is `maybe', then images in links with
526 an empty description will be inlined, while images with a description will
527 be linked only."
528 :group 'org-export-html
529 :type '(choice (const :tag "Never" nil)
530 (const :tag "Always" t)
531 (const :tag "When there is no description" maybe)))
533 ;; FIXME: rename
534 (defcustom org-export-html-expand t
535 "Non-nil means, for HTML export, treat @<...> as HTML tag.
536 When nil, these tags will be exported as plain text and therefore
537 not be interpreted by a browser.
539 This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
540 :group 'org-export-html
541 :type 'boolean)
543 (defcustom org-export-html-table-tag
544 "<table border=\"2\" cellspacing=\"0\" cellpadding=\"6\" rules=\"groups\" frame=\"hsides\">"
545 "The HTML tag that is used to start a table.
546 This must be a <table> tag, but you may change the options like
547 borders and spacing."
548 :group 'org-export-html
549 :type 'string)
551 (defcustom org-export-table-header-tags '("<th>" . "</th>")
552 "The opening tag for table header fields.
553 This is customizable so that alignment options can be specified."
554 :group 'org-export-tables
555 :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
557 (defcustom org-export-table-data-tags '("<td>" . "</td>")
558 "The opening tag for table data fields.
559 This is customizable so that alignment options can be specified."
560 :group 'org-export-tables
561 :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
563 (defcustom org-export-html-with-timestamp nil
564 "If non-nil, write `org-export-html-html-helper-timestamp'
565 into the exported HTML text. Otherwise, the buffer will just be saved
566 to a file."
567 :group 'org-export-html
568 :type 'boolean)
570 (defcustom org-export-html-html-helper-timestamp
571 "<br/><br/><hr><p><!-- hhmts start --> <!-- hhmts end --></p>\n"
572 "The HTML tag used as timestamp delimiter for HTML-helper-mode."
573 :group 'org-export-html
574 :type 'string)
576 (defgroup org-export-htmlize nil
577 "Options for processing examples with htmlize.el."
578 :tag "Org Export Htmlize"
579 :group 'org-export-html)
581 (defcustom org-export-htmlize-output-type 'inline-css
582 "Output type to be used by htmlize when formatting code snippets.
583 Normally this is `inline-css', but if you have defined to appropriate
584 classes in your css style file, setting this to `css' means that the
585 fontification will use the class names.
586 See also the function `org-export-htmlize-generate-css'."
587 :group 'org-export-htmlize
588 :type '(choice (const css) (const inline-css)))
590 (defcustom org-export-htmlize-css-font-prefix "org-"
591 "The prefix for CSS class names for htmlize font specifications."
592 :group 'org-export-htmlize
593 :type 'string)
595 (defgroup org-export-icalendar nil
596 "Options specific for iCalendar export of Org-mode files."
597 :tag "Org Export iCalendar"
598 :group 'org-export)
600 (defcustom org-combined-agenda-icalendar-file "~/org.ics"
601 "The file name for the iCalendar file covering all agenda files.
602 This file is created with the command \\[org-export-icalendar-all-agenda-files].
603 The file name should be absolute, the file will be overwritten without warning."
604 :group 'org-export-icalendar
605 :type 'file)
607 (defcustom org-icalendar-include-todo nil
608 "Non-nil means, export to iCalendar files should also cover TODO items."
609 :group 'org-export-icalendar
610 :type '(choice
611 (const :tag "None" nil)
612 (const :tag "Unfinished" t)
613 (const :tag "All" all)))
615 (defcustom org-icalendar-include-sexps t
616 "Non-nil means, export to iCalendar files should also cover sexp entries.
617 These are entries like in the diary, but directly in an Org-mode file."
618 :group 'org-export-icalendar
619 :type 'boolean)
621 (defcustom org-icalendar-include-body 100
622 "Amount of text below headline to be included in iCalendar export.
623 This is a number of characters that should maximally be included.
624 Properties, scheduling and clocking lines will always be removed.
625 The text will be inserted into the DESCRIPTION field."
626 :group 'org-export-icalendar
627 :type '(choice
628 (const :tag "Nothing" nil)
629 (const :tag "Everything" t)
630 (integer :tag "Max characters")))
632 (defcustom org-icalendar-combined-name "OrgMode"
633 "Calendar name for the combined iCalendar representing all agenda files."
634 :group 'org-export-icalendar
635 :type 'string)
637 (defcustom org-icalendar-store-UID nil
638 "Non-nil means, store any created UIDs in properties.
639 The iCalendar standard requires that all entries have a unique identifyer.
640 Org will create these identifiers as needed. When this variable is non-nil,
641 the created UIDs will be stored in the ID property of the entry. Then the
642 next time this entry is exported, it will be exported with the same UID,
643 superceeding the previous form of it. This is essential for
644 synchronization services.
645 This variable is not turned on by default because we want to avoid creating
646 a property drawer in every entry if people are only playing with this feature,
647 or if they are only using it locally."
648 :group 'org-export-icalendar
649 :type 'boolean)
651 ;;;; Exporting
653 ;;; Variables, constants, and parameter plists
655 (defconst org-level-max 20)
657 (defvar org-export-html-preamble nil
658 "Preamble, to be inserted just after <body>. Set by publishing functions.")
659 (defvar org-export-html-postamble nil
660 "Preamble, to be inserted just before </body>. Set by publishing functions.")
661 (defvar org-export-html-auto-preamble t
662 "Should default preamble be inserted? Set by publishing functions.")
663 (defvar org-export-html-auto-postamble t
664 "Should default postamble be inserted? Set by publishing functions.")
665 (defvar org-current-export-file nil) ; dynamically scoped parameter
666 (defvar org-current-export-dir nil) ; dynamically scoped parameter
668 (defconst org-export-plist-vars
669 '((:link-up . org-export-html-link-up)
670 (:link-home . org-export-html-link-home)
671 (:language . org-export-default-language)
672 (:customtime . org-display-custom-times)
673 (:headline-levels . org-export-headline-levels)
674 (:section-numbers . org-export-with-section-numbers)
675 (:table-of-contents . org-export-with-toc)
676 (:preserve-breaks . org-export-preserve-breaks)
677 (:archived-trees . org-export-with-archived-trees)
678 (:emphasize . org-export-with-emphasize)
679 (:sub-superscript . org-export-with-sub-superscripts)
680 (:special-strings . org-export-with-special-strings)
681 (:footnotes . org-export-with-footnotes)
682 (:drawers . org-export-with-drawers)
683 (:tags . org-export-with-tags)
684 (:TeX-macros . org-export-with-TeX-macros)
685 (:LaTeX-fragments . org-export-with-LaTeX-fragments)
686 (:skip-before-1st-heading . org-export-skip-text-before-1st-heading)
687 (:fixed-width . org-export-with-fixed-width)
688 (:timestamps . org-export-with-timestamps)
689 (:author-info . org-export-author-info)
690 (:time-stamp-file . org-export-time-stamp-file)
691 (:tables . org-export-with-tables)
692 (:table-auto-headline . org-export-highlight-first-table-line)
693 (:style . org-export-html-style)
694 (:agenda-style . org-agenda-export-html-style)
695 (:convert-org-links . org-export-html-link-org-files-as-html)
696 (:inline-images . org-export-html-inline-images)
697 (:html-extension . org-export-html-extension)
698 (:html-table-tag . org-export-html-table-tag)
699 (:expand-quoted-html . org-export-html-expand)
700 (:timestamp . org-export-html-with-timestamp)
701 (:publishing-directory . org-export-publishing-directory)
702 (:preamble . org-export-html-preamble)
703 (:postamble . org-export-html-postamble)
704 (:auto-preamble . org-export-html-auto-preamble)
705 (:auto-postamble . org-export-html-auto-postamble)
706 (:author . user-full-name)
707 (:email . user-mail-address)))
709 (defun org-default-export-plist ()
710 "Return the property list with default settings for the export variables."
711 (let ((l org-export-plist-vars) rtn e)
712 (while (setq e (pop l))
713 (setq rtn (cons (car e) (cons (symbol-value (cdr e)) rtn))))
714 rtn))
716 (defvar org-export-inbuffer-options-extra nil
717 "List of additional in-buffer options that should be detected.
718 Just before export, the buffer is scanned for options like #+TITLE, #+EMAIL,
719 etc. Extensions can add to this list to get their options detected, and they
720 can then add a function to `org-export-options-filters' to process these
721 options.
722 Each element in this list must be a list, with the in-buffer keyword as car,
723 and a property (a symbol) as the next element. All occurences of the
724 keyword will be found, the values concatenated with a space character
725 in between, and the result stored in the export options property list.")
727 (defvar org-export-options-filters nil
728 "Functions to be called to finalize the export/publishing options.
729 All these options are stored in a property list, and each of the functions
730 in this hook gets a chance to modify this property list. Each function
731 must accept the property list as an argument, and must return the (possibly
732 modified) list.")
734 (defun org-infile-export-plist ()
735 "Return the property list with file-local settings for export."
736 (save-excursion
737 (save-restriction
738 (widen)
739 (goto-char (point-min))
740 (let ((re (org-make-options-regexp
741 (append
742 '("TITLE" "AUTHOR" "DATE" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE"
743 "LINK_UP" "LINK_HOME" "SETUPFILE")
744 (mapcar 'car org-export-inbuffer-options-extra))))
745 p key val text options js-up js-main js-css js-opt a pr
746 ext-setup-or-nil setup-contents (start 0))
747 (while (or (and ext-setup-or-nil
748 (string-match re ext-setup-or-nil start)
749 (setq start (match-end 0)))
750 (and (setq ext-setup-or-nil nil start 0)
751 (re-search-forward re nil t)))
752 (setq key (upcase (org-match-string-no-properties 1 ext-setup-or-nil))
753 val (org-match-string-no-properties 2 ext-setup-or-nil))
754 (cond
755 ((setq a (assoc key org-export-inbuffer-options-extra))
756 (setq pr (nth 1 a))
757 (setq p (plist-put p pr (concat (plist-get p pr) " " val))))
758 ((string-equal key "TITLE") (setq p (plist-put p :title val)))
759 ((string-equal key "AUTHOR")(setq p (plist-put p :author val)))
760 ((string-equal key "EMAIL") (setq p (plist-put p :email val)))
761 ((string-equal key "DATE") (setq p (plist-put p :date val)))
762 ((string-equal key "LANGUAGE") (setq p (plist-put p :language val)))
763 ((string-equal key "TEXT")
764 (setq text (if text (concat text "\n" val) val)))
765 ((string-equal key "OPTIONS")
766 (setq options (concat val " " options)))
767 ((string-equal key "LINK_UP")
768 (setq p (plist-put p :link-up val)))
769 ((string-equal key "LINK_HOME")
770 (setq p (plist-put p :link-home val)))
771 ((equal key "SETUPFILE")
772 (setq setup-contents (org-file-contents
773 (expand-file-name
774 (org-remove-double-quotes
775 (org-trim val)))
776 'noerror))
777 (if (not ext-setup-or-nil)
778 (setq ext-setup-or-nil setup-contents start 0)
779 (setq ext-setup-or-nil
780 (concat (substring ext-setup-or-nil 0 start)
781 "\n" setup-contents "\n"
782 (substring ext-setup-or-nil start)))))))
783 (setq p (plist-put p :text text))
784 (when options
785 (setq p (org-export-add-options-to-plist p options)))
786 p))))
788 (defun org-export-add-options-to-plist (p options)
789 "Parse an OPTONS line and set values in the property list P."
790 (let (o)
791 (when options
792 (let ((op '(("H" . :headline-levels)
793 ("num" . :section-numbers)
794 ("toc" . :table-of-contents)
795 ("\\n" . :preserve-breaks)
796 ("@" . :expand-quoted-html)
797 (":" . :fixed-width)
798 ("|" . :tables)
799 ("^" . :sub-superscript)
800 ("-" . :special-strings)
801 ("f" . :footnotes)
802 ("d" . :drawers)
803 ("tags" . :tags)
804 ("*" . :emphasize)
805 ("TeX" . :TeX-macros)
806 ("LaTeX" . :LaTeX-fragments)
807 ("skip" . :skip-before-1st-heading)
808 ("author" . :author-info)
809 ("timestamp" . :time-stamp-file)))
811 (while (setq o (pop op))
812 (if (string-match (concat (regexp-quote (car o))
813 ":\\([^ \t\n\r;,.]*\\)")
814 options)
815 (setq p (plist-put p (cdr o)
816 (car (read-from-string
817 (match-string 1 options))))))))))
820 (defun org-export-add-subtree-options (p pos)
821 "Add options in subtree at position POS to property list P."
822 (save-excursion
823 (goto-char pos)
824 (when (org-at-heading-p)
825 (let (a)
826 ;; This is actually read in `org-export-get-title-from-subtree'
827 ;; (when (setq a (org-entry-get pos "EXPORT_TITLE"))
828 ;; (setq p (plist-put p :title a)))
829 (when (setq a (org-entry-get pos "EXPORT_TEXT"))
830 (setq p (plist-put p :text a)))
831 (when (setq a (org-entry-get pos "EXPORT_OPTIONS"))
832 (setq p (org-export-add-options-to-plist p a)))))
835 (defun org-export-directory (type plist)
836 (let* ((val (plist-get plist :publishing-directory))
837 (dir (if (listp val)
838 (or (cdr (assoc type val)) ".")
839 val)))
840 dir))
842 (defun org-export-process-option-filters (plist)
843 (let ((functions org-export-options-filters) f)
844 (while (setq f (pop functions))
845 (setq plist (funcall f plist))))
846 plist)
848 ;;;###autoload
849 (defun org-export (&optional arg)
850 "Export dispatcher for Org-mode.
851 When `org-export-run-in-background' is non-nil, try to run the command
852 in the background. This will be done only for commands that write
853 to a file. For details see the docstring of `org-export-run-in-background'.
855 The prefix argument ARG will be passed to the exporter. However, if
856 ARG is a double universal prefix `C-u C-u', that means to inverse the
857 value of `org-export-run-in-background'."
858 (interactive "P")
859 (let* ((bg (org-xor (equal arg '(16)) org-export-run-in-background))
860 (help "[t] insert the export option template
861 \[v] limit export to visible part of outline tree
863 \[a] export as ASCII
865 \[h] export as HTML
866 \[H] export as HTML to temporary buffer
867 \[R] export region as HTML
868 \[b] export as HTML and browse immediately
869 \[x] export as XOXO
871 \[l] export as LaTeX
872 \[L] export as LaTeX to temporary buffer
874 \[i] export current file as iCalendar file
875 \[I] export all agenda files as iCalendar files
876 \[c] export agenda files into combined iCalendar file
878 \[F] publish current file
879 \[P] publish current project
880 \[X] publish... (project will be prompted for)
881 \[A] publish all projects")
882 (cmds
883 '((?t org-insert-export-options-template nil)
884 (?v org-export-visible nil)
885 (?a org-export-as-ascii t)
886 (?h org-export-as-html t)
887 (?b org-export-as-html-and-open t)
888 (?H org-export-as-html-to-buffer nil)
889 (?R org-export-region-as-html nil)
890 (?x org-export-as-xoxo t)
891 (?l org-export-as-latex t)
892 (?L org-export-as-latex-to-buffer nil)
893 (?i org-export-icalendar-this-file t)
894 (?I org-export-icalendar-all-agenda-files t)
895 (?c org-export-icalendar-combine-agenda-files t)
896 (?F org-publish-current-file t)
897 (?P org-publish-current-project t)
898 (?X org-publish t)
899 (?A org-publish-all t)))
900 r1 r2 ass)
901 (save-window-excursion
902 (delete-other-windows)
903 (with-output-to-temp-buffer "*Org Export/Publishing Help*"
904 (princ help))
905 (message "Select command: ")
906 (setq r1 (read-char-exclusive)))
907 (setq r2 (if (< r1 27) (+ r1 96) r1))
908 (unless (setq ass (assq r2 cmds))
909 (error "No command associated with key %c" r1))
910 (if (and bg (nth 2 ass))
911 ;; execute in background
912 (let ((p (start-process
913 (concat "Exporting " (file-name-nondirectory (buffer-file-name)))
914 "*Org Processes*"
915 (expand-file-name invocation-name invocation-directory)
916 "-batch"
917 "-l" user-init-file
918 "--eval" "(require 'org-exp)"
919 "--eval" "(setq org-wait .2)"
920 (buffer-file-name)
921 "-f" (symbol-name (nth 1 ass)))))
922 (set-process-sentinel p 'org-export-process-sentinel)
923 (message "Background process \"%s\": started" p))
924 ;; background processing not requested, or not possible
925 (call-interactively (nth 1 ass)))))
927 (defun org-export-process-sentinel (process status)
928 (if (string-match "\n+\\'" status)
929 (setq status (substring status 0 -1)))
930 (message "Background process \"%s\": %s" process status))
932 (defconst org-html-entities
933 '(("nbsp")
934 ("iexcl")
935 ("cent")
936 ("pound")
937 ("curren")
938 ("yen")
939 ("brvbar")
940 ("vert" . "&#124;")
941 ("sect")
942 ("uml")
943 ("copy")
944 ("ordf")
945 ("laquo")
946 ("not")
947 ("shy")
948 ("reg")
949 ("macr")
950 ("deg")
951 ("plusmn")
952 ("sup2")
953 ("sup3")
954 ("acute")
955 ("micro")
956 ("para")
957 ("middot")
958 ("odot"."o")
959 ("star"."*")
960 ("cedil")
961 ("sup1")
962 ("ordm")
963 ("raquo")
964 ("frac14")
965 ("frac12")
966 ("frac34")
967 ("iquest")
968 ("Agrave")
969 ("Aacute")
970 ("Acirc")
971 ("Atilde")
972 ("Auml")
973 ("Aring") ("AA"."&Aring;")
974 ("AElig")
975 ("Ccedil")
976 ("Egrave")
977 ("Eacute")
978 ("Ecirc")
979 ("Euml")
980 ("Igrave")
981 ("Iacute")
982 ("Icirc")
983 ("Iuml")
984 ("ETH")
985 ("Ntilde")
986 ("Ograve")
987 ("Oacute")
988 ("Ocirc")
989 ("Otilde")
990 ("Ouml")
991 ("times")
992 ("Oslash")
993 ("Ugrave")
994 ("Uacute")
995 ("Ucirc")
996 ("Uuml")
997 ("Yacute")
998 ("THORN")
999 ("szlig")
1000 ("agrave")
1001 ("aacute")
1002 ("acirc")
1003 ("atilde")
1004 ("auml")
1005 ("aring")
1006 ("aelig")
1007 ("ccedil")
1008 ("egrave")
1009 ("eacute")
1010 ("ecirc")
1011 ("euml")
1012 ("igrave")
1013 ("iacute")
1014 ("icirc")
1015 ("iuml")
1016 ("eth")
1017 ("ntilde")
1018 ("ograve")
1019 ("oacute")
1020 ("ocirc")
1021 ("otilde")
1022 ("ouml")
1023 ("divide")
1024 ("oslash")
1025 ("ugrave")
1026 ("uacute")
1027 ("ucirc")
1028 ("uuml")
1029 ("yacute")
1030 ("thorn")
1031 ("yuml")
1032 ("fnof")
1033 ("Alpha")
1034 ("Beta")
1035 ("Gamma")
1036 ("Delta")
1037 ("Epsilon")
1038 ("Zeta")
1039 ("Eta")
1040 ("Theta")
1041 ("Iota")
1042 ("Kappa")
1043 ("Lambda")
1044 ("Mu")
1045 ("Nu")
1046 ("Xi")
1047 ("Omicron")
1048 ("Pi")
1049 ("Rho")
1050 ("Sigma")
1051 ("Tau")
1052 ("Upsilon")
1053 ("Phi")
1054 ("Chi")
1055 ("Psi")
1056 ("Omega")
1057 ("alpha")
1058 ("beta")
1059 ("gamma")
1060 ("delta")
1061 ("epsilon")
1062 ("varepsilon"."&epsilon;")
1063 ("zeta")
1064 ("eta")
1065 ("theta")
1066 ("iota")
1067 ("kappa")
1068 ("lambda")
1069 ("mu")
1070 ("nu")
1071 ("xi")
1072 ("omicron")
1073 ("pi")
1074 ("rho")
1075 ("sigmaf") ("varsigma"."&sigmaf;")
1076 ("sigma")
1077 ("tau")
1078 ("upsilon")
1079 ("phi")
1080 ("chi")
1081 ("psi")
1082 ("omega")
1083 ("thetasym") ("vartheta"."&thetasym;")
1084 ("upsih")
1085 ("piv")
1086 ("bull") ("bullet"."&bull;")
1087 ("hellip") ("dots"."&hellip;")
1088 ("prime")
1089 ("Prime")
1090 ("oline")
1091 ("frasl")
1092 ("weierp")
1093 ("image")
1094 ("real")
1095 ("trade")
1096 ("alefsym")
1097 ("larr") ("leftarrow"."&larr;") ("gets"."&larr;")
1098 ("uarr") ("uparrow"."&uarr;")
1099 ("rarr") ("to"."&rarr;") ("rightarrow"."&rarr;")
1100 ("darr")("downarrow"."&darr;")
1101 ("harr") ("leftrightarrow"."&harr;")
1102 ("crarr") ("hookleftarrow"."&crarr;") ; has round hook, not quite CR
1103 ("lArr") ("Leftarrow"."&lArr;")
1104 ("uArr") ("Uparrow"."&uArr;")
1105 ("rArr") ("Rightarrow"."&rArr;")
1106 ("dArr") ("Downarrow"."&dArr;")
1107 ("hArr") ("Leftrightarrow"."&hArr;")
1108 ("forall")
1109 ("part") ("partial"."&part;")
1110 ("exist") ("exists"."&exist;")
1111 ("empty") ("emptyset"."&empty;")
1112 ("nabla")
1113 ("isin") ("in"."&isin;")
1114 ("notin")
1115 ("ni")
1116 ("prod")
1117 ("sum")
1118 ("minus")
1119 ("lowast") ("ast"."&lowast;")
1120 ("radic")
1121 ("prop") ("proptp"."&prop;")
1122 ("infin") ("infty"."&infin;")
1123 ("ang") ("angle"."&ang;")
1124 ("and") ("wedge"."&and;")
1125 ("or") ("vee"."&or;")
1126 ("cap")
1127 ("cup")
1128 ("int")
1129 ("there4")
1130 ("sim")
1131 ("cong") ("simeq"."&cong;")
1132 ("asymp")("approx"."&asymp;")
1133 ("ne") ("neq"."&ne;")
1134 ("equiv")
1135 ("le")
1136 ("ge")
1137 ("sub") ("subset"."&sub;")
1138 ("sup") ("supset"."&sup;")
1139 ("nsub")
1140 ("sube")
1141 ("supe")
1142 ("oplus")
1143 ("otimes")
1144 ("perp")
1145 ("sdot") ("cdot"."&sdot;")
1146 ("lceil")
1147 ("rceil")
1148 ("lfloor")
1149 ("rfloor")
1150 ("lang")
1151 ("rang")
1152 ("loz") ("Diamond"."&loz;")
1153 ("spades") ("spadesuit"."&spades;")
1154 ("clubs") ("clubsuit"."&clubs;")
1155 ("hearts") ("diamondsuit"."&hearts;")
1156 ("diams") ("diamondsuit"."&diams;")
1157 ("smile"."&#9786;") ("blacksmile"."&#9787;") ("sad"."&#9785;")
1158 ("quot")
1159 ("amp")
1160 ("lt")
1161 ("gt")
1162 ("OElig")
1163 ("oelig")
1164 ("Scaron")
1165 ("scaron")
1166 ("Yuml")
1167 ("circ")
1168 ("tilde")
1169 ("ensp")
1170 ("emsp")
1171 ("thinsp")
1172 ("zwnj")
1173 ("zwj")
1174 ("lrm")
1175 ("rlm")
1176 ("ndash")
1177 ("mdash")
1178 ("lsquo")
1179 ("rsquo")
1180 ("sbquo")
1181 ("ldquo")
1182 ("rdquo")
1183 ("bdquo")
1184 ("dagger")
1185 ("Dagger")
1186 ("permil")
1187 ("lsaquo")
1188 ("rsaquo")
1189 ("euro")
1191 ("arccos"."arccos")
1192 ("arcsin"."arcsin")
1193 ("arctan"."arctan")
1194 ("arg"."arg")
1195 ("cos"."cos")
1196 ("cosh"."cosh")
1197 ("cot"."cot")
1198 ("coth"."coth")
1199 ("csc"."csc")
1200 ("deg"."deg")
1201 ("det"."det")
1202 ("dim"."dim")
1203 ("exp"."exp")
1204 ("gcd"."gcd")
1205 ("hom"."hom")
1206 ("inf"."inf")
1207 ("ker"."ker")
1208 ("lg"."lg")
1209 ("lim"."lim")
1210 ("liminf"."liminf")
1211 ("limsup"."limsup")
1212 ("ln"."ln")
1213 ("log"."log")
1214 ("max"."max")
1215 ("min"."min")
1216 ("Pr"."Pr")
1217 ("sec"."sec")
1218 ("sin"."sin")
1219 ("sinh"."sinh")
1220 ("sup"."sup")
1221 ("tan"."tan")
1222 ("tanh"."tanh")
1224 "Entities for TeX->HTML translation.
1225 Entries can be like (\"ent\"), in which case \"\\ent\" will be translated to
1226 \"&ent;\". An entry can also be a dotted pair like (\"ent\".\"&other;\").
1227 In that case, \"\\ent\" will be translated to \"&other;\".
1228 The list contains HTML entities for Latin-1, Greek and other symbols.
1229 It is supplemented by a number of commonly used TeX macros with appropriate
1230 translations. There is currently no way for users to extend this.")
1232 ;;; General functions for all backends
1234 (defun org-export-preprocess-string (string &rest parameters)
1235 "Cleanup STRING so that that the true exported has a more consistent source.
1236 This function takes STRING, which should be a buffer-string of an org-file
1237 to export. It then creates a temporary buffer where it does its job.
1238 The result is then again returned as a string, and the exporter works
1239 on this string to produce the exported version."
1240 (interactive)
1241 (let* ((re-radio (and org-target-link-regexp
1242 (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)")))
1243 (re-plain-link (concat "\\([^[<]\\)" org-plain-link-re))
1244 (re-angle-link (concat "\\([^[]\\)" org-angle-link-re))
1245 (re-archive (concat ":" org-archive-tag ":"))
1246 (re-quote (concat "^\\*+[ \t]+" org-quote-string "\\>"))
1247 (re-commented (concat "^\\*+[ \t]+" org-comment-string "\\>"))
1248 (htmlp (plist-get parameters :for-html))
1249 (asciip (plist-get parameters :for-ascii))
1250 (latexp (plist-get parameters :for-LaTeX))
1251 (commentsp (plist-get parameters :comments))
1252 (archived-trees (plist-get parameters :archived-trees))
1253 (inhibit-read-only t)
1254 (drawers org-drawers)
1255 (exp-drawers (plist-get parameters :drawers))
1256 (outline-regexp "\\*+ ")
1257 target-alist tmp target level
1258 a b xx rtn p)
1260 (with-current-buffer (get-buffer-create " org-mode-tmp")
1261 (erase-buffer)
1262 (insert string)
1263 (setq case-fold-search t)
1264 ;; Call the hook
1265 (run-hooks 'org-export-preprocess-hook)
1267 ;; Remove license-to-kill stuff
1268 ;; The caller markes some stuff fo killing, stuff that has been
1269 ;; used to create the page title, for example.
1270 (while (setq p (text-property-any (point-min) (point-max)
1271 :org-license-to-kill t))
1272 (delete-region p (next-single-property-change p :org-license-to-kill)))
1274 (let ((org-inhibit-startup t)) (org-mode))
1275 (setq case-fold-search t)
1276 (untabify (point-min) (point-max))
1278 ;; Handle incude files
1279 (org-export-handle-include-files)
1281 ;; Handle source code snippets
1282 (org-export-replace-src-segments)
1284 ;; Get rid of drawers
1285 (unless (eq t exp-drawers)
1286 (goto-char (point-min))
1287 (let ((re (concat "^[ \t]*:\\("
1288 (mapconcat
1289 'identity
1290 (org-delete-all exp-drawers
1291 (copy-sequence drawers))
1292 "\\|")
1293 "\\):[ \t]*\n\\([^@]*?\n\\)?[ \t]*:END:[ \t]*\n")))
1294 (while (re-search-forward re nil t)
1295 (replace-match ""))))
1297 ;; Get the correct stuff before the first headline
1298 (when (plist-get parameters :skip-before-1st-heading)
1299 (goto-char (point-min))
1300 (when (re-search-forward "^\\*+[ \t]" nil t)
1301 (delete-region (point-min) (match-beginning 0))
1302 (goto-char (point-min))
1303 (insert "\n")))
1304 (when (plist-get parameters :add-text)
1305 (goto-char (point-min))
1306 (insert (plist-get parameters :add-text) "\n"))
1308 ;; Get rid of archived trees
1309 (when (not (eq archived-trees t))
1310 (goto-char (point-min))
1311 (while (re-search-forward re-archive nil t)
1312 (if (not (org-on-heading-p t))
1313 (org-end-of-subtree t)
1314 (beginning-of-line 1)
1315 (setq a (if archived-trees
1316 (1+ (point-at-eol)) (point))
1317 b (org-end-of-subtree t))
1318 (if (> b a) (delete-region a b)))))
1320 ;; Find all headings and compute the targets for them
1321 (goto-char (point-min))
1322 (org-init-section-numbers)
1323 (let ((re (concat "^" org-outline-regexp)))
1324 (while (re-search-forward re nil t)
1325 (setq level (org-reduced-level
1326 (save-excursion (goto-char (point-at-bol))
1327 (org-outline-level))))
1328 (setq target (org-solidify-link-text
1329 (format "sec-%s" (org-section-number level))))
1330 (push (cons target target) target-alist)
1331 (add-text-properties
1332 (point-at-bol) (point-at-eol)
1333 (list 'target target))))
1335 ;; Find targets in comments and move them out of comments,
1336 ;; but mark them as targets that should be invisible
1337 (goto-char (point-min))
1338 (while (re-search-forward "^#.*?\\(<<<?\\([^>\r\n]+\\)>>>?\\).*" nil t)
1339 ;; Check if the line before or after is a headline with a target
1340 (if (setq target (or (get-text-property (point-at-bol 0) 'target)
1341 (get-text-property (point-at-bol 2) 'target)))
1342 (progn
1343 ;; use the existing target in a neighboring line
1344 (setq tmp (match-string 2))
1345 (replace-match "")
1346 (and (looking-at "\n") (delete-char 1))
1347 (push (cons (org-solidify-link-text tmp) target)
1348 target-alist))
1349 ;; Make an invisible target
1350 (replace-match "\\1(INVISIBLE)")))
1352 ;; Protect backend specific stuff, throw away the others.
1353 (let ((formatters
1354 `((,htmlp "HTML" "BEGIN_HTML" "END_HTML")
1355 (,asciip "ASCII" "BEGIN_ASCII" "END_ASCII")
1356 (,latexp "LaTeX" "BEGIN_LaTeX" "END_LaTeX")))
1357 fmt)
1358 (goto-char (point-min))
1359 (while (re-search-forward "^#\\+BEGIN_EXAMPLE[ \t]*\n" nil t)
1360 (goto-char (match-end 0))
1361 (while (not (looking-at "#\\+END_EXAMPLE"))
1362 (insert ": ")
1363 (beginning-of-line 2)))
1364 (goto-char (point-min))
1365 (while (re-search-forward "^[ \t]*:.*\\(\n[ \t]*:.*\\)*" nil t)
1366 (add-text-properties (match-beginning 0) (match-end 0)
1367 '(org-protected t)))
1368 (while formatters
1369 (setq fmt (pop formatters))
1370 (when (car fmt)
1371 (goto-char (point-min))
1372 (while (re-search-forward (concat "^#\\+" (cadr fmt)
1373 ":[ \t]*\\(.*\\)") nil t)
1374 (replace-match "\\1" t)
1375 (add-text-properties
1376 (point-at-bol) (min (1+ (point-at-eol)) (point-max))
1377 '(org-protected t))))
1378 (goto-char (point-min))
1379 (while (re-search-forward
1380 (concat "^#\\+"
1381 (caddr fmt) "\\>.*\\(\\(\n.*\\)*?\n\\)#\\+"
1382 (cadddr fmt) "\\>.*\n?") nil t)
1383 (if (car fmt)
1384 (add-text-properties (match-beginning 1) (1+ (match-end 1))
1385 '(org-protected t))
1386 (delete-region (match-beginning 0) (match-end 0))))))
1388 ;; Protect quoted subtrees
1389 (goto-char (point-min))
1390 (while (re-search-forward re-quote nil t)
1391 (goto-char (match-beginning 0))
1392 (end-of-line 1)
1393 (add-text-properties (point) (org-end-of-subtree t)
1394 '(org-protected t)))
1396 ;; Protect verbatim elements
1397 (goto-char (point-min))
1398 (while (re-search-forward org-verbatim-re nil t)
1399 (add-text-properties (match-beginning 4) (match-end 4)
1400 '(org-protected t))
1401 (goto-char (1+ (match-end 4))))
1403 ;; Blockquotes
1404 (goto-char (point-min))
1405 (while (re-search-forward "^#\\+\\(begin\\|end\\)_\\(block\\)?quote\\>.*" nil t)
1406 (replace-match (if (equal (downcase (match-string 1)) "end")
1407 "ORG-BLOCKQUOTE-END" "ORG-BLOCKQUOTE-START")
1408 t t))
1409 ;; Verse
1410 (goto-char (point-min))
1411 (while (re-search-forward "^#\\+\\(begin\\|end\\)_verse\\>.*" nil t)
1412 (replace-match (if (equal (downcase (match-string 1)) "end")
1413 "ORG-VERSE-END" "ORG-VERSE-START")
1414 t t))
1416 ;; Remove comment environment
1417 (goto-char (point-min))
1418 (while (re-search-forward
1419 "^#\\+BEGIN_COMMENT[ \t]*\n[^\000]*?^#\\+END_COMMENT\\>.*" nil t)
1420 (replace-match "" t t))
1422 ;; Remove subtrees that are commented
1423 (goto-char (point-min))
1424 (while (re-search-forward re-commented nil t)
1425 (goto-char (match-beginning 0))
1426 (delete-region (point) (org-end-of-subtree t)))
1428 ;; Remove special table lines
1429 (when org-export-table-remove-special-lines
1430 (goto-char (point-min))
1431 (while (re-search-forward "^[ \t]*|" nil t)
1432 (beginning-of-line 1)
1433 (if (or (looking-at "[ \t]*| *[!_^] *|")
1434 (and (looking-at ".*?| *<[0-9]+> *|")
1435 (not (looking-at ".*?| *[^ <|]"))))
1436 (delete-region (max (point-min) (1- (point-at-bol)))
1437 (point-at-eol))
1438 (end-of-line 1))))
1440 ;; Specific LaTeX stuff
1441 (when latexp
1442 (require 'org-export-latex nil)
1443 (org-export-latex-preprocess))
1445 (when asciip
1446 (org-export-ascii-clean-string))
1448 ;; Specific HTML stuff
1449 (when htmlp
1450 ;; Convert LaTeX fragments to images
1451 (when (plist-get parameters :LaTeX-fragments)
1452 (org-format-latex
1453 (concat "ltxpng/" (file-name-sans-extension
1454 (file-name-nondirectory
1455 org-current-export-file)))
1456 org-current-export-dir nil "Creating LaTeX image %s"))
1457 (message "Exporting..."))
1459 ;; Remove or replace comments
1460 (goto-char (point-min))
1461 (while (re-search-forward "^#\\(.*\n?\\)" nil t)
1462 (setq pos (match-beginning 0))
1463 (if commentsp
1464 (progn (add-text-properties
1465 (match-beginning 0) (match-end 0) '(org-protected t))
1466 (replace-match (format commentsp (match-string 1)) t t))
1467 (goto-char (1+ pos))
1468 (org-if-unprotected
1469 (replace-match "")
1470 (goto-char (max (point-min) (1- pos))))
1471 (end-of-line 1)))
1473 ;; Find matches for radio targets and turn them into internal links
1474 (goto-char (point-min))
1475 (when re-radio
1476 (while (re-search-forward re-radio nil t)
1477 (org-if-unprotected
1478 (replace-match "\\1[[\\2]]"))))
1480 ;; Find all links that contain a newline and put them into a single line
1481 (goto-char (point-min))
1482 (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t)
1483 (org-if-unprotected
1484 (replace-match "\\1 \\3")
1485 (goto-char (match-beginning 0))))
1487 ;; Find all internal links. If they have a fuzzy match (i.e. not
1488 ;; a *dedicated* target match, let the link point to the
1489 ;; correspinding section.
1491 (goto-char (point-min))
1492 (while (re-search-forward org-bracket-link-regexp nil t)
1493 (org-if-unprotected
1494 (let* ((md (match-data))
1495 (desc (match-end 2))
1496 (link (org-link-unescape (match-string 1)))
1497 (slink (org-solidify-link-text link))
1498 found props pos
1499 (target
1500 (or (cdr (assoc slink target-alist))
1501 (save-excursion
1502 (unless (string-match org-link-types-re link)
1503 (setq found (condition-case nil (org-link-search link)
1504 (error nil)))
1505 (when (and found
1506 (or (org-on-heading-p)
1507 (not (eq found 'dedicated))))
1508 (or (get-text-property (point) 'target)
1509 (get-text-property
1510 (max (point-min)
1511 (1- (previous-single-property-change
1512 (point) 'target)))
1513 'target))))))))
1514 (when target
1515 (set-match-data md)
1516 (goto-char (match-beginning 1))
1517 (setq props (text-properties-at (point)))
1518 (delete-region (match-beginning 1) (match-end 1))
1519 (setq pos (point))
1520 (insert target)
1521 (unless desc (insert "][" link))
1522 (add-text-properties pos (point) props)))))
1524 ;; Normalize links: Convert angle and plain links into bracket links
1525 ;; Expand link abbreviations
1526 (goto-char (point-min))
1527 (while (re-search-forward re-plain-link nil t)
1528 (goto-char (1- (match-end 0)))
1529 (org-if-unprotected
1530 (let* ((s (concat (match-string 1) "[[" (match-string 2)
1531 ":" (match-string 3) "]]")))
1532 ;; added 'org-link face to links
1533 (put-text-property 0 (length s) 'face 'org-link s)
1534 (replace-match s t t))))
1535 (goto-char (point-min))
1536 (while (re-search-forward re-angle-link nil t)
1537 (goto-char (1- (match-end 0)))
1538 (org-if-unprotected
1539 (let* ((s (concat (match-string 1) "[[" (match-string 2)
1540 ":" (match-string 3) "]]")))
1541 (put-text-property 0 (length s) 'face 'org-link s)
1542 (replace-match s t t))))
1543 (goto-char (point-min))
1544 (while (re-search-forward org-bracket-link-regexp nil t)
1545 (org-if-unprotected
1546 (let* ((s (concat "[[" (setq xx (save-match-data
1547 (org-link-expand-abbrev (match-string 1))))
1549 (if (match-end 3)
1550 (match-string 2)
1551 (concat "[" xx "]"))
1552 "]")))
1553 (put-text-property 0 (length s) 'face 'org-link s)
1554 (replace-match s t t))))
1556 ;; Find multiline emphasis and put them into single line
1557 (when (plist-get parameters :emph-multiline)
1558 (goto-char (point-min))
1559 (while (re-search-forward org-emph-re nil t)
1560 (if (not (= (char-after (match-beginning 3))
1561 (char-after (match-beginning 4))))
1562 (org-if-unprotected
1563 (subst-char-in-region (match-beginning 0) (match-end 0)
1564 ?\n ?\ t)
1565 (goto-char (1- (match-end 0))))
1566 (goto-char (1+ (match-beginning 0))))))
1568 (setq rtn (buffer-string)))
1569 (kill-buffer " org-mode-tmp")
1570 rtn))
1572 (defun org-export-grab-title-from-buffer ()
1573 "Get a title for the current document, from looking at the buffer."
1574 (let ((inhibit-read-only t))
1575 (save-excursion
1576 (goto-char (point-min))
1577 (let ((end (save-excursion (outline-next-heading) (point))))
1578 (when (re-search-forward "^[ \t]*[^|# \t\r\n].*\n" end t)
1579 ;; Mark the line so that it will not be exported as normal text.
1580 (org-unmodified
1581 (add-text-properties (match-beginning 0) (match-end 0)
1582 (list :org-license-to-kill t)))
1583 ;; Return the title string
1584 (org-trim (match-string 0)))))))
1586 (defun org-export-get-title-from-subtree ()
1587 "Return subtree title and exclude it from export."
1588 (let (title (m (mark)) (rbeg (region-beginning)) (rend (region-end)))
1589 (save-excursion
1590 (goto-char rbeg)
1591 (when (and (org-at-heading-p)
1592 (>= (org-end-of-subtree t t) rend))
1593 ;; This is a subtree, we take the title from the first heading
1594 (goto-char rbeg)
1595 (looking-at org-todo-line-regexp)
1596 (setq title (match-string 3))
1597 (org-unmodified
1598 (add-text-properties (point) (1+ (point-at-eol))
1599 (list :org-license-to-kill t)))
1600 (setq title (or (org-entry-get nil "EXPORT_TITLE") title))))
1601 title))
1603 (defun org-solidify-link-text (s &optional alist)
1604 "Take link text and make a safe target out of it."
1605 (save-match-data
1606 (let* ((rtn
1607 (mapconcat
1608 'identity
1609 (org-split-string s "[ \t\r\n]+") "=="))
1610 (a (assoc rtn alist)))
1611 (or (cdr a) rtn))))
1613 (defun org-get-min-level (lines)
1614 "Get the minimum level in LINES."
1615 (let ((re "^\\(\\*+\\) ") l min)
1616 (catch 'exit
1617 (while (setq l (pop lines))
1618 (if (string-match re l)
1619 (throw 'exit (org-tr-level (length (match-string 1 l))))))
1620 1)))
1622 ;; Variable holding the vector with section numbers
1623 (defvar org-section-numbers (make-vector org-level-max 0))
1625 (defun org-init-section-numbers ()
1626 "Initialize the vector for the section numbers."
1627 (let* ((level -1)
1628 (numbers (nreverse (org-split-string "" "\\.")))
1629 (depth (1- (length org-section-numbers)))
1630 (i depth) number-string)
1631 (while (>= i 0)
1632 (if (> i level)
1633 (aset org-section-numbers i 0)
1634 (setq number-string (or (car numbers) "0"))
1635 (if (string-match "\\`[A-Z]\\'" number-string)
1636 (aset org-section-numbers i
1637 (- (string-to-char number-string) ?A -1))
1638 (aset org-section-numbers i (string-to-number number-string)))
1639 (pop numbers))
1640 (setq i (1- i)))))
1642 (defun org-section-number (&optional level)
1643 "Return a string with the current section number.
1644 When LEVEL is non-nil, increase section numbers on that level."
1645 (let* ((depth (1- (length org-section-numbers))) idx n (string ""))
1646 (when level
1647 (when (> level -1)
1648 (aset org-section-numbers
1649 level (1+ (aref org-section-numbers level))))
1650 (setq idx (1+ level))
1651 (while (<= idx depth)
1652 (if (not (= idx 1))
1653 (aset org-section-numbers idx 0))
1654 (setq idx (1+ idx))))
1655 (setq idx 0)
1656 (while (<= idx depth)
1657 (setq n (aref org-section-numbers idx))
1658 (setq string (concat string (if (not (string= string "")) "." "")
1659 (int-to-string n)))
1660 (setq idx (1+ idx)))
1661 (save-match-data
1662 (if (string-match "\\`\\([@0]\\.\\)+" string)
1663 (setq string (replace-match "" t nil string)))
1664 (if (string-match "\\(\\.0\\)+\\'" string)
1665 (setq string (replace-match "" t nil string))))
1666 string))
1668 ;;; Include files
1670 (defun org-export-handle-include-files ()
1671 "Include the contents of include files, with proper formatting."
1672 (let ((case-fold-search t)
1673 params file markup lang start end)
1674 (goto-char (point-min))
1675 (while (re-search-forward "^#\\+INCLUDE:?[ \t]+\\(.*\\)" nil t)
1676 (setq params (read (concat "(" (match-string 1) ")"))
1677 file (org-symname-or-string (pop params))
1678 markup (org-symname-or-string (pop params))
1679 lang (org-symname-or-string (pop params)))
1680 (delete-region (match-beginning 0) (match-end 0))
1681 (if (or (not file)
1682 (not (file-exists-p file))
1683 (not (file-readable-p file)))
1684 (insert (format "CANNOT INCLUDE FILE %s" file))
1685 (when markup
1686 (if (equal (downcase markup) "src")
1687 (setq start (format "#+begin_src %s\n" (or lang "fundamental"))
1688 end "#+end_src")
1689 (setq start (format "#+begin_%s\n" markup)
1690 end (format "#+end_%s" markup))))
1691 (insert (or start ""))
1692 (forward-char (nth 1 (insert-file-contents (expand-file-name file))))
1693 (or (bolp) (newline))
1694 (insert (or end ""))))))
1696 (defun org-symname-or-string (s)
1697 (if (symbolp s)
1698 (if s (symbol-name s) s)
1701 ;;; Fontification of code
1702 ;; Currently only for th HTML backend, but who knows....
1703 (defun org-export-replace-src-segments ()
1704 "Replace source code segments with special code for export."
1705 (let ((case-fold-search t)
1706 lang code trans)
1707 (goto-char (point-min))
1708 (while (re-search-forward
1709 "^#\\+BEGIN_SRC:?[ \t]+\\([^ \t\n]+\\)[ \t]*\n\\([^\000]+?\n\\)#\\+END_SRC.*"
1710 nil t)
1711 (setq lang (match-string 1) code (match-string 2)
1712 trans (org-export-format-source-code lang code))
1713 (replace-match trans t t))))
1715 (defvar htmlp) ;; dynamically scoped from org-exp.el
1717 (defun org-export-format-source-code (lang code)
1718 "Format CODE from language LANG and return it formatted for export.
1719 Currently, this only does something for HTML export, for all other
1720 backends, it converts the segment into an EXAMPLE segment."
1721 (save-match-data
1722 (cond
1723 (htmlp
1724 ;; We are exporting to HTML
1725 (condition-case nil (require 'htmlize) (nil t))
1726 (if (not (fboundp 'htmlize-region-for-paste))
1727 (progn
1728 ;; we do not have htmlize.el, or an old version of it
1729 (message
1730 "htmlize.el 1.34 or later is needed for source code formatting")
1731 (concat "#+BEGIN_EXAMPLE\n" code
1732 (if (string-match "\n\\'" code) "" "\n")
1733 "#+END_EXAMPLE\n"))
1734 ;; ok, we are good to go
1735 (let* ((mode (and lang (intern (concat lang "-mode"))))
1736 (org-inhibit-startup t)
1737 (org-startup-folded nil)
1738 (htmltext
1739 (with-temp-buffer
1740 (insert code)
1741 ;; Free up the protected stuff
1742 (goto-char (point-min))
1743 (while (re-search-forward "^," nil t)
1744 (replace-match "")
1745 (end-of-line 1))
1746 (if (functionp mode)
1747 (funcall mode)
1748 (fundamental-mode))
1749 (font-lock-fontify-buffer)
1750 (org-export-htmlize-region-for-paste
1751 (point-min) (point-max)))))
1752 (if (string-match "<pre\\([^>]*\\)>\n?" htmltext)
1753 (setq htmltext (replace-match "<pre class=\"src\">"
1754 t t htmltext)))
1755 (concat "#+BEGIN_HTML\n" htmltext "\n#+END_HTML\n"))))
1757 ;; This is not HTML, so just make it an example.
1758 (concat "#+BEGIN_EXAMPLE\n" code
1759 (if (string-match "\n\\'" code) "" "\n")
1760 "#+END_EXAMPLE\n")))))
1762 ;;; ASCII export
1764 (defvar org-last-level nil) ; dynamically scoped variable
1765 (defvar org-min-level nil) ; dynamically scoped variable
1766 (defvar org-levels-open nil) ; dynamically scoped parameter
1767 (defvar org-ascii-current-indentation nil) ; For communication
1769 ;;;###autoload
1770 (defun org-export-as-ascii (arg)
1771 "Export the outline as a pretty ASCII file.
1772 If there is an active region, export only the region.
1773 The prefix ARG specifies how many levels of the outline should become
1774 underlined headlines. The default is 3."
1775 (interactive "P")
1776 (setq-default org-todo-line-regexp org-todo-line-regexp)
1777 (let* ((opt-plist (org-combine-plists (org-default-export-plist)
1778 (org-infile-export-plist)))
1779 (region-p (org-region-active-p))
1780 (rbeg (and region-p (region-beginning)))
1781 (rend (and region-p (region-end)))
1782 (subtree-p
1783 (when region-p
1784 (save-excursion
1785 (goto-char rbeg)
1786 (and (org-at-heading-p)
1787 (>= (org-end-of-subtree t t) rend)))))
1788 (opt-plist (if subtree-p
1789 (org-export-add-subtree-options opt-plist rbeg)
1790 opt-plist))
1791 (custom-times org-display-custom-times)
1792 (org-ascii-current-indentation '(0 . 0))
1793 (level 0) line txt
1794 (umax nil)
1795 (umax-toc nil)
1796 (case-fold-search nil)
1797 (filename (concat (file-name-as-directory
1798 (org-export-directory :ascii opt-plist))
1799 (file-name-sans-extension
1800 (or (and subtree-p
1801 (org-entry-get (region-beginning)
1802 "EXPORT_FILE_NAME" t))
1803 (file-name-nondirectory buffer-file-name)))
1804 ".txt"))
1805 (filename (if (equal (file-truename filename)
1806 (file-truename buffer-file-name))
1807 (concat filename ".txt")
1808 filename))
1809 (buffer (find-file-noselect filename))
1810 (org-levels-open (make-vector org-level-max nil))
1811 (odd org-odd-levels-only)
1812 (date (plist-get opt-plist :date))
1813 (author (plist-get opt-plist :author))
1814 (title (or (and subtree-p (org-export-get-title-from-subtree))
1815 (plist-get opt-plist :title)
1816 (and (not
1817 (plist-get opt-plist :skip-before-1st-heading))
1818 (org-export-grab-title-from-buffer))
1819 (file-name-sans-extension
1820 (file-name-nondirectory buffer-file-name))))
1821 (email (plist-get opt-plist :email))
1822 (language (plist-get opt-plist :language))
1823 (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
1824 ; (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
1825 (todo nil)
1826 (lang-words nil)
1827 (region
1828 (buffer-substring
1829 (if (org-region-active-p) (region-beginning) (point-min))
1830 (if (org-region-active-p) (region-end) (point-max))))
1831 (lines (org-split-string
1832 (org-export-preprocess-string
1833 region
1834 :for-ascii t
1835 :skip-before-1st-heading
1836 (plist-get opt-plist :skip-before-1st-heading)
1837 :drawers (plist-get opt-plist :drawers)
1838 :verbatim-multiline t
1839 :archived-trees
1840 (plist-get opt-plist :archived-trees)
1841 :add-text (plist-get opt-plist :text))
1842 "\n"))
1843 thetoc have-headings first-heading-pos
1844 table-open table-buffer)
1846 (let ((inhibit-read-only t))
1847 (org-unmodified
1848 (remove-text-properties (point-min) (point-max)
1849 '(:org-license-to-kill t))))
1851 (setq org-min-level (org-get-min-level lines))
1852 (setq org-last-level org-min-level)
1853 (org-init-section-numbers)
1855 (find-file-noselect filename)
1857 (setq lang-words (or (assoc language org-export-language-setup)
1858 (assoc "en" org-export-language-setup)))
1859 (switch-to-buffer-other-window buffer)
1860 (erase-buffer)
1861 (fundamental-mode)
1862 ;; create local variables for all options, to make sure all called
1863 ;; functions get the correct information
1864 (mapc (lambda (x)
1865 (set (make-local-variable (cdr x))
1866 (plist-get opt-plist (car x))))
1867 org-export-plist-vars)
1868 (org-set-local 'org-odd-levels-only odd)
1869 (setq umax (if arg (prefix-numeric-value arg)
1870 org-export-headline-levels))
1871 (setq umax-toc (if (integerp org-export-with-toc)
1872 (min org-export-with-toc umax)
1873 umax))
1875 ;; File header
1876 (if title (org-insert-centered title ?=))
1877 (insert "\n")
1878 (if (and (or author email)
1879 org-export-author-info)
1880 (insert (concat (nth 1 lang-words) ": " (or author "")
1881 (if email (concat " <" email ">") "")
1882 "\n")))
1884 (cond
1885 ((and date (string-match "%" date))
1886 (setq date (format-time-string date)))
1887 (date)
1888 (t (setq date (format-time-string "%Y/%m/%d %X"))))
1890 (if (and date org-export-time-stamp-file)
1891 (insert (concat (nth 2 lang-words) ": " date"\n")))
1893 (insert "\n\n")
1895 (if org-export-with-toc
1896 (progn
1897 (push (concat (nth 3 lang-words) "\n") thetoc)
1898 (push (concat (make-string (length (nth 3 lang-words)) ?=) "\n") thetoc)
1899 (mapc '(lambda (line)
1900 (if (string-match org-todo-line-regexp
1901 line)
1902 ;; This is a headline
1903 (progn
1904 (setq have-headings t)
1905 (setq level (- (match-end 1) (match-beginning 1))
1906 level (org-tr-level level)
1907 txt (match-string 3 line)
1908 todo
1909 (or (and org-export-mark-todo-in-toc
1910 (match-beginning 2)
1911 (not (member (match-string 2 line)
1912 org-done-keywords)))
1913 ; TODO, not DONE
1914 (and org-export-mark-todo-in-toc
1915 (= level umax-toc)
1916 (org-search-todo-below
1917 line lines level))))
1918 (setq txt (org-html-expand-for-ascii txt))
1920 (while (string-match org-bracket-link-regexp txt)
1921 (setq txt
1922 (replace-match
1923 (match-string (if (match-end 2) 3 1) txt)
1924 t t txt)))
1926 (if (and (memq org-export-with-tags '(not-in-toc nil))
1927 (string-match
1928 (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
1929 txt))
1930 (setq txt (replace-match "" t t txt)))
1931 (if (string-match quote-re0 txt)
1932 (setq txt (replace-match "" t t txt)))
1934 (if org-export-with-section-numbers
1935 (setq txt (concat (org-section-number level)
1936 " " txt)))
1937 (if (<= level umax-toc)
1938 (progn
1939 (push
1940 (concat
1941 (make-string
1942 (* (max 0 (- level org-min-level)) 4) ?\ )
1943 (format (if todo "%s (*)\n" "%s\n") txt))
1944 thetoc)
1945 (setq org-last-level level))
1946 ))))
1947 lines)
1948 (setq thetoc (if have-headings (nreverse thetoc) nil))))
1950 (org-init-section-numbers)
1951 (while (setq line (pop lines))
1952 ;; Remove the quoted HTML tags.
1953 (setq line (org-html-expand-for-ascii line))
1954 ;; Remove targets
1955 (while (string-match "<<<?[^<>]*>>>?[ \t]*\n?" line)
1956 (setq line (replace-match "" t t line)))
1957 ;; Replace internal links
1958 (while (string-match org-bracket-link-regexp line)
1959 (setq line (replace-match
1960 (if (match-end 3) "[\\3]" "[\\1]")
1961 t nil line)))
1962 (when custom-times
1963 (setq line (org-translate-time line)))
1964 (cond
1965 ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
1966 ;; a Headline
1967 (setq first-heading-pos (or first-heading-pos (point)))
1968 (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
1969 txt (match-string 2 line))
1970 (org-ascii-level-start level txt umax lines))
1972 ((and org-export-with-tables
1973 (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
1974 (if (not table-open)
1975 ;; New table starts
1976 (setq table-open t table-buffer nil))
1977 ;; Accumulate lines
1978 (setq table-buffer (cons line table-buffer))
1979 (when (or (not lines)
1980 (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
1981 (car lines))))
1982 (setq table-open nil
1983 table-buffer (nreverse table-buffer))
1984 (insert (mapconcat
1985 (lambda (x)
1986 (org-fix-indentation x org-ascii-current-indentation))
1987 (org-format-table-ascii table-buffer)
1988 "\n") "\n")))
1990 (setq line (org-fix-indentation line org-ascii-current-indentation))
1991 (if (and org-export-with-fixed-width
1992 (string-match "^\\([ \t]*\\)\\(:\\)" line))
1993 (setq line (replace-match "\\1" nil nil line)))
1994 (insert line "\n"))))
1996 (normal-mode)
1998 ;; insert the table of contents
1999 (when thetoc
2000 (goto-char (point-min))
2001 (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t)
2002 (progn
2003 (goto-char (match-beginning 0))
2004 (replace-match ""))
2005 (goto-char first-heading-pos))
2006 (mapc 'insert thetoc)
2007 (or (looking-at "[ \t]*\n[ \t]*\n")
2008 (insert "\n\n")))
2010 ;; Convert whitespace place holders
2011 (goto-char (point-min))
2012 (let (beg end)
2013 (while (setq beg (next-single-property-change (point) 'org-whitespace))
2014 (setq end (next-single-property-change beg 'org-whitespace))
2015 (goto-char beg)
2016 (delete-region beg end)
2017 (insert (make-string (- end beg) ?\ ))))
2019 (save-buffer)
2020 ;; remove display and invisible chars
2021 (let (beg end)
2022 (goto-char (point-min))
2023 (while (setq beg (next-single-property-change (point) 'display))
2024 (setq end (next-single-property-change beg 'display))
2025 (delete-region beg end)
2026 (goto-char beg)
2027 (insert "=>"))
2028 (goto-char (point-min))
2029 (while (setq beg (next-single-property-change (point) 'org-cwidth))
2030 (setq end (next-single-property-change beg 'org-cwidth))
2031 (delete-region beg end)
2032 (goto-char beg)))
2033 (goto-char (point-min))))
2035 (defun org-export-ascii-clean-string ()
2036 "Do extra work for ASCII export"
2037 (goto-char (point-min))
2038 (while (re-search-forward org-verbatim-re nil t)
2039 (goto-char (match-end 2))
2040 (backward-delete-char 1) (insert "'")
2041 (goto-char (match-beginning 2))
2042 (delete-char 1) (insert "`")
2043 (goto-char (match-end 2))))
2045 (defun org-search-todo-below (line lines level)
2046 "Search the subtree below LINE for any TODO entries."
2047 (let ((rest (cdr (memq line lines)))
2048 (re org-todo-line-regexp)
2049 line lv todo)
2050 (catch 'exit
2051 (while (setq line (pop rest))
2052 (if (string-match re line)
2053 (progn
2054 (setq lv (- (match-end 1) (match-beginning 1))
2055 todo (and (match-beginning 2)
2056 (not (member (match-string 2 line)
2057 org-done-keywords))))
2058 ; TODO, not DONE
2059 (if (<= lv level) (throw 'exit nil))
2060 (if todo (throw 'exit t))))))))
2062 (defun org-html-expand-for-ascii (line)
2063 "Handle quoted HTML for ASCII export."
2064 (if org-export-html-expand
2065 (while (string-match "@<[^<>\n]*>" line)
2066 ;; We just remove the tags for now.
2067 (setq line (replace-match "" nil nil line))))
2068 line)
2070 (defun org-insert-centered (s &optional underline)
2071 "Insert the string S centered and underline it with character UNDERLINE."
2072 (let ((ind (max (/ (- 80 (string-width s)) 2) 0)))
2073 (insert (make-string ind ?\ ) s "\n")
2074 (if underline
2075 (insert (make-string ind ?\ )
2076 (make-string (string-width s) underline)
2077 "\n"))))
2079 (defun org-ascii-level-start (level title umax &optional lines)
2080 "Insert a new level in ASCII export."
2081 (let (char (n (- level umax 1)) (ind 0))
2082 (if (> level umax)
2083 (progn
2084 (insert (make-string (* 2 n) ?\ )
2085 (char-to-string (nth (% n (length org-export-ascii-bullets))
2086 org-export-ascii-bullets))
2087 " " title "\n")
2088 ;; find the indentation of the next non-empty line
2089 (catch 'stop
2090 (while lines
2091 (if (string-match "^\\* " (car lines)) (throw 'stop nil))
2092 (if (string-match "^\\([ \t]*\\)\\S-" (car lines))
2093 (throw 'stop (setq ind (org-get-indentation (car lines)))))
2094 (pop lines)))
2095 (setq org-ascii-current-indentation (cons (* 2 (1+ n)) ind)))
2096 (if (or (not (equal (char-before) ?\n))
2097 (not (equal (char-before (1- (point))) ?\n)))
2098 (insert "\n"))
2099 (setq char (nth (- umax level) (reverse org-export-ascii-underline)))
2100 (unless org-export-with-tags
2101 (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
2102 (setq title (replace-match "" t t title))))
2103 (if org-export-with-section-numbers
2104 (setq title (concat (org-section-number level) " " title)))
2105 (insert title "\n" (make-string (string-width title) char) "\n")
2106 (setq org-ascii-current-indentation '(0 . 0)))))
2108 ;;;###autoload
2109 (defun org-export-visible (type arg)
2110 "Create a copy of the visible part of the current buffer, and export it.
2111 The copy is created in a temporary buffer and removed after use.
2112 TYPE is the final key (as a string) that also select the export command in
2113 the `C-c C-e' export dispatcher.
2114 As a special case, if the you type SPC at the prompt, the temporary
2115 org-mode file will not be removed but presented to you so that you can
2116 continue to use it. The prefix arg ARG is passed through to the exporting
2117 command."
2118 (interactive
2119 (list (progn
2120 (message "Export visible: [a]SCII [h]tml [b]rowse HTML [H/R]uffer with HTML [x]OXO [ ]keep buffer")
2121 (read-char-exclusive))
2122 current-prefix-arg))
2123 (if (not (member type '(?a ?\C-a ?b ?\C-b ?h ?x ?\ )))
2124 (error "Invalid export key"))
2125 (let* ((binding (cdr (assoc type
2126 '((?a . org-export-as-ascii)
2127 (?\C-a . org-export-as-ascii)
2128 (?b . org-export-as-html-and-open)
2129 (?\C-b . org-export-as-html-and-open)
2130 (?h . org-export-as-html)
2131 (?H . org-export-as-html-to-buffer)
2132 (?R . org-export-region-as-html)
2133 (?x . org-export-as-xoxo)))))
2134 (keepp (equal type ?\ ))
2135 (file buffer-file-name)
2136 (buffer (get-buffer-create "*Org Export Visible*"))
2137 s e)
2138 ;; Need to hack the drawers here.
2139 (save-excursion
2140 (goto-char (point-min))
2141 (while (re-search-forward org-drawer-regexp nil t)
2142 (goto-char (match-beginning 1))
2143 (or (org-invisible-p) (org-flag-drawer nil))))
2144 (with-current-buffer buffer (erase-buffer))
2145 (save-excursion
2146 (setq s (goto-char (point-min)))
2147 (while (not (= (point) (point-max)))
2148 (goto-char (org-find-invisible))
2149 (append-to-buffer buffer s (point))
2150 (setq s (goto-char (org-find-visible))))
2151 (org-cycle-hide-drawers 'all)
2152 (goto-char (point-min))
2153 (unless keepp
2154 ;; Copy all comment lines to the end, to make sure #+ settings are
2155 ;; still available for the second export step. Kind of a hack, but
2156 ;; does do the trick.
2157 (if (looking-at "#[^\r\n]*")
2158 (append-to-buffer buffer (match-beginning 0) (1+ (match-end 0))))
2159 (while (re-search-forward "[\n\r]#[^\n\r]*" nil t)
2160 (append-to-buffer buffer (1+ (match-beginning 0))
2161 (min (point-max) (1+ (match-end 0))))))
2162 (set-buffer buffer)
2163 (let ((buffer-file-name file)
2164 (org-inhibit-startup t))
2165 (org-mode)
2166 (show-all)
2167 (unless keepp (funcall binding arg))))
2168 (if (not keepp)
2169 (kill-buffer buffer)
2170 (switch-to-buffer-other-window buffer)
2171 (goto-char (point-min)))))
2173 (defun org-find-visible ()
2174 (let ((s (point)))
2175 (while (and (not (= (point-max) (setq s (next-overlay-change s))))
2176 (get-char-property s 'invisible)))
2178 (defun org-find-invisible ()
2179 (let ((s (point)))
2180 (while (and (not (= (point-max) (setq s (next-overlay-change s))))
2181 (not (get-char-property s 'invisible))))
2184 ;;; HTML export
2186 (defvar org-archive-location) ;; gets loades with the org-archive require.
2187 (defun org-get-current-options ()
2188 "Return a string with current options as keyword options.
2189 Does include HTML export options as well as TODO and CATEGORY stuff."
2190 (require 'org-archive)
2191 (format
2192 "#+TITLE: %s
2193 #+AUTHOR: %s
2194 #+EMAIL: %s
2195 #+DATE: %s
2196 #+LANGUAGE: %s
2197 #+TEXT: Some descriptive text to be emitted. Several lines OK.
2198 #+OPTIONS: H:%d num:%s toc:%s \\n:%s @:%s ::%s |:%s ^:%s -:%s f:%s *:%s TeX:%s LaTeX:%s skip:%s d:%s tags:%s
2200 #+LINK_UP: %s
2201 #+LINK_HOME: %s
2202 #+CATEGORY: %s
2203 #+SEQ_TODO: %s
2204 #+TYP_TODO: %s
2205 #+PRIORITIES: %c %c %c
2206 #+DRAWERS: %s
2207 #+STARTUP: %s %s %s %s %s
2208 #+TAGS: %s
2209 #+ARCHIVE: %s
2210 #+LINK: %s
2212 (buffer-name) (user-full-name) user-mail-address
2213 (format-time-string (car org-time-stamp-formats))
2214 org-export-default-language
2215 org-export-headline-levels
2216 org-export-with-section-numbers
2217 org-export-with-toc
2218 org-export-preserve-breaks
2219 org-export-html-expand
2220 org-export-with-fixed-width
2221 org-export-with-tables
2222 org-export-with-sub-superscripts
2223 org-export-with-special-strings
2224 org-export-with-footnotes
2225 org-export-with-emphasize
2226 org-export-with-TeX-macros
2227 org-export-with-LaTeX-fragments
2228 org-export-skip-text-before-1st-heading
2229 org-export-with-drawers
2230 org-export-with-tags
2231 (if (featurep 'org-jsinfo) (org-infojs-options-inbuffer-template) "")
2232 org-export-html-link-up
2233 org-export-html-link-home
2234 (file-name-nondirectory buffer-file-name)
2235 "TODO FEEDBACK VERIFY DONE"
2236 "Me Jason Marie DONE"
2237 org-highest-priority org-lowest-priority org-default-priority
2238 (mapconcat 'identity org-drawers " ")
2239 (cdr (assoc org-startup-folded
2240 '((nil . "showall") (t . "overview") (content . "content"))))
2241 (if org-odd-levels-only "odd" "oddeven")
2242 (if org-hide-leading-stars "hidestars" "showstars")
2243 (if org-startup-align-all-tables "align" "noalign")
2244 (cond ((eq org-log-done t) "logdone")
2245 ((equal org-log-done 'note) "lognotedone")
2246 ((not org-log-done) "nologdone"))
2247 (or (mapconcat (lambda (x)
2248 (cond
2249 ((equal '(:startgroup) x) "{")
2250 ((equal '(:endgroup) x) "}")
2251 ((cdr x) (format "%s(%c)" (car x) (cdr x)))
2252 (t (car x))))
2253 (or org-tag-alist (org-get-buffer-tags)) " ") "")
2254 org-archive-location
2255 "org file:~/org/%s.org"
2258 ;;;###autoload
2259 (defun org-insert-export-options-template ()
2260 "Insert into the buffer a template with information for exporting."
2261 (interactive)
2262 (if (not (bolp)) (newline))
2263 (let ((s (org-get-current-options)))
2264 (and (string-match "#\\+CATEGORY" s)
2265 (setq s (substring s 0 (match-beginning 0))))
2266 (insert s)))
2268 ;;;###autoload
2269 (defun org-export-as-html-and-open (arg)
2270 "Export the outline as HTML and immediately open it with a browser.
2271 If there is an active region, export only the region.
2272 The prefix ARG specifies how many levels of the outline should become
2273 headlines. The default is 3. Lower levels will become bulleted lists."
2274 (interactive "P")
2275 (org-export-as-html arg 'hidden)
2276 (org-open-file buffer-file-name))
2278 ;;;###autoload
2279 (defun org-export-as-html-batch ()
2280 "Call `org-export-as-html', may be used in batch processing as
2281 emacs --batch
2282 --load=$HOME/lib/emacs/org.el
2283 --eval \"(setq org-export-headline-levels 2)\"
2284 --visit=MyFile --funcall org-export-as-html-batch"
2285 (org-export-as-html org-export-headline-levels 'hidden))
2287 ;;;###autoload
2288 (defun org-export-as-html-to-buffer (arg)
2289 "Call `org-exort-as-html` with output to a temporary buffer.
2290 No file is created. The prefix ARG is passed through to `org-export-as-html'."
2291 (interactive "P")
2292 (org-export-as-html arg nil nil "*Org HTML Export*")
2293 (switch-to-buffer-other-window "*Org HTML Export*"))
2295 ;;;###autoload
2296 (defun org-replace-region-by-html (beg end)
2297 "Assume the current region has org-mode syntax, and convert it to HTML.
2298 This can be used in any buffer. For example, you could write an
2299 itemized list in org-mode syntax in an HTML buffer and then use this
2300 command to convert it."
2301 (interactive "r")
2302 (let (reg html buf pop-up-frames)
2303 (save-window-excursion
2304 (if (org-mode-p)
2305 (setq html (org-export-region-as-html
2306 beg end t 'string))
2307 (setq reg (buffer-substring beg end)
2308 buf (get-buffer-create "*Org tmp*"))
2309 (with-current-buffer buf
2310 (erase-buffer)
2311 (insert reg)
2312 (org-mode)
2313 (setq html (org-export-region-as-html
2314 (point-min) (point-max) t 'string)))
2315 (kill-buffer buf)))
2316 (delete-region beg end)
2317 (insert html)))
2319 ;;;###autoload
2320 (defun org-export-region-as-html (beg end &optional body-only buffer)
2321 "Convert region from BEG to END in org-mode buffer to HTML.
2322 If prefix arg BODY-ONLY is set, omit file header, footer, and table of
2323 contents, and only produce the region of converted text, useful for
2324 cut-and-paste operations.
2325 If BUFFER is a buffer or a string, use/create that buffer as a target
2326 of the converted HTML. If BUFFER is the symbol `string', return the
2327 produced HTML as a string and leave not buffer behind. For example,
2328 a Lisp program could call this function in the following way:
2330 (setq html (org-export-region-as-html beg end t 'string))
2332 When called interactively, the output buffer is selected, and shown
2333 in a window. A non-interactive call will only return the buffer."
2334 (interactive "r\nP")
2335 (when (interactive-p)
2336 (setq buffer "*Org HTML Export*"))
2337 (let ((transient-mark-mode t) (zmacs-regions t)
2338 rtn)
2339 (goto-char end)
2340 (set-mark (point)) ;; to activate the region
2341 (goto-char beg)
2342 (setq rtn (org-export-as-html
2343 nil nil nil
2344 buffer body-only))
2345 (if (fboundp 'deactivate-mark) (deactivate-mark))
2346 (if (and (interactive-p) (bufferp rtn))
2347 (switch-to-buffer-other-window rtn)
2348 rtn)))
2350 (defvar html-table-tag nil) ; dynamically scoped into this.
2351 ;;;###autoload
2352 (defun org-export-as-html (arg &optional hidden ext-plist
2353 to-buffer body-only pub-dir)
2354 "Export the outline as a pretty HTML file.
2355 If there is an active region, export only the region. The prefix
2356 ARG specifies how many levels of the outline should become
2357 headlines. The default is 3. Lower levels will become bulleted
2358 lists. When HIDDEN is non-nil, don't display the HTML buffer.
2359 EXT-PLIST is a property list with external parameters overriding
2360 org-mode's default settings, but still inferior to file-local
2361 settings. When TO-BUFFER is non-nil, create a buffer with that
2362 name and export to that buffer. If TO-BUFFER is the symbol
2363 `string', don't leave any buffer behind but just return the
2364 resulting HTML as a string. When BODY-ONLY is set, don't produce
2365 the file header and footer, simply return the content of
2366 <body>...</body>, without even the body tags themselves. When
2367 PUB-DIR is set, use this as the publishing directory."
2368 (interactive "P")
2370 ;; Make sure we have a file name when we need it.
2371 (when (and (not (or to-buffer body-only))
2372 (not buffer-file-name))
2373 (if (buffer-base-buffer)
2374 (org-set-local 'buffer-file-name
2375 (with-current-buffer (buffer-base-buffer)
2376 buffer-file-name))
2377 (error "Need a file name to be able to export.")))
2379 (message "Exporting...")
2380 (setq-default org-todo-line-regexp org-todo-line-regexp)
2381 (setq-default org-deadline-line-regexp org-deadline-line-regexp)
2382 (setq-default org-done-keywords org-done-keywords)
2383 (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp)
2384 (let* ((opt-plist
2385 (org-export-process-option-filters
2386 (org-combine-plists (org-default-export-plist)
2387 ext-plist
2388 (org-infile-export-plist))))
2390 (style (plist-get opt-plist :style))
2391 (html-extension (plist-get opt-plist :html-extension))
2392 (link-validate (plist-get opt-plist :link-validation-function))
2393 valid thetoc have-headings first-heading-pos
2394 (odd org-odd-levels-only)
2395 (region-p (org-region-active-p))
2396 (rbeg (and region-p (region-beginning)))
2397 (rend (and region-p (region-end)))
2398 (subtree-p
2399 (when region-p
2400 (save-excursion
2401 (goto-char rbeg)
2402 (and (org-at-heading-p)
2403 (>= (org-end-of-subtree t t) rend)))))
2404 (opt-plist (if subtree-p
2405 (org-export-add-subtree-options opt-plist rbeg)
2406 opt-plist))
2407 ;; The following two are dynamically scoped into other
2408 ;; routines below.
2409 (org-current-export-dir
2410 (or pub-dir (org-export-directory :html opt-plist)))
2411 (org-current-export-file buffer-file-name)
2412 (level 0) (line "") (origline "") txt todo
2413 (umax nil)
2414 (umax-toc nil)
2415 (filename (if to-buffer nil
2416 (expand-file-name
2417 (concat
2418 (file-name-sans-extension
2419 (or (and subtree-p
2420 (org-entry-get (region-beginning)
2421 "EXPORT_FILE_NAME" t))
2422 (file-name-nondirectory buffer-file-name)))
2423 "." html-extension)
2424 (file-name-as-directory
2425 (or pub-dir (org-export-directory :html opt-plist))))))
2426 (current-dir (if buffer-file-name
2427 (file-name-directory buffer-file-name)
2428 default-directory))
2429 (buffer (if to-buffer
2430 (cond
2431 ((eq to-buffer 'string) (get-buffer-create "*Org HTML Export*"))
2432 (t (get-buffer-create to-buffer)))
2433 (find-file-noselect filename)))
2434 (org-levels-open (make-vector org-level-max nil))
2435 (date (plist-get opt-plist :date))
2436 (author (plist-get opt-plist :author))
2437 (title (or (and subtree-p (org-export-get-title-from-subtree))
2438 (plist-get opt-plist :title)
2439 (and (not
2440 (plist-get opt-plist :skip-before-1st-heading))
2441 (org-export-grab-title-from-buffer))
2442 (and buffer-file-name
2443 (file-name-sans-extension
2444 (file-name-nondirectory buffer-file-name)))
2445 "UNTITLED"))
2446 (html-table-tag (plist-get opt-plist :html-table-tag))
2447 (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
2448 (quote-re (concat "^\\(\\*+\\)\\([ \t]+" org-quote-string "\\>\\)"))
2449 (inquote nil)
2450 (infixed nil)
2451 (in-local-list nil)
2452 (local-list-type nil)
2453 (local-list-indent nil)
2454 (llt org-plain-list-ordered-item-terminator)
2455 (email (plist-get opt-plist :email))
2456 (language (plist-get opt-plist :language))
2457 (lang-words nil)
2458 (head-count 0) cnt
2459 (start 0)
2460 (coding-system (and (boundp 'buffer-file-coding-system)
2461 buffer-file-coding-system))
2462 (coding-system-for-write (or org-export-html-coding-system
2463 coding-system))
2464 (save-buffer-coding-system (or org-export-html-coding-system
2465 coding-system))
2466 (charset (and coding-system-for-write
2467 (fboundp 'coding-system-get)
2468 (coding-system-get coding-system-for-write
2469 'mime-charset)))
2470 (region
2471 (buffer-substring
2472 (if region-p (region-beginning) (point-min))
2473 (if region-p (region-end) (point-max))))
2474 (lines
2475 (org-split-string
2476 (org-export-preprocess-string
2477 region
2478 :emph-multiline t
2479 :for-html t
2480 :skip-before-1st-heading
2481 (plist-get opt-plist :skip-before-1st-heading)
2482 :drawers (plist-get opt-plist :drawers)
2483 :archived-trees
2484 (plist-get opt-plist :archived-trees)
2485 :add-text
2486 (plist-get opt-plist :text)
2487 :LaTeX-fragments
2488 (plist-get opt-plist :LaTeX-fragments))
2489 "[\r\n]"))
2490 table-open type
2491 table-buffer table-orig-buffer
2492 ind item-type starter didclose
2493 rpl path desc descp desc1 desc2 link
2494 snumber fnc item-tag
2497 (let ((inhibit-read-only t))
2498 (org-unmodified
2499 (remove-text-properties (point-min) (point-max)
2500 '(:org-license-to-kill t))))
2502 (message "Exporting...")
2504 (setq org-min-level (org-get-min-level lines))
2505 (setq org-last-level org-min-level)
2506 (org-init-section-numbers)
2508 (cond
2509 ((and date (string-match "%" date))
2510 (setq date (format-time-string date)))
2511 (date)
2512 (t (setq date (format-time-string "%Y/%m/%d %X"))))
2514 ;; Get the language-dependent settings
2515 (setq lang-words (or (assoc language org-export-language-setup)
2516 (assoc "en" org-export-language-setup)))
2518 ;; Switch to the output buffer
2519 (set-buffer buffer)
2520 (let ((inhibit-read-only t)) (erase-buffer))
2521 (fundamental-mode)
2523 (and (fboundp 'set-buffer-file-coding-system)
2524 (set-buffer-file-coding-system coding-system-for-write))
2526 (let ((case-fold-search nil)
2527 (org-odd-levels-only odd))
2528 ;; create local variables for all options, to make sure all called
2529 ;; functions get the correct information
2530 (mapc (lambda (x)
2531 (set (make-local-variable (cdr x))
2532 (plist-get opt-plist (car x))))
2533 org-export-plist-vars)
2534 (setq umax (if arg (prefix-numeric-value arg)
2535 org-export-headline-levels))
2536 (setq umax-toc (if (integerp org-export-with-toc)
2537 (min org-export-with-toc umax)
2538 umax))
2539 (unless body-only
2540 ;; File header
2541 (insert (format
2542 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
2543 \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
2544 <html xmlns=\"http://www.w3.org/1999/xhtml\"
2545 lang=\"%s\" xml:lang=\"%s\">
2546 <head>
2547 <title>%s</title>
2548 <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>
2549 <meta name=\"generator\" content=\"Org-mode\"/>
2550 <meta name=\"generated\" content=\"%s\"/>
2551 <meta name=\"author\" content=\"%s\"/>
2553 </head><body>
2555 language language (org-html-expand title)
2556 (or charset "iso-8859-1") date author style))
2558 (insert (or (plist-get opt-plist :preamble) ""))
2560 (when (plist-get opt-plist :auto-preamble)
2561 (if title (insert (format org-export-html-title-format
2562 (org-html-expand title))))))
2564 (if (and org-export-with-toc (not body-only))
2565 (progn
2566 (push (format "<h%d>%s</h%d>\n"
2567 org-export-html-toplevel-hlevel
2568 (nth 3 lang-words)
2569 org-export-html-toplevel-hlevel)
2570 thetoc)
2571 (push "<div id=\"text-table-of-contents\">\n" thetoc)
2572 (push "<ul>\n<li>" thetoc)
2573 (setq lines
2574 (mapcar '(lambda (line)
2575 (if (string-match org-todo-line-regexp line)
2576 ;; This is a headline
2577 (progn
2578 (setq have-headings t)
2579 (setq level (- (match-end 1) (match-beginning 1))
2580 level (org-tr-level level)
2581 txt (save-match-data
2582 (org-html-expand
2583 (org-export-cleanup-toc-line
2584 (match-string 3 line))))
2585 todo
2586 (or (and org-export-mark-todo-in-toc
2587 (match-beginning 2)
2588 (not (member (match-string 2 line)
2589 org-done-keywords)))
2590 ; TODO, not DONE
2591 (and org-export-mark-todo-in-toc
2592 (= level umax-toc)
2593 (org-search-todo-below
2594 line lines level))))
2595 (if (string-match
2596 (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") txt)
2597 (setq txt (replace-match "&nbsp;&nbsp;&nbsp;<span class=\"tag\"> \\1</span>" t nil txt)))
2598 (if (string-match quote-re0 txt)
2599 (setq txt (replace-match "" t t txt)))
2600 (setq snumber (org-section-number level))
2601 (if org-export-with-section-numbers
2602 (setq txt (concat snumber " " txt)))
2603 (if (<= level (max umax umax-toc))
2604 (setq head-count (+ head-count 1)))
2605 (if (<= level umax-toc)
2606 (progn
2607 (if (> level org-last-level)
2608 (progn
2609 (setq cnt (- level org-last-level))
2610 (while (>= (setq cnt (1- cnt)) 0)
2611 (push "\n<ul>\n<li>" thetoc))
2612 (push "\n" thetoc)))
2613 (if (< level org-last-level)
2614 (progn
2615 (setq cnt (- org-last-level level))
2616 (while (>= (setq cnt (1- cnt)) 0)
2617 (push "</li>\n</ul>" thetoc))
2618 (push "\n" thetoc)))
2619 ;; Check for targets
2620 (while (string-match org-any-target-regexp line)
2621 (setq line (replace-match
2622 (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
2623 t t line)))
2624 (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
2625 (setq txt (replace-match "" t t txt)))
2626 (push
2627 (format
2628 (if todo
2629 "</li>\n<li><a href=\"#sec-%s\"><span class=\"todo\">%s</span></a>"
2630 "</li>\n<li><a href=\"#sec-%s\">%s</a>")
2631 snumber txt) thetoc)
2633 (setq org-last-level level))
2635 line)
2636 lines))
2637 (while (> org-last-level (1- org-min-level))
2638 (setq org-last-level (1- org-last-level))
2639 (push "</li>\n</ul>\n" thetoc))
2640 (push "</div>\n" thetoc)
2641 (setq thetoc (if have-headings (nreverse thetoc) nil))))
2643 (setq head-count 0)
2644 (org-init-section-numbers)
2646 (while (setq line (pop lines) origline line)
2647 (catch 'nextline
2649 ;; end of quote section?
2650 (when (and inquote (string-match "^\\*+ " line))
2651 (insert "</pre>\n")
2652 (setq inquote nil))
2653 ;; inside a quote section?
2654 (when inquote
2655 (insert (org-html-protect line) "\n")
2656 (throw 'nextline nil))
2658 ;; verbatim lines
2659 (when (and org-export-with-fixed-width
2660 (string-match "^[ \t]*:\\(.*\\)" line))
2661 (when (not infixed)
2662 (setq infixed t)
2663 (insert "<pre>\n"))
2664 (insert (org-html-protect (match-string 1 line)) "\n")
2665 (when (and lines
2666 (not (string-match "^[ \t]*\\(:.*\\)"
2667 (car lines))))
2668 (setq infixed nil)
2669 (insert "</pre>\n"))
2670 (throw 'nextline nil))
2672 ;; Protected HTML
2673 (when (get-text-property 0 'org-protected line)
2674 (let (par)
2675 (when (re-search-backward
2676 "\\(<p>\\)\\([ \t\r\n]*\\)\\=" (- (point) 100) t)
2677 (setq par (match-string 1))
2678 (replace-match "\\2\n"))
2679 (insert line "\n")
2680 (while (and lines
2681 (not (string-match "^[ \t]*:" (car lines)))
2682 (or (= (length (car lines)) 0)
2683 (get-text-property 0 'org-protected (car lines))))
2684 (insert (pop lines) "\n"))
2685 (and par (insert "<p>\n")))
2686 (throw 'nextline nil))
2688 ;; Horizontal line
2689 (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line)
2690 (insert "\n<hr/>\n")
2691 (throw 'nextline nil))
2693 ;; Blockquotes and verse
2694 (when (equal "ORG-BLOCKQUOTE-START" line)
2695 (insert "<blockquote>\n<p>\n")
2696 (throw 'nextline nil))
2697 (when (equal "ORG-BLOCKQUOTE-END" line)
2698 (insert "</p>\n</blockquote>\n")
2699 (throw 'nextline nil))
2700 (when (equal "ORG-VERSE-START" line)
2701 (insert "<verse>\n<p>\n")
2702 (throw 'nextline nil))
2703 (when (equal "ORG-VERSE-END" line)
2704 (insert "</p>\n</verse>\n")
2705 (throw 'nextline nil))
2707 ;; make targets to anchors
2708 (while (string-match "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line)
2709 (cond
2710 ((match-end 2)
2711 (setq line (replace-match
2712 (concat "@<a name=\""
2713 (org-solidify-link-text (match-string 1 line))
2714 "\">\\nbsp@</a>")
2715 t t line)))
2716 ((and org-export-with-toc (equal (string-to-char line) ?*))
2717 ;; FIXME: NOT DEPENDENT on TOC?????????????????????
2718 (setq line (replace-match
2719 (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
2720 ; (concat "@<i>" (match-string 1 line) "@</i> ")
2721 t t line)))
2723 (setq line (replace-match
2724 (concat "@<a name=\""
2725 (org-solidify-link-text (match-string 1 line))
2726 "\" class=\"target\">" (match-string 1 line) "@</a> ")
2727 t t line)))))
2729 (setq line (org-html-handle-time-stamps line))
2731 ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
2732 ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
2733 ;; Also handle sub_superscripts and checkboxes
2734 (or (string-match org-table-hline-regexp line)
2735 (setq line (org-html-expand line)))
2737 ;; Format the links
2738 (setq start 0)
2739 (while (string-match org-bracket-link-analytic-regexp line start)
2740 (setq start (match-beginning 0))
2741 (setq type (if (match-end 2) (match-string 2 line) "internal"))
2742 (setq path (match-string 3 line))
2743 (setq desc1 (if (match-end 5) (match-string 5 line))
2744 desc2 (if (match-end 2) (concat type ":" path) path)
2745 descp (and desc1 (not (equal desc1 desc2)))
2746 desc (or desc1 desc2))
2747 ;; Make an image out of the description if that is so wanted
2748 (when (and descp (org-file-image-p desc))
2749 (save-match-data
2750 (if (string-match "^file:" desc)
2751 (setq desc (substring desc (match-end 0)))))
2752 (setq desc (concat "<img src=\"" desc "\"/>")))
2753 ;; FIXME: do we need to unescape here somewhere?
2754 (cond
2755 ((equal type "internal")
2756 (setq rpl
2757 (concat
2758 "<a href=\"#"
2759 (org-solidify-link-text
2760 (save-match-data (org-link-unescape path)) nil)
2761 "\">" desc "</a>")))
2762 ((member type '("http" "https"))
2763 ;; standard URL, just check if we need to inline an image
2764 (if (and (or (eq t org-export-html-inline-images)
2765 (and org-export-html-inline-images (not descp)))
2766 (org-file-image-p path))
2767 (setq rpl (concat "<img src=\"" type ":" path "\"/>"))
2768 (setq link (concat type ":" path))
2769 (setq rpl (concat "<a href=\"" link "\">" desc "</a>"))))
2770 ((member type '("ftp" "mailto" "news"))
2771 ;; standard URL
2772 (setq link (concat type ":" path))
2773 (setq rpl (concat "<a href=\"" link "\">" desc "</a>")))
2774 ((string= type "file")
2775 ;; FILE link
2776 (let* ((filename path)
2777 (abs-p (file-name-absolute-p filename))
2778 thefile file-is-image-p search)
2779 (save-match-data
2780 (if (string-match "::\\(.*\\)" filename)
2781 (setq search (match-string 1 filename)
2782 filename (replace-match "" t nil filename)))
2783 (setq valid
2784 (if (functionp link-validate)
2785 (funcall link-validate filename current-dir)
2787 (setq file-is-image-p (org-file-image-p filename))
2788 (setq thefile (if abs-p (expand-file-name filename) filename))
2789 (when (and org-export-html-link-org-files-as-html
2790 (string-match "\\.org$" thefile))
2791 (setq thefile (concat (substring thefile 0
2792 (match-beginning 0))
2793 "." html-extension))
2794 (if (and search
2795 ;; make sure this is can be used as target search
2796 (not (string-match "^[0-9]*$" search))
2797 (not (string-match "^\\*" search))
2798 (not (string-match "^/.*/$" search)))
2799 (setq thefile (concat thefile "#"
2800 (org-solidify-link-text
2801 (org-link-unescape search)))))
2802 (when (string-match "^file:" desc)
2803 (setq desc (replace-match "" t t desc))
2804 (if (string-match "\\.org$" desc)
2805 (setq desc (replace-match "" t t desc))))))
2806 (setq rpl (if (and file-is-image-p
2807 (or (eq t org-export-html-inline-images)
2808 (and org-export-html-inline-images
2809 (not descp))))
2810 (concat "<img src=\"" thefile "\"/>")
2811 (concat "<a href=\"" thefile "\">" desc "</a>")))
2812 (if (not valid) (setq rpl desc))))
2814 ((functionp (setq fnc (nth 2 (assoc type org-link-protocols))))
2815 (setq rpl
2816 (save-match-data
2817 (funcall fnc (org-link-unescape path) desc1 'html))))
2820 ;; just publish the path, as default
2821 (setq rpl (concat "<i>&lt;" type ":"
2822 (save-match-data (org-link-unescape path))
2823 "&gt;</i>"))))
2824 (setq line (replace-match rpl t t line)
2825 start (+ start (length rpl))))
2827 ;; TODO items
2828 (if (and (string-match org-todo-line-regexp line)
2829 (match-beginning 2))
2831 (setq line
2832 (concat (substring line 0 (match-beginning 2))
2833 "<span class=\""
2834 (if (member (match-string 2 line)
2835 org-done-keywords)
2836 "done" "todo")
2837 "\">" (match-string 2 line)
2838 "</span>" (substring line (match-end 2)))))
2840 ;; Does this contain a reference to a footnote?
2841 (when org-export-with-footnotes
2842 (setq start 0)
2843 (while (string-match "\\([^* \t].*?\\)\\[\\([0-9]+\\)\\]" line start)
2844 (if (get-text-property (match-beginning 2) 'org-protected line)
2845 (setq start (match-end 2))
2846 (let ((n (match-string 2 line)))
2847 (setq line
2848 (replace-match
2849 (format
2850 "%s<sup><a class=\"footref\" name=\"fnr.%s\" href=\"#fn.%s\">%s</a></sup>"
2851 (match-string 1 line) n n n)
2852 t t line))))))
2854 (cond
2855 ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
2856 ;; This is a headline
2857 (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
2858 txt (match-string 2 line))
2859 (if (string-match quote-re0 txt)
2860 (setq txt (replace-match "" t t txt)))
2861 (if (<= level (max umax umax-toc))
2862 (setq head-count (+ head-count 1)))
2863 (when in-local-list
2864 ;; Close any local lists before inserting a new header line
2865 (while local-list-type
2866 (org-close-li (car local-list-type))
2867 (insert (format "</%sl>\n" (car local-list-type)))
2868 (pop local-list-type))
2869 (setq local-list-indent nil
2870 in-local-list nil))
2871 (setq first-heading-pos (or first-heading-pos (point)))
2872 (org-html-level-start level txt umax
2873 (and org-export-with-toc (<= level umax))
2874 head-count)
2875 ;; QUOTES
2876 (when (string-match quote-re line)
2877 (insert "<pre>")
2878 (setq inquote t)))
2880 ((and org-export-with-tables
2881 (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
2882 (if (not table-open)
2883 ;; New table starts
2884 (setq table-open t table-buffer nil table-orig-buffer nil))
2885 ;; Accumulate lines
2886 (setq table-buffer (cons line table-buffer)
2887 table-orig-buffer (cons origline table-orig-buffer))
2888 (when (or (not lines)
2889 (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
2890 (car lines))))
2891 (setq table-open nil
2892 table-buffer (nreverse table-buffer)
2893 table-orig-buffer (nreverse table-orig-buffer))
2894 (org-close-par-maybe)
2895 (insert (org-format-table-html table-buffer table-orig-buffer))))
2897 ;; Normal lines
2898 (when (string-match
2899 (cond
2900 ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
2901 ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
2902 ((= llt ?\)) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
2903 (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
2904 line)
2905 (setq ind (org-get-string-indentation line)
2906 item-type (if (match-beginning 4) "o" "u")
2907 starter (if (match-beginning 2)
2908 (substring (match-string 2 line) 0 -1))
2909 line (substring line (match-beginning 5))
2910 item-tag nil)
2911 (if (and starter (string-match "\\(.*?\\) ::[ \t]*" line))
2912 (setq item-type "d"
2913 item-tag (match-string 1 line)
2914 line (substring line (match-end 0))))
2915 (when (and (not (equal item-type "d"))
2916 (not (string-match "[^ \t]" line)))
2917 ;; empty line. Pretend indentation is large.
2918 (setq ind (if org-empty-line-terminates-plain-lists
2920 (1+ (or (car local-list-indent) 1)))))
2921 (setq didclose nil)
2922 (while (and in-local-list
2923 (or (and (= ind (car local-list-indent))
2924 (not starter))
2925 (< ind (car local-list-indent))))
2926 (setq didclose t)
2927 (org-close-li (car local-list-type))
2928 (insert (format "</%sl>\n" (car local-list-type)))
2929 (pop local-list-type) (pop local-list-indent)
2930 (setq in-local-list local-list-indent))
2931 (cond
2932 ((and starter
2933 (or (not in-local-list)
2934 (> ind (car local-list-indent))))
2935 ;; Start new (level of) list
2936 (org-close-par-maybe)
2937 (insert (cond
2938 ((equal item-type "u") "<ul>\n<li>\n")
2939 ((equal item-type "o") "<ol>\n<li>\n")
2940 ((equal item-type "d")
2941 (format "<dl>\n<dt>%s</dt><dd>\n" item-tag))))
2942 (push item-type local-list-type)
2943 (push ind local-list-indent)
2944 (setq in-local-list t))
2945 (starter
2946 ;; continue current list
2947 (org-close-li (car local-list-type))
2948 (insert (cond
2949 ((equal (car local-list-type) "d")
2950 (format "<dt>%s</dt><dd>\n" (or item-tag "???")))
2951 (t "<li>\n"))))
2952 (didclose
2953 ;; we did close a list, normal text follows: need <p>
2954 (org-open-par)))
2955 (if (string-match "^[ \t]*\\[\\([X ]\\)\\]" line)
2956 (setq line
2957 (replace-match
2958 (if (equal (match-string 1 line) "X")
2959 "<b>[X]</b>"
2960 "<b>[<span style=\"visibility:hidden;\">X</span>]</b>")
2961 t t line))))
2963 ;; Empty lines start a new paragraph. If hand-formatted lists
2964 ;; are not fully interpreted, lines starting with "-", "+", "*"
2965 ;; also start a new paragraph.
2966 (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (org-open-par))
2968 ;; Is this the start of a footnote?
2969 (when org-export-with-footnotes
2970 (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
2971 (org-close-par-maybe)
2972 (let ((n (match-string 1 line)))
2973 (setq line (replace-match
2974 (format "<p class=\"footnote\"><sup><a class=\"footnum\" name=\"fn.%s\" href=\"#fnr.%s\">%s</a></sup>" n n n) t t line))
2975 (setq line (concat line "</p>")))))
2977 ;; Check if the line break needs to be conserved
2978 (cond
2979 ((string-match "\\\\\\\\[ \t]*$" line)
2980 (setq line (replace-match "<br/>" t t line)))
2981 (org-export-preserve-breaks
2982 (setq line (concat line "<br/>"))))
2984 (insert line "\n")))))
2986 ;; Properly close all local lists and other lists
2987 (when inquote (insert "</pre>\n"))
2988 (when in-local-list
2989 ;; Close any local lists before inserting a new header line
2990 (while local-list-type
2991 (org-close-li (car local-list-type))
2992 (insert (format "</%sl>\n" (car local-list-type)))
2993 (pop local-list-type))
2994 (setq local-list-indent nil
2995 in-local-list nil))
2996 (org-html-level-start 1 nil umax
2997 (and org-export-with-toc (<= level umax))
2998 head-count)
2999 ;; the </div> to lose the last text-... div.
3000 (insert "</div>\n")
3002 (unless body-only
3003 (when (plist-get opt-plist :auto-postamble)
3004 (insert "<div id=\"postamble\">")
3005 (when (and org-export-author-info author)
3006 (insert "<p class=\"author\"> "
3007 (nth 1 lang-words) ": " author "\n")
3008 (when email
3009 (if (listp (split-string email ",+ *"))
3010 (mapc (lambda(e)
3011 (insert "<a href=\"mailto:" e "\">&lt;"
3012 e "&gt;</a>\n"))
3013 (split-string email ",+ *"))
3014 (insert "<a href=\"mailto:" email "\">&lt;"
3015 email "&gt;</a>\n")))
3016 (insert "</p>\n"))
3017 (when (and date org-export-time-stamp-file)
3018 (insert "<p class=\"date\"> "
3019 (nth 2 lang-words) ": "
3020 date "</p>\n"))
3021 (insert "</div>"))
3023 (if org-export-html-with-timestamp
3024 (insert org-export-html-html-helper-timestamp))
3025 (insert (or (plist-get opt-plist :postamble) ""))
3026 (insert "</body>\n</html>\n"))
3028 (normal-mode)
3029 (if (eq major-mode default-major-mode) (html-mode))
3031 ;; insert the table of contents
3032 (goto-char (point-min))
3033 (when thetoc
3034 (if (or (re-search-forward
3035 "<p>\\s-*\\[TABLE-OF-CONTENTS\\]\\s-*</p>" nil t)
3036 (re-search-forward
3037 "\\[TABLE-OF-CONTENTS\\]" nil t))
3038 (progn
3039 (goto-char (match-beginning 0))
3040 (replace-match ""))
3041 (goto-char first-heading-pos)
3042 (when (looking-at "\\s-*</p>")
3043 (goto-char (match-end 0))
3044 (insert "\n")))
3045 (insert "<div id=\"table-of-contents\">\n")
3046 (mapc 'insert thetoc)
3047 (insert "</div>\n"))
3048 ;; remove empty paragraphs and lists
3049 (goto-char (point-min))
3050 (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t)
3051 (replace-match ""))
3052 (goto-char (point-min))
3053 (while (re-search-forward "<li>[ \r\n\t]*</li>\n?" nil t)
3054 (replace-match ""))
3055 (goto-char (point-min))
3056 (while (re-search-forward "</ul>\\s-*<ul>\n?" nil t)
3057 (replace-match ""))
3058 ;; Convert whitespace place holders
3059 (goto-char (point-min))
3060 (let (beg end n)
3061 (while (setq beg (next-single-property-change (point) 'org-whitespace))
3062 (setq n (get-text-property beg 'org-whitespace)
3063 end (next-single-property-change beg 'org-whitespace))
3064 (goto-char beg)
3065 (delete-region beg end)
3066 (insert (format "<span style=\"visibility:hidden;\">%s</span>"
3067 (make-string n ?x)))))
3068 (or to-buffer (save-buffer))
3069 (goto-char (point-min))
3070 (message "Exporting... done")
3071 (if (eq to-buffer 'string)
3072 (prog1 (buffer-substring (point-min) (point-max))
3073 (kill-buffer (current-buffer)))
3074 (current-buffer)))))
3076 (defvar org-table-colgroup-info nil)
3077 (defun org-format-table-ascii (lines)
3078 "Format a table for ascii export."
3079 (if (stringp lines)
3080 (setq lines (org-split-string lines "\n")))
3081 (if (not (string-match "^[ \t]*|" (car lines)))
3082 ;; Table made by table.el - test for spanning
3083 lines
3085 ;; A normal org table
3086 ;; Get rid of hlines at beginning and end
3087 (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
3088 (setq lines (nreverse lines))
3089 (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
3090 (setq lines (nreverse lines))
3091 (when org-export-table-remove-special-lines
3092 ;; Check if the table has a marking column. If yes remove the
3093 ;; column and the special lines
3094 (setq lines (org-table-clean-before-export lines)))
3095 ;; Get rid of the vertical lines except for grouping
3096 (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info))
3097 rtn line vl1 start)
3098 (while (setq line (pop lines))
3099 (if (string-match org-table-hline-regexp line)
3100 (and (string-match "|\\(.*\\)|" line)
3101 (setq line (replace-match " \\1" t nil line)))
3102 (setq start 0 vl1 vl)
3103 (while (string-match "|" line start)
3104 (setq start (match-end 0))
3105 (or (pop vl1) (setq line (replace-match " " t t line)))))
3106 (push line rtn))
3107 (nreverse rtn))))
3109 (defun org-colgroup-info-to-vline-list (info)
3110 (let (vl new last)
3111 (while info
3112 (setq last new new (pop info))
3113 (if (or (memq last '(:end :startend))
3114 (memq new '(:start :startend)))
3115 (push t vl)
3116 (push nil vl)))
3117 (setq vl (nreverse vl))
3118 (and vl (setcar vl nil))
3119 vl))
3121 (defvar org-table-number-regexp) ; defined in org-table.el
3122 (defun org-format-table-html (lines olines)
3123 "Find out which HTML converter to use and return the HTML code."
3124 (if (stringp lines)
3125 (setq lines (org-split-string lines "\n")))
3126 (if (string-match "^[ \t]*|" (car lines))
3127 ;; A normal org table
3128 (org-format-org-table-html lines)
3129 ;; Table made by table.el - test for spanning
3130 (let* ((hlines (delq nil (mapcar
3131 (lambda (x)
3132 (if (string-match "^[ \t]*\\+-" x) x
3133 nil))
3134 lines)))
3135 (first (car hlines))
3136 (ll (and (string-match "\\S-+" first)
3137 (match-string 0 first)))
3138 (re (concat "^[ \t]*" (regexp-quote ll)))
3139 (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
3140 hlines))))
3141 (if (and (not spanning)
3142 (not org-export-prefer-native-exporter-for-tables))
3143 ;; We can use my own converter with HTML conversions
3144 (org-format-table-table-html lines)
3145 ;; Need to use the code generator in table.el, with the original text.
3146 (org-format-table-table-html-using-table-generate-source olines)))))
3148 (defvar org-table-number-fraction) ; defined in org-table.el
3149 (defun org-format-org-table-html (lines &optional splice)
3150 "Format a table into HTML."
3151 (require 'org-table)
3152 ;; Get rid of hlines at beginning and end
3153 (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
3154 (setq lines (nreverse lines))
3155 (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
3156 (setq lines (nreverse lines))
3157 (when org-export-table-remove-special-lines
3158 ;; Check if the table has a marking column. If yes remove the
3159 ;; column and the special lines
3160 (setq lines (org-table-clean-before-export lines)))
3162 (let ((head (and org-export-highlight-first-table-line
3163 (delq nil (mapcar
3164 (lambda (x) (string-match "^[ \t]*|-" x))
3165 (cdr lines)))))
3166 (nlines 0) fnum i
3167 tbopen line fields html gr colgropen)
3168 (if splice (setq head nil))
3169 (unless splice (push (if head "<thead>" "<tbody>") html))
3170 (setq tbopen t)
3171 (while (setq line (pop lines))
3172 (catch 'next-line
3173 (if (string-match "^[ \t]*|-" line)
3174 (progn
3175 (unless splice
3176 (push (if head "</thead>" "</tbody>") html)
3177 (if lines (push "<tbody>" html) (setq tbopen nil)))
3178 (setq head nil) ;; head ends here, first time around
3179 ;; ignore this line
3180 (throw 'next-line t)))
3181 ;; Break the line into fields
3182 (setq fields (org-split-string line "[ \t]*|[ \t]*"))
3183 (unless fnum (setq fnum (make-vector (length fields) 0)))
3184 (setq nlines (1+ nlines) i -1)
3185 (push (concat "<tr>"
3186 (mapconcat
3187 (lambda (x)
3188 (setq i (1+ i))
3189 (if (and (< i nlines)
3190 (string-match org-table-number-regexp x))
3191 (incf (aref fnum i)))
3192 (if head
3193 (concat (car org-export-table-header-tags) x
3194 (cdr org-export-table-header-tags))
3195 (concat (car org-export-table-data-tags) x
3196 (cdr org-export-table-data-tags))))
3197 fields "")
3198 "</tr>")
3199 html)))
3200 (unless splice (if tbopen (push "</tbody>" html)))
3201 (unless splice (push "</table>\n" html))
3202 (setq html (nreverse html))
3203 (unless splice
3204 ;; Put in col tags with the alignment (unfortuntely often ignored...)
3205 (push (mapconcat
3206 (lambda (x)
3207 (setq gr (pop org-table-colgroup-info))
3208 (format "%s<col align=\"%s\"></col>%s"
3209 (if (memq gr '(:start :startend))
3210 (prog1
3211 (if colgropen "</colgroup>\n<colgroup>" "<colgroup>")
3212 (setq colgropen t))
3214 (if (> (/ (float x) nlines) org-table-number-fraction)
3215 "right" "left")
3216 (if (memq gr '(:end :startend))
3217 (progn (setq colgropen nil) "</colgroup>")
3218 "")))
3219 fnum "")
3220 html)
3221 (if colgropen (setq html (cons (car html) (cons "</colgroup>" (cdr html)))))
3222 (push html-table-tag html))
3223 (concat (mapconcat 'identity html "\n") "\n")))
3225 (defun org-table-clean-before-export (lines)
3226 "Check if the table has a marking column.
3227 If yes remove the column and the special lines."
3228 (setq org-table-colgroup-info nil)
3229 (if (memq nil
3230 (mapcar
3231 (lambda (x) (or (string-match "^[ \t]*|-" x)
3232 (string-match "^[ \t]*| *\\([#!$*_^ /]\\) *|" x)))
3233 lines))
3234 (progn
3235 (setq org-table-clean-did-remove-column nil)
3236 (delq nil
3237 (mapcar
3238 (lambda (x)
3239 (cond
3240 ((string-match "^[ \t]*| */ *|" x)
3241 (setq org-table-colgroup-info
3242 (mapcar (lambda (x)
3243 (cond ((member x '("<" "&lt;")) :start)
3244 ((member x '(">" "&gt;")) :end)
3245 ((member x '("<>" "&lt;&gt;")) :startend)
3246 (t nil)))
3247 (org-split-string x "[ \t]*|[ \t]*")))
3248 nil)
3249 (t x)))
3250 lines)))
3251 (setq org-table-clean-did-remove-column t)
3252 (delq nil
3253 (mapcar
3254 (lambda (x)
3255 (cond
3256 ((string-match "^[ \t]*| */ *|" x)
3257 (setq org-table-colgroup-info
3258 (mapcar (lambda (x)
3259 (cond ((member x '("<" "&lt;")) :start)
3260 ((member x '(">" "&gt;")) :end)
3261 ((member x '("<>" "&lt;&gt;")) :startend)
3262 (t nil)))
3263 (cdr (org-split-string x "[ \t]*|[ \t]*"))))
3264 nil)
3265 ((string-match "^[ \t]*| *[!_^/] *|" x)
3266 nil) ; ignore this line
3267 ((or (string-match "^\\([ \t]*\\)|-+\\+" x)
3268 (string-match "^\\([ \t]*\\)|[^|]*|" x))
3269 ;; remove the first column
3270 (replace-match "\\1|" t nil x))))
3271 lines))))
3273 (defun org-format-table-table-html (lines)
3274 "Format a table generated by table.el into HTML.
3275 This conversion does *not* use `table-generate-source' from table.el.
3276 This has the advantage that Org-mode's HTML conversions can be used.
3277 But it has the disadvantage, that no cell- or row-spanning is allowed."
3278 (let (line field-buffer
3279 (head org-export-highlight-first-table-line)
3280 fields html empty)
3281 (setq html (concat html-table-tag "\n"))
3282 (while (setq line (pop lines))
3283 (setq empty "&nbsp;")
3284 (catch 'next-line
3285 (if (string-match "^[ \t]*\\+-" line)
3286 (progn
3287 (if field-buffer
3288 (progn
3289 (setq
3290 html
3291 (concat
3292 html
3293 "<tr>"
3294 (mapconcat
3295 (lambda (x)
3296 (if (equal x "") (setq x empty))
3297 (if head
3298 (concat (car org-export-table-header-tags) x
3299 (cdr org-export-table-header-tags))
3300 (concat (car org-export-table-data-tags) x
3301 (cdr org-export-table-data-tags))))
3302 field-buffer "\n")
3303 "</tr>\n"))
3304 (setq head nil)
3305 (setq field-buffer nil)))
3306 ;; Ignore this line
3307 (throw 'next-line t)))
3308 ;; Break the line into fields and store the fields
3309 (setq fields (org-split-string line "[ \t]*|[ \t]*"))
3310 (if field-buffer
3311 (setq field-buffer (mapcar
3312 (lambda (x)
3313 (concat x "<br/>" (pop fields)))
3314 field-buffer))
3315 (setq field-buffer fields))))
3316 (setq html (concat html "</table>\n"))
3317 html))
3319 (defun org-format-table-table-html-using-table-generate-source (lines)
3320 "Format a table into html, using `table-generate-source' from table.el.
3321 This has the advantage that cell- or row-spanning is allowed.
3322 But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
3323 (require 'table)
3324 (with-current-buffer (get-buffer-create " org-tmp1 ")
3325 (erase-buffer)
3326 (insert (mapconcat 'identity lines "\n"))
3327 (goto-char (point-min))
3328 (if (not (re-search-forward "|[^+]" nil t))
3329 (error "Error processing table"))
3330 (table-recognize-table)
3331 (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
3332 (table-generate-source 'html " org-tmp2 ")
3333 (set-buffer " org-tmp2 ")
3334 (buffer-substring (point-min) (point-max))))
3336 (defun org-html-handle-time-stamps (s)
3337 "Format time stamps in string S, or remove them."
3338 (catch 'exit
3339 (let (r b)
3340 (while (string-match org-maybe-keyword-time-regexp s)
3341 (if (and (match-end 1) (equal (match-string 1 s) org-clock-string))
3342 ;; never export CLOCK
3343 (throw 'exit ""))
3344 (or b (setq b (substring s 0 (match-beginning 0))))
3345 (if (not org-export-with-timestamps)
3346 (setq r (concat r (substring s 0 (match-beginning 0)))
3347 s (substring s (match-end 0)))
3348 (setq r (concat
3349 r (substring s 0 (match-beginning 0))
3350 (if (match-end 1)
3351 (format "@<span class=\"timestamp-kwd\">%s @</span>"
3352 (match-string 1 s)))
3353 (format " @<span class=\"timestamp\">%s@</span>"
3354 (substring
3355 (org-translate-time (match-string 3 s)) 1 -1)))
3356 s (substring s (match-end 0)))))
3357 ;; Line break if line started and ended with time stamp stuff
3358 (if (not r)
3360 (setq r (concat r s))
3361 (unless (string-match "\\S-" (concat b s))
3362 (setq r (concat r "@<br/>")))
3363 r))))
3365 (defun org-export-htmlize-region-for-paste (beg end)
3366 "Convert the region to HTML, using htmlize.el.
3367 This is much like `htmlize-region-for-paste', only that it uses
3368 the settings define in the org-... variables."
3369 (let* ((htmlize-output-type org-export-htmlize-output-type)
3370 (htmlize-css-name-prefix org-export-htmlize-css-font-prefix)
3371 (htmlbuf (htmlize-region beg end)))
3372 (unwind-protect
3373 (with-current-buffer htmlbuf
3374 (buffer-substring (plist-get htmlize-buffer-places 'content-start)
3375 (plist-get htmlize-buffer-places 'content-end)))
3376 (kill-buffer htmlbuf))))
3378 ;;;###autoload
3379 (defun org-export-htmlize-generate-css ()
3380 "Create the CSS for all font definitions in the current Emacs session.
3381 Use this to create face definitions in your CSS style file that can then
3382 be used by code snippets transformed by htmlize.
3383 This command just produces a buffer that contains class definitions for all
3384 faces used in the current Emacs session. You can copy and paste the ones you
3385 need into your CSS file.
3387 If you then set `org-export-htmlize-output-type' to `css', calls to
3388 the function `org-export-htmlize-region-for-paste' will produce code
3389 that uses these same face definitions."
3390 (interactive)
3391 (require 'htmlize)
3392 (and (get-buffer "*html*") (kill-buffer "*html*"))
3393 (with-temp-buffer
3394 (let ((fl (face-list))
3395 (htmlize-css-name-prefix "org-")
3396 (htmlize-output-type 'css)
3397 f i)
3398 (while (setq f (pop fl)
3399 i (and f (face-attribute f :inherit)))
3400 (when (and (symbolp f) (or (not i) (not (listp i))))
3401 (insert (org-add-props (copy-sequence "1") nil 'face f))))
3402 (htmlize-region (point-min) (point-max))))
3403 (switch-to-buffer "*html*")
3404 (goto-char (point-min))
3405 (if (re-search-forward "<style" nil t)
3406 (delete-region (point-min) (match-beginning 0)))
3407 (if (re-search-forward "</style>" nil t)
3408 (delete-region (1+ (match-end 0)) (point-max)))
3409 (beginning-of-line 1)
3410 (if (looking-at " +") (replace-match ""))
3411 (goto-char (point-min)))
3413 (defun org-html-protect (s)
3414 ;; convert & to &amp;, < to &lt; and > to &gt;
3415 (let ((start 0))
3416 (while (string-match "&" s start)
3417 (setq s (replace-match "&amp;" t t s)
3418 start (1+ (match-beginning 0))))
3419 (while (string-match "<" s)
3420 (setq s (replace-match "&lt;" t t s)))
3421 (while (string-match ">" s)
3422 (setq s (replace-match "&gt;" t t s))))
3425 (defun org-export-cleanup-toc-line (s)
3426 "Remove tags and time staps from lines going into the toc."
3427 (when (memq org-export-with-tags '(not-in-toc nil))
3428 (if (string-match (org-re " +:[[:alnum:]_@:]+: *$") s)
3429 (setq s (replace-match "" t t s))))
3430 (when org-export-remove-timestamps-from-toc
3431 (while (string-match org-maybe-keyword-time-regexp s)
3432 (setq s (replace-match "" t t s))))
3433 (while (string-match org-bracket-link-regexp s)
3434 (setq s (replace-match (match-string (if (match-end 3) 3 1) s)
3435 t t s)))
3438 (defun org-html-expand (string)
3439 "Prepare STRING for HTML export. Applies all active conversions.
3440 If there are links in the string, don't modify these."
3441 (let* ((re (concat org-bracket-link-regexp "\\|"
3442 (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$")))
3443 m s l res)
3444 (while (setq m (string-match re string))
3445 (setq s (substring string 0 m)
3446 l (match-string 0 string)
3447 string (substring string (match-end 0)))
3448 (push (org-html-do-expand s) res)
3449 (push l res))
3450 (push (org-html-do-expand string) res)
3451 (apply 'concat (nreverse res))))
3453 (defun org-html-do-expand (s)
3454 "Apply all active conversions to translate special ASCII to HTML."
3455 (setq s (org-html-protect s))
3456 (if org-export-html-expand
3457 (let ((start 0))
3458 (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
3459 (setq s (replace-match "<\\1>" t nil s)))))
3460 (if org-export-with-emphasize
3461 (setq s (org-export-html-convert-emphasize s)))
3462 (if org-export-with-special-strings
3463 (setq s (org-export-html-convert-special-strings s)))
3464 (if org-export-with-sub-superscripts
3465 (setq s (org-export-html-convert-sub-super s)))
3466 (if org-export-with-TeX-macros
3467 (let ((start 0) wd ass)
3468 (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)" s start))
3469 (if (get-text-property (match-beginning 0) 'org-protected s)
3470 (setq start (match-end 0))
3471 (setq wd (match-string 1 s))
3472 (if (setq ass (assoc wd org-html-entities))
3473 (setq s (replace-match (or (cdr ass)
3474 (concat "&" (car ass) ";"))
3475 t t s))
3476 (setq start (+ start (length wd))))))))
3479 (defun org-create-multibrace-regexp (left right n)
3480 "Create a regular expression which will match a balanced sexp.
3481 Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
3482 as single character strings.
3483 The regexp returned will match the entire expression including the
3484 delimiters. It will also define a single group which contains the
3485 match except for the outermost delimiters. The maximum depth of
3486 stacked delimiters is N. Escaping delimiters is not possible."
3487 (let* ((nothing (concat "[^" "\\" left "\\" right "]*?"))
3488 (or "\\|")
3489 (re nothing)
3490 (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
3491 (while (> n 1)
3492 (setq n (1- n)
3493 re (concat re or next)
3494 next (concat "\\(?:" nothing left next right "\\)+" nothing)))
3495 (concat left "\\(" re "\\)" right)))
3497 (defvar org-match-substring-regexp
3498 (concat
3499 "\\([^\\]\\)\\([_^]\\)\\("
3500 "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
3501 "\\|"
3502 "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
3503 "\\|"
3504 "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
3505 "The regular expression matching a sub- or superscript.")
3507 (defvar org-match-substring-with-braces-regexp
3508 (concat
3509 "\\([^\\]\\)\\([_^]\\)\\("
3510 "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
3511 "\\)")
3512 "The regular expression matching a sub- or superscript, forcing braces.")
3514 (defconst org-export-html-special-string-regexps
3515 '(("\\\\-" . "&shy;")
3516 ("---\\([^-]\\)" . "&mdash;\\1")
3517 ("--\\([^-]\\)" . "&ndash;\\1")
3518 ("\\.\\.\\." . "&hellip;"))
3519 "Regular expressions for special string conversion.")
3521 (defun org-export-html-convert-special-strings (string)
3522 "Convert special characters in STRING to HTML."
3523 (let ((all org-export-html-special-string-regexps)
3524 e a re rpl start)
3525 (while (setq a (pop all))
3526 (setq re (car a) rpl (cdr a) start 0)
3527 (while (string-match re string start)
3528 (if (get-text-property (match-beginning 0) 'org-protected string)
3529 (setq start (match-end 0))
3530 (setq string (replace-match rpl t nil string)))))
3531 string))
3533 (defun org-export-html-convert-sub-super (string)
3534 "Convert sub- and superscripts in STRING to HTML."
3535 (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
3536 (while (string-match org-match-substring-regexp string s)
3537 (cond
3538 ((and requireb (match-end 8)) (setq s (match-end 2)))
3539 ((get-text-property (match-beginning 2) 'org-protected string)
3540 (setq s (match-end 2)))
3542 (setq s (match-end 1)
3543 key (if (string= (match-string 2 string) "_") "sub" "sup")
3544 c (or (match-string 8 string)
3545 (match-string 6 string)
3546 (match-string 5 string))
3547 string (replace-match
3548 (concat (match-string 1 string)
3549 "<" key ">" c "</" key ">")
3550 t t string)))))
3551 (while (string-match "\\\\\\([_^]\\)" string)
3552 (setq string (replace-match (match-string 1 string) t t string)))
3553 string))
3555 (defun org-export-html-convert-emphasize (string)
3556 "Apply emphasis."
3557 (let ((s 0) rpl)
3558 (while (string-match org-emph-re string s)
3559 (if (not (equal
3560 (substring string (match-beginning 3) (1+ (match-beginning 3)))
3561 (substring string (match-beginning 4) (1+ (match-beginning 4)))))
3562 (setq s (match-beginning 0)
3564 (concat
3565 (match-string 1 string)
3566 (nth 2 (assoc (match-string 3 string) org-emphasis-alist))
3567 (match-string 4 string)
3568 (nth 3 (assoc (match-string 3 string)
3569 org-emphasis-alist))
3570 (match-string 5 string))
3571 string (replace-match rpl t t string)
3572 s (+ s (- (length rpl) 2)))
3573 (setq s (1+ s))))
3574 string))
3576 (defvar org-par-open nil)
3577 (defun org-open-par ()
3578 "Insert <p>, but first close previous paragraph if any."
3579 (org-close-par-maybe)
3580 (insert "\n<p>")
3581 (setq org-par-open t))
3582 (defun org-close-par-maybe ()
3583 "Close paragraph if there is one open."
3584 (when org-par-open
3585 (insert "</p>")
3586 (setq org-par-open nil)))
3587 (defun org-close-li (&optional type)
3588 "Close <li> if necessary."
3589 (org-close-par-maybe)
3590 (insert (if (equal type "d") "</dd>\n" "</li>\n")))
3592 (defvar body-only) ; dynamically scoped into this.
3593 (defun org-html-level-start (level title umax with-toc head-count)
3594 "Insert a new level in HTML export.
3595 When TITLE is nil, just close all open levels."
3596 (org-close-par-maybe)
3597 (let ((target (and title (org-get-text-property-any 0 'target title)))
3598 (l org-level-max)
3599 snumber)
3600 (while (>= l level)
3601 (if (aref org-levels-open (1- l))
3602 (progn
3603 (org-html-level-close l umax)
3604 (aset org-levels-open (1- l) nil)))
3605 (setq l (1- l)))
3606 (when title
3607 ;; If title is nil, this means this function is called to close
3608 ;; all levels, so the rest is done only if title is given
3609 (when (string-match (org-re "\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
3610 (setq title (replace-match
3611 (if org-export-with-tags
3612 (save-match-data
3613 (concat
3614 "&nbsp;&nbsp;&nbsp;<span class=\"tag\">"
3615 (mapconcat 'identity (org-split-string
3616 (match-string 1 title) ":")
3617 "&nbsp;")
3618 "</span>"))
3620 t t title)))
3621 (if (> level umax)
3622 (progn
3623 (if (aref org-levels-open (1- level))
3624 (progn
3625 (org-close-li)
3626 (if target
3627 (insert (format "<li id=\"%s\">" target) title "<br/>\n")
3628 (insert "<li>" title "<br/>\n")))
3629 (aset org-levels-open (1- level) t)
3630 (org-close-par-maybe)
3631 (if target
3632 (insert (format "<ul>\n<li id=\"%s\">" target)
3633 title "<br/>\n")
3634 (insert "<ul>\n<li>" title "<br/>\n"))))
3635 (aset org-levels-open (1- level) t)
3636 (setq snumber (org-section-number level))
3637 (if (and org-export-with-section-numbers (not body-only))
3638 (setq title (concat snumber " " title)))
3639 (setq level (+ level org-export-html-toplevel-hlevel -1))
3640 (unless (= head-count 1) (insert "\n</div>\n"))
3641 (insert (format "\n<div id=\"outline-container-%s\" class=\"outline-%d\">\n<h%d id=\"sec-%s\">%s</h%d>\n<div id=\"text-%s\">\n"
3642 snumber level level snumber title level snumber))
3643 (org-open-par)))))
3645 (defun org-get-text-property-any (pos prop &optional object)
3646 (or (get-text-property pos prop object)
3647 (and (setq pos (next-single-property-change pos prop object))
3648 (get-text-property pos prop object))))
3650 (defun org-html-level-close (level max-outline-level)
3651 "Terminate one level in HTML export."
3652 (if (<= level max-outline-level)
3653 (insert "</div>\n")
3654 (org-close-li)
3655 (insert "</ul>\n")))
3657 ;;; iCalendar export
3659 ;;;###autoload
3660 (defun org-export-icalendar-this-file ()
3661 "Export current file as an iCalendar file.
3662 The iCalendar file will be located in the same directory as the Org-mode
3663 file, but with extension `.ics'."
3664 (interactive)
3665 (org-export-icalendar nil buffer-file-name))
3667 ;;;###autoload
3668 (defun org-export-icalendar-all-agenda-files ()
3669 "Export all files in `org-agenda-files' to iCalendar .ics files.
3670 Each iCalendar file will be located in the same directory as the Org-mode
3671 file, but with extension `.ics'."
3672 (interactive)
3673 (apply 'org-export-icalendar nil (org-agenda-files t)))
3675 ;;;###autoload
3676 (defun org-export-icalendar-combine-agenda-files ()
3677 "Export all files in `org-agenda-files' to a single combined iCalendar file.
3678 The file is stored under the name `org-combined-agenda-icalendar-file'."
3679 (interactive)
3680 (apply 'org-export-icalendar t (org-agenda-files t)))
3682 (defun org-export-icalendar (combine &rest files)
3683 "Create iCalendar files for all elements of FILES.
3684 If COMBINE is non-nil, combine all calendar entries into a single large
3685 file and store it under the name `org-combined-agenda-icalendar-file'."
3686 (save-excursion
3687 (org-prepare-agenda-buffers files)
3688 (let* ((dir (org-export-directory
3689 :ical (list :publishing-directory
3690 org-export-publishing-directory)))
3691 file ical-file ical-buffer category started org-agenda-new-buffers)
3692 (and (get-buffer "*ical-tmp*") (kill-buffer "*ical-tmp*"))
3693 (when combine
3694 (setq ical-file
3695 (if (file-name-absolute-p org-combined-agenda-icalendar-file)
3696 org-combined-agenda-icalendar-file
3697 (expand-file-name org-combined-agenda-icalendar-file dir))
3698 ical-buffer (org-get-agenda-file-buffer ical-file))
3699 (set-buffer ical-buffer) (erase-buffer))
3700 (while (setq file (pop files))
3701 (catch 'nextfile
3702 (org-check-agenda-file file)
3703 (set-buffer (org-get-agenda-file-buffer file))
3704 (unless combine
3705 (setq ical-file (concat (file-name-as-directory dir)
3706 (file-name-sans-extension
3707 (file-name-nondirectory buffer-file-name))
3708 ".ics"))
3709 (setq ical-buffer (org-get-agenda-file-buffer ical-file))
3710 (with-current-buffer ical-buffer (erase-buffer)))
3711 (setq category (or org-category
3712 (file-name-sans-extension
3713 (file-name-nondirectory buffer-file-name))))
3714 (if (symbolp category) (setq category (symbol-name category)))
3715 (let ((standard-output ical-buffer))
3716 (if combine
3717 (and (not started) (setq started t)
3718 (org-start-icalendar-file org-icalendar-combined-name))
3719 (org-start-icalendar-file category))
3720 (org-print-icalendar-entries combine)
3721 (when (or (and combine (not files)) (not combine))
3722 (org-finish-icalendar-file)
3723 (set-buffer ical-buffer)
3724 (save-buffer)
3725 (run-hooks 'org-after-save-iCalendar-file-hook)
3726 (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
3727 ))))
3728 (org-release-buffers org-agenda-new-buffers))))
3730 (defvar org-after-save-iCalendar-file-hook nil
3731 "Hook run after an iCalendar file has been saved.
3732 The iCalendar buffer is still current when this hook is run.
3733 A good way to use this is to tell a desktop calenndar application to re-read
3734 the iCalendar file.")
3736 (defvar org-agenda-default-appointment-duration) ; defined in org-agenda.el
3737 (defun org-print-icalendar-entries (&optional combine)
3738 "Print iCalendar entries for the current Org-mode file to `standard-output'.
3739 When COMBINE is non nil, add the category to each line."
3740 (require 'org-agenda)
3741 (let ((re1 (concat org-ts-regexp "\\|<%%([^>\n]+>"))
3742 (re2 (concat "--?-?\\(" org-ts-regexp "\\)"))
3743 (dts (org-ical-ts-to-string
3744 (format-time-string (cdr org-time-stamp-formats) (current-time))
3745 "DTSTART"))
3746 hd ts ts2 state status (inc t) pos b sexp rrule
3747 scheduledp deadlinep prefix
3748 tmp pri category entry location summary desc uid
3749 (sexp-buffer (get-buffer-create "*ical-tmp*")))
3750 (org-refresh-category-properties)
3751 (save-excursion
3752 (goto-char (point-min))
3753 (while (re-search-forward re1 nil t)
3754 (catch :skip
3755 (org-agenda-skip)
3756 (when (boundp 'org-icalendar-verify-function)
3757 (unless (funcall org-icalendar-verify-function)
3758 (outline-next-heading)
3759 (backward-char 1)
3760 (throw :skip nil)))
3761 (setq pos (match-beginning 0)
3762 ts (match-string 0)
3763 inc t
3764 hd (condition-case nil
3765 (org-icalendar-cleanup-string
3766 (org-get-heading))
3767 (error (throw :skip nil)))
3768 summary (org-icalendar-cleanup-string
3769 (org-entry-get nil "SUMMARY"))
3770 desc (org-icalendar-cleanup-string
3771 (or (org-entry-get nil "DESCRIPTION")
3772 (and org-icalendar-include-body (org-get-entry)))
3773 t org-icalendar-include-body)
3774 location (org-icalendar-cleanup-string
3775 (org-entry-get nil "LOCATION"))
3776 uid (if org-icalendar-store-UID
3777 (org-id-get-create)
3778 (or (org-id-get) (org-id-new)))
3779 category (org-get-category)
3780 deadlinep nil scheduledp nil)
3781 (if (looking-at re2)
3782 (progn
3783 (goto-char (match-end 0))
3784 (setq ts2 (match-string 1)
3785 inc (not (string-match "[0-9]\\{1,2\\}:[0-9][0-9]" ts2))))
3786 (setq tmp (buffer-substring (max (point-min)
3787 (- pos org-ds-keyword-length))
3788 pos)
3789 ts2 (if (string-match "[0-9]\\{1,2\\}:[0-9][0-9]-\\([0-9]\\{1,2\\}:[0-9][0-9]\\)" ts)
3790 (progn
3791 (setq inc nil)
3792 (replace-match "\\1" t nil ts))
3794 deadlinep (string-match org-deadline-regexp tmp)
3795 scheduledp (string-match org-scheduled-regexp tmp)
3796 ;; donep (org-entry-is-done-p)
3798 (setq prefix (if deadlinep "DL-" (if scheduledp "SC-" "TS-")))
3799 (if (or (string-match org-tr-regexp hd)
3800 (string-match org-ts-regexp hd))
3801 (setq hd (replace-match "" t t hd)))
3802 (if (string-match "\\+\\([0-9]+\\)\\([dwmy]\\)>" ts)
3803 (setq rrule
3804 (concat "\nRRULE:FREQ="
3805 (cdr (assoc
3806 (match-string 2 ts)
3807 '(("d" . "DAILY")("w" . "WEEKLY")
3808 ("m" . "MONTHLY")("y" . "YEARLY"))))
3809 ";INTERVAL=" (match-string 1 ts)))
3810 (setq rrule ""))
3811 (setq summary (or summary hd))
3812 (if (string-match org-bracket-link-regexp summary)
3813 (setq summary
3814 (replace-match (if (match-end 3)
3815 (match-string 3 summary)
3816 (match-string 1 summary))
3817 t t summary)))
3818 (if deadlinep (setq summary (concat "DL: " summary)))
3819 (if scheduledp (setq summary (concat "S: " summary)))
3820 (if (string-match "\\`<%%" ts)
3821 (with-current-buffer sexp-buffer
3822 (insert (substring ts 1 -1) " " summary "\n"))
3823 (princ (format "BEGIN:VEVENT
3824 UID: %s
3826 %s%s
3827 SUMMARY:%s%s%s
3828 CATEGORIES:%s
3829 END:VEVENT\n"
3830 (concat prefix uid)
3831 (org-ical-ts-to-string ts "DTSTART")
3832 (org-ical-ts-to-string ts2 "DTEND" inc)
3833 rrule summary
3834 (if (and desc (string-match "\\S-" desc))
3835 (concat "\nDESCRIPTION: " desc) "")
3836 (if (and location (string-match "\\S-" location))
3837 (concat "\nLOCATION: " location) "")
3838 category)))))
3839 (when (and org-icalendar-include-sexps
3840 (condition-case nil (require 'icalendar) (error nil))
3841 (fboundp 'icalendar-export-region))
3842 ;; Get all the literal sexps
3843 (goto-char (point-min))
3844 (while (re-search-forward "^&?%%(" nil t)
3845 (catch :skip
3846 (org-agenda-skip)
3847 (setq b (match-beginning 0))
3848 (goto-char (1- (match-end 0)))
3849 (forward-sexp 1)
3850 (end-of-line 1)
3851 (setq sexp (buffer-substring b (point)))
3852 (with-current-buffer sexp-buffer
3853 (insert sexp "\n"))
3854 (princ (org-diary-to-ical-string sexp-buffer)))))
3856 (when org-icalendar-include-todo
3857 (setq prefix "TODO-")
3858 (goto-char (point-min))
3859 (while (re-search-forward org-todo-line-regexp nil t)
3860 (catch :skip
3861 (org-agenda-skip)
3862 (when (boundp 'org-icalendar-verify-function)
3863 (unless (funcall org-icalendar-verify-function)
3864 (outline-next-heading)
3865 (backward-char 1)
3866 (throw :skip nil)))
3867 (setq state (match-string 2))
3868 (setq status (if (member state org-done-keywords)
3869 "COMPLETED" "NEEDS-ACTION"))
3870 (when (and state
3871 (or (not (member state org-done-keywords))
3872 (eq org-icalendar-include-todo 'all))
3873 (not (member org-archive-tag (org-get-tags-at)))
3875 (setq hd (match-string 3)
3876 summary (org-icalendar-cleanup-string
3877 (org-entry-get nil "SUMMARY"))
3878 desc (org-icalendar-cleanup-string
3879 (or (org-entry-get nil "DESCRIPTION")
3880 (and org-icalendar-include-body (org-get-entry)))
3881 t org-icalendar-include-body)
3882 location (org-icalendar-cleanup-string
3883 (org-entry-get nil "LOCATION"))
3884 uid (if org-icalendar-store-UID
3885 (org-id-get-create)
3886 (or (org-id-get) (org-id-new))))
3887 (if (string-match org-bracket-link-regexp hd)
3888 (setq hd (replace-match (if (match-end 3) (match-string 3 hd)
3889 (match-string 1 hd))
3890 t t hd)))
3891 (if (string-match org-priority-regexp hd)
3892 (setq pri (string-to-char (match-string 2 hd))
3893 hd (concat (substring hd 0 (match-beginning 1))
3894 (substring hd (match-end 1))))
3895 (setq pri org-default-priority))
3896 (setq pri (floor (1+ (* 8. (/ (float (- org-lowest-priority pri))
3897 (- org-lowest-priority org-highest-priority))))))
3899 (princ (format "BEGIN:VTODO
3900 UID: %s
3902 SUMMARY:%s%s%s
3903 CATEGORIES:%s
3904 SEQUENCE:1
3905 PRIORITY:%d
3906 STATUS:%s
3907 END:VTODO\n"
3908 (concat prefix uid)
3910 (or summary hd)
3911 (if (and location (string-match "\\S-" location))
3912 (concat "\nLOCATION: " location) "")
3913 (if (and desc (string-match "\\S-" desc))
3914 (concat "\nDESCRIPTION: " desc) "")
3915 category
3916 pri status)))))))))
3918 (defun org-icalendar-cleanup-string (s &optional is-body maxlength)
3919 "Take out stuff and quote what needs to be quoted.
3920 When IS-BODY is non-nil, assume that this is the body of an item, clean up
3921 whitespace, newlines, drawers, and timestamps, and cut it down to MAXLENGTH
3922 characters."
3923 (if (not s)
3925 (when is-body
3926 (let ((re (concat "\\(" org-drawer-regexp "\\)[^\000]*?:END:.*\n?"))
3927 (re2 (concat "^[ \t]*" org-keyword-time-regexp ".*\n?")))
3928 (while (string-match re s) (setq s (replace-match "" t t s)))
3929 (while (string-match re2 s) (setq s (replace-match "" t t s)))))
3930 (let ((start 0))
3931 (while (string-match "\\([,;]\\)" s start)
3932 (setq start (+ (match-beginning 0) 2)
3933 s (replace-match "\\\\\\1" nil nil s))))
3934 (when is-body
3935 (while (string-match "[ \t]*\n[ \t]*" s)
3936 (setq s (replace-match "\\n" t t s))))
3937 (setq s (org-trim s))
3938 (if is-body
3939 (if maxlength
3940 (if (and (numberp maxlength)
3941 (> (length s) maxlength))
3942 (setq s (substring s 0 maxlength)))))
3945 (defun org-get-entry ()
3946 "Clean-up description string."
3947 (save-excursion
3948 (org-back-to-heading t)
3949 (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
3951 (defun org-start-icalendar-file (name)
3952 "Start an iCalendar file by inserting the header."
3953 (let ((user user-full-name)
3954 (name (or name "unknown"))
3955 (timezone (cadr (current-time-zone))))
3956 (princ
3957 (format "BEGIN:VCALENDAR
3958 VERSION:2.0
3959 X-WR-CALNAME:%s
3960 PRODID:-//%s//Emacs with Org-mode//EN
3961 X-WR-TIMEZONE:%s
3962 CALSCALE:GREGORIAN\n" name user timezone))))
3964 (defun org-finish-icalendar-file ()
3965 "Finish an iCalendar file by inserting the END statement."
3966 (princ "END:VCALENDAR\n"))
3968 (defun org-ical-ts-to-string (s keyword &optional inc)
3969 "Take a time string S and convert it to iCalendar format.
3970 KEYWORD is added in front, to make a complete line like DTSTART....
3971 When INC is non-nil, increase the hour by two (if time string contains
3972 a time), or the day by one (if it does not contain a time)."
3973 (let ((t1 (org-parse-time-string s 'nodefault))
3974 t2 fmt have-time time)
3975 (if (and (car t1) (nth 1 t1) (nth 2 t1))
3976 (setq t2 t1 have-time t)
3977 (setq t2 (org-parse-time-string s)))
3978 (let ((s (car t2)) (mi (nth 1 t2)) (h (nth 2 t2))
3979 (d (nth 3 t2)) (m (nth 4 t2)) (y (nth 5 t2)))
3980 (when inc
3981 (if have-time
3982 (if org-agenda-default-appointment-duration
3983 (setq mi (+ org-agenda-default-appointment-duration mi))
3984 (setq h (+ 2 h)))
3985 (setq d (1+ d))))
3986 (setq time (encode-time s mi h d m y)))
3987 (setq fmt (if have-time ":%Y%m%dT%H%M%S" ";VALUE=DATE:%Y%m%d"))
3988 (concat keyword (format-time-string fmt time))))
3990 ;;; XOXO export
3992 (defun org-export-as-xoxo-insert-into (buffer &rest output)
3993 (with-current-buffer buffer
3994 (apply 'insert output)))
3995 (put 'org-export-as-xoxo-insert-into 'lisp-indent-function 1)
3997 ;;;###autoload
3998 (defun org-export-as-xoxo (&optional buffer)
3999 "Export the org buffer as XOXO.
4000 The XOXO buffer is named *xoxo-<source buffer name>*"
4001 (interactive (list (current-buffer)))
4002 ;; A quickie abstraction
4004 ;; Output everything as XOXO
4005 (with-current-buffer (get-buffer buffer)
4006 (let* ((pos (point))
4007 (opt-plist (org-combine-plists (org-default-export-plist)
4008 (org-infile-export-plist)))
4009 (filename (concat (file-name-as-directory
4010 (org-export-directory :xoxo opt-plist))
4011 (file-name-sans-extension
4012 (file-name-nondirectory buffer-file-name))
4013 ".html"))
4014 (out (find-file-noselect filename))
4015 (last-level 1)
4016 (hanging-li nil))
4017 (goto-char (point-min)) ;; CD: beginning-of-buffer is not allowed.
4018 ;; Check the output buffer is empty.
4019 (with-current-buffer out (erase-buffer))
4020 ;; Kick off the output
4021 (org-export-as-xoxo-insert-into out "<ol class='xoxo'>\n")
4022 (while (re-search-forward "^\\(\\*+\\)[ \t]+\\(.+\\)" (point-max) 't)
4023 (let* ((hd (match-string-no-properties 1))
4024 (level (length hd))
4025 (text (concat
4026 (match-string-no-properties 2)
4027 (save-excursion
4028 (goto-char (match-end 0))
4029 (let ((str ""))
4030 (catch 'loop
4031 (while 't
4032 (forward-line)
4033 (if (looking-at "^[ \t]\\(.*\\)")
4034 (setq str (concat str (match-string-no-properties 1)))
4035 (throw 'loop str)))))))))
4037 ;; Handle level rendering
4038 (cond
4039 ((> level last-level)
4040 (org-export-as-xoxo-insert-into out "\n<ol>\n"))
4042 ((< level last-level)
4043 (dotimes (- (- last-level level) 1)
4044 (if hanging-li
4045 (org-export-as-xoxo-insert-into out "</li>\n"))
4046 (org-export-as-xoxo-insert-into out "</ol>\n"))
4047 (when hanging-li
4048 (org-export-as-xoxo-insert-into out "</li>\n")
4049 (setq hanging-li nil)))
4051 ((equal level last-level)
4052 (if hanging-li
4053 (org-export-as-xoxo-insert-into out "</li>\n")))
4056 (setq last-level level)
4058 ;; And output the new li
4059 (setq hanging-li 't)
4060 (if (equal ?+ (elt text 0))
4061 (org-export-as-xoxo-insert-into out "<li class='" (substring text 1) "'>")
4062 (org-export-as-xoxo-insert-into out "<li>" text))))
4064 ;; Finally finish off the ol
4065 (dotimes (- last-level 1)
4066 (if hanging-li
4067 (org-export-as-xoxo-insert-into out "</li>\n"))
4068 (org-export-as-xoxo-insert-into out "</ol>\n"))
4070 (goto-char pos)
4071 ;; Finish the buffer off and clean it up.
4072 (switch-to-buffer-other-window out)
4073 (indent-region (point-min) (point-max) nil)
4074 (save-buffer)
4075 (goto-char (point-min))
4078 (provide 'org-exp)
4080 ;; arch-tag: 65985fe9-095c-49c7-a7b6-cb4ee15c0a95
4082 ;;; org-exp.el ends here