-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprototype.rkt
50 lines (37 loc) · 1.38 KB
/
prototype.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
48
49
#lang racket
(provide eval-hlml ->html)
(define-syntax-rule (.. f g) (λ (x) (g (f x))))
(define (make-unquote ast)
(match ast
[(cons _ _) (map make-unquote ast)]
[s #:when (eq? 'script s) 'unquote]
[o o]))
(define (pp ast)
(cons 'quasiquote (list (make-unquote ast))))
(define-namespace-anchor anc)
(define ns (namespace-anchor->namespace anc))
(define (eval-hlml p)
(eval (pp (read p)) ns))
(define (->html markup)
(match markup
[(cons s (cons (cons 'attr attrs) content))
#:when (symbol? s)
(make-node s attrs content)]
[(cons s content) #:when (symbol? s)
(make-node s '() content)]
[s #:when (string? s) (string-append s " ")]
[s #:when (symbol? s) (string-append (symbol->string s) " ")]
[n #:when (number? n) (string-append (number->string n) " ")]))
(define (make-node t attrs content)
(string-append (make-tag t attrs)
(apply string-append (map ->html content))
(make-tag t attrs #t)))
(define (make-tag s attrs [close? #f])
(string-append "<" (if close? "/" "") (symbol->string s) (make-attrs attrs) ">"))
;; Attrs is list of (name value) pairs
(define (make-attrs attrs)
(define (make-attr attr)
(match attr
[(list name val) (string-append " " (symbol->string name) "=" "\"" val "\"")]
[_ (error "needs to be list")]))
(apply string-append (map make-attr attrs)))