You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pollen/main.rkt

74 lines
3.1 KiB
Racket

#lang racket/base
11 years ago
(provide (except-out (all-from-out racket/base) #%module-begin)
11 years ago
(rename-out [new-module-begin #%module-begin]))
11 years ago
(define-syntax-rule (new-module-begin body-exprs ...)
(#%module-begin
11 years ago
(module inner pollen/lang/doclang_raw
;; first three lines are positional arguments for doclang_raw
main-raw ; id of export
(λ(x) x) ; post-process function
() ; prepended exprs
(require pollen/lang/lang-helper)
11 years ago
(require-project-require-files)
(provide (all-defined-out))
;; Change behavior of undefined identifiers
(require pollen/top)
(provide (all-from-out pollen/top))
;; Build 'inner-here-path and 'inner-here
(define inner-here-path (get-here-path))
(require (only-in pollen/world PROJECT_ROOT))
11 years ago
(require (only-in racket/path find-relative-path))
(require (only-in pollen/file-tools ->output-path))
(define inner-here (path->string (->output-path (find-relative-path PROJECT_ROOT inner-here-path))))
11 years ago
11 years ago
body-exprs ...)
11 years ago
(require 'inner)
;; Split out the metas. We want to catch user-defined metas too.
;; First, append "here-path" and "here" as metas.
;; Because they might have been overridden by custom metas in the source.
(require txexpr)
(define one-with-everything `(placeholder-root
(meta "here-path" ,inner-here-path)
(meta "here" ,inner-here)
,@(cdr main-raw))) ;; cdr strips initial linebreak
11 years ago
(define is-meta-element? (λ(x) (and (txexpr? x) (equal? 'meta (get-tag x)))))
(define-values (main-without-metas meta-elements)
11 years ago
(splitf-txexpr one-with-everything is-meta-element?))
(define meta-element->assoc (λ(x) (cons (cadr x) (caddr x))))
(define metas (make-hash (map meta-element->assoc meta-elements)))
;; set up the 'main export
(define here-ext (car (regexp-match #px"\\w+$" inner-here-path)))
(define wants-decoder? (member here-ext (list "pd" "ptree")))
(define main (apply (if wants-decoder?
;; 'root is the hook for the decoder function.
;; If it's not a defined identifier, it just hits #%top and becomes `(root ,@body ...)
root
;; for textual (preprocessor-style) output. Converts x-expressions to HTML.
(λ xs (apply string-append (map (dynamic-require 'xml 'xexpr->string) xs))))
(cdr main-without-metas))) ;; cdr strips placeholder-root tag
;; derive 'here & 'here-path from the hash (because they might have been overridden in the source)
(define here (hash-ref metas "here"))
(define here-path (hash-ref metas "here-path"))
(provide metas main here here-path
;; hide the exports that were only for internal use.
(except-out (all-from-out 'inner) inner-here inner-here-path main-raw #%top))
;; for output in DrRacket
11 years ago
(module+ main
(if wants-decoder?
(print main)
(display main)))))