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