better checks for get-template-for

pull/15/merge
Matthew Butterick 10 years ago
parent b4af1936d5
commit ec4863b4a0

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

@ -36,7 +36,7 @@
(() #:rest (listof (or/c #f complete-path?)) . ->* . boolean?) (() #:rest (listof (or/c #f complete-path?)) . ->* . boolean?)
(define key (make-mod-dates-key rest-paths)) (define key (make-mod-dates-key rest-paths))
(or (not (key . in? . modification-date-hash)) ; no stored mod date (or (not (key . in? . modification-date-hash)) ; no stored mod date
(not (equal? (map path->mod-date-value key) (get modification-date-hash key))))) ; data has changed (not (equal? (map path->mod-date-value key) (get modification-date-hash key))))) ; data has changed
(define/contract+provide (render-batch . xs) (define/contract+provide (render-batch . xs)
@ -47,15 +47,15 @@
;; Using reset-modification-dates is sort of like session control. ;; Using reset-modification-dates is sort of like session control.
(reset-modification-dates) (reset-modification-dates)
(for-each (λ(x) ((if (pagetree-source? x) (for-each (λ(x) ((if (pagetree-source? x)
render-pagetree render-pagetree
render-from-source-or-output-path) x)) xs)) render-from-source-or-output-path) x)) xs))
(define/contract+provide (render-pagetree pagetree-or-path) (define/contract+provide (render-pagetree pagetree-or-path)
((or/c pagetree? pathish?) . -> . void?) ((or/c pagetree? pathish?) . -> . void?)
(define pagetree (if (pagetree? pagetree-or-path) (define pagetree (if (pagetree? pagetree-or-path)
pagetree-or-path pagetree-or-path
(cached-require pagetree-or-path world:main-pollen-export))) (cached-require pagetree-or-path world:main-pollen-export)))
(parameterize ([current-directory (world:current-project-root)]) (parameterize ([current-directory (world:current-project-root)])
(for-each render-from-source-or-output-path (map ->complete-path (pagetree->list pagetree))))) (for-each render-from-source-or-output-path (map ->complete-path (pagetree->list pagetree)))))
@ -75,8 +75,8 @@
(complete-path? . -> . (values complete-path? complete-path?)) (complete-path? . -> . (values complete-path? complete-path?))
;; file-proc returns two values, but ormap only wants one ;; file-proc returns two values, but ormap only wants one
(define file-proc (ormap (λ(test file-proc) (and (test source-or-output-path) file-proc)) (define file-proc (ormap (λ(test file-proc) (and (test source-or-output-path) file-proc))
(list has/is-null-source? has/is-preproc-source? has/is-markup-source? has/is-scribble-source? has/is-markdown-source? has/is-template-source?) (list has/is-null-source? has/is-preproc-source? has/is-markup-source? has/is-scribble-source? has/is-markdown-source? has/is-template-source?)
(list ->null-source+output-paths ->preproc-source+output-paths ->markup-source+output-paths ->scribble-source+output-paths ->markdown-source+output-paths ->template-source+output-paths))) (list ->null-source+output-paths ->preproc-source+output-paths ->markup-source+output-paths ->scribble-source+output-paths ->markdown-source+output-paths ->template-source+output-paths)))
(file-proc source-or-output-path)) (file-proc source-or-output-path))
@ -95,9 +95,9 @@
(define/contract (render-needed? source-path template-path output-path) (define/contract (render-needed? source-path template-path output-path)
(complete-path? (or/c #f complete-path?) complete-path? . -> . boolean?) (complete-path? (or/c #f complete-path?) complete-path? . -> . boolean?)
(or (not (file-exists? output-path)) (or (not (file-exists? output-path))
(modification-date-expired? source-path template-path) (modification-date-expired? source-path template-path)
(and (not (null-source? source-path)) (file-needed-rerequire? source-path)) (and (not (null-source? source-path)) (file-needed-rerequire? source-path))
(and (world:check-project-requires-in-render?) (project-requires-changed? source-path)))) (and (world:check-project-requires-in-render?) (project-requires-changed? source-path))))
(define/contract+provide (render-to-file-if-needed source-path [template-path #f] [maybe-output-path #f] #:force [force #f]) (define/contract+provide (render-to-file-if-needed source-path [template-path #f] [maybe-output-path #f] #:force [force #f])
@ -119,8 +119,8 @@
(define render-proc (define render-proc
(cond (cond
[(ormap (λ(test render-proc) (and (test source-path) render-proc)) [(ormap (λ(test render-proc) (and (test source-path) render-proc))
(list has/is-null-source? has/is-preproc-source? has/is-markup-source? has/is-scribble-source? has/is-markdown-source? has/is-template-source?) (list has/is-null-source? has/is-preproc-source? has/is-markup-source? has/is-scribble-source? has/is-markdown-source? has/is-template-source?)
(list render-null-source render-preproc-source render-markup-or-markdown-source render-scribble-source render-markup-or-markdown-source render-preproc-source))] (list render-null-source render-preproc-source render-markup-or-markdown-source render-scribble-source render-markup-or-markdown-source render-preproc-source))]
[else (error (format "render: no rendering function found for ~a" source-path))])) [else (error (format "render: no rendering function found for ~a" source-path))]))
(message (format "render: ~a" (file-name-from-path source-path))) (message (format "render: ~a" (file-name-from-path source-path)))
@ -183,17 +183,18 @@
(complete-path? . -> . (or/c #f complete-path?)) (complete-path? . -> . (or/c #f complete-path?))
(match-define-values (source-dir _ _) (split-path source-path)) (match-define-values (source-dir _ _) (split-path source-path))
(and (templated-source? source-path) ; doesn't make sense if it's not a templated source format (and (templated-source? source-path) ; doesn't make sense if it's not a templated source format
(or ; Build the possible paths and use the first one that either exists, or has existing source (template, preproc, or null) (let ([output-path (->output-path source-path)])
(ormap (λ(p) (if (ormap file-exists? (list p (->template-source-path p) (->preproc-source-path p) (->null-source-path p))) p #f)) (or ; Build the possible paths and use the first one that either exists, or has existing source (template, preproc, or null)
(ormap (λ(p) (if (ormap file-exists? (list p (->template-source-path p) (->preproc-source-path p) (->null-source-path p))) p #f))
(filter (λ(x) (->boolean x)) ; if any of the possibilities below are invalid, they return #f (filter (λ(x) (->boolean x)) ; if any of the possibilities below are invalid, they return #f
(list (list
(parameterize ([current-directory (world:current-project-root)]) (parameterize ([current-directory (world:current-project-root)])
(let ([source-metas (cached-require source-path 'metas)]) (let ([source-metas (cached-require source-path 'metas)])
(and ((->symbol world:template-meta-key) . in? . source-metas) (and ((->symbol world:template-meta-key) . in? . source-metas)
(build-path source-dir (select-from-metas (->string world:template-meta-key) source-metas))))) ; path based on metas (build-path source-dir (select-from-metas (->string world:template-meta-key) source-metas))))) ; path based on metas
(and (filename-extension (->output-path source-path)) (build-path (world:current-project-root) (and (filename-extension output-path) (build-path (world:current-project-root)
(add-ext world:default-template-prefix (get-ext (->output-path source-path)))))))) ; path to default template (add-ext world:default-template-prefix (get-ext output-path))))))) ; path to default template
(build-path (world:current-server-extras-path) world:fallback-template)))) ; fallback template (and (filename-extension output-path) (build-path (world:current-server-extras-path) (add-ext world:fallback-template-prefix (get-ext output-path)))))))) ; fallback template
(define/contract (file-needed-rerequire? source-path) (define/contract (file-needed-rerequire? source-path)
@ -228,27 +229,27 @@
(current-eval-namespace-cache (cons cache-ns (cons module-name cached-modules))))) (current-eval-namespace-cache (cons cache-ns (cons module-name cached-modules)))))
(define initial-modules-to-cache '(web-server/templates (define initial-modules-to-cache '(web-server/templates
xml xml
racket/port racket/port
racket/file racket/file
racket/rerequire racket/rerequire
racket/contract racket/contract
racket/list racket/list
racket/match racket/match
racket/syntax racket/syntax
pollen/cache pollen/cache
pollen/debug pollen/debug
pollen/decode pollen/decode
pollen/file pollen/file
pollen/main pollen/main
pollen/reader-base pollen/reader-base
pollen/pagetree pollen/pagetree
pollen/tag pollen/tag
pollen/template pollen/template
pollen/world pollen/world
pollen/project pollen/project
sugar sugar
txexpr)) txexpr))
(for-each add-module-to-current-eval-cache initial-modules-to-cache) (for-each add-module-to-current-eval-cache initial-modules-to-cache)

@ -90,8 +90,8 @@ The magic character that indicates a Pollen command, function, or variable. Init
@defthing[world:default-template-prefix string?] @defthing[world:default-template-prefix string?]
Prefix of the default template. Initialized to @racket["template"]. Prefix of the default template. Initialized to @racket["template"].
@defthing[world:fallback-template string?] @defthing[world:fallback-template-prefix string?]
Name of the fallback template (i.e., the template used to render a Pollen markup file when no other template can be found). Initialized to @racket["fallback.html.pt"]. Used to generate the name of the fallback template (i.e., the template used to render a Pollen markup file when no other template can be found). Prefix is combined with the output suffix of the source file. Initialized to @racket["fallback"].
@defthing[world:template-meta-key symbol?] @defthing[world:template-meta-key symbol?]
Meta key used to store a template name for that particular source file. Initialized to @racket['template]. Meta key used to store a template name for that particular source file. Initialized to @racket['template].

@ -28,7 +28,7 @@
(define template-command-marker #\∂) (define template-command-marker #\∂)
(define default-template-prefix "template") (define default-template-prefix "template")
(define fallback-template "fallback.html") (define fallback-template-prefix "fallback")
(define template-meta-key "template") (define template-meta-key "template")
(define main-pollen-export 'doc) ; don't forget to change fallback template too (define main-pollen-export 'doc) ; don't forget to change fallback template too

Loading…
Cancel
Save