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