Fix small regressions in Org
[bpt/emacs.git] / lisp / org / org-html.el
CommitLineData
c8d0cf5c
CD
1;;; org-html.el --- HTML export for Org-mode
2
b73f1974 3;; Copyright (C) 2004-2012 Free Software Foundation, Inc.
c8d0cf5c
CD
4
5;; Author: Carsten Dominik <carsten at orgmode dot org>
6;; Keywords: outlines, hypermedia, calendar, wp
7;; Homepage: http://orgmode.org
c8d0cf5c
CD
8;;
9;; This file is part of GNU Emacs.
10;;
11;; GNU Emacs is free software: you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
24;;
25;;; Commentary:
26
86fbb8ca
CD
27;;; Code:
28
c8d0cf5c 29(require 'org-exp)
3ab2c837 30(require 'format-spec)
86fbb8ca 31
54a0dee5 32(eval-when-compile (require 'cl))
c8d0cf5c
CD
33
34(declare-function org-id-find-id-file "org-id" (id))
35(declare-function htmlize-region "ext:htmlize" (beg end))
e66ba1df
BG
36(declare-function org-pop-to-buffer-same-window
37 "org-compat" (&optional buffer-or-name norecord label))
c8d0cf5c
CD
38
39(defgroup org-export-html nil
40 "Options specific for HTML export of Org-mode files."
41 :tag "Org Export HTML"
42 :group 'org-export)
43
44(defcustom org-export-html-footnotes-section "<div id=\"footnotes\">
45<h2 class=\"footnotes\">%s: </h2>
46<div id=\"text-footnotes\">
47%s
48</div>
49</div>"
50 "Format for the footnotes section.
51Should contain a two instances of %s. The first will be replaced with the
52language-specific word for \"Footnotes\", the second one will be replaced
53by the footnotes themselves."
54 :group 'org-export-html
55 :type 'string)
56
57(defcustom org-export-html-footnote-format "<sup>%s</sup>"
58 "The format for the footnote reference.
59%s will be replaced by the footnote reference itself."
60 :group 'org-export-html
61 :type 'string)
62
3ab2c837
BG
63
64(defcustom org-export-html-footnote-separator "<sup>, </sup>"
65 "Text used to separate footnotes."
66 :group 'org-export-html
67 :type 'string)
68
c8d0cf5c 69(defcustom org-export-html-coding-system nil
86fbb8ca 70 "Coding system for HTML export, defaults to `buffer-file-coding-system'."
c8d0cf5c
CD
71 :group 'org-export-html
72 :type 'coding-system)
73
74(defcustom org-export-html-extension "html"
75 "The extension for exported HTML files."
76 :group 'org-export-html
77 :type 'string)
78
79(defcustom org-export-html-xml-declaration
80 '(("html" . "<?xml version=\"1.0\" encoding=\"%s\"?>")
81 ("php" . "<?php echo \"<?xml version=\\\"1.0\\\" encoding=\\\"%s\\\" ?>\"; ?>"))
82 "The extension for exported HTML files.
83%s will be replaced with the charset of the exported file.
84This may be a string, or an alist with export extensions
85and corresponding declarations."
86 :group 'org-export-html
87 :type '(choice
88 (string :tag "Single declaration")
89 (repeat :tag "Dependent on extension"
90 (cons (string :tag "Extension")
91 (string :tag "Declaration")))))
92
93(defcustom org-export-html-style-include-scripts t
86fbb8ca 94 "Non-nil means include the JavaScript snippets in exported HTML files.
c8d0cf5c
CD
95The actual script is defined in `org-export-html-scripts' and should
96not be modified."
97 :group 'org-export-html
98 :type 'boolean)
99
100(defconst org-export-html-scripts
101"<script type=\"text/javascript\">
102<!--/*--><![CDATA[/*><!--*/
103 function CodeHighlightOn(elem, id)
104 {
105 var target = document.getElementById(id);
106 if(null != target) {
107 elem.cacheClassElem = elem.className;
108 elem.cacheClassTarget = target.className;
109 target.className = \"code-highlighted\";
110 elem.className = \"code-highlighted\";
111 }
112 }
113 function CodeHighlightOff(elem, id)
114 {
115 var target = document.getElementById(id);
116 if(elem.cacheClassElem)
117 elem.className = elem.cacheClassElem;
118 if(elem.cacheClassTarget)
119 target.className = elem.cacheClassTarget;
120 }
121/*]]>*///-->
122</script>"
86fbb8ca 123"Basic JavaScript that is needed by HTML files produced by Org-mode.")
c8d0cf5c
CD
124
125(defconst org-export-html-style-default
126"<style type=\"text/css\">
127 <!--/*--><![CDATA[/*><!--*/
128 html { font-family: Times, serif; font-size: 12pt; }
129 .title { text-align: center; }
130 .todo { color: red; }
131 .done { color: green; }
132 .tag { background-color: #add8e6; font-weight:normal }
133 .target { }
134 .timestamp { color: #bebebe; }
135 .timestamp-kwd { color: #5f9ea0; }
afe98dfa
CD
136 .right {margin-left:auto; margin-right:0px; text-align:right;}
137 .left {margin-left:0px; margin-right:auto; text-align:left;}
138 .center {margin-left:auto; margin-right:auto; text-align:center;}
c8d0cf5c
CD
139 p.verse { margin-left: 3% }
140 pre {
141 border: 1pt solid #AEBDCC;
142 background-color: #F3F5F7;
143 padding: 5pt;
144 font-family: courier, monospace;
145 font-size: 90%;
146 overflow:auto;
147 }
148 table { border-collapse: collapse; }
afe98dfa
CD
149 td, th { vertical-align: top; }
150 th.right { text-align:center; }
151 th.left { text-align:center; }
152 th.center { text-align:center; }
153 td.right { text-align:right; }
154 td.left { text-align:left; }
155 td.center { text-align:center; }
c8d0cf5c
CD
156 dt { font-weight: bold; }
157 div.figure { padding: 0.5em; }
158 div.figure p { text-align: center; }
e66ba1df
BG
159 div.inlinetask {
160 padding:10px;
161 border:2px solid gray;
162 margin:10px;
163 background: #ffffcc;
164 }
ed21c5c8 165 textarea { overflow-x: auto; }
c8d0cf5c
CD
166 .linenr { font-size:smaller }
167 .code-highlighted {background-color:#ffff00;}
168 .org-info-js_info-navigation { border-style:none; }
169 #org-info-js_console-label { font-size:10px; font-weight:bold;
170 white-space:nowrap; }
171 .org-info-js_search-highlight {background-color:#ffff00; color:#000000;
172 font-weight:bold; }
173 /*]]>*/-->
174</style>"
175 "The default style specification for exported HTML files.
176Please use the variables `org-export-html-style' and
177`org-export-html-style-extra' to add to this style. If you wish to not
178have the default style included, customize the variable
179`org-export-html-style-include-default'.")
180
181(defcustom org-export-html-style-include-default t
ed21c5c8 182 "Non-nil means include the default style in exported HTML files.
c8d0cf5c
CD
183The actual style is defined in `org-export-html-style-default' and should
184not be modified. Use the variables `org-export-html-style' to add
185your own style information."
186 :group 'org-export-html
187 :type 'boolean)
188;;;###autoload
364bc556 189(put 'org-export-html-style-include-default 'safe-local-variable 'booleanp)
c8d0cf5c
CD
190
191(defcustom org-export-html-style ""
192 "Org-wide style definitions for exported HTML files.
193
194This variable needs to contain the full HTML structure to provide a style,
195including the surrounding HTML tags. If you set the value of this variable,
196you should consider to include definitions for the following classes:
197 title, todo, done, timestamp, timestamp-kwd, tag, target.
198
199For example, a valid value would be:
200
201 <style type=\"text/css\">
202 <![CDATA[
203 p { font-weight: normal; color: gray; }
204 h1 { color: black; }
205 .title { text-align: center; }
206 .todo, .timestamp-kwd { color: red; }
207 .done { color: green; }
208 ]]>
209 </style>
210
3ab2c837 211If you'd like to refer to an external style file, use something like
c8d0cf5c
CD
212
213 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
214
215As the value of this option simply gets inserted into the HTML <head> header,
216you can \"misuse\" it to add arbitrary text to the header.
217See also the variable `org-export-html-style-extra'."
218 :group 'org-export-html
219 :type 'string)
220;;;###autoload
221(put 'org-export-html-style 'safe-local-variable 'stringp)
222
223(defcustom org-export-html-style-extra ""
224 "Additional style information for HTML export.
225The value of this variable is inserted into the HTML buffer right after
226the value of `org-export-html-style'. Use this variable for per-file
227settings of style information, and do not forget to surround the style
228settings with <style>...</style> tags."
229 :group 'org-export-html
230 :type 'string)
231;;;###autoload
232(put 'org-export-html-style-extra 'safe-local-variable 'stringp)
233
afe98dfa
CD
234(defcustom org-export-html-mathjax-options
235 '((path "http://orgmode.org/mathjax/MathJax.js")
236 (scale "100")
237 (align "center")
238 (indent "2em")
239 (mathml nil))
240 "Options for MathJax setup.
241
242path The path where to find MathJax
243scale Scaling for the HTML-CSS backend, usually between 100 and 133
244align How to align display math: left, center, or right
245indent If align is not center, how far from the left/right side?
246mathml Should a MathML player be used if available?
247 This is faster and reduces bandwidth use, but currently
248 sometimes has lower spacing quality. Therefore, the default is
249 nil. When browsers get better, this switch can be flipped.
250
251You can also customize this for each buffer, using something like
252
253#+MATHJAX: scale:\"133\" align:\"right\" mathml:t path:\"/MathJax/\""
254 :group 'org-export-html
255 :type '(list :greedy t
256 (list :tag "path (the path from where to load MathJax.js)"
257 (const :format " " path) (string))
258 (list :tag "scale (scaling for the displayed math)"
259 (const :format " " scale) (string))
260 (list :tag "align (alignment of displayed equations)"
261 (const :format " " align) (string))
262 (list :tag "indent (indentation with left or right alignment)"
263 (const :format " " indent) (string))
264 (list :tag "mathml (should MathML display be used is possible)"
265 (const :format " " mathml) (boolean))))
266
267(defun org-export-html-mathjax-config (template options in-buffer)
268 "Insert the user setup into the matchjax template."
269 (let (name val (yes " ") (no "// ") x)
270 (mapc
271 (lambda (e)
272 (setq name (car e) val (nth 1 e))
273 (if (string-match (concat "\\<" (symbol-name name) ":") in-buffer)
274 (setq val (car (read-from-string
275 (substring in-buffer (match-end 0))))))
276 (if (not (stringp val)) (setq val (format "%s" val)))
277 (if (string-match (concat "%" (upcase (symbol-name name))) template)
278 (setq template (replace-match val t t template))))
279 options)
280 (setq val (nth 1 (assq 'mathml options)))
281 (if (string-match (concat "\\<mathml:") in-buffer)
282 (setq val (car (read-from-string
283 (substring in-buffer (match-end 0))))))
284 ;; Exchange prefixes depending on mathml setting
285 (if (not val) (setq x yes yes no no x))
286 ;; Replace cookies to turn on or off the config/jax lines
287 (if (string-match ":MMLYES:" template)
288 (setq template (replace-match yes t t template)))
289 (if (string-match ":MMLNO:" template)
290 (setq template (replace-match no t t template)))
291 ;; Return the modified template
292 template))
293
294(defcustom org-export-html-mathjax-template
295 "<script type=\"text/javascript\" src=\"%PATH\">
296<!--/*--><![CDATA[/*><!--*/
297 MathJax.Hub.Config({
298 // Only one of the two following lines, depending on user settings
299 // First allows browser-native MathML display, second forces HTML/CSS
300 :MMLYES: config: [\"MMLorHTML.js\"], jax: [\"input/TeX\"],
301 :MMLNO: jax: [\"input/TeX\", \"output/HTML-CSS\"],
302 extensions: [\"tex2jax.js\",\"TeX/AMSmath.js\",\"TeX/AMSsymbols.js\",
303 \"TeX/noUndefined.js\"],
304 tex2jax: {
305 inlineMath: [ [\"\\\\(\",\"\\\\)\"] ],
3ab2c837 306 displayMath: [ ['$$','$$'], [\"\\\\[\",\"\\\\]\"], [\"\\\\begin{displaymath}\",\"\\\\end{displaymath}\"] ],
afe98dfa
CD
307 skipTags: [\"script\",\"noscript\",\"style\",\"textarea\",\"pre\",\"code\"],
308 ignoreClass: \"tex2jax_ignore\",
309 processEscapes: false,
310 processEnvironments: true,
311 preview: \"TeX\"
312 },
313 showProcessingMessages: true,
314 displayAlign: \"%ALIGN\",
315 displayIndent: \"%INDENT\",
316
317 \"HTML-CSS\": {
318 scale: %SCALE,
319 availableFonts: [\"STIX\",\"TeX\"],
320 preferredFont: \"TeX\",
321 webFont: \"TeX\",
322 imageFont: \"TeX\",
323 showMathMenu: true,
324 },
325 MMLorHTML: {
326 prefer: {
327 MSIE: \"MML\",
328 Firefox: \"MML\",
329 Opera: \"HTML\",
330 other: \"HTML\"
331 }
332 }
333 });
334/*]]>*///-->
335</script>"
336 "The MathJax setup for XHTML files."
337 :group 'org-export-html
338 :type 'string)
339
c8d0cf5c 340(defcustom org-export-html-tag-class-prefix ""
86fbb8ca 341 "Prefix to class names for TODO keywords.
c8d0cf5c
CD
342Each tag gets a class given by the tag itself, with this prefix.
343The default prefix is empty because it is nice to just use the keyword
344as a class name. But if you get into conflicts with other, existing
86fbb8ca 345CSS classes, then this prefix can be very useful."
c8d0cf5c
CD
346 :group 'org-export-html
347 :type 'string)
348
349(defcustom org-export-html-todo-kwd-class-prefix ""
86fbb8ca 350 "Prefix to class names for TODO keywords.
c8d0cf5c
CD
351Each TODO keyword gets a class given by the keyword itself, with this prefix.
352The default prefix is empty because it is nice to just use the keyword
353as a class name. But if you get into conflicts with other, existing
86fbb8ca 354CSS classes, then this prefix can be very useful."
c8d0cf5c
CD
355 :group 'org-export-html
356 :type 'string)
357
e66ba1df
BG
358(defcustom org-export-html-headline-anchor-format "<a name=\"%s\" id=\"%s\"></a>"
359 "Format for anchors in HTML headlines.
360It requires to %s: both will be replaced by the anchor referring
361to the headline (e.g. \"sec-2\"). When set to `nil', don't insert
362HTML anchors in headlines."
363 :group 'org-export-html
364 :type 'string)
365
3ab2c837
BG
366(defcustom org-export-html-preamble t
367 "Non-nil means insert a preamble in HTML export.
368
369When `t', insert a string as defined by one of the formatting
370strings in `org-export-html-preamble-format'. When set to a
371string, this string overrides `org-export-html-preamble-format'.
372When set to a function, apply this function and insert the
e66ba1df
BG
373returned string. The function takes no argument, but you can
374use `opt-plist' to access the current export options.
3ab2c837
BG
375
376Setting :html-preamble in publishing projects will take
377precedence over this variable."
378 :group 'org-export-html
379 :type '(choice (const :tag "No preamble" nil)
380 (const :tag "Default preamble" t)
381 (string :tag "Custom formatting string")
382 (function :tag "Function (must return a string)")))
383
384(defcustom org-export-html-preamble-format '(("en" ""))
385 "The format for the HTML preamble.
386
387%t stands for the title.
388%a stands for the author's name.
389%e stands for the author's email.
390%d stands for the date.
391
392If you need to use a \"%\" character, you need to escape it
393like that: \"%%\"."
394 :group 'org-export-html
395 :type 'string)
396
397(defcustom org-export-html-postamble 'auto
398 "Non-nil means insert a postamble in HTML export.
399
400When `t', insert a string as defined by the formatting string in
401`org-export-html-postamble-format'. When set to a string, this
402string overrides `org-export-html-postamble-format'. When set to
403'auto, discard `org-export-html-postamble-format' and honor
404`org-export-author/email/creator-info' variables. When set to a
405function, apply this function and insert the returned string.
e66ba1df
BG
406The function takes no argument, but you can use `opt-plist' to
407access the current export options.
3ab2c837
BG
408
409Setting :html-postamble in publishing projects will take
410precedence over this variable."
411 :group 'org-export-html
412 :type '(choice (const :tag "No postamble" nil)
413 (const :tag "Auto preamble" 'auto)
414 (const :tag "Default formatting string" t)
415 (string :tag "Custom formatting string")
416 (function :tag "Function (must return a string)")))
417
418(defcustom org-export-html-postamble-format
419 '(("en" "<p class=\"author\">Author: %a (%e)</p>
420<p class=\"date\">Date: %d</p>
421<p class=\"creator\">Generated by %c</p>
422<p class=\"xhtml-validation\">%v</p>
423"))
424 "The format for the HTML postamble.
425
426%a stands for the author's name.
427%e stands for the author's email.
428%d stands for the date.
429%c will be replaced by information about Org/Emacs versions.
430%v will be replaced by `org-export-html-validation-link'.
431
432If you need to use a \"%\" character, you need to escape it
433like that: \"%%\"."
c8d0cf5c
CD
434 :group 'org-export-html
435 :type 'string)
436
437(defcustom org-export-html-home/up-format
5dec9555 438 "<div id=\"org-div-home-and-up\" style=\"text-align:right;font-size:70%%;white-space:nowrap;\">
c8d0cf5c
CD
439 <a accesskey=\"h\" href=\"%s\"> UP </a>
440 |
441 <a accesskey=\"H\" href=\"%s\"> HOME </a>
442</div>"
86fbb8ca
CD
443 "Snippet used to insert the HOME and UP links.
444This is a format string, the first %s will receive the UP link,
445the second the HOME link. If both `org-export-html-link-up' and
446`org-export-html-link-home' are empty, the entire snippet will be
447ignored."
c8d0cf5c
CD
448 :group 'org-export-html
449 :type 'string)
450
451(defcustom org-export-html-toplevel-hlevel 2
452 "The <H> level for level 1 headings in HTML export.
453This is also important for the classes that will be wrapped around headlines
454and outline structure. If this variable is 1, the top-level headlines will
455be <h1>, and the corresponding classes will be outline-1, section-number-1,
456and outline-text-1. If this is 2, all of these will get a 2 instead.
457The default for this variable is 2, because we use <h1> for formatting the
458document title."
459 :group 'org-export-html
460 :type 'string)
461
462(defcustom org-export-html-link-org-files-as-html t
ed21c5c8 463 "Non-nil means make file links to `file.org' point to `file.html'.
c8d0cf5c
CD
464When org-mode is exporting an org-mode file to HTML, links to
465non-html files are directly put into a href tag in HTML.
466However, links to other Org-mode files (recognized by the
467extension `.org.) should become links to the corresponding html
468file, assuming that the linked org-mode file will also be
469converted to HTML.
470When nil, the links still point to the plain `.org' file."
471 :group 'org-export-html
472 :type 'boolean)
473
474(defcustom org-export-html-inline-images 'maybe
ed21c5c8 475 "Non-nil means inline images into exported HTML pages.
c8d0cf5c
CD
476This is done using an <img> tag. When nil, an anchor with href is used to
477link to the image. If this option is `maybe', then images in links with
478an empty description will be inlined, while images with a description will
479be linked only."
480 :group 'org-export-html
481 :type '(choice (const :tag "Never" nil)
482 (const :tag "Always" t)
483 (const :tag "When there is no description" maybe)))
484
485(defcustom org-export-html-inline-image-extensions
afe98dfa 486 '("png" "jpeg" "jpg" "gif" "svg")
c8d0cf5c
CD
487 "Extensions of image files that can be inlined into HTML."
488 :group 'org-export-html
489 :type '(repeat (string :tag "Extension")))
490
491(defcustom org-export-html-table-tag
492 "<table border=\"2\" cellspacing=\"0\" cellpadding=\"6\" rules=\"groups\" frame=\"hsides\">"
493 "The HTML tag that is used to start a table.
494This must be a <table> tag, but you may change the options like
495borders and spacing."
496 :group 'org-export-html
497 :type 'string)
498
afe98dfa 499(defcustom org-export-table-header-tags '("<th scope=\"%s\"%s>" . "</th>")
c8d0cf5c
CD
500 "The opening tag for table header fields.
501This is customizable so that alignment options can be specified.
afe98dfa
CD
502The first %s will be filled with the scope of the field, either row or col.
503The second %s will be replaced by a style entry to align the field.
504See also the variable `org-export-html-table-use-header-tags-for-first-column'.
505See also the variable `org-export-html-table-align-individual-fields'."
c8d0cf5c
CD
506 :group 'org-export-tables
507 :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
508
afe98dfa 509(defcustom org-export-table-data-tags '("<td%s>" . "</td>")
c8d0cf5c 510 "The opening tag for table data fields.
afe98dfa
CD
511This is customizable so that alignment options can be specified.
512The first %s will be filled with the scope of the field, either row or col.
513The second %s will be replaced by a style entry to align the field.
514See also the variable `org-export-html-table-align-individual-fields'."
c8d0cf5c
CD
515 :group 'org-export-tables
516 :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
517
518(defcustom org-export-table-row-tags '("<tr>" . "</tr>")
519 "The opening tag for table data fields.
520This is customizable so that alignment options can be specified.
521Instead of strings, these can be Lisp forms that will be evaluated
522for each row in order to construct the table row tags. During evaluation,
523the variable `head' will be true when this is a header line, nil when this
524is a body line. And the variable `nline' will contain the line number,
525starting from 1 in the first header line. For example
526
527 (setq org-export-table-row-tags
528 (cons '(if head
529 \"<tr>\"
530 (if (= (mod nline 2) 1)
531 \"<tr class=\\\"tr-odd\\\">\"
532 \"<tr class=\\\"tr-even\\\">\"))
533 \"</tr>\"))
534
535will give even lines the class \"tr-even\" and odd lines the class \"tr-odd\"."
536 :group 'org-export-tables
537 :type '(cons
538 (choice :tag "Opening tag"
539 (string :tag "Specify")
540 (sexp))
541 (choice :tag "Closing tag"
542 (string :tag "Specify")
543 (sexp))))
544
afe98dfa
CD
545(defcustom org-export-html-table-align-individual-fields t
546 "Non-nil means attach style attributes for alignment to each table field.
547When nil, alignment will only be specified in the column tags, but this
548is ignored by some browsers (like Firefox, Safari). Opera does it right
549though."
550 :group 'org-export-tables
551 :type 'boolean)
c8d0cf5c
CD
552
553(defcustom org-export-html-table-use-header-tags-for-first-column nil
ed21c5c8 554 "Non-nil means format column one in tables with header tags.
c8d0cf5c
CD
555When nil, also column one will use data tags."
556 :group 'org-export-tables
557 :type 'boolean)
558
3ab2c837
BG
559(defcustom org-export-html-validation-link
560 "<a href=\"http://validator.w3.org/check?uri=referer\">Validate XHTML 1.0</a>"
561 "Link to HTML validation service."
c8d0cf5c 562 :group 'org-export-html
3ab2c837 563 :type 'string)
c8d0cf5c 564
3ab2c837
BG
565;; FIXME Obsolete since Org 7.7
566;; Use the :timestamp option or `org-export-time-stamp-file' instead
567(defvar org-export-html-with-timestamp nil
568 "If non-nil, write container for HTML-helper-mode timestamp.")
569
570;; FIXME Obsolete since Org 7.7
571(defvar org-export-html-html-helper-timestamp
572 "\n<p><br/><br/>\n<!-- hhmts start --> <!-- hhmts end --></p>\n"
573 "The HTML tag used as timestamp delimiter for HTML-helper-mode.")
574
575(defcustom org-export-html-protect-char-alist
576 '(("&" . "&amp;")
577 ("<" . "&lt;")
578 (">" . "&gt;"))
579 "Alist of characters to be converted by `org-html-protect'."
c8d0cf5c 580 :group 'org-export-html
3ab2c837
BG
581 :type '(repeat (cons (string :tag "Character")
582 (string :tag "HTML equivalent"))))
c8d0cf5c
CD
583
584(defgroup org-export-htmlize nil
585 "Options for processing examples with htmlize.el."
586 :tag "Org Export Htmlize"
587 :group 'org-export-html)
588
589(defcustom org-export-htmlize-output-type 'inline-css
590 "Output type to be used by htmlize when formatting code snippets.
3ab2c837
BG
591Choices are `css', to export the CSS selectors only, or `inline-css', to
592export the CSS attribute values inline in the HTML. We use as default
593`inline-css', in order to make the resulting HTML self-containing.
594
c8d0cf5c
CD
595However, this will fail when using Emacs in batch mode for export, because
596then no rich font definitions are in place. It will also not be good if
597people with different Emacs setup contribute HTML files to a website,
598because the fonts will represent the individual setups. In these cases,
599it is much better to let Org/Htmlize assign classes only, and to use
600a style file to define the look of these classes.
601To get a start for your css file, start Emacs session and make sure that
602all the faces you are interested in are defined, for example by loading files
603in all modes you want. Then, use the command
604\\[org-export-htmlize-generate-css] to extract class definitions."
605 :group 'org-export-htmlize
606 :type '(choice (const css) (const inline-css)))
607
608(defcustom org-export-htmlize-css-font-prefix "org-"
609 "The prefix for CSS class names for htmlize font specifications."
610 :group 'org-export-htmlize
611 :type 'string)
612
613(defcustom org-export-htmlized-org-css-url nil
614 "URL pointing to a CSS file defining text colors for htmlized Emacs buffers.
615Normally when creating an htmlized version of an Org buffer, htmlize will
616create CSS to define the font colors. However, this does not work when
617converting in batch mode, and it also can look bad if different people
618with different fontification setup work on the same website.
619When this variable is non-nil, creating an htmlized version of an Org buffer
620using `org-export-as-org' will remove the internal CSS section and replace it
621with a link to this URL."
622 :group 'org-export-htmlize
623 :type '(choice
624 (const :tag "Keep internal css" nil)
625 (string :tag "URL or local href")))
626
3ab2c837
BG
627;; FIXME: The following variable is obsolete since Org 7.7 but is
628;; still declared and checked within code for compatibility reasons.
629;; Use the custom variables `org-export-html-divs' instead.
630(defvar org-export-html-content-div "content"
631 "The name of the container DIV that holds all the page contents.
632
633This variable is obsolete since Org version 7.7.
634Please set `org-export-html-divs' instead.")
c8d0cf5c 635
3ab2c837 636(defcustom org-export-html-divs '("preamble" "content" "postamble")
e66ba1df
BG
637 "The name of the main divs for HTML export.
638This is a list of three strings, the first one for the preamble
639DIV, the second one for the content DIV and the third one for the
640postamble DIV."
3ab2c837
BG
641 :group 'org-export-html
642 :type '(list
643 (string :tag " Div for the preamble:")
644 (string :tag " Div for the content:")
645 (string :tag "Div for the postamble:")))
c8d0cf5c
CD
646
647;;; Hooks
648
649(defvar org-export-html-after-blockquotes-hook nil
650 "Hook run during HTML export, after blockquote, verse, center are done.")
651
8d642074 652(defvar org-export-html-final-hook nil
ed21c5c8 653 "Hook run at the end of HTML export, in the new buffer.")
8d642074 654
c8d0cf5c
CD
655;;; HTML export
656
657(defun org-export-html-preprocess (parameters)
86fbb8ca 658 "Convert LaTeX fragments to images."
c8d0cf5c
CD
659 (when (and org-current-export-file
660 (plist-get parameters :LaTeX-fragments))
661 (org-format-latex
662 (concat "ltxpng/" (file-name-sans-extension
663 (file-name-nondirectory
664 org-current-export-file)))
86fbb8ca 665 org-current-export-dir nil "Creating LaTeX image %s"
afe98dfa
CD
666 nil nil
667 (cond
668 ((eq (plist-get parameters :LaTeX-fragments) 'verbatim) 'verbatim)
669 ((eq (plist-get parameters :LaTeX-fragments) 'mathjax ) 'mathjax)
670 ((eq (plist-get parameters :LaTeX-fragments) t ) 'mathjax)
671 ((eq (plist-get parameters :LaTeX-fragments) 'dvipng ) 'dvipng)
672 (t nil))))
ed21c5c8
CD
673 (goto-char (point-min))
674 (let (label l1)
675 (while (re-search-forward "\\\\ref{\\([^{}\n]+\\)}" nil t)
676 (org-if-unprotected-at (match-beginning 1)
677 (setq label (match-string 1))
678 (save-match-data
679 (if (string-match "\\`[a-z]\\{1,10\\}:\\(.+\\)" label)
680 (setq l1 (substring label (match-beginning 1)))
681 (setq l1 label)))
682 (replace-match (format "[[#%s][%s]]" label l1) t t)))))
c8d0cf5c
CD
683
684;;;###autoload
685(defun org-export-as-html-and-open (arg)
686 "Export the outline as HTML and immediately open it with a browser.
687If there is an active region, export only the region.
688The prefix ARG specifies how many levels of the outline should become
689headlines. The default is 3. Lower levels will become bulleted lists."
690 (interactive "P")
691 (org-export-as-html arg 'hidden)
ed21c5c8
CD
692 (org-open-file buffer-file-name)
693 (when org-export-kill-product-buffer-when-displayed
86fbb8ca 694 (kill-buffer (current-buffer))))
c8d0cf5c
CD
695
696;;;###autoload
697(defun org-export-as-html-batch ()
86fbb8ca
CD
698 "Call the function `org-export-as-html'.
699This function can be used in batch processing as:
c8d0cf5c
CD
700emacs --batch
701 --load=$HOME/lib/emacs/org.el
702 --eval \"(setq org-export-headline-levels 2)\"
703 --visit=MyFile --funcall org-export-as-html-batch"
704 (org-export-as-html org-export-headline-levels 'hidden))
705
706;;;###autoload
707(defun org-export-as-html-to-buffer (arg)
708 "Call `org-export-as-html` with output to a temporary buffer.
709No file is created. The prefix ARG is passed through to `org-export-as-html'."
710 (interactive "P")
711 (org-export-as-html arg nil nil "*Org HTML Export*")
712 (when org-export-show-temporary-export-buffer
713 (switch-to-buffer-other-window "*Org HTML Export*")))
714
715;;;###autoload
716(defun org-replace-region-by-html (beg end)
717 "Assume the current region has org-mode syntax, and convert it to HTML.
718This can be used in any buffer. For example, you could write an
719itemized list in org-mode syntax in an HTML buffer and then use this
720command to convert it."
721 (interactive "r")
722 (let (reg html buf pop-up-frames)
723 (save-window-excursion
e66ba1df 724 (if (eq major-mode 'org-mode)
c8d0cf5c
CD
725 (setq html (org-export-region-as-html
726 beg end t 'string))
727 (setq reg (buffer-substring beg end)
728 buf (get-buffer-create "*Org tmp*"))
729 (with-current-buffer buf
730 (erase-buffer)
731 (insert reg)
732 (org-mode)
733 (setq html (org-export-region-as-html
734 (point-min) (point-max) t 'string)))
735 (kill-buffer buf)))
736 (delete-region beg end)
737 (insert html)))
738
739;;;###autoload
740(defun org-export-region-as-html (beg end &optional body-only buffer)
741 "Convert region from BEG to END in org-mode buffer to HTML.
742If prefix arg BODY-ONLY is set, omit file header, footer, and table of
743contents, and only produce the region of converted text, useful for
744cut-and-paste operations.
745If BUFFER is a buffer or a string, use/create that buffer as a target
746of the converted HTML. If BUFFER is the symbol `string', return the
747produced HTML as a string and leave not buffer behind. For example,
748a Lisp program could call this function in the following way:
749
750 (setq html (org-export-region-as-html beg end t 'string))
751
752When called interactively, the output buffer is selected, and shown
753in a window. A non-interactive call will only return the buffer."
754 (interactive "r\nP")
3ab2c837 755 (when (org-called-interactively-p 'any)
c8d0cf5c
CD
756 (setq buffer "*Org HTML Export*"))
757 (let ((transient-mark-mode t) (zmacs-regions t)
758 ext-plist rtn)
8bfe682a 759 (setq ext-plist (plist-put ext-plist :ignore-subtree-p t))
c8d0cf5c
CD
760 (goto-char end)
761 (set-mark (point)) ;; to activate the region
762 (goto-char beg)
763 (setq rtn (org-export-as-html
764 nil nil ext-plist
765 buffer body-only))
766 (if (fboundp 'deactivate-mark) (deactivate-mark))
3ab2c837 767 (if (and (org-called-interactively-p 'any) (bufferp rtn))
c8d0cf5c
CD
768 (switch-to-buffer-other-window rtn)
769 rtn)))
770
771(defvar html-table-tag nil) ; dynamically scoped into this.
772(defvar org-par-open nil)
86fbb8ca
CD
773
774;;; org-html-cvt-link-fn
775(defconst org-html-cvt-link-fn
776 nil
777 "Function to convert link URLs to exportable URLs.
778Takes two arguments, TYPE and PATH.
779Returns exportable url as (TYPE PATH), or nil to signal that it
780didn't handle this case.
781Intended to be locally bound around a call to `org-export-as-html'." )
782
783(defun org-html-cvt-org-as-html (opt-plist type path)
784 "Convert an org filename to an equivalent html filename.
785If TYPE is not file, just return `nil'.
786See variable `org-export-html-link-org-files-as-html'"
787
788 (save-match-data
789 (and
790 org-export-html-link-org-files-as-html
791 (string= type "file")
792 (string-match "\\.org$" path)
793 (progn
794 (list
afe98dfa 795 "file"
86fbb8ca
CD
796 (concat
797 (substring path 0 (match-beginning 0))
798 "."
799 (plist-get opt-plist :html-extension)))))))
800
801
802;;; org-html-should-inline-p
803(defun org-html-should-inline-p (filename descp)
804 "Return non-nil if link FILENAME should be inlined.
805The decision to inline the FILENAME link is based on the current
806settings. DESCP is the boolean of whether there was a link
807description. See variables `org-export-html-inline-images' and
808`org-export-html-inline-image-extensions'."
809 (declare (special
810 org-export-html-inline-images
811 org-export-html-inline-image-extensions))
afe98dfa
CD
812 (and (or (eq t org-export-html-inline-images)
813 (and org-export-html-inline-images (not descp)))
814 (org-file-image-p
815 filename org-export-html-inline-image-extensions)))
86fbb8ca
CD
816
817;;; org-html-make-link
818(defun org-html-make-link (opt-plist type path fragment desc attr
819 may-inline-p)
820 "Make an HTML link.
821OPT-PLIST is an options list.
e66ba1df
BG
822TYPE is the device-type of the link (THIS://foo.html).
823PATH is the path of the link (http://THIS#location).
824FRAGMENT is the fragment part of the link, if any (foo.html#THIS).
86fbb8ca 825DESC is the link description, if any.
e66ba1df 826ATTR is a string of other attributes of the \"a\" element.
86fbb8ca
CD
827MAY-INLINE-P allows inlining it as an image."
828
829 (declare (special org-par-open))
830 (save-match-data
831 (let* ((filename path)
832 ;;First pass. Just sanity stuff.
833 (components-1
834 (cond
835 ((string= type "file")
836 (list
837 type
838 ;;Substitute just if original path was absolute.
839 ;;(Otherwise path must remain relative)
840 (if (file-name-absolute-p path)
afe98dfa 841 (concat "file://" (expand-file-name path))
86fbb8ca
CD
842 path)))
843 ((string= type "")
844 (list nil path))
845 (t (list type path))))
846
847 ;;Second pass. Components converted so they can refer
848 ;;to a remote site.
849 (components-2
850 (or
851 (and org-html-cvt-link-fn
852 (apply org-html-cvt-link-fn
853 opt-plist components-1))
854 (apply #'org-html-cvt-org-as-html
855 opt-plist components-1)
856 components-1))
857 (type (first components-2))
858 (thefile (second components-2)))
859
860
861 ;;Third pass. Build final link except for leading type
862 ;;spec.
863 (cond
864 ((or
865 (not type)
866 (string= type "http")
afe98dfa 867 (string= type "https")
3ab2c837
BG
868 (string= type "file")
869 (string= type "coderef"))
86fbb8ca
CD
870 (if fragment
871 (setq thefile (concat thefile "#" fragment))))
872
873 (t))
874
875 ;;Final URL-build, for all types.
876 (setq thefile
877 (let
878 ((str (org-export-html-format-href thefile)))
3ab2c837
BG
879 (if (and type (not (or (string= "file" type)
880 (string= "coderef" type))))
86fbb8ca
CD
881 (concat type ":" str)
882 str)))
883
884 (if (and
885 may-inline-p
886 ;;Can't inline a URL with a fragment.
887 (not fragment))
888 (progn
889 (message "image %s %s" thefile org-par-open)
890 (org-export-html-format-image thefile org-par-open))
891 (concat
3ab2c837 892 "<a href=\"" thefile "\"" (if attr (concat " " attr)) ">"
86fbb8ca
CD
893 (org-export-html-format-desc desc)
894 "</a>")))))
895
3ab2c837
BG
896(defun org-html-handle-links (line opt-plist)
897 "Return LINE with markup of Org mode links.
898OPT-PLIST is the export options list."
899 (let ((start 0)
900 (current-dir (if buffer-file-name
901 (file-name-directory buffer-file-name)
902 default-directory))
903 (link-validate (plist-get opt-plist :link-validation-function))
904 type id-file fnc
905 rpl path attr desc descp desc1 desc2 link)
906 (while (string-match org-bracket-link-analytic-regexp++ line start)
907 (setq start (match-beginning 0))
908 (setq path (save-match-data (org-link-unescape
909 (match-string 3 line))))
910 (setq type (cond
911 ((match-end 2) (match-string 2 line))
912 ((save-match-data
913 (or (file-name-absolute-p path)
914 (string-match "^\\.\\.?/" path)))
915 "file")
916 (t "internal")))
e66ba1df 917 (setq path (org-extract-attributes path))
3ab2c837
BG
918 (setq attr (get-text-property 0 'org-attributes path))
919 (setq desc1 (if (match-end 5) (match-string 5 line))
920 desc2 (if (match-end 2) (concat type ":" path) path)
921 descp (and desc1 (not (equal desc1 desc2)))
922 desc (or desc1 desc2))
923 ;; Make an image out of the description if that is so wanted
924 (when (and descp (org-file-image-p
925 desc org-export-html-inline-image-extensions))
926 (save-match-data
927 (if (string-match "^file:" desc)
928 (setq desc (substring desc (match-end 0)))))
929 (setq desc (org-add-props
e66ba1df 930 (concat "<img src=\"" desc "\" alt=\""
3ab2c837
BG
931 (file-name-nondirectory desc) "\"/>")
932 '(org-protected t))))
933 (cond
934 ((equal type "internal")
935 (let
936 ((frag-0
937 (if (= (string-to-char path) ?#)
938 (substring path 1)
939 path)))
940 (setq rpl
941 (org-html-make-link
942 opt-plist
943 ""
944 ""
945 (org-solidify-link-text
946 (save-match-data (org-link-unescape frag-0))
947 nil)
948 desc attr nil))))
949 ((and (equal type "id")
950 (setq id-file (org-id-find-id-file path)))
951 ;; This is an id: link to another file (if it was the same file,
952 ;; it would have become an internal link...)
953 (save-match-data
954 (setq id-file (file-relative-name
955 id-file
956 (file-name-directory org-current-export-file)))
957 (setq rpl
958 (org-html-make-link opt-plist
959 "file" id-file
960 (concat (if (org-uuidgen-p path) "ID-") path)
961 desc
962 attr
963 nil))))
964 ((member type '("http" "https"))
965 ;; standard URL, can inline as image
966 (setq rpl
967 (org-html-make-link opt-plist
968 type path nil
969 desc
970 attr
971 (org-html-should-inline-p path descp))))
972 ((member type '("ftp" "mailto" "news"))
973 ;; standard URL, can't inline as image
974 (setq rpl
975 (org-html-make-link opt-plist
976 type path nil
977 desc
978 attr
979 nil)))
980
981 ((string= type "coderef")
982 (let*
983 ((coderef-str (format "coderef-%s" path))
984 (attr-1
985 (format "class=\"coderef\" onmouseover=\"CodeHighlightOn(this, '%s');\" onmouseout=\"CodeHighlightOff(this, '%s');\""
986 coderef-str coderef-str)))
987 (setq rpl
988 (org-html-make-link opt-plist
989 type "" coderef-str
990 (format
991 (org-export-get-coderef-format
992 path
993 (and descp desc))
994 (cdr (assoc path org-export-code-refs)))
995 attr-1
996 nil))))
997
998 ((functionp (setq fnc (nth 2 (assoc type org-link-protocols))))
999 ;; The link protocol has a function for format the link
1000 (setq rpl
1001 (save-match-data
1002 (funcall fnc (org-link-unescape path) desc1 'html))))
1003
1004 ((string= type "file")
1005 ;; FILE link
1006 (save-match-data
1007 (let*
1008 ((components
1009 (if
1010 (string-match "::\\(.*\\)" path)
1011 (list
1012 (replace-match "" t nil path)
1013 (match-string 1 path))
1014 (list path nil)))
1015
1016 ;;The proper path, without a fragment
1017 (path-1
1018 (first components))
1019
1020 ;;The raw fragment
1021 (fragment-0
1022 (second components))
1023
1024 ;;Check the fragment. If it can't be used as
1025 ;;target fragment we'll pass nil instead.
1026 (fragment-1
1027 (if
1028 (and fragment-0
1029 (not (string-match "^[0-9]*$" fragment-0))
1030 (not (string-match "^\\*" fragment-0))
1031 (not (string-match "^/.*/$" fragment-0)))
1032 (org-solidify-link-text
1033 (org-link-unescape fragment-0))
1034 nil))
1035 (desc-2
1036 ;;Description minus "file:" and ".org"
1037 (if (string-match "^file:" desc)
1038 (let
1039 ((desc-1 (replace-match "" t t desc)))
1040 (if (string-match "\\.org$" desc-1)
1041 (replace-match "" t t desc-1)
1042 desc-1))
1043 desc)))
1044
1045 (setq rpl
1046 (if
1047 (and
1048 (functionp link-validate)
1049 (not (funcall link-validate path-1 current-dir)))
1050 desc
1051 (org-html-make-link opt-plist
1052 "file" path-1 fragment-1 desc-2 attr
1053 (org-html-should-inline-p path-1 descp)))))))
1054
1055 (t
1056 ;; just publish the path, as default
e66ba1df 1057 (setq rpl (concat "<i>&lt;" type ":"
3ab2c837 1058 (save-match-data (org-link-unescape path))
e66ba1df 1059 "&gt;</i>"))))
3ab2c837
BG
1060 (setq line (replace-match rpl t t line)
1061 start (+ start (length rpl))))
1062 line))
1063
86fbb8ca 1064;;; org-export-as-html
e66ba1df
BG
1065
1066(defvar org-heading-keyword-regexp-format) ; defined in org.el
1067
c8d0cf5c
CD
1068;;;###autoload
1069(defun org-export-as-html (arg &optional hidden ext-plist
1070 to-buffer body-only pub-dir)
1071 "Export the outline as a pretty HTML file.
1072If there is an active region, export only the region. The prefix
1073ARG specifies how many levels of the outline should become
1074headlines. The default is 3. Lower levels will become bulleted
1075lists. HIDDEN is obsolete and does nothing.
1076EXT-PLIST is a property list with external parameters overriding
1077org-mode's default settings, but still inferior to file-local
1078settings. When TO-BUFFER is non-nil, create a buffer with that
1079name and export to that buffer. If TO-BUFFER is the symbol
1080`string', don't leave any buffer behind but just return the
1081resulting HTML as a string. When BODY-ONLY is set, don't produce
1082the file header and footer, simply return the content of
1083<body>...</body>, without even the body tags themselves. When
1084PUB-DIR is set, use this as the publishing directory."
1085 (interactive "P")
ed21c5c8 1086 (run-hooks 'org-export-first-hook)
c8d0cf5c
CD
1087
1088 ;; Make sure we have a file name when we need it.
1089 (when (and (not (or to-buffer body-only))
1090 (not buffer-file-name))
1091 (if (buffer-base-buffer)
1092 (org-set-local 'buffer-file-name
1093 (with-current-buffer (buffer-base-buffer)
1094 buffer-file-name))
f924a367 1095 (error "Need a file name to be able to export")))
c8d0cf5c
CD
1096
1097 (message "Exporting...")
1098 (setq-default org-todo-line-regexp org-todo-line-regexp)
1099 (setq-default org-deadline-line-regexp org-deadline-line-regexp)
1100 (setq-default org-done-keywords org-done-keywords)
1101 (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp)
1102 (let* ((opt-plist
1103 (org-export-process-option-filters
1104 (org-combine-plists (org-default-export-plist)
1105 ext-plist
1106 (org-infile-export-plist))))
1107 (body-only (or body-only (plist-get opt-plist :body-only)))
1108 (style (concat (if (plist-get opt-plist :style-include-default)
1109 org-export-html-style-default)
1110 (plist-get opt-plist :style)
1111 (plist-get opt-plist :style-extra)
1112 "\n"
1113 (if (plist-get opt-plist :style-include-scripts)
1114 org-export-html-scripts)))
1115 (html-extension (plist-get opt-plist :html-extension))
c8d0cf5c
CD
1116 valid thetoc have-headings first-heading-pos
1117 (odd org-odd-levels-only)
1118 (region-p (org-region-active-p))
1119 (rbeg (and region-p (region-beginning)))
1120 (rend (and region-p (region-end)))
1121 (subtree-p
8bfe682a 1122 (if (plist-get opt-plist :ignore-subtree-p)
c8d0cf5c
CD
1123 nil
1124 (when region-p
1125 (save-excursion
1126 (goto-char rbeg)
1127 (and (org-at-heading-p)
1128 (>= (org-end-of-subtree t t) rend))))))
1129 (level-offset (if subtree-p
1130 (save-excursion
1131 (goto-char rbeg)
1132 (+ (funcall outline-level)
1133 (if org-odd-levels-only 1 0)))
1134 0))
1135 (opt-plist (setq org-export-opt-plist
1136 (if subtree-p
1137 (org-export-add-subtree-options opt-plist rbeg)
1138 opt-plist)))
1139 ;; The following two are dynamically scoped into other
1140 ;; routines below.
1141 (org-current-export-dir
1142 (or pub-dir (org-export-directory :html opt-plist)))
1143 (org-current-export-file buffer-file-name)
1144 (level 0) (line "") (origline "") txt todo
1145 (umax nil)
1146 (umax-toc nil)
1147 (filename (if to-buffer nil
1148 (expand-file-name
1149 (concat
1150 (file-name-sans-extension
1151 (or (and subtree-p
1152 (org-entry-get (region-beginning)
1153 "EXPORT_FILE_NAME" t))
1154 (file-name-nondirectory buffer-file-name)))
1155 "." html-extension)
1156 (file-name-as-directory
1157 (or pub-dir (org-export-directory :html opt-plist))))))
1158 (current-dir (if buffer-file-name
1159 (file-name-directory buffer-file-name)
1160 default-directory))
e66ba1df 1161 (auto-insert nil); Avoid any auto-insert stuff for the new file
c8d0cf5c
CD
1162 (buffer (if to-buffer
1163 (cond
1164 ((eq to-buffer 'string) (get-buffer-create "*Org HTML Export*"))
1165 (t (get-buffer-create to-buffer)))
1166 (find-file-noselect filename)))
1167 (org-levels-open (make-vector org-level-max nil))
e66ba1df
BG
1168 (date (org-html-expand (plist-get opt-plist :date)))
1169 (author (org-html-expand (plist-get opt-plist :author)))
3ab2c837
BG
1170 (html-validation-link (or org-export-html-validation-link ""))
1171 (title (org-html-expand
1172 (or (and subtree-p (org-export-get-title-from-subtree))
1173 (plist-get opt-plist :title)
1174 (and (not body-only)
1175 (not
1176 (plist-get opt-plist :skip-before-1st-heading))
1177 (org-export-grab-title-from-buffer))
1178 (and buffer-file-name
1179 (file-name-sans-extension
1180 (file-name-nondirectory buffer-file-name)))
1181 "UNTITLED")))
c8d0cf5c
CD
1182 (link-up (and (plist-get opt-plist :link-up)
1183 (string-match "\\S-" (plist-get opt-plist :link-up))
1184 (plist-get opt-plist :link-up)))
1185 (link-home (and (plist-get opt-plist :link-home)
afe98dfa
CD
1186 (string-match "\\S-" (plist-get opt-plist :link-home))
1187 (plist-get opt-plist :link-home)))
c8d0cf5c
CD
1188 (dummy (setq opt-plist (plist-put opt-plist :title title)))
1189 (html-table-tag (plist-get opt-plist :html-table-tag))
e66ba1df
BG
1190 (quote-re0 (concat "^ *" org-quote-string "\\( +\\|[ \t]*$\\)"))
1191 (quote-re (format org-heading-keyword-regexp-format
1192 org-quote-string))
c8d0cf5c
CD
1193 (inquote nil)
1194 (infixed nil)
1195 (inverse nil)
c8d0cf5c
CD
1196 (email (plist-get opt-plist :email))
1197 (language (plist-get opt-plist :language))
e66ba1df
BG
1198 (keywords (org-html-expand (plist-get opt-plist :keywords)))
1199 (description (org-html-expand (plist-get opt-plist :description)))
3ab2c837 1200 (num (plist-get opt-plist :section-numbers))
c8d0cf5c
CD
1201 (lang-words nil)
1202 (head-count 0) cnt
1203 (start 0)
1204 (coding-system (and (boundp 'buffer-file-coding-system)
1205 buffer-file-coding-system))
1206 (coding-system-for-write (or org-export-html-coding-system
1207 coding-system))
1208 (save-buffer-coding-system (or org-export-html-coding-system
1209 coding-system))
1210 (charset (and coding-system-for-write
1211 (fboundp 'coding-system-get)
1212 (coding-system-get coding-system-for-write
1213 'mime-charset)))
1214 (region
1215 (buffer-substring
1216 (if region-p (region-beginning) (point-min))
1217 (if region-p (region-end) (point-max))))
afe98dfa 1218 (org-export-have-math nil)
3ab2c837
BG
1219 (org-export-footnotes-seen nil)
1220 (org-export-footnotes-data (org-footnote-all-labels 'with-defs))
c8d0cf5c
CD
1221 (lines
1222 (org-split-string
1223 (org-export-preprocess-string
1224 region
1225 :emph-multiline t
3ab2c837 1226 :for-backend 'html
c8d0cf5c
CD
1227 :skip-before-1st-heading
1228 (plist-get opt-plist :skip-before-1st-heading)
1229 :drawers (plist-get opt-plist :drawers)
1230 :todo-keywords (plist-get opt-plist :todo-keywords)
3ab2c837 1231 :tasks (plist-get opt-plist :tasks)
c8d0cf5c
CD
1232 :tags (plist-get opt-plist :tags)
1233 :priority (plist-get opt-plist :priority)
1234 :footnotes (plist-get opt-plist :footnotes)
1235 :timestamps (plist-get opt-plist :timestamps)
1236 :archived-trees
1237 (plist-get opt-plist :archived-trees)
1238 :select-tags (plist-get opt-plist :select-tags)
1239 :exclude-tags (plist-get opt-plist :exclude-tags)
1240 :add-text
1241 (plist-get opt-plist :text)
1242 :LaTeX-fragments
1243 (plist-get opt-plist :LaTeX-fragments))
1244 "[\r\n]"))
afe98dfa
CD
1245 (mathjax
1246 (if (or (eq (plist-get opt-plist :LaTeX-fragments) 'mathjax)
1247 (and org-export-have-math
1248 (eq (plist-get opt-plist :LaTeX-fragments) t)))
1249
1250 (org-export-html-mathjax-config
1251 org-export-html-mathjax-template
1252 org-export-html-mathjax-options
1253 (or (plist-get opt-plist :mathjax) ""))
1254 ""))
3ab2c837 1255 table-open
c8d0cf5c 1256 table-buffer table-orig-buffer
3ab2c837 1257 ind
c8d0cf5c 1258 rpl path attr desc descp desc1 desc2 link
3ab2c837 1259 snumber fnc
c8d0cf5c 1260 footnotes footref-seen
3ab2c837 1261 href
c8d0cf5c
CD
1262 )
1263
1264 (let ((inhibit-read-only t))
1265 (org-unmodified
1266 (remove-text-properties (point-min) (point-max)
1267 '(:org-license-to-kill t))))
1268
1269 (message "Exporting...")
1270
1271 (setq org-min-level (org-get-min-level lines level-offset))
1272 (setq org-last-level org-min-level)
1273 (org-init-section-numbers)
1274
1275 (cond
1276 ((and date (string-match "%" date))
1277 (setq date (format-time-string date)))
1278 (date)
1279 (t (setq date (format-time-string "%Y-%m-%d %T %Z"))))
1280
1281 ;; Get the language-dependent settings
1282 (setq lang-words (or (assoc language org-export-language-setup)
1283 (assoc "en" org-export-language-setup)))
1284
1285 ;; Switch to the output buffer
1286 (set-buffer buffer)
1287 (let ((inhibit-read-only t)) (erase-buffer))
1288 (fundamental-mode)
1289 (org-install-letbind)
1290
1291 (and (fboundp 'set-buffer-file-coding-system)
1292 (set-buffer-file-coding-system coding-system-for-write))
1293
1294 (let ((case-fold-search nil)
1295 (org-odd-levels-only odd))
1296 ;; create local variables for all options, to make sure all called
1297 ;; functions get the correct information
1298 (mapc (lambda (x)
1299 (set (make-local-variable (nth 2 x))
1300 (plist-get opt-plist (car x))))
1301 org-export-plist-vars)
1302 (setq umax (if arg (prefix-numeric-value arg)
1303 org-export-headline-levels))
1304 (setq umax-toc (if (integerp org-export-with-toc)
1305 (min org-export-with-toc umax)
1306 umax))
1307 (unless body-only
1308 ;; File header
1309 (insert (format
1310 "%s
1311<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
1312 \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
e66ba1df 1313<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"%s\" xml:lang=\"%s\">
c8d0cf5c 1314<head>
c8d0cf5c
CD
1315<title>%s</title>
1316<meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>
e66ba1df 1317<meta name=\"title\" content=\"%s\"/>
c8d0cf5c
CD
1318<meta name=\"generator\" content=\"Org-mode\"/>
1319<meta name=\"generated\" content=\"%s\"/>
1320<meta name=\"author\" content=\"%s\"/>
1321<meta name=\"description\" content=\"%s\"/>
1322<meta name=\"keywords\" content=\"%s\"/>
1323%s
afe98dfa 1324%s
c8d0cf5c
CD
1325</head>
1326<body>
5dec9555 1327%s
c8d0cf5c
CD
1328"
1329 (format
1330 (or (and (stringp org-export-html-xml-declaration)
1331 org-export-html-xml-declaration)
1332 (cdr (assoc html-extension org-export-html-xml-declaration))
1333 (cdr (assoc "html" org-export-html-xml-declaration))
1334
1335 "")
1336 (or charset "iso-8859-1"))
1337 language language
86fbb8ca 1338 title
5dec9555 1339 (or charset "iso-8859-1")
e66ba1df 1340 title date author description keywords
5dec9555 1341 style
afe98dfa 1342 mathjax
c8d0cf5c
CD
1343 (if (or link-up link-home)
1344 (concat
1345 (format org-export-html-home/up-format
1346 (or link-up link-home)
1347 (or link-home link-up))
1348 "\n")
5dec9555 1349 "")))
c8d0cf5c 1350
3ab2c837
BG
1351 ;; insert html preamble
1352 (when (plist-get opt-plist :html-preamble)
e66ba1df 1353 (let ((html-pre (plist-get opt-plist :html-preamble))
153ae947 1354 (html-pre-real-contents ""))
3ab2c837 1355 (cond ((stringp html-pre)
e66ba1df
BG
1356 (setq html-pre-real-contents
1357 (format-spec html-pre `((?t . ,title) (?a . ,author)
1358 (?d . ,date) (?e . ,email)))))
3ab2c837 1359 ((functionp html-pre)
e66ba1df
BG
1360 (insert "<div id=\"" (nth 0 org-export-html-divs) "\">\n")
1361 (if (stringp (funcall html-pre)) (insert (funcall html-pre)))
1362 (insert "\n</div>\n"))
3ab2c837 1363 (t
e66ba1df 1364 (setq html-pre-real-contents
3ab2c837
BG
1365 (format-spec
1366 (or (cadr (assoc (nth 0 lang-words)
1367 org-export-html-preamble-format))
1368 (cadr (assoc "en" org-export-html-preamble-format)))
1369 `((?t . ,title) (?a . ,author)
1370 (?d . ,date) (?e . ,email))))))
e66ba1df
BG
1371 ;; don't output an empty preamble DIV
1372 (unless (and (functionp html-pre)
1373 (equal html-pre-real-contents ""))
1374 (insert "<div id=\"" (nth 0 org-export-html-divs) "\">\n")
1375 (insert html-pre-real-contents)
1376 (insert "\n</div>\n"))))
3ab2c837
BG
1377
1378 ;; begin wrap around body
e66ba1df 1379 (insert (format "\n<div id=\"%s\">"
3ab2c837 1380 ;; FIXME org-export-html-content-div is obsolete since 7.7
e66ba1df 1381 (or org-export-html-content-div
3ab2c837
BG
1382 (nth 1 org-export-html-divs)))
1383 ;; FIXME this should go in the preamble but is here so
1384 ;; that org-infojs can still find it
1385 "\n<h1 class=\"title\">" title "</h1>\n"))
1386
1387 ;; insert body
c8d0cf5c
CD
1388 (if (and org-export-with-toc (not body-only))
1389 (progn
1390 (push (format "<h%d>%s</h%d>\n"
1391 org-export-html-toplevel-hlevel
1392 (nth 3 lang-words)
1393 org-export-html-toplevel-hlevel)
1394 thetoc)
1395 (push "<div id=\"text-table-of-contents\">\n" thetoc)
1396 (push "<ul>\n<li>" thetoc)
1397 (setq lines
e66ba1df 1398 (mapcar
3ab2c837
BG
1399 #'(lambda (line)
1400 (if (and (string-match org-todo-line-regexp line)
1401 (not (get-text-property 0 'org-protected line)))
1402 ;; This is a headline
1403 (progn
1404 (setq have-headings t)
1405 (setq level (- (match-end 1) (match-beginning 1)
1406 level-offset)
1407 level (org-tr-level level)
1408 txt (save-match-data
1409 (org-html-expand
1410 (org-export-cleanup-toc-line
1411 (match-string 3 line))))
1412 todo
1413 (or (and org-export-mark-todo-in-toc
1414 (match-beginning 2)
1415 (not (member (match-string 2 line)
1416 org-done-keywords)))
c8d0cf5c 1417 ; TODO, not DONE
3ab2c837
BG
1418 (and org-export-mark-todo-in-toc
1419 (= level umax-toc)
1420 (org-search-todo-below
1421 line lines level))))
1422 (if (string-match
1423 (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") txt)
e66ba1df 1424 (setq txt (replace-match
153ae947 1425 "&nbsp;&nbsp;&nbsp;<span class=\"tag\">\\1</span>" t nil txt)))
3ab2c837
BG
1426 (if (string-match quote-re0 txt)
1427 (setq txt (replace-match "" t t txt)))
1428 (setq snumber (org-section-number level))
1429 (if (and num (if (integerp num)
1430 (>= num level)
1431 num))
1432 (setq txt (concat snumber " " txt)))
1433 (if (<= level (max umax umax-toc))
1434 (setq head-count (+ head-count 1)))
1435 (if (<= level umax-toc)
1436 (progn
1437 (if (> level org-last-level)
1438 (progn
1439 (setq cnt (- level org-last-level))
1440 (while (>= (setq cnt (1- cnt)) 0)
1441 (push "\n<ul>\n<li>" thetoc))
1442 (push "\n" thetoc)))
1443 (if (< level org-last-level)
1444 (progn
1445 (setq cnt (- org-last-level level))
1446 (while (>= (setq cnt (1- cnt)) 0)
1447 (push "</li>\n</ul>" thetoc))
1448 (push "\n" thetoc)))
1449 ;; Check for targets
1450 (while (string-match org-any-target-regexp line)
1451 (setq line (replace-match
e66ba1df 1452 (concat "@<span class=\"target\">"
3ab2c837
BG
1453 (match-string 1 line) "@</span> ")
1454 t t line)))
1455 (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
1456 (setq txt (replace-match "" t t txt)))
1457 (setq href
1458 (replace-regexp-in-string
1459 "\\." "-" (format "sec-%s" snumber)))
e66ba1df
BG
1460 (setq href (org-solidify-link-text
1461 (or (cdr (assoc href
3ab2c837
BG
1462 org-export-preferred-target-alist)) href)))
1463 (push
1464 (format
1465 (if todo
1466 "</li>\n<li><a href=\"#%s\"><span class=\"todo\">%s</span></a>"
1467 "</li>\n<li><a href=\"#%s\">%s</a>")
1468 href txt) thetoc)
e66ba1df 1469
3ab2c837
BG
1470 (setq org-last-level level)))))
1471 line)
1472 lines))
c8d0cf5c
CD
1473 (while (> org-last-level (1- org-min-level))
1474 (setq org-last-level (1- org-last-level))
1475 (push "</li>\n</ul>\n" thetoc))
1476 (push "</div>\n" thetoc)
1477 (setq thetoc (if have-headings (nreverse thetoc) nil))))
e66ba1df 1478
c8d0cf5c
CD
1479 (setq head-count 0)
1480 (org-init-section-numbers)
e66ba1df 1481
c8d0cf5c 1482 (org-open-par)
e66ba1df 1483
c8d0cf5c
CD
1484 (while (setq line (pop lines) origline line)
1485 (catch 'nextline
e66ba1df 1486
c8d0cf5c 1487 ;; end of quote section?
3ab2c837 1488 (when (and inquote (string-match org-outline-regexp-bol line))
c8d0cf5c
CD
1489 (insert "</pre>\n")
1490 (org-open-par)
1491 (setq inquote nil))
1492 ;; inside a quote section?
1493 (when inquote
1494 (insert (org-html-protect line) "\n")
1495 (throw 'nextline nil))
1496
1497 ;; Fixed-width, verbatim lines (examples)
1498 (when (and org-export-with-fixed-width
1499 (string-match "^[ \t]*:\\(\\([ \t]\\|$\\)\\(.*\\)\\)" line))
1500 (when (not infixed)
1501 (setq infixed t)
1502 (org-close-par-maybe)
1503
1504 (insert "<pre class=\"example\">\n"))
1505 (insert (org-html-protect (match-string 3 line)) "\n")
1506 (when (or (not lines)
1507 (not (string-match "^[ \t]*:\\(\\([ \t]\\|$\\)\\(.*\\)\\)"
1508 (car lines))))
1509 (setq infixed nil)
1510 (insert "</pre>\n")
1511 (org-open-par))
1512 (throw 'nextline nil))
1513
c8d0cf5c 1514 ;; Protected HTML
acedf35c
CD
1515 (when (and (get-text-property 0 'org-protected line)
1516 ;; Make sure it is the entire line that is protected
1517 (not (< (or (next-single-property-change
1518 0 'org-protected line) 10000)
1519 (length line))))
c8d0cf5c
CD
1520 (let (par (ind (get-text-property 0 'original-indentation line)))
1521 (when (re-search-backward
1522 "\\(<p>\\)\\([ \t\r\n]*\\)\\=" (- (point) 100) t)
1523 (setq par (match-string 1))
1524 (replace-match "\\2\n"))
1525 (insert line "\n")
1526 (while (and lines
1527 (or (= (length (car lines)) 0)
1528 (not ind)
1529 (equal ind (get-text-property 0 'original-indentation (car lines))))
1530 (or (= (length (car lines)) 0)
1531 (get-text-property 0 'org-protected (car lines))))
1532 (insert (pop lines) "\n"))
1533 (and par (insert "<p>\n")))
1534 (throw 'nextline nil))
1535
1536 ;; Blockquotes, verse, and center
1537 (when (equal "ORG-BLOCKQUOTE-START" line)
1538 (org-close-par-maybe)
1539 (insert "<blockquote>\n")
1540 (org-open-par)
1541 (throw 'nextline nil))
1542 (when (equal "ORG-BLOCKQUOTE-END" line)
1543 (org-close-par-maybe)
1544 (insert "\n</blockquote>\n")
1545 (org-open-par)
1546 (throw 'nextline nil))
1547 (when (equal "ORG-VERSE-START" line)
1548 (org-close-par-maybe)
1549 (insert "\n<p class=\"verse\">\n")
86fbb8ca 1550 (setq org-par-open t)
c8d0cf5c
CD
1551 (setq inverse t)
1552 (throw 'nextline nil))
1553 (when (equal "ORG-VERSE-END" line)
1554 (insert "</p>\n")
86fbb8ca 1555 (setq org-par-open nil)
c8d0cf5c
CD
1556 (org-open-par)
1557 (setq inverse nil)
1558 (throw 'nextline nil))
1559 (when (equal "ORG-CENTER-START" line)
1560 (org-close-par-maybe)
1561 (insert "\n<div style=\"text-align: center\">")
1562 (org-open-par)
1563 (throw 'nextline nil))
1564 (when (equal "ORG-CENTER-END" line)
1565 (org-close-par-maybe)
1566 (insert "\n</div>")
1567 (org-open-par)
1568 (throw 'nextline nil))
1569 (run-hooks 'org-export-html-after-blockquotes-hook)
1570 (when inverse
1571 (let ((i (org-get-string-indentation line)))
1572 (if (> i 0)
1573 (setq line (concat (mapconcat 'identity
1574 (make-list (* 2 i) "\\nbsp") "")
1575 " " (org-trim line))))
1576 (unless (string-match "\\\\\\\\[ \t]*$" line)
1577 (setq line (concat line "\\\\")))))
1578
1579 ;; make targets to anchors
8bfe682a 1580 (setq start 0)
c8d0cf5c 1581 (while (string-match
8bfe682a 1582 "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line start)
c8d0cf5c 1583 (cond
8bfe682a
CD
1584 ((get-text-property (match-beginning 1) 'org-protected line)
1585 (setq start (match-end 1)))
c8d0cf5c
CD
1586 ((match-end 2)
1587 (setq line (replace-match
1588 (format
1589 "@<a name=\"%s\" id=\"%s\">@</a>"
1590 (org-solidify-link-text (match-string 1 line))
1591 (org-solidify-link-text (match-string 1 line)))
1592 t t line)))
1593 ((and org-export-with-toc (equal (string-to-char line) ?*))
1594 ;; FIXME: NOT DEPENDENT on TOC?????????????????????
1595 (setq line (replace-match
8bfe682a
CD
1596 (concat "@<span class=\"target\">"
1597 (match-string 1 line) "@</span> ")
1598 ;; (concat "@<i>" (match-string 1 line) "@</i> ")
c8d0cf5c
CD
1599 t t line)))
1600 (t
1601 (setq line (replace-match
1602 (concat "@<a name=\""
1603 (org-solidify-link-text (match-string 1 line))
8bfe682a
CD
1604 "\" class=\"target\">" (match-string 1 line)
1605 "@</a> ")
c8d0cf5c 1606 t t line)))))
ed21c5c8 1607
c8d0cf5c
CD
1608 (setq line (org-html-handle-time-stamps line))
1609
1610 ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
1611 ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
1612 ;; Also handle sub_superscripts and checkboxes
1613 (or (string-match org-table-hline-regexp line)
3ab2c837 1614 (string-match "^[ \t]*\\([+]-\\||[ ]\\)[-+ |]*[+|][ \t]*$" line)
c8d0cf5c
CD
1615 (setq line (org-html-expand line)))
1616
1617 ;; Format the links
3ab2c837 1618 (setq line (org-html-handle-links line opt-plist))
c8d0cf5c
CD
1619
1620 ;; TODO items
e66ba1df
BG
1621 (if (and org-todo-line-regexp
1622 (string-match org-todo-line-regexp line)
c8d0cf5c
CD
1623 (match-beginning 2))
1624
1625 (setq line
1626 (concat (substring line 0 (match-beginning 2))
1627 "<span class=\""
1628 (if (member (match-string 2 line)
1629 org-done-keywords)
1630 "done" "todo")
e66ba1df
BG
1631 " " (org-export-html-get-todo-kwd-class-name
1632 (match-string 2 line))
153ae947 1633 "\">" (match-string 2 line)
c8d0cf5c
CD
1634 "</span>" (substring line (match-end 2)))))
1635
1636 ;; Does this contain a reference to a footnote?
1637 (when org-export-with-footnotes
1638 (setq start 0)
54a0dee5 1639 (while (string-match "\\([^* \t].*?\\)\\[\\([0-9]+\\)\\]" line start)
3ab2c837
BG
1640 ;; Discard protected matches not clearly identified as
1641 ;; footnote markers.
1642 (if (or (get-text-property (match-beginning 2) 'org-protected line)
1643 (not (get-text-property (match-beginning 2) 'org-footnote line)))
c8d0cf5c
CD
1644 (setq start (match-end 2))
1645 (let ((n (match-string 2 line)) extra a)
1646 (if (setq a (assoc n footref-seen))
1647 (progn
1648 (setcdr a (1+ (cdr a)))
1649 (setq extra (format ".%d" (cdr a))))
1650 (setq extra "")
1651 (push (cons n 1) footref-seen))
1652 (setq line
1653 (replace-match
3ab2c837
BG
1654 (concat
1655 (format
1656 (concat "%s"
1657 (format org-export-html-footnote-format
1658 (concat "<a class=\"footref\" name=\"fnr.%s%s\" href=\"#fn.%s\">%s</a>")))
1659 (or (match-string 1 line) "") n extra n n)
1660 ;; If another footnote is following the
1661 ;; current one, add a separator.
1662 (if (save-match-data
1663 (string-match "\\`\\[[0-9]+\\]"
1664 (substring line (match-end 0))))
1665 org-export-html-footnote-separator
1666 ""))
c8d0cf5c
CD
1667 t t line))))))
1668
1669 (cond
e66ba1df 1670 ((string-match "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$" line)
c8d0cf5c
CD
1671 ;; This is a headline
1672 (setq level (org-tr-level (- (match-end 1) (match-beginning 1)
1673 level-offset))
1674 txt (match-string 2 line))
1675 (if (string-match quote-re0 txt)
1676 (setq txt (replace-match "" t t txt)))
1677 (if (<= level (max umax umax-toc))
1678 (setq head-count (+ head-count 1)))
c8d0cf5c
CD
1679 (setq first-heading-pos (or first-heading-pos (point)))
1680 (org-html-level-start level txt umax
1681 (and org-export-with-toc (<= level umax))
3ab2c837 1682 head-count opt-plist)
c8d0cf5c
CD
1683
1684 ;; QUOTES
1685 (when (string-match quote-re line)
1686 (org-close-par-maybe)
1687 (insert "<pre>")
1688 (setq inquote t)))
1689
c8d0cf5c
CD
1690 ((and org-export-with-tables
1691 (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
1692 (when (not table-open)
1693 ;; New table starts
1694 (setq table-open t table-buffer nil table-orig-buffer nil))
1695
1696 ;; Accumulate lines
1697 (setq table-buffer (cons line table-buffer)
1698 table-orig-buffer (cons origline table-orig-buffer))
1699 (when (or (not lines)
1700 (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
1701 (car lines))))
1702 (setq table-open nil
1703 table-buffer (nreverse table-buffer)
1704 table-orig-buffer (nreverse table-orig-buffer))
1705 (org-close-par-maybe)
1706 (insert (org-format-table-html table-buffer table-orig-buffer))))
3ab2c837
BG
1707
1708 ;; Normal lines
1709
c8d0cf5c 1710 (t
3ab2c837
BG
1711 ;; This line either is list item or end a list.
1712 (when (get-text-property 0 'list-item line)
1713 (setq line (org-html-export-list-line
1714 line
1715 (get-text-property 0 'list-item line)
1716 (get-text-property 0 'list-struct line)
1717 (get-text-property 0 'list-prevs line))))
c8d0cf5c
CD
1718
1719 ;; Horizontal line
1720 (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line)
1721 (if org-par-open
1722 (insert "\n</p>\n<hr/>\n<p>\n")
1723 (insert "\n<hr/>\n"))
1724 (throw 'nextline nil))
1725
1726 ;; Empty lines start a new paragraph. If hand-formatted lists
1727 ;; are not fully interpreted, lines starting with "-", "+", "*"
1728 ;; also start a new paragraph.
1729 (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (org-open-par))
1730
1731 ;; Is this the start of a footnote?
1732 (when org-export-with-footnotes
1733 (when (and (boundp 'footnote-section-tag-regexp)
1734 (string-match (concat "^" footnote-section-tag-regexp)
1735 line))
1736 ;; ignore this line
1737 (throw 'nextline nil))
1738 (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
1739 (org-close-par-maybe)
1740 (let ((n (match-string 1 line)))
1741 (setq org-par-open t
1742 line (replace-match
54a0dee5
CD
1743 (format
1744 (concat "<p class=\"footnote\">"
1745 (format org-export-html-footnote-format
1746 "<a class=\"footnum\" name=\"fn.%s\" href=\"#fnr.%s\">%s</a>"))
1747 n n n) t t line)))))
c8d0cf5c
CD
1748 ;; Check if the line break needs to be conserved
1749 (cond
1750 ((string-match "\\\\\\\\[ \t]*$" line)
1751 (setq line (replace-match "<br/>" t t line)))
1752 (org-export-preserve-breaks
1753 (setq line (concat line "<br/>"))))
1754
1755 ;; Check if a paragraph should be started
1756 (let ((start 0))
1757 (while (and org-par-open
1758 (string-match "\\\\par\\>" line start))
1759 ;; Leave a space in the </p> so that the footnote matcher
1760 ;; does not see this.
1761 (if (not (get-text-property (match-beginning 0)
1762 'org-protected line))
1763 (setq line (replace-match "</p ><p >" t t line)))
1764 (setq start (match-end 0))))
1765
1766 (insert line "\n")))))
1767
1768 ;; Properly close all local lists and other lists
1769 (when inquote
1770 (insert "</pre>\n")
1771 (org-open-par))
afe98dfa 1772
c8d0cf5c
CD
1773 (org-html-level-start 1 nil umax
1774 (and org-export-with-toc (<= level umax))
3ab2c837 1775 head-count opt-plist)
c8d0cf5c
CD
1776 ;; the </div> to close the last text-... div.
1777 (when (and (> umax 0) first-heading-pos) (insert "</div>\n"))
1778
1779 (save-excursion
1780 (goto-char (point-min))
3ab2c837
BG
1781 (while (re-search-forward
1782 "\\(\\(<p class=\"footnote\">\\)[^\000]*?\\)\\(\\(\\2\\)\\|\\'\\)"
1783 nil t)
1784 (push (match-string 1) footnotes)
1785 (replace-match "\\4" t nil)
1786 (goto-char (match-beginning 0))))
c8d0cf5c
CD
1787 (when footnotes
1788 (insert (format org-export-html-footnotes-section
1789 (nth 4 lang-words)
1790 (mapconcat 'identity (nreverse footnotes) "\n"))
1791 "\n"))
1792 (let ((bib (org-export-html-get-bibliography)))
1793 (when bib
1794 (insert "\n" bib "\n")))
3ab2c837 1795
c8d0cf5c 1796 (unless body-only
3ab2c837
BG
1797 ;; end wrap around body
1798 (insert "</div>\n")
1799
1800 ;; export html postamble
1801 (let ((html-post (plist-get opt-plist :html-postamble))
1802 (email
1803 (mapconcat (lambda(e)
1804 (format "<a href=\"mailto:%s\">%s</a>" e e))
1805 (split-string email ",+ *")
1806 ", "))
1807 (creator-info
1808 (concat "Org version " org-version " with Emacs version "
1809 (number-to-string emacs-major-version))))
1810
1811 (when (plist-get opt-plist :html-postamble)
1812 (insert "\n<div id=\"" (nth 2 org-export-html-divs) "\">\n")
1813 (cond ((stringp html-post)
1814 (insert (format-spec html-post
1815 `((?a . ,author) (?e . ,email)
1816 (?d . ,date) (?c . ,creator-info)
1817 (?v . ,html-validation-link)))))
1818 ((functionp html-post)
e66ba1df 1819 (if (stringp (funcall html-post)) (insert (funcall html-post))))
3ab2c837
BG
1820 ((eq html-post 'auto)
1821 ;; fall back on default postamble
1822 (when (plist-get opt-plist :time-stamp-file)
1823 (insert "<p class=\"date\">" (nth 2 lang-words) ": " date "</p>\n"))
1824 (when (and (plist-get opt-plist :author-info) author)
1825 (insert "<p class=\"author\">" (nth 1 lang-words) ": " author "</p>\n"))
1826 (when (and (plist-get opt-plist :email-info) email)
1827 (insert "<p class=\"email\">" email "</p>\n"))
1828 (when (plist-get opt-plist :creator-info)
1829 (insert "<p class=\"creator\">"
1830 (concat "Org version " org-version " with Emacs version "
1831 (number-to-string emacs-major-version) "</p>\n")))
1832 (insert html-validation-link "\n"))
1833 (t
1834 (insert (format-spec
1835 (or (cadr (assoc (nth 0 lang-words)
1836 org-export-html-postamble-format))
1837 (cadr (assoc "en" org-export-html-postamble-format)))
1838 `((?a . ,author) (?e . ,email)
1839 (?d . ,date) (?c . ,creator-info)
1840 (?v . ,html-validation-link))))))
1841 (insert "\n</div>"))))
e66ba1df 1842
3ab2c837
BG
1843 ;; FIXME `org-export-html-with-timestamp' has been declared
1844 ;; obsolete since Org 7.7 -- don't forget to remove this.
1845 (if org-export-html-with-timestamp
1846 (insert org-export-html-html-helper-timestamp))
1847
1848 (unless body-only (insert "\n</body>\n</html>\n"))
c8d0cf5c
CD
1849
1850 (unless (plist-get opt-plist :buffer-will-be-killed)
1851 (normal-mode)
54a0dee5
CD
1852 (if (eq major-mode (default-value 'major-mode))
1853 (html-mode)))
c8d0cf5c
CD
1854
1855 ;; insert the table of contents
1856 (goto-char (point-min))
1857 (when thetoc
1858 (if (or (re-search-forward
1859 "<p>\\s-*\\[TABLE-OF-CONTENTS\\]\\s-*</p>" nil t)
1860 (re-search-forward
1861 "\\[TABLE-OF-CONTENTS\\]" nil t))
1862 (progn
1863 (goto-char (match-beginning 0))
1864 (replace-match ""))
1865 (goto-char first-heading-pos)
1866 (when (looking-at "\\s-*</p>")
1867 (goto-char (match-end 0))
1868 (insert "\n")))
1869 (insert "<div id=\"table-of-contents\">\n")
3ab2c837
BG
1870 (let ((beg (point)))
1871 (mapc 'insert thetoc)
1872 (insert "</div>\n")
1873 (while (re-search-backward "<li>[ \r\n\t]*</li>\n?" beg t)
1874 (replace-match ""))))
1875 ;; remove empty paragraphs
c8d0cf5c
CD
1876 (goto-char (point-min))
1877 (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t)
1878 (replace-match ""))
1879 (goto-char (point-min))
c8d0cf5c
CD
1880 ;; Convert whitespace place holders
1881 (goto-char (point-min))
1882 (let (beg end n)
1883 (while (setq beg (next-single-property-change (point) 'org-whitespace))
1884 (setq n (get-text-property beg 'org-whitespace)
1885 end (next-single-property-change beg 'org-whitespace))
1886 (goto-char beg)
1887 (delete-region beg end)
1888 (insert (format "<span style=\"visibility:hidden;\">%s</span>"
1889 (make-string n ?x)))))
ed21c5c8
CD
1890 ;; Remove empty lines at the beginning of the file.
1891 (goto-char (point-min))
1892 (when (looking-at "\\s-+\n") (replace-match ""))
1893 ;; Remove display properties
1894 (remove-text-properties (point-min) (point-max) '(display t))
1895 ;; Run the hook
8d642074 1896 (run-hooks 'org-export-html-final-hook)
c8d0cf5c
CD
1897 (or to-buffer (save-buffer))
1898 (goto-char (point-min))
1899 (or (org-export-push-to-kill-ring "HTML")
1900 (message "Exporting... done"))
1901 (if (eq to-buffer 'string)
1902 (prog1 (buffer-substring (point-min) (point-max))
1903 (kill-buffer (current-buffer)))
1904 (current-buffer)))))
1905
c8d0cf5c
CD
1906(defun org-export-html-format-href (s)
1907 "Make sure the S is valid as a href reference in an XHTML document."
1908 (save-match-data
1909 (let ((start 0))
1910 (while (string-match "&" s start)
1911 (setq start (+ (match-beginning 0) 3)
1912 s (replace-match "&amp;" t t s)))))
1913 s)
1914
1915(defun org-export-html-format-desc (s)
1916 "Make sure the S is valid as a description in a link."
1917 (if (and s (not (get-text-property 1 'org-protected s)))
1918 (save-match-data
1919 (org-html-do-expand s))
1920 s))
1921
1922(defun org-export-html-format-image (src par-open)
1923 "Create image tag with source and attributes."
1924 (save-match-data
1925 (if (string-match "^ltxpng/" src)
ed21c5c8
CD
1926 (format "<img src=\"%s\" alt=\"%s\"/>"
1927 src (org-find-text-property-in-string 'org-latex-src src))
c8d0cf5c
CD
1928 (let* ((caption (org-find-text-property-in-string 'org-caption src))
1929 (attr (org-find-text-property-in-string 'org-attributes src))
1930 (label (org-find-text-property-in-string 'org-label src)))
ed21c5c8 1931 (setq caption (and caption (org-html-do-expand caption)))
8bfe682a
CD
1932 (concat
1933 (if caption
1934 (format "%s<div %sclass=\"figure\">
1935<p>"
1936 (if org-par-open "</p>\n" "")
3ab2c837 1937 (if label (format "id=\"%s\" " (org-solidify-link-text label)) "")))
8bfe682a 1938 (format "<img src=\"%s\"%s />"
c8d0cf5c
CD
1939 src
1940 (if (string-match "\\<alt=" (or attr ""))
1941 (concat " " attr )
8bfe682a
CD
1942 (concat " " attr " alt=\"" src "\"")))
1943 (if caption
1944 (format "</p>%s
1945</div>%s"
1946 (concat "\n<p>" caption "</p>")
1947 (if org-par-open "\n<p>" ""))))))))
c8d0cf5c
CD
1948
1949(defun org-export-html-get-bibliography ()
1950 "Find bibliography, cut it out and return it."
1951 (catch 'exit
1952 (let (beg end (cnt 1) bib)
1953 (save-excursion
1954 (goto-char (point-min))
1955 (when (re-search-forward "^[ \t]*<div \\(id\\|class\\)=\"bibliography\"" nil t)
1956 (setq beg (match-beginning 0))
1957 (while (re-search-forward "</?div\\>" nil t)
1958 (setq cnt (+ cnt (if (string= (match-string 0) "<div") +1 -1)))
1959 (when (= cnt 0)
1960 (and (looking-at ">") (forward-char 1))
1961 (setq bib (buffer-substring beg (point)))
1962 (delete-region beg (point))
1963 (throw 'exit bib))))
1964 nil))))
1965
1966(defvar org-table-number-regexp) ; defined in org-table.el
afe98dfa
CD
1967(defun org-format-table-html (lines olines &optional no-css)
1968 "Find out which HTML converter to use and return the HTML code.
1969NO-CSS is passed to the exporter."
c8d0cf5c
CD
1970 (if (stringp lines)
1971 (setq lines (org-split-string lines "\n")))
1972 (if (string-match "^[ \t]*|" (car lines))
1973 ;; A normal org table
afe98dfa 1974 (org-format-org-table-html lines nil no-css)
e66ba1df 1975 ;; Table made by table.el
3ab2c837
BG
1976 (or (org-format-table-table-html-using-table-generate-source
1977 olines (not org-export-prefer-native-exporter-for-tables))
1978 ;; We are here only when table.el table has NO col or row
1979 ;; spanning and the user prefers using org's own converter for
1980 ;; exporting of such simple table.el tables.
1981 (org-format-table-table-html lines))))
c8d0cf5c
CD
1982
1983(defvar org-table-number-fraction) ; defined in org-table.el
afe98dfa
CD
1984(defun org-format-org-table-html (lines &optional splice no-css)
1985 "Format a table into HTML.
1986LINES is a list of lines. Optional argument SPLICE means, do not
1987insert header and surrounding <table> tags, just format the lines.
1988Optional argument NO-CSS means use XHTML attributes instead of CSS
1989for formatting. This is required for the DocBook exporter."
c8d0cf5c
CD
1990 (require 'org-table)
1991 ;; Get rid of hlines at beginning and end
1992 (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
1993 (setq lines (nreverse lines))
1994 (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
1995 (setq lines (nreverse lines))
1996 (when org-export-table-remove-special-lines
1997 ;; Check if the table has a marking column. If yes remove the
1998 ;; column and the special lines
1999 (setq lines (org-table-clean-before-export lines)))
2000
ed21c5c8
CD
2001 (let* ((caption (org-find-text-property-in-string 'org-caption (car lines)))
2002 (label (org-find-text-property-in-string 'org-label (car lines)))
e66ba1df
BG
2003 (col-cookies (org-find-text-property-in-string 'org-col-cookies
2004 (car lines)))
ed21c5c8
CD
2005 (attributes (org-find-text-property-in-string 'org-attributes
2006 (car lines)))
c8d0cf5c
CD
2007 (html-table-tag (org-export-splice-attributes
2008 html-table-tag attributes))
2009 (head (and org-export-highlight-first-table-line
2010 (delq nil (mapcar
2011 (lambda (x) (string-match "^[ \t]*|-" x))
2012 (cdr lines)))))
afe98dfa
CD
2013 (nline 0) fnum nfields i (cnt 0)
2014 tbopen line fields html gr colgropen rowstart rowend
2015 ali align aligns n)
ed21c5c8 2016 (setq caption (and caption (org-html-do-expand caption)))
e66ba1df
BG
2017 (when (and col-cookies org-table-clean-did-remove-column)
2018 (setq col-cookies
2019 (mapcar (lambda (x) (cons (1- (car x)) (cdr x))) col-cookies)))
c8d0cf5c
CD
2020 (if splice (setq head nil))
2021 (unless splice (push (if head "<thead>" "<tbody>") html))
2022 (setq tbopen t)
2023 (while (setq line (pop lines))
2024 (catch 'next-line
2025 (if (string-match "^[ \t]*|-" line)
2026 (progn
2027 (unless splice
2028 (push (if head "</thead>" "</tbody>") html)
2029 (if lines (push "<tbody>" html) (setq tbopen nil)))
2030 (setq head nil) ;; head ends here, first time around
2031 ;; ignore this line
2032 (throw 'next-line t)))
2033 ;; Break the line into fields
2034 (setq fields (org-split-string line "[ \t]*|[ \t]*"))
86fbb8ca
CD
2035 (unless fnum (setq fnum (make-vector (length fields) 0)
2036 nfields (length fnum)))
c8d0cf5c
CD
2037 (setq nline (1+ nline) i -1
2038 rowstart (eval (car org-export-table-row-tags))
2039 rowend (eval (cdr org-export-table-row-tags)))
2040 (push (concat rowstart
2041 (mapconcat
2042 (lambda (x)
afe98dfa 2043 (setq i (1+ i) ali (format "@@class%03d@@" i))
86fbb8ca 2044 (if (and (< i nfields) ; make sure no rogue line causes an error here
c8d0cf5c
CD
2045 (string-match org-table-number-regexp x))
2046 (incf (aref fnum i)))
2047 (cond
2048 (head
2049 (concat
afe98dfa
CD
2050 (format (car org-export-table-header-tags)
2051 "col" ali)
c8d0cf5c
CD
2052 x
2053 (cdr org-export-table-header-tags)))
2054 ((and (= i 0) org-export-html-table-use-header-tags-for-first-column)
2055 (concat
afe98dfa
CD
2056 (format (car org-export-table-header-tags)
2057 "row" ali)
c8d0cf5c
CD
2058 x
2059 (cdr org-export-table-header-tags)))
2060 (t
afe98dfa
CD
2061 (concat (format (car org-export-table-data-tags) ali)
2062 x
c8d0cf5c
CD
2063 (cdr org-export-table-data-tags)))))
2064 fields "")
2065 rowend)
2066 html)))
2067 (unless splice (if tbopen (push "</tbody>" html)))
2068 (unless splice (push "</table>\n" html))
2069 (setq html (nreverse html))
2070 (unless splice
8bfe682a 2071 ;; Put in col tags with the alignment (unfortunately often ignored...)
c8d0cf5c
CD
2072 (unless (car org-table-colgroup-info)
2073 (setq org-table-colgroup-info
2074 (cons :start (cdr org-table-colgroup-info))))
afe98dfa 2075 (setq i 0)
c8d0cf5c
CD
2076 (push (mapconcat
2077 (lambda (x)
afe98dfa
CD
2078 (setq gr (pop org-table-colgroup-info)
2079 i (1+ i)
e66ba1df
BG
2080 align (if (nth 1 (assoc i col-cookies))
2081 (cdr (assoc (nth 1 (assoc i col-cookies))
afe98dfa
CD
2082 '(("l" . "left") ("r" . "right")
2083 ("c" . "center"))))
2084 (if (> (/ (float x) nline)
2085 org-table-number-fraction)
2086 "right" "left")))
2087 (push align aligns)
2088 (format (if no-css
2089 "%s<col align=\"%s\" />%s"
2090 "%s<col class=\"%s\" />%s")
c8d0cf5c
CD
2091 (if (memq gr '(:start :startend))
2092 (prog1
afe98dfa
CD
2093 (if colgropen
2094 "</colgroup>\n<colgroup>"
2095 "<colgroup>")
c8d0cf5c
CD
2096 (setq colgropen t))
2097 "")
afe98dfa 2098 align
c8d0cf5c
CD
2099 (if (memq gr '(:end :startend))
2100 (progn (setq colgropen nil) "</colgroup>")
2101 "")))
2102 fnum "")
2103 html)
afe98dfa
CD
2104 (setq aligns (nreverse aligns))
2105 (if colgropen (setq html (cons (car html)
2106 (cons "</colgroup>" (cdr html)))))
c8d0cf5c
CD
2107 ;; Since the output of HTML table formatter can also be used in
2108 ;; DocBook document, we want to always include the caption to make
2109 ;; DocBook XML file valid.
2110 (push (format "<caption>%s</caption>" (or caption "")) html)
3ab2c837
BG
2111 (when label
2112 (setq html-table-tag (org-export-splice-attributes html-table-tag (format "id=\"%s\"" (org-solidify-link-text label)))))
c8d0cf5c 2113 (push html-table-tag html))
afe98dfa
CD
2114 (setq html (mapcar
2115 (lambda (x)
2116 (replace-regexp-in-string
2117 "@@class\\([0-9]+\\)@@"
2118 (lambda (txt)
2119 (if (not org-export-html-table-align-individual-fields)
2120 ""
2121 (setq n (string-to-number (match-string 1 txt)))
2122 (format (if no-css " align=\"%s\"" " class=\"%s\"")
2123 (or (nth n aligns) "left"))))
2124 x))
2125 html))
c8d0cf5c
CD
2126 (concat (mapconcat 'identity html "\n") "\n")))
2127
2128(defun org-export-splice-attributes (tag attributes)
2129 "Read attributes in string ATTRIBUTES, add and replace in HTML tag TAG."
2130 (if (not attributes)
2131 tag
2132 (let (oldatt newatt)
2133 (setq oldatt (org-extract-attributes-from-string tag)
2134 tag (pop oldatt)
2135 newatt (cdr (org-extract-attributes-from-string attributes)))
2136 (while newatt
2137 (setq oldatt (plist-put oldatt (pop newatt) (pop newatt))))
2138 (if (string-match ">" tag)
2139 (setq tag
2140 (replace-match (concat (org-attributes-to-string oldatt) ">")
2141 t t tag)))
2142 tag)))
2143
2144(defun org-format-table-table-html (lines)
2145 "Format a table generated by table.el into HTML.
2146This conversion does *not* use `table-generate-source' from table.el.
2147This has the advantage that Org-mode's HTML conversions can be used.
2148But it has the disadvantage, that no cell- or row-spanning is allowed."
2149 (let (line field-buffer
2150 (head org-export-highlight-first-table-line)
2151 fields html empty i)
2152 (setq html (concat html-table-tag "\n"))
2153 (while (setq line (pop lines))
2154 (setq empty "&nbsp;")
2155 (catch 'next-line
2156 (if (string-match "^[ \t]*\\+-" line)
2157 (progn
2158 (if field-buffer
2159 (progn
2160 (setq
2161 html
2162 (concat
2163 html
2164 "<tr>"
2165 (mapconcat
2166 (lambda (x)
2167 (if (equal x "") (setq x empty))
2168 (if head
2169 (concat
afe98dfa 2170 (format (car org-export-table-header-tags) "col" "")
c8d0cf5c
CD
2171 x
2172 (cdr org-export-table-header-tags))
afe98dfa 2173 (concat (format (car org-export-table-data-tags) "") x
c8d0cf5c
CD
2174 (cdr org-export-table-data-tags))))
2175 field-buffer "\n")
2176 "</tr>\n"))
2177 (setq head nil)
2178 (setq field-buffer nil)))
2179 ;; Ignore this line
2180 (throw 'next-line t)))
2181 ;; Break the line into fields and store the fields
2182 (setq fields (org-split-string line "[ \t]*|[ \t]*"))
2183 (if field-buffer
2184 (setq field-buffer (mapcar
2185 (lambda (x)
2186 (concat x "<br/>" (pop fields)))
2187 field-buffer))
2188 (setq field-buffer fields))))
2189 (setq html (concat html "</table>\n"))
2190 html))
2191
3ab2c837
BG
2192(defun org-format-table-table-html-using-table-generate-source (lines
2193 &optional
2194 spanned-only)
c8d0cf5c 2195 "Format a table into html, using `table-generate-source' from table.el.
3ab2c837
BG
2196Use SPANNED-ONLY to suppress exporting of simple table.el tables.
2197
2198When SPANNED-ONLY is nil, all table.el tables are exported. When
2199SPANNED-ONLY is non-nil, only tables with either row or column
2200spans are exported.
2201
2202This routine returns the generated source or nil as appropriate.
2203
2204Refer docstring of `org-export-prefer-native-exporter-for-tables'
2205for further information."
c8d0cf5c
CD
2206 (require 'table)
2207 (with-current-buffer (get-buffer-create " org-tmp1 ")
2208 (erase-buffer)
2209 (insert (mapconcat 'identity lines "\n"))
2210 (goto-char (point-min))
2211 (if (not (re-search-forward "|[^+]" nil t))
2212 (error "Error processing table"))
2213 (table-recognize-table)
3ab2c837
BG
2214 (when (or (not spanned-only)
2215 (let* ((dim (table-query-dimension))
2216 (c (nth 4 dim)) (r (nth 5 dim)) (cells (nth 6 dim)))
2217 (not (= (* c r) cells))))
2218 (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
2219 (table-generate-source 'html " org-tmp2 ")
2220 (set-buffer " org-tmp2 ")
2221 (buffer-substring (point-min) (point-max)))))
c8d0cf5c
CD
2222
2223(defun org-export-splice-style (style extra)
2224 "Splice EXTRA into STYLE, just before \"</style>\"."
2225 (if (and (stringp extra)
2226 (string-match "\\S-" extra)
2227 (string-match "</style>" style))
2228 (concat (substring style 0 (match-beginning 0))
2229 "\n" extra "\n"
2230 (substring style (match-beginning 0)))
2231 style))
2232
2233(defun org-html-handle-time-stamps (s)
2234 "Format time stamps in string S, or remove them."
2235 (catch 'exit
2236 (let (r b)
e66ba1df
BG
2237 (when org-maybe-keyword-time-regexp
2238 (while (string-match org-maybe-keyword-time-regexp s)
2239 (or b (setq b (substring s 0 (match-beginning 0))))
2240 (setq r (concat
2241 r (substring s 0 (match-beginning 0))
2242 " @<span class=\"timestamp-wrapper\">"
2243 (if (match-end 1)
2244 (format "@<span class=\"timestamp-kwd\">%s @</span>"
2245 (match-string 1 s)))
2246 (format " @<span class=\"timestamp\">%s@</span>"
2247 (substring
2248 (org-translate-time (match-string 3 s)) 1 -1))
2249 "@</span>")
2250 s (substring s (match-end 0)))))
c8d0cf5c
CD
2251 ;; Line break if line started and ended with time stamp stuff
2252 (if (not r)
2253 s
2254 (setq r (concat r s))
2255 (unless (string-match "\\S-" (concat b s))
2256 (setq r (concat r "@<br/>")))
2257 r))))
2258
2259(defvar htmlize-buffer-places) ; from htmlize.el
2260(defun org-export-htmlize-region-for-paste (beg end)
2261 "Convert the region to HTML, using htmlize.el.
2262This is much like `htmlize-region-for-paste', only that it uses
2263the settings define in the org-... variables."
2264 (let* ((htmlize-output-type org-export-htmlize-output-type)
2265 (htmlize-css-name-prefix org-export-htmlize-css-font-prefix)
2266 (htmlbuf (htmlize-region beg end)))
2267 (unwind-protect
2268 (with-current-buffer htmlbuf
2269 (buffer-substring (plist-get htmlize-buffer-places 'content-start)
2270 (plist-get htmlize-buffer-places 'content-end)))
2271 (kill-buffer htmlbuf))))
2272
2273;;;###autoload
2274(defun org-export-htmlize-generate-css ()
2275 "Create the CSS for all font definitions in the current Emacs session.
2276Use this to create face definitions in your CSS style file that can then
2277be used by code snippets transformed by htmlize.
2278This command just produces a buffer that contains class definitions for all
2279faces used in the current Emacs session. You can copy and paste the ones you
2280need into your CSS file.
2281
2282If you then set `org-export-htmlize-output-type' to `css', calls to
2283the function `org-export-htmlize-region-for-paste' will produce code
2284that uses these same face definitions."
2285 (interactive)
2286 (require 'htmlize)
2287 (and (get-buffer "*html*") (kill-buffer "*html*"))
2288 (with-temp-buffer
2289 (let ((fl (face-list))
2290 (htmlize-css-name-prefix "org-")
2291 (htmlize-output-type 'css)
2292 f i)
2293 (while (setq f (pop fl)
2294 i (and f (face-attribute f :inherit)))
2295 (when (and (symbolp f) (or (not i) (not (listp i))))
2296 (insert (org-add-props (copy-sequence "1") nil 'face f))))
2297 (htmlize-region (point-min) (point-max))))
e66ba1df 2298 (org-pop-to-buffer-same-window "*html*")
c8d0cf5c
CD
2299 (goto-char (point-min))
2300 (if (re-search-forward "<style" nil t)
2301 (delete-region (point-min) (match-beginning 0)))
2302 (if (re-search-forward "</style>" nil t)
2303 (delete-region (1+ (match-end 0)) (point-max)))
2304 (beginning-of-line 1)
2305 (if (looking-at " +") (replace-match ""))
2306 (goto-char (point-min)))
2307
2308(defun org-html-protect (s)
3ab2c837
BG
2309 "Convert characters to HTML equivalent.
2310Possible conversions are set in `org-export-html-protect-char-alist'."
2311 (let ((cl org-export-html-protect-char-alist) c)
2312 (while (setq c (pop cl))
2313 (let ((start 0))
2314 (while (string-match (car c) s start)
2315 (setq s (replace-match (cdr c) t t s)
2316 start (1+ (match-beginning 0))))))
2317 s))
c8d0cf5c
CD
2318
2319(defun org-html-expand (string)
86fbb8ca 2320 "Prepare STRING for HTML export. Apply all active conversions.
e66ba1df
BG
2321If there are links in the string, don't modify these. If STRING
2322is nil, return nil."
2323 (when string
2324 (let* ((re (concat org-bracket-link-regexp "\\|"
2325 (org-re "[ \t]+\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")))
2326 m s l res)
2327 (while (setq m (string-match re string))
2328 (setq s (substring string 0 m)
2329 l (match-string 0 string)
2330 string (substring string (match-end 0)))
2331 (push (org-html-do-expand s) res)
3ab2c837 2332 (push l res))
e66ba1df
BG
2333 (push (org-html-do-expand string) res)
2334 (apply 'concat (nreverse res)))))
c8d0cf5c
CD
2335
2336(defun org-html-do-expand (s)
2337 "Apply all active conversions to translate special ASCII to HTML."
2338 (setq s (org-html-protect s))
2339 (if org-export-html-expand
2340 (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
2341 (setq s (replace-match "<\\1>" t nil s))))
2342 (if org-export-with-emphasize
2343 (setq s (org-export-html-convert-emphasize s)))
2344 (if org-export-with-special-strings
2345 (setq s (org-export-html-convert-special-strings s)))
2346 (if org-export-with-sub-superscripts
2347 (setq s (org-export-html-convert-sub-super s)))
2348 (if org-export-with-TeX-macros
ed21c5c8
CD
2349 (let ((start 0) wd rep)
2350 (while (setq start (string-match "\\\\\\([a-zA-Z]+[0-9]*\\)\\({}\\)?"
c8d0cf5c
CD
2351 s start))
2352 (if (get-text-property (match-beginning 0) 'org-protected s)
2353 (setq start (match-end 0))
2354 (setq wd (match-string 1 s))
ed21c5c8
CD
2355 (if (setq rep (org-entity-get-representation wd 'html))
2356 (setq s (replace-match rep t t s))
c8d0cf5c
CD
2357 (setq start (+ start (length wd))))))))
2358 s)
2359
c8d0cf5c
CD
2360(defun org-export-html-convert-special-strings (string)
2361 "Convert special characters in STRING to HTML."
2362 (let ((all org-export-html-special-string-regexps)
2363 e a re rpl start)
2364 (while (setq a (pop all))
2365 (setq re (car a) rpl (cdr a) start 0)
2366 (while (string-match re string start)
2367 (if (get-text-property (match-beginning 0) 'org-protected string)
2368 (setq start (match-end 0))
2369 (setq string (replace-match rpl t nil string)))))
2370 string))
2371
2372(defun org-export-html-convert-sub-super (string)
2373 "Convert sub- and superscripts in STRING to HTML."
2374 (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
2375 (while (string-match org-match-substring-regexp string s)
2376 (cond
2377 ((and requireb (match-end 8)) (setq s (match-end 2)))
2378 ((get-text-property (match-beginning 2) 'org-protected string)
2379 (setq s (match-end 2)))
2380 (t
2381 (setq s (match-end 1)
2382 key (if (string= (match-string 2 string) "_") "sub" "sup")
2383 c (or (match-string 8 string)
2384 (match-string 6 string)
2385 (match-string 5 string))
2386 string (replace-match
2387 (concat (match-string 1 string)
2388 "<" key ">" c "</" key ">")
2389 t t string)))))
2390 (while (string-match "\\\\\\([_^]\\)" string)
2391 (setq string (replace-match (match-string 1 string) t t string)))
2392 string))
2393
2394(defun org-export-html-convert-emphasize (string)
2395 "Apply emphasis."
2396 (let ((s 0) rpl)
2397 (while (string-match org-emph-re string s)
2398 (if (not (equal
2399 (substring string (match-beginning 3) (1+ (match-beginning 3)))
2400 (substring string (match-beginning 4) (1+ (match-beginning 4)))))
2401 (setq s (match-beginning 0)
2402 rpl
2403 (concat
2404 (match-string 1 string)
2405 (nth 2 (assoc (match-string 3 string) org-emphasis-alist))
2406 (match-string 4 string)
2407 (nth 3 (assoc (match-string 3 string)
2408 org-emphasis-alist))
2409 (match-string 5 string))
2410 string (replace-match rpl t t string)
2411 s (+ s (- (length rpl) 2)))
2412 (setq s (1+ s))))
2413 string))
2414
2415(defun org-open-par ()
2416 "Insert <p>, but first close previous paragraph if any."
2417 (org-close-par-maybe)
2418 (insert "\n<p>")
2419 (setq org-par-open t))
2420(defun org-close-par-maybe ()
2421 "Close paragraph if there is one open."
2422 (when org-par-open
2423 (insert "</p>")
2424 (setq org-par-open nil)))
2425(defun org-close-li (&optional type)
2426 "Close <li> if necessary."
2427 (org-close-par-maybe)
2428 (insert (if (equal type "d") "</dd>\n" "</li>\n")))
2429
c8d0cf5c 2430(defvar body-only) ; dynamically scoped into this.
3ab2c837 2431(defun org-html-level-start (level title umax with-toc head-count &optional opt-plist)
c8d0cf5c
CD
2432 "Insert a new level in HTML export.
2433When TITLE is nil, just close all open levels."
2434 (org-close-par-maybe)
2435 (let* ((target (and title (org-get-text-property-any 0 'target title)))
ed21c5c8
CD
2436 (extra-targets (and target
2437 (assoc target org-export-target-aliases)))
2438 (extra-class (and title (org-get-text-property-any 0 'html-container-class title)))
2439 (preferred (and target
2440 (cdr (assoc target org-export-preferred-target-alist))))
c8d0cf5c 2441 (l org-level-max)
3ab2c837 2442 (num (plist-get opt-plist :section-numbers))
86fbb8ca 2443 snumber snu href suffix)
acedf35c 2444 (setq extra-targets (remove (or preferred target) extra-targets))
c8d0cf5c
CD
2445 (setq extra-targets
2446 (mapconcat (lambda (x)
3ab2c837
BG
2447 (setq x (org-solidify-link-text
2448 (if (org-uuidgen-p x) (concat "ID-" x) x)))
e66ba1df
BG
2449 (if (stringp org-export-html-headline-anchor-format)
2450 (format org-export-html-headline-anchor-format x x)
2451 ""))
c8d0cf5c
CD
2452 extra-targets
2453 ""))
2454 (while (>= l level)
2455 (if (aref org-levels-open (1- l))
2456 (progn
2457 (org-html-level-close l umax)
2458 (aset org-levels-open (1- l) nil)))
2459 (setq l (1- l)))
2460 (when title
2461 ;; If title is nil, this means this function is called to close
2462 ;; all levels, so the rest is done only if title is given
afe98dfa 2463 (when (string-match (org-re "\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") title)
c8d0cf5c
CD
2464 (setq title (replace-match
2465 (if org-export-with-tags
2466 (save-match-data
2467 (concat
2468 "&nbsp;&nbsp;&nbsp;<span class=\"tag\">"
2469 (mapconcat
2470 (lambda (x)
2471 (format "<span class=\"%s\">%s</span>"
2472 (org-export-html-get-tag-class-name x)
2473 x))
2474 (org-split-string (match-string 1 title) ":")
2475 "&nbsp;")
2476 "</span>"))
2477 "")
2478 t t title)))
2479 (if (> level umax)
2480 (progn
2481 (if (aref org-levels-open (1- level))
2482 (progn
2483 (org-close-li)
2484 (if target
3ab2c837 2485 (insert (format "<li id=\"%s\">" (org-solidify-link-text (or preferred target)))
acedf35c 2486 extra-targets title "<br/>\n")
c8d0cf5c
CD
2487 (insert "<li>" title "<br/>\n")))
2488 (aset org-levels-open (1- level) t)
2489 (org-close-par-maybe)
2490 (if target
3ab2c837 2491 (insert (format "<ul>\n<li id=\"%s\">" (org-solidify-link-text (or preferred target)))
c8d0cf5c
CD
2492 extra-targets title "<br/>\n")
2493 (insert "<ul>\n<li>" title "<br/>\n"))))
2494 (aset org-levels-open (1- level) t)
86fbb8ca 2495 (setq snumber (org-section-number level)
3ab2c837 2496 snu (replace-regexp-in-string "\\." "-" snumber))
c8d0cf5c 2497 (setq level (+ level org-export-html-toplevel-hlevel -1))
3ab2c837 2498 (if (and num (not body-only))
c8d0cf5c
CD
2499 (setq title (concat
2500 (format "<span class=\"section-number-%d\">%s</span>"
3ab2c837
BG
2501 level
2502 (if (and num
2503 (if (integerp num)
2504 ;; fix up num to take into
2505 ;; account the top-level
2506 ;; heading value
2507 (>= (+ num org-export-html-toplevel-hlevel -1)
2508 level)
2509 num))
2510 snumber
2511 ""))
c8d0cf5c
CD
2512 " " title)))
2513 (unless (= head-count 1) (insert "\n</div>\n"))
86fbb8ca 2514 (setq href (cdr (assoc (concat "sec-" snu) org-export-preferred-target-alist)))
3ab2c837
BG
2515 (setq suffix (org-solidify-link-text (or href snu)))
2516 (setq href (org-solidify-link-text (or href (concat "sec-" snu))))
ed21c5c8
CD
2517 (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"
2518 suffix level (if extra-class (concat " " extra-class) "")
2519 level href
c8d0cf5c
CD
2520 extra-targets
2521 title level level suffix))
2522 (org-open-par)))))
2523
2524(defun org-export-html-get-tag-class-name (tag)
2525 "Turn tag into a valid class name.
2526Replaces invalid characters with \"_\" and then prepends a prefix."
2527 (save-match-data
2528 (while (string-match "[^a-zA-Z0-9_]" tag)
2529 (setq tag (replace-match "_" t t tag))))
2530 (concat org-export-html-tag-class-prefix tag))
2531
2532(defun org-export-html-get-todo-kwd-class-name (kwd)
2533 "Turn todo keyword into a valid class name.
2534Replaces invalid characters with \"_\" and then prepends a prefix."
2535 (save-match-data
2536 (while (string-match "[^a-zA-Z0-9_]" kwd)
2537 (setq kwd (replace-match "_" t t kwd))))
2538 (concat org-export-html-todo-kwd-class-prefix kwd))
2539
2540(defun org-html-level-close (level max-outline-level)
2541 "Terminate one level in HTML export."
2542 (if (<= level max-outline-level)
2543 (insert "</div>\n")
2544 (org-close-li)
2545 (insert "</ul>\n")))
2546
3ab2c837
BG
2547(defun org-html-export-list-line (line pos struct prevs)
2548 "Insert list syntax in export buffer. Return LINE, maybe modified.
2549
2550POS is the item position or line position the line had before
2551modifications to buffer. STRUCT is the list structure. PREVS is
2552the alist of previous items."
2553 (let* ((get-type
2554 (function
2555 ;; Translate type of list containing POS to "d", "o" or
2556 ;; "u".
2557 (lambda (pos struct prevs)
2558 (let ((type (org-list-get-list-type pos struct prevs)))
2559 (cond
2560 ((eq 'ordered type) "o")
2561 ((eq 'descriptive type) "d")
2562 (t "u"))))))
2563 (get-closings
2564 (function
2565 ;; Return list of all items and sublists ending at POS, in
2566 ;; reverse order.
2567 (lambda (pos)
2568 (let (out)
2569 (catch 'exit
2570 (mapc (lambda (e)
2571 (let ((end (nth 6 e))
2572 (item (car e)))
2573 (cond
2574 ((= end pos) (push item out))
2575 ((>= item pos) (throw 'exit nil)))))
2576 struct))
2577 out)))))
2578 ;; First close any previous item, or list, ending at POS.
2579 (mapc (lambda (e)
2580 (let* ((lastp (= (org-list-get-last-item e struct prevs) e))
2581 (first-item (org-list-get-list-begin e struct prevs))
2582 (type (funcall get-type first-item struct prevs)))
2583 (org-close-par-maybe)
2584 ;; Ending for every item
2585 (org-close-li type)
2586 ;; We're ending last item of the list: end list.
2587 (when lastp
2588 (insert (format "</%sl>\n" type))
2589 (org-open-par))))
2590 (funcall get-closings pos))
2591 (cond
2592 ;; At an item: insert appropriate tags in export buffer.
2593 ((assq pos struct)
2594 (string-match
2595 (concat "[ \t]*\\(\\S-+[ \t]*\\)"
2596 "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
2597 "\\(?:\\(\\[[ X-]\\]\\)[ \t]+\\)?"
2598 "\\(?:\\(.*\\)[ \t]+::\\(?:[ \t]+\\|$\\)\\)?"
2599 "\\(.*\\)") line)
2600 (let* ((checkbox (match-string 3 line))
2601 (desc-tag (or (match-string 4 line) "???"))
2602 (body (or (match-string 5 line) ""))
2603 (list-beg (org-list-get-list-begin pos struct prevs))
2604 (firstp (= list-beg pos))
2605 ;; Always refer to first item to determine list type, in
2606 ;; case list is ill-formed.
2607 (type (funcall get-type list-beg struct prevs))
2608 (counter (let ((count-tmp (org-list-get-counter pos struct)))
2609 (cond
2610 ((not count-tmp) nil)
2611 ((string-match "[A-Za-z]" count-tmp)
2612 (- (string-to-char (upcase count-tmp)) 64))
2613 ((string-match "[0-9]+" count-tmp)
2614 count-tmp)))))
2615 (when firstp
2616 (org-close-par-maybe)
2617 (insert (format "<%sl>\n" type)))
2618 (insert (cond
2619 ((equal type "d")
2620 (format "<dt>%s</dt><dd>" desc-tag))
2621 ((and (equal type "o") counter)
2622 (format "<li value=\"%s\">" counter))
2623 (t "<li>")))
2624 ;; If line had a checkbox, some additional modification is required.
2625 (when checkbox
2626 (setq body
2627 (concat
2628 (cond
2629 ((string-match "X" checkbox) "<code>[X]</code> ")
2630 ((string-match " " checkbox) "<code>[&nbsp;]</code> ")
2631 (t "<code>[-]</code> "))
2632 body)))
2633 ;; Return modified line
2634 body))
2635 ;; At a list ender: go to next line (side-effects only).
2636 ((equal "ORG-LIST-END-MARKER" line) (throw 'nextline nil))
2637 ;; Not at an item: return line unchanged (side-effects only).
2638 (t line))))
2639
c8d0cf5c
CD
2640(provide 'org-html)
2641
c8d0cf5c 2642;;; org-html.el ends here