Export: Kill product buffer if it has been displayed directly
[org-mode.git] / lisp / org-html.el
blob343d44233361c0919453ee083450c85a593fc972
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 .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-include-default '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 id=\"org-div-home-and-up\" 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 (defvar org-export-html-final-hook nil
423 "Hook run at the end of HTML export, in the new buffer.")
425 ;;; HTML export
427 (defun org-export-html-preprocess (parameters)
428 ;; Convert LaTeX fragments to images
429 (when (and org-current-export-file
430 (plist-get parameters :LaTeX-fragments))
431 (org-format-latex
432 (concat "ltxpng/" (file-name-sans-extension
433 (file-name-nondirectory
434 org-current-export-file)))
435 org-current-export-dir nil "Creating LaTeX image %s"))
436 (message "Exporting..."))
438 ;;;###autoload
439 (defun org-export-as-html-and-open (arg)
440 "Export the outline as HTML and immediately open it with a browser.
441 If there is an active region, export only the region.
442 The prefix ARG specifies how many levels of the outline should become
443 headlines. The default is 3. Lower levels will become bulleted lists."
444 (interactive "P")
445 (org-export-as-html arg 'hidden)
446 (org-open-file buffer-file-name)
447 (when org-export-kill-product-buffer-when-displayed
448 (kill-buffer)))
450 ;;;###autoload
451 (defun org-export-as-html-batch ()
452 "Call `org-export-as-html', may be used in batch processing as
453 emacs --batch
454 --load=$HOME/lib/emacs/org.el
455 --eval \"(setq org-export-headline-levels 2)\"
456 --visit=MyFile --funcall org-export-as-html-batch"
457 (org-export-as-html org-export-headline-levels 'hidden))
459 ;;;###autoload
460 (defun org-export-as-html-to-buffer (arg)
461 "Call `org-export-as-html` with output to a temporary buffer.
462 No file is created. The prefix ARG is passed through to `org-export-as-html'."
463 (interactive "P")
464 (org-export-as-html arg nil nil "*Org HTML Export*")
465 (when org-export-show-temporary-export-buffer
466 (switch-to-buffer-other-window "*Org HTML Export*")))
468 ;;;###autoload
469 (defun org-replace-region-by-html (beg end)
470 "Assume the current region has org-mode syntax, and convert it to HTML.
471 This can be used in any buffer. For example, you could write an
472 itemized list in org-mode syntax in an HTML buffer and then use this
473 command to convert it."
474 (interactive "r")
475 (let (reg html buf pop-up-frames)
476 (save-window-excursion
477 (if (org-mode-p)
478 (setq html (org-export-region-as-html
479 beg end t 'string))
480 (setq reg (buffer-substring beg end)
481 buf (get-buffer-create "*Org tmp*"))
482 (with-current-buffer buf
483 (erase-buffer)
484 (insert reg)
485 (org-mode)
486 (setq html (org-export-region-as-html
487 (point-min) (point-max) t 'string)))
488 (kill-buffer buf)))
489 (delete-region beg end)
490 (insert html)))
492 ;;;###autoload
493 (defun org-export-region-as-html (beg end &optional body-only buffer)
494 "Convert region from BEG to END in org-mode buffer to HTML.
495 If prefix arg BODY-ONLY is set, omit file header, footer, and table of
496 contents, and only produce the region of converted text, useful for
497 cut-and-paste operations.
498 If BUFFER is a buffer or a string, use/create that buffer as a target
499 of the converted HTML. If BUFFER is the symbol `string', return the
500 produced HTML as a string and leave not buffer behind. For example,
501 a Lisp program could call this function in the following way:
503 (setq html (org-export-region-as-html beg end t 'string))
505 When called interactively, the output buffer is selected, and shown
506 in a window. A non-interactive call will only return the buffer."
507 (interactive "r\nP")
508 (when (interactive-p)
509 (setq buffer "*Org HTML Export*"))
510 (let ((transient-mark-mode t) (zmacs-regions t)
511 ext-plist rtn)
512 (setq ext-plist (plist-put ext-plist :ignore-subtree-p t))
513 (goto-char end)
514 (set-mark (point)) ;; to activate the region
515 (goto-char beg)
516 (setq rtn (org-export-as-html
517 nil nil ext-plist
518 buffer body-only))
519 (if (fboundp 'deactivate-mark) (deactivate-mark))
520 (if (and (interactive-p) (bufferp rtn))
521 (switch-to-buffer-other-window rtn)
522 rtn)))
524 (defvar html-table-tag nil) ; dynamically scoped into this.
525 (defvar org-par-open nil)
526 ;;;###autoload
527 (defun org-export-as-html (arg &optional hidden ext-plist
528 to-buffer body-only pub-dir)
529 "Export the outline as a pretty HTML file.
530 If there is an active region, export only the region. The prefix
531 ARG specifies how many levels of the outline should become
532 headlines. The default is 3. Lower levels will become bulleted
533 lists. HIDDEN is obsolete and does nothing.
534 EXT-PLIST is a property list with external parameters overriding
535 org-mode's default settings, but still inferior to file-local
536 settings. When TO-BUFFER is non-nil, create a buffer with that
537 name and export to that buffer. If TO-BUFFER is the symbol
538 `string', don't leave any buffer behind but just return the
539 resulting HTML as a string. When BODY-ONLY is set, don't produce
540 the file header and footer, simply return the content of
541 <body>...</body>, without even the body tags themselves. When
542 PUB-DIR is set, use this as the publishing directory."
543 (interactive "P")
544 (run-hooks 'org-export-first-hook)
546 ;; Make sure we have a file name when we need it.
547 (when (and (not (or to-buffer body-only))
548 (not buffer-file-name))
549 (if (buffer-base-buffer)
550 (org-set-local 'buffer-file-name
551 (with-current-buffer (buffer-base-buffer)
552 buffer-file-name))
553 (error "Need a file name to be able to export")))
555 (message "Exporting...")
556 (setq-default org-todo-line-regexp org-todo-line-regexp)
557 (setq-default org-deadline-line-regexp org-deadline-line-regexp)
558 (setq-default org-done-keywords org-done-keywords)
559 (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp)
560 (let* ((opt-plist
561 (org-export-process-option-filters
562 (org-combine-plists (org-default-export-plist)
563 ext-plist
564 (org-infile-export-plist))))
565 (body-only (or body-only (plist-get opt-plist :body-only)))
566 (style (concat (if (plist-get opt-plist :style-include-default)
567 org-export-html-style-default)
568 (plist-get opt-plist :style)
569 (plist-get opt-plist :style-extra)
570 "\n"
571 (if (plist-get opt-plist :style-include-scripts)
572 org-export-html-scripts)))
573 (html-extension (plist-get opt-plist :html-extension))
574 (link-validate (plist-get opt-plist :link-validation-function))
575 valid thetoc have-headings first-heading-pos
576 (odd org-odd-levels-only)
577 (region-p (org-region-active-p))
578 (rbeg (and region-p (region-beginning)))
579 (rend (and region-p (region-end)))
580 (subtree-p
581 (if (plist-get opt-plist :ignore-subtree-p)
583 (when region-p
584 (save-excursion
585 (goto-char rbeg)
586 (and (org-at-heading-p)
587 (>= (org-end-of-subtree t t) rend))))))
588 (level-offset (if subtree-p
589 (save-excursion
590 (goto-char rbeg)
591 (+ (funcall outline-level)
592 (if org-odd-levels-only 1 0)))
594 (opt-plist (setq org-export-opt-plist
595 (if subtree-p
596 (org-export-add-subtree-options opt-plist rbeg)
597 opt-plist)))
598 ;; The following two are dynamically scoped into other
599 ;; routines below.
600 (org-current-export-dir
601 (or pub-dir (org-export-directory :html opt-plist)))
602 (org-current-export-file buffer-file-name)
603 (level 0) (line "") (origline "") txt todo
604 (umax nil)
605 (umax-toc nil)
606 (filename (if to-buffer nil
607 (expand-file-name
608 (concat
609 (file-name-sans-extension
610 (or (and subtree-p
611 (org-entry-get (region-beginning)
612 "EXPORT_FILE_NAME" t))
613 (file-name-nondirectory buffer-file-name)))
614 "." html-extension)
615 (file-name-as-directory
616 (or pub-dir (org-export-directory :html opt-plist))))))
617 (current-dir (if buffer-file-name
618 (file-name-directory buffer-file-name)
619 default-directory))
620 (buffer (if to-buffer
621 (cond
622 ((eq to-buffer 'string) (get-buffer-create "*Org HTML Export*"))
623 (t (get-buffer-create to-buffer)))
624 (find-file-noselect filename)))
625 (org-levels-open (make-vector org-level-max nil))
626 (date (plist-get opt-plist :date))
627 (author (plist-get opt-plist :author))
628 (title (or (and subtree-p (org-export-get-title-from-subtree))
629 (plist-get opt-plist :title)
630 (and (not body-only)
631 (not
632 (plist-get opt-plist :skip-before-1st-heading))
633 (org-export-grab-title-from-buffer))
634 (and buffer-file-name
635 (file-name-sans-extension
636 (file-name-nondirectory buffer-file-name)))
637 "UNTITLED"))
638 (link-up (and (plist-get opt-plist :link-up)
639 (string-match "\\S-" (plist-get opt-plist :link-up))
640 (plist-get opt-plist :link-up)))
641 (link-home (and (plist-get opt-plist :link-home)
642 (string-match "\\S-" (plist-get opt-plist :link-home))
643 (plist-get opt-plist :link-home)))
644 (dummy (setq opt-plist (plist-put opt-plist :title title)))
645 (html-table-tag (plist-get opt-plist :html-table-tag))
646 (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
647 (quote-re (concat "^\\(\\*+\\)\\([ \t]+" org-quote-string "\\>\\)"))
648 (inquote nil)
649 (infixed nil)
650 (inverse nil)
651 (in-local-list nil)
652 (local-list-type nil)
653 (local-list-indent nil)
654 (llt org-plain-list-ordered-item-terminator)
655 (email (plist-get opt-plist :email))
656 (language (plist-get opt-plist :language))
657 (keywords (plist-get opt-plist :keywords))
658 (description (plist-get opt-plist :description))
659 (lang-words nil)
660 (head-count 0) cnt
661 (start 0)
662 (coding-system (and (boundp 'buffer-file-coding-system)
663 buffer-file-coding-system))
664 (coding-system-for-write (or org-export-html-coding-system
665 coding-system))
666 (save-buffer-coding-system (or org-export-html-coding-system
667 coding-system))
668 (charset (and coding-system-for-write
669 (fboundp 'coding-system-get)
670 (coding-system-get coding-system-for-write
671 'mime-charset)))
672 (region
673 (buffer-substring
674 (if region-p (region-beginning) (point-min))
675 (if region-p (region-end) (point-max))))
676 (lines
677 (org-split-string
678 (org-export-preprocess-string
679 region
680 :emph-multiline t
681 :for-html t
682 :skip-before-1st-heading
683 (plist-get opt-plist :skip-before-1st-heading)
684 :drawers (plist-get opt-plist :drawers)
685 :todo-keywords (plist-get opt-plist :todo-keywords)
686 :tags (plist-get opt-plist :tags)
687 :priority (plist-get opt-plist :priority)
688 :footnotes (plist-get opt-plist :footnotes)
689 :timestamps (plist-get opt-plist :timestamps)
690 :archived-trees
691 (plist-get opt-plist :archived-trees)
692 :select-tags (plist-get opt-plist :select-tags)
693 :exclude-tags (plist-get opt-plist :exclude-tags)
694 :add-text
695 (plist-get opt-plist :text)
696 :LaTeX-fragments
697 (plist-get opt-plist :LaTeX-fragments))
698 "[\r\n]"))
699 table-open type
700 table-buffer table-orig-buffer
701 ind item-type starter didclose
702 rpl path attr desc descp desc1 desc2 link
703 snumber fnc item-tag
704 footnotes footref-seen
705 id-file href
708 (let ((inhibit-read-only t))
709 (org-unmodified
710 (remove-text-properties (point-min) (point-max)
711 '(:org-license-to-kill t))))
713 (message "Exporting...")
715 (setq org-min-level (org-get-min-level lines level-offset))
716 (setq org-last-level org-min-level)
717 (org-init-section-numbers)
719 (cond
720 ((and date (string-match "%" date))
721 (setq date (format-time-string date)))
722 (date)
723 (t (setq date (format-time-string "%Y-%m-%d %T %Z"))))
725 ;; Get the language-dependent settings
726 (setq lang-words (or (assoc language org-export-language-setup)
727 (assoc "en" org-export-language-setup)))
729 ;; Switch to the output buffer
730 (set-buffer buffer)
731 (let ((inhibit-read-only t)) (erase-buffer))
732 (fundamental-mode)
733 (org-install-letbind)
735 (and (fboundp 'set-buffer-file-coding-system)
736 (set-buffer-file-coding-system coding-system-for-write))
738 (let ((case-fold-search nil)
739 (org-odd-levels-only odd))
740 ;; create local variables for all options, to make sure all called
741 ;; functions get the correct information
742 (mapc (lambda (x)
743 (set (make-local-variable (nth 2 x))
744 (plist-get opt-plist (car x))))
745 org-export-plist-vars)
746 (setq umax (if arg (prefix-numeric-value arg)
747 org-export-headline-levels))
748 (setq umax-toc (if (integerp org-export-with-toc)
749 (min org-export-with-toc umax)
750 umax))
751 (unless body-only
752 ;; File header
753 (insert (format
755 <!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
756 \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
757 <html xmlns=\"http://www.w3.org/1999/xhtml\"
758 lang=\"%s\" xml:lang=\"%s\">
759 <head>
760 <title>%s</title>
761 <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>
762 <meta name=\"generator\" content=\"Org-mode\"/>
763 <meta name=\"generated\" content=\"%s\"/>
764 <meta name=\"author\" content=\"%s\"/>
765 <meta name=\"description\" content=\"%s\"/>
766 <meta name=\"keywords\" content=\"%s\"/>
768 </head>
769 <body>
770 <div id=\"content\">
773 (format
774 (or (and (stringp org-export-html-xml-declaration)
775 org-export-html-xml-declaration)
776 (cdr (assoc html-extension org-export-html-xml-declaration))
777 (cdr (assoc "html" org-export-html-xml-declaration))
780 (or charset "iso-8859-1"))
781 language language
782 (org-html-expand title)
783 (or charset "iso-8859-1")
784 date author description keywords
785 style
786 (if (or link-up link-home)
787 (concat
788 (format org-export-html-home/up-format
789 (or link-up link-home)
790 (or link-home link-up))
791 "\n")
792 "")))
794 (org-export-html-insert-plist-item opt-plist :preamble opt-plist)
796 (when (plist-get opt-plist :auto-preamble)
797 (if title (insert (format org-export-html-title-format
798 (org-html-expand title))))))
800 (if (and org-export-with-toc (not body-only))
801 (progn
802 (push (format "<h%d>%s</h%d>\n"
803 org-export-html-toplevel-hlevel
804 (nth 3 lang-words)
805 org-export-html-toplevel-hlevel)
806 thetoc)
807 (push "<div id=\"text-table-of-contents\">\n" thetoc)
808 (push "<ul>\n<li>" thetoc)
809 (setq lines
810 (mapcar '(lambda (line)
811 (if (string-match org-todo-line-regexp line)
812 ;; This is a headline
813 (progn
814 (setq have-headings t)
815 (setq level (- (match-end 1) (match-beginning 1)
816 level-offset)
817 level (org-tr-level level)
818 txt (save-match-data
819 (org-html-expand
820 (org-export-cleanup-toc-line
821 (match-string 3 line))))
822 todo
823 (or (and org-export-mark-todo-in-toc
824 (match-beginning 2)
825 (not (member (match-string 2 line)
826 org-done-keywords)))
827 ; TODO, not DONE
828 (and org-export-mark-todo-in-toc
829 (= level umax-toc)
830 (org-search-todo-below
831 line lines level))))
832 (if (string-match
833 (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") txt)
834 (setq txt (replace-match "&nbsp;&nbsp;&nbsp;<span class=\"tag\"> \\1</span>" t nil txt)))
835 (if (string-match quote-re0 txt)
836 (setq txt (replace-match "" t t txt)))
837 (setq snumber (org-section-number level))
838 (if org-export-with-section-numbers
839 (setq txt (concat snumber " " txt)))
840 (if (<= level (max umax umax-toc))
841 (setq head-count (+ head-count 1)))
842 (if (<= level umax-toc)
843 (progn
844 (if (> level org-last-level)
845 (progn
846 (setq cnt (- level org-last-level))
847 (while (>= (setq cnt (1- cnt)) 0)
848 (push "\n<ul>\n<li>" thetoc))
849 (push "\n" thetoc)))
850 (if (< level org-last-level)
851 (progn
852 (setq cnt (- org-last-level level))
853 (while (>= (setq cnt (1- cnt)) 0)
854 (push "</li>\n</ul>" thetoc))
855 (push "\n" thetoc)))
856 ;; Check for targets
857 (while (string-match org-any-target-regexp line)
858 (setq line (replace-match
859 (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
860 t t line)))
861 (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
862 (setq txt (replace-match "" t t txt)))
863 (setq href (format "sec-%s" snumber))
864 (setq href (or (cdr (assoc href org-export-preferred-target-alist)) href))
865 (push
866 (format
867 (if todo
868 "</li>\n<li><a href=\"#%s\"><span class=\"todo\">%s</span></a>"
869 "</li>\n<li><a href=\"#%s\">%s</a>")
870 href txt) thetoc)
872 (setq org-last-level level))
874 line)
875 lines))
876 (while (> org-last-level (1- org-min-level))
877 (setq org-last-level (1- org-last-level))
878 (push "</li>\n</ul>\n" thetoc))
879 (push "</div>\n" thetoc)
880 (setq thetoc (if have-headings (nreverse thetoc) nil))))
882 (setq head-count 0)
883 (org-init-section-numbers)
885 (org-open-par)
887 (while (setq line (pop lines) origline line)
888 (catch 'nextline
890 ;; end of quote section?
891 (when (and inquote (string-match "^\\*+ " line))
892 (insert "</pre>\n")
893 (org-open-par)
894 (setq inquote nil))
895 ;; inside a quote section?
896 (when inquote
897 (insert (org-html-protect line) "\n")
898 (throw 'nextline nil))
900 ;; Fixed-width, verbatim lines (examples)
901 (when (and org-export-with-fixed-width
902 (string-match "^[ \t]*:\\(\\([ \t]\\|$\\)\\(.*\\)\\)" line))
903 (when (not infixed)
904 (setq infixed t)
905 (org-close-par-maybe)
907 (insert "<pre class=\"example\">\n"))
908 (insert (org-html-protect (match-string 3 line)) "\n")
909 (when (or (not lines)
910 (not (string-match "^[ \t]*:\\(\\([ \t]\\|$\\)\\(.*\\)\\)"
911 (car lines))))
912 (setq infixed nil)
913 (insert "</pre>\n")
914 (org-open-par))
915 (throw 'nextline nil))
917 (org-export-html-close-lists-maybe line)
919 ;; Protected HTML
920 (when (get-text-property 0 'org-protected line)
921 (let (par (ind (get-text-property 0 'original-indentation line)))
922 (when (re-search-backward
923 "\\(<p>\\)\\([ \t\r\n]*\\)\\=" (- (point) 100) t)
924 (setq par (match-string 1))
925 (replace-match "\\2\n"))
926 (insert line "\n")
927 (while (and lines
928 (or (= (length (car lines)) 0)
929 (not ind)
930 (equal ind (get-text-property 0 'original-indentation (car lines))))
931 (or (= (length (car lines)) 0)
932 (get-text-property 0 'org-protected (car lines))))
933 (insert (pop lines) "\n"))
934 (and par (insert "<p>\n")))
935 (throw 'nextline nil))
937 ;; Blockquotes, verse, and center
938 (when (equal "ORG-BLOCKQUOTE-START" line)
939 (org-close-par-maybe)
940 (insert "<blockquote>\n")
941 (org-open-par)
942 (throw 'nextline nil))
943 (when (equal "ORG-BLOCKQUOTE-END" line)
944 (org-close-par-maybe)
945 (insert "\n</blockquote>\n")
946 (org-open-par)
947 (throw 'nextline nil))
948 (when (equal "ORG-VERSE-START" line)
949 (org-close-par-maybe)
950 (insert "\n<p class=\"verse\">\n")
951 (setq inverse t)
952 (throw 'nextline nil))
953 (when (equal "ORG-VERSE-END" line)
954 (insert "</p>\n")
955 (org-open-par)
956 (setq inverse nil)
957 (throw 'nextline nil))
958 (when (equal "ORG-CENTER-START" line)
959 (org-close-par-maybe)
960 (insert "\n<div style=\"text-align: center\">")
961 (org-open-par)
962 (throw 'nextline nil))
963 (when (equal "ORG-CENTER-END" line)
964 (org-close-par-maybe)
965 (insert "\n</div>")
966 (org-open-par)
967 (throw 'nextline nil))
968 (run-hooks 'org-export-html-after-blockquotes-hook)
969 (when inverse
970 (let ((i (org-get-string-indentation line)))
971 (if (> i 0)
972 (setq line (concat (mapconcat 'identity
973 (make-list (* 2 i) "\\nbsp") "")
974 " " (org-trim line))))
975 (unless (string-match "\\\\\\\\[ \t]*$" line)
976 (setq line (concat line "\\\\")))))
978 ;; make targets to anchors
979 (setq start 0)
980 (while (string-match
981 "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line start)
982 (cond
983 ((get-text-property (match-beginning 1) 'org-protected line)
984 (setq start (match-end 1)))
985 ((match-end 2)
986 (setq line (replace-match
987 (format
988 "@<a name=\"%s\" id=\"%s\">@</a>"
989 (org-solidify-link-text (match-string 1 line))
990 (org-solidify-link-text (match-string 1 line)))
991 t t line)))
992 ((and org-export-with-toc (equal (string-to-char line) ?*))
993 ;; FIXME: NOT DEPENDENT on TOC?????????????????????
994 (setq line (replace-match
995 (concat "@<span class=\"target\">"
996 (match-string 1 line) "@</span> ")
997 ;; (concat "@<i>" (match-string 1 line) "@</i> ")
998 t t line)))
1000 (setq line (replace-match
1001 (concat "@<a name=\""
1002 (org-solidify-link-text (match-string 1 line))
1003 "\" class=\"target\">" (match-string 1 line)
1004 "@</a> ")
1005 t t line)))))
1007 (setq line (org-html-handle-time-stamps line))
1009 ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
1010 ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
1011 ;; Also handle sub_superscripts and checkboxes
1012 (or (string-match org-table-hline-regexp line)
1013 (setq line (org-html-expand line)))
1015 ;; Format the links
1016 (setq start 0)
1017 (while (string-match org-bracket-link-analytic-regexp++ line start)
1018 (setq start (match-beginning 0))
1019 (setq path (save-match-data (org-link-unescape
1020 (match-string 3 line))))
1021 (setq type (cond
1022 ((match-end 2) (match-string 2 line))
1023 ((save-match-data
1024 (or (file-name-absolute-p path)
1025 (string-match "^\\.\\.?/" path)))
1026 "file")
1027 (t "internal")))
1028 (setq path (org-extract-attributes (org-link-unescape path)))
1029 (setq attr (get-text-property 0 'org-attributes path))
1030 (setq desc1 (if (match-end 5) (match-string 5 line))
1031 desc2 (if (match-end 2) (concat type ":" path) path)
1032 descp (and desc1 (not (equal desc1 desc2)))
1033 desc (or desc1 desc2))
1034 ;; Make an image out of the description if that is so wanted
1035 (when (and descp (org-file-image-p
1036 desc org-export-html-inline-image-extensions))
1037 (save-match-data
1038 (if (string-match "^file:" desc)
1039 (setq desc (substring desc (match-end 0)))))
1040 (setq desc (org-add-props
1041 (concat "<img src=\"" desc "\"/>")
1042 '(org-protected t))))
1043 ;; FIXME: do we need to unescape here somewhere?
1044 (cond
1045 ((equal type "internal")
1046 (setq rpl
1047 (concat
1048 "<a href=\""
1049 (if (= (string-to-char path) ?#) "" "#")
1050 (org-solidify-link-text
1051 (save-match-data (org-link-unescape path)) nil)
1052 "\"" attr ">"
1053 (org-export-html-format-desc desc)
1054 "</a>")))
1055 ((and (equal type "id")
1056 (setq id-file (org-id-find-id-file path)))
1057 ;; This is an id: link to another file (if it was the same file,
1058 ;; it would have become an internal link...)
1059 (save-match-data
1060 (setq id-file (file-relative-name
1061 id-file (file-name-directory org-current-export-file)))
1062 (setq id-file (concat (file-name-sans-extension id-file)
1063 "." html-extension))
1064 (setq rpl (concat "<a href=\"" id-file "#"
1065 (if (org-uuidgen-p path) "ID-")
1066 path "\""
1067 attr ">"
1068 (org-export-html-format-desc desc)
1069 "</a>"))))
1070 ((member type '("http" "https"))
1071 ;; standard URL, just check if we need to inline an image
1072 (if (and (or (eq t org-export-html-inline-images)
1073 (and org-export-html-inline-images (not descp)))
1074 (org-file-image-p
1075 path org-export-html-inline-image-extensions))
1076 (setq rpl (org-export-html-format-image
1077 (concat type ":" path) org-par-open))
1078 (setq link (concat type ":" path))
1079 (setq rpl (concat "<a href=\""
1080 (org-export-html-format-href link)
1081 "\"" attr ">"
1082 (org-export-html-format-desc desc)
1083 "</a>"))))
1084 ((member type '("ftp" "mailto" "news"))
1085 ;; standard URL
1086 (setq link (concat type ":" path))
1087 (setq rpl (concat "<a href=\""
1088 (org-export-html-format-href link)
1089 "\"" attr ">"
1090 (org-export-html-format-desc desc)
1091 "</a>")))
1093 ((string= type "coderef")
1094 (setq rpl (format "<a href=\"#coderef-%s\" class=\"coderef\" onmouseover=\"CodeHighlightOn(this, 'coderef-%s');\" onmouseout=\"CodeHighlightOff(this, 'coderef-%s');\">%s</a>"
1095 path path path
1096 (format (org-export-get-coderef-format path (and descp desc))
1097 (cdr (assoc path org-export-code-refs))))))
1099 ((functionp (setq fnc (nth 2 (assoc type org-link-protocols))))
1100 ;; The link protocol has a function for format the link
1101 (setq rpl
1102 (save-match-data
1103 (funcall fnc (org-link-unescape path) desc1 'html))))
1105 ((string= type "file")
1106 ;; FILE link
1107 (let* ((filename path)
1108 (abs-p (file-name-absolute-p filename))
1109 thefile file-is-image-p search)
1110 (save-match-data
1111 (if (string-match "::\\(.*\\)" filename)
1112 (setq search (match-string 1 filename)
1113 filename (replace-match "" t nil filename)))
1114 (setq valid
1115 (if (functionp link-validate)
1116 (funcall link-validate filename current-dir)
1118 (setq file-is-image-p
1119 (org-file-image-p
1120 filename org-export-html-inline-image-extensions))
1121 (setq thefile (if abs-p (expand-file-name filename) filename))
1122 (when (and org-export-html-link-org-files-as-html
1123 (string-match "\\.org$" thefile))
1124 (setq thefile (concat (substring thefile 0
1125 (match-beginning 0))
1126 "." html-extension))
1127 (if (and search
1128 ;; make sure this is can be used as target search
1129 (not (string-match "^[0-9]*$" search))
1130 (not (string-match "^\\*" search))
1131 (not (string-match "^/.*/$" search)))
1132 (setq thefile (concat thefile "#"
1133 (org-solidify-link-text
1134 (org-link-unescape search)))))
1135 (when (string-match "^file:" desc)
1136 (setq desc (replace-match "" t t desc))
1137 (if (string-match "\\.org$" desc)
1138 (setq desc (replace-match "" t t desc))))))
1139 (setq rpl (if (and file-is-image-p
1140 (or (eq t org-export-html-inline-images)
1141 (and org-export-html-inline-images
1142 (not descp))))
1143 (progn
1144 (message "image %s %s" thefile org-par-open)
1145 (org-export-html-format-image thefile org-par-open))
1146 (concat "<a href=\"" thefile "\"" attr ">"
1147 (org-export-html-format-desc desc)
1148 "</a>")))
1149 (if (not valid) (setq rpl desc))))
1152 ;; just publish the path, as default
1153 (setq rpl (concat "<i>&lt;" type ":"
1154 (save-match-data (org-link-unescape path))
1155 "&gt;</i>"))))
1156 (setq line (replace-match rpl t t line)
1157 start (+ start (length rpl))))
1159 ;; TODO items
1160 (if (and (string-match org-todo-line-regexp line)
1161 (match-beginning 2))
1163 (setq line
1164 (concat (substring line 0 (match-beginning 2))
1165 "<span class=\""
1166 (if (member (match-string 2 line)
1167 org-done-keywords)
1168 "done" "todo")
1169 " " (match-string 2 line)
1170 "\"> " (org-export-html-get-todo-kwd-class-name
1171 (match-string 2 line))
1172 "</span>" (substring line (match-end 2)))))
1174 ;; Does this contain a reference to a footnote?
1175 (when org-export-with-footnotes
1176 (setq start 0)
1177 (while (string-match "\\([^* \t].*?\\)\\[\\([0-9]+\\)\\]" line start)
1178 (if (get-text-property (match-beginning 2) 'org-protected line)
1179 (setq start (match-end 2))
1180 (let ((n (match-string 2 line)) extra a)
1181 (if (setq a (assoc n footref-seen))
1182 (progn
1183 (setcdr a (1+ (cdr a)))
1184 (setq extra (format ".%d" (cdr a))))
1185 (setq extra "")
1186 (push (cons n 1) footref-seen))
1187 (setq line
1188 (replace-match
1189 (format
1190 (concat "%s"
1191 (format org-export-html-footnote-format
1192 "<a class=\"footref\" name=\"fnr.%s%s\" href=\"#fn.%s\">%s</a>"))
1193 (or (match-string 1 line) "") n extra n n)
1194 t t line))))))
1196 (cond
1197 ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
1198 ;; This is a headline
1199 (setq level (org-tr-level (- (match-end 1) (match-beginning 1)
1200 level-offset))
1201 txt (match-string 2 line))
1202 (if (string-match quote-re0 txt)
1203 (setq txt (replace-match "" t t txt)))
1204 (if (<= level (max umax umax-toc))
1205 (setq head-count (+ head-count 1)))
1206 (when in-local-list
1207 ;; Close any local lists before inserting a new header line
1208 (while local-list-type
1209 (org-close-li (car local-list-type))
1210 (insert (format "</%sl>\n" (car local-list-type)))
1211 (pop local-list-type))
1212 (setq local-list-indent nil
1213 in-local-list nil))
1214 (setq first-heading-pos (or first-heading-pos (point)))
1215 (org-html-level-start level txt umax
1216 (and org-export-with-toc (<= level umax))
1217 head-count)
1219 ;; QUOTES
1220 (when (string-match quote-re line)
1221 (org-close-par-maybe)
1222 (insert "<pre>")
1223 (setq inquote t)))
1225 ((string-match "^[ \t]*- __+[ \t]*$" line)
1226 ;; Explicit list closure
1227 (when local-list-type
1228 (let ((ind (org-get-indentation line)))
1229 (while (and local-list-indent
1230 (<= ind (car local-list-indent)))
1231 (org-close-li (car local-list-type))
1232 (insert (format "</%sl>\n" (car local-list-type)))
1233 (pop local-list-type)
1234 (pop local-list-indent))
1235 (or local-list-indent (setq in-local-list nil))))
1236 (throw 'nextline nil))
1238 ((and org-export-with-tables
1239 (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
1240 (when (not table-open)
1241 ;; New table starts
1242 (setq table-open t table-buffer nil table-orig-buffer nil))
1244 ;; Accumulate lines
1245 (setq table-buffer (cons line table-buffer)
1246 table-orig-buffer (cons origline table-orig-buffer))
1247 (when (or (not lines)
1248 (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
1249 (car lines))))
1250 (setq table-open nil
1251 table-buffer (nreverse table-buffer)
1252 table-orig-buffer (nreverse table-orig-buffer))
1253 (org-close-par-maybe)
1254 (insert (org-format-table-html table-buffer table-orig-buffer))))
1256 ;; Normal lines
1257 (when (string-match
1258 (cond
1259 ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
1260 ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
1261 ((= llt ?\)) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
1262 (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
1263 line)
1264 (setq ind (or (get-text-property 0 'original-indentation line)
1265 (org-get-string-indentation line))
1266 item-type (if (match-beginning 4) "o" "u")
1267 starter (if (match-beginning 2)
1268 (substring (match-string 2 line) 0 -1))
1269 line (substring line (match-beginning 5))
1270 item-tag nil)
1271 (if (and starter (string-match "\\(.*?\\) ::[ \t]*" line))
1272 (setq item-type "d"
1273 item-tag (match-string 1 line)
1274 line (substring line (match-end 0))))
1275 (when (and (not (equal item-type "d"))
1276 (not (string-match "[^ \t]" line)))
1277 ;; empty line. Pretend indentation is large.
1278 (setq ind (if org-empty-line-terminates-plain-lists
1280 (1+ (or (car local-list-indent) 1)))))
1281 (setq didclose nil)
1282 (while (and in-local-list
1283 (or (and (= ind (car local-list-indent))
1284 (not starter))
1285 (< ind (car local-list-indent))))
1286 (setq didclose t)
1287 (org-close-li (car local-list-type))
1288 (insert (format "</%sl>\n" (car local-list-type)))
1289 (pop local-list-type) (pop local-list-indent)
1290 (setq in-local-list local-list-indent))
1291 (cond
1292 ((and starter
1293 (or (not in-local-list)
1294 (> ind (car local-list-indent))))
1295 ;; Start new (level of) list
1296 (org-close-par-maybe)
1297 (insert (cond
1298 ((equal item-type "u") "<ul>\n<li>\n")
1299 ((equal item-type "o") "<ol>\n<li>\n")
1300 ((equal item-type "d")
1301 (format "<dl>\n<dt>%s</dt><dd>\n" item-tag))))
1302 (push item-type local-list-type)
1303 (push ind local-list-indent)
1304 (setq in-local-list t))
1305 (starter
1306 ;; continue current list
1307 (org-close-li (car local-list-type))
1308 (insert (cond
1309 ((equal (car local-list-type) "d")
1310 (format "<dt>%s</dt><dd>\n" (or item-tag "???")))
1311 (t "<li>\n"))))
1312 (didclose
1313 ;; we did close a list, normal text follows: need <p>
1314 (org-open-par)))
1315 (if (string-match "^[ \t]*\\[\\([X ]\\)\\]" line)
1316 (setq line
1317 (replace-match
1318 (if (equal (match-string 1 line) "X")
1319 "<b>[X]</b>"
1320 "<b>[<span style=\"visibility:hidden;\">X</span>]</b>")
1321 t t line))))
1323 ;; Horizontal line
1324 (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line)
1325 (if org-par-open
1326 (insert "\n</p>\n<hr/>\n<p>\n")
1327 (insert "\n<hr/>\n"))
1328 (throw 'nextline nil))
1330 ;; Empty lines start a new paragraph. If hand-formatted lists
1331 ;; are not fully interpreted, lines starting with "-", "+", "*"
1332 ;; also start a new paragraph.
1333 (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (org-open-par))
1335 ;; Is this the start of a footnote?
1336 (when org-export-with-footnotes
1337 (when (and (boundp 'footnote-section-tag-regexp)
1338 (string-match (concat "^" footnote-section-tag-regexp)
1339 line))
1340 ;; ignore this line
1341 (throw 'nextline nil))
1342 (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
1343 (org-close-par-maybe)
1344 (let ((n (match-string 1 line)))
1345 (setq org-par-open t
1346 line (replace-match
1347 (format
1348 (concat "<p class=\"footnote\">"
1349 (format org-export-html-footnote-format
1350 "<a class=\"footnum\" name=\"fn.%s\" href=\"#fnr.%s\">%s</a>"))
1351 n n n) t t line)))))
1352 ;; Check if the line break needs to be conserved
1353 (cond
1354 ((string-match "\\\\\\\\[ \t]*$" line)
1355 (setq line (replace-match "<br/>" t t line)))
1356 (org-export-preserve-breaks
1357 (setq line (concat line "<br/>"))))
1359 ;; Check if a paragraph should be started
1360 (let ((start 0))
1361 (while (and org-par-open
1362 (string-match "\\\\par\\>" line start))
1363 ;; Leave a space in the </p> so that the footnote matcher
1364 ;; does not see this.
1365 (if (not (get-text-property (match-beginning 0)
1366 'org-protected line))
1367 (setq line (replace-match "</p ><p >" t t line)))
1368 (setq start (match-end 0))))
1370 (insert line "\n")))))
1372 ;; Properly close all local lists and other lists
1373 (when inquote
1374 (insert "</pre>\n")
1375 (org-open-par))
1376 (when in-local-list
1377 ;; Close any local lists before inserting a new header line
1378 (while local-list-type
1379 (org-close-li (car local-list-type))
1380 (insert (format "</%sl>\n" (car local-list-type)))
1381 (pop local-list-type))
1382 (setq local-list-indent nil
1383 in-local-list nil))
1384 (org-html-level-start 1 nil umax
1385 (and org-export-with-toc (<= level umax))
1386 head-count)
1387 ;; the </div> to close the last text-... div.
1388 (when (and (> umax 0) first-heading-pos) (insert "</div>\n"))
1390 (save-excursion
1391 (goto-char (point-min))
1392 (while (re-search-forward "<p class=\"footnote\">[^\000]*?\\(</p>\\|\\'\\)" nil t)
1393 (push (match-string 0) footnotes)
1394 (replace-match "" t t)))
1395 (when footnotes
1396 (insert (format org-export-html-footnotes-section
1397 (nth 4 lang-words)
1398 (mapconcat 'identity (nreverse footnotes) "\n"))
1399 "\n"))
1400 (let ((bib (org-export-html-get-bibliography)))
1401 (when bib
1402 (insert "\n" bib "\n")))
1403 (unless body-only
1404 (when (plist-get opt-plist :auto-postamble)
1405 (insert "<div id=\"postamble\">\n")
1406 (when (and org-export-author-info author)
1407 (insert "<p class=\"author\"> "
1408 (nth 1 lang-words) ": " author "\n")
1409 (when email
1410 (if (listp (split-string email ",+ *"))
1411 (mapc (lambda(e)
1412 (insert "<a href=\"mailto:" e "\">&lt;"
1413 e "&gt;</a>\n"))
1414 (split-string email ",+ *"))
1415 (insert "<a href=\"mailto:" email "\">&lt;"
1416 email "&gt;</a>\n")))
1417 (insert "</p>\n"))
1418 (when (and date org-export-time-stamp-file)
1419 (insert "<p class=\"date\"> "
1420 (nth 2 lang-words) ": "
1421 date "</p>\n"))
1422 (when org-export-creator-info
1423 (insert (format "<p class=\"creator\">HTML generated by org-mode %s in emacs %s</p>\n"
1424 org-version emacs-major-version)))
1425 (when org-export-html-validation-link
1426 (insert org-export-html-validation-link "\n"))
1427 (insert "</div>"))
1429 (if org-export-html-with-timestamp
1430 (insert org-export-html-html-helper-timestamp))
1431 (org-export-html-insert-plist-item opt-plist :postamble opt-plist)
1432 (insert "\n</div>\n</body>\n</html>\n"))
1434 (unless (plist-get opt-plist :buffer-will-be-killed)
1435 (normal-mode)
1436 (if (eq major-mode (default-value 'major-mode))
1437 (html-mode)))
1439 ;; insert the table of contents
1440 (goto-char (point-min))
1441 (when thetoc
1442 (if (or (re-search-forward
1443 "<p>\\s-*\\[TABLE-OF-CONTENTS\\]\\s-*</p>" nil t)
1444 (re-search-forward
1445 "\\[TABLE-OF-CONTENTS\\]" nil t))
1446 (progn
1447 (goto-char (match-beginning 0))
1448 (replace-match ""))
1449 (goto-char first-heading-pos)
1450 (when (looking-at "\\s-*</p>")
1451 (goto-char (match-end 0))
1452 (insert "\n")))
1453 (insert "<div id=\"table-of-contents\">\n")
1454 (mapc 'insert thetoc)
1455 (insert "</div>\n"))
1456 ;; remove empty paragraphs and lists
1457 (goto-char (point-min))
1458 (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t)
1459 (replace-match ""))
1460 (goto-char (point-min))
1461 (while (re-search-forward "<li>[ \r\n\t]*</li>\n?" nil t)
1462 (replace-match ""))
1463 (goto-char (point-min))
1464 (while (re-search-forward "</ul>\\s-*<ul>\n?" nil t)
1465 (replace-match ""))
1466 ;; Convert whitespace place holders
1467 (goto-char (point-min))
1468 (let (beg end n)
1469 (while (setq beg (next-single-property-change (point) 'org-whitespace))
1470 (setq n (get-text-property beg 'org-whitespace)
1471 end (next-single-property-change beg 'org-whitespace))
1472 (goto-char beg)
1473 (delete-region beg end)
1474 (insert (format "<span style=\"visibility:hidden;\">%s</span>"
1475 (make-string n ?x)))))
1476 ;; Remove empty lines at the beginning of the file.
1477 (goto-char (point-min))
1478 (when (looking-at "\\s-+\n") (replace-match ""))
1479 ;; Remove display properties
1480 (remove-text-properties (point-min) (point-max) '(display t))
1481 ;; Run the hook
1482 (run-hooks 'org-export-html-final-hook)
1483 (or to-buffer (save-buffer))
1484 (goto-char (point-min))
1485 (or (org-export-push-to-kill-ring "HTML")
1486 (message "Exporting... done"))
1487 (if (eq to-buffer 'string)
1488 (prog1 (buffer-substring (point-min) (point-max))
1489 (kill-buffer (current-buffer)))
1490 (current-buffer)))))
1492 (defun org-export-html-insert-plist-item (plist key &rest args)
1493 (let ((item (plist-get plist key)))
1494 (cond ((functionp item)
1495 (apply item args))
1496 (item
1497 (insert item)))))
1499 (defun org-export-html-format-href (s)
1500 "Make sure the S is valid as a href reference in an XHTML document."
1501 (save-match-data
1502 (let ((start 0))
1503 (while (string-match "&" s start)
1504 (setq start (+ (match-beginning 0) 3)
1505 s (replace-match "&amp;" t t s)))))
1508 (defun org-export-html-format-desc (s)
1509 "Make sure the S is valid as a description in a link."
1510 (if (and s (not (get-text-property 1 'org-protected s)))
1511 (save-match-data
1512 (org-html-do-expand s))
1515 (defun org-export-html-format-image (src par-open)
1516 "Create image tag with source and attributes."
1517 (save-match-data
1518 (if (string-match "^ltxpng/" src)
1519 (format "<img src=\"%s\"/>" src)
1520 (let* ((caption (org-find-text-property-in-string 'org-caption src))
1521 (attr (org-find-text-property-in-string 'org-attributes src))
1522 (label (org-find-text-property-in-string 'org-label src)))
1523 (concat
1524 (if caption
1525 (format "%s<div %sclass=\"figure\">
1526 <p>"
1527 (if org-par-open "</p>\n" "")
1528 (if label (format "id=\"%s\" " label) "")))
1529 (format "<img src=\"%s\"%s />"
1531 (if (string-match "\\<alt=" (or attr ""))
1532 (concat " " attr )
1533 (concat " " attr " alt=\"" src "\"")))
1534 (if caption
1535 (format "</p>%s
1536 </div>%s"
1537 (concat "\n<p>" caption "</p>")
1538 (if org-par-open "\n<p>" ""))))))))
1540 (defun org-export-html-get-bibliography ()
1541 "Find bibliography, cut it out and return it."
1542 (catch 'exit
1543 (let (beg end (cnt 1) bib)
1544 (save-excursion
1545 (goto-char (point-min))
1546 (when (re-search-forward "^[ \t]*<div \\(id\\|class\\)=\"bibliography\"" nil t)
1547 (setq beg (match-beginning 0))
1548 (while (re-search-forward "</?div\\>" nil t)
1549 (setq cnt (+ cnt (if (string= (match-string 0) "<div") +1 -1)))
1550 (when (= cnt 0)
1551 (and (looking-at ">") (forward-char 1))
1552 (setq bib (buffer-substring beg (point)))
1553 (delete-region beg (point))
1554 (throw 'exit bib))))
1555 nil))))
1557 (defvar org-table-number-regexp) ; defined in org-table.el
1558 (defun org-format-table-html (lines olines)
1559 "Find out which HTML converter to use and return the HTML code."
1560 (if (stringp lines)
1561 (setq lines (org-split-string lines "\n")))
1562 (if (string-match "^[ \t]*|" (car lines))
1563 ;; A normal org table
1564 (org-format-org-table-html lines)
1565 ;; Table made by table.el - test for spanning
1566 (let* ((hlines (delq nil (mapcar
1567 (lambda (x)
1568 (if (string-match "^[ \t]*\\+-" x) x
1569 nil))
1570 lines)))
1571 (first (car hlines))
1572 (ll (and (string-match "\\S-+" first)
1573 (match-string 0 first)))
1574 (re (concat "^[ \t]*" (regexp-quote ll)))
1575 (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
1576 hlines))))
1577 (if (and (not spanning)
1578 (not org-export-prefer-native-exporter-for-tables))
1579 ;; We can use my own converter with HTML conversions
1580 (org-format-table-table-html lines)
1581 ;; Need to use the code generator in table.el, with the original text.
1582 (org-format-table-table-html-using-table-generate-source olines)))))
1584 (defvar org-table-number-fraction) ; defined in org-table.el
1585 (defun org-format-org-table-html (lines &optional splice)
1586 "Format a table into HTML."
1587 (require 'org-table)
1588 ;; Get rid of hlines at beginning and end
1589 (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
1590 (setq lines (nreverse lines))
1591 (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
1592 (setq lines (nreverse lines))
1593 (when org-export-table-remove-special-lines
1594 ;; Check if the table has a marking column. If yes remove the
1595 ;; column and the special lines
1596 (setq lines (org-table-clean-before-export lines)))
1598 (let* ((caption (or (get-text-property 0 'org-caption (car lines))
1599 (get-text-property (or (next-single-property-change
1600 0 'org-caption (car lines))
1602 'org-caption (car lines))))
1603 (attributes (or (get-text-property 0 'org-attributes (car lines))
1604 (get-text-property (or (next-single-property-change
1605 0 'org-attributes (car lines))
1607 'org-attributes (car lines))))
1608 (html-table-tag (org-export-splice-attributes
1609 html-table-tag attributes))
1610 (head (and org-export-highlight-first-table-line
1611 (delq nil (mapcar
1612 (lambda (x) (string-match "^[ \t]*|-" x))
1613 (cdr lines)))))
1615 (nline 0) fnum i
1616 tbopen line fields html gr colgropen rowstart rowend)
1617 (if splice (setq head nil))
1618 (unless splice (push (if head "<thead>" "<tbody>") html))
1619 (setq tbopen t)
1620 (while (setq line (pop lines))
1621 (catch 'next-line
1622 (if (string-match "^[ \t]*|-" line)
1623 (progn
1624 (unless splice
1625 (push (if head "</thead>" "</tbody>") html)
1626 (if lines (push "<tbody>" html) (setq tbopen nil)))
1627 (setq head nil) ;; head ends here, first time around
1628 ;; ignore this line
1629 (throw 'next-line t)))
1630 ;; Break the line into fields
1631 (setq fields (org-split-string line "[ \t]*|[ \t]*"))
1632 (unless fnum (setq fnum (make-vector (length fields) 0)))
1633 (setq nline (1+ nline) i -1
1634 rowstart (eval (car org-export-table-row-tags))
1635 rowend (eval (cdr org-export-table-row-tags)))
1636 (push (concat rowstart
1637 (mapconcat
1638 (lambda (x)
1639 (setq i (1+ i))
1640 (if (and (< i nline)
1641 (string-match org-table-number-regexp x))
1642 (incf (aref fnum i)))
1643 (cond
1644 (head
1645 (concat
1646 (format (car org-export-table-header-tags) "col")
1648 (cdr org-export-table-header-tags)))
1649 ((and (= i 0) org-export-html-table-use-header-tags-for-first-column)
1650 (concat
1651 (format (car org-export-table-header-tags) "row")
1653 (cdr org-export-table-header-tags)))
1655 (concat (car org-export-table-data-tags) x
1656 (cdr org-export-table-data-tags)))))
1657 fields "")
1658 rowend)
1659 html)))
1660 (unless splice (if tbopen (push "</tbody>" html)))
1661 (unless splice (push "</table>\n" html))
1662 (setq html (nreverse html))
1663 (unless splice
1664 ;; Put in col tags with the alignment (unfortunately often ignored...)
1665 (unless (car org-table-colgroup-info)
1666 (setq org-table-colgroup-info
1667 (cons :start (cdr org-table-colgroup-info))))
1668 (push (mapconcat
1669 (lambda (x)
1670 (setq gr (pop org-table-colgroup-info))
1671 (format "%s<col align=\"%s\" />%s"
1672 (if (memq gr '(:start :startend))
1673 (prog1
1674 (if colgropen "</colgroup>\n<colgroup>" "<colgroup>")
1675 (setq colgropen t))
1677 (if (> (/ (float x) nline) org-table-number-fraction)
1678 "right" "left")
1679 (if (memq gr '(:end :startend))
1680 (progn (setq colgropen nil) "</colgroup>")
1681 "")))
1682 fnum "")
1683 html)
1684 (if colgropen (setq html (cons (car html) (cons "</colgroup>" (cdr html)))))
1685 ;; Since the output of HTML table formatter can also be used in
1686 ;; DocBook document, we want to always include the caption to make
1687 ;; DocBook XML file valid.
1688 (push (format "<caption>%s</caption>" (or caption "")) html)
1689 (push html-table-tag html))
1690 (concat (mapconcat 'identity html "\n") "\n")))
1692 (defun org-export-splice-attributes (tag attributes)
1693 "Read attributes in string ATTRIBUTES, add and replace in HTML tag TAG."
1694 (if (not attributes)
1696 (let (oldatt newatt)
1697 (setq oldatt (org-extract-attributes-from-string tag)
1698 tag (pop oldatt)
1699 newatt (cdr (org-extract-attributes-from-string attributes)))
1700 (while newatt
1701 (setq oldatt (plist-put oldatt (pop newatt) (pop newatt))))
1702 (if (string-match ">" tag)
1703 (setq tag
1704 (replace-match (concat (org-attributes-to-string oldatt) ">")
1705 t t tag)))
1706 tag)))
1708 (defun org-format-table-table-html (lines)
1709 "Format a table generated by table.el into HTML.
1710 This conversion does *not* use `table-generate-source' from table.el.
1711 This has the advantage that Org-mode's HTML conversions can be used.
1712 But it has the disadvantage, that no cell- or row-spanning is allowed."
1713 (let (line field-buffer
1714 (head org-export-highlight-first-table-line)
1715 fields html empty i)
1716 (setq html (concat html-table-tag "\n"))
1717 (while (setq line (pop lines))
1718 (setq empty "&nbsp;")
1719 (catch 'next-line
1720 (if (string-match "^[ \t]*\\+-" line)
1721 (progn
1722 (if field-buffer
1723 (progn
1724 (setq
1725 html
1726 (concat
1727 html
1728 "<tr>"
1729 (mapconcat
1730 (lambda (x)
1731 (if (equal x "") (setq x empty))
1732 (if head
1733 (concat
1734 (format (car org-export-table-header-tags) "col")
1736 (cdr org-export-table-header-tags))
1737 (concat (car org-export-table-data-tags) x
1738 (cdr org-export-table-data-tags))))
1739 field-buffer "\n")
1740 "</tr>\n"))
1741 (setq head nil)
1742 (setq field-buffer nil)))
1743 ;; Ignore this line
1744 (throw 'next-line t)))
1745 ;; Break the line into fields and store the fields
1746 (setq fields (org-split-string line "[ \t]*|[ \t]*"))
1747 (if field-buffer
1748 (setq field-buffer (mapcar
1749 (lambda (x)
1750 (concat x "<br/>" (pop fields)))
1751 field-buffer))
1752 (setq field-buffer fields))))
1753 (setq html (concat html "</table>\n"))
1754 html))
1756 (defun org-format-table-table-html-using-table-generate-source (lines)
1757 "Format a table into html, using `table-generate-source' from table.el.
1758 This has the advantage that cell- or row-spanning is allowed.
1759 But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
1760 (require 'table)
1761 (with-current-buffer (get-buffer-create " org-tmp1 ")
1762 (erase-buffer)
1763 (insert (mapconcat 'identity lines "\n"))
1764 (goto-char (point-min))
1765 (if (not (re-search-forward "|[^+]" nil t))
1766 (error "Error processing table"))
1767 (table-recognize-table)
1768 (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
1769 (table-generate-source 'html " org-tmp2 ")
1770 (set-buffer " org-tmp2 ")
1771 (buffer-substring (point-min) (point-max))))
1773 (defun org-export-splice-style (style extra)
1774 "Splice EXTRA into STYLE, just before \"</style>\"."
1775 (if (and (stringp extra)
1776 (string-match "\\S-" extra)
1777 (string-match "</style>" style))
1778 (concat (substring style 0 (match-beginning 0))
1779 "\n" extra "\n"
1780 (substring style (match-beginning 0)))
1781 style))
1783 (defun org-html-handle-time-stamps (s)
1784 "Format time stamps in string S, or remove them."
1785 (catch 'exit
1786 (let (r b)
1787 (while (string-match org-maybe-keyword-time-regexp s)
1788 (or b (setq b (substring s 0 (match-beginning 0))))
1789 (setq r (concat
1790 r (substring s 0 (match-beginning 0))
1791 " @<span class=\"timestamp-wrapper\">"
1792 (if (match-end 1)
1793 (format "@<span class=\"timestamp-kwd\">%s @</span>"
1794 (match-string 1 s)))
1795 (format " @<span class=\"timestamp\">%s@</span>"
1796 (substring
1797 (org-translate-time (match-string 3 s)) 1 -1))
1798 "@</span>")
1799 s (substring s (match-end 0))))
1800 ;; Line break if line started and ended with time stamp stuff
1801 (if (not r)
1803 (setq r (concat r s))
1804 (unless (string-match "\\S-" (concat b s))
1805 (setq r (concat r "@<br/>")))
1806 r))))
1808 (defvar htmlize-buffer-places) ; from htmlize.el
1809 (defun org-export-htmlize-region-for-paste (beg end)
1810 "Convert the region to HTML, using htmlize.el.
1811 This is much like `htmlize-region-for-paste', only that it uses
1812 the settings define in the org-... variables."
1813 (let* ((htmlize-output-type org-export-htmlize-output-type)
1814 (htmlize-css-name-prefix org-export-htmlize-css-font-prefix)
1815 (htmlbuf (htmlize-region beg end)))
1816 (unwind-protect
1817 (with-current-buffer htmlbuf
1818 (buffer-substring (plist-get htmlize-buffer-places 'content-start)
1819 (plist-get htmlize-buffer-places 'content-end)))
1820 (kill-buffer htmlbuf))))
1822 ;;;###autoload
1823 (defun org-export-htmlize-generate-css ()
1824 "Create the CSS for all font definitions in the current Emacs session.
1825 Use this to create face definitions in your CSS style file that can then
1826 be used by code snippets transformed by htmlize.
1827 This command just produces a buffer that contains class definitions for all
1828 faces used in the current Emacs session. You can copy and paste the ones you
1829 need into your CSS file.
1831 If you then set `org-export-htmlize-output-type' to `css', calls to
1832 the function `org-export-htmlize-region-for-paste' will produce code
1833 that uses these same face definitions."
1834 (interactive)
1835 (require 'htmlize)
1836 (and (get-buffer "*html*") (kill-buffer "*html*"))
1837 (with-temp-buffer
1838 (let ((fl (face-list))
1839 (htmlize-css-name-prefix "org-")
1840 (htmlize-output-type 'css)
1841 f i)
1842 (while (setq f (pop fl)
1843 i (and f (face-attribute f :inherit)))
1844 (when (and (symbolp f) (or (not i) (not (listp i))))
1845 (insert (org-add-props (copy-sequence "1") nil 'face f))))
1846 (htmlize-region (point-min) (point-max))))
1847 (switch-to-buffer "*html*")
1848 (goto-char (point-min))
1849 (if (re-search-forward "<style" nil t)
1850 (delete-region (point-min) (match-beginning 0)))
1851 (if (re-search-forward "</style>" nil t)
1852 (delete-region (1+ (match-end 0)) (point-max)))
1853 (beginning-of-line 1)
1854 (if (looking-at " +") (replace-match ""))
1855 (goto-char (point-min)))
1857 (defun org-html-protect (s)
1858 ;; convert & to &amp;, < to &lt; and > to &gt;
1859 (let ((start 0))
1860 (while (string-match "&" s start)
1861 (setq s (replace-match "&amp;" t t s)
1862 start (1+ (match-beginning 0))))
1863 (while (string-match "<" s)
1864 (setq s (replace-match "&lt;" t t s)))
1865 (while (string-match ">" s)
1866 (setq s (replace-match "&gt;" t t s)))
1867 ; (while (string-match "\"" s)
1868 ; (setq s (replace-match "&quot;" t t s)))
1872 (defun org-html-expand (string)
1873 "Prepare STRING for HTML export. Applies all active conversions.
1874 If there are links in the string, don't modify these."
1875 (let* ((re (concat org-bracket-link-regexp "\\|"
1876 (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$")))
1877 m s l res)
1878 (while (setq m (string-match re string))
1879 (setq s (substring string 0 m)
1880 l (match-string 0 string)
1881 string (substring string (match-end 0)))
1882 (push (org-html-do-expand s) res)
1883 (push l res))
1884 (push (org-html-do-expand string) res)
1885 (apply 'concat (nreverse res))))
1887 (defun org-html-do-expand (s)
1888 "Apply all active conversions to translate special ASCII to HTML."
1889 (setq s (org-html-protect s))
1890 (if org-export-html-expand
1891 (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
1892 (setq s (replace-match "<\\1>" t nil s))))
1893 (if org-export-with-emphasize
1894 (setq s (org-export-html-convert-emphasize s)))
1895 (if org-export-with-special-strings
1896 (setq s (org-export-html-convert-special-strings s)))
1897 (if org-export-with-sub-superscripts
1898 (setq s (org-export-html-convert-sub-super s)))
1899 (if org-export-with-TeX-macros
1900 (let ((start 0) wd ass)
1901 (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)\\({}\\)?"
1902 s start))
1903 (if (get-text-property (match-beginning 0) 'org-protected s)
1904 (setq start (match-end 0))
1905 (setq wd (match-string 1 s))
1906 (if (setq ass (assoc wd org-html-entities))
1907 (setq s (replace-match (or (cdr ass)
1908 (concat "&" (car ass) ";"))
1909 t t s))
1910 (setq start (+ start (length wd))))))))
1913 (defun org-export-html-convert-special-strings (string)
1914 "Convert special characters in STRING to HTML."
1915 (let ((all org-export-html-special-string-regexps)
1916 e a re rpl start)
1917 (while (setq a (pop all))
1918 (setq re (car a) rpl (cdr a) start 0)
1919 (while (string-match re string start)
1920 (if (get-text-property (match-beginning 0) 'org-protected string)
1921 (setq start (match-end 0))
1922 (setq string (replace-match rpl t nil string)))))
1923 string))
1925 (defun org-export-html-convert-sub-super (string)
1926 "Convert sub- and superscripts in STRING to HTML."
1927 (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
1928 (while (string-match org-match-substring-regexp string s)
1929 (cond
1930 ((and requireb (match-end 8)) (setq s (match-end 2)))
1931 ((get-text-property (match-beginning 2) 'org-protected string)
1932 (setq s (match-end 2)))
1934 (setq s (match-end 1)
1935 key (if (string= (match-string 2 string) "_") "sub" "sup")
1936 c (or (match-string 8 string)
1937 (match-string 6 string)
1938 (match-string 5 string))
1939 string (replace-match
1940 (concat (match-string 1 string)
1941 "<" key ">" c "</" key ">")
1942 t t string)))))
1943 (while (string-match "\\\\\\([_^]\\)" string)
1944 (setq string (replace-match (match-string 1 string) t t string)))
1945 string))
1947 (defun org-export-html-convert-emphasize (string)
1948 "Apply emphasis."
1949 (let ((s 0) rpl)
1950 (while (string-match org-emph-re string s)
1951 (if (not (equal
1952 (substring string (match-beginning 3) (1+ (match-beginning 3)))
1953 (substring string (match-beginning 4) (1+ (match-beginning 4)))))
1954 (setq s (match-beginning 0)
1956 (concat
1957 (match-string 1 string)
1958 (nth 2 (assoc (match-string 3 string) org-emphasis-alist))
1959 (match-string 4 string)
1960 (nth 3 (assoc (match-string 3 string)
1961 org-emphasis-alist))
1962 (match-string 5 string))
1963 string (replace-match rpl t t string)
1964 s (+ s (- (length rpl) 2)))
1965 (setq s (1+ s))))
1966 string))
1968 (defun org-open-par ()
1969 "Insert <p>, but first close previous paragraph if any."
1970 (org-close-par-maybe)
1971 (insert "\n<p>")
1972 (setq org-par-open t))
1973 (defun org-close-par-maybe ()
1974 "Close paragraph if there is one open."
1975 (when org-par-open
1976 (insert "</p>")
1977 (setq org-par-open nil)))
1978 (defun org-close-li (&optional type)
1979 "Close <li> if necessary."
1980 (org-close-par-maybe)
1981 (insert (if (equal type "d") "</dd>\n" "</li>\n")))
1983 (defvar in-local-list)
1984 (defvar local-list-indent)
1985 (defvar local-list-type)
1986 (defun org-export-html-close-lists-maybe (line)
1987 (let ((ind (or (get-text-property 0 'original-indentation line)))
1988 ; (and (string-match "\\S-" line)
1989 ; (org-get-indentation line))))
1990 didclose)
1991 (when ind
1992 (while (and in-local-list
1993 (<= ind (car local-list-indent)))
1994 (setq didclose t)
1995 (org-close-li (car local-list-type))
1996 (insert (format "</%sl>\n" (car local-list-type)))
1997 (pop local-list-type) (pop local-list-indent)
1998 (setq in-local-list local-list-indent))
1999 (and didclose (org-open-par)))))
2001 (defvar body-only) ; dynamically scoped into this.
2002 (defun org-html-level-start (level title umax with-toc head-count)
2003 "Insert a new level in HTML export.
2004 When TITLE is nil, just close all open levels."
2005 (org-close-par-maybe)
2006 (let* ((target (and title (org-get-text-property-any 0 'target title)))
2007 (extra-targets (assoc target org-export-target-aliases))
2008 (extra-class (and title (org-get-text-property-any 0 'html-container-class title)))
2009 (preferred (cdr (assoc target org-export-preferred-target-alist)))
2010 (remove (or preferred target))
2011 (l org-level-max)
2012 snumber href suffix)
2013 (setq extra-targets (remove remove extra-targets))
2014 (setq extra-targets
2015 (mapconcat (lambda (x)
2016 (if (org-uuidgen-p x) (setq x (concat "ID-" x)))
2017 (format "<a name=\"%s\" id=\"%s\"></a>"
2018 x x))
2019 extra-targets
2020 ""))
2021 (while (>= l level)
2022 (if (aref org-levels-open (1- l))
2023 (progn
2024 (org-html-level-close l umax)
2025 (aset org-levels-open (1- l) nil)))
2026 (setq l (1- l)))
2027 (when title
2028 ;; If title is nil, this means this function is called to close
2029 ;; all levels, so the rest is done only if title is given
2030 (when (string-match (org-re "\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
2031 (setq title (replace-match
2032 (if org-export-with-tags
2033 (save-match-data
2034 (concat
2035 "&nbsp;&nbsp;&nbsp;<span class=\"tag\">"
2036 (mapconcat
2037 (lambda (x)
2038 (format "<span class=\"%s\">%s</span>"
2039 (org-export-html-get-tag-class-name x)
2041 (org-split-string (match-string 1 title) ":")
2042 "&nbsp;")
2043 "</span>"))
2045 t t title)))
2046 (if (> level umax)
2047 (progn
2048 (if (aref org-levels-open (1- level))
2049 (progn
2050 (org-close-li)
2051 (if target
2052 (insert (format "<li id=\"%s\">" target) extra-targets title "<br/>\n")
2053 (insert "<li>" title "<br/>\n")))
2054 (aset org-levels-open (1- level) t)
2055 (org-close-par-maybe)
2056 (if target
2057 (insert (format "<ul>\n<li id=\"%s\">" target)
2058 extra-targets title "<br/>\n")
2059 (insert "<ul>\n<li>" title "<br/>\n"))))
2060 (aset org-levels-open (1- level) t)
2061 (setq snumber (org-section-number level))
2062 (setq level (+ level org-export-html-toplevel-hlevel -1))
2063 (if (and org-export-with-section-numbers (not body-only))
2064 (setq title (concat
2065 (format "<span class=\"section-number-%d\">%s</span>"
2066 level snumber)
2067 " " title)))
2068 (unless (= head-count 1) (insert "\n</div>\n"))
2069 (setq href (cdr (assoc (concat "sec-" snumber) org-export-preferred-target-alist)))
2070 (setq suffix (or href snumber))
2071 (setq href (or href (concat "sec-" snumber)))
2072 (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"
2073 suffix level (if extra-class (concat " " extra-class) "")
2074 level href
2075 extra-targets
2076 title level level suffix))
2077 (org-open-par)))))
2079 (defun org-export-html-get-tag-class-name (tag)
2080 "Turn tag into a valid class name.
2081 Replaces invalid characters with \"_\" and then prepends a prefix."
2082 (save-match-data
2083 (while (string-match "[^a-zA-Z0-9_]" tag)
2084 (setq tag (replace-match "_" t t tag))))
2085 (concat org-export-html-tag-class-prefix tag))
2087 (defun org-export-html-get-todo-kwd-class-name (kwd)
2088 "Turn todo keyword 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_]" kwd)
2092 (setq kwd (replace-match "_" t t kwd))))
2093 (concat org-export-html-todo-kwd-class-prefix kwd))
2095 (defun org-html-level-close (level max-outline-level)
2096 "Terminate one level in HTML export."
2097 (if (<= level max-outline-level)
2098 (insert "</div>\n")
2099 (org-close-li)
2100 (insert "</ul>\n")))
2102 (provide 'org-html)
2104 ;; arch-tag: 8109d84d-eb8f-460b-b1a8-f45f3a6c7ea1
2105 ;;; org-html.el ends here