improve internal logic of `select`

pull/58/head
Matthew Butterick 10 years ago
parent b6769d9d6c
commit 9fd921d135

@ -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)))

@ -19,3 +19,24 @@
(check-equal? (->html #:splice #t x) "hello")
(check-equal? (->html #:tag 'brennan #:attrs '((id "dale")) x) "<brennan id=\"dale\">hello</brennan>")
(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)))

Loading…
Cancel
Save