HTML export: Show UP and HOME links if defined
[org-mode.git] / lisp / org-html.el
blob5788093e5a5c7214a653dd8f7db4313115e3a8d3
1 ;;; org-html.el --- HTML export for Org-mode
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
4 ;; Free Software Foundation, Inc.
6 ;; Author: Carsten Dominik <carsten at orgmode dot org>
7 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; Homepage: http://orgmode.org
9 ;; Version: 6.28trans
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 ;;; Commentary:
29 (require 'org-exp)
31 (declare-function org-id-find-id-file "org-id" (id))
32 (declare-function htmlize-region "ext:htmlize" (beg end))
34 (defcustom org-export-html-footnotes-section "<div id=\"footnotes\">
35 <h2 class=\"footnotes\">%s: </h2>
36 <div id=\"text-footnotes\">
38 </div>
39 </div>"
40 "Format for the footnotes section.
41 Should contain a two instances of %s. The first will be replaced with the
42 language-specific word for \"Footnotes\", the second one will be replaced
43 by the footnotes themselves."
44 :group 'org-export-html
45 :type 'string)
47 (defgroup org-export-html nil
48 "Options specific for HTML export of Org-mode files."
49 :tag "Org Export HTML"
50 :group 'org-export)
52 (defcustom org-export-html-coding-system nil
53 "Coding system for HTML export, defaults to buffer-file-coding-system."
54 :group 'org-export-html
55 :type 'coding-system)
57 (defcustom org-export-html-extension "html"
58 "The extension for exported HTML files."
59 :group 'org-export-html
60 :type 'string)
62 (defcustom org-export-html-xml-declaration
63 '(("html" . "<?xml version=\"1.0\" encoding=\"%s\"?>")
64 ("php" . "<?php echo \"<?xml version=\\\"1.0\\\" encoding=\\\"%s\\\" ?>\"; ?>"))
65 "The extension for exported HTML files.
66 %s will be replaced with the charset of the exported file.
67 This may be a string, or an alist with export extensions
68 and corresponding declarations."
69 :group 'org-export-html
70 :type '(choice
71 (string :tag "Single declaration")
72 (repeat :tag "Dependent on extension"
73 (cons (string :tag "Extension")
74 (string :tag "Declaration")))))
76 (defcustom org-export-html-style-include-scripts t
77 "Non-nil means, include the javascript snippets in exported HTML files.
78 The actual script is defined in `org-export-html-scripts' and should
79 not be modified."
80 :group 'org-export-html
81 :type 'boolean)
83 (defconst org-export-html-scripts
84 "<script type=\"text/javascript\">
85 <!--/*--><![CDATA[/*><!--*/
86 function CodeHighlightOn(elem, id)
88 var target = document.getElementById(id);
89 if(null != target) {
90 elem.cacheClassElem = elem.className;
91 elem.cacheClassTarget = target.className;
92 target.className = \"code-highlighted\";
93 elem.className = \"code-highlighted\";
96 function CodeHighlightOff(elem, id)
98 var target = document.getElementById(id);
99 if(elem.cacheClassElem)
100 elem.className = elem.cacheClassElem;
101 if(elem.cacheClassTarget)
102 target.className = elem.cacheClassTarget;
104 /*]]>*///-->
105 </script>"
106 "Basic javascript that is needed by HTML files produced by Org-mode.")
108 (defconst org-export-html-style-default
109 "<style type=\"text/css\">
110 <!--/*--><![CDATA[/*><!--*/
111 html { font-family: Times, serif; font-size: 12pt; }
112 .title { text-align: center; }
113 .todo { color: red; }
114 .done { color: green; }
115 .tag { background-color: #add8e6; font-weight:normal }
116 .target { }
117 .timestamp { color: #bebebe; }
118 .timestamp-kwd { color: #5f9ea0; }
119 p.verse { margin-left: 3% }
120 pre {
121 border: 1pt solid #AEBDCC;
122 background-color: #F3F5F7;
123 padding: 5pt;
124 font-family: courier, monospace;
125 font-size: 90%;
126 overflow:auto;
128 table { border-collapse: collapse; }
129 td, th { vertical-align: top; }
130 dt { font-weight: bold; }
131 div.figure { padding: 0.5em; }
132 div.figure p { text-align: center; }
133 .linenr { font-size:smaller }
134 .code-highlighted {background-color:#ffff00;}
135 .org-info-js_info-navigation { border-style:none; }
136 #org-info-js_console-label { font-size:10px; font-weight:bold;
137 white-space:nowrap; }
138 .org-info-js_search-highlight {background-color:#ffff00; color:#000000;
139 font-weight:bold; }
140 /*]]>*/-->
141 </style>"
142 "The default style specification for exported HTML files.
143 Please use the variables `org-export-html-style' and
144 `org-export-html-style-extra' to add to this style. If you wish to not
145 have the default style included, customize the variable
146 `org-export-html-style-include-default'.")
148 (defcustom org-export-html-style-include-default t
149 "Non-nil means, include the default style in exported HTML files.
150 The actual style is defined in `org-export-html-style-default' and should
151 not be modified. Use the variables `org-export-html-style' to add
152 your own style information."
153 :group 'org-export-html
154 :type 'boolean)
155 ;;;###autoload
156 (put 'org-export-html-style 'safe-local-variable 'booleanp)
158 (defcustom org-export-html-style ""
159 "Org-wide style definitions for exported HTML files.
161 This variable needs to contain the full HTML structure to provide a style,
162 including the surrounding HTML tags. If you set the value of this variable,
163 you should consider to include definitions for the following classes:
164 title, todo, done, timestamp, timestamp-kwd, tag, target.
166 For example, a valid value would be:
168 <style type=\"text/css\">
169 <![CDATA[
170 p { font-weight: normal; color: gray; }
171 h1 { color: black; }
172 .title { text-align: center; }
173 .todo, .timestamp-kwd { color: red; }
174 .done { color: green; }
176 </style>
178 If you'd like to refer to en external style file, use something like
180 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
182 As the value of this option simply gets inserted into the HTML <head> header,
183 you can \"misuse\" it to add arbitrary text to the header.
184 See also the variable `org-export-html-style-extra'."
185 :group 'org-export-html
186 :type 'string)
187 ;;;###autoload
188 (put 'org-export-html-style 'safe-local-variable 'stringp)
190 (defcustom org-export-html-style-extra ""
191 "Additional style information for HTML export.
192 The value of this variable is inserted into the HTML buffer right after
193 the value of `org-export-html-style'. Use this variable for per-file
194 settings of style information, and do not forget to surround the style
195 settings with <style>...</style> tags."
196 :group 'org-export-html
197 :type 'string)
198 ;;;###autoload
199 (put 'org-export-html-style-extra 'safe-local-variable 'stringp)
201 (defcustom org-export-html-tag-class-prefix ""
202 "Prefix to clas names for TODO keywords.
203 Each tag gets a class given by the tag itself, with this prefix.
204 The default prefix is empty because it is nice to just use the keyword
205 as a class name. But if you get into conflicts with other, existing
206 CSS classes, then this prefic can be very useful."
207 :group 'org-export-html
208 :type 'string)
210 (defcustom org-export-html-todo-kwd-class-prefix ""
211 "Prefix to clas names for TODO keywords.
212 Each TODO keyword gets a class given by the keyword itself, with this prefix.
213 The default prefix is empty because it is nice to just use the keyword
214 as a class name. But if you get into conflicts with other, existing
215 CSS classes, then this prefic can be very useful."
216 :group 'org-export-html
217 :type 'string)
219 (defcustom org-export-html-title-format "<h1 class=\"title\">%s</h1>\n"
220 "Format for typesetting the document title in HTML export."
221 :group 'org-export-html
222 :type 'string)
224 (defcustom org-export-html-home/up-format
225 "<div style=\"text-align:right;font-size:70%%;white-space:nowrap;\">
226 <a accesskey=\"h\" href=\"%s\"> UP </a>
228 <a accesskey=\"H\" href=\"%s\"> HOME </a>
229 </div>"
230 "Snippet used to insert the HOME and UP links. This is a format,
231 the first %s will receive the UP link, the second the HOME link.
232 If both `org-export-html-link-up' and `org-export-html-link-home' are
233 empty, the entire snippet will be ignored."
234 :group 'org-export-html
235 :type 'string)
237 (defcustom org-export-html-toplevel-hlevel 2
238 "The <H> level for level 1 headings in HTML export.
239 This is also important for the classes that will be wrapped around headlines
240 and outline structure. If this variable is 1, the top-level headlines will
241 be <h1>, and the corresponding classes will be outline-1, section-number-1,
242 and outline-text-1. If this is 2, all of these will get a 2 instead.
243 The default for this variable is 2, because we use <h1> for formatting the
244 document title."
245 :group 'org-export-html
246 :type 'string)
248 (defcustom org-export-html-link-org-files-as-html t
249 "Non-nil means, make file links to `file.org' point to `file.html'.
250 When org-mode is exporting an org-mode file to HTML, links to
251 non-html files are directly put into a href tag in HTML.
252 However, links to other Org-mode files (recognized by the
253 extension `.org.) should become links to the corresponding html
254 file, assuming that the linked org-mode file will also be
255 converted to HTML.
256 When nil, the links still point to the plain `.org' file."
257 :group 'org-export-html
258 :type 'boolean)
260 (defcustom org-export-html-inline-images 'maybe
261 "Non-nil means, inline images into exported HTML pages.
262 This is done using an <img> tag. When nil, an anchor with href is used to
263 link to the image. If this option is `maybe', then images in links with
264 an empty description will be inlined, while images with a description will
265 be linked only."
266 :group 'org-export-html
267 :type '(choice (const :tag "Never" nil)
268 (const :tag "Always" t)
269 (const :tag "When there is no description" maybe)))
271 (defcustom org-export-html-inline-image-extensions
272 '("png" "jpeg" "jpg" "gif")
273 "Extensions of image files that can be inlined into HTML."
274 :group 'org-export-html
275 :type '(repeat (string :tag "Extension")))
277 (defcustom org-export-html-table-tag
278 "<table border=\"2\" cellspacing=\"0\" cellpadding=\"6\" rules=\"groups\" frame=\"hsides\">"
279 "The HTML tag that is used to start a table.
280 This must be a <table> tag, but you may change the options like
281 borders and spacing."
282 :group 'org-export-html
283 :type 'string)
285 (defcustom org-export-table-header-tags '("<th scope=\"%s\">" . "</th>")
286 "The opening tag for table header fields.
287 This is customizable so that alignment options can be specified.
288 %s will be filled with the scope of the field, either row or col.
289 See also the variable `org-export-html-table-use-header-tags-for-first-column'."
290 :group 'org-export-tables
291 :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
293 (defcustom org-export-table-data-tags '("<td>" . "</td>")
294 "The opening tag for table data fields.
295 This is customizable so that alignment options can be specified."
296 :group 'org-export-tables
297 :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
299 (defcustom org-export-table-row-tags '("<tr>" . "</tr>")
300 "The opening tag for table data fields.
301 This is customizable so that alignment options can be specified.
302 Instead of strings, these can be Lisp forms that will be evaluated
303 for each row in order to construct the table row tags. During evaluation,
304 the variable `head' will be true when this is a header line, nil when this
305 is a body line. And the variable `nline' will contain the line number,
306 starting from 1 in the first header line. For example
308 (setq org-export-table-row-tags
309 (cons '(if head
310 \"<tr>\"
311 (if (= (mod nline 2) 1)
312 \"<tr class=\\\"tr-odd\\\">\"
313 \"<tr class=\\\"tr-even\\\">\"))
314 \"</tr>\"))
316 will give even lines the class \"tr-even\" and odd lines the class \"tr-odd\"."
317 :group 'org-export-tables
318 :type '(cons
319 (choice :tag "Opening tag"
320 (string :tag "Specify")
321 (sexp))
322 (choice :tag "Closing tag"
323 (string :tag "Specify")
324 (sexp))))
328 (defcustom org-export-html-table-use-header-tags-for-first-column nil
329 "Non-nil means, format column one in tables with header tags.
330 When nil, also column one will use data tags."
331 :group 'org-export-tables
332 :type 'boolean)
334 (defcustom org-export-html-validation-link nil
335 "Non-nil means, add validationlink to postamble of HTML exported files."
336 :group 'org-export-html
337 :type '(choice
338 (const :tag "Nothing" nil)
339 (const :tag "XHTML 1.0" "<p class=\"xhtml-validation\"><a href=\"http://validator.w3.org/check?uri=referer\">Validate XHTML 1.0</a></p>")
340 (string :tag "Specify full HTML")))
343 (defcustom org-export-html-with-timestamp nil
344 "If non-nil, write `org-export-html-html-helper-timestamp'
345 into the exported HTML text. Otherwise, the buffer will just be saved
346 to a file."
347 :group 'org-export-html
348 :type 'boolean)
350 (defcustom org-export-html-html-helper-timestamp
351 "<br/><br/><hr><p><!-- hhmts start --> <!-- hhmts end --></p>\n"
352 "The HTML tag used as timestamp delimiter for HTML-helper-mode."
353 :group 'org-export-html
354 :type 'string)
356 (defgroup org-export-htmlize nil
357 "Options for processing examples with htmlize.el."
358 :tag "Org Export Htmlize"
359 :group 'org-export-html)
361 (defcustom org-export-htmlize-output-type 'inline-css
362 "Output type to be used by htmlize when formatting code snippets.
363 We use as default `inline-css', in order to make the resulting
364 HTML self-containing.
365 However, this will fail when using Emacs in batch mode for export, because
366 then no rich font definitions are in place. It will also not be good if
367 people with different Emacs setup contribute HTML files to a website,
368 because the fonts will represent the individual setups. In these cases,
369 it is much better to let Org/Htmlize assign classes only, and to use
370 a style file to define the look of these classes.
371 To get a start for your css file, start Emacs session nnd make sure that
372 all the faces you are interested in are defined, for example by loading files
373 in all modes you want. Then, use the command
374 \\[org-export-htmlize-generate-css] to extract class definitions."
375 :group 'org-export-htmlize
376 :type '(choice (const css) (const inline-css)))
378 (defcustom org-export-htmlize-css-font-prefix "org-"
379 "The prefix for CSS class names for htmlize font specifications."
380 :group 'org-export-htmlize
381 :type 'string)
383 (defcustom org-export-htmlized-org-css-url nil
384 "URL pointing to a CSS file defining text colors for htmlized Emacs buffers.
385 Normally when creating an htmlized version of an Org buffer, htmlize will
386 create CSS to define the font colors. However, this does not work when
387 converting in batch mode, and it also can look bad if different people
388 with different fontification setup work on the same website.
389 When this variable is non-nil, creating an htmlized version of an Org buffer
390 using `org-export-as-org' will remove the internal CSS section and replace it
391 with a link to this URL."
392 :group 'org-export-htmlize
393 :type '(choice
394 (const :tag "Keep internal css" nil)
395 (string :tag "URL or local href")))
397 ;;; Variables, constants, and parameter plists
399 (defvar org-export-html-preamble nil
400 "Preamble, to be inserted just before <body>. Set by publishing functions.
401 This may also be a function, building and inserting the preamble.")
402 (defvar org-export-html-postamble nil
403 "Preamble, to be inserted just after </body>. Set by publishing functions.
404 This may also be a function, building and inserting the postamble.")
405 (defvar org-export-html-auto-preamble t
406 "Should default preamble be inserted? Set by publishing functions.")
407 (defvar org-export-html-auto-postamble t
408 "Should default postamble be inserted? Set by publishing functions.")
410 ;;; Hooks
412 (defvar org-export-html-after-blockquotes-hook nil
413 "Hook run during HTML export, after blockquote, verse, center are done.")
415 ;;; HTML export
417 (defun org-export-html-preprocess (parameters)
418 ;; Convert LaTeX fragments to images
419 (when (and org-current-export-file
420 (plist-get parameters :LaTeX-fragments))
421 (org-format-latex
422 (concat "ltxpng/" (file-name-sans-extension
423 (file-name-nondirectory
424 org-current-export-file)))
425 org-current-export-dir nil "Creating LaTeX image %s"))
426 (message "Exporting..."))
428 ;;;###autoload
429 (defun org-export-as-html-and-open (arg)
430 "Export the outline as HTML and immediately open it with a browser.
431 If there is an active region, export only the region.
432 The prefix ARG specifies how many levels of the outline should become
433 headlines. The default is 3. Lower levels will become bulleted lists."
434 (interactive "P")
435 (org-export-as-html arg 'hidden)
436 (org-open-file buffer-file-name))
438 ;;;###autoload
439 (defun org-export-as-html-batch ()
440 "Call `org-export-as-html', may be used in batch processing as
441 emacs --batch
442 --load=$HOME/lib/emacs/org.el
443 --eval \"(setq org-export-headline-levels 2)\"
444 --visit=MyFile --funcall org-export-as-html-batch"
445 (org-export-as-html org-export-headline-levels 'hidden))
447 ;;;###autoload
448 (defun org-export-as-html-to-buffer (arg)
449 "Call `org-export-as-html` with output to a temporary buffer.
450 No file is created. The prefix ARG is passed through to `org-export-as-html'."
451 (interactive "P")
452 (org-export-as-html arg nil nil "*Org HTML Export*")
453 (when org-export-show-temporary-export-buffer
454 (switch-to-buffer-other-window "*Org HTML Export*")))
456 ;;;###autoload
457 (defun org-replace-region-by-html (beg end)
458 "Assume the current region has org-mode syntax, and convert it to HTML.
459 This can be used in any buffer. For example, you could write an
460 itemized list in org-mode syntax in an HTML buffer and then use this
461 command to convert it."
462 (interactive "r")
463 (let (reg html buf pop-up-frames)
464 (save-window-excursion
465 (if (org-mode-p)
466 (setq html (org-export-region-as-html
467 beg end t 'string))
468 (setq reg (buffer-substring beg end)
469 buf (get-buffer-create "*Org tmp*"))
470 (with-current-buffer buf
471 (erase-buffer)
472 (insert reg)
473 (org-mode)
474 (setq html (org-export-region-as-html
475 (point-min) (point-max) t 'string)))
476 (kill-buffer buf)))
477 (delete-region beg end)
478 (insert html)))
480 ;;;###autoload
481 (defun org-export-region-as-html (beg end &optional body-only buffer)
482 "Convert region from BEG to END in org-mode buffer to HTML.
483 If prefix arg BODY-ONLY is set, omit file header, footer, and table of
484 contents, and only produce the region of converted text, useful for
485 cut-and-paste operations.
486 If BUFFER is a buffer or a string, use/create that buffer as a target
487 of the converted HTML. If BUFFER is the symbol `string', return the
488 produced HTML as a string and leave not buffer behind. For example,
489 a Lisp program could call this function in the following way:
491 (setq html (org-export-region-as-html beg end t 'string))
493 When called interactively, the output buffer is selected, and shown
494 in a window. A non-interactive call will only return the buffer."
495 (interactive "r\nP")
496 (when (interactive-p)
497 (setq buffer "*Org HTML Export*"))
498 (let ((transient-mark-mode t) (zmacs-regions t)
499 ext-plist rtn)
500 (setq ext-plist (plist-put ext-plist :ignore-subree-p t))
501 (goto-char end)
502 (set-mark (point)) ;; to activate the region
503 (goto-char beg)
504 (setq rtn (org-export-as-html
505 nil nil ext-plist
506 buffer body-only))
507 (if (fboundp 'deactivate-mark) (deactivate-mark))
508 (if (and (interactive-p) (bufferp rtn))
509 (switch-to-buffer-other-window rtn)
510 rtn)))
512 (defvar html-table-tag nil) ; dynamically scoped into this.
513 (defvar org-par-open nil)
514 ;;;###autoload
515 (defun org-export-as-html (arg &optional hidden ext-plist
516 to-buffer body-only pub-dir)
517 "Export the outline as a pretty HTML file.
518 If there is an active region, export only the region. The prefix
519 ARG specifies how many levels of the outline should become
520 headlines. The default is 3. Lower levels will become bulleted
521 lists. HIDDEN is obsolete and does nothing.
522 EXT-PLIST is a property list with external parameters overriding
523 org-mode's default settings, but still inferior to file-local
524 settings. When TO-BUFFER is non-nil, create a buffer with that
525 name and export to that buffer. If TO-BUFFER is the symbol
526 `string', don't leave any buffer behind but just return the
527 resulting HTML as a string. When BODY-ONLY is set, don't produce
528 the file header and footer, simply return the content of
529 <body>...</body>, without even the body tags themselves. When
530 PUB-DIR is set, use this as the publishing directory."
531 (interactive "P")
533 ;; Make sure we have a file name when we need it.
534 (when (and (not (or to-buffer body-only))
535 (not buffer-file-name))
536 (if (buffer-base-buffer)
537 (org-set-local 'buffer-file-name
538 (with-current-buffer (buffer-base-buffer)
539 buffer-file-name))
540 (error "Need a file name to be able to export.")))
542 (message "Exporting...")
543 (setq-default org-todo-line-regexp org-todo-line-regexp)
544 (setq-default org-deadline-line-regexp org-deadline-line-regexp)
545 (setq-default org-done-keywords org-done-keywords)
546 (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp)
547 (let* ((opt-plist
548 (org-export-process-option-filters
549 (org-combine-plists (org-default-export-plist)
550 ext-plist
551 (org-infile-export-plist))))
552 (body-only (or body-only (plist-get opt-plist :body-only)))
553 (style (concat (if (plist-get opt-plist :style-include-default)
554 org-export-html-style-default)
555 (plist-get opt-plist :style)
556 (plist-get opt-plist :style-extra)
557 "\n"
558 (if (plist-get opt-plist :style-include-scripts)
559 org-export-html-scripts)))
560 (html-extension (plist-get opt-plist :html-extension))
561 (link-validate (plist-get opt-plist :link-validation-function))
562 valid thetoc have-headings first-heading-pos
563 (odd org-odd-levels-only)
564 (region-p (org-region-active-p))
565 (rbeg (and region-p (region-beginning)))
566 (rend (and region-p (region-end)))
567 (subtree-p
568 (if (plist-get opt-plist :ignore-subree-p)
570 (when region-p
571 (save-excursion
572 (goto-char rbeg)
573 (and (org-at-heading-p)
574 (>= (org-end-of-subtree t t) rend))))))
575 (level-offset (if subtree-p
576 (save-excursion
577 (goto-char rbeg)
578 (+ (funcall outline-level)
579 (if org-odd-levels-only 1 0)))
581 (opt-plist (setq org-export-opt-plist
582 (if subtree-p
583 (org-export-add-subtree-options opt-plist rbeg)
584 opt-plist)))
585 ;; The following two are dynamically scoped into other
586 ;; routines below.
587 (org-current-export-dir
588 (or pub-dir (org-export-directory :html opt-plist)))
589 (org-current-export-file buffer-file-name)
590 (level 0) (line "") (origline "") txt todo
591 (umax nil)
592 (umax-toc nil)
593 (filename (if to-buffer nil
594 (expand-file-name
595 (concat
596 (file-name-sans-extension
597 (or (and subtree-p
598 (org-entry-get (region-beginning)
599 "EXPORT_FILE_NAME" t))
600 (file-name-nondirectory buffer-file-name)))
601 "." html-extension)
602 (file-name-as-directory
603 (or pub-dir (org-export-directory :html opt-plist))))))
604 (current-dir (if buffer-file-name
605 (file-name-directory buffer-file-name)
606 default-directory))
607 (buffer (if to-buffer
608 (cond
609 ((eq to-buffer 'string) (get-buffer-create "*Org HTML Export*"))
610 (t (get-buffer-create to-buffer)))
611 (find-file-noselect filename)))
612 (org-levels-open (make-vector org-level-max nil))
613 (date (plist-get opt-plist :date))
614 (author (plist-get opt-plist :author))
615 (title (or (and subtree-p (org-export-get-title-from-subtree))
616 (plist-get opt-plist :title)
617 (and (not
618 (plist-get opt-plist :skip-before-1st-heading))
619 (org-export-grab-title-from-buffer))
620 (and buffer-file-name
621 (file-name-sans-extension
622 (file-name-nondirectory buffer-file-name)))
623 "UNTITLED"))
624 (link-up (and (plist-get opt-plist :link-up)
625 (string-match "\\S-" (plist-get opt-plist :link-up))
626 (plist-get opt-plist :link-up)))
627 (link-home (and (plist-get opt-plist :link-home)
628 (string-match "\\S-" (plist-get opt-plist :link-home))
629 (plist-get opt-plist :link-home)))
630 (dummy (setq opt-plist (plist-put opt-plist :title title)))
631 (html-table-tag (plist-get opt-plist :html-table-tag))
632 (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
633 (quote-re (concat "^\\(\\*+\\)\\([ \t]+" org-quote-string "\\>\\)"))
634 (inquote nil)
635 (infixed nil)
636 (inverse nil)
637 (in-local-list nil)
638 (local-list-type nil)
639 (local-list-indent nil)
640 (llt org-plain-list-ordered-item-terminator)
641 (email (plist-get opt-plist :email))
642 (language (plist-get opt-plist :language))
643 (keywords (plist-get opt-plist :keywords))
644 (description (plist-get opt-plist :description))
645 (lang-words nil)
646 (head-count 0) cnt
647 (start 0)
648 (coding-system (and (boundp 'buffer-file-coding-system)
649 buffer-file-coding-system))
650 (coding-system-for-write (or org-export-html-coding-system
651 coding-system))
652 (save-buffer-coding-system (or org-export-html-coding-system
653 coding-system))
654 (charset (and coding-system-for-write
655 (fboundp 'coding-system-get)
656 (coding-system-get coding-system-for-write
657 'mime-charset)))
658 (region
659 (buffer-substring
660 (if region-p (region-beginning) (point-min))
661 (if region-p (region-end) (point-max))))
662 (lines
663 (org-split-string
664 (org-export-preprocess-string
665 region
666 :emph-multiline t
667 :for-html t
668 :skip-before-1st-heading
669 (plist-get opt-plist :skip-before-1st-heading)
670 :drawers (plist-get opt-plist :drawers)
671 :todo-keywords (plist-get opt-plist :todo-keywords)
672 :tags (plist-get opt-plist :tags)
673 :priority (plist-get opt-plist :priority)
674 :footnotes (plist-get opt-plist :footnotes)
675 :timestamps (plist-get opt-plist :timestamps)
676 :archived-trees
677 (plist-get opt-plist :archived-trees)
678 :select-tags (plist-get opt-plist :select-tags)
679 :exclude-tags (plist-get opt-plist :exclude-tags)
680 :add-text
681 (plist-get opt-plist :text)
682 :LaTeX-fragments
683 (plist-get opt-plist :LaTeX-fragments))
684 "[\r\n]"))
685 table-open type
686 table-buffer table-orig-buffer
687 ind item-type starter didclose
688 rpl path attr desc descp desc1 desc2 link
689 snumber fnc item-tag
690 footnotes footref-seen
691 id-file href
694 (let ((inhibit-read-only t))
695 (org-unmodified
696 (remove-text-properties (point-min) (point-max)
697 '(:org-license-to-kill t))))
699 (message "Exporting...")
701 (setq org-min-level (org-get-min-level lines level-offset))
702 (setq org-last-level org-min-level)
703 (org-init-section-numbers)
705 (cond
706 ((and date (string-match "%" date))
707 (setq date (format-time-string date)))
708 (date)
709 (t (setq date (format-time-string "%Y-%m-%d %T %Z"))))
711 ;; Get the language-dependent settings
712 (setq lang-words (or (assoc language org-export-language-setup)
713 (assoc "en" org-export-language-setup)))
715 ;; Switch to the output buffer
716 (set-buffer buffer)
717 (let ((inhibit-read-only t)) (erase-buffer))
718 (fundamental-mode)
719 (org-install-letbind)
721 (and (fboundp 'set-buffer-file-coding-system)
722 (set-buffer-file-coding-system coding-system-for-write))
724 (let ((case-fold-search nil)
725 (org-odd-levels-only odd))
726 ;; create local variables for all options, to make sure all called
727 ;; functions get the correct information
728 (mapc (lambda (x)
729 (set (make-local-variable (nth 2 x))
730 (plist-get opt-plist (car x))))
731 org-export-plist-vars)
732 (setq umax (if arg (prefix-numeric-value arg)
733 org-export-headline-levels))
734 (setq umax-toc (if (integerp org-export-with-toc)
735 (min org-export-with-toc umax)
736 umax))
737 (unless body-only
738 ;; File header
739 (insert (format
741 <!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
742 \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
743 <html xmlns=\"http://www.w3.org/1999/xhtml\"
744 lang=\"%s\" xml:lang=\"%s\">
745 <head>
747 <title>%s</title>
748 <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>
749 <meta name=\"generator\" content=\"Org-mode\"/>
750 <meta name=\"generated\" content=\"%s\"/>
751 <meta name=\"author\" content=\"%s\"/>
752 <meta name=\"description\" content=\"%s\"/>
753 <meta name=\"keywords\" content=\"%s\"/>
755 </head>
756 <body>
757 <div id=\"content\">
759 (format
760 (or (and (stringp org-export-html-xml-declaration)
761 org-export-html-xml-declaration)
762 (cdr (assoc html-extension org-export-html-xml-declaration))
763 (cdr (assoc "html" org-export-html-xml-declaration))
766 (or charset "iso-8859-1"))
767 language language
768 (if (or link-up link-home)
769 (concat
770 (format org-export-html-home/up-format
771 (or link-up link-home)
772 (or link-home link-up))
773 "\n")
775 (org-html-expand title)
776 (or charset "iso-8859-1")
777 date author description keywords
778 style))
780 (org-export-html-insert-plist-item opt-plist :preamble opt-plist)
782 (when (plist-get opt-plist :auto-preamble)
783 (if title (insert (format org-export-html-title-format
784 (org-html-expand title))))))
786 (if (and org-export-with-toc (not body-only))
787 (progn
788 (push (format "<h%d>%s</h%d>\n"
789 org-export-html-toplevel-hlevel
790 (nth 3 lang-words)
791 org-export-html-toplevel-hlevel)
792 thetoc)
793 (push "<div id=\"text-table-of-contents\">\n" thetoc)
794 (push "<ul>\n<li>" thetoc)
795 (setq lines
796 (mapcar '(lambda (line)
797 (if (string-match org-todo-line-regexp line)
798 ;; This is a headline
799 (progn
800 (setq have-headings t)
801 (setq level (- (match-end 1) (match-beginning 1)
802 level-offset)
803 level (org-tr-level level)
804 txt (save-match-data
805 (org-html-expand
806 (org-export-cleanup-toc-line
807 (match-string 3 line))))
808 todo
809 (or (and org-export-mark-todo-in-toc
810 (match-beginning 2)
811 (not (member (match-string 2 line)
812 org-done-keywords)))
813 ; TODO, not DONE
814 (and org-export-mark-todo-in-toc
815 (= level umax-toc)
816 (org-search-todo-below
817 line lines level))))
818 (if (string-match
819 (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") txt)
820 (setq txt (replace-match "&nbsp;&nbsp;&nbsp;<span class=\"tag\"> \\1</span>" t nil txt)))
821 (if (string-match quote-re0 txt)
822 (setq txt (replace-match "" t t txt)))
823 (setq snumber (org-section-number level))
824 (if org-export-with-section-numbers
825 (setq txt (concat snumber " " txt)))
826 (if (<= level (max umax umax-toc))
827 (setq head-count (+ head-count 1)))
828 (if (<= level umax-toc)
829 (progn
830 (if (> level org-last-level)
831 (progn
832 (setq cnt (- level org-last-level))
833 (while (>= (setq cnt (1- cnt)) 0)
834 (push "\n<ul>\n<li>" thetoc))
835 (push "\n" thetoc)))
836 (if (< level org-last-level)
837 (progn
838 (setq cnt (- org-last-level level))
839 (while (>= (setq cnt (1- cnt)) 0)
840 (push "</li>\n</ul>" thetoc))
841 (push "\n" thetoc)))
842 ;; Check for targets
843 (while (string-match org-any-target-regexp line)
844 (setq line (replace-match
845 (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
846 t t line)))
847 (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
848 (setq txt (replace-match "" t t txt)))
849 (setq href (format "sec-%s" snumber))
850 (setq href (or (cdr (assoc href org-export-preferred-target-alist)) href))
851 (push
852 (format
853 (if todo
854 "</li>\n<li><a href=\"#%s\"><span class=\"todo\">%s</span></a>"
855 "</li>\n<li><a href=\"#%s\">%s</a>")
856 href txt) thetoc)
858 (setq org-last-level level))
860 line)
861 lines))
862 (while (> org-last-level (1- org-min-level))
863 (setq org-last-level (1- org-last-level))
864 (push "</li>\n</ul>\n" thetoc))
865 (push "</div>\n" thetoc)
866 (setq thetoc (if have-headings (nreverse thetoc) nil))))
868 (setq head-count 0)
869 (org-init-section-numbers)
871 (org-open-par)
873 (while (setq line (pop lines) origline line)
874 (catch 'nextline
876 ;; end of quote section?
877 (when (and inquote (string-match "^\\*+ " line))
878 (insert "</pre>\n")
879 (org-open-par)
880 (setq inquote nil))
881 ;; inside a quote section?
882 (when inquote
883 (insert (org-html-protect line) "\n")
884 (throw 'nextline nil))
886 ;; Fixed-width, verbatim lines (examples)
887 (when (and org-export-with-fixed-width
888 (string-match "^[ \t]*:\\(\\([ \t]\\|$\\)\\(.*\\)\\)" line))
889 (when (not infixed)
890 (setq infixed t)
891 (org-close-par-maybe)
893 (insert "<pre class=\"example\">\n"))
894 (insert (org-html-protect (match-string 3 line)) "\n")
895 (when (or (not lines)
896 (not (string-match "^[ \t]*:\\(\\([ \t]\\|$\\)\\(.*\\)\\)"
897 (car lines))))
898 (setq infixed nil)
899 (insert "</pre>\n")
900 (org-open-par))
901 (throw 'nextline nil))
903 (org-export-html-close-lists-maybe line)
905 ;; Protected HTML
906 (when (get-text-property 0 'org-protected line)
907 (let (par (ind (get-text-property 0 'original-indentation line)))
908 (when (re-search-backward
909 "\\(<p>\\)\\([ \t\r\n]*\\)\\=" (- (point) 100) t)
910 (setq par (match-string 1))
911 (replace-match "\\2\n"))
912 (insert line "\n")
913 (while (and lines
914 (or (= (length (car lines)) 0)
915 (not ind)
916 (equal ind (get-text-property 0 'original-indentation (car lines))))
917 (or (= (length (car lines)) 0)
918 (get-text-property 0 'org-protected (car lines))))
919 (insert (pop lines) "\n"))
920 (and par (insert "<p>\n")))
921 (throw 'nextline nil))
923 ;; Horizontal line
924 (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line)
925 (if org-par-open
926 (insert "\n</p>\n<hr/>\n<p>\n")
927 (insert "\n<hr/>\n"))
928 (throw 'nextline nil))
930 ;; Blockquotes, verse, and center
931 (when (equal "ORG-BLOCKQUOTE-START" line)
932 (org-close-par-maybe)
933 (insert "<blockquote>\n")
934 (org-open-par)
935 (throw 'nextline nil))
936 (when (equal "ORG-BLOCKQUOTE-END" line)
937 (org-close-par-maybe)
938 (insert "\n</blockquote>\n")
939 (org-open-par)
940 (throw 'nextline nil))
941 (when (equal "ORG-VERSE-START" line)
942 (org-close-par-maybe)
943 (insert "\n<p class=\"verse\">\n")
944 (setq inverse t)
945 (throw 'nextline nil))
946 (when (equal "ORG-VERSE-END" line)
947 (insert "</p>\n")
948 (org-open-par)
949 (setq inverse nil)
950 (throw 'nextline nil))
951 (when (equal "ORG-CENTER-START" line)
952 (org-close-par-maybe)
953 (insert "\n<div style=\"text-align: center\">")
954 (org-open-par)
955 (throw 'nextline nil))
956 (when (equal "ORG-CENTER-END" line)
957 (org-close-par-maybe)
958 (insert "\n</div>")
959 (org-open-par)
960 (throw 'nextline nil))
961 (run-hooks 'org-export-html-after-blockquotes-hook)
962 (when inverse
963 (let ((i (org-get-string-indentation line)))
964 (if (> i 0)
965 (setq line (concat (mapconcat 'identity
966 (make-list (* 2 i) "\\nbsp") "")
967 " " (org-trim line))))
968 (unless (string-match "\\\\\\\\[ \t]*$" line)
969 (setq line (concat line "\\\\")))))
971 ;; make targets to anchors
972 (while (string-match
973 "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line)
974 (cond
975 ((match-end 2)
976 (setq line (replace-match
977 (format
978 "@<a name=\"%s\" id=\"%s\">@</a>"
979 (org-solidify-link-text (match-string 1 line))
980 (org-solidify-link-text (match-string 1 line)))
981 t t line)))
982 ((and org-export-with-toc (equal (string-to-char line) ?*))
983 ;; FIXME: NOT DEPENDENT on TOC?????????????????????
984 (setq line (replace-match
985 (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
986 ; (concat "@<i>" (match-string 1 line) "@</i> ")
987 t t line)))
989 (setq line (replace-match
990 (concat "@<a name=\""
991 (org-solidify-link-text (match-string 1 line))
992 "\" class=\"target\">" (match-string 1 line) "@</a> ")
993 t t line)))))
995 (setq line (org-html-handle-time-stamps line))
997 ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
998 ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
999 ;; Also handle sub_superscripts and checkboxes
1000 (or (string-match org-table-hline-regexp line)
1001 (setq line (org-html-expand line)))
1003 ;; Format the links
1004 (setq start 0)
1005 (while (string-match org-bracket-link-analytic-regexp++ line start)
1006 (setq start (match-beginning 0))
1007 (setq path (save-match-data (org-link-unescape
1008 (match-string 3 line))))
1009 (setq type (cond
1010 ((match-end 2) (match-string 2 line))
1011 ((save-match-data
1012 (or (file-name-absolute-p path)
1013 (string-match "^\\.\\.?/" path)))
1014 "file")
1015 (t "internal")))
1016 (setq path (org-extract-attributes (org-link-unescape path)))
1017 (setq attr (get-text-property 0 'org-attributes path))
1018 (setq desc1 (if (match-end 5) (match-string 5 line))
1019 desc2 (if (match-end 2) (concat type ":" path) path)
1020 descp (and desc1 (not (equal desc1 desc2)))
1021 desc (or desc1 desc2))
1022 ;; Make an image out of the description if that is so wanted
1023 (when (and descp (org-file-image-p
1024 desc org-export-html-inline-image-extensions))
1025 (save-match-data
1026 (if (string-match "^file:" desc)
1027 (setq desc (substring desc (match-end 0)))))
1028 (setq desc (org-add-props
1029 (concat "<img src=\"" desc "\"/>")
1030 '(org-protected t))))
1031 ;; FIXME: do we need to unescape here somewhere?
1032 (cond
1033 ((equal type "internal")
1034 (setq rpl
1035 (concat
1036 "<a href=\""
1037 (if (= (string-to-char path) ?#) "" "#")
1038 (org-solidify-link-text
1039 (save-match-data (org-link-unescape path)) nil)
1040 "\"" attr ">"
1041 (org-export-html-format-desc desc)
1042 "</a>")))
1043 ((and (equal type "id")
1044 (setq id-file (org-id-find-id-file path)))
1045 ;; This is an id: link to another file (if it was the same file,
1046 ;; it would have become an internal link...)
1047 (save-match-data
1048 (setq id-file (file-relative-name
1049 id-file (file-name-directory org-current-export-file)))
1050 (setq id-file (concat (file-name-sans-extension id-file)
1051 "." html-extension))
1052 (setq rpl (concat "<a href=\"" id-file "#"
1053 (if (org-uuidgen-p path) "ID-")
1054 path "\""
1055 attr ">"
1056 (org-export-html-format-desc desc)
1057 "</a>"))))
1058 ((member type '("http" "https"))
1059 ;; standard URL, just check if we need to inline an image
1060 (if (and (or (eq t org-export-html-inline-images)
1061 (and org-export-html-inline-images (not descp)))
1062 (org-file-image-p
1063 path org-export-html-inline-image-extensions))
1064 (setq rpl (org-export-html-format-image
1065 (concat type ":" path) org-par-open))
1066 (setq link (concat type ":" path))
1067 (setq rpl (concat "<a href=\""
1068 (org-export-html-format-href link)
1069 "\"" attr ">"
1070 (org-export-html-format-desc desc)
1071 "</a>"))))
1072 ((member type '("ftp" "mailto" "news"))
1073 ;; standard URL
1074 (setq link (concat type ":" path))
1075 (setq rpl (concat "<a href=\""
1076 (org-export-html-format-href link)
1077 "\"" attr ">"
1078 (org-export-html-format-desc desc)
1079 "</a>")))
1081 ((string= type "coderef")
1082 (setq rpl (format "<a href=\"#coderef-%s\" class=\"coderef\" onmouseover=\"CodeHighlightOn(this, 'coderef-%s');\" onmouseout=\"CodeHighlightOff(this, 'coderef-%s');\">%s</a>"
1083 path path path
1084 (format (org-export-get-coderef-format path (and descp desc))
1085 (cdr (assoc path org-export-code-refs))))))
1087 ((functionp (setq fnc (nth 2 (assoc type org-link-protocols))))
1088 ;; The link protocol has a function for format the link
1089 (setq rpl
1090 (save-match-data
1091 (funcall fnc (org-link-unescape path) desc1 'html))))
1093 ((string= type "file")
1094 ;; FILE link
1095 (let* ((filename path)
1096 (abs-p (file-name-absolute-p filename))
1097 thefile file-is-image-p search)
1098 (save-match-data
1099 (if (string-match "::\\(.*\\)" filename)
1100 (setq search (match-string 1 filename)
1101 filename (replace-match "" t nil filename)))
1102 (setq valid
1103 (if (functionp link-validate)
1104 (funcall link-validate filename current-dir)
1106 (setq file-is-image-p
1107 (org-file-image-p
1108 filename org-export-html-inline-image-extensions))
1109 (setq thefile (if abs-p (expand-file-name filename) filename))
1110 (when (and org-export-html-link-org-files-as-html
1111 (string-match "\\.org$" thefile))
1112 (setq thefile (concat (substring thefile 0
1113 (match-beginning 0))
1114 "." html-extension))
1115 (if (and search
1116 ;; make sure this is can be used as target search
1117 (not (string-match "^[0-9]*$" search))
1118 (not (string-match "^\\*" search))
1119 (not (string-match "^/.*/$" search)))
1120 (setq thefile (concat thefile "#"
1121 (org-solidify-link-text
1122 (org-link-unescape search)))))
1123 (when (string-match "^file:" desc)
1124 (setq desc (replace-match "" t t desc))
1125 (if (string-match "\\.org$" desc)
1126 (setq desc (replace-match "" t t desc))))))
1127 (setq rpl (if (and file-is-image-p
1128 (or (eq t org-export-html-inline-images)
1129 (and org-export-html-inline-images
1130 (not descp))))
1131 (progn
1132 (message "image %s %s" thefile org-par-open)
1133 (org-export-html-format-image thefile org-par-open))
1134 (concat "<a href=\"" thefile "\"" attr ">"
1135 (org-export-html-format-desc desc)
1136 "</a>")))
1137 (if (not valid) (setq rpl desc))))
1140 ;; just publish the path, as default
1141 (setq rpl (concat "<i>&lt;" type ":"
1142 (save-match-data (org-link-unescape path))
1143 "&gt;</i>"))))
1144 (setq line (replace-match rpl t t line)
1145 start (+ start (length rpl))))
1147 ;; TODO items
1148 (if (and (string-match org-todo-line-regexp line)
1149 (match-beginning 2))
1151 (setq line
1152 (concat (substring line 0 (match-beginning 2))
1153 "<span class=\""
1154 (if (member (match-string 2 line)
1155 org-done-keywords)
1156 "done" "todo")
1157 " " (match-string 2 line)
1158 "\"> " (org-export-html-get-todo-kwd-class-name
1159 (match-string 2 line))
1160 "</span>" (substring line (match-end 2)))))
1162 ;; Does this contain a reference to a footnote?
1163 (when org-export-with-footnotes
1164 (setq start 0)
1165 (while (string-match "\\([^* \t].*?\\)\\[\\([0-9]+\\)\\]" line start)
1166 (if (get-text-property (match-beginning 2) 'org-protected line)
1167 (setq start (match-end 2))
1168 (let ((n (match-string 2 line)) extra a)
1169 (if (setq a (assoc n footref-seen))
1170 (progn
1171 (setcdr a (1+ (cdr a)))
1172 (setq extra (format ".%d" (cdr a))))
1173 (setq extra "")
1174 (push (cons n 1) footref-seen))
1175 (setq line
1176 (replace-match
1177 (format
1178 "%s<sup><a class=\"footref\" name=\"fnr.%s%s\" href=\"#fn.%s\">%s</a></sup>"
1179 (match-string 1 line) n extra n n)
1180 t t line))))))
1182 (cond
1183 ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
1184 ;; This is a headline
1185 (setq level (org-tr-level (- (match-end 1) (match-beginning 1)
1186 level-offset))
1187 txt (match-string 2 line))
1188 (if (string-match quote-re0 txt)
1189 (setq txt (replace-match "" t t txt)))
1190 (if (<= level (max umax umax-toc))
1191 (setq head-count (+ head-count 1)))
1192 (when in-local-list
1193 ;; Close any local lists before inserting a new header line
1194 (while local-list-type
1195 (org-close-li (car local-list-type))
1196 (insert (format "</%sl>\n" (car local-list-type)))
1197 (pop local-list-type))
1198 (setq local-list-indent nil
1199 in-local-list nil))
1200 (setq first-heading-pos (or first-heading-pos (point)))
1201 (org-html-level-start level txt umax
1202 (and org-export-with-toc (<= level umax))
1203 head-count)
1205 ;; QUOTES
1206 (when (string-match quote-re line)
1207 (org-close-par-maybe)
1208 (insert "<pre>")
1209 (setq inquote t)))
1211 ((string-match "^[ \t]*- __+[ \t]*$" line)
1212 ;; Explicit list closure
1213 (when local-list-type
1214 (let ((ind (org-get-indentation line)))
1215 (while (and local-list-indent
1216 (<= ind (car local-list-indent)))
1217 (org-close-li (car local-list-type))
1218 (insert (format "</%sl>\n" (car local-list-type)))
1219 (pop local-list-type)
1220 (pop local-list-indent))
1221 (or local-list-indent (setq in-local-list nil))))
1222 (throw 'nextline nil))
1224 ((and org-export-with-tables
1225 (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
1226 (when (not table-open)
1227 ;; New table starts
1228 (setq table-open t table-buffer nil table-orig-buffer nil))
1230 ;; Accumulate lines
1231 (setq table-buffer (cons line table-buffer)
1232 table-orig-buffer (cons origline table-orig-buffer))
1233 (when (or (not lines)
1234 (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
1235 (car lines))))
1236 (setq table-open nil
1237 table-buffer (nreverse table-buffer)
1238 table-orig-buffer (nreverse table-orig-buffer))
1239 (org-close-par-maybe)
1240 (insert (org-format-table-html table-buffer table-orig-buffer))))
1242 ;; Normal lines
1243 (when (string-match
1244 (cond
1245 ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
1246 ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
1247 ((= llt ?\)) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
1248 (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
1249 line)
1250 (setq ind (or (get-text-property 0 'original-indentation line)
1251 (org-get-string-indentation line))
1252 item-type (if (match-beginning 4) "o" "u")
1253 starter (if (match-beginning 2)
1254 (substring (match-string 2 line) 0 -1))
1255 line (substring line (match-beginning 5))
1256 item-tag nil)
1257 (if (and starter (string-match "\\(.*?\\) ::[ \t]*" line))
1258 (setq item-type "d"
1259 item-tag (match-string 1 line)
1260 line (substring line (match-end 0))))
1261 (when (and (not (equal item-type "d"))
1262 (not (string-match "[^ \t]" line)))
1263 ;; empty line. Pretend indentation is large.
1264 (setq ind (if org-empty-line-terminates-plain-lists
1266 (1+ (or (car local-list-indent) 1)))))
1267 (setq didclose nil)
1268 (while (and in-local-list
1269 (or (and (= ind (car local-list-indent))
1270 (not starter))
1271 (< ind (car local-list-indent))))
1272 (setq didclose t)
1273 (org-close-li (car local-list-type))
1274 (insert (format "</%sl>\n" (car local-list-type)))
1275 (pop local-list-type) (pop local-list-indent)
1276 (setq in-local-list local-list-indent))
1277 (cond
1278 ((and starter
1279 (or (not in-local-list)
1280 (> ind (car local-list-indent))))
1281 ;; Start new (level of) list
1282 (org-close-par-maybe)
1283 (insert (cond
1284 ((equal item-type "u") "<ul>\n<li>\n")
1285 ((equal item-type "o") "<ol>\n<li>\n")
1286 ((equal item-type "d")
1287 (format "<dl>\n<dt>%s</dt><dd>\n" item-tag))))
1288 (push item-type local-list-type)
1289 (push ind local-list-indent)
1290 (setq in-local-list t))
1291 (starter
1292 ;; continue current list
1293 (org-close-li (car local-list-type))
1294 (insert (cond
1295 ((equal (car local-list-type) "d")
1296 (format "<dt>%s</dt><dd>\n" (or item-tag "???")))
1297 (t "<li>\n"))))
1298 (didclose
1299 ;; we did close a list, normal text follows: need <p>
1300 (org-open-par)))
1301 (if (string-match "^[ \t]*\\[\\([X ]\\)\\]" line)
1302 (setq line
1303 (replace-match
1304 (if (equal (match-string 1 line) "X")
1305 "<b>[X]</b>"
1306 "<b>[<span style=\"visibility:hidden;\">X</span>]</b>")
1307 t t line))))
1309 ;; Empty lines start a new paragraph. If hand-formatted lists
1310 ;; are not fully interpreted, lines starting with "-", "+", "*"
1311 ;; also start a new paragraph.
1312 (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (org-open-par))
1314 ;; Is this the start of a footnote?
1315 (when org-export-with-footnotes
1316 (when (and (boundp 'footnote-section-tag-regexp)
1317 (string-match (concat "^" footnote-section-tag-regexp)
1318 line))
1319 ;; ignore this line
1320 (throw 'nextline nil))
1321 (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
1322 (org-close-par-maybe)
1323 (let ((n (match-string 1 line)))
1324 (setq org-par-open t
1325 line (replace-match
1326 (format "<p class=\"footnote\"><sup><a class=\"footnum\" name=\"fn.%s\" href=\"#fnr.%s\">%s</a></sup>" n n n) t t line)))))
1328 ;; Check if the line break needs to be conserved
1329 (cond
1330 ((string-match "\\\\\\\\[ \t]*$" line)
1331 (setq line (replace-match "<br/>" t t line)))
1332 (org-export-preserve-breaks
1333 (setq line (concat line "<br/>"))))
1335 ;; Check if a paragraph should be started
1336 (let ((start 0))
1337 (while (and org-par-open
1338 (string-match "\\\\par\\>" line start))
1339 ;; Leave a space in the </p> so that the footnote matcher
1340 ;; does not see this.
1341 (if (not (get-text-property (match-beginning 0)
1342 'org-protected line))
1343 (setq line (replace-match "</p ><p >" t t line)))
1344 (setq start (match-end 0))))
1346 (insert line "\n")))))
1348 ;; Properly close all local lists and other lists
1349 (when inquote
1350 (insert "</pre>\n")
1351 (org-open-par))
1352 (when in-local-list
1353 ;; Close any local lists before inserting a new header line
1354 (while local-list-type
1355 (org-close-li (car local-list-type))
1356 (insert (format "</%sl>\n" (car local-list-type)))
1357 (pop local-list-type))
1358 (setq local-list-indent nil
1359 in-local-list nil))
1360 (org-html-level-start 1 nil umax
1361 (and org-export-with-toc (<= level umax))
1362 head-count)
1363 ;; the </div> to close the last text-... div.
1364 (when (and (> umax 0) first-heading-pos) (insert "</div>\n"))
1366 (save-excursion
1367 (goto-char (point-min))
1368 (while (re-search-forward "<p class=\"footnote\">[^\000]*?\\(</p>\\|\\'\\)" nil t)
1369 (push (match-string 0) footnotes)
1370 (replace-match "" t t)))
1371 (when footnotes
1372 (insert (format org-export-html-footnotes-section
1373 (or (nth 4 lang-words) "Footnotes")
1374 (mapconcat 'identity (nreverse footnotes) "\n"))
1375 "\n"))
1376 (let ((bib (org-export-html-get-bibliography)))
1377 (when bib
1378 (insert "\n" bib "\n")))
1379 (unless body-only
1380 (when (plist-get opt-plist :auto-postamble)
1381 (insert "<div id=\"postamble\">\n")
1382 (when (and org-export-author-info author)
1383 (insert "<p class=\"author\"> "
1384 (nth 1 lang-words) ": " author "\n")
1385 (when email
1386 (if (listp (split-string email ",+ *"))
1387 (mapc (lambda(e)
1388 (insert "<a href=\"mailto:" e "\">&lt;"
1389 e "&gt;</a>\n"))
1390 (split-string email ",+ *"))
1391 (insert "<a href=\"mailto:" email "\">&lt;"
1392 email "&gt;</a>\n")))
1393 (insert "</p>\n"))
1394 (when (and date org-export-time-stamp-file)
1395 (insert "<p class=\"date\"> "
1396 (nth 2 lang-words) ": "
1397 date "</p>\n"))
1398 (when org-export-creator-info
1399 (insert (format "<p class=\"creator\">HTML generated by org-mode %s in emacs %s</p>\n"
1400 org-version emacs-major-version)))
1401 (when org-export-html-validation-link
1402 (insert org-export-html-validation-link "\n"))
1403 (insert "</div>"))
1405 (if org-export-html-with-timestamp
1406 (insert org-export-html-html-helper-timestamp))
1407 (org-export-html-insert-plist-item opt-plist :postamble opt-plist)
1408 (insert "\n</div>\n</body>\n</html>\n"))
1410 (unless (plist-get opt-plist :buffer-will-be-killed)
1411 (normal-mode)
1412 (if (eq major-mode default-major-mode) (html-mode)))
1414 ;; insert the table of contents
1415 (goto-char (point-min))
1416 (when thetoc
1417 (if (or (re-search-forward
1418 "<p>\\s-*\\[TABLE-OF-CONTENTS\\]\\s-*</p>" nil t)
1419 (re-search-forward
1420 "\\[TABLE-OF-CONTENTS\\]" nil t))
1421 (progn
1422 (goto-char (match-beginning 0))
1423 (replace-match ""))
1424 (goto-char first-heading-pos)
1425 (when (looking-at "\\s-*</p>")
1426 (goto-char (match-end 0))
1427 (insert "\n")))
1428 (insert "<div id=\"table-of-contents\">\n")
1429 (mapc 'insert thetoc)
1430 (insert "</div>\n"))
1431 ;; remove empty paragraphs and lists
1432 (goto-char (point-min))
1433 (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t)
1434 (replace-match ""))
1435 (goto-char (point-min))
1436 (while (re-search-forward "<li>[ \r\n\t]*</li>\n?" nil t)
1437 (replace-match ""))
1438 (goto-char (point-min))
1439 (while (re-search-forward "</ul>\\s-*<ul>\n?" nil t)
1440 (replace-match ""))
1441 ;; Convert whitespace place holders
1442 (goto-char (point-min))
1443 (let (beg end n)
1444 (while (setq beg (next-single-property-change (point) 'org-whitespace))
1445 (setq n (get-text-property beg 'org-whitespace)
1446 end (next-single-property-change beg 'org-whitespace))
1447 (goto-char beg)
1448 (delete-region beg end)
1449 (insert (format "<span style=\"visibility:hidden;\">%s</span>"
1450 (make-string n ?x)))))
1451 (or to-buffer (save-buffer))
1452 (goto-char (point-min))
1453 (or (org-export-push-to-kill-ring "HTML")
1454 (message "Exporting... done"))
1455 (if (eq to-buffer 'string)
1456 (prog1 (buffer-substring (point-min) (point-max))
1457 (kill-buffer (current-buffer)))
1458 (current-buffer)))))
1460 (defun org-export-html-insert-plist-item (plist key &rest args)
1461 (let ((item (plist-get plist key)))
1462 (cond ((functionp item)
1463 (apply item args))
1464 (item
1465 (insert item)))))
1467 (defun org-export-html-format-href (s)
1468 "Make sure the S is valid as a href reference in an XHTML document."
1469 (save-match-data
1470 (let ((start 0))
1471 (while (string-match "&" s start)
1472 (setq start (+ (match-beginning 0) 3)
1473 s (replace-match "&amp;" t t s)))))
1476 (defun org-export-html-format-desc (s)
1477 "Make sure the S is valid as a description in a link."
1478 (if (and s (not (get-text-property 1 'org-protected s)))
1479 (save-match-data
1480 (org-html-do-expand s))
1483 (defun org-export-html-format-image (src par-open)
1484 "Create image tag with source and attributes."
1485 (save-match-data
1486 (if (string-match "^ltxpng/" src)
1487 (format "<img src=\"%s\"/>" src)
1488 (let* ((caption (org-find-text-property-in-string 'org-caption src))
1489 (attr (org-find-text-property-in-string 'org-attributes src))
1490 (label (org-find-text-property-in-string 'org-label src)))
1491 (format "%s<div %sclass=\"figure\">
1492 <p><img src=\"%s\"%s /></p>%s
1493 </div>%s"
1494 (if org-par-open "</p>\n" "")
1495 (if label (format "id=\"%s\" " label) "")
1497 (if (string-match "\\<alt=" (or attr ""))
1498 (concat " " attr )
1499 (concat " " attr " alt=\"" src "\""))
1500 (if caption (concat "\n<p>" caption "</p>") "")
1501 (if org-par-open "\n<p>" ""))))))
1503 (defun org-export-html-get-bibliography ()
1504 "Find bibliography, cut it out and return it."
1505 (catch 'exit
1506 (let (beg end (cnt 1) bib)
1507 (save-excursion
1508 (goto-char (point-min))
1509 (when (re-search-forward "^[ \t]*<div \\(id\\|class\\)=\"bibliography\"" nil t)
1510 (setq beg (match-beginning 0))
1511 (while (re-search-forward "</?div\\>" nil t)
1512 (setq cnt (+ cnt (if (string= (match-string 0) "<div") +1 -1)))
1513 (when (= cnt 0)
1514 (and (looking-at ">") (forward-char 1))
1515 (setq bib (buffer-substring beg (point)))
1516 (delete-region beg (point))
1517 (throw 'exit bib))))
1518 nil))))
1520 (defvar org-table-number-regexp) ; defined in org-table.el
1521 (defun org-format-table-html (lines olines)
1522 "Find out which HTML converter to use and return the HTML code."
1523 (if (stringp lines)
1524 (setq lines (org-split-string lines "\n")))
1525 (if (string-match "^[ \t]*|" (car lines))
1526 ;; A normal org table
1527 (org-format-org-table-html lines)
1528 ;; Table made by table.el - test for spanning
1529 (let* ((hlines (delq nil (mapcar
1530 (lambda (x)
1531 (if (string-match "^[ \t]*\\+-" x) x
1532 nil))
1533 lines)))
1534 (first (car hlines))
1535 (ll (and (string-match "\\S-+" first)
1536 (match-string 0 first)))
1537 (re (concat "^[ \t]*" (regexp-quote ll)))
1538 (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
1539 hlines))))
1540 (if (and (not spanning)
1541 (not org-export-prefer-native-exporter-for-tables))
1542 ;; We can use my own converter with HTML conversions
1543 (org-format-table-table-html lines)
1544 ;; Need to use the code generator in table.el, with the original text.
1545 (org-format-table-table-html-using-table-generate-source olines)))))
1547 (defvar org-table-number-fraction) ; defined in org-table.el
1548 (defun org-format-org-table-html (lines &optional splice)
1549 "Format a table into HTML."
1550 (require 'org-table)
1551 ;; Get rid of hlines at beginning and end
1552 (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
1553 (setq lines (nreverse lines))
1554 (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
1555 (setq lines (nreverse lines))
1556 (when org-export-table-remove-special-lines
1557 ;; Check if the table has a marking column. If yes remove the
1558 ;; column and the special lines
1559 (setq lines (org-table-clean-before-export lines)))
1561 (let* ((caption (or (get-text-property 0 'org-caption (car lines))
1562 (get-text-property (or (next-single-property-change
1563 0 'org-caption (car lines))
1565 'org-caption (car lines))))
1566 (attributes (or (get-text-property 0 'org-attributes (car lines))
1567 (get-text-property (or (next-single-property-change
1568 0 'org-attributes (car lines))
1570 'org-attributes (car lines))))
1571 (html-table-tag (org-export-splice-attributes
1572 html-table-tag attributes))
1573 (head (and org-export-highlight-first-table-line
1574 (delq nil (mapcar
1575 (lambda (x) (string-match "^[ \t]*|-" x))
1576 (cdr lines)))))
1578 (nline 0) fnum i
1579 tbopen line fields html gr colgropen rowstart rowend)
1580 (if splice (setq head nil))
1581 (unless splice (push (if head "<thead>" "<tbody>") html))
1582 (setq tbopen t)
1583 (while (setq line (pop lines))
1584 (catch 'next-line
1585 (if (string-match "^[ \t]*|-" line)
1586 (progn
1587 (unless splice
1588 (push (if head "</thead>" "</tbody>") html)
1589 (if lines (push "<tbody>" html) (setq tbopen nil)))
1590 (setq head nil) ;; head ends here, first time around
1591 ;; ignore this line
1592 (throw 'next-line t)))
1593 ;; Break the line into fields
1594 (setq fields (org-split-string line "[ \t]*|[ \t]*"))
1595 (unless fnum (setq fnum (make-vector (length fields) 0)))
1596 (setq nline (1+ nline) i -1
1597 rowstart (eval (car org-export-table-row-tags))
1598 rowend (eval (cdr org-export-table-row-tags)))
1599 (push (concat rowstart
1600 (mapconcat
1601 (lambda (x)
1602 (setq i (1+ i))
1603 (if (and (< i nline)
1604 (string-match org-table-number-regexp x))
1605 (incf (aref fnum i)))
1606 (cond
1607 (head
1608 (concat
1609 (format (car org-export-table-header-tags) "col")
1611 (cdr org-export-table-header-tags)))
1612 ((and (= i 0) org-export-html-table-use-header-tags-for-first-column)
1613 (concat
1614 (format (car org-export-table-header-tags) "row")
1616 (cdr org-export-table-header-tags)))
1618 (concat (car org-export-table-data-tags) x
1619 (cdr org-export-table-data-tags)))))
1620 fields "")
1621 rowend)
1622 html)))
1623 (unless splice (if tbopen (push "</tbody>" html)))
1624 (unless splice (push "</table>\n" html))
1625 (setq html (nreverse html))
1626 (unless splice
1627 ;; Put in col tags with the alignment (unfortunately often ignored...)
1628 (push (concat
1629 "<colgroup>"
1630 (mapconcat
1631 (lambda (x)
1632 (setq gr (pop org-table-colgroup-info))
1633 (format "<col align=\"%s\" />"
1634 (if (> (/ (float x) nline) org-table-number-fraction)
1635 "right" "left")))
1636 fnum "")
1637 "</colgroup>")
1638 html)
1640 ;; Since the output of HTML table formatter can also be used in
1641 ;; DocBook document, we want to always include the caption to make
1642 ;; DocBook XML file valid.
1643 (push (format "<caption>%s</caption>" (or caption "")) html)
1644 (push html-table-tag html))
1645 (concat (mapconcat 'identity html "\n") "\n")))
1647 (defun org-export-splice-attributes (tag attributes)
1648 "Read attributes in string ATTRIBUTES, add and replace in HTML tag TAG."
1649 (if (not attributes)
1651 (let (oldatt newatt)
1652 (setq oldatt (org-extract-attributes-from-string tag)
1653 tag (pop oldatt)
1654 newatt (cdr (org-extract-attributes-from-string attributes)))
1655 (while newatt
1656 (setq oldatt (plist-put oldatt (pop newatt) (pop newatt))))
1657 (if (string-match ">" tag)
1658 (setq tag
1659 (replace-match (concat (org-attributes-to-string oldatt) ">")
1660 t t tag)))
1661 tag)))
1663 (defun org-format-table-table-html (lines)
1664 "Format a table generated by table.el into HTML.
1665 This conversion does *not* use `table-generate-source' from table.el.
1666 This has the advantage that Org-mode's HTML conversions can be used.
1667 But it has the disadvantage, that no cell- or row-spanning is allowed."
1668 (let (line field-buffer
1669 (head org-export-highlight-first-table-line)
1670 fields html empty i)
1671 (setq html (concat html-table-tag "\n"))
1672 (while (setq line (pop lines))
1673 (setq empty "&nbsp;")
1674 (catch 'next-line
1675 (if (string-match "^[ \t]*\\+-" line)
1676 (progn
1677 (if field-buffer
1678 (progn
1679 (setq
1680 html
1681 (concat
1682 html
1683 "<tr>"
1684 (mapconcat
1685 (lambda (x)
1686 (if (equal x "") (setq x empty))
1687 (if head
1688 (concat
1689 (format (car org-export-table-header-tags) "col")
1691 (cdr org-export-table-header-tags))
1692 (concat (car org-export-table-data-tags) x
1693 (cdr org-export-table-data-tags))))
1694 field-buffer "\n")
1695 "</tr>\n"))
1696 (setq head nil)
1697 (setq field-buffer nil)))
1698 ;; Ignore this line
1699 (throw 'next-line t)))
1700 ;; Break the line into fields and store the fields
1701 (setq fields (org-split-string line "[ \t]*|[ \t]*"))
1702 (if field-buffer
1703 (setq field-buffer (mapcar
1704 (lambda (x)
1705 (concat x "<br/>" (pop fields)))
1706 field-buffer))
1707 (setq field-buffer fields))))
1708 (setq html (concat html "</table>\n"))
1709 html))
1711 (defun org-format-table-table-html-using-table-generate-source (lines)
1712 "Format a table into html, using `table-generate-source' from table.el.
1713 This has the advantage that cell- or row-spanning is allowed.
1714 But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
1715 (require 'table)
1716 (with-current-buffer (get-buffer-create " org-tmp1 ")
1717 (erase-buffer)
1718 (insert (mapconcat 'identity lines "\n"))
1719 (goto-char (point-min))
1720 (if (not (re-search-forward "|[^+]" nil t))
1721 (error "Error processing table"))
1722 (table-recognize-table)
1723 (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
1724 (table-generate-source 'html " org-tmp2 ")
1725 (set-buffer " org-tmp2 ")
1726 (buffer-substring (point-min) (point-max))))
1728 (defun org-export-splice-style (style extra)
1729 "Splice EXTRA into STYLE, just before \"</style>\"."
1730 (if (and (stringp extra)
1731 (string-match "\\S-" extra)
1732 (string-match "</style>" style))
1733 (concat (substring style 0 (match-beginning 0))
1734 "\n" extra "\n"
1735 (substring style (match-beginning 0)))
1736 style))
1738 (defun org-html-handle-time-stamps (s)
1739 "Format time stamps in string S, or remove them."
1740 (catch 'exit
1741 (let (r b)
1742 (while (string-match org-maybe-keyword-time-regexp s)
1743 (or b (setq b (substring s 0 (match-beginning 0))))
1744 (setq r (concat
1745 r (substring s 0 (match-beginning 0))
1746 " @<span class=\"timestamp-wrapper\">"
1747 (if (match-end 1)
1748 (format "@<span class=\"timestamp-kwd\">%s @</span>"
1749 (match-string 1 s)))
1750 (format " @<span class=\"timestamp\">%s@</span>"
1751 (substring
1752 (org-translate-time (match-string 3 s)) 1 -1))
1753 "@</span>")
1754 s (substring s (match-end 0))))
1755 ;; Line break if line started and ended with time stamp stuff
1756 (if (not r)
1758 (setq r (concat r s))
1759 (unless (string-match "\\S-" (concat b s))
1760 (setq r (concat r "@<br/>")))
1761 r))))
1763 (defvar htmlize-buffer-places) ; from htmlize.el
1764 (defun org-export-htmlize-region-for-paste (beg end)
1765 "Convert the region to HTML, using htmlize.el.
1766 This is much like `htmlize-region-for-paste', only that it uses
1767 the settings define in the org-... variables."
1768 (let* ((htmlize-output-type org-export-htmlize-output-type)
1769 (htmlize-css-name-prefix org-export-htmlize-css-font-prefix)
1770 (htmlbuf (htmlize-region beg end)))
1771 (unwind-protect
1772 (with-current-buffer htmlbuf
1773 (buffer-substring (plist-get htmlize-buffer-places 'content-start)
1774 (plist-get htmlize-buffer-places 'content-end)))
1775 (kill-buffer htmlbuf))))
1777 ;;;###autoload
1778 (defun org-export-htmlize-generate-css ()
1779 "Create the CSS for all font definitions in the current Emacs session.
1780 Use this to create face definitions in your CSS style file that can then
1781 be used by code snippets transformed by htmlize.
1782 This command just produces a buffer that contains class definitions for all
1783 faces used in the current Emacs session. You can copy and paste the ones you
1784 need into your CSS file.
1786 If you then set `org-export-htmlize-output-type' to `css', calls to
1787 the function `org-export-htmlize-region-for-paste' will produce code
1788 that uses these same face definitions."
1789 (interactive)
1790 (require 'htmlize)
1791 (and (get-buffer "*html*") (kill-buffer "*html*"))
1792 (with-temp-buffer
1793 (let ((fl (face-list))
1794 (htmlize-css-name-prefix "org-")
1795 (htmlize-output-type 'css)
1796 f i)
1797 (while (setq f (pop fl)
1798 i (and f (face-attribute f :inherit)))
1799 (when (and (symbolp f) (or (not i) (not (listp i))))
1800 (insert (org-add-props (copy-sequence "1") nil 'face f))))
1801 (htmlize-region (point-min) (point-max))))
1802 (switch-to-buffer "*html*")
1803 (goto-char (point-min))
1804 (if (re-search-forward "<style" nil t)
1805 (delete-region (point-min) (match-beginning 0)))
1806 (if (re-search-forward "</style>" nil t)
1807 (delete-region (1+ (match-end 0)) (point-max)))
1808 (beginning-of-line 1)
1809 (if (looking-at " +") (replace-match ""))
1810 (goto-char (point-min)))
1812 (defun org-html-protect (s)
1813 ;; convert & to &amp;, < to &lt; and > to &gt;
1814 (let ((start 0))
1815 (while (string-match "&" s start)
1816 (setq s (replace-match "&amp;" t t s)
1817 start (1+ (match-beginning 0))))
1818 (while (string-match "<" s)
1819 (setq s (replace-match "&lt;" t t s)))
1820 (while (string-match ">" s)
1821 (setq s (replace-match "&gt;" t t s)))
1822 ; (while (string-match "\"" s)
1823 ; (setq s (replace-match "&quot;" t t s)))
1827 (defun org-html-expand (string)
1828 "Prepare STRING for HTML export. Applies all active conversions.
1829 If there are links in the string, don't modify these."
1830 (let* ((re (concat org-bracket-link-regexp "\\|"
1831 (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$")))
1832 m s l res)
1833 (while (setq m (string-match re string))
1834 (setq s (substring string 0 m)
1835 l (match-string 0 string)
1836 string (substring string (match-end 0)))
1837 (push (org-html-do-expand s) res)
1838 (push l res))
1839 (push (org-html-do-expand string) res)
1840 (apply 'concat (nreverse res))))
1842 (defun org-html-do-expand (s)
1843 "Apply all active conversions to translate special ASCII to HTML."
1844 (setq s (org-html-protect s))
1845 (if org-export-html-expand
1846 (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
1847 (setq s (replace-match "<\\1>" t nil s))))
1848 (if org-export-with-emphasize
1849 (setq s (org-export-html-convert-emphasize s)))
1850 (if org-export-with-special-strings
1851 (setq s (org-export-html-convert-special-strings s)))
1852 (if org-export-with-sub-superscripts
1853 (setq s (org-export-html-convert-sub-super s)))
1854 (if org-export-with-TeX-macros
1855 (let ((start 0) wd ass)
1856 (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)\\({}\\)?"
1857 s start))
1858 (if (get-text-property (match-beginning 0) 'org-protected s)
1859 (setq start (match-end 0))
1860 (setq wd (match-string 1 s))
1861 (if (setq ass (assoc wd org-html-entities))
1862 (setq s (replace-match (or (cdr ass)
1863 (concat "&" (car ass) ";"))
1864 t t s))
1865 (setq start (+ start (length wd))))))))
1868 (defconst org-export-html-special-string-regexps
1869 '(("\\\\-" . "&shy;")
1870 ("---\\([^-]\\)" . "&mdash;\\1")
1871 ("--\\([^-]\\)" . "&ndash;\\1")
1872 ("\\.\\.\\." . "&hellip;"))
1873 "Regular expressions for special string conversion.")
1875 (defun org-export-html-convert-special-strings (string)
1876 "Convert special characters in STRING to HTML."
1877 (let ((all org-export-html-special-string-regexps)
1878 e a re rpl start)
1879 (while (setq a (pop all))
1880 (setq re (car a) rpl (cdr a) start 0)
1881 (while (string-match re string start)
1882 (if (get-text-property (match-beginning 0) 'org-protected string)
1883 (setq start (match-end 0))
1884 (setq string (replace-match rpl t nil string)))))
1885 string))
1887 (defun org-export-html-convert-sub-super (string)
1888 "Convert sub- and superscripts in STRING to HTML."
1889 (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
1890 (while (string-match org-match-substring-regexp string s)
1891 (cond
1892 ((and requireb (match-end 8)) (setq s (match-end 2)))
1893 ((get-text-property (match-beginning 2) 'org-protected string)
1894 (setq s (match-end 2)))
1896 (setq s (match-end 1)
1897 key (if (string= (match-string 2 string) "_") "sub" "sup")
1898 c (or (match-string 8 string)
1899 (match-string 6 string)
1900 (match-string 5 string))
1901 string (replace-match
1902 (concat (match-string 1 string)
1903 "<" key ">" c "</" key ">")
1904 t t string)))))
1905 (while (string-match "\\\\\\([_^]\\)" string)
1906 (setq string (replace-match (match-string 1 string) t t string)))
1907 string))
1909 (defun org-export-html-convert-emphasize (string)
1910 "Apply emphasis."
1911 (let ((s 0) rpl)
1912 (while (string-match org-emph-re string s)
1913 (if (not (equal
1914 (substring string (match-beginning 3) (1+ (match-beginning 3)))
1915 (substring string (match-beginning 4) (1+ (match-beginning 4)))))
1916 (setq s (match-beginning 0)
1918 (concat
1919 (match-string 1 string)
1920 (nth 2 (assoc (match-string 3 string) org-emphasis-alist))
1921 (match-string 4 string)
1922 (nth 3 (assoc (match-string 3 string)
1923 org-emphasis-alist))
1924 (match-string 5 string))
1925 string (replace-match rpl t t string)
1926 s (+ s (- (length rpl) 2)))
1927 (setq s (1+ s))))
1928 string))
1930 (defun org-open-par ()
1931 "Insert <p>, but first close previous paragraph if any."
1932 (org-close-par-maybe)
1933 (insert "\n<p>")
1934 (setq org-par-open t))
1935 (defun org-close-par-maybe ()
1936 "Close paragraph if there is one open."
1937 (when org-par-open
1938 (insert "</p>")
1939 (setq org-par-open nil)))
1940 (defun org-close-li (&optional type)
1941 "Close <li> if necessary."
1942 (org-close-par-maybe)
1943 (insert (if (equal type "d") "</dd>\n" "</li>\n")))
1945 (defvar in-local-list)
1946 (defvar local-list-indent)
1947 (defvar local-list-type)
1948 (defun org-export-html-close-lists-maybe (line)
1949 (let ((ind (or (get-text-property 0 'original-indentation line)))
1950 ; (and (string-match "\\S-" line)
1951 ; (org-get-indentation line))))
1952 didclose)
1953 (when ind
1954 (while (and in-local-list
1955 (<= ind (car local-list-indent)))
1956 (setq didclose t)
1957 (org-close-li (car local-list-type))
1958 (insert (format "</%sl>\n" (car local-list-type)))
1959 (pop local-list-type) (pop local-list-indent)
1960 (setq in-local-list local-list-indent))
1961 (and didclose (org-open-par)))))
1963 (defvar body-only) ; dynamically scoped into this.
1964 (defun org-html-level-start (level title umax with-toc head-count)
1965 "Insert a new level in HTML export.
1966 When TITLE is nil, just close all open levels."
1967 (org-close-par-maybe)
1968 (let* ((target (and title (org-get-text-property-any 0 'target title)))
1969 (extra-targets (assoc target org-export-target-aliases))
1970 (preferred (cdr (assoc target org-export-preferred-target-alist)))
1971 (remove (or preferred target))
1972 (l org-level-max)
1973 snumber href suffix)
1974 (setq extra-targets (remove remove extra-targets))
1975 (setq extra-targets
1976 (mapconcat (lambda (x)
1977 (if (org-uuidgen-p x) (setq x (concat "ID-" x)))
1978 (format "<a name=\"%s\" id=\"%s\"></a>"
1979 x x))
1980 extra-targets
1981 ""))
1982 (while (>= l level)
1983 (if (aref org-levels-open (1- l))
1984 (progn
1985 (org-html-level-close l umax)
1986 (aset org-levels-open (1- l) nil)))
1987 (setq l (1- l)))
1988 (when title
1989 ;; If title is nil, this means this function is called to close
1990 ;; all levels, so the rest is done only if title is given
1991 (when (string-match (org-re "\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
1992 (setq title (replace-match
1993 (if org-export-with-tags
1994 (save-match-data
1995 (concat
1996 "&nbsp;&nbsp;&nbsp;<span class=\"tag\">"
1997 (mapconcat
1998 (lambda (x)
1999 (format "<span class=\"%s\">%s</span>"
2000 (org-export-html-get-tag-class-name x)
2002 (org-split-string (match-string 1 title) ":")
2003 "&nbsp;")
2004 "</span>"))
2006 t t title)))
2007 (if (> level umax)
2008 (progn
2009 (if (aref org-levels-open (1- level))
2010 (progn
2011 (org-close-li)
2012 (if target
2013 (insert (format "<li id=\"%s\">" target) extra-targets title "<br/>\n")
2014 (insert "<li>" title "<br/>\n")))
2015 (aset org-levels-open (1- level) t)
2016 (org-close-par-maybe)
2017 (if target
2018 (insert (format "<ul>\n<li id=\"%s\">" target)
2019 extra-targets title "<br/>\n")
2020 (insert "<ul>\n<li>" title "<br/>\n"))))
2021 (aset org-levels-open (1- level) t)
2022 (setq snumber (org-section-number level))
2023 (setq level (+ level org-export-html-toplevel-hlevel -1))
2024 (if (and org-export-with-section-numbers (not body-only))
2025 (setq title (concat
2026 (format "<span class=\"section-number-%d\">%s</span>"
2027 level snumber)
2028 " " title)))
2029 (unless (= head-count 1) (insert "\n</div>\n"))
2030 (setq href (cdr (assoc (concat "sec-" snumber) org-export-preferred-target-alist)))
2031 (setq suffix (or href snumber))
2032 (setq href (or href (concat "sec-" snumber)))
2033 (insert (format "\n<div id=\"outline-container-%s\" class=\"outline-%d\">\n<h%d id=\"%s\">%s%s</h%d>\n<div class=\"outline-text-%d\" id=\"text-%s\">\n"
2034 suffix level level href
2035 extra-targets
2036 title level level suffix))
2037 (org-open-par)))))
2039 (defun org-export-html-get-tag-class-name (tag)
2040 "Turn tag into a valid class name.
2041 Replaces invalid characters with \"_\" and then prepends a prefix."
2042 (save-match-data
2043 (while (string-match "[^a-zA-Z0-9_]" tag)
2044 (setq tag (replace-match "_" t t tag))))
2045 (concat org-export-html-tag-class-prefix tag))
2047 (defun org-export-html-get-todo-kwd-class-name (kwd)
2048 "Turn todo keyword into a valid class name.
2049 Replaces invalid characters with \"_\" and then prepends a prefix."
2050 (save-match-data
2051 (while (string-match "[^a-zA-Z0-9_]" kwd)
2052 (setq kwd (replace-match "_" t t kwd))))
2053 (concat org-export-html-todo-kwd-class-prefix kwd))
2055 (defun org-html-level-close (level max-outline-level)
2056 "Terminate one level in HTML export."
2057 (if (<= level max-outline-level)
2058 (insert "</div>\n")
2059 (org-close-li)
2060 (insert "</ul>\n")))
2062 (provide 'org-html)
2064 ;; arch-tag: 8109d84d-eb8f-460b-b1a8-f45f3a6c7ea1
2066 ;;; org-html.el ends here