-
Notifications
You must be signed in to change notification settings - Fork 0
/
codec.ss
193 lines (177 loc) · 8.33 KB
/
codec.ss
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#lang scheme/base
(require "base.ss")
(require net/uri-codec
(only-in srfi/13 string-join)
"core.ss")
; Accessors --------------------------------------
; site string -> any
(define (site-dispatch site request)
(let ([url-string (clean-request-url request)])
(match-let ([(list-rest controller args) (site-decode site url-string)])
(if controller
(if (controller-requestless? controller)
(apply controller args)
(apply controller request args))
((site-not-found-proc site) request)))))
; controller any ... -> boolean
(define (controller-access? controller . args)
(with-handlers ([exn? (lambda (exn)
(raise (make-exn:fail (format "error determining access for ~s:~n~a"
(cons (controller-id controller) args)
(exn-message exn))
(exn-continuation-marks exn))))])
(let ([ans (apply (controller-access-proc controller) args)])
(unless (boolean? ans)
(printf "Warning: access predicate for ~a returned non-boolean value: ~s"
(controller-id controller)
ans))
(and ans #t))))
; controller any ... -> string
(define (controller-url controller . args)
(or (for/or ([rule (in-list (site-rules (controller-site controller)))])
(and (eq? (rule-controller rule) controller)
(pattern-encode (rule-pattern rule) args)))
(error "no url for controller" (cons controller args))))
; controller
; [#:body (U xml sexp #f)]
; [#:id (U string symbol #f)]
; [#:class (U string symbol #f)]
; [#:classes (listof (U string symbol))]
; [#:target (U string #f)]
; [#:title (U string #f)]
; [#:format link-format]
; [#:else (U link-substitute html)]
; ->
; (U xml sexp (listof sexp))
(define (controller-link
controller
#:body [body #f]
#:id [id #f]
#:class [class #f]
#:classes [classes (if class (list class) null)]
#:title [title #f]
#:target [target #f]
#:anchor [anchor #f]
#:format [link-format (default-link-format)]
#:else [substitute (default-link-substitute)]
. args)
(let* ([requestless? (controller-requestless? controller)]
[access? (apply controller-access? controller args)]
[plain–href (if (controller-requestless? controller)
(apply controller-url controller args)
(apply controller-url controller (cdr args)))]
[href (if anchor
(format "~a#~a" plain–href anchor)
plain–href)]
[body (cond [body body]
[(eq? link-format 'sexps) (list href)]
[else href])]
[id (and id (string+symbol->string id))]
[class (and (pair? classes) (string-join (map string+symbol->string classes) " "))])
(if access?
(enum-case link-formats link-format
[(mirrors) (xml (a (@ [href ,href]
,(opt-xml-attr id)
,(opt-xml-attr class)
,(opt-xml-attr target)
,(opt-xml-attr title)) ,body))]
[(sexp) `(a ([href ,href]
,@(opt-attr-list id)
,@(opt-attr-list class)
,@(opt-attr-list target)
,@(opt-attr-list title)) ,body)]
[(sexps) `((a ([href ,href]
,@(opt-attr-list id)
,@(opt-attr-list class)
,@(opt-attr-list target)
,@(opt-attr-list title)) ,@body))])
(enum-case link-formats link-format
[(mirrors) (enum-case link-substitutes substitute
[(hide) (xml)]
[(span) (xml (span (@ ,(opt-xml-attr id)
,(opt-xml-attr class class (format "no-access-link ~a" class))
,(opt-xml-attr title)) ,body))]
[(body) (xml ,body)]
[else substitute])]
[(sexp) (enum-case link-substitutes substitute
[(hide) '(span)]
[(span) `(span (,@(opt-attr-list id)
,@(opt-attr-list class class (format "no-access-link ~a" class))
,@(opt-attr-list title)) ,body)]
[(body) body]
[else substitute])]
[(sexps) (enum-case link-substitutes substitute
[(hide) null]
[(span) `((span (,@(opt-attr-list id)
,@(opt-attr-list class class (format "no-access-link ~a" class))
,@(opt-attr-list title)) ,@body))]
[(body) body]
[else substitute])]))))
; Patterns ---------------------------------------
; site string -> (cons controller list)
(define (site-decode site url-string)
(or (for/or ([rule (in-list (site-rules site))])
(let ([match (pattern-decode (rule-pattern rule) url-string)])
(and match (cons (rule-controller rule) match))))
(list #f #f)))
; pattern string -> (U list #f)
(define (pattern-decode pattern url-string)
(let* ([url-string url-string]
[regexp ((pattern-regexp-maker pattern))]
[matches (regexp-match regexp url-string)]
[decoded (and matches
(= (length (cdr matches))
(length (pattern-args pattern)))
(for/list ([arg (in-list (pattern-args pattern))]
[match (in-list (cdr matches))])
((arg-decoder arg) match)))])
decoded))
; pattern list -> (U string #f)
(define (pattern-encode pattern args)
(and (= (length (pattern-args pattern)) (length args))
(apply string-append
(let loop ([elems (pattern-elements pattern)]
[args args])
(match elems
[(list) null]
[(list elem elem-rest ...)
(match elem
[(? string?) (cons elem (loop elem-rest args))]
[(? procedure?) (cons (elem) (loop elem-rest args))]
[(? arg?) (cons ((arg-encoder elem) (car args))
(loop elem-rest (cdr args)))])])))))
; request -> string
(define (clean-request-url request)
(string-append "/" (string-join (map (compose uri-encode path/param-path)
(url-path (request-uri request)))
"/")))
; (_ id)
; (_ boolean-expr id)
; (_ boolean-expr id expr)
(define-syntax opt-attr-list
(syntax-rules ()
[(_ test id expr) (if test `([id ,expr]) null)]
[(_ test id) (opt-attr-list test id id)]
[(_ id) (opt-attr-list id id id)]))
; (U string symbol) -> string
(define (string+symbol->string val)
(if (string? val)
val
(symbol->string val)))
; Provides ---------------------------------------
(provide/contract
[site-dispatch (-> site? request? any)]
[controller-access? (->* (controller?) () #:rest any/c boolean?)]
[controller-url (->* (controller?) () #:rest any/c (or/c string? #f))]
[controller-link (->* (controller?)
(#:body (or/c xml+quotable? pair? null? #f)
#:id (or/c symbol? string? #f)
#:class (or/c symbol? string? #f)
#:classes (listof (or/c symbol? string?))
#:target (or/c string? #f)
#:title (or/c string? #f)
#:format (enum-value/c link-formats)
#:anchor (or/c string? #f)
#:else any/c)
#:rest any/c
any)])