59 lines
1.9 KiB
Racket
59 lines
1.9 KiB
Racket
|
#lang racket/base
|
||
|
|
||
|
(require racket/list racket/match racket/runtime-path racket/string xml
|
||
|
"../util.rkt")
|
||
|
|
||
|
(define-runtime-path *ext-path* "target/debug/capybara-ext")
|
||
|
|
||
|
(provide transform-xexprs get-scss-include)
|
||
|
|
||
|
(define (mathtex-type? s)
|
||
|
(and (string? s) (regexp-match #rx"^math/tex" s) #t))
|
||
|
|
||
|
(define (brush-class? s)
|
||
|
(and (string? s) (regexp-match #rx"^brush:" s) #t))
|
||
|
|
||
|
(define (transform-xexprs xexprs)
|
||
|
(map transform-xexpr xexprs))
|
||
|
|
||
|
(define (run-highlight lang source)
|
||
|
(match
|
||
|
(run-external
|
||
|
(path->string *ext-path*)
|
||
|
`(Highlight (language . ,lang) (source . ,source)))
|
||
|
[`(Ok . ,result) (string->xexpr result)]
|
||
|
[`(Err . ,err) (error "syntax highlighting error:" err)]))
|
||
|
|
||
|
(define (run-mathml source block?)
|
||
|
(match
|
||
|
(run-external
|
||
|
(path->string *ext-path*)
|
||
|
`(Mathml (source . ,source) (blockp . ,block?)))
|
||
|
[`(Ok . ,result) (string->xexpr result)]
|
||
|
[`(Err . ,err) (error "mathml rendering error:" err)]))
|
||
|
|
||
|
(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-mathml body block?)]
|
||
|
[(list 'pre (list (list 'class (? brush-class? cls)))
|
||
|
(list 'code '() bodies ...))
|
||
|
(define lang (second (string-split cls " ")))
|
||
|
(define body (apply string-append bodies))
|
||
|
(run-highlight lang body)]
|
||
|
[(list 'pre '() (list 'code '() bodies ...))
|
||
|
(define body (apply string-append bodies))
|
||
|
(run-highlight "txt" body)]
|
||
|
[(list tag attrs body ...)
|
||
|
(cons tag (cons attrs (map transform-xexpr body)))]
|
||
|
[(? string? str) str]))
|
||
|
|
||
|
(define (get-scss-include)
|
||
|
(match
|
||
|
(run-external (path->string *ext-path*) '(HighlightCss))
|
||
|
[`(Ok . ,result) result]
|
||
|
[`(Err . ,err) (error "syntax highlighting error:" err)]))
|