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