From 4398eba70a46b0d2ccc4ed8f3b3eb0a1344d60f5 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Thu, 27 Mar 2014 17:42:18 -0700 Subject: [PATCH] improve template abstraction --- file.rkt | 2 +- reader-base.rkt | 14 +++++++------- render.rkt | 18 +++++++++--------- tmpl.rkt | 7 +++++++ world.rkt | 3 ++- 5 files changed, 26 insertions(+), 18 deletions(-) create mode 100644 tmpl.rkt diff --git a/file.rkt b/file.rkt index 6bcdfe0..87ae0a3 100644 --- a/file.rkt +++ b/file.rkt @@ -102,7 +102,7 @@ (define+provide/contract (->output-path x) (coerce/path? . -> . coerce/path?) (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)] [else x])) diff --git a/reader-base.rkt b/reader-base.rkt index 7fc1abc..333243f 100644 --- a/reader-base.rkt +++ b/reader-base.rkt @@ -3,13 +3,6 @@ (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) (λ(p) (syntax->datum @@ -18,6 +11,13 @@ (define (make-custom-read-syntax reader-mode) (λ (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)) (datum->syntax file-contents `(module pollen-lang-module pollen diff --git a/render.rkt b/render.rkt index c6f6ffb..f5d14cd 100644 --- a/render.rkt +++ b/render.rkt @@ -64,7 +64,7 @@ ((pathish?) (#:force boolean?) . ->* . void?) (let ([so-path (->complete-path so-pathish)]) ; so-path = source or output path (could be either) (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)]) (render-to-file-if-needed source-path output-path #:force force))] [(pagetree-source? so-path) (render-pagetree so-path)])) @@ -75,8 +75,8 @@ (complete-path? . -> . (values complete-path? complete-path?)) ;; 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)) - (list has/is-null-source? has/is-preproc-source? has/is-markup-source? has/is-scribble-source? has/is-markdown-source?) - (list ->null-source+output-paths ->preproc-source+output-paths ->markup-source+output-paths ->scribble-source+output-paths ->markdown-source+output-paths))) + (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))) (file-proc source-or-output-path)) @@ -119,8 +119,8 @@ (define render-proc (cond [(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 render-null-source render-preproc-source render-markup-or-markdown-source render-scribble-source render-markup-or-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 render-preproc-source))] [else (error (format "render: no rendering function found for ~a" 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)]) (local-require pollen/pagetree pollen/template pollen/top) (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 (render-through-eval expr-to-eval)))) @@ -183,8 +183,8 @@ (complete-path? . -> . (or/c #f complete-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 - (or ; Build the possible paths and use the first one that either exists, or has a preproc source that exists. - (ormap (λ(p) (if (ormap file-exists? (list p (->preproc-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 (list (parameterize ([current-directory (world:current-project-root)]) @@ -192,7 +192,7 @@ (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 (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 diff --git a/tmpl.rkt b/tmpl.rkt new file mode 100644 index 0000000..7eb99c3 --- /dev/null +++ b/tmpl.rkt @@ -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)) \ No newline at end of file diff --git a/world.rkt b/world.rkt index 9a5f009..8956527 100644 --- a/world.rkt +++ b/world.rkt @@ -17,6 +17,7 @@ (define mode-markup 'markup) (define mode-markdown 'markdown) (define mode-pagetree 'ptree) +(define mode-template 'template) (define decodable-extensions (list markup-source-ext pagetree-source-ext)) @@ -24,7 +25,7 @@ (define pagetree-root-node 'pagetree-root) (define command-marker #\◊) -(define template-field-delimiter command-marker) +(define template-command-marker #\∂) (define default-template-prefix "main") (define fallback-template "fallback.html.pt")