Embed JS.

This commit is contained in:
Yujie Wen 2015-07-27 15:15:50 +08:00
parent 74b9a3c8cb
commit 44f3e701f8
1 changed files with 106 additions and 53 deletions

View File

@ -427,22 +427,51 @@ holding contextual information."
:tag "Org Export Reveal"
:group 'org-export)
(defun org-reveal--read-file (file)
"Return the content of file"
(with-temp-buffer
(insert-file-contents-literally file)
(buffer-string)))
(defun org-reveal-stylesheets (info)
"Return the HTML contents for declaring reveal stylesheets
using custom variable `org-reveal-root'."
(let ((root-path (file-name-as-directory (plist-get info :reveal-root))))
(let* ((root-path (file-name-as-directory (plist-get info :reveal-root)))
(reveal-css (concat root-path "css/reveal.css"))
(theme (plist-get info :reveal-theme))
(theme-css (concat root-path "css/theme/" theme ".css"))
;; Local file names.
(local-root (replace-regexp-in-string "^file:///" "" root-path))
(local-reveal-css (concat local-root "css/reveal.css"))
(local-theme-css (concat local-root "css/theme/" theme ".css"))
(in-single-file (plist-get info :reveal-single-file)))
(concat
;; stylesheets
(format "
<link rel=\"stylesheet\" href=\"%scss/reveal.css\"/>
<link rel=\"stylesheet\" href=\"%scss/theme/%s.css\" id=\"theme\"/>
"
root-path root-path
(plist-get info :reveal-theme))
(if (and in-single-file
(file-readable-p local-reveal-css)
(file-readable-p local-theme-css))
;; CSS files exist and are readable. Embed them.
(concat "<style type=\"text/css\">\n"
(org-reveal--read-file local-reveal-css)
"\n"
(org-reveal--read-file local-theme-css)
"</style>\n")
;; Fall-back to external CSS links.
(if in-single-file
;; Tried to embed CSS files but failed. Print a message about possible errors.
(error (concat "Cannot read "
(mapconcat 'identity
(delq nil (mapcar (lambda (file) (if (not (file-readable-p file)) file))
(list local-reveal-css local-theme-css)))
", "))))
;; Create links to CSS files.
(concat "<link rel=\"stylesheet\" href=\"" reveal-css "\"/>\n"
"<link rel=\"stylesheet\" href=\"" theme-css "\" id=\"theme\"/>\n"))
;; extra css
(let ((extra-css (plist-get info :reveal-extra-css)))
(if extra-css (format "<link rel=\"stylesheet\" href=\"%s\"/>" extra-css) ""))
;; print-pdf
(if in-single-file ""
(format "
<!-- If the query includes 'print-pdf', include the PDF print sheet -->
<script>
@ -455,7 +484,7 @@ using custom variable `org-reveal-root'."
}
</script>
"
root-path))))
root-path)))))
(defun org-reveal-mathjax-scripts (info)
"Return the HTML contents for declaring MathJax scripts"
@ -467,15 +496,37 @@ using custom variable `org-reveal-root'."
(defun org-reveal-scripts (info)
"Return the necessary scripts for initializing reveal.js using
custom variable `org-reveal-root'."
(let* ((root-path (file-name-as-directory (plist-get info :reveal-root))))
(let* ((root-path (file-name-as-directory (plist-get info :reveal-root)))
(head-min-js (concat root-path "lib/js/head.min.js"))
(reveal-js (concat root-path "js/reveal.js"))
;; Local files
(local-root-path (replace-regexp-in-string "^file:///" "" root-path))
(local-head-min-js (concat local-root-path "lib/js/head.min.js"))
(local-reveal-js (concat local-root-path "js/reveal.js"))
(in-single-file (plist-get info :reveal-single-file)))
(concat
;; reveal.js/lib/js/head.min.js
;; reveal.js/js/reveal.js
(format "
<script src=\"%slib/js/head.min.js\"></script>
<script src=\"%sjs/reveal.js\"></script>
"
root-path root-path)
(if (and in-single-file
(file-readable-p local-head-min-js)
(file-readable-p local-reveal-js))
;; Embed scripts into HTML
(concat "<script>\n"
(org-reveal--read-file local-head-min-js)
"\n"
(org-reveal--read-file local-reveal-js)
"\n</script>")
;; Fall-back to extern script links
(if in-single-file
;; Tried to embed scripts but failed. Print a message about possible errors.
(error (concat "Cannot read "
(mapconcat 'identity
(delq nil (mapcar (lambda (file) (if (not (file-readable-p file)) file))
(list local-head-min-js local-reveal-js)))
", "))))
(concat
"<script src=\"" head-min-js "\"></script>\n"
"<script src=\"" reveal-js "\"></script>\n"))
;; plugin headings
"
<script>
@ -545,13 +596,16 @@ transitionSpeed: '%s',\n"
(plist-get info :reveal-multiplex-url)))
;; optional JS library heading
(if in-single-file ""
(concat
"
// Optional libraries used to extend on reveal.js
dependencies: [
"
;; JS libraries
(let* ((builtins
'(classList (format " { src: '%slib/js/classList.js', condition: function() { return !document.body.classList; } }" root-path)
'(classList
(format " { src: '%slib/js/classList.js', condition: function() { return !document.body.classList; } }" root-path)
markdown (format " { src: '%splugin/markdown/marked.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: '%splugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } }" root-path root-path)
highlight (format " { src: '%splugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } }" root-path)
@ -567,7 +621,6 @@ dependencies: [
(if (plist-get info :reveal-multiplex-secret)
(setq client-multiplex t))
(format " { src: '%splugin/multiplex/master.js', async: true }" root-path))
(format " { src: '%splugin/multiplex/client.js', async: true }" root-path)))))
(builtin-codes
(mapcar
@ -583,10 +636,10 @@ dependencies: [
(if (string= "" extra-codes) builtin-codes
(append (list extra-codes) builtin-codes))))
(mapconcat 'identity total-codes ",\n"))
"
]
});
</script>\n")))
"]\n"
)
"\n")
"});\n</script>\n")))
(defun org-reveal-toc (depth info)
"Build a slide of table of contents."