capybara/render.rkt

359 lines
15 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
(require racket/class racket/draw racket/fasl racket/list racket/match racket/port racket/path
racket/set racket/string racket/runtime-path net/url-string pict
2021-06-07 07:56:53 +00:00
markdown markdown/display-xexpr markdown/toc
2021-06-10 05:02:35 +00:00
"compiler.rkt" "defs.rkt" "fetch.rkt"
2021-06-07 09:09:20 +00:00
threading
2021-06-07 03:09:23 +00:00
(prefix-in sass: sass)
2021-06-12 02:55:13 +00:00
(prefix-in ext: "ext/main.rkt")
2021-06-24 04:16:14 +00:00
(prefix-in page: "templates/page.html.rkt")
(prefix-in hashtag: "templates/hashtag.html.rkt"))
2021-06-06 04:59:21 +00:00
2021-06-11 08:54:03 +00:00
(provide scan-for-rules)
2021-06-11 00:44:03 +00:00
(struct input-doc [metadata xref-name text] #:transparent)
(struct ir-doc [metadata xref-name html] #:prefab)
2021-06-07 02:38:03 +00:00
2021-06-10 05:02:35 +00:00
(define-runtime-path *render.scss* "render.scss")
2021-06-11 00:44:03 +00:00
(define *index.md* (build-path "index.md"))
(define *config.rktd* (build-path "config.rktd"))
2021-06-11 00:44:03 +00:00
2021-06-12 02:55:13 +00:00
(define *reserved-slugs*
(apply set (map build-path '("hashtag" "user" "tech" "capybara" "dynamic" "robots.txt"
2021-07-07 05:47:37 +00:00
"sitemap.xml" "feeds" "unsupported-browser"))))
(define *special-slugs*
(apply set (map build-path '("/40x" "/50x"))))
2021-06-11 05:12:39 +00:00
2021-06-11 00:44:03 +00:00
(define (read-input-doc xref-name [port (current-input-port)])
2021-06-07 02:38:03 +00:00
(define metadata (read port))
(unless (metadata? metadata)
(error "post front matter is not valid metadata!"))
(define text (port->string port))
2021-06-11 00:44:03 +00:00
(input-doc metadata xref-name text))
2021-06-07 09:09:20 +00:00
2021-06-07 09:31:39 +00:00
(define *xref-char* "^")
(define *user-char* "@")
(define *hashtag-char* "#")
;; transforms the xexpr into an xexpr that includes links to entities
(define (extract-text-starting-with xexpr start-char tag-name)
(define re (pregexp (string-append start-char "(\\p{L}|\\p{N})+")))
2021-06-07 09:09:20 +00:00
(define (process+ xexpr)
(match xexpr
2021-06-10 05:02:35 +00:00
;; do not descend into other special tags
[(list (or 'code 'math 'tech 'deftech 'masto 'user 'xref) attrs children ...) (list xexpr)]
2021-06-07 09:09:20 +00:00
;; recursive
2021-06-11 08:54:03 +00:00
[(list (? symbol? tag) attrs children ...)
2021-06-07 09:09:20 +00:00
(list (cons tag (cons attrs
(apply append
(map (lambda (child) (process+ child)) children)))))]
2021-06-07 09:31:39 +00:00
;; extract and transform the prefixed text
2021-06-07 09:09:20 +00:00
[(? string? str)
2021-06-07 09:31:39 +00:00
(define posns (regexp-match-positions* re str))
2021-06-07 09:09:20 +00:00
(define-values [items last-pos]
(for/fold ([items '()] [last-pos 0]) ([pos (in-list posns)])
2021-06-07 09:31:39 +00:00
(define value (substring str (car pos) (cdr pos)))
(values (cons `(,tag-name ([target ,(substring value 1)]) ,value)
2021-06-07 09:09:20 +00:00
(cons (substring str last-pos (car pos)) items))
(cdr pos))))
(reverse (cons (substring str last-pos) items))]))
2021-06-07 09:31:39 +00:00
(first (process+ xexpr)))
(define (extract-text-starting-with/many xexprs start-char tag-name)
(map (λ (xexpr) (extract-text-starting-with xexpr start-char tag-name)) xexprs))
2021-06-07 09:09:20 +00:00
2021-06-07 09:31:39 +00:00
;; extracts prefixed links and converts them into the <xref> pseudo-tag
(define (extract-prefixed-links xexpr start-char tag-name)
(define (pred? s)
(and (string? s) (> (string-length s) 1) (string=? start-char (substring s 0 1))))
(define (extract-prefixed-links+ xexpr)
(match xexpr
[(list 'a attrs body ...)
(match (assoc 'href attrs)
[(list _ (? pred? value))
(define target (substring value 1))
`(,tag-name ([target ,target]) ,@(map extract-prefixed-links+ body))]
[_ (apply list 'a attrs (map extract-prefixed-links+ body))])]
2021-06-11 08:54:03 +00:00
[(list (? symbol? tag) attrs children ...)
2021-06-07 09:31:39 +00:00
(apply list tag attrs (map extract-prefixed-links+ children))]
[(? string? str) str]))
(extract-prefixed-links+ xexpr))
2021-06-11 00:44:03 +00:00
2021-06-07 09:31:39 +00:00
(define (extract-prefixed-links/many xexprs start-char tag-name)
(map (λ (xexpr) (extract-prefixed-links xexpr start-char tag-name)) xexprs))
2021-06-07 02:38:03 +00:00
2021-06-11 05:12:39 +00:00
(define (check-xrefs xexprs xrefs-repo base-xref)
2021-06-11 00:44:03 +00:00
(define (check-xrefs-help xexpr)
(match xexpr
[(list 'xref (list (list 'target target)) body ...)
2021-06-11 05:12:39 +00:00
(define abs-target
(simplify-path
(url->path
(combine-url/relative
(path->url base-xref)
target))
#f))
(unless (set-member? xrefs-repo (explode-path abs-target))
2021-06-11 00:44:03 +00:00
(error "invalid xref!" target))
2021-06-11 05:12:39 +00:00
(apply list 'xref (list (list 'target (path->string abs-target)))
(map check-xrefs-help body))]
2021-06-11 08:54:03 +00:00
[(list (? symbol? tag) attrs body ...)
2021-06-11 05:12:39 +00:00
(cons tag (cons attrs (map check-xrefs-help body)))]
[(? string?) xexpr]))
(map check-xrefs-help xexprs))
2021-06-11 00:44:03 +00:00
(define (input-doc->ir-doc doc xrefs-repo)
(match-define (input-doc md xref-name text) doc)
2021-06-07 02:38:03 +00:00
(define output-raw (parse-markdown text))
2021-06-07 09:31:39 +00:00
(define output-cooked
2021-06-12 02:55:13 +00:00
(~> output-raw ext:transform-xexprs
2021-06-07 09:31:39 +00:00
(extract-text-starting-with/many *hashtag-char* 'hashtag)
(extract-text-starting-with/many *user-char* 'user)
(extract-prefixed-links/many *xref-char* 'xref)
2021-06-11 00:44:03 +00:00
(extract-prefixed-links/many *user-char* 'user)
2021-06-11 05:12:39 +00:00
(check-xrefs xrefs-repo xref-name)))
2021-06-11 00:44:03 +00:00
(ir-doc md xref-name output-cooked))
2021-06-07 02:38:03 +00:00
2021-06-07 09:09:20 +00:00
;; lowers pseudo-tags into their final form
(define (lower-specials xexpr base-url)
(define (lower-specials* xexpr)
(match xexpr
;; hashtags
2021-06-07 09:31:39 +00:00
[(list 'hashtag (list (list 'target target)) body ...)
2021-06-24 04:16:14 +00:00
`(a ([class "hashtag"] [href ,(format "~a/hashtag#~a" base-url target)])
2021-06-07 09:31:39 +00:00
,@(map lower-specials* body))]
2021-06-07 09:09:20 +00:00
;; xrefs
[(list 'xref (list (list 'target target)) body ...)
2021-06-11 05:12:39 +00:00
`(a ([class "xref"] [href ,(format "~a~a" base-url target)])
2021-06-07 09:09:20 +00:00
,@(map lower-specials* body))]
2021-06-07 09:31:39 +00:00
;; users
[(list 'user (list (list 'target target)) body ...)
2021-06-10 05:02:35 +00:00
`(a ([class "user"] [href ,(format "~a/user/~a" base-url target)])
2021-06-07 09:31:39 +00:00
,@(map lower-specials* body))]
2021-06-10 05:02:35 +00:00
;; deftech and tech
[(list 'deftech (list (list 'key key)) (? string? body))
`(em ([class "deftech"] [id ,(format "tech-~a" key)]) ,body)]
[(list 'tech (list (list 'key key)) (? string? body))
`(a ([class "tech"] [href ,(format "~a/tech/~a" base-url key)]) ,body)]
;; masto
2021-06-10 22:48:29 +00:00
;; TODO make this not janky
2021-06-10 05:02:35 +00:00
[(list 'p _ (list 'masto _ (? string? url)))
(masto-fetch-embed url)]
2021-06-07 09:09:20 +00:00
;; everything else
2021-06-11 08:54:03 +00:00
[(list (? symbol? tag) attrs children ...)
2021-06-07 09:09:20 +00:00
(apply list tag attrs (map lower-specials* children))]
[(? string? str) str]))
2021-06-11 08:54:03 +00:00
(map lower-specials* xexpr))
2021-06-07 09:09:20 +00:00
2021-06-24 04:16:14 +00:00
(define (capy-xexp->string document)
(with-output-to-string (λ () (display "<!doctype html>") (display-xexpr document))))
2021-06-11 10:09:29 +00:00
(define (ir-doc->page doc xrefs-repo site-config)
2021-06-11 00:44:03 +00:00
(match-define (ir-doc md xref-name content) doc)
(define base-url (assoc-ref+ site-config 'base))
2021-06-11 10:09:29 +00:00
(define exploded (explode-path xref-name))
(define exploded-length (length exploded))
(define (neighbor/immediate-child? p)
(or (= exploded-length (length p))
(and (= (add1 exploded-length) (length p))
(equal? (list-ref p (sub1 exploded-length))
(last exploded)))))
(define site-toc-defs
(for/list ([p (in-set xrefs-repo)] #:when (neighbor/immediate-child? p))
(define target (apply build-path p))
(define link (format "~a~a" base-url (path->string target)))
(define name (path->string (apply build-path (list-tail p (sub1 exploded-length)))))
(list
name
`(li () (a ([href ,link])
,(if (equal? p exploded) `(strong () ,name) name))))))
2021-06-11 10:14:34 +00:00
(define parent-name (simplify-path (build-path xref-name 'up) #f))
2021-06-11 10:09:29 +00:00
(define site-toc
2021-06-11 10:14:34 +00:00
`(ol ()
,@(if (equal? (build-path "/") xref-name)
'()
`((li () (a ([href ,(format "~a~a" base-url (path->string parent-name))]) "<up>"))))
,@(for/list ([def (in-list (sort site-toc-defs string<? #:key first))])
2021-06-11 10:09:29 +00:00
(second def))))
(let ([content (lower-specials content base-url)])
2021-06-07 09:09:20 +00:00
(define content-toc (toc content))
(define document
2021-06-08 07:14:44 +00:00
(page:execute
2021-06-07 09:09:20 +00:00
(hash 'metadata md
'site-config site-config
'base-url base-url
2021-06-11 10:09:29 +00:00
'site-toc site-toc
2021-06-07 09:09:20 +00:00
'content-toc content-toc
'content content)))
2021-06-24 04:16:14 +00:00
(capy-xexp->string document)))
(define (make-hashtag-page hashtags-map site-config)
(define base-url (assoc-ref+ site-config 'base))
(define content
(hashtag:execute
(hash 'site-config site-config
'base-url base-url
'hashtags-map hashtags-map)))
(define site-toc
`(ol ()
(li () (a ([href ,(format "~a~a" base-url "/")]) "/"))))
(define document
(page:execute
(hash 'metadata `((lang "en")
(title "All pages by tag")
(summary "List of all pages on this site by tags"))
'site-config site-config
'base-url base-url
'site-toc site-toc
'content-toc #f
'content content)))
(capy-xexp->string document))
2021-06-07 09:09:20 +00:00
(define (compile-index-scss index.scss)
2021-06-12 02:55:13 +00:00
(define scss-files (list *render.scss*))
2021-06-07 03:09:23 +00:00
(define top-level-style
(string-join
2021-06-12 02:55:13 +00:00
(apply list index.scss (ext:get-scss-include)
(map (λ (x) (format "@import \"~a\";" (path->string x))) scss-files))
"\n"))
2021-06-07 09:09:20 +00:00
(sass:compile/string top-level-style #t))
2021-06-06 04:59:21 +00:00
2021-06-10 22:48:29 +00:00
(define (scan-for-rules [output-dir (build-path "target")]
[build-dir (build-path "build")]
[src-dir (build-path "src")])
(define-rule (scss [out (build-path output-dir "index.css")]
[in (build-path src-dir "index.scss")])
(~> (compile-index-scss (port->string in)) (write-string out)))
(define img-rules
(parameterize ([current-directory src-dir])
(for/list ([rkt-file (in-directory #f)]
#:when (regexp-match? #px"\\.(png|jpg|jpeg|svg)\\.rkt$" rkt-file))
(define out-file (path-replace-extension rkt-file #""))
(define filetype
(match (path-get-extension out-file)
[#".png" 'png]
[(or #".jpg" #".jpeg") 'jpeg]
[#".svg" 'svg]))
(define-rule (render-1-image [out (build-path output-dir out-file)]
[in (build-path src-dir rkt-file)])
(define icon-pict (dynamic-require (build-path src-dir rkt-file) 'icon-pict))
(cond
[(eq? 'svg filetype)
(define dc (new svg-dc%
[width (pict-width icon-pict)]
[height (pict-height icon-pict)]
[output out]))
(send dc start-doc "meow")
(send dc start-page)
(draw-pict icon-pict dc 0 0)
(send dc end-page)
(send dc end-doc)]
[else
(define icon-bitmap (pict->bitmap icon-pict))
(send icon-bitmap save-file out filetype)]))
render-1-image)))
(define static-rules
(parameterize ([current-directory src-dir])
(for/list ([static-file (in-directory #f)]
#:when (regexp-match? #px"\\.(png|jpg|jpeg|svg)$" static-file))
(define-rule (copy-static [out (build-path output-dir static-file)]
[in (build-path src-dir static-file)])
(copy-port in out))
copy-static)))
2021-06-10 22:48:29 +00:00
2021-06-11 00:44:03 +00:00
(struct rule-spec [src intermediate output xref-name] #:transparent)
2021-06-10 22:48:29 +00:00
(define rule-specs
(parameterize ([current-directory src-dir])
(for/list ([md-file (in-directory #f)]
2021-06-11 00:44:03 +00:00
#:when (equal? (path-get-extension md-file) #".md"))
2021-06-10 22:48:29 +00:00
(define fasl-file (path-replace-extension md-file #".fasl"))
(define out-file (path-replace-extension md-file #".html"))
2021-06-11 00:44:03 +00:00
(define xref-name
(let-values ([(base name dir?) (split-path md-file)])
(if (equal? *index.md* name)
(if (eq? 'relative base)
(build-path "/")
(build-path "/" base))
(build-path "/" (path-replace-extension md-file #"")))))
2021-06-10 22:48:29 +00:00
(rule-spec (build-path src-dir md-file)
(build-path build-dir fasl-file)
2021-06-11 00:44:03 +00:00
(build-path output-dir out-file)
xref-name))))
(define config.rktd (build-path src-dir *config.rktd*))
2021-06-11 00:44:03 +00:00
2021-06-11 10:09:29 +00:00
;; TODO : this sucks
;; we should have a radix tree
2021-06-11 00:44:03 +00:00
(define xrefs-repo
2021-07-07 05:47:37 +00:00
(for/set ([spec (in-list rule-specs)]
#:unless (set-member? *special-slugs* (rule-spec-xref-name spec)))
2021-06-11 05:12:39 +00:00
(define exploded (explode-path (rule-spec-xref-name spec)))
(when (and (> (length exploded) 1)
(set-member? *reserved-slugs* (second exploded)))
(error "file is using a reserved slug" (rule-spec-src spec)))
exploded))
2021-06-10 22:48:29 +00:00
(define intermediate-rules
(for/list ([spec (in-list rule-specs)])
(define-rule (intermediate-rule [out (rule-spec-intermediate spec)]
[in (rule-spec-src spec)])
2021-06-11 00:44:03 +00:00
(~> (read-input-doc (rule-spec-xref-name spec) in)
(input-doc->ir-doc xrefs-repo)
s-exp->fasl (write-bytes out)))
2021-06-10 22:48:29 +00:00
intermediate-rule))
2021-06-24 04:16:14 +00:00
(define-rule* (hashtag-page [out (build-path output-dir "hashtag.html")]
[ins (cons config.rktd (map rule-spec-intermediate rule-specs))])
(define config (read (first ins)))
(unless (config? config)
(error "invalid config in config.rktd!"))
(define hashtag-map (make-hash))
(for ([ir-in (in-list (rest ins))] [xref-name (in-list (map rule-spec-xref-name rule-specs))])
(define md (~> (port->bytes ir-in) fasl->s-exp ir-doc-metadata))
(define title (assoc-ref+ md 'title "<untitled page>"))
(for ([tag (in-list (assoc-ref md 'tags '()))])
(hash-set! (hash-ref! hashtag-map tag make-hash) xref-name title)))
(~> (make-hashtag-page hashtag-map config) (write-string out)))
2021-06-10 22:48:29 +00:00
(define output-rules
(for/list ([spec (in-list rule-specs)])
(define-rule (output-rule [out (rule-spec-output spec)]
[in (rule-spec-intermediate spec)]
[config-in config.rktd])
(define config (read config-in))
(unless (config? config)
(error "invalid config in config.rktd!"))
2021-06-11 10:09:29 +00:00
(~> (port->bytes in) fasl->s-exp
(ir-doc->page xrefs-repo config)
(write-string out)))
2021-06-10 22:48:29 +00:00
output-rule))
(define all-rules (append intermediate-rules output-rules img-rules static-rules (list scss hashtag-page)))
(define existing-output-files
(parameterize ([current-directory output-dir])
(for/set ([file (in-directory #f)] #:when (file-exists? file))
(build-path output-dir file))))
(define accounted-output-files
(for/set ([rule (in-list all-rules)]) (rule-output rule)))
(define unknown-output-files (set-subtract existing-output-files accounted-output-files))
(for ([f (in-set unknown-output-files)])
(printf "WARN: unknown file in output dir: ~a\n" f))
all-rules)
2021-06-10 22:48:29 +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 "capybara-render"
2021-06-10 22:48:29 +00:00
#:args ()
(generate/execute (scan-for-rules))))