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