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