capybara/ext-mathml/main.rkt

36 lines
1.1 KiB
Racket
Raw Normal View History

2021-06-06 04:59:21 +00:00
#lang racket/base
2021-06-07 03:16:08 +00:00
(require racket/list racket/match racket/runtime-path racket/string xml
"../util.rkt")
2021-06-06 04:59:21 +00:00
2021-06-08 07:14:44 +00:00
(define-runtime-path *js-path* "mathml-render.js")
2021-06-06 04:59:21 +00:00
2021-06-07 03:09:23 +00:00
(provide transform-xexprs scss-files)
2021-06-06 04:59:21 +00:00
(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?)
2021-06-08 07:14:44 +00:00
(define out-str (run-external (list "node" (path->string *js-path*)) source))
2021-06-06 04:59:21 +00:00
(match-define (list 'math attrs bodies ...) (string->xexpr out-str))
`(math ,(cons (list 'display (if block? "block" "inline"))
2021-06-07 02:38:03 +00:00
(filter (λ (x) (not (eq? (first x) 'display))) attrs))
2021-06-06 04:59:21 +00:00
,@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]))
2021-06-07 03:09:23 +00:00
(define scss-files '())