capybara/render.rkt

83 lines
2.7 KiB
Racket
Raw Normal View History

2021-06-06 04:59:21 +00:00
#lang racket
2021-06-06 06:28:40 +00:00
(require markdown markdown/display-xexpr markdown/toc
2021-06-06 04:59:21 +00:00
(prefix-in mathml: "ext-mathml/main.rkt")
(prefix-in syntax: "ext-syntax/main.rkt"))
2021-06-07 02:38:03 +00:00
(define metadata?
(listof (or/c
(list/c 'date integer? integer? integer?)
(list/c 'title string?)
(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 *banner*
'(!HTML-COMMENT () " this page made with: trans rights 🏳️‍⚧️ "))
(define *mathjax-placeholder*
'(div ([style "display: none"]) "__X_MATHJAX_POLYFILL_PLACEHOLDER__"))
(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))
(define styles (string-append (mathml:get-styles) (syntax:get-styles)))
(define document
`(html
([lang "en"])
(head
2021-06-06 06:28:40 +00:00
()
2021-06-07 02:38:03 +00:00
,*banner*
(meta ([charset "utf-8"]))
(title () ,(metadata-ref+ md 'title "<untitled article>"))
(meta ([name "viewport"] [content "width=device-width, initial-scale=1"]))
,@(match (metadata-ref+ md 'summary)
[#f '()]
[summary
`((meta ([name "description"] [content ,summary])))])
,@(match (metadata-ref md 'authors)
[(or #f '()) '()]
[authors
`((meta ([name "author"] [content ,(string-join authors ", ")])))])
,@(match (metadata-ref md 'date)
[#f '()]
[(list y m d)
`((meta ([name "DC.Date.created"] [content ,(format "~a-~a~a" y m d)])))])
(meta ([name "generator"] [content "meow meow meow meow"]))
;<link rel="shortcut icon" type="image/png" href="haskal.png"/>
(style ([type "text/css"]) ,styles))
(body
2021-06-06 04:59:21 +00:00
()
2021-06-07 02:38:03 +00:00
(nav
()
,output-toc)
(main
()
,@output-cooked)
,*mathjax-placeholder*)))
(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
(define doc (read-doc))
(displayln (markdown->html doc)))