* gnus/nnheader.el (nnheader-find-file-noselect):
[emacs.git] / lisp / org / org-html.el
blob3170f3ae07ceb387d0387d70eabc5ecdaf612586
1 ;;; org-html.el --- HTML export for Org-mode
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
4 ;; Free Software Foundation, Inc.
6 ;; Author: Carsten Dominik <carsten at orgmode dot org>
7 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; Homepage: http://orgmode.org
9 ;; Version: 6.29c
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 ;;; Commentary:
29 (require 'org-exp)
31 (declare-function org-id-find-id-file "org-id" (id))
32 (declare-function htmlize-region "ext:htmlize" (beg end))
34 (defgroup org-export-html nil
35 "Options specific for HTML export of Org-mode files."
36 :tag "Org Export HTML"
37 :group 'org-export)
39 (defcustom org-export-html-footnotes-section "<div id=\"footnotes\">
40 <h2 class=\"footnotes\">%s: </h2>
41 <div id=\"text-footnotes\">
43 </div>
44 </div>"
45 "Format for the footnotes section.
46 Should contain a two instances of %s. The first will be replaced with the
47 language-specific word for \"Footnotes\", the second one will be replaced
48 by the footnotes themselves."
49 :group 'org-export-html
50 :type 'string)
52 (defcustom org-export-html-footnote-format "<sup>%s</sup>"
53 "The format for the footnote reference.
54 %s will be replaced by the footnote reference itself."
55 :group 'org-export-html
56 :type 'string)
58 (defcustom org-export-html-coding-system nil
59 "Coding system for HTML export, defaults to buffer-file-coding-system."
60 :group 'org-export-html
61 :type 'coding-system)
63 (defcustom org-export-html-extension "html"
64 "The extension for exported HTML files."
65 :group 'org-export-html
66 :type 'string)
68 (defcustom org-export-html-xml-declaration
69 '(("html" . "<?xml version=\"1.0\" encoding=\"%s\"?>")
70 ("php" . "<?php echo \"<?xml version=\\\"1.0\\\" encoding=\\\"%s\\\" ?>\"; ?>"))
71 "The extension for exported HTML files.
72 %s will be replaced with the charset of the exported file.
73 This may be a string, or an alist with export extensions
74 and corresponding declarations."
75 :group 'org-export-html
76 :type '(choice
77 (string :tag "Single declaration")
78 (repeat :tag "Dependent on extension"
79 (cons (string :tag "Extension")
80 (string :tag "Declaration")))))
82 (defcustom org-export-html-style-include-scripts t
83 "Non-nil means, include the javascript snippets in exported HTML files.
84 The actual script is defined in `org-export-html-scripts' and should
85 not be modified."
86 :group 'org-export-html
87 :type 'boolean)
89 (defconst org-export-html-scripts
90 "<script type=\"text/javascript\">
91 <!--/*--><![CDATA[/*><!--*/
92 function CodeHighlightOn(elem, id)
94 var target = document.getElementById(id);
95 if(null != target) {
96 elem.cacheClassElem = elem.className;
97 elem.cacheClassTarget = target.className;
98 target.className = \"code-highlighted\";
99 elem.className = \"code-highlighted\";
102 function CodeHighlightOff(elem, id)
104 var target = document.getElementById(id);
105 if(elem.cacheClassElem)
106 elem.className = elem.cacheClassElem;
107 if(elem.cacheClassTarget)
108 target.className = elem.cacheClassTarget;
110 /*]]>*///-->
111 </script>"
112 "Basic javascript that is needed by HTML files produced by Org-mode.")
114 (defconst org-export-html-style-default
115 "<style type=\"text/css\">
116 <!--/*--><![CDATA[/*><!--*/
117 html { font-family: Times, serif; font-size: 12pt; }
118 .title { text-align: center; }
119 .todo { color: red; }
120 .done { color: green; }
121 .tag { background-color: #add8e6; font-weight:normal }
122 .target { }
123 .timestamp { color: #bebebe; }
124 .timestamp-kwd { color: #5f9ea0; }
125 p.verse { margin-left: 3% }
126 pre {
127 border: 1pt solid #AEBDCC;
128 background-color: #F3F5F7;
129 padding: 5pt;
130 font-family: courier, monospace;
131 font-size: 90%;
132 overflow:auto;
134 table { border-collapse: collapse; }
135 td, th { vertical-align: top; }
136 dt { font-weight: bold; }
137 div.figure { padding: 0.5em; }
138 div.figure p { text-align: center; }
139 .linenr { font-size:smaller }
140 .code-highlighted {background-color:#ffff00;}
141 .org-info-js_info-navigation { border-style:none; }
142 #org-info-js_console-label { font-size:10px; font-weight:bold;
143 white-space:nowrap; }
144 .org-info-js_search-highlight {background-color:#ffff00; color:#000000;
145 font-weight:bold; }
146 /*]]>*/-->
147 </style>"
148 "The default style specification for exported HTML files.
149 Please use the variables `org-export-html-style' and
150 `org-export-html-style-extra' to add to this style. If you wish to not
151 have the default style included, customize the variable
152 `org-export-html-style-include-default'.")
154 (defcustom org-export-html-style-include-default t
155 "Non-nil means, include the default style in exported HTML files.
156 The actual style is defined in `org-export-html-style-default' and should
157 not be modified. Use the variables `org-export-html-style' to add
158 your own style information."
159 :group 'org-export-html
160 :type 'boolean)
161 ;;;###autoload
162 (put 'org-export-html-style 'safe-local-variable 'booleanp)
164 (defcustom org-export-html-style ""
165 "Org-wide style definitions for exported HTML files.
167 This variable needs to contain the full HTML structure to provide a style,
168 including the surrounding HTML tags. If you set the value of this variable,
169 you should consider to include definitions for the following classes:
170 title, todo, done, timestamp, timestamp-kwd, tag, target.
172 For example, a valid value would be:
174 <style type=\"text/css\">
175 <![CDATA[
176 p { font-weight: normal; color: gray; }
177 h1 { color: black; }
178 .title { text-align: center; }
179 .todo, .timestamp-kwd { color: red; }
180 .done { color: green; }
182 </style>
184 If you'd like to refer to en external style file, use something like
186 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
188 As the value of this option simply gets inserted into the HTML <head> header,
189 you can \"misuse\" it to add arbitrary text to the header.
190 See also the variable `org-export-html-style-extra'."
191 :group 'org-export-html
192 :type 'string)
193 ;;;###autoload
194 (put 'org-export-html-style 'safe-local-variable 'stringp)
196 (defcustom org-export-html-style-extra ""
197 "Additional style information for HTML export.
198 The value of this variable is inserted into the HTML buffer right after
199 the value of `org-export-html-style'. Use this variable for per-file
200 settings of style information, and do not forget to surround the style
201 settings with <style>...</style> tags."
202 :group 'org-export-html
203 :type 'string)
204 ;;;###autoload
205 (put 'org-export-html-style-extra 'safe-local-variable 'stringp)
207 (defcustom org-export-html-tag-class-prefix ""
208 "Prefix to clas names for TODO keywords.
209 Each tag gets a class given by the tag itself, with this prefix.
210 The default prefix is empty because it is nice to just use the keyword
211 as a class name. But if you get into conflicts with other, existing
212 CSS classes, then this prefic can be very useful."
213 :group 'org-export-html
214 :type 'string)
216 (defcustom org-export-html-todo-kwd-class-prefix ""
217 "Prefix to clas names for TODO keywords.
218 Each TODO keyword gets a class given by the keyword itself, with this prefix.
219 The default prefix is empty because it is nice to just use the keyword
220 as a class name. But if you get into conflicts with other, existing
221 CSS classes, then this prefic can be very useful."
222 :group 'org-export-html
223 :type 'string)
225 (defcustom org-export-html-title-format "<h1 class=\"title\">%s</h1>\n"
226 "Format for typesetting the document title in HTML export."
227 :group 'org-export-html
228 :type 'string)
230 (defcustom org-export-html-home/up-format
231 "<div style=\"text-align:right;font-size:70%%;white-space:nowrap;\">
232 <a accesskey=\"h\" href=\"%s\"> UP </a>
234 <a accesskey=\"H\" href=\"%s\"> HOME </a>
235 </div>"
236 "Snippet used to insert the HOME and UP links. This is a format,
237 the first %s will receive the UP link, the second the HOME link.
238 If both `org-export-html-link-up' and `org-export-html-link-home' are
239 empty, the entire snippet will be ignored."
240 :group 'org-export-html
241 :type 'string)
243 (defcustom org-export-html-toplevel-hlevel 2
244 "The <H> level for level 1 headings in HTML export.
245 This is also important for the classes that will be wrapped around headlines
246 and outline structure. If this variable is 1, the top-level headlines will
247 be <h1>, and the corresponding classes will be outline-1, section-number-1,
248 and outline-text-1. If this is 2, all of these will get a 2 instead.
249 The default for this variable is 2, because we use <h1> for formatting the
250 document title."
251 :group 'org-export-html
252 :type 'string)
254 (defcustom org-export-html-link-org-files-as-html t
255 "Non-nil means, make file links to `file.org' point to `file.html'.
256 When org-mode is exporting an org-mode file to HTML, links to
257 non-html files are directly put into a href tag in HTML.
258 However, links to other Org-mode files (recognized by the
259 extension `.org.) should become links to the corresponding html
260 file, assuming that the linked org-mode file will also be
261 converted to HTML.
262 When nil, the links still point to the plain `.org' file."
263 :group 'org-export-html
264 :type 'boolean)
266 (defcustom org-export-html-inline-images 'maybe
267 "Non-nil means, inline images into exported HTML pages.
268 This is done using an <img> tag. When nil, an anchor with href is used to
269 link to the image. If this option is `maybe', then images in links with
270 an empty description will be inlined, while images with a description will
271 be linked only."
272 :group 'org-export-html
273 :type '(choice (const :tag "Never" nil)
274 (const :tag "Always" t)
275 (const :tag "When there is no description" maybe)))
277 (defcustom org-export-html-inline-image-extensions
278 '("png" "jpeg" "jpg" "gif")
279 "Extensions of image files that can be inlined into HTML."
280 :group 'org-export-html
281 :type '(repeat (string :tag "Extension")))
283 (defcustom org-export-html-table-tag
284 "<table border=\"2\" cellspacing=\"0\" cellpadding=\"6\" rules=\"groups\" frame=\"hsides\">"
285 "The HTML tag that is used to start a table.
286 This must be a <table> tag, but you may change the options like
287 borders and spacing."
288 :group 'org-export-html
289 :type 'string)
291 (defcustom org-export-table-header-tags '("<th scope=\"%s\">" . "</th>")
292 "The opening tag for table header fields.
293 This is customizable so that alignment options can be specified.
294 %s will be filled with the scope of the field, either row or col.
295 See also the variable `org-export-html-table-use-header-tags-for-first-column'."
296 :group 'org-export-tables
297 :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
299 (defcustom org-export-table-data-tags '("<td>" . "</td>")
300 "The opening tag for table data fields.
301 This is customizable so that alignment options can be specified."
302 :group 'org-export-tables
303 :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
305 (defcustom org-export-table-row-tags '("<tr>" . "</tr>")
306 "The opening tag for table data fields.
307 This is customizable so that alignment options can be specified.
308 Instead of strings, these can be Lisp forms that will be evaluated
309 for each row in order to construct the table row tags. During evaluation,
310 the variable `head' will be true when this is a header line, nil when this
311 is a body line. And the variable `nline' will contain the line number,
312 starting from 1 in the first header line. For example
314 (setq org-export-table-row-tags
315 (cons '(if head
316 \"<tr>\"
317 (if (= (mod nline 2) 1)
318 \"<tr class=\\\"tr-odd\\\">\"
319 \"<tr class=\\\"tr-even\\\">\"))
320 \"</tr>\"))
322 will give even lines the class \"tr-even\" and odd lines the class \"tr-odd\"."
323 :group 'org-export-tables
324 :type '(cons
325 (choice :tag "Opening tag"
326 (string :tag "Specify")
327 (sexp))
328 (choice :tag "Closing tag"
329 (string :tag "Specify")
330 (sexp))))
334 (defcustom org-export-html-table-use-header-tags-for-first-column nil
335 "Non-nil means, format column one in tables with header tags.
336 When nil, also column one will use data tags."
337 :group 'org-export-tables
338 :type 'boolean)
340 (defcustom org-export-html-validation-link nil
341 "Non-nil means, add validationlink to postamble of HTML exported files."
342 :group 'org-export-html
343 :type '(choice
344 (const :tag "Nothing" nil)
345 (const :tag "XHTML 1.0" "<p class=\"xhtml-validation\"><a href=\"http://validator.w3.org/check?uri=referer\">Validate XHTML 1.0</a></p>")
346 (string :tag "Specify full HTML")))
349 (defcustom org-export-html-with-timestamp nil
350 "If non-nil, write `org-export-html-html-helper-timestamp'
351 into the exported HTML text. Otherwise, the buffer will just be saved
352 to a file."
353 :group 'org-export-html
354 :type 'boolean)
356 (defcustom org-export-html-html-helper-timestamp
357 "<br/><br/><hr><p><!-- hhmts start --> <!-- hhmts end --></p>\n"
358 "The HTML tag used as timestamp delimiter for HTML-helper-mode."
359 :group 'org-export-html
360 :type 'string)
362 (defgroup org-export-htmlize nil
363 "Options for processing examples with htmlize.el."
364 :tag "Org Export Htmlize"
365 :group 'org-export-html)
367 (defcustom org-export-htmlize-output-type 'inline-css
368 "Output type to be used by htmlize when formatting code snippets.
369 We use as default `inline-css', in order to make the resulting
370 HTML self-containing.
371 However, this will fail when using Emacs in batch mode for export, because
372 then no rich font definitions are in place. It will also not be good if
373 people with different Emacs setup contribute HTML files to a website,
374 because the fonts will represent the individual setups. In these cases,
375 it is much better to let Org/Htmlize assign classes only, and to use
376 a style file to define the look of these classes.
377 To get a start for your css file, start Emacs session and make sure that
378 all the faces you are interested in are defined, for example by loading files
379 in all modes you want. Then, use the command
380 \\[org-export-htmlize-generate-css] to extract class definitions."
381 :group 'org-export-htmlize
382 :type '(choice (const css) (const inline-css)))
384 (defcustom org-export-htmlize-css-font-prefix "org-"
385 "The prefix for CSS class names for htmlize font specifications."
386 :group 'org-export-htmlize
387 :type 'string)
389 (defcustom org-export-htmlized-org-css-url nil
390 "URL pointing to a CSS file defining text colors for htmlized Emacs buffers.
391 Normally when creating an htmlized version of an Org buffer, htmlize will
392 create CSS to define the font colors. However, this does not work when
393 converting in batch mode, and it also can look bad if different people
394 with different fontification setup work on the same website.
395 When this variable is non-nil, creating an htmlized version of an Org buffer
396 using `org-export-as-org' will remove the internal CSS section and replace it
397 with a link to this URL."
398 :group 'org-export-htmlize
399 :type '(choice
400 (const :tag "Keep internal css" nil)
401 (string :tag "URL or local href")))
403 ;;; Variables, constants, and parameter plists
405 (defvar org-export-html-preamble nil
406 "Preamble, to be inserted just before <body>. Set by publishing functions.
407 This may also be a function, building and inserting the preamble.")
408 (defvar org-export-html-postamble nil
409 "Preamble, to be inserted just after </body>. Set by publishing functions.
410 This may also be a function, building and inserting the postamble.")
411 (defvar org-export-html-auto-preamble t
412 "Should default preamble be inserted? Set by publishing functions.")
413 (defvar org-export-html-auto-postamble t
414 "Should default postamble be inserted? Set by publishing functions.")
416 ;;; Hooks
418 (defvar org-export-html-after-blockquotes-hook nil
419 "Hook run during HTML export, after blockquote, verse, center are done.")
421 ;;; HTML export
423 (defun org-export-html-preprocess (parameters)
424 ;; Convert LaTeX fragments to images
425 (when (and org-current-export-file
426 (plist-get parameters :LaTeX-fragments))
427 (org-format-latex
428 (concat "ltxpng/" (file-name-sans-extension
429 (file-name-nondirectory
430 org-current-export-file)))
431 org-current-export-dir nil "Creating LaTeX image %s"))
432 (message "Exporting..."))
434 ;;;###autoload
435 (defun org-export-as-html-and-open (arg)
436 "Export the outline as HTML and immediately open it with a browser.
437 If there is an active region, export only the region.
438 The prefix ARG specifies how many levels of the outline should become
439 headlines. The default is 3. Lower levels will become bulleted lists."
440 (interactive "P")
441 (org-export-as-html arg 'hidden)
442 (org-open-file buffer-file-name))
444 ;;;###autoload
445 (defun org-export-as-html-batch ()
446 "Call `org-export-as-html', may be used in batch processing as
447 emacs --batch
448 --load=$HOME/lib/emacs/org.el
449 --eval \"(setq org-export-headline-levels 2)\"
450 --visit=MyFile --funcall org-export-as-html-batch"
451 (org-export-as-html org-export-headline-levels 'hidden))
453 ;;;###autoload
454 (defun org-export-as-html-to-buffer (arg)
455 "Call `org-export-as-html` with output to a temporary buffer.
456 No file is created. The prefix ARG is passed through to `org-export-as-html'."
457 (interactive "P")
458 (org-export-as-html arg nil nil "*Org HTML Export*")
459 (when org-export-show-temporary-export-buffer
460 (switch-to-buffer-other-window "*Org HTML Export*")))
462 ;;;###autoload
463 (defun org-replace-region-by-html (beg end)
464 "Assume the current region has org-mode syntax, and convert it to HTML.
465 This can be used in any buffer. For example, you could write an
466 itemized list in org-mode syntax in an HTML buffer and then use this
467 command to convert it."
468 (interactive "r")
469 (let (reg html buf pop-up-frames)
470 (save-window-excursion
471 (if (org-mode-p)
472 (setq html (org-export-region-as-html
473 beg end t 'string))
474 (setq reg (buffer-substring beg end)
475 buf (get-buffer-create "*Org tmp*"))
476 (with-current-buffer buf
477 (erase-buffer)
478 (insert reg)
479 (org-mode)
480 (setq html (org-export-region-as-html
481 (point-min) (point-max) t 'string)))
482 (kill-buffer buf)))
483 (delete-region beg end)
484 (insert html)))
486 ;;;###autoload
487 (defun org-export-region-as-html (beg end &optional body-only buffer)
488 "Convert region from BEG to END in org-mode buffer to HTML.
489 If prefix arg BODY-ONLY is set, omit file header, footer, and table of
490 contents, and only produce the region of converted text, useful for
491 cut-and-paste operations.
492 If BUFFER is a buffer or a string, use/create that buffer as a target
493 of the converted HTML. If BUFFER is the symbol `string', return the
494 produced HTML as a string and leave not buffer behind. For example,
495 a Lisp program could call this function in the following way:
497 (setq html (org-export-region-as-html beg end t 'string))
499 When called interactively, the output buffer is selected, and shown
500 in a window. A non-interactive call will only return the buffer."
501 (interactive "r\nP")
502 (when (interactive-p)
503 (setq buffer "*Org HTML Export*"))
504 (let ((transient-mark-mode t) (zmacs-regions t)
505 ext-plist rtn)
506 (setq ext-plist (plist-put ext-plist :ignore-subree-p t))
507 (goto-char end)
508 (set-mark (point)) ;; to activate the region
509 (goto-char beg)
510 (setq rtn (org-export-as-html
511 nil nil ext-plist
512 buffer body-only))
513 (if (fboundp 'deactivate-mark) (deactivate-mark))
514 (if (and (interactive-p) (bufferp rtn))
515 (switch-to-buffer-other-window rtn)
516 rtn)))
518 (defvar html-table-tag nil) ; dynamically scoped into this.
519 (defvar org-par-open nil)
520 ;;;###autoload
521 (defun org-export-as-html (arg &optional hidden ext-plist
522 to-buffer body-only pub-dir)
523 "Export the outline as a pretty HTML file.
524 If there is an active region, export only the region. The prefix
525 ARG specifies how many levels of the outline should become
526 headlines. The default is 3. Lower levels will become bulleted
527 lists. HIDDEN is obsolete and does nothing.
528 EXT-PLIST is a property list with external parameters overriding
529 org-mode's default settings, but still inferior to file-local
530 settings. When TO-BUFFER is non-nil, create a buffer with that
531 name and export to that buffer. If TO-BUFFER is the symbol
532 `string', don't leave any buffer behind but just return the
533 resulting HTML as a string. When BODY-ONLY is set, don't produce
534 the file header and footer, simply return the content of
535 <body>...</body>, without even the body tags themselves. When
536 PUB-DIR is set, use this as the publishing directory."
537 (interactive "P")
539 ;; Make sure we have a file name when we need it.
540 (when (and (not (or to-buffer body-only))
541 (not buffer-file-name))
542 (if (buffer-base-buffer)
543 (org-set-local 'buffer-file-name
544 (with-current-buffer (buffer-base-buffer)
545 buffer-file-name))
546 (error "Need a file name to be able to export.")))
548 (message "Exporting...")
549 (setq-default org-todo-line-regexp org-todo-line-regexp)
550 (setq-default org-deadline-line-regexp org-deadline-line-regexp)
551 (setq-default org-done-keywords org-done-keywords)
552 (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp)
553 (let* ((opt-plist
554 (org-export-process-option-filters
555 (org-combine-plists (org-default-export-plist)
556 ext-plist
557 (org-infile-export-plist))))
558 (body-only (or body-only (plist-get opt-plist :body-only)))
559 (style (concat (if (plist-get opt-plist :style-include-default)
560 org-export-html-style-default)
561 (plist-get opt-plist :style)
562 (plist-get opt-plist :style-extra)
563 "\n"
564 (if (plist-get opt-plist :style-include-scripts)
565 org-export-html-scripts)))
566 (html-extension (plist-get opt-plist :html-extension))
567 (link-validate (plist-get opt-plist :link-validation-function))
568 valid thetoc have-headings first-heading-pos
569 (odd org-odd-levels-only)
570 (region-p (org-region-active-p))
571 (rbeg (and region-p (region-beginning)))
572 (rend (and region-p (region-end)))
573 (subtree-p
574 (if (plist-get opt-plist :ignore-subree-p)
576 (when region-p
577 (save-excursion
578 (goto-char rbeg)
579 (and (org-at-heading-p)
580 (>= (org-end-of-subtree t t) rend))))))
581 (level-offset (if subtree-p
582 (save-excursion
583 (goto-char rbeg)
584 (+ (funcall outline-level)
585 (if org-odd-levels-only 1 0)))
587 (opt-plist (setq org-export-opt-plist
588 (if subtree-p
589 (org-export-add-subtree-options opt-plist rbeg)
590 opt-plist)))
591 ;; The following two are dynamically scoped into other
592 ;; routines below.
593 (org-current-export-dir
594 (or pub-dir (org-export-directory :html opt-plist)))
595 (org-current-export-file buffer-file-name)
596 (level 0) (line "") (origline "") txt todo
597 (umax nil)
598 (umax-toc nil)
599 (filename (if to-buffer nil
600 (expand-file-name
601 (concat
602 (file-name-sans-extension
603 (or (and subtree-p
604 (org-entry-get (region-beginning)
605 "EXPORT_FILE_NAME" t))
606 (file-name-nondirectory buffer-file-name)))
607 "." html-extension)
608 (file-name-as-directory
609 (or pub-dir (org-export-directory :html opt-plist))))))
610 (current-dir (if buffer-file-name
611 (file-name-directory buffer-file-name)
612 default-directory))
613 (buffer (if to-buffer
614 (cond
615 ((eq to-buffer 'string) (get-buffer-create "*Org HTML Export*"))
616 (t (get-buffer-create to-buffer)))
617 (find-file-noselect filename)))
618 (org-levels-open (make-vector org-level-max nil))
619 (date (plist-get opt-plist :date))
620 (author (plist-get opt-plist :author))
621 (title (or (and subtree-p (org-export-get-title-from-subtree))
622 (plist-get opt-plist :title)
623 (and (not
624 (plist-get opt-plist :skip-before-1st-heading))
625 (org-export-grab-title-from-buffer))
626 (and buffer-file-name
627 (file-name-sans-extension
628 (file-name-nondirectory buffer-file-name)))
629 "UNTITLED"))
630 (link-up (and (plist-get opt-plist :link-up)
631 (string-match "\\S-" (plist-get opt-plist :link-up))
632 (plist-get opt-plist :link-up)))
633 (link-home (and (plist-get opt-plist :link-home)
634 (string-match "\\S-" (plist-get opt-plist :link-home))
635 (plist-get opt-plist :link-home)))
636 (dummy (setq opt-plist (plist-put opt-plist :title title)))
637 (html-table-tag (plist-get opt-plist :html-table-tag))
638 (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
639 (quote-re (concat "^\\(\\*+\\)\\([ \t]+" org-quote-string "\\>\\)"))
640 (inquote nil)
641 (infixed nil)
642 (inverse nil)
643 (in-local-list nil)
644 (local-list-type nil)
645 (local-list-indent nil)
646 (llt org-plain-list-ordered-item-terminator)
647 (email (plist-get opt-plist :email))
648 (language (plist-get opt-plist :language))
649 (keywords (plist-get opt-plist :keywords))
650 (description (plist-get opt-plist :description))
651 (lang-words nil)
652 (head-count 0) cnt
653 (start 0)
654 (coding-system (and (boundp 'buffer-file-coding-system)
655 buffer-file-coding-system))
656 (coding-system-for-write (or org-export-html-coding-system
657 coding-system))
658 (save-buffer-coding-system (or org-export-html-coding-system
659 coding-system))
660 (charset (and coding-system-for-write
661 (fboundp 'coding-system-get)
662 (coding-system-get coding-system-for-write
663 'mime-charset)))
664 (region
665 (buffer-substring
666 (if region-p (region-beginning) (point-min))
667 (if region-p (region-end) (point-max))))
668 (lines
669 (org-split-string
670 (org-export-preprocess-string
671 region
672 :emph-multiline t
673 :for-html t
674 :skip-before-1st-heading
675 (plist-get opt-plist :skip-before-1st-heading)
676 :drawers (plist-get opt-plist :drawers)
677 :todo-keywords (plist-get opt-plist :todo-keywords)
678 :tags (plist-get opt-plist :tags)
679 :priority (plist-get opt-plist :priority)
680 :footnotes (plist-get opt-plist :footnotes)
681 :timestamps (plist-get opt-plist :timestamps)
682 :archived-trees
683 (plist-get opt-plist :archived-trees)
684 :select-tags (plist-get opt-plist :select-tags)
685 :exclude-tags (plist-get opt-plist :exclude-tags)
686 :add-text
687 (plist-get opt-plist :text)
688 :LaTeX-fragments
689 (plist-get opt-plist :LaTeX-fragments))
690 "[\r\n]"))
691 table-open type
692 table-buffer table-orig-buffer
693 ind item-type starter didclose
694 rpl path attr desc descp desc1 desc2 link
695 snumber fnc item-tag
696 footnotes footref-seen
697 id-file href
700 (let ((inhibit-read-only t))
701 (org-unmodified
702 (remove-text-properties (point-min) (point-max)
703 '(:org-license-to-kill t))))
705 (message "Exporting...")
707 (setq org-min-level (org-get-min-level lines level-offset))
708 (setq org-last-level org-min-level)
709 (org-init-section-numbers)
711 (cond
712 ((and date (string-match "%" date))
713 (setq date (format-time-string date)))
714 (date)
715 (t (setq date (format-time-string "%Y-%m-%d %T %Z"))))
717 ;; Get the language-dependent settings
718 (setq lang-words (or (assoc language org-export-language-setup)
719 (assoc "en" org-export-language-setup)))
721 ;; Switch to the output buffer
722 (set-buffer buffer)
723 (let ((inhibit-read-only t)) (erase-buffer))
724 (fundamental-mode)
725 (org-install-letbind)
727 (and (fboundp 'set-buffer-file-coding-system)
728 (set-buffer-file-coding-system coding-system-for-write))
730 (let ((case-fold-search nil)
731 (org-odd-levels-only odd))
732 ;; create local variables for all options, to make sure all called
733 ;; functions get the correct information
734 (mapc (lambda (x)
735 (set (make-local-variable (nth 2 x))
736 (plist-get opt-plist (car x))))
737 org-export-plist-vars)
738 (setq umax (if arg (prefix-numeric-value arg)
739 org-export-headline-levels))
740 (setq umax-toc (if (integerp org-export-with-toc)
741 (min org-export-with-toc umax)
742 umax))
743 (unless body-only
744 ;; File header
745 (insert (format
747 <!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
748 \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
749 <html xmlns=\"http://www.w3.org/1999/xhtml\"
750 lang=\"%s\" xml:lang=\"%s\">
751 <head>
753 <title>%s</title>
754 <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>
755 <meta name=\"generator\" content=\"Org-mode\"/>
756 <meta name=\"generated\" content=\"%s\"/>
757 <meta name=\"author\" content=\"%s\"/>
758 <meta name=\"description\" content=\"%s\"/>
759 <meta name=\"keywords\" content=\"%s\"/>
761 </head>
762 <body>
763 <div id=\"content\">
765 (format
766 (or (and (stringp org-export-html-xml-declaration)
767 org-export-html-xml-declaration)
768 (cdr (assoc html-extension org-export-html-xml-declaration))
769 (cdr (assoc "html" org-export-html-xml-declaration))
772 (or charset "iso-8859-1"))
773 language language
774 (if (or link-up link-home)
775 (concat
776 (format org-export-html-home/up-format
777 (or link-up link-home)
778 (or link-home link-up))
779 "\n")
781 (org-html-expand title)
782 (or charset "iso-8859-1")
783 date author description keywords
784 style))
786 (org-export-html-insert-plist-item opt-plist :preamble opt-plist)
788 (when (plist-get opt-plist :auto-preamble)
789 (if title (insert (format org-export-html-title-format
790 (org-html-expand title))))))
792 (if (and org-export-with-toc (not body-only))
793 (progn
794 (push (format "<h%d>%s</h%d>\n"
795 org-export-html-toplevel-hlevel
796 (nth 3 lang-words)
797 org-export-html-toplevel-hlevel)
798 thetoc)
799 (push "<div id=\"text-table-of-contents\">\n" thetoc)
800 (push "<ul>\n<li>" thetoc)
801 (setq lines
802 (mapcar '(lambda (line)
803 (if (string-match org-todo-line-regexp line)
804 ;; This is a headline
805 (progn
806 (setq have-headings t)
807 (setq level (- (match-end 1) (match-beginning 1)
808 level-offset)
809 level (org-tr-level level)
810 txt (save-match-data
811 (org-html-expand
812 (org-export-cleanup-toc-line
813 (match-string 3 line))))
814 todo
815 (or (and org-export-mark-todo-in-toc
816 (match-beginning 2)
817 (not (member (match-string 2 line)
818 org-done-keywords)))
819 ; TODO, not DONE
820 (and org-export-mark-todo-in-toc
821 (= level umax-toc)
822 (org-search-todo-below
823 line lines level))))
824 (if (string-match
825 (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") txt)
826 (setq txt (replace-match "&nbsp;&nbsp;&nbsp;<span class=\"tag\"> \\1</span>" t nil txt)))
827 (if (string-match quote-re0 txt)
828 (setq txt (replace-match "" t t txt)))
829 (setq snumber (org-section-number level))
830 (if org-export-with-section-numbers
831 (setq txt (concat snumber " " txt)))
832 (if (<= level (max umax umax-toc))
833 (setq head-count (+ head-count 1)))
834 (if (<= level umax-toc)
835 (progn
836 (if (> level org-last-level)
837 (progn
838 (setq cnt (- level org-last-level))
839 (while (>= (setq cnt (1- cnt)) 0)
840 (push "\n<ul>\n<li>" thetoc))
841 (push "\n" thetoc)))
842 (if (< level org-last-level)
843 (progn
844 (setq cnt (- org-last-level level))
845 (while (>= (setq cnt (1- cnt)) 0)
846 (push "</li>\n</ul>" thetoc))
847 (push "\n" thetoc)))
848 ;; Check for targets
849 (while (string-match org-any-target-regexp line)
850 (setq line (replace-match
851 (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
852 t t line)))
853 (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
854 (setq txt (replace-match "" t t txt)))
855 (setq href (format "sec-%s" snumber))
856 (setq href (or (cdr (assoc href org-export-preferred-target-alist)) href))
857 (push
858 (format
859 (if todo
860 "</li>\n<li><a href=\"#%s\"><span class=\"todo\">%s</span></a>"
861 "</li>\n<li><a href=\"#%s\">%s</a>")
862 href txt) thetoc)
864 (setq org-last-level level))
866 line)
867 lines))
868 (while (> org-last-level (1- org-min-level))
869 (setq org-last-level (1- org-last-level))
870 (push "</li>\n</ul>\n" thetoc))
871 (push "</div>\n" thetoc)
872 (setq thetoc (if have-headings (nreverse thetoc) nil))))
874 (setq head-count 0)
875 (org-init-section-numbers)
877 (org-open-par)
879 (while (setq line (pop lines) origline line)
880 (catch 'nextline
882 ;; end of quote section?
883 (when (and inquote (string-match "^\\*+ " line))
884 (insert "</pre>\n")
885 (org-open-par)
886 (setq inquote nil))
887 ;; inside a quote section?
888 (when inquote
889 (insert (org-html-protect line) "\n")
890 (throw 'nextline nil))
892 ;; Fixed-width, verbatim lines (examples)
893 (when (and org-export-with-fixed-width
894 (string-match "^[ \t]*:\\(\\([ \t]\\|$\\)\\(.*\\)\\)" line))
895 (when (not infixed)
896 (setq infixed t)
897 (org-close-par-maybe)
899 (insert "<pre class=\"example\">\n"))
900 (insert (org-html-protect (match-string 3 line)) "\n")
901 (when (or (not lines)
902 (not (string-match "^[ \t]*:\\(\\([ \t]\\|$\\)\\(.*\\)\\)"
903 (car lines))))
904 (setq infixed nil)
905 (insert "</pre>\n")
906 (org-open-par))
907 (throw 'nextline nil))
909 (org-export-html-close-lists-maybe line)
911 ;; Protected HTML
912 (when (get-text-property 0 'org-protected line)
913 (let (par (ind (get-text-property 0 'original-indentation line)))
914 (when (re-search-backward
915 "\\(<p>\\)\\([ \t\r\n]*\\)\\=" (- (point) 100) t)
916 (setq par (match-string 1))
917 (replace-match "\\2\n"))
918 (insert line "\n")
919 (while (and lines
920 (or (= (length (car lines)) 0)
921 (not ind)
922 (equal ind (get-text-property 0 'original-indentation (car lines))))
923 (or (= (length (car lines)) 0)
924 (get-text-property 0 'org-protected (car lines))))
925 (insert (pop lines) "\n"))
926 (and par (insert "<p>\n")))
927 (throw 'nextline nil))
929 ;; Blockquotes, verse, and center
930 (when (equal "ORG-BLOCKQUOTE-START" line)
931 (org-close-par-maybe)
932 (insert "<blockquote>\n")
933 (org-open-par)
934 (throw 'nextline nil))
935 (when (equal "ORG-BLOCKQUOTE-END" line)
936 (org-close-par-maybe)
937 (insert "\n</blockquote>\n")
938 (org-open-par)
939 (throw 'nextline nil))
940 (when (equal "ORG-VERSE-START" line)
941 (org-close-par-maybe)
942 (insert "\n<p class=\"verse\">\n")
943 (setq inverse t)
944 (throw 'nextline nil))
945 (when (equal "ORG-VERSE-END" line)
946 (insert "</p>\n")
947 (org-open-par)
948 (setq inverse nil)
949 (throw 'nextline nil))
950 (when (equal "ORG-CENTER-START" line)
951 (org-close-par-maybe)
952 (insert "\n<div style=\"text-align: center\">")
953 (org-open-par)
954 (throw 'nextline nil))
955 (when (equal "ORG-CENTER-END" line)
956 (org-close-par-maybe)
957 (insert "\n</div>")
958 (org-open-par)
959 (throw 'nextline nil))
960 (run-hooks 'org-export-html-after-blockquotes-hook)
961 (when inverse
962 (let ((i (org-get-string-indentation line)))
963 (if (> i 0)
964 (setq line (concat (mapconcat 'identity
965 (make-list (* 2 i) "\\nbsp") "")
966 " " (org-trim line))))
967 (unless (string-match "\\\\\\\\[ \t]*$" line)
968 (setq line (concat line "\\\\")))))
970 ;; make targets to anchors
971 (while (string-match
972 "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line)
973 (cond
974 ((match-end 2)
975 (setq line (replace-match
976 (format
977 "@<a name=\"%s\" id=\"%s\">@</a>"
978 (org-solidify-link-text (match-string 1 line))
979 (org-solidify-link-text (match-string 1 line)))
980 t t line)))
981 ((and org-export-with-toc (equal (string-to-char line) ?*))
982 ;; FIXME: NOT DEPENDENT on TOC?????????????????????
983 (setq line (replace-match
984 (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
985 ; (concat "@<i>" (match-string 1 line) "@</i> ")
986 t t line)))
988 (setq line (replace-match
989 (concat "@<a name=\""
990 (org-solidify-link-text (match-string 1 line))
991 "\" class=\"target\">" (match-string 1 line) "@</a> ")
992 t t line)))))
994 (setq line (org-html-handle-time-stamps line))
996 ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
997 ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
998 ;; Also handle sub_superscripts and checkboxes
999 (or (string-match org-table-hline-regexp line)
1000 (setq line (org-html-expand line)))
1002 ;; Format the links
1003 (setq start 0)
1004 (while (string-match org-bracket-link-analytic-regexp++ line start)
1005 (setq start (match-beginning 0))
1006 (setq path (save-match-data (org-link-unescape
1007 (match-string 3 line))))
1008 (setq type (cond
1009 ((match-end 2) (match-string 2 line))
1010 ((save-match-data
1011 (or (file-name-absolute-p path)
1012 (string-match "^\\.\\.?/" path)))
1013 "file")
1014 (t "internal")))
1015 (setq path (org-extract-attributes (org-link-unescape path)))
1016 (setq attr (get-text-property 0 'org-attributes path))
1017 (setq desc1 (if (match-end 5) (match-string 5 line))
1018 desc2 (if (match-end 2) (concat type ":" path) path)
1019 descp (and desc1 (not (equal desc1 desc2)))
1020 desc (or desc1 desc2))
1021 ;; Make an image out of the description if that is so wanted
1022 (when (and descp (org-file-image-p
1023 desc org-export-html-inline-image-extensions))
1024 (save-match-data
1025 (if (string-match "^file:" desc)
1026 (setq desc (substring desc (match-end 0)))))
1027 (setq desc (org-add-props
1028 (concat "<img src=\"" desc "\"/>")
1029 '(org-protected t))))
1030 ;; FIXME: do we need to unescape here somewhere?
1031 (cond
1032 ((equal type "internal")
1033 (setq rpl
1034 (concat
1035 "<a href=\""
1036 (if (= (string-to-char path) ?#) "" "#")
1037 (org-solidify-link-text
1038 (save-match-data (org-link-unescape path)) nil)
1039 "\"" attr ">"
1040 (org-export-html-format-desc desc)
1041 "</a>")))
1042 ((and (equal type "id")
1043 (setq id-file (org-id-find-id-file path)))
1044 ;; This is an id: link to another file (if it was the same file,
1045 ;; it would have become an internal link...)
1046 (save-match-data
1047 (setq id-file (file-relative-name
1048 id-file (file-name-directory org-current-export-file)))
1049 (setq id-file (concat (file-name-sans-extension id-file)
1050 "." html-extension))
1051 (setq rpl (concat "<a href=\"" id-file "#"
1052 (if (org-uuidgen-p path) "ID-")
1053 path "\""
1054 attr ">"
1055 (org-export-html-format-desc desc)
1056 "</a>"))))
1057 ((member type '("http" "https"))
1058 ;; standard URL, just check if we need to inline an image
1059 (if (and (or (eq t org-export-html-inline-images)
1060 (and org-export-html-inline-images (not descp)))
1061 (org-file-image-p
1062 path org-export-html-inline-image-extensions))
1063 (setq rpl (org-export-html-format-image
1064 (concat type ":" path) org-par-open))
1065 (setq link (concat type ":" path))
1066 (setq rpl (concat "<a href=\""
1067 (org-export-html-format-href link)
1068 "\"" attr ">"
1069 (org-export-html-format-desc desc)
1070 "</a>"))))
1071 ((member type '("ftp" "mailto" "news"))
1072 ;; standard URL
1073 (setq link (concat type ":" path))
1074 (setq rpl (concat "<a href=\""
1075 (org-export-html-format-href link)
1076 "\"" attr ">"
1077 (org-export-html-format-desc desc)
1078 "</a>")))
1080 ((string= type "coderef")
1081 (setq rpl (format "<a href=\"#coderef-%s\" class=\"coderef\" onmouseover=\"CodeHighlightOn(this, 'coderef-%s');\" onmouseout=\"CodeHighlightOff(this, 'coderef-%s');\">%s</a>"
1082 path path path
1083 (format (org-export-get-coderef-format path (and descp desc))
1084 (cdr (assoc path org-export-code-refs))))))
1086 ((functionp (setq fnc (nth 2 (assoc type org-link-protocols))))
1087 ;; The link protocol has a function for format the link
1088 (setq rpl
1089 (save-match-data
1090 (funcall fnc (org-link-unescape path) desc1 'html))))
1092 ((string= type "file")
1093 ;; FILE link
1094 (let* ((filename path)
1095 (abs-p (file-name-absolute-p filename))
1096 thefile file-is-image-p search)
1097 (save-match-data
1098 (if (string-match "::\\(.*\\)" filename)
1099 (setq search (match-string 1 filename)
1100 filename (replace-match "" t nil filename)))
1101 (setq valid
1102 (if (functionp link-validate)
1103 (funcall link-validate filename current-dir)
1105 (setq file-is-image-p
1106 (org-file-image-p
1107 filename org-export-html-inline-image-extensions))
1108 (setq thefile (if abs-p (expand-file-name filename) filename))
1109 (when (and org-export-html-link-org-files-as-html
1110 (string-match "\\.org$" thefile))
1111 (setq thefile (concat (substring thefile 0
1112 (match-beginning 0))
1113 "." html-extension))
1114 (if (and search
1115 ;; make sure this is can be used as target search
1116 (not (string-match "^[0-9]*$" search))
1117 (not (string-match "^\\*" search))
1118 (not (string-match "^/.*/$" search)))
1119 (setq thefile (concat thefile "#"
1120 (org-solidify-link-text
1121 (org-link-unescape search)))))
1122 (when (string-match "^file:" desc)
1123 (setq desc (replace-match "" t t desc))
1124 (if (string-match "\\.org$" desc)
1125 (setq desc (replace-match "" t t desc))))))
1126 (setq rpl (if (and file-is-image-p
1127 (or (eq t org-export-html-inline-images)
1128 (and org-export-html-inline-images
1129 (not descp))))
1130 (progn
1131 (message "image %s %s" thefile org-par-open)
1132 (org-export-html-format-image thefile org-par-open))
1133 (concat "<a href=\"" thefile "\"" attr ">"
1134 (org-export-html-format-desc desc)
1135 "</a>")))
1136 (if (not valid) (setq rpl desc))))
1139 ;; just publish the path, as default
1140 (setq rpl (concat "<i>&lt;" type ":"
1141 (save-match-data (org-link-unescape path))
1142 "&gt;</i>"))))
1143 (setq line (replace-match rpl t t line)
1144 start (+ start (length rpl))))
1146 ;; TODO items
1147 (if (and (string-match org-todo-line-regexp line)
1148 (match-beginning 2))
1150 (setq line
1151 (concat (substring line 0 (match-beginning 2))
1152 "<span class=\""
1153 (if (member (match-string 2 line)
1154 org-done-keywords)
1155 "done" "todo")
1156 " " (match-string 2 line)
1157 "\"> " (org-export-html-get-todo-kwd-class-name
1158 (match-string 2 line))
1159 "</span>" (substring line (match-end 2)))))
1161 ;; Does this contain a reference to a footnote?
1162 (when org-export-with-footnotes
1163 (setq start 0)
1164 (while (string-match "\\([^* \t].*\\)?\\[\\([0-9]+\\)\\]" line start)
1165 (if (get-text-property (match-beginning 2) 'org-protected line)
1166 (setq start (match-end 2))
1167 (let ((n (match-string 2 line)) extra a)
1168 (if (setq a (assoc n footref-seen))
1169 (progn
1170 (setcdr a (1+ (cdr a)))
1171 (setq extra (format ".%d" (cdr a))))
1172 (setq extra "")
1173 (push (cons n 1) footref-seen))
1174 (setq line
1175 (replace-match
1176 (format
1177 (concat (if (match-string 1 line) "%s" "")
1178 (format org-export-html-footnote-format
1179 "<a class=\"footref\" name=\"fnr.%s%s\" href=\"#fn.%s\">%s</a>"))
1180 (match-string 1 line) n extra n n)
1181 t t line))))))
1183 (cond
1184 ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
1185 ;; This is a headline
1186 (setq level (org-tr-level (- (match-end 1) (match-beginning 1)
1187 level-offset))
1188 txt (match-string 2 line))
1189 (if (string-match quote-re0 txt)
1190 (setq txt (replace-match "" t t txt)))
1191 (if (<= level (max umax umax-toc))
1192 (setq head-count (+ head-count 1)))
1193 (when in-local-list
1194 ;; Close any local lists before inserting a new header line
1195 (while local-list-type
1196 (org-close-li (car local-list-type))
1197 (insert (format "</%sl>\n" (car local-list-type)))
1198 (pop local-list-type))
1199 (setq local-list-indent nil
1200 in-local-list nil))
1201 (setq first-heading-pos (or first-heading-pos (point)))
1202 (org-html-level-start level txt umax
1203 (and org-export-with-toc (<= level umax))
1204 head-count)
1206 ;; QUOTES
1207 (when (string-match quote-re line)
1208 (org-close-par-maybe)
1209 (insert "<pre>")
1210 (setq inquote t)))
1212 ((string-match "^[ \t]*- __+[ \t]*$" line)
1213 ;; Explicit list closure
1214 (when local-list-type
1215 (let ((ind (org-get-indentation line)))
1216 (while (and local-list-indent
1217 (<= ind (car local-list-indent)))
1218 (org-close-li (car local-list-type))
1219 (insert (format "</%sl>\n" (car local-list-type)))
1220 (pop local-list-type)
1221 (pop local-list-indent))
1222 (or local-list-indent (setq in-local-list nil))))
1223 (throw 'nextline nil))
1225 ((and org-export-with-tables
1226 (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
1227 (when (not table-open)
1228 ;; New table starts
1229 (setq table-open t table-buffer nil table-orig-buffer nil))
1231 ;; Accumulate lines
1232 (setq table-buffer (cons line table-buffer)
1233 table-orig-buffer (cons origline table-orig-buffer))
1234 (when (or (not lines)
1235 (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
1236 (car lines))))
1237 (setq table-open nil
1238 table-buffer (nreverse table-buffer)
1239 table-orig-buffer (nreverse table-orig-buffer))
1240 (org-close-par-maybe)
1241 (insert (org-format-table-html table-buffer table-orig-buffer))))
1243 ;; Normal lines
1244 (when (string-match
1245 (cond
1246 ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
1247 ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
1248 ((= llt ?\)) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
1249 (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
1250 line)
1251 (setq ind (or (get-text-property 0 'original-indentation line)
1252 (org-get-string-indentation line))
1253 item-type (if (match-beginning 4) "o" "u")
1254 starter (if (match-beginning 2)
1255 (substring (match-string 2 line) 0 -1))
1256 line (substring line (match-beginning 5))
1257 item-tag nil)
1258 (if (and starter (string-match "\\(.*?\\) ::[ \t]*" line))
1259 (setq item-type "d"
1260 item-tag (match-string 1 line)
1261 line (substring line (match-end 0))))
1262 (when (and (not (equal item-type "d"))
1263 (not (string-match "[^ \t]" line)))
1264 ;; empty line. Pretend indentation is large.
1265 (setq ind (if org-empty-line-terminates-plain-lists
1267 (1+ (or (car local-list-indent) 1)))))
1268 (setq didclose nil)
1269 (while (and in-local-list
1270 (or (and (= ind (car local-list-indent))
1271 (not starter))
1272 (< ind (car local-list-indent))))
1273 (setq didclose t)
1274 (org-close-li (car local-list-type))
1275 (insert (format "</%sl>\n" (car local-list-type)))
1276 (pop local-list-type) (pop local-list-indent)
1277 (setq in-local-list local-list-indent))
1278 (cond
1279 ((and starter
1280 (or (not in-local-list)
1281 (> ind (car local-list-indent))))
1282 ;; Start new (level of) list
1283 (org-close-par-maybe)
1284 (insert (cond
1285 ((equal item-type "u") "<ul>\n<li>\n")
1286 ((equal item-type "o") "<ol>\n<li>\n")
1287 ((equal item-type "d")
1288 (format "<dl>\n<dt>%s</dt><dd>\n" item-tag))))
1289 (push item-type local-list-type)
1290 (push ind local-list-indent)
1291 (setq in-local-list t))
1292 (starter
1293 ;; continue current list
1294 (org-close-li (car local-list-type))
1295 (insert (cond
1296 ((equal (car local-list-type) "d")
1297 (format "<dt>%s</dt><dd>\n" (or item-tag "???")))
1298 (t "<li>\n"))))
1299 (didclose
1300 ;; we did close a list, normal text follows: need <p>
1301 (org-open-par)))
1302 (if (string-match "^[ \t]*\\[\\([X ]\\)\\]" line)
1303 (setq line
1304 (replace-match
1305 (if (equal (match-string 1 line) "X")
1306 "<b>[X]</b>"
1307 "<b>[<span style=\"visibility:hidden;\">X</span>]</b>")
1308 t t line))))
1310 ;; Horizontal line
1311 (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line)
1312 (if org-par-open
1313 (insert "\n</p>\n<hr/>\n<p>\n")
1314 (insert "\n<hr/>\n"))
1315 (throw 'nextline nil))
1317 ;; Empty lines start a new paragraph. If hand-formatted lists
1318 ;; are not fully interpreted, lines starting with "-", "+", "*"
1319 ;; also start a new paragraph.
1320 (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (org-open-par))
1322 ;; Is this the start of a footnote?
1323 (when org-export-with-footnotes
1324 (when (and (boundp 'footnote-section-tag-regexp)
1325 (string-match (concat "^" footnote-section-tag-regexp)
1326 line))
1327 ;; ignore this line
1328 (throw 'nextline nil))
1329 (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
1330 (org-close-par-maybe)
1331 (let ((n (match-string 1 line)))
1332 (setq org-par-open t
1333 line (replace-match
1334 (concat "<p class=\"footnote\">"
1335 (format org-export-html-footnote-format
1336 "<a class=\"footnum\" name=\"fn.%s\" href=\"#fnr.%s\">%s</a>"
1337 n n n) t t line))))))
1338 ;; Check if the line break needs to be conserved
1339 (cond
1340 ((string-match "\\\\\\\\[ \t]*$" line)
1341 (setq line (replace-match "<br/>" t t line)))
1342 (org-export-preserve-breaks
1343 (setq line (concat line "<br/>"))))
1345 ;; Check if a paragraph should be started
1346 (let ((start 0))
1347 (while (and org-par-open
1348 (string-match "\\\\par\\>" line start))
1349 ;; Leave a space in the </p> so that the footnote matcher
1350 ;; does not see this.
1351 (if (not (get-text-property (match-beginning 0)
1352 'org-protected line))
1353 (setq line (replace-match "</p ><p >" t t line)))
1354 (setq start (match-end 0))))
1356 (insert line "\n")))))
1358 ;; Properly close all local lists and other lists
1359 (when inquote
1360 (insert "</pre>\n")
1361 (org-open-par))
1362 (when in-local-list
1363 ;; Close any local lists before inserting a new header line
1364 (while local-list-type
1365 (org-close-li (car local-list-type))
1366 (insert (format "</%sl>\n" (car local-list-type)))
1367 (pop local-list-type))
1368 (setq local-list-indent nil
1369 in-local-list nil))
1370 (org-html-level-start 1 nil umax
1371 (and org-export-with-toc (<= level umax))
1372 head-count)
1373 ;; the </div> to close the last text-... div.
1374 (when (and (> umax 0) first-heading-pos) (insert "</div>\n"))
1376 (save-excursion
1377 (goto-char (point-min))
1378 (while (re-search-forward "<p class=\"footnote\">[^\000]*?\\(</p>\\|\\'\\)" nil t)
1379 (push (match-string 0) footnotes)
1380 (replace-match "" t t)))
1381 (when footnotes
1382 (insert (format org-export-html-footnotes-section
1383 (nth 4 lang-words)
1384 (mapconcat 'identity (nreverse footnotes) "\n"))
1385 "\n"))
1386 (let ((bib (org-export-html-get-bibliography)))
1387 (when bib
1388 (insert "\n" bib "\n")))
1389 (unless body-only
1390 (when (plist-get opt-plist :auto-postamble)
1391 (insert "<div id=\"postamble\">\n")
1392 (when (and org-export-author-info author)
1393 (insert "<p class=\"author\"> "
1394 (nth 1 lang-words) ": " author "\n")
1395 (when email
1396 (if (listp (split-string email ",+ *"))
1397 (mapc (lambda(e)
1398 (insert "<a href=\"mailto:" e "\">&lt;"
1399 e "&gt;</a>\n"))
1400 (split-string email ",+ *"))
1401 (insert "<a href=\"mailto:" email "\">&lt;"
1402 email "&gt;</a>\n")))
1403 (insert "</p>\n"))
1404 (when (and date org-export-time-stamp-file)
1405 (insert "<p class=\"date\"> "
1406 (nth 2 lang-words) ": "
1407 date "</p>\n"))
1408 (when org-export-creator-info
1409 (insert (format "<p class=\"creator\">HTML generated by org-mode %s in emacs %s</p>\n"
1410 org-version emacs-major-version)))
1411 (when org-export-html-validation-link
1412 (insert org-export-html-validation-link "\n"))
1413 (insert "</div>"))
1415 (if org-export-html-with-timestamp
1416 (insert org-export-html-html-helper-timestamp))
1417 (org-export-html-insert-plist-item opt-plist :postamble opt-plist)
1418 (insert "\n</div>\n</body>\n</html>\n"))
1420 (unless (plist-get opt-plist :buffer-will-be-killed)
1421 (normal-mode)
1422 (if (eq major-mode (default-value 'major-mode)) (html-mode)))
1424 ;; insert the table of contents
1425 (goto-char (point-min))
1426 (when thetoc
1427 (if (or (re-search-forward
1428 "<p>\\s-*\\[TABLE-OF-CONTENTS\\]\\s-*</p>" nil t)
1429 (re-search-forward
1430 "\\[TABLE-OF-CONTENTS\\]" nil t))
1431 (progn
1432 (goto-char (match-beginning 0))
1433 (replace-match ""))
1434 (goto-char first-heading-pos)
1435 (when (looking-at "\\s-*</p>")
1436 (goto-char (match-end 0))
1437 (insert "\n")))
1438 (insert "<div id=\"table-of-contents\">\n")
1439 (mapc 'insert thetoc)
1440 (insert "</div>\n"))
1441 ;; remove empty paragraphs and lists
1442 (goto-char (point-min))
1443 (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t)
1444 (replace-match ""))
1445 (goto-char (point-min))
1446 (while (re-search-forward "<li>[ \r\n\t]*</li>\n?" nil t)
1447 (replace-match ""))
1448 (goto-char (point-min))
1449 (while (re-search-forward "</ul>\\s-*<ul>\n?" nil t)
1450 (replace-match ""))
1451 ;; Convert whitespace place holders
1452 (goto-char (point-min))
1453 (let (beg end n)
1454 (while (setq beg (next-single-property-change (point) 'org-whitespace))
1455 (setq n (get-text-property beg 'org-whitespace)
1456 end (next-single-property-change beg 'org-whitespace))
1457 (goto-char beg)
1458 (delete-region beg end)
1459 (insert (format "<span style=\"visibility:hidden;\">%s</span>"
1460 (make-string n ?x)))))
1461 (or to-buffer (save-buffer))
1462 (goto-char (point-min))
1463 (or (org-export-push-to-kill-ring "HTML")
1464 (message "Exporting... done"))
1465 (if (eq to-buffer 'string)
1466 (prog1 (buffer-substring (point-min) (point-max))
1467 (kill-buffer (current-buffer)))
1468 (current-buffer)))))
1470 (defun org-export-html-insert-plist-item (plist key &rest args)
1471 (let ((item (plist-get plist key)))
1472 (cond ((functionp item)
1473 (apply item args))
1474 (item
1475 (insert item)))))
1477 (defun org-export-html-format-href (s)
1478 "Make sure the S is valid as a href reference in an XHTML document."
1479 (save-match-data
1480 (let ((start 0))
1481 (while (string-match "&" s start)
1482 (setq start (+ (match-beginning 0) 3)
1483 s (replace-match "&amp;" t t s)))))
1486 (defun org-export-html-format-desc (s)
1487 "Make sure the S is valid as a description in a link."
1488 (if (and s (not (get-text-property 1 'org-protected s)))
1489 (save-match-data
1490 (org-html-do-expand s))
1493 (defun org-export-html-format-image (src par-open)
1494 "Create image tag with source and attributes."
1495 (save-match-data
1496 (if (string-match "^ltxpng/" src)
1497 (format "<img src=\"%s\"/>" src)
1498 (let* ((caption (org-find-text-property-in-string 'org-caption src))
1499 (attr (org-find-text-property-in-string 'org-attributes src))
1500 (label (org-find-text-property-in-string 'org-label src)))
1501 (format "%s<div %sclass=\"figure\">
1502 <p><img src=\"%s\"%s /></p>%s
1503 </div>%s"
1504 (if org-par-open "</p>\n" "")
1505 (if label (format "id=\"%s\" " label) "")
1507 (if (string-match "\\<alt=" (or attr ""))
1508 (concat " " attr )
1509 (concat " " attr " alt=\"" src "\""))
1510 (if caption (concat "\n<p>" caption "</p>") "")
1511 (if org-par-open "\n<p>" ""))))))
1513 (defun org-export-html-get-bibliography ()
1514 "Find bibliography, cut it out and return it."
1515 (catch 'exit
1516 (let (beg end (cnt 1) bib)
1517 (save-excursion
1518 (goto-char (point-min))
1519 (when (re-search-forward "^[ \t]*<div \\(id\\|class\\)=\"bibliography\"" nil t)
1520 (setq beg (match-beginning 0))
1521 (while (re-search-forward "</?div\\>" nil t)
1522 (setq cnt (+ cnt (if (string= (match-string 0) "<div") +1 -1)))
1523 (when (= cnt 0)
1524 (and (looking-at ">") (forward-char 1))
1525 (setq bib (buffer-substring beg (point)))
1526 (delete-region beg (point))
1527 (throw 'exit bib))))
1528 nil))))
1530 (defvar org-table-number-regexp) ; defined in org-table.el
1531 (defun org-format-table-html (lines olines)
1532 "Find out which HTML converter to use and return the HTML code."
1533 (if (stringp lines)
1534 (setq lines (org-split-string lines "\n")))
1535 (if (string-match "^[ \t]*|" (car lines))
1536 ;; A normal org table
1537 (org-format-org-table-html lines)
1538 ;; Table made by table.el - test for spanning
1539 (let* ((hlines (delq nil (mapcar
1540 (lambda (x)
1541 (if (string-match "^[ \t]*\\+-" x) x
1542 nil))
1543 lines)))
1544 (first (car hlines))
1545 (ll (and (string-match "\\S-+" first)
1546 (match-string 0 first)))
1547 (re (concat "^[ \t]*" (regexp-quote ll)))
1548 (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
1549 hlines))))
1550 (if (and (not spanning)
1551 (not org-export-prefer-native-exporter-for-tables))
1552 ;; We can use my own converter with HTML conversions
1553 (org-format-table-table-html lines)
1554 ;; Need to use the code generator in table.el, with the original text.
1555 (org-format-table-table-html-using-table-generate-source olines)))))
1557 (defvar org-table-number-fraction) ; defined in org-table.el
1558 (defun org-format-org-table-html (lines &optional splice)
1559 "Format a table into HTML."
1560 (require 'org-table)
1561 ;; Get rid of hlines at beginning and end
1562 (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
1563 (setq lines (nreverse lines))
1564 (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
1565 (setq lines (nreverse lines))
1566 (when org-export-table-remove-special-lines
1567 ;; Check if the table has a marking column. If yes remove the
1568 ;; column and the special lines
1569 (setq lines (org-table-clean-before-export lines)))
1571 (let* ((caption (or (get-text-property 0 'org-caption (car lines))
1572 (get-text-property (or (next-single-property-change
1573 0 'org-caption (car lines))
1575 'org-caption (car lines))))
1576 (attributes (or (get-text-property 0 'org-attributes (car lines))
1577 (get-text-property (or (next-single-property-change
1578 0 'org-attributes (car lines))
1580 'org-attributes (car lines))))
1581 (html-table-tag (org-export-splice-attributes
1582 html-table-tag attributes))
1583 (head (and org-export-highlight-first-table-line
1584 (delq nil (mapcar
1585 (lambda (x) (string-match "^[ \t]*|-" x))
1586 (cdr lines)))))
1588 (nline 0) fnum i
1589 tbopen line fields html gr colgropen rowstart rowend)
1590 (if splice (setq head nil))
1591 (unless splice (push (if head "<thead>" "<tbody>") html))
1592 (setq tbopen t)
1593 (while (setq line (pop lines))
1594 (catch 'next-line
1595 (if (string-match "^[ \t]*|-" line)
1596 (progn
1597 (unless splice
1598 (push (if head "</thead>" "</tbody>") html)
1599 (if lines (push "<tbody>" html) (setq tbopen nil)))
1600 (setq head nil) ;; head ends here, first time around
1601 ;; ignore this line
1602 (throw 'next-line t)))
1603 ;; Break the line into fields
1604 (setq fields (org-split-string line "[ \t]*|[ \t]*"))
1605 (unless fnum (setq fnum (make-vector (length fields) 0)))
1606 (setq nline (1+ nline) i -1
1607 rowstart (eval (car org-export-table-row-tags))
1608 rowend (eval (cdr org-export-table-row-tags)))
1609 (push (concat rowstart
1610 (mapconcat
1611 (lambda (x)
1612 (setq i (1+ i))
1613 (if (and (< i nline)
1614 (string-match org-table-number-regexp x))
1615 (incf (aref fnum i)))
1616 (cond
1617 (head
1618 (concat
1619 (format (car org-export-table-header-tags) "col")
1621 (cdr org-export-table-header-tags)))
1622 ((and (= i 0) org-export-html-table-use-header-tags-for-first-column)
1623 (concat
1624 (format (car org-export-table-header-tags) "row")
1626 (cdr org-export-table-header-tags)))
1628 (concat (car org-export-table-data-tags) x
1629 (cdr org-export-table-data-tags)))))
1630 fields "")
1631 rowend)
1632 html)))
1633 (unless splice (if tbopen (push "</tbody>" html)))
1634 (unless splice (push "</table>\n" html))
1635 (setq html (nreverse html))
1636 (unless splice
1637 ;; Put in col tags with the alignment (unfortuntely often ignored...)
1638 (unless (car org-table-colgroup-info)
1639 (setq org-table-colgroup-info
1640 (cons :start (cdr org-table-colgroup-info))))
1641 (push (mapconcat
1642 (lambda (x)
1643 (setq gr (pop org-table-colgroup-info))
1644 (format "%s<col align=\"%s\"></col>%s"
1645 (if (memq gr '(:start :startend))
1646 (prog1
1647 (if colgropen "</colgroup>\n<colgroup>" "<colgroup>")
1648 (setq colgropen t))
1650 (if (> (/ (float x) nline) org-table-number-fraction)
1651 "right" "left")
1652 (if (memq gr '(:end :startend))
1653 (progn (setq colgropen nil) "</colgroup>")
1654 "")))
1655 fnum "")
1656 html)
1657 (if colgropen (setq html (cons (car html) (cons "</colgroup>" (cdr html)))))
1658 ;; Since the output of HTML table formatter can also be used in
1659 ;; DocBook document, we want to always include the caption to make
1660 ;; DocBook XML file valid.
1661 (push (format "<caption>%s</caption>" (or caption "")) html)
1662 (push html-table-tag html))
1663 (concat (mapconcat 'identity html "\n") "\n")))
1665 (defun org-export-splice-attributes (tag attributes)
1666 "Read attributes in string ATTRIBUTES, add and replace in HTML tag TAG."
1667 (if (not attributes)
1669 (let (oldatt newatt)
1670 (setq oldatt (org-extract-attributes-from-string tag)
1671 tag (pop oldatt)
1672 newatt (cdr (org-extract-attributes-from-string attributes)))
1673 (while newatt
1674 (setq oldatt (plist-put oldatt (pop newatt) (pop newatt))))
1675 (if (string-match ">" tag)
1676 (setq tag
1677 (replace-match (concat (org-attributes-to-string oldatt) ">")
1678 t t tag)))
1679 tag)))
1681 (defun org-format-table-table-html (lines)
1682 "Format a table generated by table.el into HTML.
1683 This conversion does *not* use `table-generate-source' from table.el.
1684 This has the advantage that Org-mode's HTML conversions can be used.
1685 But it has the disadvantage, that no cell- or row-spanning is allowed."
1686 (let (line field-buffer
1687 (head org-export-highlight-first-table-line)
1688 fields html empty i)
1689 (setq html (concat html-table-tag "\n"))
1690 (while (setq line (pop lines))
1691 (setq empty "&nbsp;")
1692 (catch 'next-line
1693 (if (string-match "^[ \t]*\\+-" line)
1694 (progn
1695 (if field-buffer
1696 (progn
1697 (setq
1698 html
1699 (concat
1700 html
1701 "<tr>"
1702 (mapconcat
1703 (lambda (x)
1704 (if (equal x "") (setq x empty))
1705 (if head
1706 (concat
1707 (format (car org-export-table-header-tags) "col")
1709 (cdr org-export-table-header-tags))
1710 (concat (car org-export-table-data-tags) x
1711 (cdr org-export-table-data-tags))))
1712 field-buffer "\n")
1713 "</tr>\n"))
1714 (setq head nil)
1715 (setq field-buffer nil)))
1716 ;; Ignore this line
1717 (throw 'next-line t)))
1718 ;; Break the line into fields and store the fields
1719 (setq fields (org-split-string line "[ \t]*|[ \t]*"))
1720 (if field-buffer
1721 (setq field-buffer (mapcar
1722 (lambda (x)
1723 (concat x "<br/>" (pop fields)))
1724 field-buffer))
1725 (setq field-buffer fields))))
1726 (setq html (concat html "</table>\n"))
1727 html))
1729 (defun org-format-table-table-html-using-table-generate-source (lines)
1730 "Format a table into html, using `table-generate-source' from table.el.
1731 This has the advantage that cell- or row-spanning is allowed.
1732 But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
1733 (require 'table)
1734 (with-current-buffer (get-buffer-create " org-tmp1 ")
1735 (erase-buffer)
1736 (insert (mapconcat 'identity lines "\n"))
1737 (goto-char (point-min))
1738 (if (not (re-search-forward "|[^+]" nil t))
1739 (error "Error processing table"))
1740 (table-recognize-table)
1741 (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
1742 (table-generate-source 'html " org-tmp2 ")
1743 (set-buffer " org-tmp2 ")
1744 (buffer-substring (point-min) (point-max))))
1746 (defun org-export-splice-style (style extra)
1747 "Splice EXTRA into STYLE, just before \"</style>\"."
1748 (if (and (stringp extra)
1749 (string-match "\\S-" extra)
1750 (string-match "</style>" style))
1751 (concat (substring style 0 (match-beginning 0))
1752 "\n" extra "\n"
1753 (substring style (match-beginning 0)))
1754 style))
1756 (defun org-html-handle-time-stamps (s)
1757 "Format time stamps in string S, or remove them."
1758 (catch 'exit
1759 (let (r b)
1760 (while (string-match org-maybe-keyword-time-regexp s)
1761 (or b (setq b (substring s 0 (match-beginning 0))))
1762 (setq r (concat
1763 r (substring s 0 (match-beginning 0))
1764 " @<span class=\"timestamp-wrapper\">"
1765 (if (match-end 1)
1766 (format "@<span class=\"timestamp-kwd\">%s @</span>"
1767 (match-string 1 s)))
1768 (format " @<span class=\"timestamp\">%s@</span>"
1769 (substring
1770 (org-translate-time (match-string 3 s)) 1 -1))
1771 "@</span>")
1772 s (substring s (match-end 0))))
1773 ;; Line break if line started and ended with time stamp stuff
1774 (if (not r)
1776 (setq r (concat r s))
1777 (unless (string-match "\\S-" (concat b s))
1778 (setq r (concat r "@<br/>")))
1779 r))))
1781 (defvar htmlize-buffer-places) ; from htmlize.el
1782 (defun org-export-htmlize-region-for-paste (beg end)
1783 "Convert the region to HTML, using htmlize.el.
1784 This is much like `htmlize-region-for-paste', only that it uses
1785 the settings define in the org-... variables."
1786 (let* ((htmlize-output-type org-export-htmlize-output-type)
1787 (htmlize-css-name-prefix org-export-htmlize-css-font-prefix)
1788 (htmlbuf (htmlize-region beg end)))
1789 (unwind-protect
1790 (with-current-buffer htmlbuf
1791 (buffer-substring (plist-get htmlize-buffer-places 'content-start)
1792 (plist-get htmlize-buffer-places 'content-end)))
1793 (kill-buffer htmlbuf))))
1795 ;;;###autoload
1796 (defun org-export-htmlize-generate-css ()
1797 "Create the CSS for all font definitions in the current Emacs session.
1798 Use this to create face definitions in your CSS style file that can then
1799 be used by code snippets transformed by htmlize.
1800 This command just produces a buffer that contains class definitions for all
1801 faces used in the current Emacs session. You can copy and paste the ones you
1802 need into your CSS file.
1804 If you then set `org-export-htmlize-output-type' to `css', calls to
1805 the function `org-export-htmlize-region-for-paste' will produce code
1806 that uses these same face definitions."
1807 (interactive)
1808 (require 'htmlize)
1809 (and (get-buffer "*html*") (kill-buffer "*html*"))
1810 (with-temp-buffer
1811 (let ((fl (face-list))
1812 (htmlize-css-name-prefix "org-")
1813 (htmlize-output-type 'css)
1814 f i)
1815 (while (setq f (pop fl)
1816 i (and f (face-attribute f :inherit)))
1817 (when (and (symbolp f) (or (not i) (not (listp i))))
1818 (insert (org-add-props (copy-sequence "1") nil 'face f))))
1819 (htmlize-region (point-min) (point-max))))
1820 (switch-to-buffer "*html*")
1821 (goto-char (point-min))
1822 (if (re-search-forward "<style" nil t)
1823 (delete-region (point-min) (match-beginning 0)))
1824 (if (re-search-forward "</style>" nil t)
1825 (delete-region (1+ (match-end 0)) (point-max)))
1826 (beginning-of-line 1)
1827 (if (looking-at " +") (replace-match ""))
1828 (goto-char (point-min)))
1830 (defun org-html-protect (s)
1831 ;; convert & to &amp;, < to &lt; and > to &gt;
1832 (let ((start 0))
1833 (while (string-match "&" s start)
1834 (setq s (replace-match "&amp;" t t s)
1835 start (1+ (match-beginning 0))))
1836 (while (string-match "<" s)
1837 (setq s (replace-match "&lt;" t t s)))
1838 (while (string-match ">" s)
1839 (setq s (replace-match "&gt;" t t s)))
1840 ; (while (string-match "\"" s)
1841 ; (setq s (replace-match "&quot;" t t s)))
1845 (defun org-html-expand (string)
1846 "Prepare STRING for HTML export. Applies all active conversions.
1847 If there are links in the string, don't modify these."
1848 (let* ((re (concat org-bracket-link-regexp "\\|"
1849 (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$")))
1850 m s l res)
1851 (while (setq m (string-match re string))
1852 (setq s (substring string 0 m)
1853 l (match-string 0 string)
1854 string (substring string (match-end 0)))
1855 (push (org-html-do-expand s) res)
1856 (push l res))
1857 (push (org-html-do-expand string) res)
1858 (apply 'concat (nreverse res))))
1860 (defun org-html-do-expand (s)
1861 "Apply all active conversions to translate special ASCII to HTML."
1862 (setq s (org-html-protect s))
1863 (if org-export-html-expand
1864 (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
1865 (setq s (replace-match "<\\1>" t nil s))))
1866 (if org-export-with-emphasize
1867 (setq s (org-export-html-convert-emphasize s)))
1868 (if org-export-with-special-strings
1869 (setq s (org-export-html-convert-special-strings s)))
1870 (if org-export-with-sub-superscripts
1871 (setq s (org-export-html-convert-sub-super s)))
1872 (if org-export-with-TeX-macros
1873 (let ((start 0) wd ass)
1874 (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)\\({}\\)?"
1875 s start))
1876 (if (get-text-property (match-beginning 0) 'org-protected s)
1877 (setq start (match-end 0))
1878 (setq wd (match-string 1 s))
1879 (if (setq ass (assoc wd org-html-entities))
1880 (setq s (replace-match (or (cdr ass)
1881 (concat "&" (car ass) ";"))
1882 t t s))
1883 (setq start (+ start (length wd))))))))
1886 (defconst org-export-html-special-string-regexps
1887 '(("\\\\-" . "&shy;")
1888 ("---\\([^-]\\)" . "&mdash;\\1")
1889 ("--\\([^-]\\)" . "&ndash;\\1")
1890 ("\\.\\.\\." . "&hellip;"))
1891 "Regular expressions for special string conversion.")
1893 (defun org-export-html-convert-special-strings (string)
1894 "Convert special characters in STRING to HTML."
1895 (let ((all org-export-html-special-string-regexps)
1896 e a re rpl start)
1897 (while (setq a (pop all))
1898 (setq re (car a) rpl (cdr a) start 0)
1899 (while (string-match re string start)
1900 (if (get-text-property (match-beginning 0) 'org-protected string)
1901 (setq start (match-end 0))
1902 (setq string (replace-match rpl t nil string)))))
1903 string))
1905 (defun org-export-html-convert-sub-super (string)
1906 "Convert sub- and superscripts in STRING to HTML."
1907 (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
1908 (while (string-match org-match-substring-regexp string s)
1909 (cond
1910 ((and requireb (match-end 8)) (setq s (match-end 2)))
1911 ((get-text-property (match-beginning 2) 'org-protected string)
1912 (setq s (match-end 2)))
1914 (setq s (match-end 1)
1915 key (if (string= (match-string 2 string) "_") "sub" "sup")
1916 c (or (match-string 8 string)
1917 (match-string 6 string)
1918 (match-string 5 string))
1919 string (replace-match
1920 (concat (match-string 1 string)
1921 "<" key ">" c "</" key ">")
1922 t t string)))))
1923 (while (string-match "\\\\\\([_^]\\)" string)
1924 (setq string (replace-match (match-string 1 string) t t string)))
1925 string))
1927 (defun org-export-html-convert-emphasize (string)
1928 "Apply emphasis."
1929 (let ((s 0) rpl)
1930 (while (string-match org-emph-re string s)
1931 (if (not (equal
1932 (substring string (match-beginning 3) (1+ (match-beginning 3)))
1933 (substring string (match-beginning 4) (1+ (match-beginning 4)))))
1934 (setq s (match-beginning 0)
1936 (concat
1937 (match-string 1 string)
1938 (nth 2 (assoc (match-string 3 string) org-emphasis-alist))
1939 (match-string 4 string)
1940 (nth 3 (assoc (match-string 3 string)
1941 org-emphasis-alist))
1942 (match-string 5 string))
1943 string (replace-match rpl t t string)
1944 s (+ s (- (length rpl) 2)))
1945 (setq s (1+ s))))
1946 string))
1948 (defun org-open-par ()
1949 "Insert <p>, but first close previous paragraph if any."
1950 (org-close-par-maybe)
1951 (insert "\n<p>")
1952 (setq org-par-open t))
1953 (defun org-close-par-maybe ()
1954 "Close paragraph if there is one open."
1955 (when org-par-open
1956 (insert "</p>")
1957 (setq org-par-open nil)))
1958 (defun org-close-li (&optional type)
1959 "Close <li> if necessary."
1960 (org-close-par-maybe)
1961 (insert (if (equal type "d") "</dd>\n" "</li>\n")))
1963 (defvar in-local-list)
1964 (defvar local-list-indent)
1965 (defvar local-list-type)
1966 (defun org-export-html-close-lists-maybe (line)
1967 (let ((ind (or (get-text-property 0 'original-indentation line)))
1968 ; (and (string-match "\\S-" line)
1969 ; (org-get-indentation line))))
1970 didclose)
1971 (when ind
1972 (while (and in-local-list
1973 (<= ind (car local-list-indent)))
1974 (setq didclose t)
1975 (org-close-li (car local-list-type))
1976 (insert (format "</%sl>\n" (car local-list-type)))
1977 (pop local-list-type) (pop local-list-indent)
1978 (setq in-local-list local-list-indent))
1979 (and didclose (org-open-par)))))
1981 (defvar body-only) ; dynamically scoped into this.
1982 (defun org-html-level-start (level title umax with-toc head-count)
1983 "Insert a new level in HTML export.
1984 When TITLE is nil, just close all open levels."
1985 (org-close-par-maybe)
1986 (let* ((target (and title (org-get-text-property-any 0 'target title)))
1987 (extra-targets (assoc target org-export-target-aliases))
1988 (preferred (cdr (assoc target org-export-preferred-target-alist)))
1989 (remove (or preferred target))
1990 (l org-level-max)
1991 snumber href suffix)
1992 (setq extra-targets (remove remove extra-targets))
1993 (setq extra-targets
1994 (mapconcat (lambda (x)
1995 (if (org-uuidgen-p x) (setq x (concat "ID-" x)))
1996 (format "<a name=\"%s\" id=\"%s\"></a>"
1997 x x))
1998 extra-targets
1999 ""))
2000 (while (>= l level)
2001 (if (aref org-levels-open (1- l))
2002 (progn
2003 (org-html-level-close l umax)
2004 (aset org-levels-open (1- l) nil)))
2005 (setq l (1- l)))
2006 (when title
2007 ;; If title is nil, this means this function is called to close
2008 ;; all levels, so the rest is done only if title is given
2009 (when (string-match (org-re "\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
2010 (setq title (replace-match
2011 (if org-export-with-tags
2012 (save-match-data
2013 (concat
2014 "&nbsp;&nbsp;&nbsp;<span class=\"tag\">"
2015 (mapconcat
2016 (lambda (x)
2017 (format "<span class=\"%s\">%s</span>"
2018 (org-export-html-get-tag-class-name x)
2020 (org-split-string (match-string 1 title) ":")
2021 "&nbsp;")
2022 "</span>"))
2024 t t title)))
2025 (if (> level umax)
2026 (progn
2027 (if (aref org-levels-open (1- level))
2028 (progn
2029 (org-close-li)
2030 (if target
2031 (insert (format "<li id=\"%s\">" target) extra-targets title "<br/>\n")
2032 (insert "<li>" title "<br/>\n")))
2033 (aset org-levels-open (1- level) t)
2034 (org-close-par-maybe)
2035 (if target
2036 (insert (format "<ul>\n<li id=\"%s\">" target)
2037 extra-targets title "<br/>\n")
2038 (insert "<ul>\n<li>" title "<br/>\n"))))
2039 (aset org-levels-open (1- level) t)
2040 (setq snumber (org-section-number level))
2041 (setq level (+ level org-export-html-toplevel-hlevel -1))
2042 (if (and org-export-with-section-numbers (not body-only))
2043 (setq title (concat
2044 (format "<span class=\"section-number-%d\">%s</span>"
2045 level snumber)
2046 " " title)))
2047 (unless (= head-count 1) (insert "\n</div>\n"))
2048 (setq href (cdr (assoc (concat "sec-" snumber) org-export-preferred-target-alist)))
2049 (setq suffix (or href snumber))
2050 (setq href (or href (concat "sec-" snumber)))
2051 (insert (format "\n<div id=\"outline-container-%s\" class=\"outline-%d\">\n<h%d id=\"%s\">%s%s</h%d>\n<div class=\"outline-text-%d\" id=\"text-%s\">\n"
2052 suffix level level href
2053 extra-targets
2054 title level level suffix))
2055 (org-open-par)))))
2057 (defun org-export-html-get-tag-class-name (tag)
2058 "Turn tag into a valid class name.
2059 Replaces invalid characters with \"_\" and then prepends a prefix."
2060 (save-match-data
2061 (while (string-match "[^a-zA-Z0-9_]" tag)
2062 (setq tag (replace-match "_" t t tag))))
2063 (concat org-export-html-tag-class-prefix tag))
2065 (defun org-export-html-get-todo-kwd-class-name (kwd)
2066 "Turn todo keyword into a valid class name.
2067 Replaces invalid characters with \"_\" and then prepends a prefix."
2068 (save-match-data
2069 (while (string-match "[^a-zA-Z0-9_]" kwd)
2070 (setq kwd (replace-match "_" t t kwd))))
2071 (concat org-export-html-todo-kwd-class-prefix kwd))
2073 (defun org-html-level-close (level max-outline-level)
2074 "Terminate one level in HTML export."
2075 (if (<= level max-outline-level)
2076 (insert "</div>\n")
2077 (org-close-li)
2078 (insert "</ul>\n")))
2080 (provide 'org-html)
2082 ;; arch-tag: 8109d84d-eb8f-460b-b1a8-f45f3a6c7ea1
2083 ;;; org-html.el ends here