capybara/render.rkt

65 lines
2.3 KiB
Racket
Raw Normal View History

2021-06-07 07:56:53 +00:00
#lang racket/base
2021-06-06 04:59:21 +00:00
2021-06-07 07:56:53 +00:00
(require racket/contract racket/list racket/match racket/port racket/string
markdown markdown/display-xexpr markdown/toc
"compiler.rkt"
2021-06-07 03:09:23 +00:00
(prefix-in sass: sass)
2021-06-06 04:59:21 +00:00
(prefix-in mathml: "ext-mathml/main.rkt")
2021-06-07 03:09:23 +00:00
(prefix-in syntax: "ext-syntax/main.rkt")
(prefix-in article: "templates/article.html.rkt"))
2021-06-06 04:59:21 +00:00
2021-06-07 02:38:03 +00:00
(define metadata?
(listof (or/c
(list/c 'date integer? integer? integer?)
(list/c 'title string?)
2021-06-07 07:56:53 +00:00
(list/c 'slug string?)
2021-06-07 02:38:03 +00:00
(list/c 'summary string?)
(cons/c 'authors (listof string?)))))
(define (metadata-ref md key [default (λ () (error "no such key"))])
(match (assoc key md)
[#f (if (procedure? default) (default) default)]
[(cons _ rst) rst]))
(define (metadata-ref+ md key [default (λ () (error "no such key"))])
(first (metadata-ref md key default)))
(struct input-document [metadata text] #:transparent)
(define (read-doc [port (current-input-port)])
(define metadata (read port))
(unless (metadata? metadata)
(error "post front matter is not valid metadata!"))
(define text (port->string port))
(input-document metadata text))
(define (markdown->html input)
(match-define (input-document md text) input)
(define output-raw (parse-markdown text))
(define output-cooked (mathml:transform-xexprs (syntax:transform-xexprs output-raw)))
(define output-toc (toc output-cooked))
2021-06-07 03:09:23 +00:00
(define scss-files (append mathml:scss-files syntax:scss-files))
(define top-level-style
(string-join (map (λ (x) (format "@import \"~a\";" (path->string x))) scss-files) "\n"))
(define styles (sass:compile/string top-level-style #t))
2021-06-07 02:38:03 +00:00
(define document
2021-06-07 03:09:23 +00:00
(article:execute (hash 'metadata-ref metadata-ref
'metadata-ref+ metadata-ref+
'metadata md
'page-styles styles
'content-toc output-toc
'content output-cooked)))
2021-06-07 02:38:03 +00:00
(with-output-to-string (λ () (display "<!doctype html>") (display-xexpr document))))
2021-06-06 04:59:21 +00:00
2021-06-07 02:38:03 +00:00
(module+ main
2021-06-07 07:56:53 +00:00
(require racket/cmdline)
(command-line
#:program "meow"
#:args (infile outfile)
(define-rule (render [out outfile] [in infile])
(write-string (markdown->html (read-doc in)) out))
(generate/execute (list render))))