-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.rkt
47 lines (40 loc) · 1.71 KB
/
render.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#lang racket/base
(require pollen/cache
pollen/setup
racket/string
sugar/file
"private/constants.rkt"
"private/files.rkt")
(provide get-template-proc
render)
(define (err msg . vals)
(apply raise-user-error 'beeswax-render msg vals))
(define (beeswax-exn e orig-source)
(raise
(exn:fail:contract (string-replace (exn-message e) orig-source "beeswax-renderer")
(exn-continuation-marks e))))
(define (get-template-proc template-path)
(define template-proc
(with-handlers ([exn:fail:filesystem:missing-module?
(λ (e) (beeswax-exn e "standard-module-name-resolver"))]
[exn:fail:contract?
(λ (e) (beeswax-exn e "dynamic-require"))])
(dynamic-require (make-resolved-module-path template-path) template-proc-provide)))
(unless (procedure? template-proc)
(err "The binding ‘~a’ provided by ~a is not a procedure: ~v"
template-proc-provide
template-path
template-proc))
template-proc)
;; This function is specifically for rendering Pollen sources.
(define (render source-path output-path)
(define target-ext (get-ext output-path))
(parameterize ([current-poly-target (string->symbol target-ext)])
(define doc (cached-doc source-path))
(define metas (cached-metas source-path))
(define template-path
(or (template-from-metas source-path metas target-ext)
(find-default-template source-path target-ext)
(err "No template available for ~a targeting ~a" source-path target-ext)))
(define render-proc (get-template-proc template-path))
(render-proc doc metas (string->symbol (format "~a" output-path)))))