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