capybara/ext-syntax/main.rkt

37 lines
1.1 KiB
Racket

#lang racket/base
(require racket/list racket/match racket/runtime-path racket/string xml
"../util.rkt")
(define-runtime-path *python-path* "syntax-render.py")
(define-runtime-path *css-path* "css")
(provide transform-xexprs scss-files)
(define (brush-class? s)
(and (string? s) (regexp-match #rx"^brush:" s) #t))
(define (transform-xexprs xexprs)
(map transform-xexpr xexprs))
(define (run-pygments lang source)
(define out-str
(run-external (list "python3" (path->string *python-path*) lang) source))
(string->xexpr out-str))
(define (transform-xexpr xexpr)
(match xexpr
[(list 'pre (list (list 'class (? brush-class? cls)))
(list 'code '() bodies ...))
(define lang (second (string-split cls " ")))
(define body (apply string-append bodies))
(run-pygments lang body)]
[(list 'pre '() (list 'code '() bodies ...))
(define body (apply string-append bodies))
(run-pygments "text" body)]
[(list tag attrs body ...)
(cons tag (cons attrs (map transform-xexpr body)))]
[(? string? str) str]))
(define scss-files (list (build-path *css-path* "syntax.scss")))