experimental support for MathJax

pull/9/head
Matthew Butterick 11 years ago
parent 4c7872ea0f
commit 5ac93d08ff

@ -20,9 +20,8 @@
[else x]))) [else x])))
(define/contract+provide (html->pollen html-string #:p-breaks [p-breaks #f]) (define/contract+provide (html->xexpr html-string)
((string?) (#:p-breaks boolean?) . ->* . string?) (string? . -> . xexpr?)
(use-html-spec #f) (use-html-spec #f)
(define xexpr-results (define xexpr-results
(let loop ([elem (read-html-as-xml (open-input-string html-string))]) (let loop ([elem (read-html-as-xml (open-input-string html-string))])
@ -33,9 +32,14 @@
[(struct element (start stop name attributes content)) `(,name ,(map loop attributes) ,@(map loop content))] [(struct element (start stop name attributes content)) `(,name ,(map loop attributes) ,@(map loop content))]
[(list elements ...) (map loop elements)] [(list elements ...) (map loop elements)]
[else (format "unknown item: ~a" elem)]))) [else (format "unknown item: ~a" elem)])))
; xexpr-results will be a list with whitespace elements, so strip those out ; xexpr-results will be a list with whitespace elements, so strip those out
(xexpr->pollen #:p-breaks p-breaks (car (filter-not (λ(x) (and (string? x) (regexp-match #px"\\s+" x))) xexpr-results)))) (car (filter-not (λ(x) (and (string? x) (regexp-match #px"\\s+" x))) xexpr-results)))
(define/contract+provide (html->pollen html-string #:p-breaks [p-breaks #f])
((string?) (#:p-breaks boolean?) . ->* . string?)
(xexpr->pollen #:p-breaks p-breaks (html->xexpr html-string)))
(define/contract+provide (url->pollen url-or-string #:p-breaks [p-breaks #f]) (define/contract+provide (url->pollen url-or-string #:p-breaks [p-breaks #f])
@ -50,4 +54,4 @@
; (xexpr->pollen '(p ((class "foo")) "You are" "\n\n" "puppy")) ; (xexpr->pollen '(p ((class "foo")) "You are" "\n\n" "puppy"))
; (xexpr->pollen '(p ((class "foo")) "You are " (em "so") " puppy")) ; (xexpr->pollen '(p ((class "foo")) "You are " (em "so") " puppy"))
; (display (html->pollen #:p-breaks #t (file->string "index.html")))) ; (display (html->pollen #:p-breaks #t (file->string "index.html"))))
) )

@ -0,0 +1,34 @@
#lang racket/base
(require pollen/convert)
(provide $ $$)
(define mathjax-config #<<HTML
<script type="text/x-mathjax-config">
MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$']]}});
</script>
HTML
)
(define mathjax-library #<<HTML
<script type="text/javascript"
src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
HTML
)
(define first-run #t)
(define (mathjax-wrapper #:delimiter delimiter xs)
`(mathjax
,@(if first-run ; only need mathjax-config & mathjax-library once on the page
(begin (set! first-run #f) (map html->xexpr (list mathjax-config mathjax-library)))
null)
,(apply string-append `(,delimiter ,@xs ,delimiter))))
(define ($ . x)
(mathjax-wrapper #:delimiter "$" x))
(define ($$ . x)
(mathjax-wrapper #:delimiter "$$" x))
Loading…
Cancel
Save