file protocol in HTML links
[org-mode.git] / lisp / org-html.el
blob735867f08ac945452a7b3a1af736e18e93793fb8
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.36trans
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 after <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 before </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)
537 ;;; org-html-cvt-link-fn
538 (defconst org-html-cvt-link-fn
540 "Function to convert link URLs to exportable URLs.
541 Takes two arguments, TYPE and PATH.
542 Returns exportable url as (TYPE PATH), or `nil' to signal that it
543 didn't handle this case.
544 Intended to be locally bound around a call to `org-export-as-html'." )
546 (defun org-html-cvt-org-as-html (opt-plist type path)
547 "Convert and org filename to an equivalent html filename.
548 If TYPE is not file, just return `nil'.
549 See variable `org-export-html-link-org-files-as-html'"
551 (save-match-data
552 (and
553 org-export-html-link-org-files-as-html
554 (string= type "file")
555 (string-match "\\.org$" path)
556 (progn
557 (list
558 "http"
559 (concat
560 (substring path 0 (match-beginning 0))
561 "."
562 (plist-get opt-plist :html-extension)))))))
565 ;;; org-html-should-inline-p
566 (defun org-html-should-inline-p (filename descp)
567 "Return non-nil if link FILENAME should be inlined, according to
568 current settings.
569 DESCP is the boolean of whether there was a link description.
570 See variables `org-export-html-inline-images' and
571 `org-export-html-inline-image-extensions'."
572 (declare (special
573 org-export-html-inline-images
574 org-export-html-inline-image-extensions))
575 (or
576 (eq t org-export-html-inline-images)
577 (and
578 org-export-html-inline-images
579 (not descp)))
580 (org-file-image-p
581 filename org-export-html-inline-image-extensions))
583 ;;; org-html-make-link
584 (defun org-html-make-link (opt-plist type path fragment desc attr
585 may-inline-p)
586 "Make an HTML link.
587 OPT-PLIST is an options list.
588 TYPE is the device-type of the link (THIS://foo.html)
589 PATH is the path of the link (http://THIS#locationx)
590 FRAGMENT is the fragment part of the link, if any (foo.html#THIS)
591 DESC is the link description, if any.
592 ATTR is a string of other attributes of the a element.
593 MAY-INLINE-P allows inlining it as an image."
595 (declare (special org-par-open))
596 (save-match-data
597 (let* ((filename path)
598 ;;First pass. Just sanity stuff.
599 (components-1
600 (cond
601 ((string= type "file")
602 (list
603 type
604 ;;Substitute just if original path was absolute.
605 ;;(Otherwise path must remain relative)
606 (if (file-name-absolute-p path)
607 (expand-file-name path)
608 path)))
609 ((string= type "")
610 (list nil path))
611 (t (list type path))))
613 ;;Second pass. Components converted so they can refer
614 ;;to a remote site.
615 (components-2
617 (and org-html-cvt-link-fn
618 (apply org-html-cvt-link-fn
619 opt-plist components-1))
620 (apply #'org-html-cvt-org-as-html
621 opt-plist components-1)
622 components-1))
623 (type (first components-2))
624 (thefile (second components-2)))
627 ;;Third pass. Build final link except for leading type
628 ;;spec.
629 (cond
630 ((or
631 (not type)
632 (string= type "http")
633 (string= type "https"))
634 (if fragment
635 (setq thefile (concat thefile "#" fragment))))
637 (t))
639 ;;Final URL-build, for all types.
640 (setq thefile
641 (let
642 ((str (org-export-html-format-href thefile)))
643 (if (and type (not (string= "file" type))
644 (string-match "^//" str))
645 (concat type ":" str)
646 str)))
648 (if (and
649 may-inline-p
650 ;;Can't inline a URL with a fragment.
651 (not fragment))
652 (progn
653 (message "image %s %s" thefile org-par-open)
654 (org-export-html-format-image thefile org-par-open))
655 (concat
656 "<a href=\"" thefile "\"" attr ">"
657 (org-export-html-format-desc desc)
658 "</a>")))))
660 ;;; org-export-as-html
661 ;;;###autoload
662 (defun org-export-as-html (arg &optional hidden ext-plist
663 to-buffer body-only pub-dir)
664 "Export the outline as a pretty HTML file.
665 If there is an active region, export only the region. The prefix
666 ARG specifies how many levels of the outline should become
667 headlines. The default is 3. Lower levels will become bulleted
668 lists. HIDDEN is obsolete and does nothing.
669 EXT-PLIST is a property list with external parameters overriding
670 org-mode's default settings, but still inferior to file-local
671 settings. When TO-BUFFER is non-nil, create a buffer with that
672 name and export to that buffer. If TO-BUFFER is the symbol
673 `string', don't leave any buffer behind but just return the
674 resulting HTML as a string. When BODY-ONLY is set, don't produce
675 the file header and footer, simply return the content of
676 <body>...</body>, without even the body tags themselves. When
677 PUB-DIR is set, use this as the publishing directory."
678 (interactive "P")
679 (run-hooks 'org-export-first-hook)
681 ;; Make sure we have a file name when we need it.
682 (when (and (not (or to-buffer body-only))
683 (not buffer-file-name))
684 (if (buffer-base-buffer)
685 (org-set-local 'buffer-file-name
686 (with-current-buffer (buffer-base-buffer)
687 buffer-file-name))
688 (error "Need a file name to be able to export")))
690 (message "Exporting...")
691 (setq-default org-todo-line-regexp org-todo-line-regexp)
692 (setq-default org-deadline-line-regexp org-deadline-line-regexp)
693 (setq-default org-done-keywords org-done-keywords)
694 (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp)
695 (let* ((opt-plist
696 (org-export-process-option-filters
697 (org-combine-plists (org-default-export-plist)
698 ext-plist
699 (org-infile-export-plist))))
700 (body-only (or body-only (plist-get opt-plist :body-only)))
701 (style (concat (if (plist-get opt-plist :style-include-default)
702 org-export-html-style-default)
703 (plist-get opt-plist :style)
704 (plist-get opt-plist :style-extra)
705 "\n"
706 (if (plist-get opt-plist :style-include-scripts)
707 org-export-html-scripts)))
708 (html-extension (plist-get opt-plist :html-extension))
709 (link-validate (plist-get opt-plist :link-validation-function))
710 valid thetoc have-headings first-heading-pos
711 (odd org-odd-levels-only)
712 (region-p (org-region-active-p))
713 (rbeg (and region-p (region-beginning)))
714 (rend (and region-p (region-end)))
715 (subtree-p
716 (if (plist-get opt-plist :ignore-subtree-p)
718 (when region-p
719 (save-excursion
720 (goto-char rbeg)
721 (and (org-at-heading-p)
722 (>= (org-end-of-subtree t t) rend))))))
723 (level-offset (if subtree-p
724 (save-excursion
725 (goto-char rbeg)
726 (+ (funcall outline-level)
727 (if org-odd-levels-only 1 0)))
729 (opt-plist (setq org-export-opt-plist
730 (if subtree-p
731 (org-export-add-subtree-options opt-plist rbeg)
732 opt-plist)))
733 ;; The following two are dynamically scoped into other
734 ;; routines below.
735 (org-current-export-dir
736 (or pub-dir (org-export-directory :html opt-plist)))
737 (org-current-export-file buffer-file-name)
738 (level 0) (line "") (origline "") txt todo
739 (umax nil)
740 (umax-toc nil)
741 (filename (if to-buffer nil
742 (expand-file-name
743 (concat
744 (file-name-sans-extension
745 (or (and subtree-p
746 (org-entry-get (region-beginning)
747 "EXPORT_FILE_NAME" t))
748 (file-name-nondirectory buffer-file-name)))
749 "." html-extension)
750 (file-name-as-directory
751 (or pub-dir (org-export-directory :html opt-plist))))))
752 (current-dir (if buffer-file-name
753 (file-name-directory buffer-file-name)
754 default-directory))
755 (buffer (if to-buffer
756 (cond
757 ((eq to-buffer 'string) (get-buffer-create "*Org HTML Export*"))
758 (t (get-buffer-create to-buffer)))
759 (find-file-noselect filename)))
760 (org-levels-open (make-vector org-level-max nil))
761 (date (plist-get opt-plist :date))
762 (author (plist-get opt-plist :author))
763 (title (or (and subtree-p (org-export-get-title-from-subtree))
764 (plist-get opt-plist :title)
765 (and (not body-only)
766 (not
767 (plist-get opt-plist :skip-before-1st-heading))
768 (org-export-grab-title-from-buffer))
769 (and buffer-file-name
770 (file-name-sans-extension
771 (file-name-nondirectory buffer-file-name)))
772 "UNTITLED"))
773 (link-up (and (plist-get opt-plist :link-up)
774 (string-match "\\S-" (plist-get opt-plist :link-up))
775 (plist-get opt-plist :link-up)))
776 (link-home (and (plist-get opt-plist :link-home)
777 (string-match "\\S-" (plist-get opt-plist :link-home))
778 (plist-get opt-plist :link-home)))
779 (dummy (setq opt-plist (plist-put opt-plist :title title)))
780 (html-table-tag (plist-get opt-plist :html-table-tag))
781 (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
782 (quote-re (concat "^\\(\\*+\\)\\([ \t]+" org-quote-string "\\>\\)"))
783 (inquote nil)
784 (infixed nil)
785 (inverse nil)
786 (in-local-list nil)
787 (local-list-type nil)
788 (local-list-indent nil)
789 (llt org-plain-list-ordered-item-terminator)
790 (email (plist-get opt-plist :email))
791 (language (plist-get opt-plist :language))
792 (keywords (plist-get opt-plist :keywords))
793 (description (plist-get opt-plist :description))
794 (lang-words nil)
795 (head-count 0) cnt
796 (start 0)
797 (coding-system (and (boundp 'buffer-file-coding-system)
798 buffer-file-coding-system))
799 (coding-system-for-write (or org-export-html-coding-system
800 coding-system))
801 (save-buffer-coding-system (or org-export-html-coding-system
802 coding-system))
803 (charset (and coding-system-for-write
804 (fboundp 'coding-system-get)
805 (coding-system-get coding-system-for-write
806 'mime-charset)))
807 (region
808 (buffer-substring
809 (if region-p (region-beginning) (point-min))
810 (if region-p (region-end) (point-max))))
811 (lines
812 (org-split-string
813 (org-export-preprocess-string
814 region
815 :emph-multiline t
816 :for-html t
817 :skip-before-1st-heading
818 (plist-get opt-plist :skip-before-1st-heading)
819 :drawers (plist-get opt-plist :drawers)
820 :todo-keywords (plist-get opt-plist :todo-keywords)
821 :tags (plist-get opt-plist :tags)
822 :priority (plist-get opt-plist :priority)
823 :footnotes (plist-get opt-plist :footnotes)
824 :timestamps (plist-get opt-plist :timestamps)
825 :archived-trees
826 (plist-get opt-plist :archived-trees)
827 :select-tags (plist-get opt-plist :select-tags)
828 :exclude-tags (plist-get opt-plist :exclude-tags)
829 :add-text
830 (plist-get opt-plist :text)
831 :LaTeX-fragments
832 (plist-get opt-plist :LaTeX-fragments))
833 "[\r\n]"))
834 table-open type
835 table-buffer table-orig-buffer
836 ind item-type starter didclose
837 rpl path attr desc descp desc1 desc2 link
838 snumber fnc item-tag initial-number
839 footnotes footref-seen
840 id-file href
843 (let ((inhibit-read-only t))
844 (org-unmodified
845 (remove-text-properties (point-min) (point-max)
846 '(:org-license-to-kill t))))
848 (message "Exporting...")
850 (setq org-min-level (org-get-min-level lines level-offset))
851 (setq org-last-level org-min-level)
852 (org-init-section-numbers)
854 (cond
855 ((and date (string-match "%" date))
856 (setq date (format-time-string date)))
857 (date)
858 (t (setq date (format-time-string "%Y-%m-%d %T %Z"))))
860 ;; Get the language-dependent settings
861 (setq lang-words (or (assoc language org-export-language-setup)
862 (assoc "en" org-export-language-setup)))
864 ;; Switch to the output buffer
865 (set-buffer buffer)
866 (let ((inhibit-read-only t)) (erase-buffer))
867 (fundamental-mode)
868 (org-install-letbind)
870 (and (fboundp 'set-buffer-file-coding-system)
871 (set-buffer-file-coding-system coding-system-for-write))
873 (let ((case-fold-search nil)
874 (org-odd-levels-only odd))
875 ;; create local variables for all options, to make sure all called
876 ;; functions get the correct information
877 (mapc (lambda (x)
878 (set (make-local-variable (nth 2 x))
879 (plist-get opt-plist (car x))))
880 org-export-plist-vars)
881 (setq umax (if arg (prefix-numeric-value arg)
882 org-export-headline-levels))
883 (setq umax-toc (if (integerp org-export-with-toc)
884 (min org-export-with-toc umax)
885 umax))
886 (unless body-only
887 ;; File header
888 (insert (format
890 <!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
891 \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
892 <html xmlns=\"http://www.w3.org/1999/xhtml\"
893 lang=\"%s\" xml:lang=\"%s\">
894 <head>
895 <title>%s</title>
896 <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>
897 <meta name=\"generator\" content=\"Org-mode\"/>
898 <meta name=\"generated\" content=\"%s\"/>
899 <meta name=\"author\" content=\"%s\"/>
900 <meta name=\"description\" content=\"%s\"/>
901 <meta name=\"keywords\" content=\"%s\"/>
903 </head>
904 <body>
905 <div id=\"content\">
908 (format
909 (or (and (stringp org-export-html-xml-declaration)
910 org-export-html-xml-declaration)
911 (cdr (assoc html-extension org-export-html-xml-declaration))
912 (cdr (assoc "html" org-export-html-xml-declaration))
915 (or charset "iso-8859-1"))
916 language language
917 title
918 (or charset "iso-8859-1")
919 date author description keywords
920 style
921 (if (or link-up link-home)
922 (concat
923 (format org-export-html-home/up-format
924 (or link-up link-home)
925 (or link-home link-up))
926 "\n")
927 "")))
929 (org-export-html-insert-plist-item opt-plist :preamble opt-plist)
931 (when (plist-get opt-plist :auto-preamble)
932 (if title (insert (format org-export-html-title-format
933 (org-html-expand title))))))
935 (if (and org-export-with-toc (not body-only))
936 (progn
937 (push (format "<h%d>%s</h%d>\n"
938 org-export-html-toplevel-hlevel
939 (nth 3 lang-words)
940 org-export-html-toplevel-hlevel)
941 thetoc)
942 (push "<div id=\"text-table-of-contents\">\n" thetoc)
943 (push "<ul>\n<li>" thetoc)
944 (setq lines
945 (mapcar '(lambda (line)
946 (if (and (string-match org-todo-line-regexp line)
947 (not (get-text-property 0 'org-protected line)))
948 ;; This is a headline
949 (progn
950 (setq have-headings t)
951 (setq level (- (match-end 1) (match-beginning 1)
952 level-offset)
953 level (org-tr-level level)
954 txt (save-match-data
955 (org-html-expand
956 (org-export-cleanup-toc-line
957 (match-string 3 line))))
958 todo
959 (or (and org-export-mark-todo-in-toc
960 (match-beginning 2)
961 (not (member (match-string 2 line)
962 org-done-keywords)))
963 ; TODO, not DONE
964 (and org-export-mark-todo-in-toc
965 (= level umax-toc)
966 (org-search-todo-below
967 line lines level))))
968 (if (string-match
969 (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") txt)
970 (setq txt (replace-match "&nbsp;&nbsp;&nbsp;<span class=\"tag\"> \\1</span>" t nil txt)))
971 (if (string-match quote-re0 txt)
972 (setq txt (replace-match "" t t txt)))
973 (setq snumber (org-section-number level))
974 (if org-export-with-section-numbers
975 (setq txt (concat snumber " " txt)))
976 (if (<= level (max umax umax-toc))
977 (setq head-count (+ head-count 1)))
978 (if (<= level umax-toc)
979 (progn
980 (if (> level org-last-level)
981 (progn
982 (setq cnt (- level org-last-level))
983 (while (>= (setq cnt (1- cnt)) 0)
984 (push "\n<ul>\n<li>" thetoc))
985 (push "\n" thetoc)))
986 (if (< level org-last-level)
987 (progn
988 (setq cnt (- org-last-level level))
989 (while (>= (setq cnt (1- cnt)) 0)
990 (push "</li>\n</ul>" thetoc))
991 (push "\n" thetoc)))
992 ;; Check for targets
993 (while (string-match org-any-target-regexp line)
994 (setq line (replace-match
995 (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
996 t t line)))
997 (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
998 (setq txt (replace-match "" t t txt)))
999 (setq href
1000 (replace-regexp-in-string
1001 "\\." "_" (format "sec-%s" snumber)))
1002 (setq href (or (cdr (assoc href org-export-preferred-target-alist)) href))
1003 (push
1004 (format
1005 (if todo
1006 "</li>\n<li><a href=\"#%s\"><span class=\"todo\">%s</span></a>"
1007 "</li>\n<li><a href=\"#%s\">%s</a>")
1008 href txt) thetoc)
1010 (setq org-last-level level))
1012 line)
1013 lines))
1014 (while (> org-last-level (1- org-min-level))
1015 (setq org-last-level (1- org-last-level))
1016 (push "</li>\n</ul>\n" thetoc))
1017 (push "</div>\n" thetoc)
1018 (setq thetoc (if have-headings (nreverse thetoc) nil))))
1020 (setq head-count 0)
1021 (org-init-section-numbers)
1023 (org-open-par)
1025 (while (setq line (pop lines) origline line)
1026 (catch 'nextline
1028 ;; end of quote section?
1029 (when (and inquote (string-match "^\\*+ " line))
1030 (insert "</pre>\n")
1031 (org-open-par)
1032 (setq inquote nil))
1033 ;; inside a quote section?
1034 (when inquote
1035 (insert (org-html-protect line) "\n")
1036 (throw 'nextline nil))
1038 ;; Fixed-width, verbatim lines (examples)
1039 (when (and org-export-with-fixed-width
1040 (string-match "^[ \t]*:\\(\\([ \t]\\|$\\)\\(.*\\)\\)" line))
1041 (when (not infixed)
1042 (setq infixed t)
1043 (org-close-par-maybe)
1045 (insert "<pre class=\"example\">\n"))
1046 (insert (org-html-protect (match-string 3 line)) "\n")
1047 (when (or (not lines)
1048 (not (string-match "^[ \t]*:\\(\\([ \t]\\|$\\)\\(.*\\)\\)"
1049 (car lines))))
1050 (setq infixed nil)
1051 (insert "</pre>\n")
1052 (org-open-par))
1053 (throw 'nextline nil))
1055 (org-export-html-close-lists-maybe line)
1057 ;; Protected HTML
1058 (when (get-text-property 0 'org-protected line)
1059 (let (par (ind (get-text-property 0 'original-indentation line)))
1060 (when (re-search-backward
1061 "\\(<p>\\)\\([ \t\r\n]*\\)\\=" (- (point) 100) t)
1062 (setq par (match-string 1))
1063 (replace-match "\\2\n"))
1064 (insert line "\n")
1065 (while (and lines
1066 (or (= (length (car lines)) 0)
1067 (not ind)
1068 (equal ind (get-text-property 0 'original-indentation (car lines))))
1069 (or (= (length (car lines)) 0)
1070 (get-text-property 0 'org-protected (car lines))))
1071 (insert (pop lines) "\n"))
1072 (and par (insert "<p>\n")))
1073 (throw 'nextline nil))
1075 ;; Blockquotes, verse, and center
1076 (when (equal "ORG-BLOCKQUOTE-START" line)
1077 (org-close-par-maybe)
1078 (insert "<blockquote>\n")
1079 (org-open-par)
1080 (throw 'nextline nil))
1081 (when (equal "ORG-BLOCKQUOTE-END" line)
1082 (org-close-par-maybe)
1083 (insert "\n</blockquote>\n")
1084 (org-open-par)
1085 (throw 'nextline nil))
1086 (when (equal "ORG-VERSE-START" line)
1087 (org-close-par-maybe)
1088 (insert "\n<p class=\"verse\">\n")
1089 (setq org-par-open t)
1090 (setq inverse t)
1091 (throw 'nextline nil))
1092 (when (equal "ORG-VERSE-END" line)
1093 (insert "</p>\n")
1094 (setq org-par-open nil)
1095 (org-open-par)
1096 (setq inverse nil)
1097 (throw 'nextline nil))
1098 (when (equal "ORG-CENTER-START" line)
1099 (org-close-par-maybe)
1100 (insert "\n<div style=\"text-align: center\">")
1101 (org-open-par)
1102 (throw 'nextline nil))
1103 (when (equal "ORG-CENTER-END" line)
1104 (org-close-par-maybe)
1105 (insert "\n</div>")
1106 (org-open-par)
1107 (throw 'nextline nil))
1108 (run-hooks 'org-export-html-after-blockquotes-hook)
1109 (when inverse
1110 (let ((i (org-get-string-indentation line)))
1111 (if (> i 0)
1112 (setq line (concat (mapconcat 'identity
1113 (make-list (* 2 i) "\\nbsp") "")
1114 " " (org-trim line))))
1115 (unless (string-match "\\\\\\\\[ \t]*$" line)
1116 (setq line (concat line "\\\\")))))
1118 ;; make targets to anchors
1119 (setq start 0)
1120 (while (string-match
1121 "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line start)
1122 (cond
1123 ((get-text-property (match-beginning 1) 'org-protected line)
1124 (setq start (match-end 1)))
1125 ((match-end 2)
1126 (setq line (replace-match
1127 (format
1128 "@<a name=\"%s\" id=\"%s\">@</a>"
1129 (org-solidify-link-text (match-string 1 line))
1130 (org-solidify-link-text (match-string 1 line)))
1131 t t line)))
1132 ((and org-export-with-toc (equal (string-to-char line) ?*))
1133 ;; FIXME: NOT DEPENDENT on TOC?????????????????????
1134 (setq line (replace-match
1135 (concat "@<span class=\"target\">"
1136 (match-string 1 line) "@</span> ")
1137 ;; (concat "@<i>" (match-string 1 line) "@</i> ")
1138 t t line)))
1140 (setq line (replace-match
1141 (concat "@<a name=\""
1142 (org-solidify-link-text (match-string 1 line))
1143 "\" class=\"target\">" (match-string 1 line)
1144 "@</a> ")
1145 t t line)))))
1147 (setq line (org-html-handle-time-stamps line))
1149 ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
1150 ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
1151 ;; Also handle sub_superscripts and checkboxes
1152 (or (string-match org-table-hline-regexp line)
1153 (setq line (org-html-expand line)))
1155 ;; Format the links
1156 (setq start 0)
1157 (while (string-match org-bracket-link-analytic-regexp++ line start)
1158 (setq start (match-beginning 0))
1159 (setq path (save-match-data (org-link-unescape
1160 (match-string 3 line))))
1161 (setq type (cond
1162 ((match-end 2) (match-string 2 line))
1163 ((save-match-data
1164 (or (file-name-absolute-p path)
1165 (string-match "^\\.\\.?/" path)))
1166 "file")
1167 (t "internal")))
1168 (setq path (org-extract-attributes (org-link-unescape path)))
1169 (setq attr (get-text-property 0 'org-attributes path))
1170 (setq desc1 (if (match-end 5) (match-string 5 line))
1171 desc2 (if (match-end 2) (concat type ":" path) path)
1172 descp (and desc1 (not (equal desc1 desc2)))
1173 desc (or desc1 desc2))
1174 (cond
1175 ((equal type "internal")
1176 (let
1177 ((frag-0
1178 (if (= (string-to-char path) ?#)
1179 (substring path 1)
1180 path)))
1181 (setq rpl
1182 (org-html-make-link
1183 opt-plist
1186 (org-solidify-link-text
1187 (save-match-data (org-link-unescape frag-0))
1188 nil)
1189 desc attr nil))))
1190 ((and (equal type "id")
1191 (setq id-file (org-id-find-id-file path)))
1192 ;; This is an id: link to another file (if it was the same file,
1193 ;; it would have become an internal link...)
1194 (save-match-data
1195 (setq id-file (file-relative-name
1196 id-file
1197 (file-name-directory org-current-export-file)))
1198 (setq rpl
1199 (org-html-make-link opt-plist
1200 "file" id-file
1201 (concat (if (org-uuidgen-p path) "ID-") path)
1202 desc
1203 attr
1204 nil))))
1205 ((member type '("http" "https"))
1206 ;; standard URL, can inline as image
1207 (setq rpl
1208 (org-html-make-link opt-plist
1209 type path nil
1210 desc
1211 attr
1212 (org-html-should-inline-p path descp))))
1213 ((member type '("ftp" "mailto" "news"))
1214 ;; standard URL, can't inline as image
1215 (setq rpl
1216 (org-html-make-link opt-plist
1217 type path nil
1218 desc
1219 attr
1220 nil)))
1222 ((string= type "coderef")
1223 (let*
1224 ((coderef-str (format "coderef-%s" path))
1225 (attr-1
1226 (format "class=\"coderef\" onmouseover=\"CodeHighlightOn(this, '%s');\" onmouseout=\"CodeHighlightOff(this, '%s');\""
1227 coderef-str coderef-str)))
1228 (setq rpl
1229 (org-html-make-link opt-plist
1230 type "" coderef-str
1231 (format
1232 (org-export-get-coderef-format
1233 path
1234 (and descp desc))
1235 (cdr (assoc path org-export-code-refs)))
1236 attr-1
1237 nil))))
1239 ((functionp (setq fnc (nth 2 (assoc type org-link-protocols))))
1240 ;; The link protocol has a function for format the link
1241 (setq rpl
1242 (save-match-data
1243 (funcall fnc (org-link-unescape path) desc1 'html))))
1245 ((string= type "file")
1246 ;; FILE link
1247 (save-match-data
1248 (let*
1249 ((components
1251 (string-match "::\\(.*\\)" path)
1252 (list
1253 (replace-match "" t nil path)
1254 (match-string 1 path))
1255 (list path nil)))
1257 ;;The proper path, without a fragment
1258 (path-1
1259 (first components))
1261 ;;The raw fragment
1262 (fragment-0
1263 (second components))
1265 ;;Check the fragment. If it can't be used as
1266 ;;target fragment we'll pass nil instead.
1267 (fragment-1
1269 (and fragment-0
1270 (not (string-match "^[0-9]*$" fragment-0))
1271 (not (string-match "^\\*" fragment-0))
1272 (not (string-match "^/.*/$" fragment-0)))
1273 (org-solidify-link-text
1274 (org-link-unescape fragment-0))
1275 nil))
1276 (desc-2
1277 ;;Description minus "file:" and ".org"
1278 (if (string-match "^file:" desc)
1279 (let
1280 ((desc-1 (replace-match "" t t desc)))
1281 (if (string-match "\\.org$" desc-1)
1282 (replace-match "" t t desc-1)
1283 desc-1))
1284 desc)))
1286 (setq rpl
1288 (and
1289 (functionp link-validate)
1290 (not (funcall link-validate path-1 current-dir)))
1291 desc
1292 (org-html-make-link opt-plist
1293 "file" path-1 fragment-1 desc-2 attr
1294 (org-html-should-inline-p path-1 descp)))))))
1297 ;; just publish the path, as default
1298 (setq rpl (concat "<i>&lt;" type ":"
1299 (save-match-data (org-link-unescape path))
1300 "&gt;</i>"))))
1301 (setq line (replace-match rpl t t line)
1302 start (+ start (length rpl))))
1304 ;; TODO items
1305 (if (and (string-match org-todo-line-regexp line)
1306 (match-beginning 2))
1308 (setq line
1309 (concat (substring line 0 (match-beginning 2))
1310 "<span class=\""
1311 (if (member (match-string 2 line)
1312 org-done-keywords)
1313 "done" "todo")
1314 " " (match-string 2 line)
1315 "\"> " (org-export-html-get-todo-kwd-class-name
1316 (match-string 2 line))
1317 "</span>" (substring line (match-end 2)))))
1319 ;; Does this contain a reference to a footnote?
1320 (when org-export-with-footnotes
1321 (setq start 0)
1322 (while (string-match "\\([^* \t].*?\\)\\[\\([0-9]+\\)\\]" line start)
1323 (if (get-text-property (match-beginning 2) 'org-protected line)
1324 (setq start (match-end 2))
1325 (let ((n (match-string 2 line)) extra a)
1326 (if (setq a (assoc n footref-seen))
1327 (progn
1328 (setcdr a (1+ (cdr a)))
1329 (setq extra (format ".%d" (cdr a))))
1330 (setq extra "")
1331 (push (cons n 1) footref-seen))
1332 (setq line
1333 (replace-match
1334 (format
1335 (concat "%s"
1336 (format org-export-html-footnote-format
1337 "<a class=\"footref\" name=\"fnr.%s%s\" href=\"#fn.%s\">%s</a>"))
1338 (or (match-string 1 line) "") n extra n n)
1339 t t line))))))
1341 (cond
1342 ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
1343 ;; This is a headline
1344 (setq level (org-tr-level (- (match-end 1) (match-beginning 1)
1345 level-offset))
1346 txt (match-string 2 line))
1347 (if (string-match quote-re0 txt)
1348 (setq txt (replace-match "" t t txt)))
1349 (if (<= level (max umax umax-toc))
1350 (setq head-count (+ head-count 1)))
1351 (when in-local-list
1352 ;; Close any local lists before inserting a new header line
1353 (while local-list-type
1354 (org-close-li (car local-list-type))
1355 (insert (format "</%sl>\n" (car local-list-type)))
1356 (pop local-list-type))
1357 (setq local-list-indent nil
1358 in-local-list nil))
1359 (setq first-heading-pos (or first-heading-pos (point)))
1360 (org-html-level-start level txt umax
1361 (and org-export-with-toc (<= level umax))
1362 head-count)
1364 ;; QUOTES
1365 (when (string-match quote-re line)
1366 (org-close-par-maybe)
1367 (insert "<pre>")
1368 (setq inquote t)))
1370 ((string-match "^[ \t]*- __+[ \t]*$" line)
1371 ;; Explicit list closure
1372 (when local-list-type
1373 (let ((ind (org-get-indentation line)))
1374 (while (and local-list-indent
1375 (<= ind (car local-list-indent)))
1376 (org-close-li (car local-list-type))
1377 (insert (format "</%sl>\n" (car local-list-type)))
1378 (pop local-list-type)
1379 (pop local-list-indent))
1380 (or local-list-indent (setq in-local-list nil))))
1381 (throw 'nextline nil))
1383 ((and org-export-with-tables
1384 (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
1385 (when (not table-open)
1386 ;; New table starts
1387 (setq table-open t table-buffer nil table-orig-buffer nil))
1389 ;; Accumulate lines
1390 (setq table-buffer (cons line table-buffer)
1391 table-orig-buffer (cons origline table-orig-buffer))
1392 (when (or (not lines)
1393 (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
1394 (car lines))))
1395 (setq table-open nil
1396 table-buffer (nreverse table-buffer)
1397 table-orig-buffer (nreverse table-orig-buffer))
1398 (org-close-par-maybe)
1399 (insert (org-format-table-html table-buffer table-orig-buffer))))
1401 ;; Normal lines
1402 (when (string-match
1403 (cond
1404 ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
1405 ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
1406 ((= llt ?\)) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
1407 (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
1408 line)
1409 (setq ind (or (get-text-property 0 'original-indentation line)
1410 (org-get-string-indentation line))
1411 item-type (if (match-beginning 4) "o" "u")
1412 starter (if (match-beginning 2)
1413 (substring (match-string 2 line) 0 -1))
1414 line (substring line (match-beginning 5))
1415 initial-number nil
1416 item-tag nil)
1417 (if (string-match "\\`\\[@start:\\([0-9]+\\)\\][ \t]?" line)
1418 (setq initial-number (match-string 1 line)
1419 line (replace-match "" t t line)))
1420 (if (and starter (string-match "\\(.*?\\) ::[ \t]*" line))
1421 (setq item-type "d"
1422 item-tag (match-string 1 line)
1423 line (substring line (match-end 0))))
1424 (when (and (not (equal item-type "d"))
1425 (not (string-match "[^ \t]" line)))
1426 ;; empty line. Pretend indentation is large.
1427 (setq ind (if org-empty-line-terminates-plain-lists
1429 (1+ (or (car local-list-indent) 1)))))
1430 (setq didclose nil)
1431 (while (and in-local-list
1432 (or (and (= ind (car local-list-indent))
1433 (not starter))
1434 (< ind (car local-list-indent))))
1435 (setq didclose t)
1436 (org-close-li (car local-list-type))
1437 (insert (format "</%sl>\n" (car local-list-type)))
1438 (pop local-list-type) (pop local-list-indent)
1439 (setq in-local-list local-list-indent))
1440 (cond
1441 ((and starter
1442 (or (not in-local-list)
1443 (> ind (car local-list-indent))))
1444 ;; check for a specified start number
1445 ;; Start new (level of) list
1446 (org-close-par-maybe)
1447 (insert (cond
1448 ((equal item-type "u") "<ul>\n<li>\n")
1449 ((equal item-type "o")
1450 (if initial-number
1451 (format "<ol start=%s>\n<li>\n" initial-number)
1452 "<ol>\n<li>\n"))
1453 ((equal item-type "d")
1454 (format "<dl>\n<dt>%s</dt><dd>\n" item-tag))))
1455 (push item-type local-list-type)
1456 (push ind local-list-indent)
1457 (setq in-local-list t))
1458 (starter
1459 ;; continue current list
1460 (org-close-li (car local-list-type))
1461 (insert (cond
1462 ((equal (car local-list-type) "d")
1463 (format "<dt>%s</dt><dd>\n" (or item-tag "???")))
1464 (t "<li>\n"))))
1465 (didclose
1466 ;; we did close a list, normal text follows: need <p>
1467 (org-open-par)))
1468 (if (string-match "^[ \t]*\\[\\([X ]\\)\\]" line)
1469 (setq line
1470 (replace-match
1471 (if (equal (match-string 1 line) "X")
1472 "<b>[X]</b>"
1473 "<b>[<span style=\"visibility:hidden;\">X</span>]</b>")
1474 t t line))))
1476 ;; Horizontal line
1477 (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line)
1478 (if org-par-open
1479 (insert "\n</p>\n<hr/>\n<p>\n")
1480 (insert "\n<hr/>\n"))
1481 (throw 'nextline nil))
1483 ;; Empty lines start a new paragraph. If hand-formatted lists
1484 ;; are not fully interpreted, lines starting with "-", "+", "*"
1485 ;; also start a new paragraph.
1486 (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (org-open-par))
1488 ;; Is this the start of a footnote?
1489 (when org-export-with-footnotes
1490 (when (and (boundp 'footnote-section-tag-regexp)
1491 (string-match (concat "^" footnote-section-tag-regexp)
1492 line))
1493 ;; ignore this line
1494 (throw 'nextline nil))
1495 (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
1496 (org-close-par-maybe)
1497 (let ((n (match-string 1 line)))
1498 (setq org-par-open t
1499 line (replace-match
1500 (format
1501 (concat "<p class=\"footnote\">"
1502 (format org-export-html-footnote-format
1503 "<a class=\"footnum\" name=\"fn.%s\" href=\"#fnr.%s\">%s</a>"))
1504 n n n) t t line)))))
1505 ;; Check if the line break needs to be conserved
1506 (cond
1507 ((string-match "\\\\\\\\[ \t]*$" line)
1508 (setq line (replace-match "<br/>" t t line)))
1509 (org-export-preserve-breaks
1510 (setq line (concat line "<br/>"))))
1512 ;; Check if a paragraph should be started
1513 (let ((start 0))
1514 (while (and org-par-open
1515 (string-match "\\\\par\\>" line start))
1516 ;; Leave a space in the </p> so that the footnote matcher
1517 ;; does not see this.
1518 (if (not (get-text-property (match-beginning 0)
1519 'org-protected line))
1520 (setq line (replace-match "</p ><p >" t t line)))
1521 (setq start (match-end 0))))
1523 (insert line "\n")))))
1525 ;; Properly close all local lists and other lists
1526 (when inquote
1527 (insert "</pre>\n")
1528 (org-open-par))
1529 (when in-local-list
1530 ;; Close any local lists before inserting a new header line
1531 (while local-list-type
1532 (org-close-li (car local-list-type))
1533 (insert (format "</%sl>\n" (car local-list-type)))
1534 (pop local-list-type))
1535 (setq local-list-indent nil
1536 in-local-list nil))
1537 (org-html-level-start 1 nil umax
1538 (and org-export-with-toc (<= level umax))
1539 head-count)
1540 ;; the </div> to close the last text-... div.
1541 (when (and (> umax 0) first-heading-pos) (insert "</div>\n"))
1543 (save-excursion
1544 (goto-char (point-min))
1545 (while (re-search-forward "<p class=\"footnote\">[^\000]*?\\(</p>\\|\\'\\)" nil t)
1546 (push (match-string 0) footnotes)
1547 (replace-match "" t t)))
1548 (when footnotes
1549 (insert (format org-export-html-footnotes-section
1550 (nth 4 lang-words)
1551 (mapconcat 'identity (nreverse footnotes) "\n"))
1552 "\n"))
1553 (let ((bib (org-export-html-get-bibliography)))
1554 (when bib
1555 (insert "\n" bib "\n")))
1556 (unless body-only
1557 (when (plist-get opt-plist :auto-postamble)
1558 (insert "<div id=\"postamble\">\n")
1559 (when (and org-export-author-info author)
1560 (insert "<p class=\"author\"> "
1561 (nth 1 lang-words) ": " author "\n")
1562 (when (and org-export-email-info email (string-match "\\S-" email))
1563 (if (listp (split-string email ",+ *"))
1564 (mapc (lambda(e)
1565 (insert "<a href=\"mailto:" e "\">&lt;"
1566 e "&gt;</a>\n"))
1567 (split-string email ",+ *"))
1568 (insert "<a href=\"mailto:" email "\">&lt;"
1569 email "&gt;</a>\n")))
1570 (insert "</p>\n"))
1571 (when (and date org-export-time-stamp-file)
1572 (insert "<p class=\"date\"> "
1573 (nth 2 lang-words) ": "
1574 date "</p>\n"))
1575 (when org-export-creator-info
1576 (insert (format "<p class=\"creator\">HTML generated by org-mode %s in emacs %s</p>\n"
1577 org-version emacs-major-version)))
1578 (when org-export-html-validation-link
1579 (insert org-export-html-validation-link "\n"))
1580 (insert "</div>"))
1582 (if org-export-html-with-timestamp
1583 (insert org-export-html-html-helper-timestamp))
1584 (org-export-html-insert-plist-item opt-plist :postamble opt-plist)
1585 (insert "\n</div>\n</body>\n</html>\n"))
1587 (unless (plist-get opt-plist :buffer-will-be-killed)
1588 (normal-mode)
1589 (if (eq major-mode (default-value 'major-mode))
1590 (html-mode)))
1592 ;; insert the table of contents
1593 (goto-char (point-min))
1594 (when thetoc
1595 (if (or (re-search-forward
1596 "<p>\\s-*\\[TABLE-OF-CONTENTS\\]\\s-*</p>" nil t)
1597 (re-search-forward
1598 "\\[TABLE-OF-CONTENTS\\]" nil t))
1599 (progn
1600 (goto-char (match-beginning 0))
1601 (replace-match ""))
1602 (goto-char first-heading-pos)
1603 (when (looking-at "\\s-*</p>")
1604 (goto-char (match-end 0))
1605 (insert "\n")))
1606 (insert "<div id=\"table-of-contents\">\n")
1607 (mapc 'insert thetoc)
1608 (insert "</div>\n"))
1609 ;; remove empty paragraphs and lists
1610 (goto-char (point-min))
1611 (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t)
1612 (replace-match ""))
1613 (goto-char (point-min))
1614 (while (re-search-forward "<li>[ \r\n\t]*</li>\n?" nil t)
1615 (replace-match ""))
1616 (goto-char (point-min))
1617 (while (re-search-forward "</ul>\\s-*<ul>\n?" nil t)
1618 (replace-match ""))
1619 ;; Convert whitespace place holders
1620 (goto-char (point-min))
1621 (let (beg end n)
1622 (while (setq beg (next-single-property-change (point) 'org-whitespace))
1623 (setq n (get-text-property beg 'org-whitespace)
1624 end (next-single-property-change beg 'org-whitespace))
1625 (goto-char beg)
1626 (delete-region beg end)
1627 (insert (format "<span style=\"visibility:hidden;\">%s</span>"
1628 (make-string n ?x)))))
1629 ;; Remove empty lines at the beginning of the file.
1630 (goto-char (point-min))
1631 (when (looking-at "\\s-+\n") (replace-match ""))
1632 ;; Remove display properties
1633 (remove-text-properties (point-min) (point-max) '(display t))
1634 ;; Run the hook
1635 (run-hooks 'org-export-html-final-hook)
1636 (or to-buffer (save-buffer))
1637 (goto-char (point-min))
1638 (or (org-export-push-to-kill-ring "HTML")
1639 (message "Exporting... done"))
1640 (if (eq to-buffer 'string)
1641 (prog1 (buffer-substring (point-min) (point-max))
1642 (kill-buffer (current-buffer)))
1643 (current-buffer)))))
1645 (defun org-export-html-insert-plist-item (plist key &rest args)
1646 (let ((item (plist-get plist key)))
1647 (cond ((functionp item)
1648 (apply item args))
1649 (item
1650 (insert item)))))
1652 (defun org-export-html-format-href (s)
1653 "Make sure the S is valid as a href reference in an XHTML document."
1654 (save-match-data
1655 (let ((start 0))
1656 (while (string-match "&" s start)
1657 (setq start (+ (match-beginning 0) 3)
1658 s (replace-match "&amp;" t t s)))))
1661 (defun org-export-html-format-desc (s)
1662 "Make sure the S is valid as a description in a link."
1663 (if (and s (not (get-text-property 1 'org-protected s)))
1664 (save-match-data
1665 (org-html-do-expand s))
1668 (defun org-export-html-format-image (src par-open)
1669 "Create image tag with source and attributes."
1670 (save-match-data
1671 (if (string-match "^ltxpng/" src)
1672 (format "<img src=\"%s\" alt=\"%s\"/>"
1673 src (org-find-text-property-in-string 'org-latex-src src))
1674 (let* ((caption (org-find-text-property-in-string 'org-caption src))
1675 (attr (org-find-text-property-in-string 'org-attributes src))
1676 (label (org-find-text-property-in-string 'org-label src)))
1677 (setq caption (and caption (org-html-do-expand caption)))
1678 (concat
1679 (if caption
1680 (format "%s<div %sclass=\"figure\">
1681 <p>"
1682 (if org-par-open "</p>\n" "")
1683 (if label (format "id=\"%s\" " label) "")))
1684 (format "<img src=\"%s\"%s />"
1686 (if (string-match "\\<alt=" (or attr ""))
1687 (concat " " attr )
1688 (concat " " attr " alt=\"" src "\"")))
1689 (if caption
1690 (format "</p>%s
1691 </div>%s"
1692 (concat "\n<p>" caption "</p>")
1693 (if org-par-open "\n<p>" ""))))))))
1695 (defun org-export-html-get-bibliography ()
1696 "Find bibliography, cut it out and return it."
1697 (catch 'exit
1698 (let (beg end (cnt 1) bib)
1699 (save-excursion
1700 (goto-char (point-min))
1701 (when (re-search-forward "^[ \t]*<div \\(id\\|class\\)=\"bibliography\"" nil t)
1702 (setq beg (match-beginning 0))
1703 (while (re-search-forward "</?div\\>" nil t)
1704 (setq cnt (+ cnt (if (string= (match-string 0) "<div") +1 -1)))
1705 (when (= cnt 0)
1706 (and (looking-at ">") (forward-char 1))
1707 (setq bib (buffer-substring beg (point)))
1708 (delete-region beg (point))
1709 (throw 'exit bib))))
1710 nil))))
1712 (defvar org-table-number-regexp) ; defined in org-table.el
1713 (defun org-format-table-html (lines olines)
1714 "Find out which HTML converter to use and return the HTML code."
1715 (if (stringp lines)
1716 (setq lines (org-split-string lines "\n")))
1717 (if (string-match "^[ \t]*|" (car lines))
1718 ;; A normal org table
1719 (org-format-org-table-html lines)
1720 ;; Table made by table.el - test for spanning
1721 (let* ((hlines (delq nil (mapcar
1722 (lambda (x)
1723 (if (string-match "^[ \t]*\\+-" x) x
1724 nil))
1725 lines)))
1726 (first (car hlines))
1727 (ll (and (string-match "\\S-+" first)
1728 (match-string 0 first)))
1729 (re (concat "^[ \t]*" (regexp-quote ll)))
1730 (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
1731 hlines))))
1732 (if (and (not spanning)
1733 (not org-export-prefer-native-exporter-for-tables))
1734 ;; We can use my own converter with HTML conversions
1735 (org-format-table-table-html lines)
1736 ;; Need to use the code generator in table.el, with the original text.
1737 (org-format-table-table-html-using-table-generate-source olines)))))
1739 (defvar org-table-number-fraction) ; defined in org-table.el
1740 (defun org-format-org-table-html (lines &optional splice)
1741 "Format a table into HTML."
1742 (require 'org-table)
1743 ;; Get rid of hlines at beginning and end
1744 (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
1745 (setq lines (nreverse lines))
1746 (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
1747 (setq lines (nreverse lines))
1748 (when org-export-table-remove-special-lines
1749 ;; Check if the table has a marking column. If yes remove the
1750 ;; column and the special lines
1751 (setq lines (org-table-clean-before-export lines)))
1753 (let* ((caption (org-find-text-property-in-string 'org-caption (car lines)))
1754 (label (org-find-text-property-in-string 'org-label (car lines)))
1755 (attributes (org-find-text-property-in-string 'org-attributes
1756 (car lines)))
1757 (html-table-tag (org-export-splice-attributes
1758 html-table-tag attributes))
1759 (head (and org-export-highlight-first-table-line
1760 (delq nil (mapcar
1761 (lambda (x) (string-match "^[ \t]*|-" x))
1762 (cdr lines)))))
1764 (nline 0) fnum nfields i
1765 tbopen line fields html gr colgropen rowstart rowend)
1766 (setq caption (and caption (org-html-do-expand caption)))
1767 (if splice (setq head nil))
1768 (unless splice (push (if head "<thead>" "<tbody>") html))
1769 (setq tbopen t)
1770 (while (setq line (pop lines))
1771 (catch 'next-line
1772 (if (string-match "^[ \t]*|-" line)
1773 (progn
1774 (unless splice
1775 (push (if head "</thead>" "</tbody>") html)
1776 (if lines (push "<tbody>" html) (setq tbopen nil)))
1777 (setq head nil) ;; head ends here, first time around
1778 ;; ignore this line
1779 (throw 'next-line t)))
1780 ;; Break the line into fields
1781 (setq fields (org-split-string line "[ \t]*|[ \t]*"))
1782 (unless fnum (setq fnum (make-vector (length fields) 0)
1783 nfields (length fnum)))
1784 (setq nline (1+ nline) i -1
1785 rowstart (eval (car org-export-table-row-tags))
1786 rowend (eval (cdr org-export-table-row-tags)))
1787 (push (concat rowstart
1788 (mapconcat
1789 (lambda (x)
1790 (setq i (1+ i))
1791 (if (and (< i nfields) ; make sure no rogue line causes an error here
1792 (string-match org-table-number-regexp x))
1793 (incf (aref fnum i)))
1794 (cond
1795 (head
1796 (concat
1797 (format (car org-export-table-header-tags) "col")
1799 (cdr org-export-table-header-tags)))
1800 ((and (= i 0) org-export-html-table-use-header-tags-for-first-column)
1801 (concat
1802 (format (car org-export-table-header-tags) "row")
1804 (cdr org-export-table-header-tags)))
1806 (concat (car org-export-table-data-tags) x
1807 (cdr org-export-table-data-tags)))))
1808 fields "")
1809 rowend)
1810 html)))
1811 (unless splice (if tbopen (push "</tbody>" html)))
1812 (unless splice (push "</table>\n" html))
1813 (setq html (nreverse html))
1814 (unless splice
1815 ;; Put in col tags with the alignment (unfortunately often ignored...)
1816 (unless (car org-table-colgroup-info)
1817 (setq org-table-colgroup-info
1818 (cons :start (cdr org-table-colgroup-info))))
1819 (push (mapconcat
1820 (lambda (x)
1821 (setq gr (pop org-table-colgroup-info))
1822 (format "%s<col align=\"%s\" />%s"
1823 (if (memq gr '(:start :startend))
1824 (prog1
1825 (if colgropen "</colgroup>\n<colgroup>" "<colgroup>")
1826 (setq colgropen t))
1828 (if (> (/ (float x) nline) org-table-number-fraction)
1829 "right" "left")
1830 (if (memq gr '(:end :startend))
1831 (progn (setq colgropen nil) "</colgroup>")
1832 "")))
1833 fnum "")
1834 html)
1835 (if colgropen (setq html (cons (car html) (cons "</colgroup>" (cdr html)))))
1836 ;; Since the output of HTML table formatter can also be used in
1837 ;; DocBook document, we want to always include the caption to make
1838 ;; DocBook XML file valid.
1839 (push (format "<caption>%s</caption>" (or caption "")) html)
1840 (when label (push (format "<a name=\"%s\" id=\"%s\"></a>" label label)
1841 html))
1842 (push html-table-tag html))
1843 (concat (mapconcat 'identity html "\n") "\n")))
1845 (defun org-export-splice-attributes (tag attributes)
1846 "Read attributes in string ATTRIBUTES, add and replace in HTML tag TAG."
1847 (if (not attributes)
1849 (let (oldatt newatt)
1850 (setq oldatt (org-extract-attributes-from-string tag)
1851 tag (pop oldatt)
1852 newatt (cdr (org-extract-attributes-from-string attributes)))
1853 (while newatt
1854 (setq oldatt (plist-put oldatt (pop newatt) (pop newatt))))
1855 (if (string-match ">" tag)
1856 (setq tag
1857 (replace-match (concat (org-attributes-to-string oldatt) ">")
1858 t t tag)))
1859 tag)))
1861 (defun org-format-table-table-html (lines)
1862 "Format a table generated by table.el into HTML.
1863 This conversion does *not* use `table-generate-source' from table.el.
1864 This has the advantage that Org-mode's HTML conversions can be used.
1865 But it has the disadvantage, that no cell- or row-spanning is allowed."
1866 (let (line field-buffer
1867 (head org-export-highlight-first-table-line)
1868 fields html empty i)
1869 (setq html (concat html-table-tag "\n"))
1870 (while (setq line (pop lines))
1871 (setq empty "&nbsp;")
1872 (catch 'next-line
1873 (if (string-match "^[ \t]*\\+-" line)
1874 (progn
1875 (if field-buffer
1876 (progn
1877 (setq
1878 html
1879 (concat
1880 html
1881 "<tr>"
1882 (mapconcat
1883 (lambda (x)
1884 (if (equal x "") (setq x empty))
1885 (if head
1886 (concat
1887 (format (car org-export-table-header-tags) "col")
1889 (cdr org-export-table-header-tags))
1890 (concat (car org-export-table-data-tags) x
1891 (cdr org-export-table-data-tags))))
1892 field-buffer "\n")
1893 "</tr>\n"))
1894 (setq head nil)
1895 (setq field-buffer nil)))
1896 ;; Ignore this line
1897 (throw 'next-line t)))
1898 ;; Break the line into fields and store the fields
1899 (setq fields (org-split-string line "[ \t]*|[ \t]*"))
1900 (if field-buffer
1901 (setq field-buffer (mapcar
1902 (lambda (x)
1903 (concat x "<br/>" (pop fields)))
1904 field-buffer))
1905 (setq field-buffer fields))))
1906 (setq html (concat html "</table>\n"))
1907 html))
1909 (defun org-format-table-table-html-using-table-generate-source (lines)
1910 "Format a table into html, using `table-generate-source' from table.el.
1911 This has the advantage that cell- or row-spanning is allowed.
1912 But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
1913 (require 'table)
1914 (with-current-buffer (get-buffer-create " org-tmp1 ")
1915 (erase-buffer)
1916 (insert (mapconcat 'identity lines "\n"))
1917 (goto-char (point-min))
1918 (if (not (re-search-forward "|[^+]" nil t))
1919 (error "Error processing table"))
1920 (table-recognize-table)
1921 (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
1922 (table-generate-source 'html " org-tmp2 ")
1923 (set-buffer " org-tmp2 ")
1924 (buffer-substring (point-min) (point-max))))
1926 (defun org-export-splice-style (style extra)
1927 "Splice EXTRA into STYLE, just before \"</style>\"."
1928 (if (and (stringp extra)
1929 (string-match "\\S-" extra)
1930 (string-match "</style>" style))
1931 (concat (substring style 0 (match-beginning 0))
1932 "\n" extra "\n"
1933 (substring style (match-beginning 0)))
1934 style))
1936 (defun org-html-handle-time-stamps (s)
1937 "Format time stamps in string S, or remove them."
1938 (catch 'exit
1939 (let (r b)
1940 (while (string-match org-maybe-keyword-time-regexp s)
1941 (or b (setq b (substring s 0 (match-beginning 0))))
1942 (setq r (concat
1943 r (substring s 0 (match-beginning 0))
1944 " @<span class=\"timestamp-wrapper\">"
1945 (if (match-end 1)
1946 (format "@<span class=\"timestamp-kwd\">%s @</span>"
1947 (match-string 1 s)))
1948 (format " @<span class=\"timestamp\">%s@</span>"
1949 (substring
1950 (org-translate-time (match-string 3 s)) 1 -1))
1951 "@</span>")
1952 s (substring s (match-end 0))))
1953 ;; Line break if line started and ended with time stamp stuff
1954 (if (not r)
1956 (setq r (concat r s))
1957 (unless (string-match "\\S-" (concat b s))
1958 (setq r (concat r "@<br/>")))
1959 r))))
1961 (defvar htmlize-buffer-places) ; from htmlize.el
1962 (defun org-export-htmlize-region-for-paste (beg end)
1963 "Convert the region to HTML, using htmlize.el.
1964 This is much like `htmlize-region-for-paste', only that it uses
1965 the settings define in the org-... variables."
1966 (let* ((htmlize-output-type org-export-htmlize-output-type)
1967 (htmlize-css-name-prefix org-export-htmlize-css-font-prefix)
1968 (htmlbuf (htmlize-region beg end)))
1969 (unwind-protect
1970 (with-current-buffer htmlbuf
1971 (buffer-substring (plist-get htmlize-buffer-places 'content-start)
1972 (plist-get htmlize-buffer-places 'content-end)))
1973 (kill-buffer htmlbuf))))
1975 ;;;###autoload
1976 (defun org-export-htmlize-generate-css ()
1977 "Create the CSS for all font definitions in the current Emacs session.
1978 Use this to create face definitions in your CSS style file that can then
1979 be used by code snippets transformed by htmlize.
1980 This command just produces a buffer that contains class definitions for all
1981 faces used in the current Emacs session. You can copy and paste the ones you
1982 need into your CSS file.
1984 If you then set `org-export-htmlize-output-type' to `css', calls to
1985 the function `org-export-htmlize-region-for-paste' will produce code
1986 that uses these same face definitions."
1987 (interactive)
1988 (require 'htmlize)
1989 (and (get-buffer "*html*") (kill-buffer "*html*"))
1990 (with-temp-buffer
1991 (let ((fl (face-list))
1992 (htmlize-css-name-prefix "org-")
1993 (htmlize-output-type 'css)
1994 f i)
1995 (while (setq f (pop fl)
1996 i (and f (face-attribute f :inherit)))
1997 (when (and (symbolp f) (or (not i) (not (listp i))))
1998 (insert (org-add-props (copy-sequence "1") nil 'face f))))
1999 (htmlize-region (point-min) (point-max))))
2000 (switch-to-buffer "*html*")
2001 (goto-char (point-min))
2002 (if (re-search-forward "<style" nil t)
2003 (delete-region (point-min) (match-beginning 0)))
2004 (if (re-search-forward "</style>" nil t)
2005 (delete-region (1+ (match-end 0)) (point-max)))
2006 (beginning-of-line 1)
2007 (if (looking-at " +") (replace-match ""))
2008 (goto-char (point-min)))
2010 (defun org-html-protect (s)
2011 ;; convert & to &amp;, < to &lt; and > to &gt;
2012 (let ((start 0))
2013 (while (string-match "&" s start)
2014 (setq s (replace-match "&amp;" t t s)
2015 start (1+ (match-beginning 0))))
2016 (while (string-match "<" s)
2017 (setq s (replace-match "&lt;" t t s)))
2018 (while (string-match ">" s)
2019 (setq s (replace-match "&gt;" t t s)))
2020 ; (while (string-match "\"" s)
2021 ; (setq s (replace-match "&quot;" t t s)))
2025 (defun org-html-expand (string)
2026 "Prepare STRING for HTML export. Applies all active conversions.
2027 If there are links in the string, don't modify these."
2028 (let* ((re (concat org-bracket-link-regexp "\\|"
2029 (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$")))
2030 m s l res)
2031 (if (string-match "^[ \t]*\\+-[-+]*\\+[ \t]*$" string)
2032 string
2033 (while (setq m (string-match re string))
2034 (setq s (substring string 0 m)
2035 l (match-string 0 string)
2036 string (substring string (match-end 0)))
2037 (push (org-html-do-expand s) res)
2038 (push l res))
2039 (push (org-html-do-expand string) res)
2040 (apply 'concat (nreverse res)))))
2042 (defun org-html-do-expand (s)
2043 "Apply all active conversions to translate special ASCII to HTML."
2044 (setq s (org-html-protect s))
2045 (if org-export-html-expand
2046 (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
2047 (setq s (replace-match "<\\1>" t nil s))))
2048 (if org-export-with-emphasize
2049 (setq s (org-export-html-convert-emphasize s)))
2050 (if org-export-with-special-strings
2051 (setq s (org-export-html-convert-special-strings s)))
2052 (if org-export-with-sub-superscripts
2053 (setq s (org-export-html-convert-sub-super s)))
2054 (if org-export-with-TeX-macros
2055 (let ((start 0) wd rep)
2056 (while (setq start (string-match "\\\\\\([a-zA-Z]+[0-9]*\\)\\({}\\)?"
2057 s start))
2058 (if (get-text-property (match-beginning 0) 'org-protected s)
2059 (setq start (match-end 0))
2060 (setq wd (match-string 1 s))
2061 (if (setq rep (org-entity-get-representation wd 'html))
2062 (setq s (replace-match rep t t s))
2063 (setq start (+ start (length wd))))))))
2066 (defun org-export-html-convert-special-strings (string)
2067 "Convert special characters in STRING to HTML."
2068 (let ((all org-export-html-special-string-regexps)
2069 e a re rpl start)
2070 (while (setq a (pop all))
2071 (setq re (car a) rpl (cdr a) start 0)
2072 (while (string-match re string start)
2073 (if (get-text-property (match-beginning 0) 'org-protected string)
2074 (setq start (match-end 0))
2075 (setq string (replace-match rpl t nil string)))))
2076 string))
2078 (defun org-export-html-convert-sub-super (string)
2079 "Convert sub- and superscripts in STRING to HTML."
2080 (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
2081 (while (string-match org-match-substring-regexp string s)
2082 (cond
2083 ((and requireb (match-end 8)) (setq s (match-end 2)))
2084 ((get-text-property (match-beginning 2) 'org-protected string)
2085 (setq s (match-end 2)))
2087 (setq s (match-end 1)
2088 key (if (string= (match-string 2 string) "_") "sub" "sup")
2089 c (or (match-string 8 string)
2090 (match-string 6 string)
2091 (match-string 5 string))
2092 string (replace-match
2093 (concat (match-string 1 string)
2094 "<" key ">" c "</" key ">")
2095 t t string)))))
2096 (while (string-match "\\\\\\([_^]\\)" string)
2097 (setq string (replace-match (match-string 1 string) t t string)))
2098 string))
2100 (defun org-export-html-convert-emphasize (string)
2101 "Apply emphasis."
2102 (let ((s 0) rpl)
2103 (while (string-match org-emph-re string s)
2104 (if (not (equal
2105 (substring string (match-beginning 3) (1+ (match-beginning 3)))
2106 (substring string (match-beginning 4) (1+ (match-beginning 4)))))
2107 (setq s (match-beginning 0)
2109 (concat
2110 (match-string 1 string)
2111 (nth 2 (assoc (match-string 3 string) org-emphasis-alist))
2112 (match-string 4 string)
2113 (nth 3 (assoc (match-string 3 string)
2114 org-emphasis-alist))
2115 (match-string 5 string))
2116 string (replace-match rpl t t string)
2117 s (+ s (- (length rpl) 2)))
2118 (setq s (1+ s))))
2119 string))
2121 (defun org-open-par ()
2122 "Insert <p>, but first close previous paragraph if any."
2123 (org-close-par-maybe)
2124 (insert "\n<p>")
2125 (setq org-par-open t))
2126 (defun org-close-par-maybe ()
2127 "Close paragraph if there is one open."
2128 (when org-par-open
2129 (insert "</p>")
2130 (setq org-par-open nil)))
2131 (defun org-close-li (&optional type)
2132 "Close <li> if necessary."
2133 (org-close-par-maybe)
2134 (insert (if (equal type "d") "</dd>\n" "</li>\n")))
2136 (defvar in-local-list)
2137 (defvar local-list-indent)
2138 (defvar local-list-type)
2139 (defun org-export-html-close-lists-maybe (line)
2140 (let* ((rawhtml (and in-local-list
2141 (get-text-property 0 'org-protected line)))
2142 (ind (if rawhtml
2143 (org-get-indentation line)
2144 (or (get-text-property 0 'original-indentation line))))
2145 didclose)
2146 (when ind
2147 (while (and in-local-list
2148 (<= ind (car local-list-indent)))
2149 (setq didclose t)
2150 (org-close-li (car local-list-type))
2151 (insert (format "</%sl>\n" (car local-list-type)))
2152 (pop local-list-type) (pop local-list-indent)
2153 (setq in-local-list local-list-indent))
2154 (and didclose (org-open-par)))))
2156 (defvar body-only) ; dynamically scoped into this.
2157 (defun org-html-level-start (level title umax with-toc head-count)
2158 "Insert a new level in HTML export.
2159 When TITLE is nil, just close all open levels."
2160 (org-close-par-maybe)
2161 (let* ((target (and title (org-get-text-property-any 0 'target title)))
2162 (extra-targets (and target
2163 (assoc target org-export-target-aliases)))
2164 (extra-class (and title (org-get-text-property-any 0 'html-container-class title)))
2165 (preferred (and target
2166 (cdr (assoc target org-export-preferred-target-alist))))
2167 (remove (or preferred target))
2168 (l org-level-max)
2169 snumber snu href suffix)
2170 (setq extra-targets (remove remove extra-targets))
2171 (setq extra-targets
2172 (mapconcat (lambda (x)
2173 (if (org-uuidgen-p x) (setq x (concat "ID-" x)))
2174 (format "<a name=\"%s\" id=\"%s\"></a>"
2175 x x))
2176 extra-targets
2177 ""))
2178 (while (>= l level)
2179 (if (aref org-levels-open (1- l))
2180 (progn
2181 (org-html-level-close l umax)
2182 (aset org-levels-open (1- l) nil)))
2183 (setq l (1- l)))
2184 (when title
2185 ;; If title is nil, this means this function is called to close
2186 ;; all levels, so the rest is done only if title is given
2187 (when (string-match (org-re "\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
2188 (setq title (replace-match
2189 (if org-export-with-tags
2190 (save-match-data
2191 (concat
2192 "&nbsp;&nbsp;&nbsp;<span class=\"tag\">"
2193 (mapconcat
2194 (lambda (x)
2195 (format "<span class=\"%s\">%s</span>"
2196 (org-export-html-get-tag-class-name x)
2198 (org-split-string (match-string 1 title) ":")
2199 "&nbsp;")
2200 "</span>"))
2202 t t title)))
2203 (if (> level umax)
2204 (progn
2205 (if (aref org-levels-open (1- level))
2206 (progn
2207 (org-close-li)
2208 (if target
2209 (insert (format "<li id=\"%s\">" target) extra-targets title "<br/>\n")
2210 (insert "<li>" title "<br/>\n")))
2211 (aset org-levels-open (1- level) t)
2212 (org-close-par-maybe)
2213 (if target
2214 (insert (format "<ul>\n<li id=\"%s\">" target)
2215 extra-targets title "<br/>\n")
2216 (insert "<ul>\n<li>" title "<br/>\n"))))
2217 (aset org-levels-open (1- level) t)
2218 (setq snumber (org-section-number level)
2219 snu (replace-regexp-in-string "\\." "_" snumber))
2220 (setq level (+ level org-export-html-toplevel-hlevel -1))
2221 (if (and org-export-with-section-numbers (not body-only))
2222 (setq title (concat
2223 (format "<span class=\"section-number-%d\">%s</span>"
2224 level snumber)
2225 " " title)))
2226 (unless (= head-count 1) (insert "\n</div>\n"))
2227 (setq href (cdr (assoc (concat "sec-" snu) org-export-preferred-target-alist)))
2228 (setq suffix (or href snu))
2229 (setq href (or href (concat "sec-" snu)))
2230 (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"
2231 suffix level (if extra-class (concat " " extra-class) "")
2232 level href
2233 extra-targets
2234 title level level suffix))
2235 (org-open-par)))))
2237 (defun org-export-html-get-tag-class-name (tag)
2238 "Turn tag into a valid class name.
2239 Replaces invalid characters with \"_\" and then prepends a prefix."
2240 (save-match-data
2241 (while (string-match "[^a-zA-Z0-9_]" tag)
2242 (setq tag (replace-match "_" t t tag))))
2243 (concat org-export-html-tag-class-prefix tag))
2245 (defun org-export-html-get-todo-kwd-class-name (kwd)
2246 "Turn todo keyword into a valid class name.
2247 Replaces invalid characters with \"_\" and then prepends a prefix."
2248 (save-match-data
2249 (while (string-match "[^a-zA-Z0-9_]" kwd)
2250 (setq kwd (replace-match "_" t t kwd))))
2251 (concat org-export-html-todo-kwd-class-prefix kwd))
2253 (defun org-html-level-close (level max-outline-level)
2254 "Terminate one level in HTML export."
2255 (if (<= level max-outline-level)
2256 (insert "</div>\n")
2257 (org-close-li)
2258 (insert "</ul>\n")))
2260 (provide 'org-html)
2262 ;; arch-tag: 8109d84d-eb8f-460b-b1a8-f45f3a6c7ea1
2263 ;;; org-html.el ends here