improve template abstraction

pull/9/head
Matthew Butterick 11 years ago
parent 521b8c77d9
commit 4398eba70a

@ -102,7 +102,7 @@
(define+provide/contract (->output-path x) (define+provide/contract (->output-path x)
(coerce/path? . -> . coerce/path?) (coerce/path? . -> . coerce/path?)
(cond (cond
[(or (markup-source? x) (preproc-source? x) (null-source? x) (markdown-source? x)) (remove-ext x)] [(or (markup-source? x) (preproc-source? x) (null-source? x) (markdown-source? x) (template-source? x)) (remove-ext x)]
[(scribble-source? x) (add-ext (remove-ext x) 'html)] [(scribble-source? x) (add-ext (remove-ext x) 'html)]
[else x])) [else x]))

@ -3,13 +3,6 @@
(provide define+provide-reader-in-mode (all-from-out pollen/world)) (provide define+provide-reader-in-mode (all-from-out pollen/world))
(define read-inner (make-at-reader
#:command-char world:command-marker
#:syntax? #t
#:inside? #t))
(define (make-custom-read custom-read-syntax-proc) (define (make-custom-read custom-read-syntax-proc)
(λ(p) (λ(p)
(syntax->datum (syntax->datum
@ -18,6 +11,13 @@
(define (make-custom-read-syntax reader-mode) (define (make-custom-read-syntax reader-mode)
(λ (path-string p) (λ (path-string p)
(define read-inner (make-at-reader
#:command-char (if (or (equal? reader-mode world:mode-template)
(regexp-match (pregexp (format "\\.~a$" world:template-source-ext)) path-string))
world:template-command-marker
world:command-marker)
#:syntax? #t
#:inside? #t))
(define file-contents (read-inner path-string p)) (define file-contents (read-inner path-string p))
(datum->syntax file-contents (datum->syntax file-contents
`(module pollen-lang-module pollen `(module pollen-lang-module pollen

@ -64,7 +64,7 @@
((pathish?) (#:force boolean?) . ->* . void?) ((pathish?) (#:force boolean?) . ->* . void?)
(let ([so-path (->complete-path so-pathish)]) ; so-path = source or output path (could be either) (let ([so-path (->complete-path so-pathish)]) ; so-path = source or output path (could be either)
(cond (cond
[(ormap (λ(test) (test so-path)) (list has/is-null-source? has/is-preproc-source? has/is-markup-source? has/is-scribble-source? has/is-markdown-source?)) [(ormap (λ(test) (test so-path)) (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?))
(let-values ([(source-path output-path) (->source+output-paths so-path)]) (let-values ([(source-path output-path) (->source+output-paths so-path)])
(render-to-file-if-needed source-path output-path #:force force))] (render-to-file-if-needed source-path output-path #:force force))]
[(pagetree-source? so-path) (render-pagetree so-path)])) [(pagetree-source? so-path) (render-pagetree so-path)]))
@ -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?) (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))) (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))
@ -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?) (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))] (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)))
@ -168,7 +168,7 @@
[metas (cached-require ,source-path ',world:meta-pollen-export)]) [metas (cached-require ,source-path ',world:meta-pollen-export)])
(local-require pollen/pagetree pollen/template pollen/top) (local-require pollen/pagetree pollen/template pollen/top)
(define here (metas->here metas)) (define here (metas->here metas))
(include-template #:command-char ,world:template-field-delimiter ,(->string (find-relative-path source-dir template-path)))))) (include-template #:command-char ,world:command-marker ,(->string (find-relative-path source-dir template-path))))))
(time (parameterize ([current-directory source-dir]) ; because include-template wants to work relative to source location (time (parameterize ([current-directory source-dir]) ; because include-template wants to work relative to source location
(render-through-eval expr-to-eval)))) (render-through-eval expr-to-eval))))
@ -183,8 +183,8 @@
(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 a preproc source that exists. (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 (->preproc-source-path p))) p #f)) (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)])
@ -192,7 +192,7 @@
(and ((->symbol world:template-meta-key) . in? . source-metas) (and ((->symbol world:template-meta-key) . in? . source-metas)
(build-path source-dir (get source-metas (->string world:template-meta-key)))))) ; path based on metas (build-path source-dir (get source-metas (->string world:template-meta-key)))))) ; path based on metas
(build-path (world:current-project-root) (build-path (world:current-project-root)
(add-ext (add-ext world:default-template-prefix (get-ext (->output-path source-path))) world:template-source-ext))))) ; path to default template (add-ext world:default-template-prefix (get-ext (->output-path source-path))))))) ; path to default template
(build-path (world:current-server-extras-path) world:fallback-template)))) ; fallback template (build-path (world:current-server-extras-path) world:fallback-template)))) ; fallback template

@ -0,0 +1,7 @@
#lang racket/base
(require pollen/main-base)
(define+provide-module-begin-in-mode world:mode-template)
(module reader racket/base
(require pollen/reader-base)
(define+provide-reader-in-mode world:mode-template))

@ -17,6 +17,7 @@
(define mode-markup 'markup) (define mode-markup 'markup)
(define mode-markdown 'markdown) (define mode-markdown 'markdown)
(define mode-pagetree 'ptree) (define mode-pagetree 'ptree)
(define mode-template 'template)
(define decodable-extensions (list markup-source-ext pagetree-source-ext)) (define decodable-extensions (list markup-source-ext pagetree-source-ext))
@ -24,7 +25,7 @@
(define pagetree-root-node 'pagetree-root) (define pagetree-root-node 'pagetree-root)
(define command-marker #\◊) (define command-marker #\◊)
(define template-field-delimiter command-marker) (define template-command-marker #\∂)
(define default-template-prefix "main") (define default-template-prefix "main")
(define fallback-template "fallback.html.pt") (define fallback-template "fallback.html.pt")

Loading…
Cancel
Save