diff --git a/file-tools.rkt b/file-tools.rkt index a39719d..a1c8d87 100644 --- a/file-tools.rkt +++ b/file-tools.rkt @@ -116,6 +116,9 @@ (any/c . -> . coerce/boolean?) (and (pathish? x) (has-ext? (->path x) PREPROC_SOURCE_EXT))) +(define+provide/contract (has-null-source? x) + (any/c . -> . coerce/boolean?) + (and (pathish? x) (file-exists? (->null-source-path (->path x))))) (define+provide/contract (has-preproc-source? x) (any/c . -> . coerce/boolean?) @@ -136,6 +139,11 @@ ; or a file (e.g., html) that has a pollen source file (and (pathish? x) (ormap (λ(proc) (proc (->path x))) (list decoder-source? has-decoder-source?)))) +(define+provide/contract (needs-null? x) + (any/c . -> . coerce/boolean?) + ; it's a null source file, or a file that's the result of a null source + (and (pathish? x) (ormap (λ(proc) (proc (->path x))) (list null-source? has-null-source?)))) + (define+provide/contract (ptree-source? x) (any/c . -> . coerce/boolean?) @@ -144,7 +152,12 @@ (define+provide/contract (decoder-source? x) (any/c . -> . coerce/boolean?) - (and (pathish? x) (has-ext? x DECODER_SOURCE_EXT))) + (and (pathish? x) ((->path x) . has-ext? . DECODER_SOURCE_EXT))) + + +(define+provide/contract (null-source? x) + (any/c . -> . coerce/boolean?) + (and (pathish? x) ((->path x) . has-ext? . NULL_SOURCE_EXT))) (define+provide/contract (template-source? x) @@ -163,9 +176,15 @@ x (add-ext x PREPROC_SOURCE_EXT))) +(define+provide/contract (->null-source-path x) + (coerce/path? . -> . coerce/path?) + (if (decoder-source? x) + x + (add-ext x NULL_SOURCE_EXT))) + (define+provide/contract (->output-path x) (coerce/path? . -> . coerce/path?) - (if (or (decoder-source? x) (preproc-source? x)) + (if (or (decoder-source? x) (preproc-source? x) (null-source? x)) (remove-ext x) x)) diff --git a/pollen-server-extras/fallback.html.pt b/pollen-server-extras/fallback.html.pt new file mode 100644 index 0000000..eebe495 --- /dev/null +++ b/pollen-server-extras/fallback.html.pt @@ -0,0 +1 @@ +◊(->html (html (head (meta 'charset: "UTF-8")) (body "Rendered without template:" (hr) main))) \ No newline at end of file diff --git a/render.rkt b/render.rkt index d035e9a..0c468e9 100644 --- a/render.rkt +++ b/render.rkt @@ -98,6 +98,7 @@ (define (&render x) (let ([path (->complete-path x)]) (cond + [(needs-null? path) (render-null-source path #:force force)] [(needs-preproc? path) (render-preproc-source-if-needed path #:force force)] [(needs-template? path) (render-with-template path #:force force)] [(ptree-source? path) (let ([ptree (cached-require path 'main)]) @@ -119,6 +120,15 @@ (define (up-to-date-message path) (message (->string (file-name-from-path path)) "is up to date, using cached copy")) +(define (render-null-source path #:force force) + ;; this op is trivial & fast, so do it every time. + (define source-path (->complete-path (->null-source-path path))) + (define output-path (->complete-path (->output-path path))) + (message (format "Copying ~a to ~a" + (file-name-from-path source-path) + (file-name-from-path output-path))) + (copy-file source-path output-path #t)) + (define (render-preproc-source source-path output-path) ;; how we render: import 'main from preproc source file, ;; which is rendered during source parsing, and write that to output path @@ -187,10 +197,10 @@ (let ([source-metas (cached-require source-path 'metas)]) (and (TEMPLATE_META_KEY . in? . source-metas) (build-path source-dir (get source-metas TEMPLATE_META_KEY))))) ; path based on metas - (build-path source-dir - (add-ext DEFAULT_TEMPLATE_PREFIX (get-ext (->output-path source-path))))))) ; path using default template + (report (build-path source-dir + (add-ext (add-ext DEFAULT_TEMPLATE_PREFIX (get-ext (->output-path source-path))) TEMPLATE_EXT)))))) ; path using default template (let ([ft-path (build-path source-dir FALLBACK_TEMPLATE)]) ; if none of these work, make fallback template file - (display-to-file fallback-template-data ft-path #:exists 'replace) + (copy-file (build-path (current-server-extras-path) FALLBACK_TEMPLATE) ft-path #t) ft-path))) (render template-path #:force force-render) ; bc template might have its own preprocessor source @@ -227,10 +237,10 @@ racket/match pollen/debug pollen/decode - pollen/file-tools + ;; pollen/file-tools ;; not pollen/main, because it brings in pollen/top pollen/lang/inner-lang-helper - pollen/predicates + pollen/predicates ;; exports file-tools pollen/ptree pollen/cache sugar @@ -259,7 +269,6 @@ racket/match pollen/debug pollen/decode - pollen/file-tools pollen/lang/inner-lang-helper pollen/predicates pollen/ptree @@ -297,12 +306,15 @@ (render-through-eval source-dir string-to-eval)) +#| (module+ main (parameterize ([current-cache (make-cache)] [CURRENT_PROJECT_ROOT (string->path "/Users/mb/git/bpt")]) (render-source-with-template (string->path "/Users/mb/git/bpt/test.html.pm") (string->path "/Users/mb/git/bpt/-test.html")))) +|# + (define (render-files-in-ptree ptree #:force [force #f]) diff --git a/server-routes.rkt b/server-routes.rkt index f60d334..d6518ed 100644 --- a/server-routes.rkt +++ b/server-routes.rkt @@ -163,7 +163,7 @@ (define possible-sources (if (directory-exists? fn) empty ;; folders don't have source files - (filter file-in-dir? (list (->preproc-source-path filename) (->decoder-source-path filename))))) + (filter file-in-dir? (list (->preproc-source-path filename) (->decoder-source-path filename) (->null-source-path filename))))) (define source (and (not (empty? possible-sources)) (->string (car possible-sources)))) `(tr ,@(map make-link-cell (append (list diff --git a/server.rkt b/server.rkt index 2d5c826..a554c01 100755 --- a/server.rkt +++ b/server.rkt @@ -24,14 +24,16 @@ (message "Ready to rock") -(define MODULE_ROOT (apply build-path (drop-right (explode-path (current-contract-region)) 1))) -(define SERVER_EXTRAS_DIR (build-path MODULE_ROOT "pollen-server-extras")) +(current-module-root (apply build-path (drop-right (explode-path (current-contract-region)) 1))) +(current-server-extras-path (build-path (current-module-root) "pollen-server-extras")) -(parameterize ([current-cache (make-cache)]) +(parameterize ([current-module-root (current-module-root)] + [current-server-extras-path (current-server-extras-path)] + [current-cache (make-cache)]) (serve/servlet pollen-servlet #:port SERVER_PORT #:listen-ip #f #:servlet-regexp #rx"" ; respond to top level #:command-line? #t #:file-not-found-responder route-404 - #:extra-files-paths (list SERVER_EXTRAS_DIR (CURRENT_PROJECT_ROOT)))) \ No newline at end of file + #:extra-files-paths (list (current-server-extras-path) (CURRENT_PROJECT_ROOT)))) \ No newline at end of file diff --git a/template.rkt b/template.rkt index 7716571..55edc61 100644 --- a/template.rkt +++ b/template.rkt @@ -9,9 +9,6 @@ (require sugar/scribble sugar/coerce) (provide (all-from-out sugar/scribble sugar/coerce)) -;; todo: better fallback template - -(define fallback-template-data "FALLBACK! ◊(main)") ;; todo: docstrings for this subsection diff --git a/world.rkt b/world.rkt index 0faf358..66d117d 100644 --- a/world.rkt +++ b/world.rkt @@ -8,6 +8,7 @@ (define PREPROC_SOURCE_EXT 'p) (define DECODER_SOURCE_EXT 'pm) +(define NULL_SOURCE_EXT 'px) (define PTREE_SOURCE_EXT 'ptree) (define DECODABLE_EXTENSIONS (list DECODER_SOURCE_EXT PTREE_SOURCE_EXT)) @@ -18,8 +19,9 @@ (define EXPRESSION_DELIMITER #\◊) (define TEMPLATE_FIELD_DELIMITER EXPRESSION_DELIMITER) -(define DEFAULT_TEMPLATE_PREFIX "-main") -(define FALLBACK_TEMPLATE "-temp-fallback-template.html") +(define DEFAULT_TEMPLATE_PREFIX "main") +(define TEMPLATE_EXT 'pt) +(define FALLBACK_TEMPLATE "fallback.html.pt") (define TEMPLATE_META_KEY "template") (define MAIN_POLLEN_EXPORT 'main) @@ -50,4 +52,7 @@ (define SERVER_PORT 8088) (define DASHBOARD_NAME "index.ptree") -(define DASHBOARD_CSS "poldash.css") \ No newline at end of file +(define DASHBOARD_CSS "poldash.css") + +(define current-module-root (make-parameter #f)) +(define current-server-extras-path (make-parameter #f)) \ No newline at end of file