pull/9/head
Matthew Butterick 11 years ago
parent 10b6bd446d
commit 6e7a72cc99

@ -6,12 +6,10 @@
(provide reset-cache current-cache make-cache cached-require cache-ref) (provide reset-cache current-cache make-cache cached-require cache-ref)
;; Don't initialize a cache when the module is loaded. This induces reliance.
;; The cache only makes sense if a single one is used across a whole session (e.g., via parameterize).
(define current-cache (make-parameter #f))
(define (make-cache) (make-hash)) (define (make-cache) (make-hash))
(define current-cache (make-parameter (make-cache)))
(define (reset-cache) (hash-clear! (current-cache))) (define (reset-cache) (hash-clear! (current-cache)))
(define (->complete-path path-string) (define (->complete-path path-string)

@ -2,11 +2,13 @@
(require "world.rkt") (require "world.rkt")
(provide (all-defined-out)) (provide (all-defined-out))
(define (project-require-file? path) (define (project-require-file? path)
(define path-string (path->string path)) (define path-string (path->string path))
(define racket-ext "rkt") (define racket-ext "rkt")
(equal? (substring path-string (- (string-length path-string) (string-length racket-ext)) (string-length path-string)) racket-ext)) (equal? (substring path-string (- (string-length path-string) (string-length racket-ext)) (string-length path-string)) racket-ext))
;; list of all eligible requires in project require directory ;; list of all eligible requires in project require directory
(define (get-project-require-files) (define (get-project-require-files)
(define extras-directory (build-path (world:current-project-root) world:extras-dir)) (define extras-directory (build-path (world:current-project-root) world:extras-dir))

@ -5,7 +5,7 @@
@(define my-eval (make-base-eval)) @(define my-eval (make-base-eval))
@(my-eval `(require pollen pollen/pagemap txexpr)) @(my-eval `(require pollen pollen/pagemap txexpr))
@title{pagemaps} @title{Pagemaps}
@defmodule[pollen/pagemap] @defmodule[pollen/pagemap]

@ -15,7 +15,7 @@ Convenience functions for templates. These are automatically imported into the @
(->html (->html
[tx txexpr?]) [tx txexpr?])
string?] string?]
Convert @racket[_tx] to an HTML string. Consistent with the HTML spec (and unlike @racket[xexpr->string]), text that appears within @code{script} or @code{style} blocks will not be escaped. Convert @racket[_tx] to an HTML string. Similar to @racket[xexpr->string], but consistent with the HTML spec, text that appears within @code{script} or @code{style} blocks will not be escaped.
@examples[#:eval my-eval @examples[#:eval my-eval
(define tx '(root (script "3 > 2") "Why is 3 > 2?")) (define tx '(root (script "3 > 2") "Why is 3 > 2?"))

@ -1,6 +1,6 @@
#lang racket/base #lang racket/base
(require (for-syntax racket/base)) (require (for-syntax racket/base))
(require racket/string xml xml/path sugar/define sugar/container) (require racket/string xml xml/path sugar/define sugar/container sugar/coerce/contract)
(require "tools.rkt" txexpr "world.rkt" "cache.rkt") (require "tools.rkt" txexpr "world.rkt" "cache.rkt")
@ -8,47 +8,47 @@
(provide (all-from-out sugar/coerce/value)) (provide (all-from-out sugar/coerce/value))
(define+provide/contract (puttable-item? x) (define/contract+provide (doc x)
(any/c . -> . boolean?) (coerce/path? . -> . txexpr?)
(or (txexpr? x) (has-markup-source? x))) (cached-require x world:main-pollen-export))
(define+provide/contract (query-key? x) (define/contract+provide (metas x)
(any/c . -> . boolean?) (coerce/path? . -> . hash?)
(or (string? x) (symbol? x))) (cached-require x world:meta-pollen-export))
(define+provide/contract (put x) (define/contract+provide (find query . xs)
(puttable-item? . -> . txexpr?) ((coerce/symbol?) #:rest (listof (or/c #f hash? txexpr? pathish?)) . ->* . (or/c #f txexpr-element?))
(cond (define result (apply find* query xs))
;; Using put has no effect on txexprs. It's here to make the idiom smooth. (or (null? result) (car result)))
[(txexpr? x) x]
[(has-markup-source? x) (cached-require (->markup-source-path x) world:main-pollen-export)]))
(define/contract+provide (find* query . pxs)
((coerce/symbol?) #:rest (listof (or/c #f hash? txexpr? pathish?)) . ->* . (or/c #f txexpr-element?))
(define+provide/contract (find query px) (define (finder x)
(query-key? (or/c #f puttable-item?) . -> . (or/c #f txexpr-element?)) (cond
(define result (and px (or (find-in-metas px query) (find-in-doc px query)))) [(hash? x) (find-in-metas query x)]
(and result (car result))) ;; return false or first element [(txexpr? x) (find-in-doc query x)]
[(pathish? x) (find* query (doc x) (metas x))]
[else null]))
(define+provide/contract (find-in-metas px key) (append-map finder pxs))
(puttable-item? query-key? . -> . (or/c #f txexpr-elements?))
(and (has-markup-source? px)
(let ([metas (cached-require (->markup-source-path px) 'metas)] (define/contract+provide (find-in-metas query hash-or-path)
[key (->string key)]) (coerce/symbol? (or/c hash? pathish?) . -> . (or/c #f txexpr-elements?))
(and (key . in? . metas ) (->list (get metas key)))))) (let ([metas (or (and (hash? hash-or-path) hash-or-path)
(metas (->path hash-or-path)))])
(with-handlers ([exn:fail? (λ(e) null)])
(define+provide/contract (find-in-doc px query) (list (hash-ref metas query)))))
(puttable-item? (or/c query-key? (listof query-key?))
. -> . (or/c #f txexpr-elements?))
(let* ([px (put px)] (define/contract+provide (find-in-doc query doc-or-path)
;; make sure query is a list of symbols (required by se-path*/list) (coerce/symbol? (or/c txexpr? pathish?) . -> . (or/c #f txexpr-elements?))
[query (map ->symbol (->list query))] (let ([doc (or (and (txexpr? doc-or-path) doc-or-path)
[results (se-path*/list query px)]) (doc (->path doc-or-path)))])
;; if results exist, send back xexpr as output (with-handlers ([exn:fail? (λ(e) null)])
(and (not (empty? results)) results))) (se-path*/list query doc))))
;; turns input into xexpr-elements so they can be spliced into template ;; turns input into xexpr-elements so they can be spliced into template

Loading…
Cancel
Save