From ef2394e2585539a5e5ff55d701d3f0755e57bfe9 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Sat, 1 Mar 2014 14:30:46 -0800 Subject: [PATCH] change export from 'main to 'doc --- cache.rkt | 6 +++--- main.rkt | 28 ++++++++++++++-------------- ptree.rkt | 2 +- render.rkt | 13 +++++++------ server-routes.rkt | 2 +- template.rkt | 14 +++++++------- world.rkt | 3 ++- 7 files changed, 35 insertions(+), 33 deletions(-) diff --git a/cache.rkt b/cache.rkt index 4e7dc05..7664024 100644 --- a/cache.rkt +++ b/cache.rkt @@ -1,5 +1,5 @@ #lang racket/base -(require racket/rerequire) +(require racket/rerequire "world.rkt") ;; The cache is a hash with paths as keys. ;; The cache values are also hashes, with key/value pairs for that path. @@ -26,8 +26,8 @@ (hash-set! (current-cache) path (make-hash)) (define cache-hash (cache-ref path)) (hash-set! cache-hash 'mod-time (file-or-directory-modify-seconds path)) - (hash-set! cache-hash 'main (dynamic-require path 'main)) - (hash-set! cache-hash 'metas (dynamic-require path 'metas)) + (hash-set! cache-hash world:main-pollen-export (dynamic-require path world:main-pollen-export)) + (hash-set! cache-hash world:meta-pollen-export (dynamic-require path world:meta-pollen-export)) (void)) (define (cached-require path-string key) diff --git a/main.rkt b/main.rkt index 85a4376..39b0877 100644 --- a/main.rkt +++ b/main.rkt @@ -8,7 +8,7 @@ (module inner pollen/lang/doclang-raw ;; doclang_raw is a version of scribble/doclang with the decoder disabled ;; first three lines are positional arguments for doclang-raw - main-raw ; id of export + doc-raw ; id of export (λ(x) x) ; post-process function () ; prepended exprs @@ -40,22 +40,22 @@ (define (split-metas-to-hash tx) ;; return tx without metas, and meta hash (define is-meta-element? (λ(x) (and (txexpr? x) (equal? 'meta (car x))))) - (define-values (main-without-metas meta-elements) + (define-values (doc-without-metas meta-elements) (splitf-txexpr tx is-meta-element?)) (define meta-element->assoc (λ(x) (let ([key (car (caadr x))][value (cadr (caadr x))]) (cons key value)))) (define metas (make-hash (map meta-element->assoc meta-elements))) - (values main-without-metas metas)) + (values doc-without-metas metas)) - (define main-txexpr `(placeholder-root + (define doc-txexpr `(placeholder-root ,@(cons (meta 'here: inner-here) (cons (meta 'here-path: inner-here-path) - ;; cdr strips initial linebreak, but make sure main-raw isn't blank - (if (and (list? main-raw) (> 0 (length main-raw))) (cdr main-raw) main-raw))))) + ;; cdr strips initial linebreak, but make sure doc-raw isn't blank + (if (and (list? doc-raw) (> 0 (length doc-raw))) (cdr doc-raw) doc-raw))))) - (define-values (main-without-metas metas) (split-metas-to-hash main-txexpr)) + (define-values (doc-without-metas metas) (split-metas-to-hash doc-txexpr)) - ;; set up the 'main export + ;; set up the 'doc export (require pollen/decode) ;; set the parser mode based on reader mode @@ -69,7 +69,7 @@ [else world:reader-mode-preproc])) reader-mode)) - (define main (apply (cond + (define doc (apply (cond [(equal? parser-mode world:reader-mode-ptree) (λ xs (decode (cons world:ptree-root-node xs) #:xexpr-elements-proc (λ(xs) (filter (compose1 not (def/c whitespace?)) xs))))] @@ -78,15 +78,15 @@ [(equal? parser-mode world:reader-mode-markup) root] ;; for preprocessor output, just make a string. [else (λ xs (apply string-append (map to-string xs)))]) - (cdr main-without-metas))) ;; cdr strips placeholder-root tag + (cdr doc-without-metas))) ;; cdr strips placeholder-root tag - (provide metas main + (provide metas doc ;; hide the exports that were only for internal use. - (except-out (all-from-out 'inner) inner-here inner-here-path main-raw #%top)) + (except-out (all-from-out 'inner) inner-here inner-here-path doc-raw #%top)) ;; for output in DrRacket (module+ main (if (equal? parser-mode world:reader-mode-preproc) - (display main) - (print main))))) + (display doc) + (print doc))))) diff --git a/ptree.rkt b/ptree.rkt index d75b340..39723a7 100644 --- a/ptree.rkt +++ b/ptree.rkt @@ -58,7 +58,7 @@ (define+provide/contract (make-project-ptree project-dir) (directory-pathish? . -> . ptree?) (define ptree-source (build-path project-dir world:default-ptree)) - (cached-require ptree-source 'main)) + (cached-require ptree-source world:main-pollen-export)) (module+ test diff --git a/render.rkt b/render.rkt index cafb332..0f5a8d3 100644 --- a/render.rkt +++ b/render.rkt @@ -88,7 +88,7 @@ [(has/is-null-source? path) (render-null-source path #:force force)] [(has/is-preproc-source? path) (render-preproc-source-if-needed path #:force force)] [(has/is-markup-source? path) (render-markup path #:force force)] - [(ptree-source? path) (let ([ptree (cached-require path 'main)]) + [(ptree-source? path) (let ([ptree (cached-require path world:main-pollen-export)]) (render-files-in-ptree ptree #:force force))] [(equal? world:fallback-template (->string (file-name-from-path path))) (message "Render: using fallback template")] @@ -117,14 +117,14 @@ (copy-file source-path output-path #t)) (define (render-preproc-source source-path output-path) - ;; how we render: import 'main from preproc source file, + ;; how we render: import world:main-pollen-export from preproc source file, ;; which is rendered during source parsing, and write that to output path (define-values (source-dir source-name _) (split-path source-path)) (rendering-message (format "~a from ~a" (file-name-from-path output-path) (file-name-from-path source-path))) - (let ([main (time (render-through-eval source-dir `(begin (require pollen/cache)(cached-require ,source-path 'main))))]) ;; todo: how to use world global here? Wants an identifier, not a value - (display-to-file main output-path #:exists 'replace)) + (let ([doc (time (render-through-eval source-dir `(begin (require pollen/cache)(cached-require ,source-path ',world:main-pollen-export))))]) ;; todo: how to use world global here? Wants an identifier, not a value + (display-to-file doc output-path #:exists 'replace)) (store-render-in-mod-dates source-path) ; don't store mod date until render has completed! (rendered-message output-path)) @@ -269,6 +269,7 @@ (match-define-values (_ template-name _) (split-path template-path)) (set! source-name (->string source-name)) + (define string-to-eval `(begin (require (for-syntax racket/base)) @@ -280,8 +281,8 @@ ;; then fetch the other exports out of the cache. (require pollen/lang/inner-lang-helper) (require-project-require-files) - (let ([main (cached-require ,source-name 'main)] - [metas (cached-require ,source-name 'metas)]) + (let ([doc (cached-require ,source-name ',world:main-pollen-export)] + [metas (cached-require ,source-name ',world:meta-pollen-export)]) (local-require pollen/debug pollen/ptree pollen/template pollen/top) (include-template #:command-char ,world:template-field-delimiter ,(->string template-name))))) diff --git a/server-routes.rkt b/server-routes.rkt index cc73173..3cdb7ce 100644 --- a/server-routes.rkt +++ b/server-routes.rkt @@ -57,7 +57,7 @@ ((complete-path?) (#:render boolean?) . ->* . txexpr?) (when wants-render (render path)) (dynamic-rerequire path) ; stores module mod date; reloads if it's changed - (dynamic-require path 'main)) + (dynamic-require path world:main-pollen-export)) ;; todo: rewrite this test, obsolete since filename convention changed ;;(module+ test diff --git a/template.rkt b/template.rkt index bda0314..ffc0015 100644 --- a/template.rkt +++ b/template.rkt @@ -1,6 +1,6 @@ #lang racket/base (require racket/contract racket/string xml xml/path) -(require "tools.rkt" "ptree.rkt" "cache.rkt" sugar txexpr) +(require "tools.rkt" "ptree.rkt" "cache.rkt" sugar txexpr "world.rkt") ;; setup for test cases (module+ test (require rackunit racket/path)) @@ -31,8 +31,8 @@ (cond ;; Using put has no effect on txexprs. It's here to make the idiom smooth. [(txexpr? x) x] - [(has-markup-source? x) (cached-require (->markup-source-path x) 'main)] - [(has-markup-source? (pnode->url x)) (cached-require (->markup-source-path (pnode->url x)) 'main)])) + [(has-markup-source? x) (cached-require (->markup-source-path x) world:main-pollen-export)] + [(has-markup-source? (pnode->url x)) (cached-require (->markup-source-path (pnode->url x)) world:main-pollen-export)])) #|(module+ test (check-equal? (put '(foo "bar")) '(foo "bar")) @@ -43,7 +43,7 @@ (define/contract (find query px) (query-key? (or/c #f puttable-item?) . -> . (or/c #f txexpr-element?)) - (define result (and px (or (find-in-metas px query) (find-in-main px query)))) + (define result (and px (or (find-in-metas px query) (find-in-doc px query)))) (and result (car result))) ;; return false or first element #| @@ -70,7 +70,7 @@ (check-equal? here (list "tests/template/put"))))) |# -(define/contract (find-in-main px query) +(define/contract (find-in-doc px query) (puttable-item? (or/c query-key? (listof query-key?)) . -> . (or/c #f txexpr-elements?)) (let* ([px (put px)] @@ -83,8 +83,8 @@ #| (module+ test (parameterize ([current-directory "tests/template"]) - (check-false (find-in-main "put" "nonexistent-key")) - (check-equal? (find-in-main "put" "em") (list "One" "paragraph")))) + (check-false (find-in-doc "put" "nonexistent-key")) + (check-equal? (find-in-doc "put" "em") (list "One" "paragraph")))) |# ;; turns input into xexpr-elements so they can be spliced into template diff --git a/world.rkt b/world.rkt index 7dacfaf..f460358 100644 --- a/world.rkt +++ b/world.rkt @@ -28,7 +28,8 @@ (define fallback-template "fallback.html.pt") (define template-meta-key "template") -(define main-pollen-export 'main) +(define main-pollen-export 'doc) +(define meta-pollen-export 'metas) (define extras-dir (string->path "pollen-require"))