improve handling of scribble render

pull/155/head
Matthew Butterick 7 years ago
parent 1886bb7f5d
commit 25ca9e9426

@ -1 +1 @@
1502141536 1502144359

@ -134,7 +134,7 @@
(define render-proc (for/first ([test (in-list tests)] (define render-proc (for/first ([test (in-list tests)]
[render-proc (in-list render-procs)] [render-proc (in-list render-procs)]
#:when (test source-path)) #:when (test source-path))
render-proc)) render-proc))
(unless render-proc (unless render-proc
(raise-argument-error 'render (format "valid rendering function for ~a" source-path) render-proc)) (raise-argument-error 'render (format "valid rendering function for ~a" source-path) render-proc))
@ -162,18 +162,20 @@
(define (render-scribble-source source-path . _) (define (render-scribble-source source-path . _)
;((complete-path?) #:rest any/c . ->* . string?) ;((complete-path?) #:rest any/c . ->* . string?)
(define scribble-render (parameterize ([current-namespace (make-base-namespace)]) (local-require scribble/core scribble/manual (prefix-in scribble- scribble/render))
(dynamic-require 'scribble/render 'render))) (define source-dir (dirname source-path))
(time (parameterize ([current-directory (->complete-path (dirname source-path))]) ;; make fresh namespace for scribble rendering (avoids dep/zo caching)
;; if there's a compiled zo file for the Scribble file, (time (parameterize ([current-namespace (make-base-namespace)]
;; (as is usually the case in existing packages) [current-directory (->complete-path source-dir)])
;; it will foul up the render (namespace-attach-module (namespace-anchor->namespace render-module-ns) 'scribble/core)
;; so recompile first to avoid "can't redefine a constant" errors. (namespace-attach-module (namespace-anchor->namespace render-module-ns) 'scribble/manual)
(managed-compile-zo source-path)
;; scribble/lp files have their doc export in a 'doc submodule, so check both locations ;; scribble/lp files have their doc export in a 'doc submodule, so check both locations
(define doc (dynamic-require source-path 'doc (define doc
(λ () (dynamic-require `(submod ,source-path doc) 'doc [cond
(λ () #f))))) [(dynamic-require source-path 'doc (λ () #f))]
[(dynamic-require `(submod ,source-path doc) 'doc (λ () #f))]
[else #f]])
;; BTW this next action has side effects: scribble will copy in its core files if they don't exist. ;; BTW this next action has side effects: scribble will copy in its core files if they don't exist.
(when doc (when doc
(scribble-render (list doc) (list source-path))))) (scribble-render (list doc) (list source-path)))))
@ -186,8 +188,8 @@
(time (parameterize ([current-directory (->complete-path (dirname source-path))]) (time (parameterize ([current-directory (->complete-path (dirname source-path))])
(render-through-eval (syntax->datum (render-through-eval (syntax->datum
(with-syntax ([SOURCE-PATH source-path]) (with-syntax ([SOURCE-PATH source-path])
#`(begin (require pollen/cache) #`(begin (require pollen/cache)
(cached-doc SOURCE-PATH)))))))) (cached-doc SOURCE-PATH))))))))
(define (render-markup-or-markdown-source source-path [maybe-template-path #f] [maybe-output-path #f]) (define (render-markup-or-markdown-source source-path [maybe-template-path #f] [maybe-output-path #f])
@ -202,29 +204,29 @@
(define expr-to-eval (define expr-to-eval
(syntax->datum (syntax->datum
(with-syntax ([DIRECTORY-REQUIRE-FILES (require-directory-require-files source-path)] (with-syntax ([DIRECTORY-REQUIRE-FILES (require-directory-require-files source-path)]
[DOC-ID (setup:main-export source-path)] [DOC-ID (setup:main-export source-path)]
[META-ID (setup:meta-export source-path)] [META-ID (setup:meta-export source-path)]
[SOURCE-PATH-STRING (path->string source-path)] [SOURCE-PATH-STRING (path->string source-path)]
[CPR (current-project-root)] [CPR (current-project-root)]
[HERE-PATH-KEY (setup:here-path-key source-path)] [HERE-PATH-KEY (setup:here-path-key source-path)]
[COMMAND-CHAR (setup:command-char source-path)] [COMMAND-CHAR (setup:command-char source-path)]
[TEMPLATE-PATH (->string template-path)]) [TEMPLATE-PATH (->string template-path)])
#'(begin #'(begin
(require (for-syntax racket/base) (require (for-syntax racket/base)
pollen/private/include-template pollen/private/include-template
pollen/cache pollen/cache
pollen/private/debug pollen/private/debug
pollen/pagetree pollen/pagetree
pollen/core) pollen/core)
DIRECTORY-REQUIRE-FILES DIRECTORY-REQUIRE-FILES
(parameterize ([current-pagetree (make-project-pagetree CPR)]) (parameterize ([current-pagetree (make-project-pagetree CPR)])
(define DOC-ID (cached-doc SOURCE-PATH-STRING)) (define DOC-ID (cached-doc SOURCE-PATH-STRING))
(define META-ID (cached-metas SOURCE-PATH-STRING)) (define META-ID (cached-metas SOURCE-PATH-STRING))
(local-require pollen/template pollen/top) (local-require pollen/template pollen/top)
(define here (path->pagenode (or (select-from-metas 'HERE-PATH-KEY META-ID) 'unknown))) (define here (path->pagenode (or (select-from-metas 'HERE-PATH-KEY META-ID) 'unknown)))
(if (bytes? DOC-ID) ; if main export is binary, just pass it through (if (bytes? DOC-ID) ; if main export is binary, just pass it through
DOC-ID DOC-ID
(include-template #:command-char COMMAND-CHAR (file TEMPLATE-PATH)))))))) (include-template #:command-char COMMAND-CHAR (file TEMPLATE-PATH))))))))
;; set current-directory because include-template wants to work relative to source location ;; set current-directory because include-template wants to work relative to source location
(time (parameterize ([current-directory (->complete-path source-dir)]) (time (parameterize ([current-directory (->complete-path source-dir)])
(render-through-eval expr-to-eval)))) (render-through-eval expr-to-eval))))
@ -241,7 +243,7 @@
(define (file-exists-or-has-source? p) ; p could be #f (define (file-exists-or-has-source? p) ; p could be #f
(and p (for/first ([proc (in-list (list identity ->preproc-source-path ->null-source-path))] (and p (for/first ([proc (in-list (list identity ->preproc-source-path ->null-source-path))]
#:when (file-exists? (proc p))) #:when (file-exists? (proc p)))
p))) p)))
(define (get-template) (define (get-template)
(define source-dir (dirname source-path)) (define source-dir (dirname source-path))

Loading…
Cancel
Save