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