simplify lang modules

pull/9/head
Matthew Butterick 11 years ago
parent 88251d9a07
commit 978a910b74

@ -3,99 +3,102 @@
(provide (all-defined-out) (all-from-out pollen/world)) (provide (all-defined-out) (all-from-out pollen/world))
(define-syntax (make-new-module-begin stx) (define-syntax (define+provide-new-module-begin stx)
(syntax-case stx () (syntax-case stx ()
[(_ mode-arg) [(_ mode-arg)
(with-syntax ([new-module-begin (format-id stx "new-module-begin")]) (with-syntax ([new-module-begin (format-id stx "new-module-begin")])
#'(define-syntax (new-module-begin stx-arg) #'(begin
(syntax-case stx-arg () (provide (except-out (all-from-out racket/base) #%module-begin)
[(_ body-exprs (... ...)) (rename-out [new-module-begin #%module-begin]))
(syntax-protect (define-syntax (new-module-begin stx-arg)
#'(#%module-begin (syntax-case stx-arg ()
(module inner pollen/lang/doclang-raw [(_ body-exprs (... ...))
;; doclang_raw is a version of scribble/doclang with the decoder disabled (syntax-protect
;; first three lines are positional arguments for doclang-raw #'(#%module-begin
doc-raw ; id of export (module inner pollen/lang/doclang-raw
(λ(x) x) ; post-process function ;; doclang_raw is a version of scribble/doclang with the decoder disabled
() ; prepended exprs ;; first three lines are positional arguments for doclang-raw
doc-raw ; id of export
;; Change behavior of undefined identifiers with #%top (λ(x) x) ; post-process function
;; Get project values from world () ; prepended exprs
(require pollen/top pollen/world)
(provide (all-from-out pollen/top pollen/world)) ;; Change behavior of undefined identifiers with #%top
;; Get project values from world
;; for anything defined in pollen source file (require pollen/top pollen/world)
(provide (all-defined-out)) (provide (all-from-out pollen/top pollen/world))
body-exprs (... ...)) ;; for anything defined in pollen source file
(provide (all-defined-out))
(require 'inner)
body-exprs (... ...))
;; if reader-here-path is undefined, it will become a proc courtesy of #%top (require 'inner)
;; therefore that's how we can detect if it's undefined
(define here-path (if (procedure? reader-here-path) "anonymous-module" reader-here-path))
;; if reader-here-path is undefined, it will become a proc courtesy of #%top
;; therefore that's how we can detect if it's undefined
;; set the parser mode based on reader mode (define here-path (if (procedure? reader-here-path) "anonymous-module" reader-here-path))
;; todo: this won't work with inline submodules
(define parser-mode
(if (not (procedure? reader-mode)) ;; set the parser mode based on reader mode
(if (equal? reader-mode world:mode-auto) ;; todo: this won't work with inline submodules
(let* ([file-ext-pattern (pregexp "\\w+$")] (define parser-mode
[here-ext (string->symbol (car (regexp-match file-ext-pattern here-path)))]) (if (not (procedure? reader-mode))
(cond (if (equal? reader-mode world:mode-auto)
[(equal? here-ext world:pagetree-source-ext) world:mode-pagetree] (let* ([file-ext-pattern (pregexp "\\w+$")]
[(equal? here-ext world:markup-source-ext) world:mode-markup] [here-ext (string->symbol (car (regexp-match file-ext-pattern here-path)))])
[(equal? here-ext world:markdown-source-ext) world:mode-markdown] (cond
[else world:mode-preproc])) [(equal? here-ext world:pagetree-source-ext) world:mode-pagetree]
reader-mode) [(equal? here-ext world:markup-source-ext) world:mode-markup]
mode-arg)) [(equal? here-ext world:markdown-source-ext) world:mode-markdown]
[else world:mode-preproc]))
reader-mode)
;; Split out the metas. mode-arg))
(require txexpr)
(define (split-metas-to-hash tx) ; helper function
;; return tx without metas, and meta hash ;; Split out the metas.
(define is-meta-element? (λ(x) (and (txexpr? x) (equal? 'meta (car x))))) (require txexpr)
(define-values (doc-without-metas meta-elements) (define (split-metas-to-hash tx) ; helper function
(splitf-txexpr tx is-meta-element?)) ;; return tx without metas, and meta hash
(define meta-element->assoc (λ(x) (let ([key (car (caadr x))][value (cadr (caadr x))]) (cons key value)))) (define is-meta-element? (λ(x) (and (txexpr? x) (equal? 'meta (car x)))))
(define metas (make-hash (map meta-element->assoc meta-elements))) (define-values (doc-without-metas meta-elements)
(values doc-without-metas metas)) (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)))
(define doc-txexpr (values doc-without-metas metas))
(let ([doc-raw (if (equal? parser-mode world:mode-markdown)
(apply (compose1 (dynamic-require 'markdown 'parse-markdown) string-append) doc-raw)
doc-raw)]) (define doc-txexpr
`(placeholder-root (let ([doc-raw (if (equal? parser-mode world:mode-markdown)
,@(cons (meta 'here-path: here-path) (apply (compose1 (dynamic-require 'markdown 'parse-markdown) string-append) doc-raw)
;; cdr strips initial linebreak, but make sure doc-raw isn't blank doc-raw)])
(if (and (list? doc-raw) (> 0 (length doc-raw))) (cdr doc-raw) doc-raw))))) `(placeholder-root
,@(cons (meta 'here-path: here-path)
(define-values (doc-without-metas metas) (split-metas-to-hash doc-txexpr)) ;; 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)))))
;; set up the 'doc export (define-values (doc-without-metas metas) (split-metas-to-hash doc-txexpr))
(require pollen/decode)
(define doc (apply (cond
[(equal? parser-mode world:mode-pagetree) (λ xs ((dynamic-require 'pollen/pagetree 'decode-pagetree) xs))] ;; set up the 'doc export
;; 'root is the hook for the decoder function. (require pollen/decode)
;; If it's not a defined identifier, it just hits #%top and becomes `(root ,@body ...) (define doc (apply (cond
[(or (equal? parser-mode world:mode-markup) [(equal? parser-mode world:mode-pagetree) (λ xs ((dynamic-require 'pollen/pagetree 'decode-pagetree) xs))]
(equal? parser-mode world:mode-markdown)) root] ;; 'root is the hook for the decoder function.
;; for preprocessor output, just make a string. ;; If it's not a defined identifier, it just hits #%top and becomes `(root ,@body ...)
[else (λ xs (apply string-append (map to-string xs)))]) [(or (equal? parser-mode world:mode-markup)
(cdr doc-without-metas))) ;; cdr strips placeholder-root tag (equal? parser-mode world:mode-markdown)) root]
;; for preprocessor output, just make a string.
[else (λ xs (apply string-append (map to-string xs)))])
(provide metas doc (cdr doc-without-metas))) ;; cdr strips placeholder-root tag
;; hide the exports that were only for internal use.
(except-out (all-from-out 'inner) doc-raw #%top))
(provide metas doc
;; for output in DrRacket ;; hide the exports that were only for internal use.
(module+ main (except-out (all-from-out 'inner) doc-raw #%top))
(if (equal? parser-mode world:mode-preproc)
(display doc) ;; for output in DrRacket
(print doc)))))])))])) (module+ main
(if (equal? parser-mode world:mode-preproc)
(display doc)
(print doc)))))]))))]))

@ -1,7 +1,3 @@
#lang racket/base #lang racket/base
(require pollen/main-base) (require pollen/main-base)
(define+provide-new-module-begin world:mode-preproc)
(provide (except-out (all-from-out racket/base) #%module-begin)
(rename-out [new-module-begin #%module-begin]))
(make-new-module-begin world:mode-preproc)

@ -1,7 +1,3 @@
#lang racket/base #lang racket/base
(require pollen/main-base) (require pollen/main-base)
(define+provide-new-module-begin world:mode-markdown)
(provide (except-out (all-from-out racket/base) #%module-begin)
(rename-out [new-module-begin #%module-begin]))
(make-new-module-begin world:mode-markdown)

@ -1,7 +1,3 @@
#lang racket/base #lang racket/base
(require pollen/main-base) (require pollen/main-base)
(define+provide-new-module-begin world:mode-markup)
(provide (except-out (all-from-out racket/base) #%module-begin)
(rename-out [new-module-begin #%module-begin]))
(make-new-module-begin world:mode-markup)

@ -1,7 +1,3 @@
#lang racket/base #lang racket/base
(require pollen/main-base) (require pollen/main-base)
(define+provide-new-module-begin world:mode-preproc)
(provide (except-out (all-from-out racket/base) #%module-begin)
(rename-out [new-module-begin #%module-begin]))
(make-new-module-begin world:mode-preproc)

@ -1,7 +1,3 @@
#lang racket/base #lang racket/base
(require pollen/main-base) (require pollen/main-base)
(define+provide-new-module-begin world:mode-pagetree)
(provide (except-out (all-from-out racket/base) #%module-begin)
(rename-out [new-module-begin #%module-begin]))
(make-new-module-begin world:mode-pagetree)
Loading…
Cancel
Save