add support for binary output

pull/27/head
Matthew Butterick 10 years ago
parent 563a145b28
commit 46d16e7c84

@ -111,11 +111,13 @@
(define/contract+provide (render-to-file source-path [template-path #f] [maybe-output-path #f]) (define/contract+provide (render-to-file source-path [template-path #f] [maybe-output-path #f])
((complete-path?) ((or/c #f complete-path?) (or/c #f complete-path?)) . ->* . void?) ((complete-path?) ((or/c #f complete-path?) (or/c #f complete-path?)) . ->* . void?)
(define output-path (or maybe-output-path (->output-path source-path))) (define output-path (or maybe-output-path (->output-path source-path)))
(display-to-file (render source-path template-path) output-path #:exists 'replace)) (define render-result (render source-path template-path)) ; will either be string or bytes
(display-to-file render-result output-path #:exists 'replace
#:mode (if (string? render-result) 'text 'binary)))
(define/contract+provide (render source-path [template-path #f]) (define/contract+provide (render source-path [template-path #f])
((complete-path?) ((or/c #f complete-path?)) . ->* . bytes?) ((complete-path?) ((or/c #f complete-path?)) . ->* . (or/c string? bytes?))
(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))
@ -136,7 +138,7 @@
(define/contract (render-scribble-source source-path) (define/contract (render-scribble-source source-path)
(complete-path? . -> . bytes?) (complete-path? . -> . string?)
(match-define-values (source-dir _ _) (split-path source-path)) (match-define-values (source-dir _ _) (split-path source-path))
(file-needed-rerequire? source-path) ; called for its reqrequire side effect only, so dynamic-require below isn't cached (file-needed-rerequire? source-path) ; called for its reqrequire side effect only, so dynamic-require below isn't cached
(time (parameterize ([current-directory (->complete-path source-dir)]) (time (parameterize ([current-directory (->complete-path source-dir)])
@ -148,14 +150,14 @@
(define/contract (render-preproc-source source-path) (define/contract (render-preproc-source source-path)
(complete-path? . -> . bytes?) (complete-path? . -> . (or/c string? bytes?))
(match-define-values (source-dir _ _) (split-path source-path)) (match-define-values (source-dir _ _) (split-path source-path))
(time (parameterize ([current-directory (->complete-path source-dir)]) (time (parameterize ([current-directory (->complete-path source-dir)])
(render-through-eval `(begin (require pollen/cache)(cached-require ,source-path ',world:main-pollen-export)))))) (render-through-eval `(begin (require pollen/cache)(cached-require ,source-path ',world:main-pollen-export))))))
(define/contract (render-markup-or-markdown-source source-path [maybe-template-path #f]) (define/contract (render-markup-or-markdown-source source-path [maybe-template-path #f])
((complete-path?) ((or/c #f complete-path?)) . ->* . bytes?) ((complete-path?) ((or/c #f complete-path?)) . ->* . (or/c string? bytes?))
(match-define-values (source-dir _ _) (split-path source-path)) (match-define-values (source-dir _ _) (split-path source-path))
(define template-path (or maybe-template-path (get-template-for source-path))) (define template-path (or maybe-template-path (get-template-for source-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
@ -168,8 +170,10 @@
[metas (cached-require ,(path->string source-path) ',world:meta-pollen-export)]) [metas (cached-require ,(path->string 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:command-marker (file ,(->string (find-relative-path source-dir template-path))))))) (cond
[(bytes? doc) doc] ; if doc is binary, just pass it through
[else
(include-template #:command-char ,world:command-marker (file ,(->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))))
@ -256,11 +260,11 @@
(define/contract (render-through-eval expr-to-eval) (define/contract (render-through-eval expr-to-eval)
(list? . -> . bytes?) (list? . -> . (or/c string? bytes?))
(define cache-ns (car (current-eval-namespace-cache))) (define cache-ns (car (current-eval-namespace-cache)))
(define cached-modules (cdr (current-eval-namespace-cache))) (define cached-modules (cdr (current-eval-namespace-cache)))
(parameterize ([current-namespace (make-base-namespace)] (parameterize ([current-namespace (make-base-namespace)]
[current-output-port (current-error-port)] [current-output-port (current-error-port)]
[current-pagetree (make-project-pagetree (world:current-project-root))]) [current-pagetree (make-project-pagetree (world:current-project-root))])
(for-each (λ(mod-name) (namespace-attach-module cache-ns mod-name)) cached-modules) (for-each (λ(mod-name) (namespace-attach-module cache-ns mod-name)) cached-modules)
(string->bytes/utf-8 (eval expr-to-eval (current-namespace))))) (eval expr-to-eval (current-namespace))))
Loading…
Cancel
Save