From 029c02bb979752e7ea3662c2ad73b0161e1e1f20 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Sat, 24 Aug 2013 13:38:35 -0700 Subject: [PATCH] fixes to template --- main-helper.rkt | 11 ++++---- template.rkt | 62 ++++++++++++++++++++++++++++---------------- tests/template/put.p | 4 +-- 3 files changed, 47 insertions(+), 30 deletions(-) diff --git a/main-helper.rkt b/main-helper.rkt index 36d5c97..64e1dc7 100644 --- a/main-helper.rkt +++ b/main-helper.rkt @@ -57,11 +57,12 @@ ;; file isn't yet saved in drracket [(equal? 'pollen-lang-module ccr) 'nowhere] [else ccr])]) - (displayln "ccr in main=") - (displayln ccr) - (displayln "current-directory in main=") - ;; todo: this does not respond to parameterize. Why? - (displayln (current-directory)) + ;; pass complete path back as here value (as string) + ;; Why not relative to current-directory? + ;; Because current-directory can't be parameterized + ;; so raises possibility of inconsistent values. + ;; Whereas the complete path is unambiguous, + ;; and can be made relative by the caller (or otherwise altered). (->string here-path))))) (module+ test diff --git a/template.rkt b/template.rkt index fac9902..c7f0309 100644 --- a/template.rkt +++ b/template.rkt @@ -11,12 +11,16 @@ (define fallback-template-data "FALLBACK! ◊(put-as-html main)") -;; todo: tests & contracts for this subsection +;; todo: docstrings for this subsection (define/contract (puttable-item? x) (any/c . -> . boolean?) (or (tagged-xexpr? x) (has-pollen-source? x))) +(define/contract (query-key? x) + (any/c . -> . boolean?) + (or (string? x) (symbol? x))) + (define/contract (put x) (puttable-item? . -> . tagged-xexpr?) (cond @@ -27,38 +31,50 @@ (module+ test (check-equal? (put '(foo "bar")) '(foo "bar")) (check-equal? (put "tests/template/put.p") - '(root "\n" "\n" "One paragraph" "\n" "\n" "Another paragraph" "\n" "\n"))) + '(root "\n" "\n" (em "One") " paragraph" "\n" "\n" "Another " (em "paragraph") "\n" "\n"))) -(define/contract (from x query) - (puttable-item? (or/c string? symbol?) . -> . (or/c list? false?)) - (or - (and (has-pollen-source? x) (from-metas x query)) - (from-main x query))) +(define/contract (find-in x query) + (puttable-item? query-key? . -> . (or/c xexpr-elements? false?)) + (or (find-in-metas x query) (find-in-main x query))) +(module+ test + (parameterize ([current-directory "tests/template"]) + (check-false (find-in "put" "nonexistent-key")) + (check-equal? (find-in "put" "foo") (list "bar")) + (check-equal? (find-in "put" "em") (list "One" "paragraph")))) -(define/contract (from-metas x key) - (has-pollen-source? (or/c string? symbol?) . -> . (or/c list? false?)) - (let ([metas (dynamic-require (make-pollen-source-path x) 'metas)] - [key (->string key)]) - ;; todo: why am I returning value as xexpr? - (and (key . in? . metas ) `(value ,(get metas key))))) +(define/contract (find-in-metas x key) + (puttable-item? query-key? . -> . (or/c xexpr-elements? false?)) + (and (has-pollen-source? x) + (let ([metas (dynamic-require (make-pollen-source-path x) 'metas)] + [key (->string key)]) + (and (key . in? . metas ) (->list (get metas key)))))) (module+ test (parameterize ([current-directory "tests/template"]) - (let ([metas (dynamic-require (make-pollen-source-path 'put) 'metas)]) - (check-equal? (from-metas "put" "foo") '(value "bar")) - (check-equal? (from-metas 'put 'here) `(value ,(find-relative-path (current-directory) (->path (get metas "here")))))))) + (check-equal? (find-in-metas "put" "foo") (list "bar")) + (let* ([metas (dynamic-require (make-pollen-source-path 'put) 'metas)] + [here (find-in-metas 'put 'here)] + [here-relative (list (->string (find-relative-path (current-directory) (car here))))]) + (check-equal? here-relative (list "put.p"))))) + -(define (from-main x query) ; this used to be plain from - ; check results first +(define/contract (find-in-main x query) + (puttable-item? (or/c query-key? (listof query-key?)) + . -> . (or/c xexpr-elements? false?)) (let* ([x (put x)] - [results (se-path*/list (list query) x)]) - ; if results exist, send back xexpr as output - (if (not (empty? results)) - `(,query ,@results) ; todo: why use query as tag? - #f))) + ;; make sure query is a list of symbols (required by se-path*/list) + [query (map ->symbol (->list query))] + [results (se-path*/list query x)]) + ;; if results exist, send back xexpr as output + (and (not (empty? results)) results))) + +(module+ test + (parameterize ([current-directory "tests/template"]) + (check-false (find-in-main "put" "nonexistent-key")) + (check-equal? (find-in-main "put" "em") (list "One" "paragraph")))) (define (merge x) diff --git a/tests/template/put.p b/tests/template/put.p index dafd098..9a17da4 100644 --- a/tests/template/put.p +++ b/tests/template/put.p @@ -1,7 +1,7 @@ #lang planet mb/pollen -One paragraph +◊em{One} paragraph -Another paragraph +Another ◊em{paragraph} ◊meta["foo" "bar"] \ No newline at end of file