From 780e0d6845a20332f5114bfede83fb0aff4f29d6 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Sat, 12 Oct 2013 12:22:12 -0700 Subject: [PATCH] convert path to key, and key to url --- pmap.rkt | 24 +++++++++++++++++++++--- predicates.rkt | 10 +++++----- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/pmap.rkt b/pmap.rkt index 573622f..0a7a286 100644 --- a/pmap.rkt +++ b/pmap.rkt @@ -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")) + diff --git a/predicates.rkt b/predicates.rkt index d06cb5a..93b9e79 100644 --- a/predicates.rkt +++ b/predicates.rkt @@ -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? " ")))