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

80 lines
2.6 KiB
Racket

#lang racket/base
11 years ago
(require (for-syntax racket/base))
(require racket/string xml xml/path sugar/define sugar/container)
(require "tools.rkt" txexpr "world.rkt" "cache.rkt")
11 years ago
(require sugar/coerce/value)
(provide (all-from-out sugar/coerce/value))
11 years ago
(define+provide/contract (puttable-item? x)
(any/c . -> . boolean?)
11 years ago
(or (txexpr? x) (has-markup-source? x)))
11 years ago
(define+provide/contract (query-key? x)
11 years ago
(any/c . -> . boolean?)
(or (string? x) (symbol? x)))
11 years ago
(define+provide/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
(define+provide/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
(define+provide/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
(define+provide/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)))
;; 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?
11 years ago
(define+provide/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)]))
11 years ago
11 years ago
(define+provide/contract (->html x)
11 years ago
(txexpr? . -> . string?)
(txexpr->html x))
11 years ago
11 years ago
(provide when/block)
(define-syntax (when/block stx)
(syntax-case stx ()
[(_ condition body ...)
#'(if condition (string-append*
(with-handlers ([exn:fail? (λ(exn) (error (format "when/block: ~a" (exn-message exn))))])
(map ->string (list body ...))))
"")]))