org-e-html: Fix preamble and postamble

* contrib/lisp/org-e-html.el (org-e-html--build-preamble): Renamed
  from `org-e-html-preamble'.  Properly call user-provided preamble
  function when appropriate.  Refactor code.
(org-e-html--build-postamble): Renamed from `org-e-html-postamble.
  Properly call user-provided preamble function when appropriate.
  Refactor code.
(org-e-html-template): Small refactoring.
(org-e-html--translate): Use :html entries in `org-export-dictionary'
instead of :utf-8.
(org-e-html--build-meta-info): Renamed from `org-e-html-meta-info'.
Add docstring.
(org-e-html--build-style): Renamed from `org-e-html-style'. Add
docstring.
(org-e-html--build-mathjax-config): Renamed from
`org-e-html-mathjax-config'.  Add docstring.
This commit is contained in:
Nicolas Goaziou 2012-09-25 22:20:37 +02:00
parent 5b303c8df3
commit b40d8f7c73
1 changed files with 137 additions and 174 deletions

View File

@ -1247,7 +1247,9 @@ is a plist used as a communication channel."
;;; Template ;;; Template
(defun org-e-html-meta-info (info) (defun org-e-html--build-meta-info (info)
"Return meta tags for exported document.
INFO is a plist used as a communication channel."
(let* ((title (org-export-data (plist-get info :title) info)) (let* ((title (org-export-data (plist-get info :title) info))
(author (and (plist-get info :with-author) (author (and (plist-get info :with-author)
(let ((auth (plist-get info :author))) (let ((auth (plist-get info :author)))
@ -1255,33 +1257,35 @@ is a plist used as a communication channel."
(description (plist-get info :description)) (description (plist-get info :description))
(keywords (plist-get info :keywords))) (keywords (plist-get info :keywords)))
(concat (concat
(format "\n<title>%s</title>\n" title) (format "<title>%s</title>\n" title)
(format (format
"\n<meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>" "<meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>"
(or (and org-e-html-coding-system (or (and org-e-html-coding-system
(fboundp 'coding-system-get) (fboundp 'coding-system-get)
(coding-system-get org-e-html-coding-system (coding-system-get org-e-html-coding-system
'mime-charset)) 'mime-charset))
"iso-8859-1")) "iso-8859-1"))
(format "\n<meta name=\"title\" content=\"%s\"/>" title) (format "<meta name=\"title\" content=\"%s\"/>\n" title)
(format "\n<meta name=\"generator\" content=\"Org-mode\"/>") (format "<meta name=\"generator\" content=\"Org-mode\"/>\n")
(format "\n<meta name=\"generated\" content=\"%s\"/>" (format "<meta name=\"generated\" content=\"%s\"/>\n"
(org-e-html-format-date info)) (org-e-html-format-date info))
(format "\n<meta name=\"author\" content=\"%s\"/>" author) (format "<meta name=\"author\" content=\"%s\"/>\n" author)
(format "\n<meta name=\"description\" content=\"%s\"/>" description) (format "<meta name=\"description\" content=\"%s\"/>\n" description)
(format "\n<meta name=\"keywords\" content=\"%s\"/>" keywords)))) (format "<meta name=\"keywords\" content=\"%s\"/>\n" keywords))))
(defun org-e-html-style (info) (defun org-e-html--build-style (info)
(concat "Return style information for exported document.
"\n" (when (plist-get info :style-include-default) org-e-html-style-default) INFO is a plist used as a communication channel."
(plist-get info :style) (org-element-normalize-string
(plist-get info :style-extra) (concat
"\n" (when (plist-get info :style-include-default) org-e-html-style-default)
(when (plist-get info :style-include-scripts) (org-element-normalize-string (plist-get info :style))
org-e-html-scripts))) (org-element-normalize-string (plist-get info :style-extra))
(when (plist-get info :style-include-scripts) org-e-html-scripts))))
(defun org-e-html-mathjax-config (info) (defun org-e-html--build-mathjax-config (info)
"Insert the user setup into the matchjax template." "Insert the user setup into the mathjax template.
INFO is a plist used as a communication channel."
(when (member (plist-get info :LaTeX-fragments) '(mathjax t)) (when (member (plist-get info :LaTeX-fragments) '(mathjax t))
(let ((template org-e-html-mathjax-template) (let ((template org-e-html-mathjax-template)
(options org-e-html-mathjax-options) (options org-e-html-mathjax-options)
@ -1301,115 +1305,99 @@ is a plist used as a communication channel."
(if (string-match (concat "\\<mathml:") in-buffer) (if (string-match (concat "\\<mathml:") in-buffer)
(setq val (car (read-from-string (setq val (car (read-from-string
(substring in-buffer (match-end 0)))))) (substring in-buffer (match-end 0))))))
;; Exchange prefixes depending on mathml setting ;; Exchange prefixes depending on mathml setting.
(if (not val) (setq x yes yes no no x)) (if (not val) (setq x yes yes no no x))
;; Replace cookies to turn on or off the config/jax lines ;; Replace cookies to turn on or off the config/jax lines.
(if (string-match ":MMLYES:" template) (if (string-match ":MMLYES:" template)
(setq template (replace-match yes t t template))) (setq template (replace-match yes t t template)))
(if (string-match ":MMLNO:" template) (if (string-match ":MMLNO:" template)
(setq template (replace-match no t t template))) (setq template (replace-match no t t template)))
;; Return the modified template ;; Return the modified template.
template))) (org-element-normalize-string template))))
(defun org-e-html-preamble (info) (defun org-e-html--build-preamble (info)
(when (plist-get info :html-preamble) "Return document preamble as a string, or nil.
(let* ((title (org-export-data (plist-get info :title) info)) INFO is a plist used as a communication channel."
(date (org-e-html-format-date info)) (let ((preamble (plist-get info :html-preamble)))
(author (org-export-data (plist-get info :author) info)) (when preamble
(email (plist-get info :email)) (let ((preamble-contents
(html-pre-real-contents (if (functionp preamble) (funcall preamble info)
(cond (let ((title (org-export-data (plist-get info :title) info))
((functionp (plist-get info :html-preamble)) (date (org-e-html-format-date info))
(with-temp-buffer (author (if (not (plist-get info :with-author)) ""
(funcall (plist-get info :html-preamble)) (org-export-data (plist-get info :author) info)))
(buffer-string))) (email (if (not (plist-get info :with-email)) ""
((stringp (plist-get info :html-preamble)) (plist-get info :email))))
(format-spec (plist-get info :html-preamble) (if (stringp preamble)
`((?t . ,title) (?a . ,author) (format-spec preamble
(?d . ,date) (?e . ,email)))) `((?t . ,title) (?a . ,author)
(t (?d . ,date) (?e . ,email)))
(format-spec (format-spec
(or (cadr (assoc (plist-get info :language) (or (cadr (assoc (plist-get info :language)
org-e-html-preamble-format)) org-e-html-preamble-format))
(cadr (assoc "en" org-e-html-preamble-format))) (cadr (assoc "en" org-e-html-preamble-format)))
`((?t . ,title) (?a . ,author) `((?t . ,title) (?a . ,author)
(?d . ,date) (?e . ,email))))))) (?d . ,date) (?e . ,email))))))))
(when (not (equal html-pre-real-contents "")) (when (org-string-nw-p preamble-contents)
(concat (concat (format "<div id=\"%s\">\n" (nth 0 org-e-html-divs))
(format " (org-element-normalize-string preamble-contents)
<div id=\"%s\"> " (nth 0 org-e-html-divs)) "</div>\n"))))))
"
"
html-pre-real-contents
"
</div>")))))
(defun org-e-html-postamble (info) (defun org-e-html--build-postamble (info)
(concat "Return document postamble as a string, or nil.
(when (and (not body-only) INFO is a plist used as a communication channel."
(plist-get info :html-postamble)) (let ((postamble (plist-get info :html-postamble)))
(let* ((html-post (plist-get info :html-postamble)) (when postamble
(date (org-e-html-format-date info)) (let ((postamble-contents
(author (let ((author (plist-get info :author))) (if (functionp postamble) (funcall postamble info)
(and author (org-export-data author info)))) (let ((date (org-e-html-format-date info))
(email (author (let ((author (plist-get info :author)))
(mapconcat (lambda(e) (and author (org-export-data author info))))
(format "<a href=\"mailto:%s\">%s</a>" e e)) (email (mapconcat
(split-string (plist-get info :email) ",+ *") (lambda (e)
", ")) (format "<a href=\"mailto:%s\">%s</a>" e e))
(html-validation-link (or org-e-html-validation-link "")) (split-string (plist-get info :email) ",+ *")
(creator-info org-export-creator-string)) ", "))
(concat (html-validation-link (or org-e-html-validation-link ""))
;; begin postamble (creator-info (plist-get info :creator)))
" (cond ((stringp postamble)
<div id=\"" (nth 2 org-e-html-divs) "\">" (format-spec postamble
(cond `((?a . ,author) (?e . ,email)
;; auto postamble (?d . ,date) (?c . ,creator-info)
((eq (plist-get info :html-postamble) 'auto) (?v . ,html-validation-link))))
((eq postamble 'auto)
(concat
(when (plist-get info :time-stamp-file)
(format "<p class=\"date\">%s: %s</p>\n"
(org-e-html--translate "Date" info)
date))
(when (and (plist-get info :with-author) author)
(format "<p class=\"author\">%s : %s</p>\n"
(org-e-html--translate "Author" info)
author))
(when (and (plist-get info :with-email) email)
(format "<p class=\"email\">%s </p>\n" email))
(when (plist-get info :with-creator)
(format "<p class=\"creator\">%s</p>\n"
creator-info))
html-validation-link "\n"))
(t (format-spec
(or (cadr (assoc (plist-get info :language)
org-e-html-postamble-format))
(cadr (assoc "en" org-e-html-postamble-format)))
`((?a . ,author) (?e . ,email)
(?d . ,date) (?c . ,creator-info)
(?v . ,html-validation-link)))))))))
(when (org-string-nw-p postamble-contents)
(concat (concat
(when (plist-get info :time-stamp-file) (format "<div id=\"%s\">\n" (nth 2 org-e-html-divs))
(format " (org-element-normalize-string postamble-contents)
<p class=\"date\"> %s: %s </p> " (org-e-html--translate "Date" info) date)) "</div>\n"))))))
(when (and (plist-get info :with-author) author)
(format "
<p class=\"author\"> %s : %s</p>" (org-e-html--translate "Author" info) author))
(when (and (plist-get info :with-email) email)
(format "
<p class=\"email\"> %s </p>" email))
(when (plist-get info :with-creator)
(format "
<p class=\"creator\"> %s </p>" creator-info))
html-validation-link "\n"))
;; postamble from a string
((stringp (plist-get info :html-postamble))
(format-spec (plist-get info :html-postamble)
`((?a . ,author) (?e . ,email)
(?d . ,date) (?c . ,creator-info)
(?v . ,html-validation-link))))
;; postamble from a function
((functionp (plist-get info :html-postamble))
(with-temp-buffer
(funcall (plist-get info :html-postamble))
(buffer-string)))
;; default postamble
(t
(format-spec
(or (cadr (assoc (plist-get info :language)
org-e-html-postamble-format))
(cadr (assoc "en" org-e-html-postamble-format)))
`((?a . ,author) (?e . ,email)
(?d . ,date) (?c . ,creator-info)
(?v . ,html-validation-link)))))
"
</div>")))
;; org-e-html-html-helper-timestamp
))
(defun org-e-html-template (contents info) (defun org-e-html-template (contents info)
"Return complete document string after HTML conversion. "Return complete document string after HTML conversion.
CONTENTS is the transcoded contents string. RAW-DATA is the CONTENTS is the transcoded contents string. INFO is a plist
original parsed data. INFO is a plist holding export options." holding export options."
(concat (concat
(format (format
(or (and (stringp org-e-html-xml-declaration) (or (and (stringp org-e-html-xml-declaration)
@ -1420,78 +1408,53 @@ original parsed data. INFO is a plist holding export options."
"") "")
(or (and org-e-html-coding-system (or (and org-e-html-coding-system
(fboundp 'coding-system-get) (fboundp 'coding-system-get)
(coding-system-get org-e-html-coding-system (coding-system-get org-e-html-coding-system 'mime-charset))
'mime-charset))
"iso-8859-1")) "iso-8859-1"))
" "\n"
<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"
(format " (format "<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"%s\" xml:lang=\"%s\">\n"
<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"%s\" xml:lang=\"%s\"> "
(plist-get info :language) (plist-get info :language)) (plist-get info :language) (plist-get info :language))
" "<head>\n"
<head>" (org-e-html--build-meta-info info)
(org-e-html-meta-info info) ; meta (org-e-html--build-style info)
(org-e-html-style info) ; style (org-e-html--build-mathjax-config info)
(org-e-html-mathjax-config info) ; mathjax "</head>\n"
" "<body>\n"
</head>"
"
<body>"
(let ((link-up (org-trim (plist-get info :link-up))) (let ((link-up (org-trim (plist-get info :link-up)))
(link-home (org-trim (plist-get info :link-home)))) (link-home (org-trim (plist-get info :link-home))))
(unless (and (string= link-up "") (string= link-up "")) (unless (and (string= link-up "") (string= link-up ""))
(format org-e-html-home/up-format (format org-e-html-home/up-format
(or link-up link-home) (or link-up link-home)
(or link-home link-up)))) (or link-home link-up))))
;; preamble ;; Preamble.
(org-e-html-preamble info) (org-e-html--build-preamble info)
;; begin content ;; Begin content.
(format " (format "<div id=\"%s\">\n" (nth 1 org-e-html-divs))
<div id=\"%s\">" (or org-e-html-content-div ;; Document title.
(nth 1 org-e-html-divs))) (format "<h1 class=\"title\">%s</h1>\n"
;; document title (org-export-data (plist-get info :title) info))
(format " ;; Table of contents.
<h1 class=\"title\">%s</h1>\n" (org-export-data (plist-get info :title) info))
;; table of contents
(let ((depth (plist-get info :with-toc))) (let ((depth (plist-get info :with-toc)))
(when depth (org-e-html-toc depth info))) (when depth (org-e-html-toc depth info)))
;; document contents ;; Document contents.
contents contents
;; footnotes section ;; Footnotes section.
(org-e-html-footnote-section info) (org-e-html-footnote-section info)
;; bibliography ;; Bibliography.
(org-e-html-bibliography) (org-e-html-bibliography)
;; end content ;; End content.
(unless body-only "\n</div>"
" ;; Postamble.
</div>") (org-e-html--build-postamble info)
;; Closing document.
;; postamble "</body>\n</html>"))
(org-e-html-postamble info)
(unless body-only
"
</body>")
"
</html>"))
(defun org-e-html--translate (s info) (defun org-e-html--translate (s info)
"Transcode string S in to HTML. "Translate string S according to specified language.
INFO is a plist used as a communication channel. INFO is a plist used as a communication channel."
(org-export-translate s :html info))
Lookup utf-8 equivalent of S in `org-export-dictionary' and
replace all non-ascii characters with its numeric reference."
(let ((s (org-export-translate s :utf-8 info)))
;; Protect HTML metacharacters.
(setq s (org-e-html-encode-plain-text s))
;; Replace non-ascii characters with their numeric equivalents.
(replace-regexp-in-string
"[[:nonascii:]]"
(lambda (m) (format "&#%d;" (encode-char (string-to-char m) 'ucs)))
s t t)))
;;;; Anchor ;;;; Anchor