capybara/ext-mathml/main.rkt

36 lines
1.1 KiB
Racket

#lang racket/base
(require racket/list racket/match racket/runtime-path racket/string xml
"../util.rkt")
(define-runtime-path *js-path* "mathml-render.js")
(provide transform-xexprs scss-files)
(define (mathtex-type? s)
(and (string? s) (regexp-match #rx"^math/tex" s) #t))
(define (transform-xexprs xexprs)
(map transform-xexpr xexprs))
(define (run-mathjax source block?)
(define out-str (run-external (list "node" (path->string *js-path*)) source))
(match-define (list 'math attrs bodies ...) (string->xexpr out-str))
`(math ,(cons (list 'display (if block? "block" "inline"))
(filter (λ (x) (not (eq? (first x) 'display))) attrs))
,@bodies))
(define (transform-xexpr xexpr)
(match xexpr
[(list 'script (list (list 'type (? mathtex-type? type)))
bodies ...)
(define body (apply string-append bodies))
(define block? (> (length (string-split type " ")) 1))
(run-mathjax body block?)]
[(list tag attrs body ...)
(cons tag (cons attrs (map transform-xexpr body)))]
[(? string? str) str]))
(define scss-files '())