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,23 +427,52 @@ holding contextual information."
:tag "Org Export Reveal" :tag "Org Export Reveal"
:group 'org-export) :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) (defun org-reveal-stylesheets (info)
"Return the HTML contents for declaring reveal stylesheets "Return the HTML contents for declaring reveal stylesheets
using custom variable `org-reveal-root'." 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 (concat
;; stylesheets ;; stylesheets
(format " (if (and in-single-file
<link rel=\"stylesheet\" href=\"%scss/reveal.css\"/> (file-readable-p local-reveal-css)
<link rel=\"stylesheet\" href=\"%scss/theme/%s.css\" id=\"theme\"/> (file-readable-p local-theme-css))
" ;; CSS files exist and are readable. Embed them.
root-path root-path (concat "<style type=\"text/css\">\n"
(plist-get info :reveal-theme)) (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 ;; extra css
(let ((extra-css (plist-get info :reveal-extra-css))) (let ((extra-css (plist-get info :reveal-extra-css)))
(if extra-css (format "<link rel=\"stylesheet\" href=\"%s\"/>" extra-css) "")) (if extra-css (format "<link rel=\"stylesheet\" href=\"%s\"/>" extra-css) ""))
;; print-pdf ;; print-pdf
(format " (if in-single-file ""
(format "
<!-- If the query includes 'print-pdf', include the PDF print sheet --> <!-- If the query includes 'print-pdf', include the PDF print sheet -->
<script> <script>
if( window.location.search.match( /print-pdf/gi ) ) { if( window.location.search.match( /print-pdf/gi ) ) {
@ -455,7 +484,7 @@ using custom variable `org-reveal-root'."
} }
</script> </script>
" "
root-path)))) root-path)))))
(defun org-reveal-mathjax-scripts (info) (defun org-reveal-mathjax-scripts (info)
"Return the HTML contents for declaring MathJax scripts" "Return the HTML contents for declaring MathJax scripts"
@ -467,15 +496,37 @@ using custom variable `org-reveal-root'."
(defun org-reveal-scripts (info) (defun org-reveal-scripts (info)
"Return the necessary scripts for initializing reveal.js using "Return the necessary scripts for initializing reveal.js using
custom variable `org-reveal-root'." 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 (concat
;; reveal.js/lib/js/head.min.js ;; reveal.js/lib/js/head.min.js
;; reveal.js/js/reveal.js ;; reveal.js/js/reveal.js
(format " (if (and in-single-file
<script src=\"%slib/js/head.min.js\"></script> (file-readable-p local-head-min-js)
<script src=\"%sjs/reveal.js\"></script> (file-readable-p local-reveal-js))
" ;; Embed scripts into HTML
root-path root-path) (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 ;; plugin headings
" "
<script> <script>
@ -545,48 +596,50 @@ transitionSpeed: '%s',\n"
(plist-get info :reveal-multiplex-url))) (plist-get info :reveal-multiplex-url)))
;; optional JS library heading ;; optional JS library heading
" (if in-single-file ""
(concat
"
// Optional libraries used to extend on reveal.js // Optional libraries used to extend on reveal.js
dependencies: [ dependencies: [
" "
;; JS libraries ;; JS libraries
(let* ((builtins (let* ((builtins
'(classList (format " { src: '%slib/js/classList.js', condition: function() { return !document.body.classList; } }" root-path) '(classList
markdown (format " { src: '%splugin/markdown/marked.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } }, (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) { 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) highlight (format " { src: '%splugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } }" root-path)
zoom (format " { src: '%splugin/zoom-js/zoom.js', async: true, condition: function() { return !!document.body.classList; } }" root-path) zoom (format " { src: '%splugin/zoom-js/zoom.js', async: true, condition: function() { return !!document.body.classList; } }" root-path)
notes (format " { src: '%splugin/notes/notes.js', async: true, condition: function() { return !!document.body.classList; } }" root-path) notes (format " { src: '%splugin/notes/notes.js', async: true, condition: function() { return !!document.body.classList; } }" root-path)
search (format " { src: '%splugin/search/search.js', async: true, condition: function() { return !!document.body.classList; } }" root-path) search (format " { src: '%splugin/search/search.js', async: true, condition: function() { return !!document.body.classList; } }" root-path)
remotes (format " { src: '%splugin/remotes/remotes.js', async: true, condition: function() { return !!document.body.classList; } }" root-path) remotes (format " { src: '%splugin/remotes/remotes.js', async: true, condition: function() { return !!document.body.classList; } }" root-path)
multiplex (format " { src: '%s', async: true },\n%s" multiplex (format " { src: '%s', async: true },\n%s"
(plist-get info :reveal-multiplex-socketio-url) (plist-get info :reveal-multiplex-socketio-url)
; following ensures that either client.js or master.js is included depending on defva client-multiplex value state ; following ensures that either client.js or master.js is included depending on defva client-multiplex value state
(if (not client-multiplex) (if (not client-multiplex)
(progn (progn
(if (plist-get info :reveal-multiplex-secret) (if (plist-get info :reveal-multiplex-secret)
(setq client-multiplex t)) (setq client-multiplex t))
(format " { src: '%splugin/multiplex/master.js', async: true }" root-path)) (format " { src: '%splugin/multiplex/master.js', async: true }" root-path))
(format " { src: '%splugin/multiplex/client.js', async: true }" root-path)))))
(format " { src: '%splugin/multiplex/client.js', async: true }" root-path))))) (builtin-codes
(builtin-codes (mapcar
(mapcar (lambda (p)
(lambda (p) (eval (plist-get builtins p)))
(eval (plist-get builtins p))) (let ((buffer-plugins (plist-get info :reveal-plugins)))
(let ((buffer-plugins (plist-get info :reveal-plugins))) (cond
(cond ((string= buffer-plugins "") ())
((string= buffer-plugins "") ()) (buffer-plugins (car (read-from-string buffer-plugins)))
(buffer-plugins (car (read-from-string buffer-plugins))) (t org-reveal-plugins)))))
(t org-reveal-plugins))))) (extra-codes (plist-get info :reveal-extra-js))
(extra-codes (plist-get info :reveal-extra-js)) (total-codes
(total-codes (if (string= "" extra-codes) builtin-codes
(if (string= "" extra-codes) builtin-codes (append (list extra-codes) builtin-codes))))
(append (list extra-codes) builtin-codes)))) (mapconcat 'identity total-codes ",\n"))
(mapconcat 'identity total-codes ",\n")) "]\n"
" )
] "\n")
}); "});\n</script>\n")))
</script>\n")))
(defun org-reveal-toc (depth info) (defun org-reveal-toc (depth info)
"Build a slide of table of contents." "Build a slide of table of contents."