You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pollen/template.rkt

123 lines
3.9 KiB
Racket

#lang racket/base
11 years ago
(require racket/contract racket/string xml xml/path)
(require "tools.rkt" "ptree.rkt" "cache.rkt" sugar txexpr "world.rkt")
;; setup for test cases
(module+ test (require rackunit racket/path))
(provide (all-defined-out))
11 years ago
(require sugar/scribble sugar/coerce/value)
(provide (all-from-out sugar/scribble sugar/coerce/value))
11 years ago
;; todo: docstrings for this subsection
(define/contract (puttable-item? x)
(any/c . -> . boolean?)
11 years ago
(or (txexpr? x) (has-markup-source? x)))
(module+ test
(check-false (puttable-item? #t))
(check-false (puttable-item? #f)))
11 years ago
(define/contract (query-key? x)
(any/c . -> . boolean?)
(or (string? x) (symbol? x)))
(define/contract (put x)
(puttable-item? . -> . txexpr?)
(cond
;; Using put has no effect on txexprs. It's here to make the idiom smooth.
[(txexpr? x) x]
11 years ago
[(has-markup-source? x) (cached-require (->markup-source-path x) world:main-pollen-export)]))
11 years ago
#|(module+ test
(check-equal? (put '(foo "bar")) '(foo "bar"))
11 years ago
(check-equal? (put "tests/template/put.pd")
11 years ago
'(root "\n" "\n" (em "One") " paragraph" "\n" "\n" "Another " (em "paragraph") "\n" "\n")))
11 years ago
|#
11 years ago
(define/contract (find query px)
(query-key? (or/c #f puttable-item?) . -> . (or/c #f txexpr-element?))
(define result (and px (or (find-in-metas px query) (find-in-doc px query))))
11 years ago
(and result (car result))) ;; return false or first element
11 years ago
#|
11 years ago
(module+ test
(parameterize ([current-directory "tests/template"])
11 years ago
(check-false (find "nonexistent-key" "put"))
(check-equal? (find "foo" "put") "bar")
(check-equal? (find "em" "put") "One"))
(check-equal? (find "foo" #f) #f))
11 years ago
|#
(define/contract (find-in-metas px key)
(puttable-item? query-key? . -> . (or/c #f txexpr-elements?))
(and (has-markup-source? px)
(let ([metas (cached-require (->markup-source-path px) 'metas)]
11 years ago
[key (->string key)])
(and (key . in? . metas ) (->list (get metas key))))))
11 years ago
#|(module+ test
(parameterize ([current-directory "tests/template"])
11 years ago
(check-equal? (find-in-metas "put" "foo") (list "bar"))
(let* ([metas (cached-require (->markup-source-path 'put) 'metas)]
11 years ago
[here (find-in-metas 'put 'here)])
(check-equal? here (list "tests/template/put")))))
11 years ago
|#
(define/contract (find-in-doc px query)
11 years ago
(puttable-item? (or/c query-key? (listof query-key?))
. -> . (or/c #f txexpr-elements?))
(let* ([px (put px)]
11 years ago
;; make sure query is a list of symbols (required by se-path*/list)
[query (map ->symbol (->list query))]
[results (se-path*/list query px)])
11 years ago
;; if results exist, send back xexpr as output
(and (not (empty? results)) results)))
11 years ago
#|
11 years ago
(module+ test
(parameterize ([current-directory "tests/template"])
(check-false (find-in-doc "put" "nonexistent-key"))
(check-equal? (find-in-doc "put" "em") (list "One" "paragraph"))))
11 years ago
|#
;; turns input into xexpr-elements so they can be spliced into template
;; (as opposed to dropped in as a full txexpr)
;; by returning a list, pollen rules will automatically merge into main flow
;; todo: explain why
;; todo: do I need this?
(define/contract (splice x)
((or/c txexpr? txexpr-elements? string?) . -> . txexpr-elements?)
(cond
[(txexpr? x) (get-elements x)]
[(txexpr-elements? x) x]
[(string? x) (->list x)]))
(module+ test
(check-equal? (splice '(p "foo" "bar")) (list "foo" "bar"))
(check-equal? (splice (list "foo" "bar")) (list "foo" "bar"))
(check-equal? (splice "foo") (list "foo")))
(define/contract (make-html x)
((or/c txexpr? txexpr-elements? txexpr-element?) . -> . string?)
(cond
[(txexpr? x) (xexpr->string x)]
[else (let ([x (->list x)])
(string-join (map xexpr->string x) ""))]))
; generate *-as-html versions of functions
(define-values (put-as-html splice-as-html)
(apply values (map (λ(proc) (λ(x) (make-html (proc x)))) (list put splice))))
11 years ago
11 years ago
(define/contract (->html x)
(txexpr? . -> . string?)
(txexpr->html x))
11 years ago