Release 6.03.
[org-mode.git] / lisp / org-exp.el
blobf76f87a90c0d3e993e4e1aecf48149a3a4559523
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 ;;;; Exporting
637 ;;; Variables, constants, and parameter plists
639 (defconst org-level-max 20)
641 (defvar org-export-html-preamble nil
642 "Preamble, to be inserted just after <body>. Set by publishing functions.")
643 (defvar org-export-html-postamble nil
644 "Preamble, to be inserted just before </body>. Set by publishing functions.")
645 (defvar org-export-html-auto-preamble t
646 "Should default preamble be inserted? Set by publishing functions.")
647 (defvar org-export-html-auto-postamble t
648 "Should default postamble be inserted? Set by publishing functions.")
649 (defvar org-current-export-file nil) ; dynamically scoped parameter
650 (defvar org-current-export-dir nil) ; dynamically scoped parameter
652 (defconst org-export-plist-vars
653 '((:link-up . org-export-html-link-up)
654 (:link-home . org-export-html-link-home)
655 (:language . org-export-default-language)
656 (:customtime . org-display-custom-times)
657 (:headline-levels . org-export-headline-levels)
658 (:section-numbers . org-export-with-section-numbers)
659 (:table-of-contents . org-export-with-toc)
660 (:preserve-breaks . org-export-preserve-breaks)
661 (:archived-trees . org-export-with-archived-trees)
662 (:emphasize . org-export-with-emphasize)
663 (:sub-superscript . org-export-with-sub-superscripts)
664 (:special-strings . org-export-with-special-strings)
665 (:footnotes . org-export-with-footnotes)
666 (:drawers . org-export-with-drawers)
667 (:tags . org-export-with-tags)
668 (:TeX-macros . org-export-with-TeX-macros)
669 (:LaTeX-fragments . org-export-with-LaTeX-fragments)
670 (:skip-before-1st-heading . org-export-skip-text-before-1st-heading)
671 (:fixed-width . org-export-with-fixed-width)
672 (:timestamps . org-export-with-timestamps)
673 (:author-info . org-export-author-info)
674 (:time-stamp-file . org-export-time-stamp-file)
675 (:tables . org-export-with-tables)
676 (:table-auto-headline . org-export-highlight-first-table-line)
677 (:style . org-export-html-style)
678 (:agenda-style . org-agenda-export-html-style)
679 (:convert-org-links . org-export-html-link-org-files-as-html)
680 (:inline-images . org-export-html-inline-images)
681 (:html-extension . org-export-html-extension)
682 (:html-table-tag . org-export-html-table-tag)
683 (:expand-quoted-html . org-export-html-expand)
684 (:timestamp . org-export-html-with-timestamp)
685 (:publishing-directory . org-export-publishing-directory)
686 (:preamble . org-export-html-preamble)
687 (:postamble . org-export-html-postamble)
688 (:auto-preamble . org-export-html-auto-preamble)
689 (:auto-postamble . org-export-html-auto-postamble)
690 (:author . user-full-name)
691 (:email . user-mail-address)))
693 (defun org-default-export-plist ()
694 "Return the property list with default settings for the export variables."
695 (let ((l org-export-plist-vars) rtn e)
696 (while (setq e (pop l))
697 (setq rtn (cons (car e) (cons (symbol-value (cdr e)) rtn))))
698 rtn))
700 (defvar org-export-inbuffer-options-extra nil
701 "List of additional in-buffer options that should be detected.
702 Just before export, the buffer is scanned for options like #+TITLE, #+EMAIL,
703 etc. Extensions can add to this list to get their options detected, and they
704 can then add a function to `org-export-options-filters' to process these
705 options.
706 Each element in this list must be a list, with the in-buffer keyword as car,
707 and a property (a symbol) as the next element. All occurences of the
708 keyword will be found, the values concatenated with a space character
709 in between, and the result stored in the export options property list.")
711 (defvar org-export-options-filters nil
712 "Functions to be called to finalize the export/publishing options.
713 All these options are stored in a property list, and each of the functions
714 in this hook gets a chance to modify this property list. Each function
715 must accept the property list as an argument, and must return the (possibly
716 modified) list.")
718 (defun org-infile-export-plist ()
719 "Return the property list with file-local settings for export."
720 (save-excursion
721 (save-restriction
722 (widen)
723 (goto-char (point-min))
724 (let ((re (org-make-options-regexp
725 (append
726 '("TITLE" "AUTHOR" "DATE" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE"
727 "LINK_UP" "LINK_HOME" "SETUPFILE")
728 (mapcar 'car org-export-inbuffer-options-extra))))
729 p key val text options js-up js-main js-css js-opt a pr
730 ext-setup-or-nil setup-contents (start 0))
731 (while (or (and ext-setup-or-nil
732 (string-match re ext-setup-or-nil start)
733 (setq start (match-end 0)))
734 (and (setq ext-setup-or-nil nil start 0)
735 (re-search-forward re nil t)))
736 (setq key (upcase (org-match-string-no-properties 1 ext-setup-or-nil))
737 val (org-match-string-no-properties 2 ext-setup-or-nil))
738 (cond
739 ((setq a (assoc key org-export-inbuffer-options-extra))
740 (setq pr (nth 1 a))
741 (setq p (plist-put p pr (concat (plist-get p pr) " " val))))
742 ((string-equal key "TITLE") (setq p (plist-put p :title val)))
743 ((string-equal key "AUTHOR")(setq p (plist-put p :author val)))
744 ((string-equal key "EMAIL") (setq p (plist-put p :email val)))
745 ((string-equal key "DATE") (setq p (plist-put p :date val)))
746 ((string-equal key "LANGUAGE") (setq p (plist-put p :language val)))
747 ((string-equal key "TEXT")
748 (setq text (if text (concat text "\n" val) val)))
749 ((string-equal key "OPTIONS")
750 (setq options (concat val " " options)))
751 ((string-equal key "LINK_UP")
752 (setq p (plist-put p :link-up val)))
753 ((string-equal key "LINK_HOME")
754 (setq p (plist-put p :link-home val)))
755 ((equal key "SETUPFILE")
756 (setq setup-contents (org-file-contents
757 (expand-file-name
758 (org-remove-double-quotes
759 (org-trim val)))
760 'noerror))
761 (if (not ext-setup-or-nil)
762 (setq ext-setup-or-nil setup-contents start 0)
763 (setq ext-setup-or-nil
764 (concat (substring ext-setup-or-nil 0 start)
765 "\n" setup-contents "\n"
766 (substring ext-setup-or-nil start)))))))
767 (setq p (plist-put p :text text))
768 (when options
769 (let ((op '(("H" . :headline-levels)
770 ("num" . :section-numbers)
771 ("toc" . :table-of-contents)
772 ("\\n" . :preserve-breaks)
773 ("@" . :expand-quoted-html)
774 (":" . :fixed-width)
775 ("|" . :tables)
776 ("^" . :sub-superscript)
777 ("-" . :special-strings)
778 ("f" . :footnotes)
779 ("d" . :drawers)
780 ("tags" . :tags)
781 ("*" . :emphasize)
782 ("TeX" . :TeX-macros)
783 ("LaTeX" . :LaTeX-fragments)
784 ("skip" . :skip-before-1st-heading)
785 ("author" . :author-info)
786 ("timestamp" . :time-stamp-file)))
788 (while (setq o (pop op))
789 (if (string-match (concat (regexp-quote (car o))
790 ":\\([^ \t\n\r;,.]*\\)")
791 options)
792 (setq p (plist-put p (cdr o)
793 (car (read-from-string
794 (match-string 1 options)))))))))
795 p))))
797 (defun org-export-directory (type plist)
798 (let* ((val (plist-get plist :publishing-directory))
799 (dir (if (listp val)
800 (or (cdr (assoc type val)) ".")
801 val)))
802 dir))
804 (defun org-export-process-option-filters (plist)
805 (let ((functions org-export-options-filters) f)
806 (while (setq f (pop functions))
807 (setq plist (funcall f plist))))
808 plist)
810 ;;;###autoload
811 (defun org-export (&optional arg)
812 "Export dispatcher for Org-mode.
813 When `org-export-run-in-background' is non-nil, try to run the command
814 in the background. This will be done only for commands that write
815 to a file. For details see the docstring of `org-export-run-in-background'.
817 The prefix argument ARG will be passed to the exporter. However, if
818 ARG is a double universal prefix `C-u C-u', that means to inverse the
819 value of `org-export-run-in-background'."
820 (interactive "P")
821 (let* ((bg (org-xor (equal arg '(16)) org-export-run-in-background))
822 (help "[t] insert the export option template
823 \[v] limit export to visible part of outline tree
825 \[a] export as ASCII
827 \[h] export as HTML
828 \[H] export as HTML to temporary buffer
829 \[R] export region as HTML
830 \[b] export as HTML and browse immediately
831 \[x] export as XOXO
833 \[l] export as LaTeX
834 \[L] export as LaTeX to temporary buffer
836 \[i] export current file as iCalendar file
837 \[I] export all agenda files as iCalendar files
838 \[c] export agenda files into combined iCalendar file
840 \[F] publish current file
841 \[P] publish current project
842 \[X] publish... (project will be prompted for)
843 \[A] publish all projects")
844 (cmds
845 '((?t org-insert-export-options-template nil)
846 (?v org-export-visible nil)
847 (?a org-export-as-ascii t)
848 (?h org-export-as-html t)
849 (?b org-export-as-html-and-open t)
850 (?H org-export-as-html-to-buffer nil)
851 (?R org-export-region-as-html nil)
852 (?x org-export-as-xoxo t)
853 (?l org-export-as-latex t)
854 (?L org-export-as-latex-to-buffer nil)
855 (?i org-export-icalendar-this-file t)
856 (?I org-export-icalendar-all-agenda-files t)
857 (?c org-export-icalendar-combine-agenda-files t)
858 (?F org-publish-current-file t)
859 (?P org-publish-current-project t)
860 (?X org-publish t)
861 (?A org-publish-all t)))
862 r1 r2 ass)
863 (save-window-excursion
864 (delete-other-windows)
865 (with-output-to-temp-buffer "*Org Export/Publishing Help*"
866 (princ help))
867 (message "Select command: ")
868 (setq r1 (read-char-exclusive)))
869 (setq r2 (if (< r1 27) (+ r1 96) r1))
870 (unless (setq ass (assq r2 cmds))
871 (error "No command associated with key %c" r1))
872 (if (and bg (nth 2 ass))
873 ;; execute in background
874 (let ((p (start-process
875 (concat "Exporting " (file-name-nondirectory (buffer-file-name)))
876 "*Org Processes*"
877 (expand-file-name invocation-name invocation-directory)
878 "-batch"
879 "-l" user-init-file
880 "--eval" "(require 'org-exp)"
881 "--eval" "(setq org-wait .2)"
882 (buffer-file-name)
883 "-f" (symbol-name (nth 1 ass)))))
884 (set-process-sentinel p 'org-export-process-sentinel)
885 (message "Background process \"%s\": started" p))
886 ;; background processing not requested, or not possible
887 (call-interactively (nth 1 ass)))))
889 (defun org-export-process-sentinel (process status)
890 (if (string-match "\n+\\'" status)
891 (setq status (substring status 0 -1)))
892 (message "Background process \"%s\": %s" process status))
894 (defconst org-html-entities
895 '(("nbsp")
896 ("iexcl")
897 ("cent")
898 ("pound")
899 ("curren")
900 ("yen")
901 ("brvbar")
902 ("vert" . "&#124;")
903 ("sect")
904 ("uml")
905 ("copy")
906 ("ordf")
907 ("laquo")
908 ("not")
909 ("shy")
910 ("reg")
911 ("macr")
912 ("deg")
913 ("plusmn")
914 ("sup2")
915 ("sup3")
916 ("acute")
917 ("micro")
918 ("para")
919 ("middot")
920 ("odot"."o")
921 ("star"."*")
922 ("cedil")
923 ("sup1")
924 ("ordm")
925 ("raquo")
926 ("frac14")
927 ("frac12")
928 ("frac34")
929 ("iquest")
930 ("Agrave")
931 ("Aacute")
932 ("Acirc")
933 ("Atilde")
934 ("Auml")
935 ("Aring") ("AA"."&Aring;")
936 ("AElig")
937 ("Ccedil")
938 ("Egrave")
939 ("Eacute")
940 ("Ecirc")
941 ("Euml")
942 ("Igrave")
943 ("Iacute")
944 ("Icirc")
945 ("Iuml")
946 ("ETH")
947 ("Ntilde")
948 ("Ograve")
949 ("Oacute")
950 ("Ocirc")
951 ("Otilde")
952 ("Ouml")
953 ("times")
954 ("Oslash")
955 ("Ugrave")
956 ("Uacute")
957 ("Ucirc")
958 ("Uuml")
959 ("Yacute")
960 ("THORN")
961 ("szlig")
962 ("agrave")
963 ("aacute")
964 ("acirc")
965 ("atilde")
966 ("auml")
967 ("aring")
968 ("aelig")
969 ("ccedil")
970 ("egrave")
971 ("eacute")
972 ("ecirc")
973 ("euml")
974 ("igrave")
975 ("iacute")
976 ("icirc")
977 ("iuml")
978 ("eth")
979 ("ntilde")
980 ("ograve")
981 ("oacute")
982 ("ocirc")
983 ("otilde")
984 ("ouml")
985 ("divide")
986 ("oslash")
987 ("ugrave")
988 ("uacute")
989 ("ucirc")
990 ("uuml")
991 ("yacute")
992 ("thorn")
993 ("yuml")
994 ("fnof")
995 ("Alpha")
996 ("Beta")
997 ("Gamma")
998 ("Delta")
999 ("Epsilon")
1000 ("Zeta")
1001 ("Eta")
1002 ("Theta")
1003 ("Iota")
1004 ("Kappa")
1005 ("Lambda")
1006 ("Mu")
1007 ("Nu")
1008 ("Xi")
1009 ("Omicron")
1010 ("Pi")
1011 ("Rho")
1012 ("Sigma")
1013 ("Tau")
1014 ("Upsilon")
1015 ("Phi")
1016 ("Chi")
1017 ("Psi")
1018 ("Omega")
1019 ("alpha")
1020 ("beta")
1021 ("gamma")
1022 ("delta")
1023 ("epsilon")
1024 ("varepsilon"."&epsilon;")
1025 ("zeta")
1026 ("eta")
1027 ("theta")
1028 ("iota")
1029 ("kappa")
1030 ("lambda")
1031 ("mu")
1032 ("nu")
1033 ("xi")
1034 ("omicron")
1035 ("pi")
1036 ("rho")
1037 ("sigmaf") ("varsigma"."&sigmaf;")
1038 ("sigma")
1039 ("tau")
1040 ("upsilon")
1041 ("phi")
1042 ("chi")
1043 ("psi")
1044 ("omega")
1045 ("thetasym") ("vartheta"."&thetasym;")
1046 ("upsih")
1047 ("piv")
1048 ("bull") ("bullet"."&bull;")
1049 ("hellip") ("dots"."&hellip;")
1050 ("prime")
1051 ("Prime")
1052 ("oline")
1053 ("frasl")
1054 ("weierp")
1055 ("image")
1056 ("real")
1057 ("trade")
1058 ("alefsym")
1059 ("larr") ("leftarrow"."&larr;") ("gets"."&larr;")
1060 ("uarr") ("uparrow"."&uarr;")
1061 ("rarr") ("to"."&rarr;") ("rightarrow"."&rarr;")
1062 ("darr")("downarrow"."&darr;")
1063 ("harr") ("leftrightarrow"."&harr;")
1064 ("crarr") ("hookleftarrow"."&crarr;") ; has round hook, not quite CR
1065 ("lArr") ("Leftarrow"."&lArr;")
1066 ("uArr") ("Uparrow"."&uArr;")
1067 ("rArr") ("Rightarrow"."&rArr;")
1068 ("dArr") ("Downarrow"."&dArr;")
1069 ("hArr") ("Leftrightarrow"."&hArr;")
1070 ("forall")
1071 ("part") ("partial"."&part;")
1072 ("exist") ("exists"."&exist;")
1073 ("empty") ("emptyset"."&empty;")
1074 ("nabla")
1075 ("isin") ("in"."&isin;")
1076 ("notin")
1077 ("ni")
1078 ("prod")
1079 ("sum")
1080 ("minus")
1081 ("lowast") ("ast"."&lowast;")
1082 ("radic")
1083 ("prop") ("proptp"."&prop;")
1084 ("infin") ("infty"."&infin;")
1085 ("ang") ("angle"."&ang;")
1086 ("and") ("wedge"."&and;")
1087 ("or") ("vee"."&or;")
1088 ("cap")
1089 ("cup")
1090 ("int")
1091 ("there4")
1092 ("sim")
1093 ("cong") ("simeq"."&cong;")
1094 ("asymp")("approx"."&asymp;")
1095 ("ne") ("neq"."&ne;")
1096 ("equiv")
1097 ("le")
1098 ("ge")
1099 ("sub") ("subset"."&sub;")
1100 ("sup") ("supset"."&sup;")
1101 ("nsub")
1102 ("sube")
1103 ("supe")
1104 ("oplus")
1105 ("otimes")
1106 ("perp")
1107 ("sdot") ("cdot"."&sdot;")
1108 ("lceil")
1109 ("rceil")
1110 ("lfloor")
1111 ("rfloor")
1112 ("lang")
1113 ("rang")
1114 ("loz") ("Diamond"."&loz;")
1115 ("spades") ("spadesuit"."&spades;")
1116 ("clubs") ("clubsuit"."&clubs;")
1117 ("hearts") ("diamondsuit"."&hearts;")
1118 ("diams") ("diamondsuit"."&diams;")
1119 ("smile"."&#9786;") ("blacksmile"."&#9787;") ("sad"."&#9785;")
1120 ("quot")
1121 ("amp")
1122 ("lt")
1123 ("gt")
1124 ("OElig")
1125 ("oelig")
1126 ("Scaron")
1127 ("scaron")
1128 ("Yuml")
1129 ("circ")
1130 ("tilde")
1131 ("ensp")
1132 ("emsp")
1133 ("thinsp")
1134 ("zwnj")
1135 ("zwj")
1136 ("lrm")
1137 ("rlm")
1138 ("ndash")
1139 ("mdash")
1140 ("lsquo")
1141 ("rsquo")
1142 ("sbquo")
1143 ("ldquo")
1144 ("rdquo")
1145 ("bdquo")
1146 ("dagger")
1147 ("Dagger")
1148 ("permil")
1149 ("lsaquo")
1150 ("rsaquo")
1151 ("euro")
1153 ("arccos"."arccos")
1154 ("arcsin"."arcsin")
1155 ("arctan"."arctan")
1156 ("arg"."arg")
1157 ("cos"."cos")
1158 ("cosh"."cosh")
1159 ("cot"."cot")
1160 ("coth"."coth")
1161 ("csc"."csc")
1162 ("deg"."deg")
1163 ("det"."det")
1164 ("dim"."dim")
1165 ("exp"."exp")
1166 ("gcd"."gcd")
1167 ("hom"."hom")
1168 ("inf"."inf")
1169 ("ker"."ker")
1170 ("lg"."lg")
1171 ("lim"."lim")
1172 ("liminf"."liminf")
1173 ("limsup"."limsup")
1174 ("ln"."ln")
1175 ("log"."log")
1176 ("max"."max")
1177 ("min"."min")
1178 ("Pr"."Pr")
1179 ("sec"."sec")
1180 ("sin"."sin")
1181 ("sinh"."sinh")
1182 ("sup"."sup")
1183 ("tan"."tan")
1184 ("tanh"."tanh")
1186 "Entities for TeX->HTML translation.
1187 Entries can be like (\"ent\"), in which case \"\\ent\" will be translated to
1188 \"&ent;\". An entry can also be a dotted pair like (\"ent\".\"&other;\").
1189 In that case, \"\\ent\" will be translated to \"&other;\".
1190 The list contains HTML entities for Latin-1, Greek and other symbols.
1191 It is supplemented by a number of commonly used TeX macros with appropriate
1192 translations. There is currently no way for users to extend this.")
1194 ;;; General functions for all backends
1196 (defun org-export-preprocess-string (string &rest parameters)
1197 "Cleanup STRING so that that the true exported has a more consistent source.
1198 This function takes STRING, which should be a buffer-string of an org-file
1199 to export. It then creates a temporary buffer where it does its job.
1200 The result is then again returned as a string, and the exporter works
1201 on this string to produce the exported version."
1202 (interactive)
1203 (let* ((re-radio (and org-target-link-regexp
1204 (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)")))
1205 (re-plain-link (concat "\\([^[<]\\)" org-plain-link-re))
1206 (re-angle-link (concat "\\([^[]\\)" org-angle-link-re))
1207 (re-archive (concat ":" org-archive-tag ":"))
1208 (re-quote (concat "^\\*+[ \t]+" org-quote-string "\\>"))
1209 (re-commented (concat "^\\*+[ \t]+" org-comment-string "\\>"))
1210 (htmlp (plist-get parameters :for-html))
1211 (asciip (plist-get parameters :for-ascii))
1212 (latexp (plist-get parameters :for-LaTeX))
1213 (commentsp (plist-get parameters :comments))
1214 (archived-trees (plist-get parameters :archived-trees))
1215 (inhibit-read-only t)
1216 (drawers org-drawers)
1217 (exp-drawers (plist-get parameters :drawers))
1218 (outline-regexp "\\*+ ")
1219 target-alist tmp target level
1220 a b xx rtn p)
1222 (with-current-buffer (get-buffer-create " org-mode-tmp")
1223 (erase-buffer)
1224 (insert string)
1225 (setq case-fold-search t)
1226 ;; Call the hook
1227 (run-hooks 'org-export-preprocess-hook)
1229 ;; Remove license-to-kill stuff
1230 ;; The caller markes some stuff fo killing, stuff that has been
1231 ;; used to create the page title, for example.
1232 (while (setq p (text-property-any (point-min) (point-max)
1233 :org-license-to-kill t))
1234 (delete-region p (next-single-property-change p :org-license-to-kill)))
1236 (let ((org-inhibit-startup t)) (org-mode))
1237 (setq case-fold-search t)
1238 (untabify (point-min) (point-max))
1240 ;; Handle incude files
1241 (org-export-handle-include-files)
1243 ;; Handle source code snippets
1244 (org-export-replace-src-segments)
1246 ;; Get rid of drawers
1247 (unless (eq t exp-drawers)
1248 (goto-char (point-min))
1249 (let ((re (concat "^[ \t]*:\\("
1250 (mapconcat
1251 'identity
1252 (org-delete-all exp-drawers
1253 (copy-sequence drawers))
1254 "\\|")
1255 "\\):[ \t]*\n\\([^@]*?\n\\)?[ \t]*:END:[ \t]*\n")))
1256 (while (re-search-forward re nil t)
1257 (replace-match ""))))
1259 ;; Get the correct stuff before the first headline
1260 (when (plist-get parameters :skip-before-1st-heading)
1261 (goto-char (point-min))
1262 (when (re-search-forward "^\\*+[ \t]" nil t)
1263 (delete-region (point-min) (match-beginning 0))
1264 (goto-char (point-min))
1265 (insert "\n")))
1266 (when (plist-get parameters :add-text)
1267 (goto-char (point-min))
1268 (insert (plist-get parameters :add-text) "\n"))
1270 ;; Get rid of archived trees
1271 (when (not (eq archived-trees t))
1272 (goto-char (point-min))
1273 (while (re-search-forward re-archive nil t)
1274 (if (not (org-on-heading-p t))
1275 (org-end-of-subtree t)
1276 (beginning-of-line 1)
1277 (setq a (if archived-trees
1278 (1+ (point-at-eol)) (point))
1279 b (org-end-of-subtree t))
1280 (if (> b a) (delete-region a b)))))
1282 ;; Find all headings and compute the targets for them
1283 (goto-char (point-min))
1284 (org-init-section-numbers)
1285 (let ((re (concat "^" org-outline-regexp)))
1286 (while (re-search-forward re nil t)
1287 (setq level (org-reduced-level
1288 (save-excursion (goto-char (point-at-bol))
1289 (org-outline-level))))
1290 (setq target (org-solidify-link-text
1291 (format "sec-%s" (org-section-number level))))
1292 (push (cons target target) target-alist)
1293 (add-text-properties
1294 (point-at-bol) (point-at-eol)
1295 (list 'target target))))
1297 ;; Find targets in comments and move them out of comments,
1298 ;; but mark them as targets that should be invisible
1299 (goto-char (point-min))
1300 (while (re-search-forward "^#.*?\\(<<<?\\([^>\r\n]+\\)>>>?\\).*" nil t)
1301 ;; Check if the line before or after is a headline with a target
1302 (if (setq target (or (get-text-property (point-at-bol 0) 'target)
1303 (get-text-property (point-at-bol 2) 'target)))
1304 (progn
1305 ;; use the existing target in a neighboring line
1306 (setq tmp (match-string 2))
1307 (replace-match "")
1308 (and (looking-at "\n") (delete-char 1))
1309 (push (cons (org-solidify-link-text tmp) target)
1310 target-alist))
1311 ;; Make an invisible target
1312 (replace-match "\\1(INVISIBLE)")))
1314 ;; Protect backend specific stuff, throw away the others.
1315 (let ((formatters
1316 `((,htmlp "HTML" "BEGIN_HTML" "END_HTML")
1317 (,asciip "ASCII" "BEGIN_ASCII" "END_ASCII")
1318 (,latexp "LaTeX" "BEGIN_LaTeX" "END_LaTeX")))
1319 fmt)
1320 (goto-char (point-min))
1321 (while (re-search-forward "^#\\+BEGIN_EXAMPLE[ \t]*\n" nil t)
1322 (goto-char (match-end 0))
1323 (while (not (looking-at "#\\+END_EXAMPLE"))
1324 (insert ": ")
1325 (beginning-of-line 2)))
1326 (goto-char (point-min))
1327 (while (re-search-forward "^[ \t]*:.*\\(\n[ \t]*:.*\\)*" nil t)
1328 (add-text-properties (match-beginning 0) (match-end 0)
1329 '(org-protected t)))
1330 (while formatters
1331 (setq fmt (pop formatters))
1332 (when (car fmt)
1333 (goto-char (point-min))
1334 (while (re-search-forward (concat "^#\\+" (cadr fmt)
1335 ":[ \t]*\\(.*\\)") nil t)
1336 (replace-match "\\1" t)
1337 (add-text-properties
1338 (point-at-bol) (min (1+ (point-at-eol)) (point-max))
1339 '(org-protected t))))
1340 (goto-char (point-min))
1341 (while (re-search-forward
1342 (concat "^#\\+"
1343 (caddr fmt) "\\>.*\\(\\(\n.*\\)*?\n\\)#\\+"
1344 (cadddr fmt) "\\>.*\n?") nil t)
1345 (if (car fmt)
1346 (add-text-properties (match-beginning 1) (1+ (match-end 1))
1347 '(org-protected t))
1348 (delete-region (match-beginning 0) (match-end 0))))))
1350 ;; Protect quoted subtrees
1351 (goto-char (point-min))
1352 (while (re-search-forward re-quote nil t)
1353 (goto-char (match-beginning 0))
1354 (end-of-line 1)
1355 (add-text-properties (point) (org-end-of-subtree t)
1356 '(org-protected t)))
1358 ;; Protect verbatim elements
1359 (goto-char (point-min))
1360 (while (re-search-forward org-verbatim-re nil t)
1361 (add-text-properties (match-beginning 4) (match-end 4)
1362 '(org-protected t))
1363 (goto-char (1+ (match-end 4))))
1365 ;; Blockquotes
1366 (goto-char (point-min))
1367 (while (re-search-forward "^#\\+\\(begin\\|end\\)_\\(block\\)?quote\\>.*" nil t)
1368 (replace-match (if (equal (downcase (match-string 1)) "end")
1369 "ORG-BLOCKQUOTE-END" "ORG-BLOCKQUOTE-START")
1370 t t))
1371 ;; Verse
1372 (goto-char (point-min))
1373 (while (re-search-forward "^#\\+\\(begin\\|end\\)_verse\\>.*" nil t)
1374 (replace-match (if (equal (downcase (match-string 1)) "end")
1375 "ORG-VERSE-END" "ORG-VERSE-START")
1376 t t))
1378 ;; Remove comment environment
1379 (goto-char (point-min))
1380 (while (re-search-forward
1381 "^#\\+BEGIN_COMMENT[ \t]*\n[^\000]*?^#\\+END_COMMENT\\>.*" nil t)
1382 (replace-match "" t t))
1384 ;; Remove subtrees that are commented
1385 (goto-char (point-min))
1386 (while (re-search-forward re-commented nil t)
1387 (goto-char (match-beginning 0))
1388 (delete-region (point) (org-end-of-subtree t)))
1390 ;; Remove special table lines
1391 (when org-export-table-remove-special-lines
1392 (goto-char (point-min))
1393 (while (re-search-forward "^[ \t]*|" nil t)
1394 (beginning-of-line 1)
1395 (if (or (looking-at "[ \t]*| *[!_^] *|")
1396 (and (looking-at ".*?| *<[0-9]+> *|")
1397 (not (looking-at ".*?| *[^ <|]"))))
1398 (delete-region (max (point-min) (1- (point-at-bol)))
1399 (point-at-eol))
1400 (end-of-line 1))))
1402 ;; Specific LaTeX stuff
1403 (when latexp
1404 (require 'org-export-latex nil)
1405 (org-export-latex-preprocess))
1407 (when asciip
1408 (org-export-ascii-clean-string))
1410 ;; Specific HTML stuff
1411 (when htmlp
1412 ;; Convert LaTeX fragments to images
1413 (when (plist-get parameters :LaTeX-fragments)
1414 (org-format-latex
1415 (concat "ltxpng/" (file-name-sans-extension
1416 (file-name-nondirectory
1417 org-current-export-file)))
1418 org-current-export-dir nil "Creating LaTeX image %s"))
1419 (message "Exporting..."))
1421 ;; Remove or replace comments
1422 (goto-char (point-min))
1423 (while (re-search-forward "^#\\(.*\n?\\)" nil t)
1424 (if commentsp
1425 (progn (add-text-properties
1426 (match-beginning 0) (match-end 0) '(org-protected t))
1427 (replace-match (format commentsp (match-string 1)) t t))
1428 (replace-match "")))
1430 ;; Find matches for radio targets and turn them into internal links
1431 (goto-char (point-min))
1432 (when re-radio
1433 (while (re-search-forward re-radio nil t)
1434 (org-if-unprotected
1435 (replace-match "\\1[[\\2]]"))))
1437 ;; Find all links that contain a newline and put them into a single line
1438 (goto-char (point-min))
1439 (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t)
1440 (org-if-unprotected
1441 (replace-match "\\1 \\3")
1442 (goto-char (match-beginning 0))))
1444 ;; Find all internal links. If they have a fuzzy match (i.e. not
1445 ;; a *dedicated* target match, let the link point to the
1446 ;; correspinding section.
1448 (goto-char (point-min))
1449 (while (re-search-forward org-bracket-link-regexp nil t)
1450 (org-if-unprotected
1451 (let* ((md (match-data))
1452 (desc (match-end 2))
1453 (link (org-link-unescape (match-string 1)))
1454 (slink (org-solidify-link-text link))
1455 found props pos
1456 (target
1457 (or (cdr (assoc slink target-alist))
1458 (save-excursion
1459 (unless (string-match org-link-types-re link)
1460 (setq found (condition-case nil (org-link-search link)
1461 (error nil)))
1462 (when (and found
1463 (or (org-on-heading-p)
1464 (not (eq found 'dedicated))))
1465 (or (get-text-property (point) 'target)
1466 (get-text-property
1467 (max (point-min)
1468 (1- (previous-single-property-change
1469 (point) 'target)))
1470 'target))))))))
1471 (when target
1472 (set-match-data md)
1473 (goto-char (match-beginning 1))
1474 (setq props (text-properties-at (point)))
1475 (delete-region (match-beginning 1) (match-end 1))
1476 (setq pos (point))
1477 (insert target)
1478 (unless desc (insert "][" link))
1479 (add-text-properties pos (point) props)))))
1481 ;; Normalize links: Convert angle and plain links into bracket links
1482 ;; Expand link abbreviations
1483 (goto-char (point-min))
1484 (while (re-search-forward re-plain-link nil t)
1485 (goto-char (1- (match-end 0)))
1486 (org-if-unprotected
1487 (let* ((s (concat (match-string 1) "[[" (match-string 2)
1488 ":" (match-string 3) "]]")))
1489 ;; added 'org-link face to links
1490 (put-text-property 0 (length s) 'face 'org-link s)
1491 (replace-match s t t))))
1492 (goto-char (point-min))
1493 (while (re-search-forward re-angle-link nil t)
1494 (goto-char (1- (match-end 0)))
1495 (org-if-unprotected
1496 (let* ((s (concat (match-string 1) "[[" (match-string 2)
1497 ":" (match-string 3) "]]")))
1498 (put-text-property 0 (length s) 'face 'org-link s)
1499 (replace-match s t t))))
1500 (goto-char (point-min))
1501 (while (re-search-forward org-bracket-link-regexp nil t)
1502 (org-if-unprotected
1503 (let* ((s (concat "[[" (setq xx (save-match-data
1504 (org-link-expand-abbrev (match-string 1))))
1506 (if (match-end 3)
1507 (match-string 2)
1508 (concat "[" xx "]"))
1509 "]")))
1510 (put-text-property 0 (length s) 'face 'org-link s)
1511 (replace-match s t t))))
1513 ;; Find multiline emphasis and put them into single line
1514 (when (plist-get parameters :emph-multiline)
1515 (goto-char (point-min))
1516 (while (re-search-forward org-emph-re nil t)
1517 (if (not (= (char-after (match-beginning 3))
1518 (char-after (match-beginning 4))))
1519 (org-if-unprotected
1520 (subst-char-in-region (match-beginning 0) (match-end 0)
1521 ?\n ?\ t)
1522 (goto-char (1- (match-end 0))))
1523 (goto-char (1+ (match-beginning 0))))))
1525 (setq rtn (buffer-string)))
1526 (kill-buffer " org-mode-tmp")
1527 rtn))
1529 (defun org-export-grab-title-from-buffer ()
1530 "Get a title for the current document, from looking at the buffer."
1531 (let ((inhibit-read-only t))
1532 (save-excursion
1533 (goto-char (point-min))
1534 (let ((end (save-excursion (outline-next-heading) (point))))
1535 (when (re-search-forward "^[ \t]*[^|# \t\r\n].*\n" end t)
1536 ;; Mark the line so that it will not be exported as normal text.
1537 (org-unmodified
1538 (add-text-properties (match-beginning 0) (match-end 0)
1539 (list :org-license-to-kill t)))
1540 ;; Return the title string
1541 (org-trim (match-string 0)))))))
1543 (defun org-export-get-title-from-subtree ()
1544 "Return subtree title and exclude it from export."
1545 (let (title (m (mark)))
1546 (save-excursion
1547 (goto-char (region-beginning))
1548 (when (and (org-at-heading-p)
1549 (>= (org-end-of-subtree t t) (region-end)))
1550 ;; This is a subtree, we take the title from the first heading
1551 (goto-char (region-beginning))
1552 (looking-at org-todo-line-regexp)
1553 (setq title (match-string 3))
1554 (org-unmodified
1555 (add-text-properties (point) (1+ (point-at-eol))
1556 (list :org-license-to-kill t)))))
1557 title))
1559 (defun org-solidify-link-text (s &optional alist)
1560 "Take link text and make a safe target out of it."
1561 (save-match-data
1562 (let* ((rtn
1563 (mapconcat
1564 'identity
1565 (org-split-string s "[ \t\r\n]+") "=="))
1566 (a (assoc rtn alist)))
1567 (or (cdr a) rtn))))
1569 (defun org-get-min-level (lines)
1570 "Get the minimum level in LINES."
1571 (let ((re "^\\(\\*+\\) ") l min)
1572 (catch 'exit
1573 (while (setq l (pop lines))
1574 (if (string-match re l)
1575 (throw 'exit (org-tr-level (length (match-string 1 l))))))
1576 1)))
1578 ;; Variable holding the vector with section numbers
1579 (defvar org-section-numbers (make-vector org-level-max 0))
1581 (defun org-init-section-numbers ()
1582 "Initialize the vector for the section numbers."
1583 (let* ((level -1)
1584 (numbers (nreverse (org-split-string "" "\\.")))
1585 (depth (1- (length org-section-numbers)))
1586 (i depth) number-string)
1587 (while (>= i 0)
1588 (if (> i level)
1589 (aset org-section-numbers i 0)
1590 (setq number-string (or (car numbers) "0"))
1591 (if (string-match "\\`[A-Z]\\'" number-string)
1592 (aset org-section-numbers i
1593 (- (string-to-char number-string) ?A -1))
1594 (aset org-section-numbers i (string-to-number number-string)))
1595 (pop numbers))
1596 (setq i (1- i)))))
1598 (defun org-section-number (&optional level)
1599 "Return a string with the current section number.
1600 When LEVEL is non-nil, increase section numbers on that level."
1601 (let* ((depth (1- (length org-section-numbers))) idx n (string ""))
1602 (when level
1603 (when (> level -1)
1604 (aset org-section-numbers
1605 level (1+ (aref org-section-numbers level))))
1606 (setq idx (1+ level))
1607 (while (<= idx depth)
1608 (if (not (= idx 1))
1609 (aset org-section-numbers idx 0))
1610 (setq idx (1+ idx))))
1611 (setq idx 0)
1612 (while (<= idx depth)
1613 (setq n (aref org-section-numbers idx))
1614 (setq string (concat string (if (not (string= string "")) "." "")
1615 (int-to-string n)))
1616 (setq idx (1+ idx)))
1617 (save-match-data
1618 (if (string-match "\\`\\([@0]\\.\\)+" string)
1619 (setq string (replace-match "" t nil string)))
1620 (if (string-match "\\(\\.0\\)+\\'" string)
1621 (setq string (replace-match "" t nil string))))
1622 string))
1624 ;;; Include files
1626 (defun org-export-handle-include-files ()
1627 "Include the contents of include files, with proper formatting."
1628 (let ((case-fold-search t)
1629 params file markup lang start end)
1630 (goto-char (point-min))
1631 (while (re-search-forward "^#\\+INCLUDE:?[ \t]+\\(.*\\)" nil t)
1632 (setq params (read (concat "(" (match-string 1) ")"))
1633 file (org-symname-or-string (pop params))
1634 markup (org-symname-or-string (pop params))
1635 lang (org-symname-or-string (pop params)))
1636 (delete-region (match-beginning 0) (match-end 0))
1637 (if (or (not file)
1638 (not (file-exists-p file))
1639 (not (file-readable-p file)))
1640 (insert (format "CANNOT INCLUDE FILE %s" file))
1641 (when markup
1642 (if (equal (downcase markup) "src")
1643 (setq start (format "#+begin_src %s\n" (or lang "fundamental"))
1644 end "#+end_src")
1645 (setq start (format "#+begin_%s\n" markup)
1646 end (format "#+end_%s" markup))))
1647 (insert (or start ""))
1648 (forward-char (nth 1 (insert-file-contents (expand-file-name file))))
1649 (or (bolp) (newline))
1650 (insert (or end ""))))))
1652 (defun org-symname-or-string (s)
1653 (if (symbolp s)
1654 (if s (symbol-name s) s)
1657 ;;; Fontification of code
1658 ;; Currently only for th HTML backend, but who knows....
1659 (defun org-export-replace-src-segments ()
1660 "Replace source code segments with special code for export."
1661 (let ((case-fold-search t)
1662 lang code trans)
1663 (goto-char (point-min))
1664 (while (re-search-forward
1665 "^#\\+BEGIN_SRC:?[ \t]+\\([^ \t\n]+\\)[ \t]*\n\\([^\000]+?\n\\)#\\+END_SRC.*"
1666 nil t)
1667 (setq lang (match-string 1) code (match-string 2)
1668 trans (org-export-format-source-code lang code))
1669 (replace-match trans t t))))
1671 (defvar htmlp) ;; dynamically scoped from org-exp.el
1673 (defun org-export-format-source-code (lang code)
1674 "Format CODE from language LANG and return it formatted for export.
1675 Currently, this only does something for HTML export, for all other
1676 backends, it converts the segment into an EXAMPLE segment."
1677 (save-match-data
1678 (cond
1679 (htmlp
1680 ;; We are exporting to HTML
1681 (condition-case nil (require 'htmlize) (nil t))
1682 (if (not (fboundp 'htmlize-region-for-paste))
1683 (progn
1684 ;; we do not have htmlize.el, or an old version of it
1685 (message
1686 "htmlize.el 1.34 or later is needed for source code formatting")
1687 (concat "#+BEGIN_EXAMPLE\n" code
1688 (if (string-match "\n\\'" code) "" "\n")
1689 "#+END_EXAMPLE\n"))
1690 ;; ok, we are good to go
1691 (let* ((mode (and lang (intern (concat lang "-mode"))))
1692 (org-inhibit-startup t)
1693 (org-startup-folded nil)
1694 (htmltext
1695 (with-temp-buffer
1696 (insert code)
1697 ;; Free up the protected stuff
1698 (goto-char (point-min))
1699 (while (re-search-forward "^," nil t)
1700 (replace-match ""))
1701 (if (functionp mode)
1702 (funcall mode)
1703 (fundamental-mode))
1704 (font-lock-fontify-buffer)
1705 (org-export-htmlize-region-for-paste
1706 (point-min) (point-max)))))
1707 (if (string-match "<pre\\([^>]*\\)>\n?" htmltext)
1708 (setq htmltext (replace-match "<pre class=\"src\">"
1709 t t htmltext)))
1710 (concat "#+BEGIN_HTML\n" htmltext "\n#+END_HTML\n"))))
1712 ;; This is not HTML, so just make it an example.
1713 (concat "#+BEGIN_EXAMPLE\n" code
1714 (if (string-match "\n\\'" code) "" "\n")
1715 "#+END_EXAMPLE\n")))))
1717 ;;; ASCII export
1719 (defvar org-last-level nil) ; dynamically scoped variable
1720 (defvar org-min-level nil) ; dynamically scoped variable
1721 (defvar org-levels-open nil) ; dynamically scoped parameter
1722 (defvar org-ascii-current-indentation nil) ; For communication
1724 ;;;###autoload
1725 (defun org-export-as-ascii (arg)
1726 "Export the outline as a pretty ASCII file.
1727 If there is an active region, export only the region.
1728 The prefix ARG specifies how many levels of the outline should become
1729 underlined headlines. The default is 3."
1730 (interactive "P")
1731 (setq-default org-todo-line-regexp org-todo-line-regexp)
1732 (let* ((opt-plist (org-combine-plists (org-default-export-plist)
1733 (org-infile-export-plist)))
1734 (region-p (org-region-active-p))
1735 (subtree-p
1736 (when region-p
1737 (save-excursion
1738 (goto-char (region-beginning))
1739 (and (org-at-heading-p)
1740 (>= (org-end-of-subtree t t) (region-end))))))
1741 (custom-times org-display-custom-times)
1742 (org-ascii-current-indentation '(0 . 0))
1743 (level 0) line txt
1744 (umax nil)
1745 (umax-toc nil)
1746 (case-fold-search nil)
1747 (filename (concat (file-name-as-directory
1748 (org-export-directory :ascii opt-plist))
1749 (file-name-sans-extension
1750 (or (and subtree-p
1751 (org-entry-get (region-beginning)
1752 "EXPORT_FILE_NAME" t))
1753 (file-name-nondirectory buffer-file-name)))
1754 ".txt"))
1755 (filename (if (equal (file-truename filename)
1756 (file-truename buffer-file-name))
1757 (concat filename ".txt")
1758 filename))
1759 (buffer (find-file-noselect filename))
1760 (org-levels-open (make-vector org-level-max nil))
1761 (odd org-odd-levels-only)
1762 (date (plist-get opt-plist :date))
1763 (author (plist-get opt-plist :author))
1764 (title (or (and subtree-p (org-export-get-title-from-subtree))
1765 (plist-get opt-plist :title)
1766 (and (not
1767 (plist-get opt-plist :skip-before-1st-heading))
1768 (org-export-grab-title-from-buffer))
1769 (file-name-sans-extension
1770 (file-name-nondirectory buffer-file-name))))
1771 (email (plist-get opt-plist :email))
1772 (language (plist-get opt-plist :language))
1773 (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
1774 ; (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
1775 (todo nil)
1776 (lang-words nil)
1777 (region
1778 (buffer-substring
1779 (if (org-region-active-p) (region-beginning) (point-min))
1780 (if (org-region-active-p) (region-end) (point-max))))
1781 (lines (org-split-string
1782 (org-export-preprocess-string
1783 region
1784 :for-ascii t
1785 :skip-before-1st-heading
1786 (plist-get opt-plist :skip-before-1st-heading)
1787 :drawers (plist-get opt-plist :drawers)
1788 :verbatim-multiline t
1789 :archived-trees
1790 (plist-get opt-plist :archived-trees)
1791 :add-text (plist-get opt-plist :text))
1792 "\n"))
1793 thetoc have-headings first-heading-pos
1794 table-open table-buffer)
1796 (let ((inhibit-read-only t))
1797 (org-unmodified
1798 (remove-text-properties (point-min) (point-max)
1799 '(:org-license-to-kill t))))
1801 (setq org-min-level (org-get-min-level lines))
1802 (setq org-last-level org-min-level)
1803 (org-init-section-numbers)
1805 (find-file-noselect filename)
1807 (setq lang-words (or (assoc language org-export-language-setup)
1808 (assoc "en" org-export-language-setup)))
1809 (switch-to-buffer-other-window buffer)
1810 (erase-buffer)
1811 (fundamental-mode)
1812 ;; create local variables for all options, to make sure all called
1813 ;; functions get the correct information
1814 (mapc (lambda (x)
1815 (set (make-local-variable (cdr x))
1816 (plist-get opt-plist (car x))))
1817 org-export-plist-vars)
1818 (org-set-local 'org-odd-levels-only odd)
1819 (setq umax (if arg (prefix-numeric-value arg)
1820 org-export-headline-levels))
1821 (setq umax-toc (if (integerp org-export-with-toc)
1822 (min org-export-with-toc umax)
1823 umax))
1825 ;; File header
1826 (if title (org-insert-centered title ?=))
1827 (insert "\n")
1828 (if (and (or author email)
1829 org-export-author-info)
1830 (insert (concat (nth 1 lang-words) ": " (or author "")
1831 (if email (concat " <" email ">") "")
1832 "\n")))
1834 (cond
1835 ((and date (string-match "%" date))
1836 (setq date (format-time-string date)))
1837 (date)
1838 (t (setq date (format-time-string "%Y/%m/%d %X"))))
1840 (if (and date org-export-time-stamp-file)
1841 (insert (concat (nth 2 lang-words) ": " date"\n")))
1843 (insert "\n\n")
1845 (if org-export-with-toc
1846 (progn
1847 (push (concat (nth 3 lang-words) "\n") thetoc)
1848 (push (concat (make-string (length (nth 3 lang-words)) ?=) "\n") thetoc)
1849 (mapc '(lambda (line)
1850 (if (string-match org-todo-line-regexp
1851 line)
1852 ;; This is a headline
1853 (progn
1854 (setq have-headings t)
1855 (setq level (- (match-end 1) (match-beginning 1))
1856 level (org-tr-level level)
1857 txt (match-string 3 line)
1858 todo
1859 (or (and org-export-mark-todo-in-toc
1860 (match-beginning 2)
1861 (not (member (match-string 2 line)
1862 org-done-keywords)))
1863 ; TODO, not DONE
1864 (and org-export-mark-todo-in-toc
1865 (= level umax-toc)
1866 (org-search-todo-below
1867 line lines level))))
1868 (setq txt (org-html-expand-for-ascii txt))
1870 (while (string-match org-bracket-link-regexp txt)
1871 (setq txt
1872 (replace-match
1873 (match-string (if (match-end 2) 3 1) txt)
1874 t t txt)))
1876 (if (and (memq org-export-with-tags '(not-in-toc nil))
1877 (string-match
1878 (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
1879 txt))
1880 (setq txt (replace-match "" t t txt)))
1881 (if (string-match quote-re0 txt)
1882 (setq txt (replace-match "" t t txt)))
1884 (if org-export-with-section-numbers
1885 (setq txt (concat (org-section-number level)
1886 " " txt)))
1887 (if (<= level umax-toc)
1888 (progn
1889 (push
1890 (concat
1891 (make-string
1892 (* (max 0 (- level org-min-level)) 4) ?\ )
1893 (format (if todo "%s (*)\n" "%s\n") txt))
1894 thetoc)
1895 (setq org-last-level level))
1896 ))))
1897 lines)
1898 (setq thetoc (if have-headings (nreverse thetoc) nil))))
1900 (org-init-section-numbers)
1901 (while (setq line (pop lines))
1902 ;; Remove the quoted HTML tags.
1903 (setq line (org-html-expand-for-ascii line))
1904 ;; Remove targets
1905 (while (string-match "<<<?[^<>]*>>>?[ \t]*\n?" line)
1906 (setq line (replace-match "" t t line)))
1907 ;; Replace internal links
1908 (while (string-match org-bracket-link-regexp line)
1909 (setq line (replace-match
1910 (if (match-end 3) "[\\3]" "[\\1]")
1911 t nil line)))
1912 (when custom-times
1913 (setq line (org-translate-time line)))
1914 (cond
1915 ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
1916 ;; a Headline
1917 (setq first-heading-pos (or first-heading-pos (point)))
1918 (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
1919 txt (match-string 2 line))
1920 (org-ascii-level-start level txt umax lines))
1922 ((and org-export-with-tables
1923 (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
1924 (if (not table-open)
1925 ;; New table starts
1926 (setq table-open t table-buffer nil))
1927 ;; Accumulate lines
1928 (setq table-buffer (cons line table-buffer))
1929 (when (or (not lines)
1930 (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
1931 (car lines))))
1932 (setq table-open nil
1933 table-buffer (nreverse table-buffer))
1934 (insert (mapconcat
1935 (lambda (x)
1936 (org-fix-indentation x org-ascii-current-indentation))
1937 (org-format-table-ascii table-buffer)
1938 "\n") "\n")))
1940 (setq line (org-fix-indentation line org-ascii-current-indentation))
1941 (if (and org-export-with-fixed-width
1942 (string-match "^\\([ \t]*\\)\\(:\\)" line))
1943 (setq line (replace-match "\\1" nil nil line)))
1944 (insert line "\n"))))
1946 (normal-mode)
1948 ;; insert the table of contents
1949 (when thetoc
1950 (goto-char (point-min))
1951 (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t)
1952 (progn
1953 (goto-char (match-beginning 0))
1954 (replace-match ""))
1955 (goto-char first-heading-pos))
1956 (mapc 'insert thetoc)
1957 (or (looking-at "[ \t]*\n[ \t]*\n")
1958 (insert "\n\n")))
1960 ;; Convert whitespace place holders
1961 (goto-char (point-min))
1962 (let (beg end)
1963 (while (setq beg (next-single-property-change (point) 'org-whitespace))
1964 (setq end (next-single-property-change beg 'org-whitespace))
1965 (goto-char beg)
1966 (delete-region beg end)
1967 (insert (make-string (- end beg) ?\ ))))
1969 (save-buffer)
1970 ;; remove display and invisible chars
1971 (let (beg end)
1972 (goto-char (point-min))
1973 (while (setq beg (next-single-property-change (point) 'display))
1974 (setq end (next-single-property-change beg 'display))
1975 (delete-region beg end)
1976 (goto-char beg)
1977 (insert "=>"))
1978 (goto-char (point-min))
1979 (while (setq beg (next-single-property-change (point) 'org-cwidth))
1980 (setq end (next-single-property-change beg 'org-cwidth))
1981 (delete-region beg end)
1982 (goto-char beg)))
1983 (goto-char (point-min))))
1985 (defun org-export-ascii-clean-string ()
1986 "Do extra work for ASCII export"
1987 (goto-char (point-min))
1988 (while (re-search-forward org-verbatim-re nil t)
1989 (goto-char (match-end 2))
1990 (backward-delete-char 1) (insert "'")
1991 (goto-char (match-beginning 2))
1992 (delete-char 1) (insert "`")
1993 (goto-char (match-end 2))))
1995 (defun org-search-todo-below (line lines level)
1996 "Search the subtree below LINE for any TODO entries."
1997 (let ((rest (cdr (memq line lines)))
1998 (re org-todo-line-regexp)
1999 line lv todo)
2000 (catch 'exit
2001 (while (setq line (pop rest))
2002 (if (string-match re line)
2003 (progn
2004 (setq lv (- (match-end 1) (match-beginning 1))
2005 todo (and (match-beginning 2)
2006 (not (member (match-string 2 line)
2007 org-done-keywords))))
2008 ; TODO, not DONE
2009 (if (<= lv level) (throw 'exit nil))
2010 (if todo (throw 'exit t))))))))
2012 (defun org-html-expand-for-ascii (line)
2013 "Handle quoted HTML for ASCII export."
2014 (if org-export-html-expand
2015 (while (string-match "@<[^<>\n]*>" line)
2016 ;; We just remove the tags for now.
2017 (setq line (replace-match "" nil nil line))))
2018 line)
2020 (defun org-insert-centered (s &optional underline)
2021 "Insert the string S centered and underline it with character UNDERLINE."
2022 (let ((ind (max (/ (- 80 (string-width s)) 2) 0)))
2023 (insert (make-string ind ?\ ) s "\n")
2024 (if underline
2025 (insert (make-string ind ?\ )
2026 (make-string (string-width s) underline)
2027 "\n"))))
2029 (defun org-ascii-level-start (level title umax &optional lines)
2030 "Insert a new level in ASCII export."
2031 (let (char (n (- level umax 1)) (ind 0))
2032 (if (> level umax)
2033 (progn
2034 (insert (make-string (* 2 n) ?\ )
2035 (char-to-string (nth (% n (length org-export-ascii-bullets))
2036 org-export-ascii-bullets))
2037 " " title "\n")
2038 ;; find the indentation of the next non-empty line
2039 (catch 'stop
2040 (while lines
2041 (if (string-match "^\\* " (car lines)) (throw 'stop nil))
2042 (if (string-match "^\\([ \t]*\\)\\S-" (car lines))
2043 (throw 'stop (setq ind (org-get-indentation (car lines)))))
2044 (pop lines)))
2045 (setq org-ascii-current-indentation (cons (* 2 (1+ n)) ind)))
2046 (if (or (not (equal (char-before) ?\n))
2047 (not (equal (char-before (1- (point))) ?\n)))
2048 (insert "\n"))
2049 (setq char (nth (- umax level) (reverse org-export-ascii-underline)))
2050 (unless org-export-with-tags
2051 (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
2052 (setq title (replace-match "" t t title))))
2053 (if org-export-with-section-numbers
2054 (setq title (concat (org-section-number level) " " title)))
2055 (insert title "\n" (make-string (string-width title) char) "\n")
2056 (setq org-ascii-current-indentation '(0 . 0)))))
2058 ;;;###autoload
2059 (defun org-export-visible (type arg)
2060 "Create a copy of the visible part of the current buffer, and export it.
2061 The copy is created in a temporary buffer and removed after use.
2062 TYPE is the final key (as a string) that also select the export command in
2063 the `C-c C-e' export dispatcher.
2064 As a special case, if the you type SPC at the prompt, the temporary
2065 org-mode file will not be removed but presented to you so that you can
2066 continue to use it. The prefix arg ARG is passed through to the exporting
2067 command."
2068 (interactive
2069 (list (progn
2070 (message "Export visible: [a]SCII [h]tml [b]rowse HTML [H/R]uffer with HTML [x]OXO [ ]keep buffer")
2071 (read-char-exclusive))
2072 current-prefix-arg))
2073 (if (not (member type '(?a ?\C-a ?b ?\C-b ?h ?x ?\ )))
2074 (error "Invalid export key"))
2075 (let* ((binding (cdr (assoc type
2076 '((?a . org-export-as-ascii)
2077 (?\C-a . org-export-as-ascii)
2078 (?b . org-export-as-html-and-open)
2079 (?\C-b . org-export-as-html-and-open)
2080 (?h . org-export-as-html)
2081 (?H . org-export-as-html-to-buffer)
2082 (?R . org-export-region-as-html)
2083 (?x . org-export-as-xoxo)))))
2084 (keepp (equal type ?\ ))
2085 (file buffer-file-name)
2086 (buffer (get-buffer-create "*Org Export Visible*"))
2087 s e)
2088 ;; Need to hack the drawers here.
2089 (save-excursion
2090 (goto-char (point-min))
2091 (while (re-search-forward org-drawer-regexp nil t)
2092 (goto-char (match-beginning 1))
2093 (or (org-invisible-p) (org-flag-drawer nil))))
2094 (with-current-buffer buffer (erase-buffer))
2095 (save-excursion
2096 (setq s (goto-char (point-min)))
2097 (while (not (= (point) (point-max)))
2098 (goto-char (org-find-invisible))
2099 (append-to-buffer buffer s (point))
2100 (setq s (goto-char (org-find-visible))))
2101 (org-cycle-hide-drawers 'all)
2102 (goto-char (point-min))
2103 (unless keepp
2104 ;; Copy all comment lines to the end, to make sure #+ settings are
2105 ;; still available for the second export step. Kind of a hack, but
2106 ;; does do the trick.
2107 (if (looking-at "#[^\r\n]*")
2108 (append-to-buffer buffer (match-beginning 0) (1+ (match-end 0))))
2109 (while (re-search-forward "[\n\r]#[^\n\r]*" nil t)
2110 (append-to-buffer buffer (1+ (match-beginning 0))
2111 (min (point-max) (1+ (match-end 0))))))
2112 (set-buffer buffer)
2113 (let ((buffer-file-name file)
2114 (org-inhibit-startup t))
2115 (org-mode)
2116 (show-all)
2117 (unless keepp (funcall binding arg))))
2118 (if (not keepp)
2119 (kill-buffer buffer)
2120 (switch-to-buffer-other-window buffer)
2121 (goto-char (point-min)))))
2123 (defun org-find-visible ()
2124 (let ((s (point)))
2125 (while (and (not (= (point-max) (setq s (next-overlay-change s))))
2126 (get-char-property s 'invisible)))
2128 (defun org-find-invisible ()
2129 (let ((s (point)))
2130 (while (and (not (= (point-max) (setq s (next-overlay-change s))))
2131 (not (get-char-property s 'invisible))))
2134 ;;; HTML export
2136 (defvar org-archive-location) ;; gets loades with the org-archive require.
2137 (defun org-get-current-options ()
2138 "Return a string with current options as keyword options.
2139 Does include HTML export options as well as TODO and CATEGORY stuff."
2140 (require 'org-archive)
2141 (format
2142 "#+TITLE: %s
2143 #+AUTHOR: %s
2144 #+EMAIL: %s
2145 #+DATE: %s
2146 #+LANGUAGE: %s
2147 #+TEXT: Some descriptive text to be emitted. Several lines OK.
2148 #+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
2150 #+LINK_UP: %s
2151 #+LINK_HOME: %s
2152 #+CATEGORY: %s
2153 #+SEQ_TODO: %s
2154 #+TYP_TODO: %s
2155 #+PRIORITIES: %c %c %c
2156 #+DRAWERS: %s
2157 #+STARTUP: %s %s %s %s %s
2158 #+TAGS: %s
2159 #+ARCHIVE: %s
2160 #+LINK: %s
2162 (buffer-name) (user-full-name) user-mail-address
2163 (format-time-string (car org-time-stamp-formats))
2164 org-export-default-language
2165 org-export-headline-levels
2166 org-export-with-section-numbers
2167 org-export-with-toc
2168 org-export-preserve-breaks
2169 org-export-html-expand
2170 org-export-with-fixed-width
2171 org-export-with-tables
2172 org-export-with-sub-superscripts
2173 org-export-with-special-strings
2174 org-export-with-footnotes
2175 org-export-with-emphasize
2176 org-export-with-TeX-macros
2177 org-export-with-LaTeX-fragments
2178 org-export-skip-text-before-1st-heading
2179 org-export-with-drawers
2180 org-export-with-tags
2181 (if (featurep 'org-jsinfo) (org-infojs-options-inbuffer-template) "")
2182 org-export-html-link-up
2183 org-export-html-link-home
2184 (file-name-nondirectory buffer-file-name)
2185 "TODO FEEDBACK VERIFY DONE"
2186 "Me Jason Marie DONE"
2187 org-highest-priority org-lowest-priority org-default-priority
2188 (mapconcat 'identity org-drawers " ")
2189 (cdr (assoc org-startup-folded
2190 '((nil . "showall") (t . "overview") (content . "content"))))
2191 (if org-odd-levels-only "odd" "oddeven")
2192 (if org-hide-leading-stars "hidestars" "showstars")
2193 (if org-startup-align-all-tables "align" "noalign")
2194 (cond ((eq org-log-done t) "logdone")
2195 ((equal org-log-done 'note) "lognotedone")
2196 ((not org-log-done) "nologdone"))
2197 (or (mapconcat (lambda (x)
2198 (cond
2199 ((equal '(:startgroup) x) "{")
2200 ((equal '(:endgroup) x) "}")
2201 ((cdr x) (format "%s(%c)" (car x) (cdr x)))
2202 (t (car x))))
2203 (or org-tag-alist (org-get-buffer-tags)) " ") "")
2204 org-archive-location
2205 "org file:~/org/%s.org"
2208 ;;;###autoload
2209 (defun org-insert-export-options-template ()
2210 "Insert into the buffer a template with information for exporting."
2211 (interactive)
2212 (if (not (bolp)) (newline))
2213 (let ((s (org-get-current-options)))
2214 (and (string-match "#\\+CATEGORY" s)
2215 (setq s (substring s 0 (match-beginning 0))))
2216 (insert s)))
2218 ;;;###autoload
2219 (defun org-export-as-html-and-open (arg)
2220 "Export the outline as HTML and immediately open it with a browser.
2221 If there is an active region, export only the region.
2222 The prefix ARG specifies how many levels of the outline should become
2223 headlines. The default is 3. Lower levels will become bulleted lists."
2224 (interactive "P")
2225 (org-export-as-html arg 'hidden)
2226 (org-open-file buffer-file-name))
2228 ;;;###autoload
2229 (defun org-export-as-html-batch ()
2230 "Call `org-export-as-html', may be used in batch processing as
2231 emacs --batch
2232 --load=$HOME/lib/emacs/org.el
2233 --eval \"(setq org-export-headline-levels 2)\"
2234 --visit=MyFile --funcall org-export-as-html-batch"
2235 (org-export-as-html org-export-headline-levels 'hidden))
2237 ;;;###autoload
2238 (defun org-export-as-html-to-buffer (arg)
2239 "Call `org-exort-as-html` with output to a temporary buffer.
2240 No file is created. The prefix ARG is passed through to `org-export-as-html'."
2241 (interactive "P")
2242 (org-export-as-html arg nil nil "*Org HTML Export*")
2243 (switch-to-buffer-other-window "*Org HTML Export*"))
2245 ;;;###autoload
2246 (defun org-replace-region-by-html (beg end)
2247 "Assume the current region has org-mode syntax, and convert it to HTML.
2248 This can be used in any buffer. For example, you could write an
2249 itemized list in org-mode syntax in an HTML buffer and then use this
2250 command to convert it."
2251 (interactive "r")
2252 (let (reg html buf pop-up-frames)
2253 (save-window-excursion
2254 (if (org-mode-p)
2255 (setq html (org-export-region-as-html
2256 beg end t 'string))
2257 (setq reg (buffer-substring beg end)
2258 buf (get-buffer-create "*Org tmp*"))
2259 (with-current-buffer buf
2260 (erase-buffer)
2261 (insert reg)
2262 (org-mode)
2263 (setq html (org-export-region-as-html
2264 (point-min) (point-max) t 'string)))
2265 (kill-buffer buf)))
2266 (delete-region beg end)
2267 (insert html)))
2269 ;;;###autoload
2270 (defun org-export-region-as-html (beg end &optional body-only buffer)
2271 "Convert region from BEG to END in org-mode buffer to HTML.
2272 If prefix arg BODY-ONLY is set, omit file header, footer, and table of
2273 contents, and only produce the region of converted text, useful for
2274 cut-and-paste operations.
2275 If BUFFER is a buffer or a string, use/create that buffer as a target
2276 of the converted HTML. If BUFFER is the symbol `string', return the
2277 produced HTML as a string and leave not buffer behind. For example,
2278 a Lisp program could call this function in the following way:
2280 (setq html (org-export-region-as-html beg end t 'string))
2282 When called interactively, the output buffer is selected, and shown
2283 in a window. A non-interactive call will only return the buffer."
2284 (interactive "r\nP")
2285 (when (interactive-p)
2286 (setq buffer "*Org HTML Export*"))
2287 (let ((transient-mark-mode t) (zmacs-regions t)
2288 rtn)
2289 (goto-char end)
2290 (set-mark (point)) ;; to activate the region
2291 (goto-char beg)
2292 (setq rtn (org-export-as-html
2293 nil nil nil
2294 buffer body-only))
2295 (if (fboundp 'deactivate-mark) (deactivate-mark))
2296 (if (and (interactive-p) (bufferp rtn))
2297 (switch-to-buffer-other-window rtn)
2298 rtn)))
2300 (defvar html-table-tag nil) ; dynamically scoped into this.
2301 ;;;###autoload
2302 (defun org-export-as-html (arg &optional hidden ext-plist
2303 to-buffer body-only pub-dir)
2304 "Export the outline as a pretty HTML file.
2305 If there is an active region, export only the region. The prefix
2306 ARG specifies how many levels of the outline should become
2307 headlines. The default is 3. Lower levels will become bulleted
2308 lists. When HIDDEN is non-nil, don't display the HTML buffer.
2309 EXT-PLIST is a property list with external parameters overriding
2310 org-mode's default settings, but still inferior to file-local
2311 settings. When TO-BUFFER is non-nil, create a buffer with that
2312 name and export to that buffer. If TO-BUFFER is the symbol
2313 `string', don't leave any buffer behind but just return the
2314 resulting HTML as a string. When BODY-ONLY is set, don't produce
2315 the file header and footer, simply return the content of
2316 <body>...</body>, without even the body tags themselves. When
2317 PUB-DIR is set, use this as the publishing directory."
2318 (interactive "P")
2320 ;; Make sure we have a file name when we need it.
2321 (when (and (not (or to-buffer body-only))
2322 (not buffer-file-name))
2323 (if (buffer-base-buffer)
2324 (org-set-local 'buffer-file-name
2325 (with-current-buffer (buffer-base-buffer)
2326 buffer-file-name))
2327 (error "Need a file name to be able to export.")))
2329 (message "Exporting...")
2330 (setq-default org-todo-line-regexp org-todo-line-regexp)
2331 (setq-default org-deadline-line-regexp org-deadline-line-regexp)
2332 (setq-default org-done-keywords org-done-keywords)
2333 (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp)
2334 (let* ((opt-plist
2335 (org-export-process-option-filters
2336 (org-combine-plists (org-default-export-plist)
2337 ext-plist
2338 (org-infile-export-plist))))
2340 (style (plist-get opt-plist :style))
2341 (html-extension (plist-get opt-plist :html-extension))
2342 (link-validate (plist-get opt-plist :link-validation-function))
2343 valid thetoc have-headings first-heading-pos
2344 (odd org-odd-levels-only)
2345 (region-p (org-region-active-p))
2346 (subtree-p
2347 (when region-p
2348 (save-excursion
2349 (goto-char (region-beginning))
2350 (and (org-at-heading-p)
2351 (>= (org-end-of-subtree t t) (region-end))))))
2352 ;; The following two are dynamically scoped into other
2353 ;; routines below.
2354 (org-current-export-dir
2355 (or pub-dir (org-export-directory :html opt-plist)))
2356 (org-current-export-file buffer-file-name)
2357 (level 0) (line "") (origline "") txt todo
2358 (umax nil)
2359 (umax-toc nil)
2360 (filename (if to-buffer nil
2361 (expand-file-name
2362 (concat
2363 (file-name-sans-extension
2364 (or (and subtree-p
2365 (org-entry-get (region-beginning)
2366 "EXPORT_FILE_NAME" t))
2367 (file-name-nondirectory buffer-file-name)))
2368 "." html-extension)
2369 (file-name-as-directory
2370 (or pub-dir (org-export-directory :html opt-plist))))))
2371 (current-dir (if buffer-file-name
2372 (file-name-directory buffer-file-name)
2373 default-directory))
2374 (buffer (if to-buffer
2375 (cond
2376 ((eq to-buffer 'string) (get-buffer-create "*Org HTML Export*"))
2377 (t (get-buffer-create to-buffer)))
2378 (find-file-noselect filename)))
2379 (org-levels-open (make-vector org-level-max nil))
2380 (date (plist-get opt-plist :date))
2381 (author (plist-get opt-plist :author))
2382 (title (or (and subtree-p (org-export-get-title-from-subtree))
2383 (plist-get opt-plist :title)
2384 (and (not
2385 (plist-get opt-plist :skip-before-1st-heading))
2386 (org-export-grab-title-from-buffer))
2387 (and buffer-file-name
2388 (file-name-sans-extension
2389 (file-name-nondirectory buffer-file-name)))
2390 "UNTITLED"))
2391 (html-table-tag (plist-get opt-plist :html-table-tag))
2392 (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
2393 (quote-re (concat "^\\(\\*+\\)\\([ \t]+" org-quote-string "\\>\\)"))
2394 (inquote nil)
2395 (infixed nil)
2396 (in-local-list nil)
2397 (local-list-type nil)
2398 (local-list-indent nil)
2399 (llt org-plain-list-ordered-item-terminator)
2400 (email (plist-get opt-plist :email))
2401 (language (plist-get opt-plist :language))
2402 (lang-words nil)
2403 (head-count 0) cnt
2404 (start 0)
2405 (coding-system (and (boundp 'buffer-file-coding-system)
2406 buffer-file-coding-system))
2407 (coding-system-for-write (or org-export-html-coding-system
2408 coding-system))
2409 (save-buffer-coding-system (or org-export-html-coding-system
2410 coding-system))
2411 (charset (and coding-system-for-write
2412 (fboundp 'coding-system-get)
2413 (coding-system-get coding-system-for-write
2414 'mime-charset)))
2415 (region
2416 (buffer-substring
2417 (if region-p (region-beginning) (point-min))
2418 (if region-p (region-end) (point-max))))
2419 (lines
2420 (org-split-string
2421 (org-export-preprocess-string
2422 region
2423 :emph-multiline t
2424 :for-html t
2425 :skip-before-1st-heading
2426 (plist-get opt-plist :skip-before-1st-heading)
2427 :drawers (plist-get opt-plist :drawers)
2428 :archived-trees
2429 (plist-get opt-plist :archived-trees)
2430 :add-text
2431 (plist-get opt-plist :text)
2432 :LaTeX-fragments
2433 (plist-get opt-plist :LaTeX-fragments))
2434 "[\r\n]"))
2435 table-open type
2436 table-buffer table-orig-buffer
2437 ind item-type starter didclose
2438 rpl path desc descp desc1 desc2 link
2439 snumber fnc item-tag
2442 (let ((inhibit-read-only t))
2443 (org-unmodified
2444 (remove-text-properties (point-min) (point-max)
2445 '(:org-license-to-kill t))))
2447 (message "Exporting...")
2449 (setq org-min-level (org-get-min-level lines))
2450 (setq org-last-level org-min-level)
2451 (org-init-section-numbers)
2453 (cond
2454 ((and date (string-match "%" date))
2455 (setq date (format-time-string date)))
2456 (date)
2457 (t (setq date (format-time-string "%Y/%m/%d %X"))))
2459 ;; Get the language-dependent settings
2460 (setq lang-words (or (assoc language org-export-language-setup)
2461 (assoc "en" org-export-language-setup)))
2463 ;; Switch to the output buffer
2464 (set-buffer buffer)
2465 (let ((inhibit-read-only t)) (erase-buffer))
2466 (fundamental-mode)
2468 (and (fboundp 'set-buffer-file-coding-system)
2469 (set-buffer-file-coding-system coding-system-for-write))
2471 (let ((case-fold-search nil)
2472 (org-odd-levels-only odd))
2473 ;; create local variables for all options, to make sure all called
2474 ;; functions get the correct information
2475 (mapc (lambda (x)
2476 (set (make-local-variable (cdr x))
2477 (plist-get opt-plist (car x))))
2478 org-export-plist-vars)
2479 (setq umax (if arg (prefix-numeric-value arg)
2480 org-export-headline-levels))
2481 (setq umax-toc (if (integerp org-export-with-toc)
2482 (min org-export-with-toc umax)
2483 umax))
2484 (unless body-only
2485 ;; File header
2486 (insert (format
2487 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
2488 \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
2489 <html xmlns=\"http://www.w3.org/1999/xhtml\"
2490 lang=\"%s\" xml:lang=\"%s\">
2491 <head>
2492 <title>%s</title>
2493 <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>
2494 <meta name=\"generator\" content=\"Org-mode\"/>
2495 <meta name=\"generated\" content=\"%s\"/>
2496 <meta name=\"author\" content=\"%s\"/>
2498 </head><body>
2500 language language (org-html-expand title)
2501 (or charset "iso-8859-1") date author style))
2503 (insert (or (plist-get opt-plist :preamble) ""))
2505 (when (plist-get opt-plist :auto-preamble)
2506 (if title (insert (format org-export-html-title-format
2507 (org-html-expand title))))))
2509 (if (and org-export-with-toc (not body-only))
2510 (progn
2511 (push (format "<h%d>%s</h%d>\n"
2512 org-export-html-toplevel-hlevel
2513 (nth 3 lang-words)
2514 org-export-html-toplevel-hlevel)
2515 thetoc)
2516 (push "<div id=\"text-table-of-contents\">\n" thetoc)
2517 (push "<ul>\n<li>" thetoc)
2518 (setq lines
2519 (mapcar '(lambda (line)
2520 (if (string-match org-todo-line-regexp line)
2521 ;; This is a headline
2522 (progn
2523 (setq have-headings t)
2524 (setq level (- (match-end 1) (match-beginning 1))
2525 level (org-tr-level level)
2526 txt (save-match-data
2527 (org-html-expand
2528 (org-export-cleanup-toc-line
2529 (match-string 3 line))))
2530 todo
2531 (or (and org-export-mark-todo-in-toc
2532 (match-beginning 2)
2533 (not (member (match-string 2 line)
2534 org-done-keywords)))
2535 ; TODO, not DONE
2536 (and org-export-mark-todo-in-toc
2537 (= level umax-toc)
2538 (org-search-todo-below
2539 line lines level))))
2540 (if (string-match
2541 (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") txt)
2542 (setq txt (replace-match "&nbsp;&nbsp;&nbsp;<span class=\"tag\"> \\1</span>" t nil txt)))
2543 (if (string-match quote-re0 txt)
2544 (setq txt (replace-match "" t t txt)))
2545 (setq snumber (org-section-number level))
2546 (if org-export-with-section-numbers
2547 (setq txt (concat snumber " " txt)))
2548 (if (<= level (max umax umax-toc))
2549 (setq head-count (+ head-count 1)))
2550 (if (<= level umax-toc)
2551 (progn
2552 (if (> level org-last-level)
2553 (progn
2554 (setq cnt (- level org-last-level))
2555 (while (>= (setq cnt (1- cnt)) 0)
2556 (push "\n<ul>\n<li>" thetoc))
2557 (push "\n" thetoc)))
2558 (if (< level org-last-level)
2559 (progn
2560 (setq cnt (- org-last-level level))
2561 (while (>= (setq cnt (1- cnt)) 0)
2562 (push "</li>\n</ul>" thetoc))
2563 (push "\n" thetoc)))
2564 ;; Check for targets
2565 (while (string-match org-any-target-regexp line)
2566 (setq line (replace-match
2567 (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
2568 t t line)))
2569 (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
2570 (setq txt (replace-match "" t t txt)))
2571 (push
2572 (format
2573 (if todo
2574 "</li>\n<li><a href=\"#sec-%s\"><span class=\"todo\">%s</span></a>"
2575 "</li>\n<li><a href=\"#sec-%s\">%s</a>")
2576 snumber txt) thetoc)
2578 (setq org-last-level level))
2580 line)
2581 lines))
2582 (while (> org-last-level (1- org-min-level))
2583 (setq org-last-level (1- org-last-level))
2584 (push "</li>\n</ul>\n" thetoc))
2585 (push "</div>\n" thetoc)
2586 (setq thetoc (if have-headings (nreverse thetoc) nil))))
2588 (setq head-count 0)
2589 (org-init-section-numbers)
2591 (while (setq line (pop lines) origline line)
2592 (catch 'nextline
2594 ;; end of quote section?
2595 (when (and inquote (string-match "^\\*+ " line))
2596 (insert "</pre>\n")
2597 (setq inquote nil))
2598 ;; inside a quote section?
2599 (when inquote
2600 (insert (org-html-protect line) "\n")
2601 (throw 'nextline nil))
2603 ;; verbatim lines
2604 (when (and org-export-with-fixed-width
2605 (string-match "^[ \t]*:\\(.*\\)" line))
2606 (when (not infixed)
2607 (setq infixed t)
2608 (insert "<pre>\n"))
2609 (insert (org-html-protect (match-string 1 line)) "\n")
2610 (when (and lines
2611 (not (string-match "^[ \t]*\\(:.*\\)"
2612 (car lines))))
2613 (setq infixed nil)
2614 (insert "</pre>\n"))
2615 (throw 'nextline nil))
2617 ;; Protected HTML
2618 (when (get-text-property 0 'org-protected line)
2619 (let (par)
2620 (when (re-search-backward
2621 "\\(<p>\\)\\([ \t\r\n]*\\)\\=" (- (point) 100) t)
2622 (setq par (match-string 1))
2623 (replace-match "\\2\n"))
2624 (insert line "\n")
2625 (while (and lines
2626 (not (string-match "^[ \t]*:" (car lines)))
2627 (or (= (length (car lines)) 0)
2628 (get-text-property 0 'org-protected (car lines))))
2629 (insert (pop lines) "\n"))
2630 (and par (insert "<p>\n")))
2631 (throw 'nextline nil))
2633 ;; Horizontal line
2634 (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line)
2635 (insert "\n<hr/>\n")
2636 (throw 'nextline nil))
2638 ;; Blockquotes and verse
2639 (when (equal "ORG-BLOCKQUOTE-START" line)
2640 (insert "<blockquote>\n<p>\n")
2641 (throw 'nextline nil))
2642 (when (equal "ORG-BLOCKQUOTE-END" line)
2643 (insert "</p>\n</blockquote>\n")
2644 (throw 'nextline nil))
2645 (when (equal "ORG-VERSE-START" line)
2646 (insert "<verse>\n<p>\n")
2647 (throw 'nextline nil))
2648 (when (equal "ORG-VERSE-END" line)
2649 (insert "</p>\n</verse>\n")
2650 (throw 'nextline nil))
2652 ;; make targets to anchors
2653 (while (string-match "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line)
2654 (cond
2655 ((match-end 2)
2656 (setq line (replace-match
2657 (concat "@<a name=\""
2658 (org-solidify-link-text (match-string 1 line))
2659 "\">\\nbsp@</a>")
2660 t t line)))
2661 ((and org-export-with-toc (equal (string-to-char line) ?*))
2662 ;; FIXME: NOT DEPENDENT on TOC?????????????????????
2663 (setq line (replace-match
2664 (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
2665 ; (concat "@<i>" (match-string 1 line) "@</i> ")
2666 t t line)))
2668 (setq line (replace-match
2669 (concat "@<a name=\""
2670 (org-solidify-link-text (match-string 1 line))
2671 "\" class=\"target\">" (match-string 1 line) "@</a> ")
2672 t t line)))))
2674 (setq line (org-html-handle-time-stamps line))
2676 ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
2677 ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
2678 ;; Also handle sub_superscripts and checkboxes
2679 (or (string-match org-table-hline-regexp line)
2680 (setq line (org-html-expand line)))
2682 ;; Format the links
2683 (setq start 0)
2684 (while (string-match org-bracket-link-analytic-regexp line start)
2685 (setq start (match-beginning 0))
2686 (setq type (if (match-end 2) (match-string 2 line) "internal"))
2687 (setq path (match-string 3 line))
2688 (setq desc1 (if (match-end 5) (match-string 5 line))
2689 desc2 (if (match-end 2) (concat type ":" path) path)
2690 descp (and desc1 (not (equal desc1 desc2)))
2691 desc (or desc1 desc2))
2692 ;; Make an image out of the description if that is so wanted
2693 (when (and descp (org-file-image-p desc))
2694 (save-match-data
2695 (if (string-match "^file:" desc)
2696 (setq desc (substring desc (match-end 0)))))
2697 (setq desc (concat "<img src=\"" desc "\"/>")))
2698 ;; FIXME: do we need to unescape here somewhere?
2699 (cond
2700 ((equal type "internal")
2701 (setq rpl
2702 (concat
2703 "<a href=\"#"
2704 (org-solidify-link-text
2705 (save-match-data (org-link-unescape path)) nil)
2706 "\">" desc "</a>")))
2707 ((member type '("http" "https"))
2708 ;; standard URL, just check if we need to inline an image
2709 (if (and (or (eq t org-export-html-inline-images)
2710 (and org-export-html-inline-images (not descp)))
2711 (org-file-image-p path))
2712 (setq rpl (concat "<img src=\"" type ":" path "\"/>"))
2713 (setq link (concat type ":" path))
2714 (setq rpl (concat "<a href=\"" link "\">" desc "</a>"))))
2715 ((member type '("ftp" "mailto" "news"))
2716 ;; standard URL
2717 (setq link (concat type ":" path))
2718 (setq rpl (concat "<a href=\"" link "\">" desc "</a>")))
2719 ((string= type "file")
2720 ;; FILE link
2721 (let* ((filename path)
2722 (abs-p (file-name-absolute-p filename))
2723 thefile file-is-image-p search)
2724 (save-match-data
2725 (if (string-match "::\\(.*\\)" filename)
2726 (setq search (match-string 1 filename)
2727 filename (replace-match "" t nil filename)))
2728 (setq valid
2729 (if (functionp link-validate)
2730 (funcall link-validate filename current-dir)
2732 (setq file-is-image-p (org-file-image-p filename))
2733 (setq thefile (if abs-p (expand-file-name filename) filename))
2734 (when (and org-export-html-link-org-files-as-html
2735 (string-match "\\.org$" thefile))
2736 (setq thefile (concat (substring thefile 0
2737 (match-beginning 0))
2738 "." html-extension))
2739 (if (and search
2740 ;; make sure this is can be used as target search
2741 (not (string-match "^[0-9]*$" search))
2742 (not (string-match "^\\*" search))
2743 (not (string-match "^/.*/$" search)))
2744 (setq thefile (concat thefile "#"
2745 (org-solidify-link-text
2746 (org-link-unescape search)))))
2747 (when (string-match "^file:" desc)
2748 (setq desc (replace-match "" t t desc))
2749 (if (string-match "\\.org$" desc)
2750 (setq desc (replace-match "" t t desc))))))
2751 (setq rpl (if (and file-is-image-p
2752 (or (eq t org-export-html-inline-images)
2753 (and org-export-html-inline-images
2754 (not descp))))
2755 (concat "<img src=\"" thefile "\"/>")
2756 (concat "<a href=\"" thefile "\">" desc "</a>")))
2757 (if (not valid) (setq rpl desc))))
2759 ((functionp (setq fnc (nth 2 (assoc type org-link-protocols))))
2760 (setq rpl
2761 (save-match-data
2762 (funcall fnc (org-link-unescape path) desc1 'html))))
2765 ;; just publish the path, as default
2766 (setq rpl (concat "<i>&lt;" type ":"
2767 (save-match-data (org-link-unescape path))
2768 "&gt;</i>"))))
2769 (setq line (replace-match rpl t t line)
2770 start (+ start (length rpl))))
2772 ;; TODO items
2773 (if (and (string-match org-todo-line-regexp line)
2774 (match-beginning 2))
2776 (setq line
2777 (concat (substring line 0 (match-beginning 2))
2778 "<span class=\""
2779 (if (member (match-string 2 line)
2780 org-done-keywords)
2781 "done" "todo")
2782 "\">" (match-string 2 line)
2783 "</span>" (substring line (match-end 2)))))
2785 ;; Does this contain a reference to a footnote?
2786 (when org-export-with-footnotes
2787 (setq start 0)
2788 (while (string-match "\\([^* \t].*?\\)\\[\\([0-9]+\\)\\]" line start)
2789 (if (get-text-property (match-beginning 2) 'org-protected line)
2790 (setq start (match-end 2))
2791 (let ((n (match-string 2 line)))
2792 (setq line
2793 (replace-match
2794 (format
2795 "%s<sup><a class=\"footref\" name=\"fnr.%s\" href=\"#fn.%s\">%s</a></sup>"
2796 (match-string 1 line) n n n)
2797 t t line))))))
2799 (cond
2800 ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
2801 ;; This is a headline
2802 (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
2803 txt (match-string 2 line))
2804 (if (string-match quote-re0 txt)
2805 (setq txt (replace-match "" t t txt)))
2806 (if (<= level (max umax umax-toc))
2807 (setq head-count (+ head-count 1)))
2808 (when in-local-list
2809 ;; Close any local lists before inserting a new header line
2810 (while local-list-type
2811 (org-close-li (car local-list-type))
2812 (insert (format "</%sl>\n" (car local-list-type)))
2813 (pop local-list-type))
2814 (setq local-list-indent nil
2815 in-local-list nil))
2816 (setq first-heading-pos (or first-heading-pos (point)))
2817 (org-html-level-start level txt umax
2818 (and org-export-with-toc (<= level umax))
2819 head-count)
2820 ;; QUOTES
2821 (when (string-match quote-re line)
2822 (insert "<pre>")
2823 (setq inquote t)))
2825 ((and org-export-with-tables
2826 (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
2827 (if (not table-open)
2828 ;; New table starts
2829 (setq table-open t table-buffer nil table-orig-buffer nil))
2830 ;; Accumulate lines
2831 (setq table-buffer (cons line table-buffer)
2832 table-orig-buffer (cons origline table-orig-buffer))
2833 (when (or (not lines)
2834 (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
2835 (car lines))))
2836 (setq table-open nil
2837 table-buffer (nreverse table-buffer)
2838 table-orig-buffer (nreverse table-orig-buffer))
2839 (org-close-par-maybe)
2840 (insert (org-format-table-html table-buffer table-orig-buffer))))
2842 ;; Normal lines
2843 (when (string-match
2844 (cond
2845 ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
2846 ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
2847 ((= llt ?\)) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
2848 (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
2849 line)
2850 (setq ind (org-get-string-indentation line)
2851 item-type (if (match-beginning 4) "o" "u")
2852 starter (if (match-beginning 2)
2853 (substring (match-string 2 line) 0 -1))
2854 line (substring line (match-beginning 5))
2855 item-tag nil)
2856 (if (and starter (string-match "\\(.*?\\) ::[ \t]*" line))
2857 (setq item-type "d"
2858 item-tag (match-string 1 line)
2859 line (substring line (match-end 0))))
2860 (when (and (not (equal item-type "d"))
2861 (not (string-match "[^ \t]" line)))
2862 ;; empty line. Pretend indentation is large.
2863 (setq ind (if org-empty-line-terminates-plain-lists
2865 (1+ (or (car local-list-indent) 1)))))
2866 (setq didclose nil)
2867 (while (and in-local-list
2868 (or (and (= ind (car local-list-indent))
2869 (not starter))
2870 (< ind (car local-list-indent))))
2871 (setq didclose t)
2872 (org-close-li (car local-list-type))
2873 (insert (format "</%sl>\n" (car local-list-type)))
2874 (pop local-list-type) (pop local-list-indent)
2875 (setq in-local-list local-list-indent))
2876 (cond
2877 ((and starter
2878 (or (not in-local-list)
2879 (> ind (car local-list-indent))))
2880 ;; Start new (level of) list
2881 (org-close-par-maybe)
2882 (insert (cond
2883 ((equal item-type "u") "<ul>\n<li>\n")
2884 ((equal item-type "o") "<ol>\n<li>\n")
2885 ((equal item-type "d")
2886 (format "<dl>\n<dt>%s</dt><dd>\n" item-tag))))
2887 (push item-type local-list-type)
2888 (push ind local-list-indent)
2889 (setq in-local-list t))
2890 (starter
2891 ;; continue current list
2892 (org-close-li (car local-list-type))
2893 (insert (cond
2894 ((equal (car local-list-type) "d")
2895 (format "<dt>%s</dt><dd>\n" (or item-tag "???")))
2896 (t "<li>\n"))))
2897 (didclose
2898 ;; we did close a list, normal text follows: need <p>
2899 (org-open-par)))
2900 (if (string-match "^[ \t]*\\[\\([X ]\\)\\]" line)
2901 (setq line
2902 (replace-match
2903 (if (equal (match-string 1 line) "X")
2904 "<b>[X]</b>"
2905 "<b>[<span style=\"visibility:hidden;\">X</span>]</b>")
2906 t t line))))
2908 ;; Empty lines start a new paragraph. If hand-formatted lists
2909 ;; are not fully interpreted, lines starting with "-", "+", "*"
2910 ;; also start a new paragraph.
2911 (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (org-open-par))
2913 ;; Is this the start of a footnote?
2914 (when org-export-with-footnotes
2915 (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
2916 (org-close-par-maybe)
2917 (let ((n (match-string 1 line)))
2918 (setq line (replace-match
2919 (format "<p class=\"footnote\"><sup><a class=\"footnum\" name=\"fn.%s\" href=\"#fnr.%s\">%s</a></sup>" n n n) t t line)))))
2921 ;; Check if the line break needs to be conserved
2922 (cond
2923 ((string-match "\\\\\\\\[ \t]*$" line)
2924 (setq line (replace-match "<br/>" t t line)))
2925 (org-export-preserve-breaks
2926 (setq line (concat line "<br/>"))))
2928 (insert line "\n")))))
2930 ;; Properly close all local lists and other lists
2931 (when inquote (insert "</pre>\n"))
2932 (when in-local-list
2933 ;; Close any local lists before inserting a new header line
2934 (while local-list-type
2935 (org-close-li (car local-list-type))
2936 (insert (format "</%sl>\n" (car local-list-type)))
2937 (pop local-list-type))
2938 (setq local-list-indent nil
2939 in-local-list nil))
2940 (org-html-level-start 1 nil umax
2941 (and org-export-with-toc (<= level umax))
2942 head-count)
2943 ;; the </div> to lose the last text-... div.
2944 (insert "</div>\n")
2946 (unless body-only
2947 (when (plist-get opt-plist :auto-postamble)
2948 (insert "<div id=\"postamble\">")
2949 (when (and org-export-author-info author)
2950 (insert "<p class=\"author\"> "
2951 (nth 1 lang-words) ": " author "\n")
2952 (when email
2953 (if (listp (split-string email ",+ *"))
2954 (mapc (lambda(e)
2955 (insert "<a href=\"mailto:" e "\">&lt;"
2956 e "&gt;</a>\n"))
2957 (split-string email ",+ *"))
2958 (insert "<a href=\"mailto:" email "\">&lt;"
2959 email "&gt;</a>\n")))
2960 (insert "</p>\n"))
2961 (when (and date org-export-time-stamp-file)
2962 (insert "<p class=\"date\"> "
2963 (nth 2 lang-words) ": "
2964 date "</p>\n"))
2965 (insert "</div>"))
2967 (if org-export-html-with-timestamp
2968 (insert org-export-html-html-helper-timestamp))
2969 (insert (or (plist-get opt-plist :postamble) ""))
2970 (insert "</body>\n</html>\n"))
2972 (normal-mode)
2973 (if (eq major-mode default-major-mode) (html-mode))
2975 ;; insert the table of contents
2976 (goto-char (point-min))
2977 (when thetoc
2978 (if (or (re-search-forward
2979 "<p>\\s-*\\[TABLE-OF-CONTENTS\\]\\s-*</p>" nil t)
2980 (re-search-forward
2981 "\\[TABLE-OF-CONTENTS\\]" nil t))
2982 (progn
2983 (goto-char (match-beginning 0))
2984 (replace-match ""))
2985 (goto-char first-heading-pos)
2986 (when (looking-at "\\s-*</p>")
2987 (goto-char (match-end 0))
2988 (insert "\n")))
2989 (insert "<div id=\"table-of-contents\">\n")
2990 (mapc 'insert thetoc)
2991 (insert "</div>\n"))
2992 ;; remove empty paragraphs and lists
2993 (goto-char (point-min))
2994 (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t)
2995 (replace-match ""))
2996 (goto-char (point-min))
2997 (while (re-search-forward "<li>[ \r\n\t]*</li>\n?" nil t)
2998 (replace-match ""))
2999 (goto-char (point-min))
3000 (while (re-search-forward "</ul>\\s-*<ul>\n?" nil t)
3001 (replace-match ""))
3002 ;; Convert whitespace place holders
3003 (goto-char (point-min))
3004 (let (beg end n)
3005 (while (setq beg (next-single-property-change (point) 'org-whitespace))
3006 (setq n (get-text-property beg 'org-whitespace)
3007 end (next-single-property-change beg 'org-whitespace))
3008 (goto-char beg)
3009 (delete-region beg end)
3010 (insert (format "<span style=\"visibility:hidden;\">%s</span>"
3011 (make-string n ?x)))))
3012 (or to-buffer (save-buffer))
3013 (goto-char (point-min))
3014 (message "Exporting... done")
3015 (if (eq to-buffer 'string)
3016 (prog1 (buffer-substring (point-min) (point-max))
3017 (kill-buffer (current-buffer)))
3018 (current-buffer)))))
3020 (defvar org-table-colgroup-info nil)
3021 (defun org-format-table-ascii (lines)
3022 "Format a table for ascii export."
3023 (if (stringp lines)
3024 (setq lines (org-split-string lines "\n")))
3025 (if (not (string-match "^[ \t]*|" (car lines)))
3026 ;; Table made by table.el - test for spanning
3027 lines
3029 ;; A normal org table
3030 ;; Get rid of hlines at beginning and end
3031 (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
3032 (setq lines (nreverse lines))
3033 (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
3034 (setq lines (nreverse lines))
3035 (when org-export-table-remove-special-lines
3036 ;; Check if the table has a marking column. If yes remove the
3037 ;; column and the special lines
3038 (setq lines (org-table-clean-before-export lines)))
3039 ;; Get rid of the vertical lines except for grouping
3040 (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info))
3041 rtn line vl1 start)
3042 (while (setq line (pop lines))
3043 (if (string-match org-table-hline-regexp line)
3044 (and (string-match "|\\(.*\\)|" line)
3045 (setq line (replace-match " \\1" t nil line)))
3046 (setq start 0 vl1 vl)
3047 (while (string-match "|" line start)
3048 (setq start (match-end 0))
3049 (or (pop vl1) (setq line (replace-match " " t t line)))))
3050 (push line rtn))
3051 (nreverse rtn))))
3053 (defun org-colgroup-info-to-vline-list (info)
3054 (let (vl new last)
3055 (while info
3056 (setq last new new (pop info))
3057 (if (or (memq last '(:end :startend))
3058 (memq new '(:start :startend)))
3059 (push t vl)
3060 (push nil vl)))
3061 (setq vl (nreverse vl))
3062 (and vl (setcar vl nil))
3063 vl))
3065 (defvar org-table-number-regexp) ; defined in org-table.el
3066 (defun org-format-table-html (lines olines)
3067 "Find out which HTML converter to use and return the HTML code."
3068 (if (stringp lines)
3069 (setq lines (org-split-string lines "\n")))
3070 (if (string-match "^[ \t]*|" (car lines))
3071 ;; A normal org table
3072 (org-format-org-table-html lines)
3073 ;; Table made by table.el - test for spanning
3074 (let* ((hlines (delq nil (mapcar
3075 (lambda (x)
3076 (if (string-match "^[ \t]*\\+-" x) x
3077 nil))
3078 lines)))
3079 (first (car hlines))
3080 (ll (and (string-match "\\S-+" first)
3081 (match-string 0 first)))
3082 (re (concat "^[ \t]*" (regexp-quote ll)))
3083 (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
3084 hlines))))
3085 (if (and (not spanning)
3086 (not org-export-prefer-native-exporter-for-tables))
3087 ;; We can use my own converter with HTML conversions
3088 (org-format-table-table-html lines)
3089 ;; Need to use the code generator in table.el, with the original text.
3090 (org-format-table-table-html-using-table-generate-source olines)))))
3092 (defvar org-table-number-fraction) ; defined in org-table.el
3093 (defun org-format-org-table-html (lines &optional splice)
3094 "Format a table into HTML."
3095 (require 'org-table)
3096 ;; Get rid of hlines at beginning and end
3097 (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
3098 (setq lines (nreverse lines))
3099 (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
3100 (setq lines (nreverse lines))
3101 (when org-export-table-remove-special-lines
3102 ;; Check if the table has a marking column. If yes remove the
3103 ;; column and the special lines
3104 (setq lines (org-table-clean-before-export lines)))
3106 (let ((head (and org-export-highlight-first-table-line
3107 (delq nil (mapcar
3108 (lambda (x) (string-match "^[ \t]*|-" x))
3109 (cdr lines)))))
3110 (nlines 0) fnum i
3111 tbopen line fields html gr colgropen)
3112 (if splice (setq head nil))
3113 (unless splice (push (if head "<thead>" "<tbody>") html))
3114 (setq tbopen t)
3115 (while (setq line (pop lines))
3116 (catch 'next-line
3117 (if (string-match "^[ \t]*|-" line)
3118 (progn
3119 (unless splice
3120 (push (if head "</thead>" "</tbody>") html)
3121 (if lines (push "<tbody>" html) (setq tbopen nil)))
3122 (setq head nil) ;; head ends here, first time around
3123 ;; ignore this line
3124 (throw 'next-line t)))
3125 ;; Break the line into fields
3126 (setq fields (org-split-string line "[ \t]*|[ \t]*"))
3127 (unless fnum (setq fnum (make-vector (length fields) 0)))
3128 (setq nlines (1+ nlines) i -1)
3129 (push (concat "<tr>"
3130 (mapconcat
3131 (lambda (x)
3132 (setq i (1+ i))
3133 (if (and (< i nlines)
3134 (string-match org-table-number-regexp x))
3135 (incf (aref fnum i)))
3136 (if head
3137 (concat (car org-export-table-header-tags) x
3138 (cdr org-export-table-header-tags))
3139 (concat (car org-export-table-data-tags) x
3140 (cdr org-export-table-data-tags))))
3141 fields "")
3142 "</tr>")
3143 html)))
3144 (unless splice (if tbopen (push "</tbody>" html)))
3145 (unless splice (push "</table>\n" html))
3146 (setq html (nreverse html))
3147 (unless splice
3148 ;; Put in col tags with the alignment (unfortuntely often ignored...)
3149 (push (mapconcat
3150 (lambda (x)
3151 (setq gr (pop org-table-colgroup-info))
3152 (format "%s<col align=\"%s\"></col>%s"
3153 (if (memq gr '(:start :startend))
3154 (prog1
3155 (if colgropen "</colgroup>\n<colgroup>" "<colgroup>")
3156 (setq colgropen t))
3158 (if (> (/ (float x) nlines) org-table-number-fraction)
3159 "right" "left")
3160 (if (memq gr '(:end :startend))
3161 (progn (setq colgropen nil) "</colgroup>")
3162 "")))
3163 fnum "")
3164 html)
3165 (if colgropen (setq html (cons (car html) (cons "</colgroup>" (cdr html)))))
3166 (push html-table-tag html))
3167 (concat (mapconcat 'identity html "\n") "\n")))
3169 (defun org-table-clean-before-export (lines)
3170 "Check if the table has a marking column.
3171 If yes remove the column and the special lines."
3172 (setq org-table-colgroup-info nil)
3173 (if (memq nil
3174 (mapcar
3175 (lambda (x) (or (string-match "^[ \t]*|-" x)
3176 (string-match "^[ \t]*| *\\([#!$*_^ /]\\) *|" x)))
3177 lines))
3178 (progn
3179 (setq org-table-clean-did-remove-column nil)
3180 (delq nil
3181 (mapcar
3182 (lambda (x)
3183 (cond
3184 ((string-match "^[ \t]*| */ *|" x)
3185 (setq org-table-colgroup-info
3186 (mapcar (lambda (x)
3187 (cond ((member x '("<" "&lt;")) :start)
3188 ((member x '(">" "&gt;")) :end)
3189 ((member x '("<>" "&lt;&gt;")) :startend)
3190 (t nil)))
3191 (org-split-string x "[ \t]*|[ \t]*")))
3192 nil)
3193 (t x)))
3194 lines)))
3195 (setq org-table-clean-did-remove-column t)
3196 (delq nil
3197 (mapcar
3198 (lambda (x)
3199 (cond
3200 ((string-match "^[ \t]*| */ *|" x)
3201 (setq org-table-colgroup-info
3202 (mapcar (lambda (x)
3203 (cond ((member x '("<" "&lt;")) :start)
3204 ((member x '(">" "&gt;")) :end)
3205 ((member x '("<>" "&lt;&gt;")) :startend)
3206 (t nil)))
3207 (cdr (org-split-string x "[ \t]*|[ \t]*"))))
3208 nil)
3209 ((string-match "^[ \t]*| *[!_^/] *|" x)
3210 nil) ; ignore this line
3211 ((or (string-match "^\\([ \t]*\\)|-+\\+" x)
3212 (string-match "^\\([ \t]*\\)|[^|]*|" x))
3213 ;; remove the first column
3214 (replace-match "\\1|" t nil x))))
3215 lines))))
3217 (defun org-format-table-table-html (lines)
3218 "Format a table generated by table.el into HTML.
3219 This conversion does *not* use `table-generate-source' from table.el.
3220 This has the advantage that Org-mode's HTML conversions can be used.
3221 But it has the disadvantage, that no cell- or row-spanning is allowed."
3222 (let (line field-buffer
3223 (head org-export-highlight-first-table-line)
3224 fields html empty)
3225 (setq html (concat html-table-tag "\n"))
3226 (while (setq line (pop lines))
3227 (setq empty "&nbsp;")
3228 (catch 'next-line
3229 (if (string-match "^[ \t]*\\+-" line)
3230 (progn
3231 (if field-buffer
3232 (progn
3233 (setq
3234 html
3235 (concat
3236 html
3237 "<tr>"
3238 (mapconcat
3239 (lambda (x)
3240 (if (equal x "") (setq x empty))
3241 (if head
3242 (concat (car org-export-table-header-tags) x
3243 (cdr org-export-table-header-tags))
3244 (concat (car org-export-table-data-tags) x
3245 (cdr org-export-table-data-tags))))
3246 field-buffer "\n")
3247 "</tr>\n"))
3248 (setq head nil)
3249 (setq field-buffer nil)))
3250 ;; Ignore this line
3251 (throw 'next-line t)))
3252 ;; Break the line into fields and store the fields
3253 (setq fields (org-split-string line "[ \t]*|[ \t]*"))
3254 (if field-buffer
3255 (setq field-buffer (mapcar
3256 (lambda (x)
3257 (concat x "<br/>" (pop fields)))
3258 field-buffer))
3259 (setq field-buffer fields))))
3260 (setq html (concat html "</table>\n"))
3261 html))
3263 (defun org-format-table-table-html-using-table-generate-source (lines)
3264 "Format a table into html, using `table-generate-source' from table.el.
3265 This has the advantage that cell- or row-spanning is allowed.
3266 But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
3267 (require 'table)
3268 (with-current-buffer (get-buffer-create " org-tmp1 ")
3269 (erase-buffer)
3270 (insert (mapconcat 'identity lines "\n"))
3271 (goto-char (point-min))
3272 (if (not (re-search-forward "|[^+]" nil t))
3273 (error "Error processing table"))
3274 (table-recognize-table)
3275 (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
3276 (table-generate-source 'html " org-tmp2 ")
3277 (set-buffer " org-tmp2 ")
3278 (buffer-substring (point-min) (point-max))))
3280 (defun org-html-handle-time-stamps (s)
3281 "Format time stamps in string S, or remove them."
3282 (catch 'exit
3283 (let (r b)
3284 (while (string-match org-maybe-keyword-time-regexp s)
3285 (if (and (match-end 1) (equal (match-string 1 s) org-clock-string))
3286 ;; never export CLOCK
3287 (throw 'exit ""))
3288 (or b (setq b (substring s 0 (match-beginning 0))))
3289 (if (not org-export-with-timestamps)
3290 (setq r (concat r (substring s 0 (match-beginning 0)))
3291 s (substring s (match-end 0)))
3292 (setq r (concat
3293 r (substring s 0 (match-beginning 0))
3294 (if (match-end 1)
3295 (format "@<span class=\"timestamp-kwd\">%s @</span>"
3296 (match-string 1 s)))
3297 (format " @<span class=\"timestamp\">%s@</span>"
3298 (substring
3299 (org-translate-time (match-string 3 s)) 1 -1)))
3300 s (substring s (match-end 0)))))
3301 ;; Line break if line started and ended with time stamp stuff
3302 (if (not r)
3304 (setq r (concat r s))
3305 (unless (string-match "\\S-" (concat b s))
3306 (setq r (concat r "@<br/>")))
3307 r))))
3309 (defun org-export-htmlize-region-for-paste (beg end)
3310 "Convert the region to HTML, using htmlize.el.
3311 This is much like `htmlize-region-for-paste', only that it uses
3312 the settings define in the org-... variables."
3313 (let* ((htmlize-output-type org-export-htmlize-output-type)
3314 (htmlize-css-name-prefix org-export-htmlize-css-font-prefix)
3315 (htmlbuf (htmlize-region beg end)))
3316 (unwind-protect
3317 (with-current-buffer htmlbuf
3318 (buffer-substring (plist-get htmlize-buffer-places 'content-start)
3319 (plist-get htmlize-buffer-places 'content-end)))
3320 (kill-buffer htmlbuf))))
3322 ;;;###autoload
3323 (defun org-export-htmlize-generate-css ()
3324 "Create the CSS for all font definitions in the current Emacs session.
3325 Use this to create face definitions in your CSS style file that can then
3326 be used by code snippets transformed by htmlize.
3327 This command just produces a buffer that contains class definitions for all
3328 faces used in the current Emacs session. You can copy and paste the ones you
3329 need into your CSS file.
3331 If you then set `org-export-htmlize-output-type' to `css', calls to
3332 the function `org-export-htmlize-region-for-paste' will produce code
3333 that uses these same face definitions."
3334 (interactive)
3335 (require 'htmlize)
3336 (and (get-buffer "*html*") (kill-buffer "*html*"))
3337 (with-temp-buffer
3338 (let ((fl (face-list))
3339 (htmlize-css-name-prefix "org-")
3340 (htmlize-output-type 'css)
3341 f i)
3342 (while (setq f (pop fl)
3343 i (and f (face-attribute f :inherit)))
3344 (when (and (symbolp f) (or (not i) (not (listp i))))
3345 (insert (org-add-props (copy-sequence "1") nil 'face f))))
3346 (htmlize-region (point-min) (point-max))))
3347 (switch-to-buffer "*html*")
3348 (goto-char (point-min))
3349 (if (re-search-forward "<style" nil t)
3350 (delete-region (point-min) (match-beginning 0)))
3351 (if (re-search-forward "</style>" nil t)
3352 (delete-region (1+ (match-end 0)) (point-max)))
3353 (beginning-of-line 1)
3354 (if (looking-at " +") (replace-match ""))
3355 (goto-char (point-min)))
3357 (defun org-html-protect (s)
3358 ;; convert & to &amp;, < to &lt; and > to &gt;
3359 (let ((start 0))
3360 (while (string-match "&" s start)
3361 (setq s (replace-match "&amp;" t t s)
3362 start (1+ (match-beginning 0))))
3363 (while (string-match "<" s)
3364 (setq s (replace-match "&lt;" t t s)))
3365 (while (string-match ">" s)
3366 (setq s (replace-match "&gt;" t t s))))
3369 (defun org-export-cleanup-toc-line (s)
3370 "Remove tags and time staps from lines going into the toc."
3371 (when (memq org-export-with-tags '(not-in-toc nil))
3372 (if (string-match (org-re " +:[[:alnum:]_@:]+: *$") s)
3373 (setq s (replace-match "" t t s))))
3374 (when org-export-remove-timestamps-from-toc
3375 (while (string-match org-maybe-keyword-time-regexp s)
3376 (setq s (replace-match "" t t s))))
3377 (while (string-match org-bracket-link-regexp s)
3378 (setq s (replace-match (match-string (if (match-end 3) 3 1) s)
3379 t t s)))
3382 (defun org-html-expand (string)
3383 "Prepare STRING for HTML export. Applies all active conversions.
3384 If there are links in the string, don't modify these."
3385 (let* ((re (concat org-bracket-link-regexp "\\|"
3386 (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$")))
3387 m s l res)
3388 (while (setq m (string-match re string))
3389 (setq s (substring string 0 m)
3390 l (match-string 0 string)
3391 string (substring string (match-end 0)))
3392 (push (org-html-do-expand s) res)
3393 (push l res))
3394 (push (org-html-do-expand string) res)
3395 (apply 'concat (nreverse res))))
3397 (defun org-html-do-expand (s)
3398 "Apply all active conversions to translate special ASCII to HTML."
3399 (setq s (org-html-protect s))
3400 (if org-export-html-expand
3401 (let ((start 0))
3402 (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
3403 (setq s (replace-match "<\\1>" t nil s)))))
3404 (if org-export-with-emphasize
3405 (setq s (org-export-html-convert-emphasize s)))
3406 (if org-export-with-special-strings
3407 (setq s (org-export-html-convert-special-strings s)))
3408 (if org-export-with-sub-superscripts
3409 (setq s (org-export-html-convert-sub-super s)))
3410 (if org-export-with-TeX-macros
3411 (let ((start 0) wd ass)
3412 (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)" s start))
3413 (if (get-text-property (match-beginning 0) 'org-protected s)
3414 (setq start (match-end 0))
3415 (setq wd (match-string 1 s))
3416 (if (setq ass (assoc wd org-html-entities))
3417 (setq s (replace-match (or (cdr ass)
3418 (concat "&" (car ass) ";"))
3419 t t s))
3420 (setq start (+ start (length wd))))))))
3423 (defun org-create-multibrace-regexp (left right n)
3424 "Create a regular expression which will match a balanced sexp.
3425 Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
3426 as single character strings.
3427 The regexp returned will match the entire expression including the
3428 delimiters. It will also define a single group which contains the
3429 match except for the outermost delimiters. The maximum depth of
3430 stacked delimiters is N. Escaping delimiters is not possible."
3431 (let* ((nothing (concat "[^" "\\" left "\\" right "]*?"))
3432 (or "\\|")
3433 (re nothing)
3434 (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
3435 (while (> n 1)
3436 (setq n (1- n)
3437 re (concat re or next)
3438 next (concat "\\(?:" nothing left next right "\\)+" nothing)))
3439 (concat left "\\(" re "\\)" right)))
3441 (defvar org-match-substring-regexp
3442 (concat
3443 "\\([^\\]\\)\\([_^]\\)\\("
3444 "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
3445 "\\|"
3446 "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
3447 "\\|"
3448 "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
3449 "The regular expression matching a sub- or superscript.")
3451 (defvar org-match-substring-with-braces-regexp
3452 (concat
3453 "\\([^\\]\\)\\([_^]\\)\\("
3454 "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
3455 "\\)")
3456 "The regular expression matching a sub- or superscript, forcing braces.")
3458 (defconst org-export-html-special-string-regexps
3459 '(("\\\\-" . "&shy;")
3460 ("---\\([^-]\\)" . "&mdash;\\1")
3461 ("--\\([^-]\\)" . "&ndash;\\1")
3462 ("\\.\\.\\." . "&hellip;"))
3463 "Regular expressions for special string conversion.")
3465 (defun org-export-html-convert-special-strings (string)
3466 "Convert special characters in STRING to HTML."
3467 (let ((all org-export-html-special-string-regexps)
3468 e a re rpl start)
3469 (while (setq a (pop all))
3470 (setq re (car a) rpl (cdr a) start 0)
3471 (while (string-match re string start)
3472 (if (get-text-property (match-beginning 0) 'org-protected string)
3473 (setq start (match-end 0))
3474 (setq string (replace-match rpl t nil string)))))
3475 string))
3477 (defun org-export-html-convert-sub-super (string)
3478 "Convert sub- and superscripts in STRING to HTML."
3479 (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
3480 (while (string-match org-match-substring-regexp string s)
3481 (cond
3482 ((and requireb (match-end 8)) (setq s (match-end 2)))
3483 ((get-text-property (match-beginning 2) 'org-protected string)
3484 (setq s (match-end 2)))
3486 (setq s (match-end 1)
3487 key (if (string= (match-string 2 string) "_") "sub" "sup")
3488 c (or (match-string 8 string)
3489 (match-string 6 string)
3490 (match-string 5 string))
3491 string (replace-match
3492 (concat (match-string 1 string)
3493 "<" key ">" c "</" key ">")
3494 t t string)))))
3495 (while (string-match "\\\\\\([_^]\\)" string)
3496 (setq string (replace-match (match-string 1 string) t t string)))
3497 string))
3499 (defun org-export-html-convert-emphasize (string)
3500 "Apply emphasis."
3501 (let ((s 0) rpl)
3502 (while (string-match org-emph-re string s)
3503 (if (not (equal
3504 (substring string (match-beginning 3) (1+ (match-beginning 3)))
3505 (substring string (match-beginning 4) (1+ (match-beginning 4)))))
3506 (setq s (match-beginning 0)
3508 (concat
3509 (match-string 1 string)
3510 (nth 2 (assoc (match-string 3 string) org-emphasis-alist))
3511 (match-string 4 string)
3512 (nth 3 (assoc (match-string 3 string)
3513 org-emphasis-alist))
3514 (match-string 5 string))
3515 string (replace-match rpl t t string)
3516 s (+ s (- (length rpl) 2)))
3517 (setq s (1+ s))))
3518 string))
3520 (defvar org-par-open nil)
3521 (defun org-open-par ()
3522 "Insert <p>, but first close previous paragraph if any."
3523 (org-close-par-maybe)
3524 (insert "\n<p>")
3525 (setq org-par-open t))
3526 (defun org-close-par-maybe ()
3527 "Close paragraph if there is one open."
3528 (when org-par-open
3529 (insert "</p>")
3530 (setq org-par-open nil)))
3531 (defun org-close-li (&optional type)
3532 "Close <li> if necessary."
3533 (org-close-par-maybe)
3534 (insert (if (equal type "d") "</dd>\n" "</li>\n")))
3536 (defvar body-only) ; dynamically scoped into this.
3537 (defun org-html-level-start (level title umax with-toc head-count)
3538 "Insert a new level in HTML export.
3539 When TITLE is nil, just close all open levels."
3540 (org-close-par-maybe)
3541 (let ((target (and title (org-get-text-property-any 0 'target title)))
3542 (l org-level-max)
3543 snumber)
3544 (while (>= l level)
3545 (if (aref org-levels-open (1- l))
3546 (progn
3547 (org-html-level-close l umax)
3548 (aset org-levels-open (1- l) nil)))
3549 (setq l (1- l)))
3550 (when title
3551 ;; If title is nil, this means this function is called to close
3552 ;; all levels, so the rest is done only if title is given
3553 (when (string-match (org-re "\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
3554 (setq title (replace-match
3555 (if org-export-with-tags
3556 (save-match-data
3557 (concat
3558 "&nbsp;&nbsp;&nbsp;<span class=\"tag\">"
3559 (mapconcat 'identity (org-split-string
3560 (match-string 1 title) ":")
3561 "&nbsp;")
3562 "</span>"))
3564 t t title)))
3565 (if (> level umax)
3566 (progn
3567 (if (aref org-levels-open (1- level))
3568 (progn
3569 (org-close-li)
3570 (if target
3571 (insert (format "<li id=\"%s\">" target) title "<br/>\n")
3572 (insert "<li>" title "<br/>\n")))
3573 (aset org-levels-open (1- level) t)
3574 (org-close-par-maybe)
3575 (if target
3576 (insert (format "<ul>\n<li id=\"%s\">" target)
3577 title "<br/>\n")
3578 (insert "<ul>\n<li>" title "<br/>\n"))))
3579 (aset org-levels-open (1- level) t)
3580 (setq snumber (org-section-number level))
3581 (if (and org-export-with-section-numbers (not body-only))
3582 (setq title (concat snumber " " title)))
3583 (setq level (+ level org-export-html-toplevel-hlevel -1))
3584 (unless (= head-count 1) (insert "\n</div>\n"))
3585 (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"
3586 snumber level level snumber title level snumber))
3587 (org-open-par)))))
3589 (defun org-get-text-property-any (pos prop &optional object)
3590 (or (get-text-property pos prop object)
3591 (and (setq pos (next-single-property-change pos prop object))
3592 (get-text-property pos prop object))))
3594 (defun org-html-level-close (level max-outline-level)
3595 "Terminate one level in HTML export."
3596 (if (<= level max-outline-level)
3597 (insert "</div>\n")
3598 (org-close-li)
3599 (insert "</ul>\n")))
3601 ;;; iCalendar export
3603 ;;;###autoload
3604 (defun org-export-icalendar-this-file ()
3605 "Export current file as an iCalendar file.
3606 The iCalendar file will be located in the same directory as the Org-mode
3607 file, but with extension `.ics'."
3608 (interactive)
3609 (org-export-icalendar nil buffer-file-name))
3611 ;;;###autoload
3612 (defun org-export-icalendar-all-agenda-files ()
3613 "Export all files in `org-agenda-files' to iCalendar .ics files.
3614 Each iCalendar file will be located in the same directory as the Org-mode
3615 file, but with extension `.ics'."
3616 (interactive)
3617 (apply 'org-export-icalendar nil (org-agenda-files t)))
3619 ;;;###autoload
3620 (defun org-export-icalendar-combine-agenda-files ()
3621 "Export all files in `org-agenda-files' to a single combined iCalendar file.
3622 The file is stored under the name `org-combined-agenda-icalendar-file'."
3623 (interactive)
3624 (apply 'org-export-icalendar t (org-agenda-files t)))
3626 (defun org-export-icalendar (combine &rest files)
3627 "Create iCalendar files for all elements of FILES.
3628 If COMBINE is non-nil, combine all calendar entries into a single large
3629 file and store it under the name `org-combined-agenda-icalendar-file'."
3630 (save-excursion
3631 (org-prepare-agenda-buffers files)
3632 (let* ((dir (org-export-directory
3633 :ical (list :publishing-directory
3634 org-export-publishing-directory)))
3635 file ical-file ical-buffer category started org-agenda-new-buffers)
3636 (and (get-buffer "*ical-tmp*") (kill-buffer "*ical-tmp*"))
3637 (when combine
3638 (setq ical-file
3639 (if (file-name-absolute-p org-combined-agenda-icalendar-file)
3640 org-combined-agenda-icalendar-file
3641 (expand-file-name org-combined-agenda-icalendar-file dir))
3642 ical-buffer (org-get-agenda-file-buffer ical-file))
3643 (set-buffer ical-buffer) (erase-buffer))
3644 (while (setq file (pop files))
3645 (catch 'nextfile
3646 (org-check-agenda-file file)
3647 (set-buffer (org-get-agenda-file-buffer file))
3648 (unless combine
3649 (setq ical-file (concat (file-name-as-directory dir)
3650 (file-name-sans-extension
3651 (file-name-nondirectory buffer-file-name))
3652 ".ics"))
3653 (setq ical-buffer (org-get-agenda-file-buffer ical-file))
3654 (with-current-buffer ical-buffer (erase-buffer)))
3655 (setq category (or org-category
3656 (file-name-sans-extension
3657 (file-name-nondirectory buffer-file-name))))
3658 (if (symbolp category) (setq category (symbol-name category)))
3659 (let ((standard-output ical-buffer))
3660 (if combine
3661 (and (not started) (setq started t)
3662 (org-start-icalendar-file org-icalendar-combined-name))
3663 (org-start-icalendar-file category))
3664 (org-print-icalendar-entries combine)
3665 (when (or (and combine (not files)) (not combine))
3666 (org-finish-icalendar-file)
3667 (set-buffer ical-buffer)
3668 (save-buffer)
3669 (run-hooks 'org-after-save-iCalendar-file-hook)
3670 (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
3671 ))))
3672 (org-release-buffers org-agenda-new-buffers))))
3674 (defvar org-after-save-iCalendar-file-hook nil
3675 "Hook run after an iCalendar file has been saved.
3676 The iCalendar buffer is still current when this hook is run.
3677 A good way to use this is to tell a desktop calenndar application to re-read
3678 the iCalendar file.")
3680 (defvar org-agenda-default-appointment-duration) ; defined in org-agenda.el
3681 (defun org-print-icalendar-entries (&optional combine)
3682 "Print iCalendar entries for the current Org-mode file to `standard-output'.
3683 When COMBINE is non nil, add the category to each line."
3684 (require 'org-agenda)
3685 (let ((re1 (concat org-ts-regexp "\\|<%%([^>\n]+>"))
3686 (re2 (concat "--?-?\\(" org-ts-regexp "\\)"))
3687 (dts (org-ical-ts-to-string
3688 (format-time-string (cdr org-time-stamp-formats) (current-time))
3689 "DTSTART"))
3690 hd ts ts2 state status (inc t) pos b sexp rrule
3691 scheduledp deadlinep tmp pri category entry location summary desc
3692 (sexp-buffer (get-buffer-create "*ical-tmp*")))
3693 (org-refresh-category-properties)
3694 (save-excursion
3695 (goto-char (point-min))
3696 (while (re-search-forward re1 nil t)
3697 (catch :skip
3698 (org-agenda-skip)
3699 (when (boundp 'org-icalendar-verify-function)
3700 (unless (funcall org-icalendar-verify-function)
3701 (outline-next-heading)
3702 (backward-char 1)
3703 (throw :skip nil)))
3704 (setq pos (match-beginning 0)
3705 ts (match-string 0)
3706 inc t
3707 hd (condition-case nil
3708 (org-icalendar-cleanup-string
3709 (org-get-heading))
3710 (error (throw :skip nil)))
3711 summary (org-icalendar-cleanup-string
3712 (org-entry-get nil "SUMMARY"))
3713 desc (org-icalendar-cleanup-string
3714 (or (org-entry-get nil "DESCRIPTION")
3715 (and org-icalendar-include-body (org-get-entry)))
3716 t org-icalendar-include-body)
3717 location (org-icalendar-cleanup-string
3718 (org-entry-get nil "LOCATION"))
3719 category (org-get-category))
3720 (if (looking-at re2)
3721 (progn
3722 (goto-char (match-end 0))
3723 (setq ts2 (match-string 1)
3724 inc (not (string-match "[0-9]\\{1,2\\}:[0-9][0-9]" ts2))))
3725 (setq tmp (buffer-substring (max (point-min)
3726 (- pos org-ds-keyword-length))
3727 pos)
3728 ts2 (if (string-match "[0-9]\\{1,2\\}:[0-9][0-9]-\\([0-9]\\{1,2\\}:[0-9][0-9]\\)" ts)
3729 (progn
3730 (setq inc nil)
3731 (replace-match "\\1" t nil ts))
3733 deadlinep (string-match org-deadline-regexp tmp)
3734 scheduledp (string-match org-scheduled-regexp tmp)
3735 ;; donep (org-entry-is-done-p)
3737 (if (or (string-match org-tr-regexp hd)
3738 (string-match org-ts-regexp hd))
3739 (setq hd (replace-match "" t t hd)))
3740 (if (string-match "\\+\\([0-9]+\\)\\([dwmy]\\)>" ts)
3741 (setq rrule
3742 (concat "\nRRULE:FREQ="
3743 (cdr (assoc
3744 (match-string 2 ts)
3745 '(("d" . "DAILY")("w" . "WEEKLY")
3746 ("m" . "MONTHLY")("y" . "YEARLY"))))
3747 ";INTERVAL=" (match-string 1 ts)))
3748 (setq rrule ""))
3749 (setq summary (or summary hd))
3750 (if (string-match org-bracket-link-regexp summary)
3751 (setq summary
3752 (replace-match (if (match-end 3)
3753 (match-string 3 summary)
3754 (match-string 1 summary))
3755 t t summary)))
3756 (if deadlinep (setq summary (concat "DL: " summary)))
3757 (if scheduledp (setq summary (concat "S: " summary)))
3758 (if (string-match "\\`<%%" ts)
3759 (with-current-buffer sexp-buffer
3760 (insert (substring ts 1 -1) " " summary "\n"))
3761 (princ (format "BEGIN:VEVENT
3763 %s%s
3764 SUMMARY:%s%s%s
3765 CATEGORIES:%s
3766 END:VEVENT\n"
3767 (org-ical-ts-to-string ts "DTSTART")
3768 (org-ical-ts-to-string ts2 "DTEND" inc)
3769 rrule summary
3770 (if (and desc (string-match "\\S-" desc))
3771 (concat "\nDESCRIPTION: " desc) "")
3772 (if (and location (string-match "\\S-" location))
3773 (concat "\nLOCATION: " location) "")
3774 category)))))
3776 (when (and org-icalendar-include-sexps
3777 (condition-case nil (require 'icalendar) (error nil))
3778 (fboundp 'icalendar-export-region))
3779 ;; Get all the literal sexps
3780 (goto-char (point-min))
3781 (while (re-search-forward "^&?%%(" nil t)
3782 (catch :skip
3783 (org-agenda-skip)
3784 (setq b (match-beginning 0))
3785 (goto-char (1- (match-end 0)))
3786 (forward-sexp 1)
3787 (end-of-line 1)
3788 (setq sexp (buffer-substring b (point)))
3789 (with-current-buffer sexp-buffer
3790 (insert sexp "\n"))
3791 (princ (org-diary-to-ical-string sexp-buffer)))))
3793 (when org-icalendar-include-todo
3794 (goto-char (point-min))
3795 (while (re-search-forward org-todo-line-regexp nil t)
3796 (catch :skip
3797 (org-agenda-skip)
3798 (when (boundp 'org-icalendar-verify-function)
3799 (unless (funcall org-icalendar-verify-function)
3800 (outline-next-heading)
3801 (backward-char 1)
3802 (throw :skip nil)))
3803 (setq state (match-string 2))
3804 (setq status (if (member state org-done-keywords)
3805 "COMPLETED" "NEEDS-ACTION"))
3806 (when (and state
3807 (or (not (member state org-done-keywords))
3808 (eq org-icalendar-include-todo 'all))
3809 (not (member org-archive-tag (org-get-tags-at)))
3811 (setq hd (match-string 3)
3812 summary (org-icalendar-cleanup-string
3813 (org-entry-get nil "SUMMARY"))
3814 desc (org-icalendar-cleanup-string
3815 (or (org-entry-get nil "DESCRIPTION")
3816 (and org-icalendar-include-body (org-get-entry)))
3817 t org-icalendar-include-body)
3818 location (org-icalendar-cleanup-string
3819 (org-entry-get nil "LOCATION")))
3820 (if (string-match org-bracket-link-regexp hd)
3821 (setq hd (replace-match (if (match-end 3) (match-string 3 hd)
3822 (match-string 1 hd))
3823 t t hd)))
3824 (if (string-match org-priority-regexp hd)
3825 (setq pri (string-to-char (match-string 2 hd))
3826 hd (concat (substring hd 0 (match-beginning 1))
3827 (substring hd (match-end 1))))
3828 (setq pri org-default-priority))
3829 (setq pri (floor (1+ (* 8. (/ (float (- org-lowest-priority pri))
3830 (- org-lowest-priority org-highest-priority))))))
3832 (princ (format "BEGIN:VTODO
3834 SUMMARY:%s%s%s
3835 CATEGORIES:%s
3836 SEQUENCE:1
3837 PRIORITY:%d
3838 STATUS:%s
3839 END:VTODO\n"
3841 (or summary hd)
3842 (if (and location (string-match "\\S-" location))
3843 (concat "\nLOCATION: " location) "")
3844 (if (and desc (string-match "\\S-" desc))
3845 (concat "\nDESCRIPTION: " desc) "")
3846 category pri status)))))))))
3848 (defun org-icalendar-cleanup-string (s &optional is-body maxlength)
3849 "Take out stuff and quote what needs to be quoted.
3850 When IS-BODY is non-nil, assume that this is the body of an item, clean up
3851 whitespace, newlines, drawers, and timestamps, and cut it down to MAXLENGTH
3852 characters."
3853 (if (not s)
3855 (when is-body
3856 (let ((re (concat "\\(" org-drawer-regexp "\\)[^\000]*?:END:.*\n?"))
3857 (re2 (concat "^[ \t]*" org-keyword-time-regexp ".*\n?")))
3858 (while (string-match re s) (setq s (replace-match "" t t s)))
3859 (while (string-match re2 s) (setq s (replace-match "" t t s)))))
3860 (let ((start 0))
3861 (while (string-match "\\([,;]\\)" s start)
3862 (setq start (+ (match-beginning 0) 2)
3863 s (replace-match "\\\\\\1" nil nil s))))
3864 (when is-body
3865 (while (string-match "[ \t]*\n[ \t]*" s)
3866 (setq s (replace-match "\\n" t t s))))
3867 (setq s (org-trim s))
3868 (if is-body
3869 (if maxlength
3870 (if (and (numberp maxlength)
3871 (> (length s) maxlength))
3872 (setq s (substring s 0 maxlength)))))
3875 (defun org-get-entry ()
3876 "Clean-up description string."
3877 (save-excursion
3878 (org-back-to-heading t)
3879 (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
3881 (defun org-start-icalendar-file (name)
3882 "Start an iCalendar file by inserting the header."
3883 (let ((user user-full-name)
3884 (name (or name "unknown"))
3885 (timezone (cadr (current-time-zone))))
3886 (princ
3887 (format "BEGIN:VCALENDAR
3888 VERSION:2.0
3889 X-WR-CALNAME:%s
3890 PRODID:-//%s//Emacs with Org-mode//EN
3891 X-WR-TIMEZONE:%s
3892 CALSCALE:GREGORIAN\n" name user timezone))))
3894 (defun org-finish-icalendar-file ()
3895 "Finish an iCalendar file by inserting the END statement."
3896 (princ "END:VCALENDAR\n"))
3898 (defun org-ical-ts-to-string (s keyword &optional inc)
3899 "Take a time string S and convert it to iCalendar format.
3900 KEYWORD is added in front, to make a complete line like DTSTART....
3901 When INC is non-nil, increase the hour by two (if time string contains
3902 a time), or the day by one (if it does not contain a time)."
3903 (let ((t1 (org-parse-time-string s 'nodefault))
3904 t2 fmt have-time time)
3905 (if (and (car t1) (nth 1 t1) (nth 2 t1))
3906 (setq t2 t1 have-time t)
3907 (setq t2 (org-parse-time-string s)))
3908 (let ((s (car t2)) (mi (nth 1 t2)) (h (nth 2 t2))
3909 (d (nth 3 t2)) (m (nth 4 t2)) (y (nth 5 t2)))
3910 (when inc
3911 (if have-time
3912 (if org-agenda-default-appointment-duration
3913 (setq mi (+ org-agenda-default-appointment-duration mi))
3914 (setq h (+ 2 h)))
3915 (setq d (1+ d))))
3916 (setq time (encode-time s mi h d m y)))
3917 (setq fmt (if have-time ":%Y%m%dT%H%M%S" ";VALUE=DATE:%Y%m%d"))
3918 (concat keyword (format-time-string fmt time))))
3920 ;;; XOXO export
3922 (defun org-export-as-xoxo-insert-into (buffer &rest output)
3923 (with-current-buffer buffer
3924 (apply 'insert output)))
3925 (put 'org-export-as-xoxo-insert-into 'lisp-indent-function 1)
3927 ;;;###autoload
3928 (defun org-export-as-xoxo (&optional buffer)
3929 "Export the org buffer as XOXO.
3930 The XOXO buffer is named *xoxo-<source buffer name>*"
3931 (interactive (list (current-buffer)))
3932 ;; A quickie abstraction
3934 ;; Output everything as XOXO
3935 (with-current-buffer (get-buffer buffer)
3936 (let* ((pos (point))
3937 (opt-plist (org-combine-plists (org-default-export-plist)
3938 (org-infile-export-plist)))
3939 (filename (concat (file-name-as-directory
3940 (org-export-directory :xoxo opt-plist))
3941 (file-name-sans-extension
3942 (file-name-nondirectory buffer-file-name))
3943 ".html"))
3944 (out (find-file-noselect filename))
3945 (last-level 1)
3946 (hanging-li nil))
3947 (goto-char (point-min)) ;; CD: beginning-of-buffer is not allowed.
3948 ;; Check the output buffer is empty.
3949 (with-current-buffer out (erase-buffer))
3950 ;; Kick off the output
3951 (org-export-as-xoxo-insert-into out "<ol class='xoxo'>\n")
3952 (while (re-search-forward "^\\(\\*+\\)[ \t]+\\(.+\\)" (point-max) 't)
3953 (let* ((hd (match-string-no-properties 1))
3954 (level (length hd))
3955 (text (concat
3956 (match-string-no-properties 2)
3957 (save-excursion
3958 (goto-char (match-end 0))
3959 (let ((str ""))
3960 (catch 'loop
3961 (while 't
3962 (forward-line)
3963 (if (looking-at "^[ \t]\\(.*\\)")
3964 (setq str (concat str (match-string-no-properties 1)))
3965 (throw 'loop str)))))))))
3967 ;; Handle level rendering
3968 (cond
3969 ((> level last-level)
3970 (org-export-as-xoxo-insert-into out "\n<ol>\n"))
3972 ((< level last-level)
3973 (dotimes (- (- last-level level) 1)
3974 (if hanging-li
3975 (org-export-as-xoxo-insert-into out "</li>\n"))
3976 (org-export-as-xoxo-insert-into out "</ol>\n"))
3977 (when hanging-li
3978 (org-export-as-xoxo-insert-into out "</li>\n")
3979 (setq hanging-li nil)))
3981 ((equal level last-level)
3982 (if hanging-li
3983 (org-export-as-xoxo-insert-into out "</li>\n")))
3986 (setq last-level level)
3988 ;; And output the new li
3989 (setq hanging-li 't)
3990 (if (equal ?+ (elt text 0))
3991 (org-export-as-xoxo-insert-into out "<li class='" (substring text 1) "'>")
3992 (org-export-as-xoxo-insert-into out "<li>" text))))
3994 ;; Finally finish off the ol
3995 (dotimes (- last-level 1)
3996 (if hanging-li
3997 (org-export-as-xoxo-insert-into out "</li>\n"))
3998 (org-export-as-xoxo-insert-into out "</ol>\n"))
4000 (goto-char pos)
4001 ;; Finish the buffer off and clean it up.
4002 (switch-to-buffer-other-window out)
4003 (indent-region (point-min) (point-max) nil)
4004 (save-buffer)
4005 (goto-char (point-min))
4008 (provide 'org-exp)
4010 ;; arch-tag: 65985fe9-095c-49c7-a7b6-cb4ee15c0a95
4012 ;;; org-exp.el ends here