fixes to template

pull/9/head
Matthew Butterick 11 years ago
parent bb9bb04832
commit 029c02bb97

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

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

@ -1,7 +1,7 @@
#lang planet mb/pollen
One paragraph
em{One} paragraph
Another paragraph
Another em{paragraph}
meta["foo" "bar"]
Loading…
Cancel
Save