From 9fd921d135c4b92a7da356fa7d4be2a8b295bfa3 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Sat, 13 Jun 2015 17:24:45 -0500 Subject: [PATCH] improve internal logic of `select` --- template.rkt | 22 +++++++++++++++------- tests/test-template.rkt | 21 +++++++++++++++++++++ 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/template.rkt b/template.rkt index 38f105e..04495dd 100644 --- a/template.rkt +++ b/template.rkt @@ -17,10 +17,18 @@ (define+provide/contract (select key value-source) (coerce/symbol? (or/c hash? txexpr? pagenode? pathish?) . -> . (or/c #f txexpr-element?)) - (define metas-result (and (not (txexpr? value-source)) (select-from-metas key value-source))) - (or metas-result - (let ([doc-result (select-from-doc key value-source)]) - (and doc-result (car doc-result))))) + ;; of value-source is specfically 'metas or 'doc, + ;; restrict the search to there. + ;; otherwise look in metas, then in doc for key + (define (do-doc-result) + (define doc-result (select-from-doc key value-source)) + (and doc-result (car doc-result))) + (cond + [(or (hash? value-source) (equal? value-source world:meta-pollen-export)) (select-from-metas key value-source)] + [(equal? value-source world:main-pollen-export) (do-doc-result)] + [else + (define metas-result (and (not (txexpr? value-source)) (select-from-metas key value-source))) + (or metas-result (do-doc-result))])) (define+provide/contract (select* key value-source) @@ -75,10 +83,10 @@ (define+provide/contract (->html x #:tag [tag #f] #:attrs [attrs #f] #:splice [splice? #f]) ((xexpr?) (#:tag (or/c #f txexpr-tag?) #:attrs (or/c #f txexpr-attrs?) #:splice boolean?) . ->* . string?) - + (when (and (not (txexpr? x)) attrs (not tag)) - (error '->html "can't use attribute list '~a without a #:tag argument" attrs)) - + (error '->html "can't use attribute list '~a without a #:tag argument" attrs)) + (if (or tag (txexpr? x)) (let () (define html-tag (or tag (get-tag x))) diff --git a/tests/test-template.rkt b/tests/test-template.rkt index 1d551d8..4d73376 100644 --- a/tests/test-template.rkt +++ b/tests/test-template.rkt @@ -19,3 +19,24 @@ (check-equal? (->html #:splice #t x) "hello") (check-equal? (->html #:tag 'brennan #:attrs '((id "dale")) x) "hello") (check-equal? (->html #:tag 'brennan #:attrs '((id "dale")) #:splice #t x) "hello") + + +(check-equal? (select 'key '#hash((key . "value"))) "value") +(check-false (select 'absent-key '#hash((key . "value")))) + +(let ([metas '#hash((key . "value"))]) + (check-equal? (select 'key metas) "value") + (check-false (select 'absent-key metas)) + (check-equal? (select-from-metas 'key metas) "value") + (check-false (select-from-metas 'absent-key metas))) + +(check-equal? (select 'key '(root (key "value"))) "value") +(check-false (select 'absent-key '(root (key "value")))) +(check-equal? (select-from-doc 'key '(root (key "value"))) '("value")) +(check-false (select-from-doc 'absent-key '(root (key "value")))) + +(let ([doc '(root (key "value"))]) + (check-equal? (select 'key doc) "value") + (check-false (select 'absent-key doc)) + (check-equal? (select-from-doc 'key doc) '("value")) + (check-false (select-from-doc 'absent-key doc)))