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