Defvar to silence byte compiler
[org-mode.git] / lisp / org-html.el
blobcd15ececa4fcba7653cd75a2438ce9642b92dc42
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 th.right { text-align:center; }
141 th.left { text-align:center; }
142 th.center { text-align:center; }
143 td.right { text-align:right; }
144 td.left { text-align:left; }
145 td.center { text-align:center; }
146 dt { font-weight: bold; }
147 div.figure { padding: 0.5em; }
148 div.figure p { text-align: center; }
149 textarea { overflow-x: auto; }
150 .linenr { font-size:smaller }
151 .code-highlighted {background-color:#ffff00;}
152 .org-info-js_info-navigation { border-style:none; }
153 #org-info-js_console-label { font-size:10px; font-weight:bold;
154 white-space:nowrap; }
155 .org-info-js_search-highlight {background-color:#ffff00; color:#000000;
156 font-weight:bold; }
157 /*]]>*/-->
158 </style>"
159 "The default style specification for exported HTML files.
160 Please use the variables `org-export-html-style' and
161 `org-export-html-style-extra' to add to this style. If you wish to not
162 have the default style included, customize the variable
163 `org-export-html-style-include-default'.")
165 (defcustom org-export-html-style-include-default t
166 "Non-nil means include the default style in exported HTML files.
167 The actual style is defined in `org-export-html-style-default' and should
168 not be modified. Use the variables `org-export-html-style' to add
169 your own style information."
170 :group 'org-export-html
171 :type 'boolean)
172 ;;;###autoload
173 (put 'org-export-html-style-include-default 'safe-local-variable 'booleanp)
175 (defcustom org-export-html-style ""
176 "Org-wide style definitions for exported HTML files.
178 This variable needs to contain the full HTML structure to provide a style,
179 including the surrounding HTML tags. If you set the value of this variable,
180 you should consider to include definitions for the following classes:
181 title, todo, done, timestamp, timestamp-kwd, tag, target.
183 For example, a valid value would be:
185 <style type=\"text/css\">
186 <![CDATA[
187 p { font-weight: normal; color: gray; }
188 h1 { color: black; }
189 .title { text-align: center; }
190 .todo, .timestamp-kwd { color: red; }
191 .done { color: green; }
193 </style>
195 If you'd like to refer to en external style file, use something like
197 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
199 As the value of this option simply gets inserted into the HTML <head> header,
200 you can \"misuse\" it to add arbitrary text to the header.
201 See also the variable `org-export-html-style-extra'."
202 :group 'org-export-html
203 :type 'string)
204 ;;;###autoload
205 (put 'org-export-html-style 'safe-local-variable 'stringp)
207 (defcustom org-export-html-style-extra ""
208 "Additional style information for HTML export.
209 The value of this variable is inserted into the HTML buffer right after
210 the value of `org-export-html-style'. Use this variable for per-file
211 settings of style information, and do not forget to surround the style
212 settings with <style>...</style> tags."
213 :group 'org-export-html
214 :type 'string)
215 ;;;###autoload
216 (put 'org-export-html-style-extra 'safe-local-variable 'stringp)
218 (defcustom org-export-html-mathjax-options
219 '((path "http://orgmode.org/mathjax/MathJax.js")
220 (scale "100")
221 (align "center")
222 (indent "2em")
223 (mathml nil))
224 "Options for MathJax setup.
226 path The path where to find MathJax
227 scale Scaling for the HTML-CSS backend, usually between 100 and 133
228 align How to align display math: left, center, or right
229 indent If align is not center, how far from the left/right side?
230 mathml Should a MathML player be used if available?
231 This is faster and reduces bandwidth use, but currently
232 sometimes has lower spacing quality. Therefore, the default is
233 nil. When browsers get better, this switch can be flipped.
235 You can also customize this for each buffer, using something like
237 #+MATHJAX: scale:\"133\" align:\"right\" mathml:t path:\"/MathJax/\""
238 :group 'org-export-html
239 :type '(list :greedy t
240 (list :tag "path (the path from where to load MathJax.js)"
241 (const :format " " path) (string))
242 (list :tag "scale (scaling for the displayed math)"
243 (const :format " " scale) (string))
244 (list :tag "align (alignment of displayed equations)"
245 (const :format " " align) (string))
246 (list :tag "indent (indentation with left or right alignment)"
247 (const :format " " indent) (string))
248 (list :tag "mathml (should MathML display be used is possible)"
249 (const :format " " mathml) (boolean))))
251 (defun org-export-html-mathjax-config (template options in-buffer)
252 "Insert the user setup into the matchjax template."
253 (let (name val (yes " ") (no "// ") x)
254 (mapc
255 (lambda (e)
256 (setq name (car e) val (nth 1 e))
257 (if (string-match (concat "\\<" (symbol-name name) ":") in-buffer)
258 (setq val (car (read-from-string
259 (substring in-buffer (match-end 0))))))
260 (if (not (stringp val)) (setq val (format "%s" val)))
261 (if (string-match (concat "%" (upcase (symbol-name name))) template)
262 (setq template (replace-match val t t template))))
263 options)
264 (setq val (nth 1 (assq 'mathml options)))
265 (if (string-match (concat "\\<mathml:") in-buffer)
266 (setq val (car (read-from-string
267 (substring in-buffer (match-end 0))))))
268 ;; Exchange prefixes depending on mathml setting
269 (if (not val) (setq x yes yes no no x))
270 ;; Replace cookies to turn on or off the config/jax lines
271 (if (string-match ":MMLYES:" template)
272 (setq template (replace-match yes t t template)))
273 (if (string-match ":MMLNO:" template)
274 (setq template (replace-match no t t template)))
275 ;; Return the modified template
276 template))
278 (defcustom org-export-html-mathjax-template
279 "<script type=\"text/javascript\" src=\"%PATH\">
280 <!--/*--><![CDATA[/*><!--*/
281 MathJax.Hub.Config({
282 // Only one of the two following lines, depending on user settings
283 // First allows browser-native MathML display, second forces HTML/CSS
284 :MMLYES: config: [\"MMLorHTML.js\"], jax: [\"input/TeX\"],
285 :MMLNO: jax: [\"input/TeX\", \"output/HTML-CSS\"],
286 extensions: [\"tex2jax.js\",\"TeX/AMSmath.js\",\"TeX/AMSsymbols.js\",
287 \"TeX/noUndefined.js\"],
288 tex2jax: {
289 inlineMath: [ [\"\\\\(\",\"\\\\)\"] ],
290 displayMath: [ ['$$','$$'], [\"\\\\[\",\"\\\\]\"] ],
291 skipTags: [\"script\",\"noscript\",\"style\",\"textarea\",\"pre\",\"code\"],
292 ignoreClass: \"tex2jax_ignore\",
293 processEscapes: false,
294 processEnvironments: true,
295 preview: \"TeX\"
297 showProcessingMessages: true,
298 displayAlign: \"%ALIGN\",
299 displayIndent: \"%INDENT\",
301 \"HTML-CSS\": {
302 scale: %SCALE,
303 availableFonts: [\"STIX\",\"TeX\"],
304 preferredFont: \"TeX\",
305 webFont: \"TeX\",
306 imageFont: \"TeX\",
307 showMathMenu: true,
309 MMLorHTML: {
310 prefer: {
311 MSIE: \"MML\",
312 Firefox: \"MML\",
313 Opera: \"HTML\",
314 other: \"HTML\"
318 /*]]>*///-->
319 </script>"
320 "The MathJax setup for XHTML files."
321 :group 'org-export-html
322 :type 'string)
324 (defcustom org-export-html-tag-class-prefix ""
325 "Prefix to class names for TODO keywords.
326 Each tag gets a class given by the tag itself, with this prefix.
327 The default prefix is empty because it is nice to just use the keyword
328 as a class name. But if you get into conflicts with other, existing
329 CSS classes, then this prefix can be very useful."
330 :group 'org-export-html
331 :type 'string)
333 (defcustom org-export-html-todo-kwd-class-prefix ""
334 "Prefix to class names for TODO keywords.
335 Each TODO keyword gets a class given by the keyword itself, with this prefix.
336 The default prefix is empty because it is nice to just use the keyword
337 as a class name. But if you get into conflicts with other, existing
338 CSS classes, then this prefix can be very useful."
339 :group 'org-export-html
340 :type 'string)
342 (defcustom org-export-html-title-format "<h1 class=\"title\">%s</h1>\n"
343 "Format for typesetting the document title in HTML export."
344 :group 'org-export-html
345 :type 'string)
347 (defcustom org-export-html-home/up-format
348 "<div id=\"org-div-home-and-up\" style=\"text-align:right;font-size:70%%;white-space:nowrap;\">
349 <a accesskey=\"h\" href=\"%s\"> UP </a>
351 <a accesskey=\"H\" href=\"%s\"> HOME </a>
352 </div>"
353 "Snippet used to insert the HOME and UP links.
354 This is a format string, the first %s will receive the UP link,
355 the second the HOME link. If both `org-export-html-link-up' and
356 `org-export-html-link-home' are empty, the entire snippet will be
357 ignored."
358 :group 'org-export-html
359 :type 'string)
361 (defcustom org-export-html-toplevel-hlevel 2
362 "The <H> level for level 1 headings in HTML export.
363 This is also important for the classes that will be wrapped around headlines
364 and outline structure. If this variable is 1, the top-level headlines will
365 be <h1>, and the corresponding classes will be outline-1, section-number-1,
366 and outline-text-1. If this is 2, all of these will get a 2 instead.
367 The default for this variable is 2, because we use <h1> for formatting the
368 document title."
369 :group 'org-export-html
370 :type 'string)
372 (defcustom org-export-html-link-org-files-as-html t
373 "Non-nil means make file links to `file.org' point to `file.html'.
374 When org-mode is exporting an org-mode file to HTML, links to
375 non-html files are directly put into a href tag in HTML.
376 However, links to other Org-mode files (recognized by the
377 extension `.org.) should become links to the corresponding html
378 file, assuming that the linked org-mode file will also be
379 converted to HTML.
380 When nil, the links still point to the plain `.org' file."
381 :group 'org-export-html
382 :type 'boolean)
384 (defcustom org-export-html-inline-images 'maybe
385 "Non-nil means inline images into exported HTML pages.
386 This is done using an <img> tag. When nil, an anchor with href is used to
387 link to the image. If this option is `maybe', then images in links with
388 an empty description will be inlined, while images with a description will
389 be linked only."
390 :group 'org-export-html
391 :type '(choice (const :tag "Never" nil)
392 (const :tag "Always" t)
393 (const :tag "When there is no description" maybe)))
395 (defcustom org-export-html-inline-image-extensions
396 '("png" "jpeg" "jpg" "gif" "svg")
397 "Extensions of image files that can be inlined into HTML."
398 :group 'org-export-html
399 :type '(repeat (string :tag "Extension")))
401 (defcustom org-export-html-table-tag
402 "<table border=\"2\" cellspacing=\"0\" cellpadding=\"6\" rules=\"groups\" frame=\"hsides\">"
403 "The HTML tag that is used to start a table.
404 This must be a <table> tag, but you may change the options like
405 borders and spacing."
406 :group 'org-export-html
407 :type 'string)
409 (defcustom org-export-table-header-tags '("<th scope=\"%s\"%s>" . "</th>")
410 "The opening tag for table header fields.
411 This is customizable so that alignment options can be specified.
412 The first %s will be filled with the scope of the field, either row or col.
413 The second %s will be replaced by a style entry to align the field.
414 See also the variable `org-export-html-table-use-header-tags-for-first-column'.
415 See also the variable `org-export-html-table-align-individual-fields'."
416 :group 'org-export-tables
417 :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
419 (defcustom org-export-table-data-tags '("<td%s>" . "</td>")
420 "The opening tag for table data fields.
421 This is customizable so that alignment options can be specified.
422 The first %s will be filled with the scope of the field, either row or col.
423 The second %s will be replaced by a style entry to align the field.
424 See also the variable `org-export-html-table-align-individual-fields'."
425 :group 'org-export-tables
426 :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
428 (defcustom org-export-table-row-tags '("<tr>" . "</tr>")
429 "The opening tag for table data fields.
430 This is customizable so that alignment options can be specified.
431 Instead of strings, these can be Lisp forms that will be evaluated
432 for each row in order to construct the table row tags. During evaluation,
433 the variable `head' will be true when this is a header line, nil when this
434 is a body line. And the variable `nline' will contain the line number,
435 starting from 1 in the first header line. For example
437 (setq org-export-table-row-tags
438 (cons '(if head
439 \"<tr>\"
440 (if (= (mod nline 2) 1)
441 \"<tr class=\\\"tr-odd\\\">\"
442 \"<tr class=\\\"tr-even\\\">\"))
443 \"</tr>\"))
445 will give even lines the class \"tr-even\" and odd lines the class \"tr-odd\"."
446 :group 'org-export-tables
447 :type '(cons
448 (choice :tag "Opening tag"
449 (string :tag "Specify")
450 (sexp))
451 (choice :tag "Closing tag"
452 (string :tag "Specify")
453 (sexp))))
455 (defcustom org-export-html-table-align-individual-fields t
456 "Non-nil means attach style attributes for alignment to each table field.
457 When nil, alignment will only be specified in the column tags, but this
458 is ignored by some browsers (like Firefox, Safari). Opera does it right
459 though."
460 :group 'org-export-tables
461 :type 'boolean)
463 (defcustom org-export-html-table-use-header-tags-for-first-column nil
464 "Non-nil means format column one in tables with header tags.
465 When nil, also column one will use data tags."
466 :group 'org-export-tables
467 :type 'boolean)
469 (defcustom org-export-html-validation-link nil
470 "Non-nil means add validation link to postamble of HTML exported files."
471 :group 'org-export-html
472 :type '(choice
473 (const :tag "Nothing" nil)
474 (const :tag "XHTML 1.0" "<p class=\"xhtml-validation\"><a href=\"http://validator.w3.org/check?uri=referer\">Validate XHTML 1.0</a></p>")
475 (string :tag "Specify full HTML")))
478 (defcustom org-export-html-with-timestamp nil
479 "If non-nil, write timestamp into the exported HTML text.
480 If non-nil Write `org-export-html-html-helper-timestamp' into the
481 exported HTML text. Otherwise, the buffer will just be saved to
482 a file."
483 :group 'org-export-html
484 :type 'boolean)
486 (defcustom org-export-html-html-helper-timestamp
487 "<br/><br/><hr><p><!-- hhmts start --> <!-- hhmts end --></p>\n"
488 "The HTML tag used as timestamp delimiter for HTML-helper-mode."
489 :group 'org-export-html
490 :type 'string)
492 (defgroup org-export-htmlize nil
493 "Options for processing examples with htmlize.el."
494 :tag "Org Export Htmlize"
495 :group 'org-export-html)
497 (defcustom org-export-htmlize-output-type 'inline-css
498 "Output type to be used by htmlize when formatting code snippets.
499 We use as default `inline-css', in order to make the resulting
500 HTML self-containing.
501 However, this will fail when using Emacs in batch mode for export, because
502 then no rich font definitions are in place. It will also not be good if
503 people with different Emacs setup contribute HTML files to a website,
504 because the fonts will represent the individual setups. In these cases,
505 it is much better to let Org/Htmlize assign classes only, and to use
506 a style file to define the look of these classes.
507 To get a start for your css file, start Emacs session and make sure that
508 all the faces you are interested in are defined, for example by loading files
509 in all modes you want. Then, use the command
510 \\[org-export-htmlize-generate-css] to extract class definitions."
511 :group 'org-export-htmlize
512 :type '(choice (const css) (const inline-css)))
514 (defcustom org-export-htmlize-css-font-prefix "org-"
515 "The prefix for CSS class names for htmlize font specifications."
516 :group 'org-export-htmlize
517 :type 'string)
519 (defcustom org-export-htmlized-org-css-url nil
520 "URL pointing to a CSS file defining text colors for htmlized Emacs buffers.
521 Normally when creating an htmlized version of an Org buffer, htmlize will
522 create CSS to define the font colors. However, this does not work when
523 converting in batch mode, and it also can look bad if different people
524 with different fontification setup work on the same website.
525 When this variable is non-nil, creating an htmlized version of an Org buffer
526 using `org-export-as-org' will remove the internal CSS section and replace it
527 with a link to this URL."
528 :group 'org-export-htmlize
529 :type '(choice
530 (const :tag "Keep internal css" nil)
531 (string :tag "URL or local href")))
533 ;;; Variables, constants, and parameter plists
535 (defvar org-export-html-preamble nil
536 "Preamble, to be inserted just after <body>. Set by publishing functions.
537 This may also be a function, building and inserting the preamble.")
538 (defvar org-export-html-postamble nil
539 "Postamble, to be inserted just before </body>. Set by publishing functions.
540 This may also be a function, building and inserting the postamble.")
541 (defvar org-export-html-auto-preamble t
542 "Should default preamble be inserted? Set by publishing functions.")
543 (defvar org-export-html-auto-postamble t
544 "Should default postamble be inserted? Set by publishing functions.")
546 ;;; Hooks
548 (defvar org-export-html-after-blockquotes-hook nil
549 "Hook run during HTML export, after blockquote, verse, center are done.")
551 (defvar org-export-html-final-hook nil
552 "Hook run at the end of HTML export, in the new buffer.")
554 ;;; HTML export
556 (defun org-export-html-preprocess (parameters)
557 "Convert LaTeX fragments to images."
558 (when (and org-current-export-file
559 (plist-get parameters :LaTeX-fragments))
560 (org-format-latex
561 (concat "ltxpng/" (file-name-sans-extension
562 (file-name-nondirectory
563 org-current-export-file)))
564 org-current-export-dir nil "Creating LaTeX image %s"
565 nil nil
566 (cond
567 ((eq (plist-get parameters :LaTeX-fragments) 'verbatim) 'verbatim)
568 ((eq (plist-get parameters :LaTeX-fragments) 'mathjax ) 'mathjax)
569 ((eq (plist-get parameters :LaTeX-fragments) t ) 'mathjax)
570 ((eq (plist-get parameters :LaTeX-fragments) 'dvipng ) 'dvipng)
571 (t nil))))
572 (goto-char (point-min))
573 (let (label l1)
574 (while (re-search-forward "\\\\ref{\\([^{}\n]+\\)}" nil t)
575 (org-if-unprotected-at (match-beginning 1)
576 (setq label (match-string 1))
577 (save-match-data
578 (if (string-match "\\`[a-z]\\{1,10\\}:\\(.+\\)" label)
579 (setq l1 (substring label (match-beginning 1)))
580 (setq l1 label)))
581 (replace-match (format "[[#%s][%s]]" label l1) t t)))))
583 ;;;###autoload
584 (defun org-export-as-html-and-open (arg)
585 "Export the outline as HTML and immediately open it with a browser.
586 If there is an active region, export only the region.
587 The prefix ARG specifies how many levels of the outline should become
588 headlines. The default is 3. Lower levels will become bulleted lists."
589 (interactive "P")
590 (org-export-as-html arg 'hidden)
591 (org-open-file buffer-file-name)
592 (when org-export-kill-product-buffer-when-displayed
593 (kill-buffer (current-buffer))))
595 ;;;###autoload
596 (defun org-export-as-html-batch ()
597 "Call the function `org-export-as-html'.
598 This function can be used in batch processing as:
599 emacs --batch
600 --load=$HOME/lib/emacs/org.el
601 --eval \"(setq org-export-headline-levels 2)\"
602 --visit=MyFile --funcall org-export-as-html-batch"
603 (org-export-as-html org-export-headline-levels 'hidden))
605 ;;;###autoload
606 (defun org-export-as-html-to-buffer (arg)
607 "Call `org-export-as-html` with output to a temporary buffer.
608 No file is created. The prefix ARG is passed through to `org-export-as-html'."
609 (interactive "P")
610 (org-export-as-html arg nil nil "*Org HTML Export*")
611 (when org-export-show-temporary-export-buffer
612 (switch-to-buffer-other-window "*Org HTML Export*")))
614 ;;;###autoload
615 (defun org-replace-region-by-html (beg end)
616 "Assume the current region has org-mode syntax, and convert it to HTML.
617 This can be used in any buffer. For example, you could write an
618 itemized list in org-mode syntax in an HTML buffer and then use this
619 command to convert it."
620 (interactive "r")
621 (let (reg html buf pop-up-frames)
622 (save-window-excursion
623 (if (org-mode-p)
624 (setq html (org-export-region-as-html
625 beg end t 'string))
626 (setq reg (buffer-substring beg end)
627 buf (get-buffer-create "*Org tmp*"))
628 (with-current-buffer buf
629 (erase-buffer)
630 (insert reg)
631 (org-mode)
632 (setq html (org-export-region-as-html
633 (point-min) (point-max) t 'string)))
634 (kill-buffer buf)))
635 (delete-region beg end)
636 (insert html)))
638 ;;;###autoload
639 (defun org-export-region-as-html (beg end &optional body-only buffer)
640 "Convert region from BEG to END in org-mode buffer to HTML.
641 If prefix arg BODY-ONLY is set, omit file header, footer, and table of
642 contents, and only produce the region of converted text, useful for
643 cut-and-paste operations.
644 If BUFFER is a buffer or a string, use/create that buffer as a target
645 of the converted HTML. If BUFFER is the symbol `string', return the
646 produced HTML as a string and leave not buffer behind. For example,
647 a Lisp program could call this function in the following way:
649 (setq html (org-export-region-as-html beg end t 'string))
651 When called interactively, the output buffer is selected, and shown
652 in a window. A non-interactive call will only return the buffer."
653 (interactive "r\nP")
654 (when (interactive-p)
655 (setq buffer "*Org HTML Export*"))
656 (let ((transient-mark-mode t) (zmacs-regions t)
657 ext-plist rtn)
658 (setq ext-plist (plist-put ext-plist :ignore-subtree-p t))
659 (goto-char end)
660 (set-mark (point)) ;; to activate the region
661 (goto-char beg)
662 (setq rtn (org-export-as-html
663 nil nil ext-plist
664 buffer body-only))
665 (if (fboundp 'deactivate-mark) (deactivate-mark))
666 (if (and (interactive-p) (bufferp rtn))
667 (switch-to-buffer-other-window rtn)
668 rtn)))
670 (defvar html-table-tag nil) ; dynamically scoped into this.
671 (defvar org-par-open nil)
673 ;;; org-html-cvt-link-fn
674 (defconst org-html-cvt-link-fn
676 "Function to convert link URLs to exportable URLs.
677 Takes two arguments, TYPE and PATH.
678 Returns exportable url as (TYPE PATH), or nil to signal that it
679 didn't handle this case.
680 Intended to be locally bound around a call to `org-export-as-html'." )
682 (defun org-html-cvt-org-as-html (opt-plist type path)
683 "Convert an org filename to an equivalent html filename.
684 If TYPE is not file, just return `nil'.
685 See variable `org-export-html-link-org-files-as-html'"
687 (save-match-data
688 (and
689 org-export-html-link-org-files-as-html
690 (string= type "file")
691 (string-match "\\.org$" path)
692 (progn
693 (list
694 "file"
695 (concat
696 (substring path 0 (match-beginning 0))
698 (plist-get opt-plist :html-extension)))))))
701 ;;; org-html-should-inline-p
702 (defun org-html-should-inline-p (filename descp)
703 "Return non-nil if link FILENAME should be inlined.
704 The decision to inline the FILENAME link is based on the current
705 settings. DESCP is the boolean of whether there was a link
706 description. See variables `org-export-html-inline-images' and
707 `org-export-html-inline-image-extensions'."
708 (declare (special
709 org-export-html-inline-images
710 org-export-html-inline-image-extensions))
711 (and (or (eq t org-export-html-inline-images)
712 (and org-export-html-inline-images (not descp)))
713 (org-file-image-p
714 filename org-export-html-inline-image-extensions)))
716 ;;; org-html-make-link
717 (defun org-html-make-link (opt-plist type path fragment desc attr
718 may-inline-p)
719 "Make an HTML link.
720 OPT-PLIST is an options list.
721 TYPE is the device-type of the link (THIS://foo.html)
722 PATH is the path of the link (http://THIS#locationx)
723 FRAGMENT is the fragment part of the link, if any (foo.html#THIS)
724 DESC is the link description, if any.
725 ATTR is a string of other attributes of the a element.
726 MAY-INLINE-P allows inlining it as an image."
728 (declare (special org-par-open))
729 (save-match-data
730 (let* ((filename path)
731 ;;First pass. Just sanity stuff.
732 (components-1
733 (cond
734 ((string= type "file")
735 (list
736 type
737 ;;Substitute just if original path was absolute.
738 ;;(Otherwise path must remain relative)
739 (if (file-name-absolute-p path)
740 (concat "file://" (expand-file-name path))
741 path)))
742 ((string= type "")
743 (list nil path))
744 (t (list type path))))
746 ;;Second pass. Components converted so they can refer
747 ;;to a remote site.
748 (components-2
750 (and org-html-cvt-link-fn
751 (apply org-html-cvt-link-fn
752 opt-plist components-1))
753 (apply #'org-html-cvt-org-as-html
754 opt-plist components-1)
755 components-1))
756 (type (first components-2))
757 (thefile (second components-2)))
760 ;;Third pass. Build final link except for leading type
761 ;;spec.
762 (cond
763 ((or
764 (not type)
765 (string= type "http")
766 (string= type "https")
767 (string= type "file"))
768 (if fragment
769 (setq thefile (concat thefile "#" fragment))))
771 (t))
773 ;;Final URL-build, for all types.
774 (setq thefile
775 (let
776 ((str (org-export-html-format-href thefile)))
777 (if (and type (not (string= "file" type)))
778 (concat type ":" str)
779 str)))
781 (if (and
782 may-inline-p
783 ;;Can't inline a URL with a fragment.
784 (not fragment))
785 (progn
786 (message "image %s %s" thefile org-par-open)
787 (org-export-html-format-image thefile org-par-open))
788 (concat
789 "<a href=\"" thefile "\"" attr ">"
790 (org-export-html-format-desc desc)
791 "</a>")))))
793 ;;; org-export-as-html
794 ;;;###autoload
795 (defun org-export-as-html (arg &optional hidden ext-plist
796 to-buffer body-only pub-dir)
797 "Export the outline as a pretty HTML file.
798 If there is an active region, export only the region. The prefix
799 ARG specifies how many levels of the outline should become
800 headlines. The default is 3. Lower levels will become bulleted
801 lists. HIDDEN is obsolete and does nothing.
802 EXT-PLIST is a property list with external parameters overriding
803 org-mode's default settings, but still inferior to file-local
804 settings. When TO-BUFFER is non-nil, create a buffer with that
805 name and export to that buffer. If TO-BUFFER is the symbol
806 `string', don't leave any buffer behind but just return the
807 resulting HTML as a string. When BODY-ONLY is set, don't produce
808 the file header and footer, simply return the content of
809 <body>...</body>, without even the body tags themselves. When
810 PUB-DIR is set, use this as the publishing directory."
811 (interactive "P")
812 (run-hooks 'org-export-first-hook)
814 ;; Make sure we have a file name when we need it.
815 (when (and (not (or to-buffer body-only))
816 (not buffer-file-name))
817 (if (buffer-base-buffer)
818 (org-set-local 'buffer-file-name
819 (with-current-buffer (buffer-base-buffer)
820 buffer-file-name))
821 (error "Need a file name to be able to export")))
823 (message "Exporting...")
824 (setq-default org-todo-line-regexp org-todo-line-regexp)
825 (setq-default org-deadline-line-regexp org-deadline-line-regexp)
826 (setq-default org-done-keywords org-done-keywords)
827 (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp)
828 (let* ((opt-plist
829 (org-export-process-option-filters
830 (org-combine-plists (org-default-export-plist)
831 ext-plist
832 (org-infile-export-plist))))
833 (body-only (or body-only (plist-get opt-plist :body-only)))
834 (style (concat (if (plist-get opt-plist :style-include-default)
835 org-export-html-style-default)
836 (plist-get opt-plist :style)
837 (plist-get opt-plist :style-extra)
838 "\n"
839 (if (plist-get opt-plist :style-include-scripts)
840 org-export-html-scripts)))
841 (html-extension (plist-get opt-plist :html-extension))
842 (link-validate (plist-get opt-plist :link-validation-function))
843 valid thetoc have-headings first-heading-pos
844 (odd org-odd-levels-only)
845 (region-p (org-region-active-p))
846 (rbeg (and region-p (region-beginning)))
847 (rend (and region-p (region-end)))
848 (subtree-p
849 (if (plist-get opt-plist :ignore-subtree-p)
851 (when region-p
852 (save-excursion
853 (goto-char rbeg)
854 (and (org-at-heading-p)
855 (>= (org-end-of-subtree t t) rend))))))
856 (level-offset (if subtree-p
857 (save-excursion
858 (goto-char rbeg)
859 (+ (funcall outline-level)
860 (if org-odd-levels-only 1 0)))
862 (opt-plist (setq org-export-opt-plist
863 (if subtree-p
864 (org-export-add-subtree-options opt-plist rbeg)
865 opt-plist)))
866 ;; The following two are dynamically scoped into other
867 ;; routines below.
868 (org-current-export-dir
869 (or pub-dir (org-export-directory :html opt-plist)))
870 (org-current-export-file buffer-file-name)
871 (level 0) (line "") (origline "") txt todo
872 (umax nil)
873 (umax-toc nil)
874 (filename (if to-buffer nil
875 (expand-file-name
876 (concat
877 (file-name-sans-extension
878 (or (and subtree-p
879 (org-entry-get (region-beginning)
880 "EXPORT_FILE_NAME" t))
881 (file-name-nondirectory buffer-file-name)))
882 "." html-extension)
883 (file-name-as-directory
884 (or pub-dir (org-export-directory :html opt-plist))))))
885 (current-dir (if buffer-file-name
886 (file-name-directory buffer-file-name)
887 default-directory))
888 (buffer (if to-buffer
889 (cond
890 ((eq to-buffer 'string) (get-buffer-create "*Org HTML Export*"))
891 (t (get-buffer-create to-buffer)))
892 (find-file-noselect filename)))
893 (org-levels-open (make-vector org-level-max nil))
894 (date (plist-get opt-plist :date))
895 (author (plist-get opt-plist :author))
896 (title (or (and subtree-p (org-export-get-title-from-subtree))
897 (plist-get opt-plist :title)
898 (and (not body-only)
899 (not
900 (plist-get opt-plist :skip-before-1st-heading))
901 (org-export-grab-title-from-buffer))
902 (and buffer-file-name
903 (file-name-sans-extension
904 (file-name-nondirectory buffer-file-name)))
905 "UNTITLED"))
906 (link-up (and (plist-get opt-plist :link-up)
907 (string-match "\\S-" (plist-get opt-plist :link-up))
908 (plist-get opt-plist :link-up)))
909 (link-home (and (plist-get opt-plist :link-home)
910 (string-match "\\S-" (plist-get opt-plist :link-home))
911 (plist-get opt-plist :link-home)))
912 (dummy (setq opt-plist (plist-put opt-plist :title title)))
913 (html-table-tag (plist-get opt-plist :html-table-tag))
914 (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
915 (quote-re (concat "^\\(\\*+\\)\\([ \t]+" org-quote-string "\\>\\)"))
916 (inquote nil)
917 (infixed nil)
918 (inverse nil)
919 (in-local-list nil)
920 (local-list-type nil)
921 (local-list-indent nil)
922 (llt org-plain-list-ordered-item-terminator)
923 (email (plist-get opt-plist :email))
924 (language (plist-get opt-plist :language))
925 (keywords (plist-get opt-plist :keywords))
926 (description (plist-get opt-plist :description))
927 (lang-words nil)
928 (head-count 0) cnt
929 (start 0)
930 (coding-system (and (boundp 'buffer-file-coding-system)
931 buffer-file-coding-system))
932 (coding-system-for-write (or org-export-html-coding-system
933 coding-system))
934 (save-buffer-coding-system (or org-export-html-coding-system
935 coding-system))
936 (charset (and coding-system-for-write
937 (fboundp 'coding-system-get)
938 (coding-system-get coding-system-for-write
939 'mime-charset)))
940 (region
941 (buffer-substring
942 (if region-p (region-beginning) (point-min))
943 (if region-p (region-end) (point-max))))
944 (org-export-have-math nil)
945 (lines
946 (org-split-string
947 (org-export-preprocess-string
948 region
949 :emph-multiline t
950 :for-html t
951 :skip-before-1st-heading
952 (plist-get opt-plist :skip-before-1st-heading)
953 :drawers (plist-get opt-plist :drawers)
954 :todo-keywords (plist-get opt-plist :todo-keywords)
955 :tags (plist-get opt-plist :tags)
956 :priority (plist-get opt-plist :priority)
957 :footnotes (plist-get opt-plist :footnotes)
958 :timestamps (plist-get opt-plist :timestamps)
959 :archived-trees
960 (plist-get opt-plist :archived-trees)
961 :select-tags (plist-get opt-plist :select-tags)
962 :exclude-tags (plist-get opt-plist :exclude-tags)
963 :add-text
964 (plist-get opt-plist :text)
965 :LaTeX-fragments
966 (plist-get opt-plist :LaTeX-fragments))
967 "[\r\n]"))
968 (mathjax
969 (if (or (eq (plist-get opt-plist :LaTeX-fragments) 'mathjax)
970 (and org-export-have-math
971 (eq (plist-get opt-plist :LaTeX-fragments) t)))
973 (org-export-html-mathjax-config
974 org-export-html-mathjax-template
975 org-export-html-mathjax-options
976 (or (plist-get opt-plist :mathjax) ""))
977 ""))
978 table-open type
979 table-buffer table-orig-buffer
980 ind item-type starter
981 rpl path attr desc descp desc1 desc2 link
982 snumber fnc item-tag item-number
983 footnotes footref-seen
984 id-file href
987 (let ((inhibit-read-only t))
988 (org-unmodified
989 (remove-text-properties (point-min) (point-max)
990 '(:org-license-to-kill t))))
992 (message "Exporting...")
994 (setq org-min-level (org-get-min-level lines level-offset))
995 (setq org-last-level org-min-level)
996 (org-init-section-numbers)
998 (cond
999 ((and date (string-match "%" date))
1000 (setq date (format-time-string date)))
1001 (date)
1002 (t (setq date (format-time-string "%Y-%m-%d %T %Z"))))
1004 ;; Get the language-dependent settings
1005 (setq lang-words (or (assoc language org-export-language-setup)
1006 (assoc "en" org-export-language-setup)))
1008 ;; Switch to the output buffer
1009 (set-buffer buffer)
1010 (let ((inhibit-read-only t)) (erase-buffer))
1011 (fundamental-mode)
1012 (org-install-letbind)
1014 (and (fboundp 'set-buffer-file-coding-system)
1015 (set-buffer-file-coding-system coding-system-for-write))
1017 (let ((case-fold-search nil)
1018 (org-odd-levels-only odd))
1019 ;; create local variables for all options, to make sure all called
1020 ;; functions get the correct information
1021 (mapc (lambda (x)
1022 (set (make-local-variable (nth 2 x))
1023 (plist-get opt-plist (car x))))
1024 org-export-plist-vars)
1025 (setq umax (if arg (prefix-numeric-value arg)
1026 org-export-headline-levels))
1027 (setq umax-toc (if (integerp org-export-with-toc)
1028 (min org-export-with-toc umax)
1029 umax))
1030 (unless body-only
1031 ;; File header
1032 (insert (format
1034 <!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
1035 \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
1036 <html xmlns=\"http://www.w3.org/1999/xhtml\"
1037 lang=\"%s\" xml:lang=\"%s\">
1038 <head>
1039 <title>%s</title>
1040 <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>
1041 <meta name=\"generator\" content=\"Org-mode\"/>
1042 <meta name=\"generated\" content=\"%s\"/>
1043 <meta name=\"author\" content=\"%s\"/>
1044 <meta name=\"description\" content=\"%s\"/>
1045 <meta name=\"keywords\" content=\"%s\"/>
1048 </head>
1049 <body>
1050 <div id=\"content\">
1053 (format
1054 (or (and (stringp org-export-html-xml-declaration)
1055 org-export-html-xml-declaration)
1056 (cdr (assoc html-extension org-export-html-xml-declaration))
1057 (cdr (assoc "html" org-export-html-xml-declaration))
1060 (or charset "iso-8859-1"))
1061 language language
1062 title
1063 (or charset "iso-8859-1")
1064 date author description keywords
1065 style
1066 mathjax
1067 (if (or link-up link-home)
1068 (concat
1069 (format org-export-html-home/up-format
1070 (or link-up link-home)
1071 (or link-home link-up))
1072 "\n")
1073 "")))
1075 (org-export-html-insert-plist-item opt-plist :preamble opt-plist)
1077 (when (plist-get opt-plist :auto-preamble)
1078 (if title (insert (format org-export-html-title-format
1079 (org-html-expand title))))))
1081 (if (and org-export-with-toc (not body-only))
1082 (progn
1083 (push (format "<h%d>%s</h%d>\n"
1084 org-export-html-toplevel-hlevel
1085 (nth 3 lang-words)
1086 org-export-html-toplevel-hlevel)
1087 thetoc)
1088 (push "<div id=\"text-table-of-contents\">\n" thetoc)
1089 (push "<ul>\n<li>" thetoc)
1090 (setq lines
1091 (mapcar '(lambda (line)
1092 (if (and (string-match org-todo-line-regexp line)
1093 (not (get-text-property 0 'org-protected line)))
1094 ;; This is a headline
1095 (progn
1096 (setq have-headings t)
1097 (setq level (- (match-end 1) (match-beginning 1)
1098 level-offset)
1099 level (org-tr-level level)
1100 txt (save-match-data
1101 (org-html-expand
1102 (org-export-cleanup-toc-line
1103 (match-string 3 line))))
1104 todo
1105 (or (and org-export-mark-todo-in-toc
1106 (match-beginning 2)
1107 (not (member (match-string 2 line)
1108 org-done-keywords)))
1109 ; TODO, not DONE
1110 (and org-export-mark-todo-in-toc
1111 (= level umax-toc)
1112 (org-search-todo-below
1113 line lines level))))
1114 (if (string-match
1115 (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") txt)
1116 (setq txt (replace-match "&nbsp;&nbsp;&nbsp;<span class=\"tag\"> \\1</span>" t nil txt)))
1117 (if (string-match quote-re0 txt)
1118 (setq txt (replace-match "" t t txt)))
1119 (setq snumber (org-section-number level))
1120 (if org-export-with-section-numbers
1121 (setq txt (concat snumber " " txt)))
1122 (if (<= level (max umax umax-toc))
1123 (setq head-count (+ head-count 1)))
1124 (if (<= level umax-toc)
1125 (progn
1126 (if (> level org-last-level)
1127 (progn
1128 (setq cnt (- level org-last-level))
1129 (while (>= (setq cnt (1- cnt)) 0)
1130 (push "\n<ul>\n<li>" thetoc))
1131 (push "\n" thetoc)))
1132 (if (< level org-last-level)
1133 (progn
1134 (setq cnt (- org-last-level level))
1135 (while (>= (setq cnt (1- cnt)) 0)
1136 (push "</li>\n</ul>" thetoc))
1137 (push "\n" thetoc)))
1138 ;; Check for targets
1139 (while (string-match org-any-target-regexp line)
1140 (setq line (replace-match
1141 (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
1142 t t line)))
1143 (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
1144 (setq txt (replace-match "" t t txt)))
1145 (setq href
1146 (replace-regexp-in-string
1147 "\\." "_" (format "sec-%s" snumber)))
1148 (setq href (or (cdr (assoc href org-export-preferred-target-alist)) href))
1149 (push
1150 (format
1151 (if todo
1152 "</li>\n<li><a href=\"#%s\"><span class=\"todo\">%s</span></a>"
1153 "</li>\n<li><a href=\"#%s\">%s</a>")
1154 href txt) thetoc)
1156 (setq org-last-level level))
1158 line)
1159 lines))
1160 (while (> org-last-level (1- org-min-level))
1161 (setq org-last-level (1- org-last-level))
1162 (push "</li>\n</ul>\n" thetoc))
1163 (push "</div>\n" thetoc)
1164 (setq thetoc (if have-headings (nreverse thetoc) nil))))
1166 (setq head-count 0)
1167 (org-init-section-numbers)
1169 (org-open-par)
1171 (while (setq line (pop lines) origline line)
1172 (catch 'nextline
1174 ;; end of quote section?
1175 (when (and inquote (string-match "^\\*+ " line))
1176 (insert "</pre>\n")
1177 (org-open-par)
1178 (setq inquote nil))
1179 ;; inside a quote section?
1180 (when inquote
1181 (insert (org-html-protect line) "\n")
1182 (throw 'nextline nil))
1184 ;; Fixed-width, verbatim lines (examples)
1185 (when (and org-export-with-fixed-width
1186 (string-match "^[ \t]*:\\(\\([ \t]\\|$\\)\\(.*\\)\\)" line))
1187 (when (not infixed)
1188 (setq infixed t)
1189 (org-close-par-maybe)
1191 (insert "<pre class=\"example\">\n"))
1192 (insert (org-html-protect (match-string 3 line)) "\n")
1193 (when (or (not lines)
1194 (not (string-match "^[ \t]*:\\(\\([ \t]\\|$\\)\\(.*\\)\\)"
1195 (car lines))))
1196 (setq infixed nil)
1197 (insert "</pre>\n")
1198 (org-open-par))
1199 (throw 'nextline nil))
1201 ;; Explicit list closure
1202 (when (equal "ORG-LIST-END" line)
1203 (while local-list-indent
1204 (org-close-li (car local-list-type))
1205 (insert (format "</%sl>\n" (car local-list-type)))
1206 (pop local-list-type)
1207 (pop local-list-indent))
1208 (setq in-local-list nil)
1209 (org-open-par)
1210 (throw 'nextline nil))
1212 ;; Protected HTML
1213 (when (get-text-property 0 'org-protected line)
1214 (let (par (ind (get-text-property 0 'original-indentation line)))
1215 (when (re-search-backward
1216 "\\(<p>\\)\\([ \t\r\n]*\\)\\=" (- (point) 100) t)
1217 (setq par (match-string 1))
1218 (replace-match "\\2\n"))
1219 (insert line "\n")
1220 (while (and lines
1221 (or (= (length (car lines)) 0)
1222 (not ind)
1223 (equal ind (get-text-property 0 'original-indentation (car lines))))
1224 (or (= (length (car lines)) 0)
1225 (get-text-property 0 'org-protected (car lines))))
1226 (insert (pop lines) "\n"))
1227 (and par (insert "<p>\n")))
1228 (throw 'nextline nil))
1230 ;; Blockquotes, verse, and center
1231 (when (equal "ORG-BLOCKQUOTE-START" line)
1232 (org-close-par-maybe)
1233 (insert "<blockquote>\n")
1234 (org-open-par)
1235 (throw 'nextline nil))
1236 (when (equal "ORG-BLOCKQUOTE-END" line)
1237 (org-close-par-maybe)
1238 (insert "\n</blockquote>\n")
1239 (org-open-par)
1240 (throw 'nextline nil))
1241 (when (equal "ORG-VERSE-START" line)
1242 (org-close-par-maybe)
1243 (insert "\n<p class=\"verse\">\n")
1244 (setq org-par-open t)
1245 (setq inverse t)
1246 (throw 'nextline nil))
1247 (when (equal "ORG-VERSE-END" line)
1248 (insert "</p>\n")
1249 (setq org-par-open nil)
1250 (org-open-par)
1251 (setq inverse nil)
1252 (throw 'nextline nil))
1253 (when (equal "ORG-CENTER-START" line)
1254 (org-close-par-maybe)
1255 (insert "\n<div style=\"text-align: center\">")
1256 (org-open-par)
1257 (throw 'nextline nil))
1258 (when (equal "ORG-CENTER-END" line)
1259 (org-close-par-maybe)
1260 (insert "\n</div>")
1261 (org-open-par)
1262 (throw 'nextline nil))
1263 (run-hooks 'org-export-html-after-blockquotes-hook)
1264 (when inverse
1265 (let ((i (org-get-string-indentation line)))
1266 (if (> i 0)
1267 (setq line (concat (mapconcat 'identity
1268 (make-list (* 2 i) "\\nbsp") "")
1269 " " (org-trim line))))
1270 (unless (string-match "\\\\\\\\[ \t]*$" line)
1271 (setq line (concat line "\\\\")))))
1273 ;; make targets to anchors
1274 (setq start 0)
1275 (while (string-match
1276 "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line start)
1277 (cond
1278 ((get-text-property (match-beginning 1) 'org-protected line)
1279 (setq start (match-end 1)))
1280 ((match-end 2)
1281 (setq line (replace-match
1282 (format
1283 "@<a name=\"%s\" id=\"%s\">@</a>"
1284 (org-solidify-link-text (match-string 1 line))
1285 (org-solidify-link-text (match-string 1 line)))
1286 t t line)))
1287 ((and org-export-with-toc (equal (string-to-char line) ?*))
1288 ;; FIXME: NOT DEPENDENT on TOC?????????????????????
1289 (setq line (replace-match
1290 (concat "@<span class=\"target\">"
1291 (match-string 1 line) "@</span> ")
1292 ;; (concat "@<i>" (match-string 1 line) "@</i> ")
1293 t t line)))
1295 (setq line (replace-match
1296 (concat "@<a name=\""
1297 (org-solidify-link-text (match-string 1 line))
1298 "\" class=\"target\">" (match-string 1 line)
1299 "@</a> ")
1300 t t line)))))
1302 (setq line (org-html-handle-time-stamps line))
1304 ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
1305 ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
1306 ;; Also handle sub_superscripts and checkboxes
1307 (or (string-match org-table-hline-regexp line)
1308 (setq line (org-html-expand line)))
1310 ;; Format the links
1311 (setq start 0)
1312 (while (string-match org-bracket-link-analytic-regexp++ line start)
1313 (setq start (match-beginning 0))
1314 (setq path (save-match-data (org-link-unescape
1315 (match-string 3 line))))
1316 (setq type (cond
1317 ((match-end 2) (match-string 2 line))
1318 ((save-match-data
1319 (or (file-name-absolute-p path)
1320 (string-match "^\\.\\.?/" path)))
1321 "file")
1322 (t "internal")))
1323 (setq path (org-extract-attributes (org-link-unescape path)))
1324 (setq attr (get-text-property 0 'org-attributes path))
1325 (setq desc1 (if (match-end 5) (match-string 5 line))
1326 desc2 (if (match-end 2) (concat type ":" path) path)
1327 descp (and desc1 (not (equal desc1 desc2)))
1328 desc (or desc1 desc2))
1329 ;; Make an image out of the description if that is so wanted
1330 (when (and descp (org-file-image-p
1331 desc org-export-html-inline-image-extensions))
1332 (save-match-data
1333 (if (string-match "^file:" desc)
1334 (setq desc (substring desc (match-end 0)))))
1335 (setq desc (org-add-props
1336 (concat "<img src=\"" desc "\"/>")
1337 '(org-protected t))))
1338 (cond
1339 ((equal type "internal")
1340 (let
1341 ((frag-0
1342 (if (= (string-to-char path) ?#)
1343 (substring path 1)
1344 path)))
1345 (setq rpl
1346 (org-html-make-link
1347 opt-plist
1350 (org-solidify-link-text
1351 (save-match-data (org-link-unescape frag-0))
1352 nil)
1353 desc attr nil))))
1354 ((and (equal type "id")
1355 (setq id-file (org-id-find-id-file path)))
1356 ;; This is an id: link to another file (if it was the same file,
1357 ;; it would have become an internal link...)
1358 (save-match-data
1359 (setq id-file (file-relative-name
1360 id-file
1361 (file-name-directory org-current-export-file)))
1362 (setq rpl
1363 (org-html-make-link opt-plist
1364 "file" id-file
1365 (concat (if (org-uuidgen-p path) "ID-") path)
1366 desc
1367 attr
1368 nil))))
1369 ((member type '("http" "https"))
1370 ;; standard URL, can inline as image
1371 (setq rpl
1372 (org-html-make-link opt-plist
1373 type path nil
1374 desc
1375 attr
1376 (org-html-should-inline-p path descp))))
1377 ((member type '("ftp" "mailto" "news"))
1378 ;; standard URL, can't inline as image
1379 (setq rpl
1380 (org-html-make-link opt-plist
1381 type path nil
1382 desc
1383 attr
1384 nil)))
1386 ((string= type "coderef")
1387 (let*
1388 ((coderef-str (format "coderef-%s" path))
1389 (attr-1
1390 (format "class=\"coderef\" onmouseover=\"CodeHighlightOn(this, '%s');\" onmouseout=\"CodeHighlightOff(this, '%s');\""
1391 coderef-str coderef-str)))
1392 (setq rpl
1393 (org-html-make-link opt-plist
1394 type "" coderef-str
1395 (format
1396 (org-export-get-coderef-format
1397 path
1398 (and descp desc))
1399 (cdr (assoc path org-export-code-refs)))
1400 attr-1
1401 nil))))
1403 ((functionp (setq fnc (nth 2 (assoc type org-link-protocols))))
1404 ;; The link protocol has a function for format the link
1405 (setq rpl
1406 (save-match-data
1407 (funcall fnc (org-link-unescape path) desc1 'html))))
1409 ((string= type "file")
1410 ;; FILE link
1411 (save-match-data
1412 (let*
1413 ((components
1415 (string-match "::\\(.*\\)" path)
1416 (list
1417 (replace-match "" t nil path)
1418 (match-string 1 path))
1419 (list path nil)))
1421 ;;The proper path, without a fragment
1422 (path-1
1423 (first components))
1425 ;;The raw fragment
1426 (fragment-0
1427 (second components))
1429 ;;Check the fragment. If it can't be used as
1430 ;;target fragment we'll pass nil instead.
1431 (fragment-1
1433 (and fragment-0
1434 (not (string-match "^[0-9]*$" fragment-0))
1435 (not (string-match "^\\*" fragment-0))
1436 (not (string-match "^/.*/$" fragment-0)))
1437 (org-solidify-link-text
1438 (org-link-unescape fragment-0))
1439 nil))
1440 (desc-2
1441 ;;Description minus "file:" and ".org"
1442 (if (string-match "^file:" desc)
1443 (let
1444 ((desc-1 (replace-match "" t t desc)))
1445 (if (string-match "\\.org$" desc-1)
1446 (replace-match "" t t desc-1)
1447 desc-1))
1448 desc)))
1450 (setq rpl
1452 (and
1453 (functionp link-validate)
1454 (not (funcall link-validate path-1 current-dir)))
1455 desc
1456 (org-html-make-link opt-plist
1457 "file" path-1 fragment-1 desc-2 attr
1458 (org-html-should-inline-p path-1 descp)))))))
1461 ;; just publish the path, as default
1462 (setq rpl (concat "<i>&lt;" type ":"
1463 (save-match-data (org-link-unescape path))
1464 "&gt;</i>"))))
1465 (setq line (replace-match rpl t t line)
1466 start (+ start (length rpl))))
1468 ;; TODO items
1469 (if (and (string-match org-todo-line-regexp line)
1470 (match-beginning 2))
1472 (setq line
1473 (concat (substring line 0 (match-beginning 2))
1474 "<span class=\""
1475 (if (member (match-string 2 line)
1476 org-done-keywords)
1477 "done" "todo")
1478 " " (match-string 2 line)
1479 "\"> " (org-export-html-get-todo-kwd-class-name
1480 (match-string 2 line))
1481 "</span>" (substring line (match-end 2)))))
1483 ;; Does this contain a reference to a footnote?
1484 (when org-export-with-footnotes
1485 (setq start 0)
1486 (while (string-match "\\([^* \t].*?\\)\\[\\([0-9]+\\)\\]" line start)
1487 (if (get-text-property (match-beginning 2) 'org-protected line)
1488 (setq start (match-end 2))
1489 (let ((n (match-string 2 line)) extra a)
1490 (if (setq a (assoc n footref-seen))
1491 (progn
1492 (setcdr a (1+ (cdr a)))
1493 (setq extra (format ".%d" (cdr a))))
1494 (setq extra "")
1495 (push (cons n 1) footref-seen))
1496 (setq line
1497 (replace-match
1498 (format
1499 (concat "%s"
1500 (format org-export-html-footnote-format
1501 "<a class=\"footref\" name=\"fnr.%s%s\" href=\"#fn.%s\">%s</a>"))
1502 (or (match-string 1 line) "") n extra n n)
1503 t t line))))))
1505 (cond
1506 ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
1507 ;; This is a headline
1508 (setq level (org-tr-level (- (match-end 1) (match-beginning 1)
1509 level-offset))
1510 txt (match-string 2 line))
1511 (if (string-match quote-re0 txt)
1512 (setq txt (replace-match "" t t txt)))
1513 (if (<= level (max umax umax-toc))
1514 (setq head-count (+ head-count 1)))
1515 (setq first-heading-pos (or first-heading-pos (point)))
1516 (org-html-level-start level txt umax
1517 (and org-export-with-toc (<= level umax))
1518 head-count)
1520 ;; QUOTES
1521 (when (string-match quote-re line)
1522 (org-close-par-maybe)
1523 (insert "<pre>")
1524 (setq inquote t)))
1526 ((and org-export-with-tables
1527 (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
1528 (when (not table-open)
1529 ;; New table starts
1530 (setq table-open t table-buffer nil table-orig-buffer nil))
1532 ;; Accumulate lines
1533 (setq table-buffer (cons line table-buffer)
1534 table-orig-buffer (cons origline table-orig-buffer))
1535 (when (or (not lines)
1536 (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
1537 (car lines))))
1538 (setq table-open nil
1539 table-buffer (nreverse table-buffer)
1540 table-orig-buffer (nreverse table-orig-buffer))
1541 (org-close-par-maybe)
1542 (insert (org-format-table-html table-buffer table-orig-buffer))))
1544 ;; Normal lines
1545 (when (string-match
1546 (cond
1547 ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
1548 ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
1549 ((= llt ?\)) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
1550 (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
1551 line)
1552 (setq ind (or (get-text-property 0 'original-indentation line)
1553 (org-get-string-indentation line))
1554 item-type (if (match-beginning 4) "o" "u")
1555 starter (if (match-beginning 2)
1556 (substring (match-string 2 line) 0 -1))
1557 line (substring line (match-beginning 5))
1558 item-number nil
1559 item-tag nil)
1560 (if (string-match "\\[@\\(?:start:\\)?\\([0-9]+\\)\\][ \t]?" line)
1561 (setq item-number (match-string 1 line)
1562 line (replace-match "" t t line)))
1563 (if (and starter (string-match "\\(.*?\\) ::[ \t]*" line))
1564 (setq item-type "d"
1565 item-tag (match-string 1 line)
1566 line (substring line (match-end 0))))
1567 (cond
1568 ((and starter
1569 (or (not in-local-list)
1570 (> ind (car local-list-indent))))
1571 ;; Start new (level of) list
1572 (org-close-par-maybe)
1573 (insert (cond
1574 ((equal item-type "u") "<ul>\n<li>\n")
1575 ((and (equal item-type "o") item-number)
1576 (format "<ol>\n<li value=\"%s\">\n" item-number))
1577 ((equal item-type "o") "<ol>\n<li>\n")
1578 ((equal item-type "d")
1579 (format "<dl>\n<dt>%s</dt><dd>\n" item-tag))))
1580 (push item-type local-list-type)
1581 (push ind local-list-indent)
1582 (setq in-local-list t))
1583 ;; Continue list
1584 (starter
1585 ;; terminate any previous sublist but first ensure
1586 ;; list is not ill-formed.
1587 (let ((min-ind (apply 'min local-list-indent)))
1588 (when (< ind min-ind) (setq ind min-ind)))
1589 (while (< ind (car local-list-indent))
1590 (org-close-li (car local-list-type))
1591 (insert (format "</%sl>\n" (car local-list-type)))
1592 (pop local-list-type) (pop local-list-indent)
1593 (setq in-local-list local-list-indent))
1594 ;; insert new item
1595 (org-close-li (car local-list-type))
1596 (insert (cond
1597 ((equal (car local-list-type) "d")
1598 (format "<dt>%s</dt><dd>\n" (or item-tag "???")))
1599 ((and (equal item-type "o") item-number)
1600 (format "<li value=\"%s\">\n" item-number))
1601 (t "<li>\n")))))
1602 (if (string-match "^[ \t]*\\[\\([X ]\\)\\]" line)
1603 (setq line
1604 (replace-match
1605 (if (equal (match-string 1 line) "X")
1606 "<b>[X]</b>"
1607 "<b>[<span style=\"visibility:hidden;\">X</span>]</b>")
1608 t t line))))
1610 ;; Horizontal line
1611 (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line)
1612 (if org-par-open
1613 (insert "\n</p>\n<hr/>\n<p>\n")
1614 (insert "\n<hr/>\n"))
1615 (throw 'nextline nil))
1617 ;; Empty lines start a new paragraph. If hand-formatted lists
1618 ;; are not fully interpreted, lines starting with "-", "+", "*"
1619 ;; also start a new paragraph.
1620 (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (org-open-par))
1622 ;; Is this the start of a footnote?
1623 (when org-export-with-footnotes
1624 (when (and (boundp 'footnote-section-tag-regexp)
1625 (string-match (concat "^" footnote-section-tag-regexp)
1626 line))
1627 ;; ignore this line
1628 (throw 'nextline nil))
1629 (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
1630 (org-close-par-maybe)
1631 (let ((n (match-string 1 line)))
1632 (setq org-par-open t
1633 line (replace-match
1634 (format
1635 (concat "<p class=\"footnote\">"
1636 (format org-export-html-footnote-format
1637 "<a class=\"footnum\" name=\"fn.%s\" href=\"#fnr.%s\">%s</a>"))
1638 n n n) t t line)))))
1639 ;; Check if the line break needs to be conserved
1640 (cond
1641 ((string-match "\\\\\\\\[ \t]*$" line)
1642 (setq line (replace-match "<br/>" t t line)))
1643 (org-export-preserve-breaks
1644 (setq line (concat line "<br/>"))))
1646 ;; Check if a paragraph should be started
1647 (let ((start 0))
1648 (while (and org-par-open
1649 (string-match "\\\\par\\>" line start))
1650 ;; Leave a space in the </p> so that the footnote matcher
1651 ;; does not see this.
1652 (if (not (get-text-property (match-beginning 0)
1653 'org-protected line))
1654 (setq line (replace-match "</p ><p >" t t line)))
1655 (setq start (match-end 0))))
1657 (insert line "\n")))))
1659 ;; Properly close all local lists and other lists
1660 (when inquote
1661 (insert "</pre>\n")
1662 (org-open-par))
1664 (org-html-level-start 1 nil umax
1665 (and org-export-with-toc (<= level umax))
1666 head-count)
1667 ;; the </div> to close the last text-... div.
1668 (when (and (> umax 0) first-heading-pos) (insert "</div>\n"))
1670 (save-excursion
1671 (goto-char (point-min))
1672 (while (re-search-forward "<p class=\"footnote\">[^\000]*?\\(</p>\\|\\'\\)" nil t)
1673 (push (match-string 0) footnotes)
1674 (replace-match "" t t)))
1675 (when footnotes
1676 (insert (format org-export-html-footnotes-section
1677 (nth 4 lang-words)
1678 (mapconcat 'identity (nreverse footnotes) "\n"))
1679 "\n"))
1680 (let ((bib (org-export-html-get-bibliography)))
1681 (when bib
1682 (insert "\n" bib "\n")))
1683 (unless body-only
1684 (when (plist-get opt-plist :auto-postamble)
1685 (insert "<div id=\"postamble\">\n")
1686 (when (and org-export-author-info author)
1687 (insert "<p class=\"author\"> "
1688 (nth 1 lang-words) ": " author "\n")
1689 (when (and org-export-email-info email (string-match "\\S-" email))
1690 (if (listp (split-string email ",+ *"))
1691 (mapc (lambda(e)
1692 (insert "<a href=\"mailto:" e "\">&lt;"
1693 e "&gt;</a>\n"))
1694 (split-string email ",+ *"))
1695 (insert "<a href=\"mailto:" email "\">&lt;"
1696 email "&gt;</a>\n")))
1697 (insert "</p>\n"))
1698 (when (and date org-export-time-stamp-file)
1699 (insert "<p class=\"date\"> "
1700 (nth 2 lang-words) ": "
1701 date "</p>\n"))
1702 (when org-export-creator-info
1703 (insert (format "<p class=\"creator\">HTML generated by org-mode %s in emacs %s</p>\n"
1704 org-version emacs-major-version)))
1705 (when org-export-html-validation-link
1706 (insert org-export-html-validation-link "\n"))
1707 (insert "</div>"))
1709 (if org-export-html-with-timestamp
1710 (insert org-export-html-html-helper-timestamp))
1711 (org-export-html-insert-plist-item opt-plist :postamble opt-plist)
1712 (insert "\n</div>\n</body>\n</html>\n"))
1714 (unless (plist-get opt-plist :buffer-will-be-killed)
1715 (normal-mode)
1716 (if (eq major-mode (default-value 'major-mode))
1717 (html-mode)))
1719 ;; insert the table of contents
1720 (goto-char (point-min))
1721 (when thetoc
1722 (if (or (re-search-forward
1723 "<p>\\s-*\\[TABLE-OF-CONTENTS\\]\\s-*</p>" nil t)
1724 (re-search-forward
1725 "\\[TABLE-OF-CONTENTS\\]" nil t))
1726 (progn
1727 (goto-char (match-beginning 0))
1728 (replace-match ""))
1729 (goto-char first-heading-pos)
1730 (when (looking-at "\\s-*</p>")
1731 (goto-char (match-end 0))
1732 (insert "\n")))
1733 (insert "<div id=\"table-of-contents\">\n")
1734 (mapc 'insert thetoc)
1735 (insert "</div>\n"))
1736 ;; remove empty paragraphs and lists
1737 (goto-char (point-min))
1738 (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t)
1739 (replace-match ""))
1740 (goto-char (point-min))
1741 (while (re-search-forward "<li>[ \r\n\t]*</li>\n?" nil t)
1742 (replace-match ""))
1743 (goto-char (point-min))
1744 ;; Convert whitespace place holders
1745 (goto-char (point-min))
1746 (let (beg end n)
1747 (while (setq beg (next-single-property-change (point) 'org-whitespace))
1748 (setq n (get-text-property beg 'org-whitespace)
1749 end (next-single-property-change beg 'org-whitespace))
1750 (goto-char beg)
1751 (delete-region beg end)
1752 (insert (format "<span style=\"visibility:hidden;\">%s</span>"
1753 (make-string n ?x)))))
1754 ;; Remove empty lines at the beginning of the file.
1755 (goto-char (point-min))
1756 (when (looking-at "\\s-+\n") (replace-match ""))
1757 ;; Remove display properties
1758 (remove-text-properties (point-min) (point-max) '(display t))
1759 ;; Run the hook
1760 (run-hooks 'org-export-html-final-hook)
1761 (or to-buffer (save-buffer))
1762 (goto-char (point-min))
1763 (or (org-export-push-to-kill-ring "HTML")
1764 (message "Exporting... done"))
1765 (if (eq to-buffer 'string)
1766 (prog1 (buffer-substring (point-min) (point-max))
1767 (kill-buffer (current-buffer)))
1768 (current-buffer)))))
1770 (defun org-export-html-insert-plist-item (plist key &rest args)
1771 (let ((item (plist-get plist key)))
1772 (cond ((functionp item)
1773 (apply item args))
1774 (item
1775 (insert item)))))
1777 (defun org-export-html-format-href (s)
1778 "Make sure the S is valid as a href reference in an XHTML document."
1779 (save-match-data
1780 (let ((start 0))
1781 (while (string-match "&" s start)
1782 (setq start (+ (match-beginning 0) 3)
1783 s (replace-match "&amp;" t t s)))))
1786 (defun org-export-html-format-desc (s)
1787 "Make sure the S is valid as a description in a link."
1788 (if (and s (not (get-text-property 1 'org-protected s)))
1789 (save-match-data
1790 (org-html-do-expand s))
1793 (defun org-export-html-format-image (src par-open)
1794 "Create image tag with source and attributes."
1795 (save-match-data
1796 (if (string-match "^ltxpng/" src)
1797 (format "<img src=\"%s\" alt=\"%s\"/>"
1798 src (org-find-text-property-in-string 'org-latex-src src))
1799 (let* ((caption (org-find-text-property-in-string 'org-caption src))
1800 (attr (org-find-text-property-in-string 'org-attributes src))
1801 (label (org-find-text-property-in-string 'org-label src)))
1802 (setq caption (and caption (org-html-do-expand caption)))
1803 (concat
1804 (if caption
1805 (format "%s<div %sclass=\"figure\">
1806 <p>"
1807 (if org-par-open "</p>\n" "")
1808 (if label (format "id=\"%s\" " label) "")))
1809 (format "<img src=\"%s\"%s />"
1811 (if (string-match "\\<alt=" (or attr ""))
1812 (concat " " attr )
1813 (concat " " attr " alt=\"" src "\"")))
1814 (if caption
1815 (format "</p>%s
1816 </div>%s"
1817 (concat "\n<p>" caption "</p>")
1818 (if org-par-open "\n<p>" ""))))))))
1820 (defun org-export-html-get-bibliography ()
1821 "Find bibliography, cut it out and return it."
1822 (catch 'exit
1823 (let (beg end (cnt 1) bib)
1824 (save-excursion
1825 (goto-char (point-min))
1826 (when (re-search-forward "^[ \t]*<div \\(id\\|class\\)=\"bibliography\"" nil t)
1827 (setq beg (match-beginning 0))
1828 (while (re-search-forward "</?div\\>" nil t)
1829 (setq cnt (+ cnt (if (string= (match-string 0) "<div") +1 -1)))
1830 (when (= cnt 0)
1831 (and (looking-at ">") (forward-char 1))
1832 (setq bib (buffer-substring beg (point)))
1833 (delete-region beg (point))
1834 (throw 'exit bib))))
1835 nil))))
1837 (defvar org-table-number-regexp) ; defined in org-table.el
1838 (defun org-format-table-html (lines olines)
1839 "Find out which HTML converter to use and return the HTML code."
1840 (if (stringp lines)
1841 (setq lines (org-split-string lines "\n")))
1842 (if (string-match "^[ \t]*|" (car lines))
1843 ;; A normal org table
1844 (org-format-org-table-html lines)
1845 ;; Table made by table.el - test for spanning
1846 (let* ((hlines (delq nil (mapcar
1847 (lambda (x)
1848 (if (string-match "^[ \t]*\\+-" x) x
1849 nil))
1850 lines)))
1851 (first (car hlines))
1852 (ll (and (string-match "\\S-+" first)
1853 (match-string 0 first)))
1854 (re (concat "^[ \t]*" (regexp-quote ll)))
1855 (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
1856 hlines))))
1857 (if (and (not spanning)
1858 (not org-export-prefer-native-exporter-for-tables))
1859 ;; We can use my own converter with HTML conversions
1860 (org-format-table-table-html lines)
1861 ;; Need to use the code generator in table.el, with the original text.
1862 (org-format-table-table-html-using-table-generate-source olines)))))
1864 (defvar org-table-number-fraction) ; defined in org-table.el
1865 (defun org-format-org-table-html (lines &optional splice)
1866 "Format a table into HTML."
1867 (require 'org-table)
1868 ;; Get rid of hlines at beginning and end
1869 (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
1870 (setq lines (nreverse lines))
1871 (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
1872 (setq lines (nreverse lines))
1873 (when org-export-table-remove-special-lines
1874 ;; Check if the table has a marking column. If yes remove the
1875 ;; column and the special lines
1876 (setq lines (org-table-clean-before-export lines)))
1878 (let* ((caption (org-find-text-property-in-string 'org-caption (car lines)))
1879 (label (org-find-text-property-in-string 'org-label (car lines)))
1880 (forced-aligns (org-find-text-property-in-string 'org-forced-aligns
1881 (car lines)))
1882 (attributes (org-find-text-property-in-string 'org-attributes
1883 (car lines)))
1884 (html-table-tag (org-export-splice-attributes
1885 html-table-tag attributes))
1886 (head (and org-export-highlight-first-table-line
1887 (delq nil (mapcar
1888 (lambda (x) (string-match "^[ \t]*|-" x))
1889 (cdr lines)))))
1890 (nline 0) fnum nfields i (cnt 0)
1891 tbopen line fields html gr colgropen rowstart rowend
1892 ali align aligns n)
1893 (setq caption (and caption (org-html-do-expand caption)))
1894 (when (and forced-aligns org-table-clean-did-remove-column)
1895 (setq forced-aligns
1896 (mapcar (lambda (x) (cons (1- (car x)) (cdr x))) forced-aligns)))
1897 (if splice (setq head nil))
1898 (unless splice (push (if head "<thead>" "<tbody>") html))
1899 (setq tbopen t)
1900 (while (setq line (pop lines))
1901 (catch 'next-line
1902 (if (string-match "^[ \t]*|-" line)
1903 (progn
1904 (unless splice
1905 (push (if head "</thead>" "</tbody>") html)
1906 (if lines (push "<tbody>" html) (setq tbopen nil)))
1907 (setq head nil) ;; head ends here, first time around
1908 ;; ignore this line
1909 (throw 'next-line t)))
1910 ;; Break the line into fields
1911 (setq fields (org-split-string line "[ \t]*|[ \t]*"))
1912 (unless fnum (setq fnum (make-vector (length fields) 0)
1913 nfields (length fnum)))
1914 (setq nline (1+ nline) i -1
1915 rowstart (eval (car org-export-table-row-tags))
1916 rowend (eval (cdr org-export-table-row-tags)))
1917 (push (concat rowstart
1918 (mapconcat
1919 (lambda (x)
1920 (setq i (1+ i) ali (format "@@class%03d@@" i))
1921 (if (and (< i nfields) ; make sure no rogue line causes an error here
1922 (string-match org-table-number-regexp x))
1923 (incf (aref fnum i)))
1924 (cond
1925 (head
1926 (concat
1927 (format (car org-export-table-header-tags)
1928 "col" ali)
1930 (cdr org-export-table-header-tags)))
1931 ((and (= i 0) org-export-html-table-use-header-tags-for-first-column)
1932 (concat
1933 (format (car org-export-table-header-tags)
1934 "row" ali)
1936 (cdr org-export-table-header-tags)))
1938 (concat (format (car org-export-table-data-tags) ali)
1940 (cdr org-export-table-data-tags)))))
1941 fields "")
1942 rowend)
1943 html)))
1944 (unless splice (if tbopen (push "</tbody>" html)))
1945 (unless splice (push "</table>\n" html))
1946 (setq html (nreverse html))
1947 (unless splice
1948 ;; Put in col tags with the alignment (unfortunately often ignored...)
1949 (unless (car org-table-colgroup-info)
1950 (setq org-table-colgroup-info
1951 (cons :start (cdr org-table-colgroup-info))))
1952 (setq i 0)
1953 (push (mapconcat
1954 (lambda (x)
1955 (setq gr (pop org-table-colgroup-info)
1956 i (1+ i)
1957 align (if (assoc i forced-aligns)
1958 (cdr (assoc (cdr (assoc i forced-aligns))
1959 '(("l" . "left") ("r" . "right")
1960 ("c" . "center"))))
1961 (if (> (/ (float x) nline)
1962 org-table-number-fraction)
1963 "right" "left")))
1964 (push align aligns)
1965 (format "%s<col align=\"%s\" />%s"
1966 (if (memq gr '(:start :startend))
1967 (prog1
1968 (if colgropen
1969 "</colgroup>\n<colgroup>"
1970 "<colgroup>")
1971 (setq colgropen t))
1973 align
1974 (if (memq gr '(:end :startend))
1975 (progn (setq colgropen nil) "</colgroup>")
1976 "")))
1977 fnum "")
1978 html)
1979 (setq aligns (nreverse aligns))
1980 (if colgropen (setq html (cons (car html)
1981 (cons "</colgroup>" (cdr html)))))
1982 ;; Since the output of HTML table formatter can also be used in
1983 ;; DocBook document, we want to always include the caption to make
1984 ;; DocBook XML file valid.
1985 (push (format "<caption>%s</caption>" (or caption "")) html)
1986 (when label (push (format "<a name=\"%s\" id=\"%s\"></a>" label label)
1987 html))
1988 (push html-table-tag html))
1989 (setq html (mapcar
1990 (lambda (x)
1991 (replace-regexp-in-string
1992 "@@class\\([0-9]+\\)@@"
1993 (lambda (txt)
1994 (if (not org-export-html-table-align-individual-fields)
1996 (setq n (string-to-number (match-string 1 txt)))
1997 (format " class=\"%s\""
1998 (or (nth n aligns) "left"))))
2000 html))
2001 (concat (mapconcat 'identity html "\n") "\n")))
2003 (defun org-export-splice-attributes (tag attributes)
2004 "Read attributes in string ATTRIBUTES, add and replace in HTML tag TAG."
2005 (if (not attributes)
2007 (let (oldatt newatt)
2008 (setq oldatt (org-extract-attributes-from-string tag)
2009 tag (pop oldatt)
2010 newatt (cdr (org-extract-attributes-from-string attributes)))
2011 (while newatt
2012 (setq oldatt (plist-put oldatt (pop newatt) (pop newatt))))
2013 (if (string-match ">" tag)
2014 (setq tag
2015 (replace-match (concat (org-attributes-to-string oldatt) ">")
2016 t t tag)))
2017 tag)))
2019 (defun org-format-table-table-html (lines)
2020 "Format a table generated by table.el into HTML.
2021 This conversion does *not* use `table-generate-source' from table.el.
2022 This has the advantage that Org-mode's HTML conversions can be used.
2023 But it has the disadvantage, that no cell- or row-spanning is allowed."
2024 (let (line field-buffer
2025 (head org-export-highlight-first-table-line)
2026 fields html empty i)
2027 (setq html (concat html-table-tag "\n"))
2028 (while (setq line (pop lines))
2029 (setq empty "&nbsp;")
2030 (catch 'next-line
2031 (if (string-match "^[ \t]*\\+-" line)
2032 (progn
2033 (if field-buffer
2034 (progn
2035 (setq
2036 html
2037 (concat
2038 html
2039 "<tr>"
2040 (mapconcat
2041 (lambda (x)
2042 (if (equal x "") (setq x empty))
2043 (if head
2044 (concat
2045 (format (car org-export-table-header-tags) "col" "")
2047 (cdr org-export-table-header-tags))
2048 (concat (format (car org-export-table-data-tags) "") x
2049 (cdr org-export-table-data-tags))))
2050 field-buffer "\n")
2051 "</tr>\n"))
2052 (setq head nil)
2053 (setq field-buffer nil)))
2054 ;; Ignore this line
2055 (throw 'next-line t)))
2056 ;; Break the line into fields and store the fields
2057 (setq fields (org-split-string line "[ \t]*|[ \t]*"))
2058 (if field-buffer
2059 (setq field-buffer (mapcar
2060 (lambda (x)
2061 (concat x "<br/>" (pop fields)))
2062 field-buffer))
2063 (setq field-buffer fields))))
2064 (setq html (concat html "</table>\n"))
2065 html))
2067 (defun org-format-table-table-html-using-table-generate-source (lines)
2068 "Format a table into html, using `table-generate-source' from table.el.
2069 This has the advantage that cell- or row-spanning is allowed.
2070 But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
2071 (require 'table)
2072 (with-current-buffer (get-buffer-create " org-tmp1 ")
2073 (erase-buffer)
2074 (insert (mapconcat 'identity lines "\n"))
2075 (goto-char (point-min))
2076 (if (not (re-search-forward "|[^+]" nil t))
2077 (error "Error processing table"))
2078 (table-recognize-table)
2079 (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
2080 (table-generate-source 'html " org-tmp2 ")
2081 (set-buffer " org-tmp2 ")
2082 (buffer-substring (point-min) (point-max))))
2084 (defun org-export-splice-style (style extra)
2085 "Splice EXTRA into STYLE, just before \"</style>\"."
2086 (if (and (stringp extra)
2087 (string-match "\\S-" extra)
2088 (string-match "</style>" style))
2089 (concat (substring style 0 (match-beginning 0))
2090 "\n" extra "\n"
2091 (substring style (match-beginning 0)))
2092 style))
2094 (defun org-html-handle-time-stamps (s)
2095 "Format time stamps in string S, or remove them."
2096 (catch 'exit
2097 (let (r b)
2098 (while (string-match org-maybe-keyword-time-regexp s)
2099 (or b (setq b (substring s 0 (match-beginning 0))))
2100 (setq r (concat
2101 r (substring s 0 (match-beginning 0))
2102 " @<span class=\"timestamp-wrapper\">"
2103 (if (match-end 1)
2104 (format "@<span class=\"timestamp-kwd\">%s @</span>"
2105 (match-string 1 s)))
2106 (format " @<span class=\"timestamp\">%s@</span>"
2107 (substring
2108 (org-translate-time (match-string 3 s)) 1 -1))
2109 "@</span>")
2110 s (substring s (match-end 0))))
2111 ;; Line break if line started and ended with time stamp stuff
2112 (if (not r)
2114 (setq r (concat r s))
2115 (unless (string-match "\\S-" (concat b s))
2116 (setq r (concat r "@<br/>")))
2117 r))))
2119 (defvar htmlize-buffer-places) ; from htmlize.el
2120 (defun org-export-htmlize-region-for-paste (beg end)
2121 "Convert the region to HTML, using htmlize.el.
2122 This is much like `htmlize-region-for-paste', only that it uses
2123 the settings define in the org-... variables."
2124 (let* ((htmlize-output-type org-export-htmlize-output-type)
2125 (htmlize-css-name-prefix org-export-htmlize-css-font-prefix)
2126 (htmlbuf (htmlize-region beg end)))
2127 (unwind-protect
2128 (with-current-buffer htmlbuf
2129 (buffer-substring (plist-get htmlize-buffer-places 'content-start)
2130 (plist-get htmlize-buffer-places 'content-end)))
2131 (kill-buffer htmlbuf))))
2133 ;;;###autoload
2134 (defun org-export-htmlize-generate-css ()
2135 "Create the CSS for all font definitions in the current Emacs session.
2136 Use this to create face definitions in your CSS style file that can then
2137 be used by code snippets transformed by htmlize.
2138 This command just produces a buffer that contains class definitions for all
2139 faces used in the current Emacs session. You can copy and paste the ones you
2140 need into your CSS file.
2142 If you then set `org-export-htmlize-output-type' to `css', calls to
2143 the function `org-export-htmlize-region-for-paste' will produce code
2144 that uses these same face definitions."
2145 (interactive)
2146 (require 'htmlize)
2147 (and (get-buffer "*html*") (kill-buffer "*html*"))
2148 (with-temp-buffer
2149 (let ((fl (face-list))
2150 (htmlize-css-name-prefix "org-")
2151 (htmlize-output-type 'css)
2152 f i)
2153 (while (setq f (pop fl)
2154 i (and f (face-attribute f :inherit)))
2155 (when (and (symbolp f) (or (not i) (not (listp i))))
2156 (insert (org-add-props (copy-sequence "1") nil 'face f))))
2157 (htmlize-region (point-min) (point-max))))
2158 (switch-to-buffer "*html*")
2159 (goto-char (point-min))
2160 (if (re-search-forward "<style" nil t)
2161 (delete-region (point-min) (match-beginning 0)))
2162 (if (re-search-forward "</style>" nil t)
2163 (delete-region (1+ (match-end 0)) (point-max)))
2164 (beginning-of-line 1)
2165 (if (looking-at " +") (replace-match ""))
2166 (goto-char (point-min)))
2168 (defun org-html-protect (s)
2169 "convert & to &amp;, < to &lt; and > to &gt;"
2170 (let ((start 0))
2171 (while (string-match "&" s start)
2172 (setq s (replace-match "&amp;" t t s)
2173 start (1+ (match-beginning 0))))
2174 (while (string-match "<" s)
2175 (setq s (replace-match "&lt;" t t s)))
2176 (while (string-match ">" s)
2177 (setq s (replace-match "&gt;" t t s)))
2178 ; (while (string-match "\"" s)
2179 ; (setq s (replace-match "&quot;" t t s)))
2183 (defun org-html-expand (string)
2184 "Prepare STRING for HTML export. Apply all active conversions.
2185 If there are links in the string, don't modify these."
2186 (let* ((re (concat org-bracket-link-regexp "\\|"
2187 (org-re "[ \t]+\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")))
2188 m s l res)
2189 (if (string-match "^[ \t]*\\+-[-+]*\\+[ \t]*$" string)
2190 string
2191 (while (setq m (string-match re string))
2192 (setq s (substring string 0 m)
2193 l (match-string 0 string)
2194 string (substring string (match-end 0)))
2195 (push (org-html-do-expand s) res)
2196 (push l res))
2197 (push (org-html-do-expand string) res)
2198 (apply 'concat (nreverse res)))))
2200 (defun org-html-do-expand (s)
2201 "Apply all active conversions to translate special ASCII to HTML."
2202 (setq s (org-html-protect s))
2203 (if org-export-html-expand
2204 (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
2205 (setq s (replace-match "<\\1>" t nil s))))
2206 (if org-export-with-emphasize
2207 (setq s (org-export-html-convert-emphasize s)))
2208 (if org-export-with-special-strings
2209 (setq s (org-export-html-convert-special-strings s)))
2210 (if org-export-with-sub-superscripts
2211 (setq s (org-export-html-convert-sub-super s)))
2212 (if org-export-with-TeX-macros
2213 (let ((start 0) wd rep)
2214 (while (setq start (string-match "\\\\\\([a-zA-Z]+[0-9]*\\)\\({}\\)?"
2215 s start))
2216 (if (get-text-property (match-beginning 0) 'org-protected s)
2217 (setq start (match-end 0))
2218 (setq wd (match-string 1 s))
2219 (if (setq rep (org-entity-get-representation wd 'html))
2220 (setq s (replace-match rep t t s))
2221 (setq start (+ start (length wd))))))))
2224 (defun org-export-html-convert-special-strings (string)
2225 "Convert special characters in STRING to HTML."
2226 (let ((all org-export-html-special-string-regexps)
2227 e a re rpl start)
2228 (while (setq a (pop all))
2229 (setq re (car a) rpl (cdr a) start 0)
2230 (while (string-match re string start)
2231 (if (get-text-property (match-beginning 0) 'org-protected string)
2232 (setq start (match-end 0))
2233 (setq string (replace-match rpl t nil string)))))
2234 string))
2236 (defun org-export-html-convert-sub-super (string)
2237 "Convert sub- and superscripts in STRING to HTML."
2238 (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
2239 (while (string-match org-match-substring-regexp string s)
2240 (cond
2241 ((and requireb (match-end 8)) (setq s (match-end 2)))
2242 ((get-text-property (match-beginning 2) 'org-protected string)
2243 (setq s (match-end 2)))
2245 (setq s (match-end 1)
2246 key (if (string= (match-string 2 string) "_") "sub" "sup")
2247 c (or (match-string 8 string)
2248 (match-string 6 string)
2249 (match-string 5 string))
2250 string (replace-match
2251 (concat (match-string 1 string)
2252 "<" key ">" c "</" key ">")
2253 t t string)))))
2254 (while (string-match "\\\\\\([_^]\\)" string)
2255 (setq string (replace-match (match-string 1 string) t t string)))
2256 string))
2258 (defun org-export-html-convert-emphasize (string)
2259 "Apply emphasis."
2260 (let ((s 0) rpl)
2261 (while (string-match org-emph-re string s)
2262 (if (not (equal
2263 (substring string (match-beginning 3) (1+ (match-beginning 3)))
2264 (substring string (match-beginning 4) (1+ (match-beginning 4)))))
2265 (setq s (match-beginning 0)
2267 (concat
2268 (match-string 1 string)
2269 (nth 2 (assoc (match-string 3 string) org-emphasis-alist))
2270 (match-string 4 string)
2271 (nth 3 (assoc (match-string 3 string)
2272 org-emphasis-alist))
2273 (match-string 5 string))
2274 string (replace-match rpl t t string)
2275 s (+ s (- (length rpl) 2)))
2276 (setq s (1+ s))))
2277 string))
2279 (defun org-open-par ()
2280 "Insert <p>, but first close previous paragraph if any."
2281 (org-close-par-maybe)
2282 (insert "\n<p>")
2283 (setq org-par-open t))
2284 (defun org-close-par-maybe ()
2285 "Close paragraph if there is one open."
2286 (when org-par-open
2287 (insert "</p>")
2288 (setq org-par-open nil)))
2289 (defun org-close-li (&optional type)
2290 "Close <li> if necessary."
2291 (org-close-par-maybe)
2292 (insert (if (equal type "d") "</dd>\n" "</li>\n")))
2294 (defvar in-local-list)
2295 (defvar local-list-indent)
2296 (defvar local-list-type)
2298 (defvar body-only) ; dynamically scoped into this.
2299 (defun org-html-level-start (level title umax with-toc head-count)
2300 "Insert a new level in HTML export.
2301 When TITLE is nil, just close all open levels."
2302 (org-close-par-maybe)
2303 (let* ((target (and title (org-get-text-property-any 0 'target title)))
2304 (extra-targets (and target
2305 (assoc target org-export-target-aliases)))
2306 (extra-class (and title (org-get-text-property-any 0 'html-container-class title)))
2307 (preferred (and target
2308 (cdr (assoc target org-export-preferred-target-alist))))
2309 (remove (or preferred target))
2310 (l org-level-max)
2311 snumber snu href suffix)
2312 (setq extra-targets (remove remove extra-targets))
2313 (setq extra-targets
2314 (mapconcat (lambda (x)
2315 (if (org-uuidgen-p x) (setq x (concat "ID-" x)))
2316 (format "<a name=\"%s\" id=\"%s\"></a>"
2317 x x))
2318 extra-targets
2319 ""))
2320 (while (>= l level)
2321 (if (aref org-levels-open (1- l))
2322 (progn
2323 (org-html-level-close l umax)
2324 (aset org-levels-open (1- l) nil)))
2325 (setq l (1- l)))
2326 (when title
2327 ;; If title is nil, this means this function is called to close
2328 ;; all levels, so the rest is done only if title is given
2329 (when (string-match (org-re "\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") title)
2330 (setq title (replace-match
2331 (if org-export-with-tags
2332 (save-match-data
2333 (concat
2334 "&nbsp;&nbsp;&nbsp;<span class=\"tag\">"
2335 (mapconcat
2336 (lambda (x)
2337 (format "<span class=\"%s\">%s</span>"
2338 (org-export-html-get-tag-class-name x)
2340 (org-split-string (match-string 1 title) ":")
2341 "&nbsp;")
2342 "</span>"))
2344 t t title)))
2345 (if (> level umax)
2346 (progn
2347 (if (aref org-levels-open (1- level))
2348 (progn
2349 (org-close-li)
2350 (if target
2351 (insert (format "<li id=\"%s\">" target) extra-targets title "<br/>\n")
2352 (insert "<li>" title "<br/>\n")))
2353 (aset org-levels-open (1- level) t)
2354 (org-close-par-maybe)
2355 (if target
2356 (insert (format "<ul>\n<li id=\"%s\">" target)
2357 extra-targets title "<br/>\n")
2358 (insert "<ul>\n<li>" title "<br/>\n"))))
2359 (aset org-levels-open (1- level) t)
2360 (setq snumber (org-section-number level)
2361 snu (replace-regexp-in-string "\\." "_" snumber))
2362 (setq level (+ level org-export-html-toplevel-hlevel -1))
2363 (if (and org-export-with-section-numbers (not body-only))
2364 (setq title (concat
2365 (format "<span class=\"section-number-%d\">%s</span>"
2366 level snumber)
2367 " " title)))
2368 (unless (= head-count 1) (insert "\n</div>\n"))
2369 (setq href (cdr (assoc (concat "sec-" snu) org-export-preferred-target-alist)))
2370 (setq suffix (or href snu))
2371 (setq href (or href (concat "sec-" snu)))
2372 (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"
2373 suffix level (if extra-class (concat " " extra-class) "")
2374 level href
2375 extra-targets
2376 title level level suffix))
2377 (org-open-par)))))
2379 (defun org-export-html-get-tag-class-name (tag)
2380 "Turn tag into a valid class name.
2381 Replaces invalid characters with \"_\" and then prepends a prefix."
2382 (save-match-data
2383 (while (string-match "[^a-zA-Z0-9_]" tag)
2384 (setq tag (replace-match "_" t t tag))))
2385 (concat org-export-html-tag-class-prefix tag))
2387 (defun org-export-html-get-todo-kwd-class-name (kwd)
2388 "Turn todo keyword into a valid class name.
2389 Replaces invalid characters with \"_\" and then prepends a prefix."
2390 (save-match-data
2391 (while (string-match "[^a-zA-Z0-9_]" kwd)
2392 (setq kwd (replace-match "_" t t kwd))))
2393 (concat org-export-html-todo-kwd-class-prefix kwd))
2395 (defun org-html-level-close (level max-outline-level)
2396 "Terminate one level in HTML export."
2397 (if (<= level max-outline-level)
2398 (insert "</div>\n")
2399 (org-close-li)
2400 (insert "</ul>\n")))
2402 (provide 'org-html)
2404 ;; arch-tag: 8109d84d-eb8f-460b-b1a8-f45f3a6c7ea1
2405 ;;; org-html.el ends here