convert path to key, and key to url

pull/9/head
Matthew Butterick 11 years ago
parent 12aa680383
commit 780e0d6845

@ -213,6 +213,24 @@
(check-equal? (next-page 'one test-pmap) "two")
(check-false (next-page 'three test-pmap)))
;; convert path to pmap-key
;; used for converting "here" values to pmap-keys
(define/contract (->pmap-key x)
(any/c . -> . pmap-key?)
(->string (remove-all-ext (last (explode-path (->path x))))))
(module+ test
(check-equal? (->pmap-key "bar") "bar")
(check-equal? (->pmap-key "foo/bar") "bar")
(check-equal? (->pmap-key "foo/bar.html") "bar")
(check-equal? (->pmap-key "/Users/this/that/foo/bar.html.pp") "bar"))
;; convert key to URL
;; = key name + suffix of template (or suffix of default template)
;; todo: finish this function, right now it just appends html
;; this would also be useful for start page (showing correct url of generated pages)
(define/contract (pmap-key->url key)
(pmap-key? . -> . string?)
(string-append key ".html"))

@ -208,9 +208,8 @@
(define result
(or (eq? x #f) ; OK for map-key to be #f
(and (or (symbol? x) (string? x))
(not (= (len x) 0)) ; not empty
; no whitespace
(andmap (compose not whitespace?) (map ->string (string->list (->string x)))))))
;; todo: should test be same as valid module name?
(->boolean (regexp-match #px"^[-_A-Za-z0-9]+$" (->string x))))))
(if (and (not result) loud)
(error "Not a valid pmap key:" x)
result))
@ -218,9 +217,10 @@
(module+ test
(check-true (pmap-key? #f))
(check-true (pmap-key? "foo-bar"))
(check-true (pmap-key? "Foo_Bar_0123"))
(check-true (pmap-key? 'foo-bar))
; todo: should this fail?
(check-false (pmap-key? "foobar.p"))
(check-false (pmap-key? "foo-bar.p"))
(check-false (pmap-key? "/Users/MB/foo-bar"))
(check-false (pmap-key? ""))
(check-false (pmap-key? " ")))

Loading…
Cancel
Save