coleslaw/plugins/mathjax.lisp
Brit Butler e44d0bde9e Make add-injection support more complex injections. Thanks @PuercoPop.
In short, an injection is now a FUNCTION that takes a
document and returns either a STRING to insert in the
page (possibly with data from the document) or NIL.
2014-09-22 14:31:08 -04:00

30 lines
1.1 KiB
Common Lisp

(defpackage :coleslaw-mathjax
(:use :cl)
(:export #:enable)
(:import-from :coleslaw #:add-injection
#:content
#:index
#:tag-p
#:index-content))
(in-package :coleslaw-mathjax)
(defvar *mathjax-header* "~@[<script type=\"text/x-mathjax-config\">
MathJax.Hub.Config({~A});
</script>~]
<script type=\"text/javascript\" src=\"~A~@[?config=~A~]\"></script>")
(defgeneric mathjax-p (document)
(:documentation "Test if DOCUMENT requires contains any math-tagged content.")
(:method ((content content))
(tag-p "math" content))
(:method ((index index))
(and (slot-boundp index 'content)
(some #'mathjax-p (index-content index)))))
(defun enable (&key force config (preset "TeX-AMS-MML_HTMLorMML")
(location "http://cdn.mathjax.org/mathjax/latest/MathJax.js"))
(flet ((inject-p (x)
(when (or force (mathjax-p x))
(format nil *mathjax-header* config location preset))))
(add-injection #'inject-p :head)))