correct render cache handling & improve render dynamism

pull/199/head
Matthew Butterick 6 years ago
parent f1a45de2dd
commit 25ef0b9618

@ -1 +1 @@
1552843398 1553393594

@ -90,29 +90,33 @@
(unless output-path (unless output-path
(raise-argument-error caller "valid output path" output-path)) (raise-argument-error caller "valid output path" output-path))
(define template-path (or maybe-template-path (get-template-for source-path output-path))) (define template-path (or maybe-template-path (get-template-for source-path output-path)))
(define render-cache-activated? (setup:render-cache-active source-path))
(define render-needed? (define render-needed?
(cond (cond
[force?] [force?]
[(not (file-exists? output-path)) 'file-missing] [(not (file-exists? output-path)) 'file-missing]
[(mod-date-missing-or-changed? source-path template-path) 'mod-key-missing-or-changed] [(mod-date-missing-or-changed? source-path template-path) 'mod-key-missing-or-changed]
[(not (setup:render-cache-active source-path)) 'render-cache-deactivated] [(not render-cache-activated?) 'render-cache-deactivated]
[else #false])) [else #false]))
(when render-needed? (when render-needed?
(define render-result (define render-result
(let ([key (paths->key source-path template-path output-path)]) (let ([key (paths->key source-path template-path output-path)]
[render-thunk (λ () (render source-path template-path output-path))]) ; returns either string or bytes
(if render-cache-activated?
(hash-ref! render-ram-cache (hash-ref! render-ram-cache
;; within a session, this will prevent repeat players like "template.html.p" ;; within a session, this will prevent repeat players like "template.html.p"
;; from hitting the file cache repeatedly ;; from hitting the file cache repeatedly
key key
(λ ()
(cache-ref! key (cache-ref! key
(λ () (render source-path template-path output-path)) render-thunk
#:dest-path 'output #:dest-path 'output
#:notify-cache-use #:notify-cache-use
(λ (str) (λ (str)
(message (format "from cache /~a" (message (format "from cache /~a"
(find-relative-path (current-project-root) output-path))))))))) ; will either be string or bytes (find-relative-path (current-project-root) output-path))))))
(display-to-file render-result output-path (render-thunk))))
(display-to-file render-result
output-path
#:exists 'replace #:exists 'replace
#:mode (if (string? render-result) 'text 'binary)))) #:mode (if (string? render-result) 'text 'binary))))
@ -211,7 +215,10 @@
(unless template-path (unless template-path
(raise-argument-error 'render-markup-or-markdown-source "valid template path" template-path)) (raise-argument-error 'render-markup-or-markdown-source "valid template path" template-path))
(render-from-source-or-output-path template-path) ; because template might have its own preprocessor source (render-from-source-or-output-path template-path) ; because template might have its own preprocessor source
(parameterize ([current-output-port (current-error-port)]) (parameterize ([current-output-port (current-error-port)]
[current-namespace (make-base-namespace)])
(define outer-ns (namespace-anchor->namespace render-module-ns))
(namespace-attach-module outer-ns 'pollen/setup)
(eval (with-syntax ([MODNAME (gensym)] (eval (with-syntax ([MODNAME (gensym)]
[SOURCE-PATH-STRING (->string source-path)] [SOURCE-PATH-STRING (->string source-path)]
[TEMPLATE-PATH-STRING (->string template-path)]) [TEMPLATE-PATH-STRING (->string template-path)])

Loading…
Cancel
Save