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 ;; file isn't yet saved in drracket
[(equal? 'pollen-lang-module ccr) 'nowhere] [(equal? 'pollen-lang-module ccr) 'nowhere]
[else ccr])]) [else ccr])])
(displayln "ccr in main=") ;; pass complete path back as here value (as string)
(displayln ccr) ;; Why not relative to current-directory?
(displayln "current-directory in main=") ;; Because current-directory can't be parameterized
;; todo: this does not respond to parameterize. Why? ;; so raises possibility of inconsistent values.
(displayln (current-directory)) ;; Whereas the complete path is unambiguous,
;; and can be made relative by the caller (or otherwise altered).
(->string here-path))))) (->string here-path)))))
(module+ test (module+ test

@ -11,12 +11,16 @@
(define fallback-template-data "FALLBACK! ◊(put-as-html main)") (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) (define/contract (puttable-item? x)
(any/c . -> . boolean?) (any/c . -> . boolean?)
(or (tagged-xexpr? x) (has-pollen-source? x))) (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) (define/contract (put x)
(puttable-item? . -> . tagged-xexpr?) (puttable-item? . -> . tagged-xexpr?)
(cond (cond
@ -27,38 +31,50 @@
(module+ test (module+ test
(check-equal? (put '(foo "bar")) '(foo "bar")) (check-equal? (put '(foo "bar")) '(foo "bar"))
(check-equal? (put "tests/template/put.p") (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) (define/contract (find-in x query)
(puttable-item? (or/c string? symbol?) . -> . (or/c list? false?)) (puttable-item? query-key? . -> . (or/c xexpr-elements? false?))
(or (or (find-in-metas x query) (find-in-main x query)))
(and (has-pollen-source? x) (from-metas x query))
(from-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) (define/contract (find-in-metas x key)
(has-pollen-source? (or/c string? symbol?) . -> . (or/c list? false?)) (puttable-item? query-key? . -> . (or/c xexpr-elements? false?))
(and (has-pollen-source? x)
(let ([metas (dynamic-require (make-pollen-source-path x) 'metas)] (let ([metas (dynamic-require (make-pollen-source-path x) 'metas)]
[key (->string key)]) [key (->string key)])
;; todo: why am I returning value as xexpr? (and (key . in? . metas ) (->list (get metas key))))))
(and (key . in? . metas ) `(value ,(get metas key)))))
(module+ test (module+ test
(parameterize ([current-directory "tests/template"]) (parameterize ([current-directory "tests/template"])
(let ([metas (dynamic-require (make-pollen-source-path 'put) 'metas)]) (check-equal? (find-in-metas "put" "foo") (list "bar"))
(check-equal? (from-metas "put" "foo") '(value "bar")) (let* ([metas (dynamic-require (make-pollen-source-path 'put) 'metas)]
(check-equal? (from-metas 'put 'here) `(value ,(find-relative-path (current-directory) (->path (get metas "here")))))))) [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)] (let* ([x (put x)]
[results (se-path*/list (list query) x)]) ;; make sure query is a list of symbols (required by se-path*/list)
; if results exist, send back xexpr as output [query (map ->symbol (->list query))]
(if (not (empty? results)) [results (se-path*/list query x)])
`(,query ,@results) ; todo: why use query as tag? ;; if results exist, send back xexpr as output
#f))) (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) (define (merge x)

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