change export from 'main to 'doc

pull/9/head
Matthew Butterick 11 years ago
parent 53fd17aafd
commit ef2394e258

@ -1,5 +1,5 @@
#lang racket/base #lang racket/base
(require racket/rerequire) (require racket/rerequire "world.rkt")
;; The cache is a hash with paths as keys. ;; The cache is a hash with paths as keys.
;; The cache values are also hashes, with key/value pairs for that path. ;; The cache values are also hashes, with key/value pairs for that path.
@ -26,8 +26,8 @@
(hash-set! (current-cache) path (make-hash)) (hash-set! (current-cache) path (make-hash))
(define cache-hash (cache-ref path)) (define cache-hash (cache-ref path))
(hash-set! cache-hash 'mod-time (file-or-directory-modify-seconds 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 world:main-pollen-export (dynamic-require path world:main-pollen-export))
(hash-set! cache-hash 'metas (dynamic-require path 'metas)) (hash-set! cache-hash world:meta-pollen-export (dynamic-require path world:meta-pollen-export))
(void)) (void))
(define (cached-require path-string key) (define (cached-require path-string key)

@ -8,7 +8,7 @@
(module inner pollen/lang/doclang-raw (module inner pollen/lang/doclang-raw
;; doclang_raw is a version of scribble/doclang with the decoder disabled ;; doclang_raw is a version of scribble/doclang with the decoder disabled
;; first three lines are positional arguments for doclang-raw ;; first three lines are positional arguments for doclang-raw
main-raw ; id of export doc-raw ; id of export
(λ(x) x) ; post-process function (λ(x) x) ; post-process function
() ; prepended exprs () ; prepended exprs
@ -40,22 +40,22 @@
(define (split-metas-to-hash tx) (define (split-metas-to-hash tx)
;; return tx without metas, and meta hash ;; return tx without metas, and meta hash
(define is-meta-element? (λ(x) (and (txexpr? x) (equal? 'meta (car x))))) (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?)) (splitf-txexpr tx is-meta-element?))
(define meta-element->assoc (λ(x) (let ([key (car (caadr x))][value (cadr (caadr x))]) (cons key value)))) (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))) (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: inner-here)
(cons (meta 'here-path: inner-here-path) (cons (meta 'here-path: inner-here-path)
;; cdr strips initial linebreak, but make sure main-raw isn't blank ;; cdr strips initial linebreak, but make sure doc-raw isn't blank
(if (and (list? main-raw) (> 0 (length main-raw))) (cdr main-raw) main-raw))))) (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) (require pollen/decode)
;; set the parser mode based on reader mode ;; set the parser mode based on reader mode
@ -69,7 +69,7 @@
[else world:reader-mode-preproc])) [else world:reader-mode-preproc]))
reader-mode)) reader-mode))
(define main (apply (cond (define doc (apply (cond
[(equal? parser-mode world:reader-mode-ptree) [(equal? parser-mode world:reader-mode-ptree)
(λ xs (decode (cons world:ptree-root-node xs) (λ xs (decode (cons world:ptree-root-node xs)
#:xexpr-elements-proc (λ(xs) (filter (compose1 not (def/c whitespace?)) xs))))] #:xexpr-elements-proc (λ(xs) (filter (compose1 not (def/c whitespace?)) xs))))]
@ -78,15 +78,15 @@
[(equal? parser-mode world:reader-mode-markup) root] [(equal? parser-mode world:reader-mode-markup) root]
;; for preprocessor output, just make a string. ;; for preprocessor output, just make a string.
[else (λ xs (apply string-append (map to-string xs)))]) [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. ;; 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 ;; for output in DrRacket
(module+ main (module+ main
(if (equal? parser-mode world:reader-mode-preproc) (if (equal? parser-mode world:reader-mode-preproc)
(display main) (display doc)
(print main))))) (print doc)))))

@ -58,7 +58,7 @@
(define+provide/contract (make-project-ptree project-dir) (define+provide/contract (make-project-ptree project-dir)
(directory-pathish? . -> . ptree?) (directory-pathish? . -> . ptree?)
(define ptree-source (build-path project-dir world:default-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 (module+ test

@ -88,7 +88,7 @@
[(has/is-null-source? path) (render-null-source path #:force force)] [(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-preproc-source? path) (render-preproc-source-if-needed path #:force force)]
[(has/is-markup-source? path) (render-markup 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))] (render-files-in-ptree ptree #:force force))]
[(equal? world:fallback-template (->string (file-name-from-path path))) [(equal? world:fallback-template (->string (file-name-from-path path)))
(message "Render: using fallback template")] (message "Render: using fallback template")]
@ -117,14 +117,14 @@
(copy-file source-path output-path #t)) (copy-file source-path output-path #t))
(define (render-preproc-source source-path output-path) (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 ;; which is rendered during source parsing, and write that to output path
(define-values (source-dir source-name _) (split-path source-path)) (define-values (source-dir source-name _) (split-path source-path))
(rendering-message (format "~a from ~a" (rendering-message (format "~a from ~a"
(file-name-from-path output-path) (file-name-from-path output-path)
(file-name-from-path source-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 (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 main output-path #:exists 'replace)) (display-to-file doc output-path #:exists 'replace))
(store-render-in-mod-dates source-path) ; don't store mod date until render has completed! (store-render-in-mod-dates source-path) ; don't store mod date until render has completed!
(rendered-message output-path)) (rendered-message output-path))
@ -269,6 +269,7 @@
(match-define-values (_ template-name _) (split-path template-path)) (match-define-values (_ template-name _) (split-path template-path))
(set! source-name (->string source-name)) (set! source-name (->string source-name))
(define string-to-eval (define string-to-eval
`(begin `(begin
(require (for-syntax racket/base)) (require (for-syntax racket/base))
@ -280,8 +281,8 @@
;; then fetch the other exports out of the cache. ;; then fetch the other exports out of the cache.
(require pollen/lang/inner-lang-helper) (require pollen/lang/inner-lang-helper)
(require-project-require-files) (require-project-require-files)
(let ([main (cached-require ,source-name 'main)] (let ([doc (cached-require ,source-name ',world:main-pollen-export)]
[metas (cached-require ,source-name 'metas)]) [metas (cached-require ,source-name ',world:meta-pollen-export)])
(local-require pollen/debug pollen/ptree pollen/template pollen/top) (local-require pollen/debug pollen/ptree pollen/template pollen/top)
(include-template #:command-char ,world:template-field-delimiter ,(->string template-name))))) (include-template #:command-char ,world:template-field-delimiter ,(->string template-name)))))

@ -57,7 +57,7 @@
((complete-path?) (#:render boolean?) . ->* . txexpr?) ((complete-path?) (#:render boolean?) . ->* . txexpr?)
(when wants-render (render path)) (when wants-render (render path))
(dynamic-rerequire path) ; stores module mod date; reloads if it's changed (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 ;; todo: rewrite this test, obsolete since filename convention changed
;;(module+ test ;;(module+ test

@ -1,6 +1,6 @@
#lang racket/base #lang racket/base
(require racket/contract racket/string xml xml/path) (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 ;; setup for test cases
(module+ test (require rackunit racket/path)) (module+ test (require rackunit racket/path))
@ -31,8 +31,8 @@
(cond (cond
;; Using put has no effect on txexprs. It's here to make the idiom smooth. ;; Using put has no effect on txexprs. It's here to make the idiom smooth.
[(txexpr? x) x] [(txexpr? x) x]
[(has-markup-source? x) (cached-require (->markup-source-path 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)) 'main)])) [(has-markup-source? (pnode->url x)) (cached-require (->markup-source-path (pnode->url x)) world:main-pollen-export)]))
#|(module+ test #|(module+ test
(check-equal? (put '(foo "bar")) '(foo "bar")) (check-equal? (put '(foo "bar")) '(foo "bar"))
@ -43,7 +43,7 @@
(define/contract (find query px) (define/contract (find query px)
(query-key? (or/c #f puttable-item?) . -> . (or/c #f txexpr-element?)) (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 (and result (car result))) ;; return false or first element
#| #|
@ -70,7 +70,7 @@
(check-equal? here (list "tests/template/put"))))) (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?)) (puttable-item? (or/c query-key? (listof query-key?))
. -> . (or/c #f txexpr-elements?)) . -> . (or/c #f txexpr-elements?))
(let* ([px (put px)] (let* ([px (put px)]
@ -83,8 +83,8 @@
#| #|
(module+ test (module+ test
(parameterize ([current-directory "tests/template"]) (parameterize ([current-directory "tests/template"])
(check-false (find-in-main "put" "nonexistent-key")) (check-false (find-in-doc "put" "nonexistent-key"))
(check-equal? (find-in-main "put" "em") (list "One" "paragraph")))) (check-equal? (find-in-doc "put" "em") (list "One" "paragraph"))))
|# |#
;; turns input into xexpr-elements so they can be spliced into template ;; turns input into xexpr-elements so they can be spliced into template

@ -28,7 +28,8 @@
(define fallback-template "fallback.html.pt") (define fallback-template "fallback.html.pt")
(define template-meta-key "template") (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")) (define extras-dir (string->path "pollen-require"))

Loading…
Cancel
Save