From 6e7a72cc9982ce8f19218f4a372d40253ae723de Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Thu, 13 Mar 2014 17:54:10 -0700 Subject: [PATCH] updates --- cache.rkt | 6 +-- project-requires.rkt | 2 + scribblings/ptree.scrbl | 2 +- scribblings/template.scrbl | 2 +- template.rkt | 76 +++++++++++++++++++------------------- 5 files changed, 44 insertions(+), 44 deletions(-) diff --git a/cache.rkt b/cache.rkt index 7c0af82..d1168db 100644 --- a/cache.rkt +++ b/cache.rkt @@ -6,12 +6,10 @@ (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 current-cache (make-parameter (make-cache))) + (define (reset-cache) (hash-clear! (current-cache))) (define (->complete-path path-string) diff --git a/project-requires.rkt b/project-requires.rkt index c416417..09268ee 100644 --- a/project-requires.rkt +++ b/project-requires.rkt @@ -2,11 +2,13 @@ (require "world.rkt") (provide (all-defined-out)) + (define (project-require-file? path) (define path-string (path->string path)) (define racket-ext "rkt") (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 (define (get-project-require-files) (define extras-directory (build-path (world:current-project-root) world:extras-dir)) diff --git a/scribblings/ptree.scrbl b/scribblings/ptree.scrbl index 00c0228..6de922f 100644 --- a/scribblings/ptree.scrbl +++ b/scribblings/ptree.scrbl @@ -5,7 +5,7 @@ @(define my-eval (make-base-eval)) @(my-eval `(require pollen pollen/pagemap txexpr)) -@title{pagemaps} +@title{Pagemaps} @defmodule[pollen/pagemap] diff --git a/scribblings/template.scrbl b/scribblings/template.scrbl index 44c073a..7346979 100644 --- a/scribblings/template.scrbl +++ b/scribblings/template.scrbl @@ -15,7 +15,7 @@ Convenience functions for templates. These are automatically imported into the @ (->html [tx txexpr?]) 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 (define tx '(root (script "3 > 2") "Why is 3 > 2?")) diff --git a/template.rkt b/template.rkt index 2322362..9044bc0 100644 --- a/template.rkt +++ b/template.rkt @@ -1,6 +1,6 @@ #lang 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") @@ -8,47 +8,47 @@ (provide (all-from-out sugar/coerce/value)) -(define+provide/contract (puttable-item? x) - (any/c . -> . boolean?) - (or (txexpr? x) (has-markup-source? x))) +(define/contract+provide (doc x) + (coerce/path? . -> . txexpr?) + (cached-require x world:main-pollen-export)) -(define+provide/contract (query-key? x) - (any/c . -> . boolean?) - (or (string? x) (symbol? x))) +(define/contract+provide (metas x) + (coerce/path? . -> . hash?) + (cached-require x world:meta-pollen-export)) -(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] - [(has-markup-source? x) (cached-require (->markup-source-path x) world:main-pollen-export)])) - - -(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)))) - (and result (car result))) ;; return false or first element - - -(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)] - [key (->string key)]) - (and (key . in? . metas ) (->list (get metas key)))))) - - -(define+provide/contract (find-in-doc px query) - (puttable-item? (or/c query-key? (listof query-key?)) - . -> . (or/c #f txexpr-elements?)) - (let* ([px (put px)] - ;; make sure query is a list of symbols (required by se-path*/list) - [query (map ->symbol (->list query))] - [results (se-path*/list query px)]) - ;; if results exist, send back xexpr as output - (and (not (empty? results)) results))) +(define/contract+provide (find query . xs) + ((coerce/symbol?) #:rest (listof (or/c #f hash? txexpr? pathish?)) . ->* . (or/c #f txexpr-element?)) + (define result (apply find* query xs)) + (or (null? result) (car result))) + + +(define/contract+provide (find* query . pxs) + ((coerce/symbol?) #:rest (listof (or/c #f hash? txexpr? pathish?)) . ->* . (or/c #f txexpr-element?)) + (define (finder x) + (cond + [(hash? x) (find-in-metas query x)] + [(txexpr? x) (find-in-doc query x)] + [(pathish? x) (find* query (doc x) (metas x))] + [else null])) + (append-map finder pxs)) + + +(define/contract+provide (find-in-metas query hash-or-path) + (coerce/symbol? (or/c hash? pathish?) . -> . (or/c #f txexpr-elements?)) + (let ([metas (or (and (hash? hash-or-path) hash-or-path) + (metas (->path hash-or-path)))]) + (with-handlers ([exn:fail? (λ(e) null)]) + (list (hash-ref metas query))))) + + +(define/contract+provide (find-in-doc query doc-or-path) + (coerce/symbol? (or/c txexpr? pathish?) . -> . (or/c #f txexpr-elements?)) + (let ([doc (or (and (txexpr? doc-or-path) doc-or-path) + (doc (->path doc-or-path)))]) + (with-handlers ([exn:fail? (λ(e) null)]) + (se-path*/list query doc)))) ;; turns input into xexpr-elements so they can be spliced into template