simplify splicing; add to docs

pull/110/head
Matthew Butterick 8 years ago
parent 90617e2bfb
commit 3f69df3ff5

@ -6,7 +6,6 @@
"cache.rkt"
"pagetree.rkt"
"tag.rkt"
"private/to-string.rkt"
"private/splice.rkt")
(define is-meta-value? hash?)
@ -15,7 +14,7 @@
(define not-false? identity)
(define+provide define-meta identity) ;; stub so it will be picked up for docs
(define+provide @ (make-default-tag-function '@))
(define+provide/contract (select* key value-source)
(coerce/symbol? (or/c is-meta-value? is-doc-value? pagenode? pathish?) . -> . (or/c #f txexpr-elements?))
@ -104,6 +103,8 @@
(cached-doc (convert+validate-path pagenode-or-path 'get-doc)))
(define+provide @ (make-default-tag-function '@))
(provide when/splice)
(define-syntax (when/splice stx)
(syntax-case stx ()
@ -111,21 +112,10 @@
(with-syntax ([SPLICING-TAG (datum->syntax stx (world:current-splicing-tag))])
#'(if COND
(with-handlers ([exn:fail? (λ(exn) (error (format "within when/splice, ~a" (exn-message exn))))])
(list 'SPLICING-TAG BODY ...))
""))]))
(provide when/splice/text)
(define-syntax (when/splice/text stx)
(syntax-case stx ()
[(_ COND BODY ...)
(with-syntax ([SPLICING-TAG (datum->syntax stx (world:current-splicing-tag))])
#'(if COND
(with-handlers ([exn:fail? (λ(exn) (error (format "within when/splice, ~a" (exn-message exn))))])
(map to-string (list BODY ...)))
(SPLICING-TAG BODY ...))
""))]))
(provide when/block) ; bw compat
(define-syntax-rule (when/block cond body ...)
(when/splice/text cond body ...))
(when/splice cond body ...))

@ -176,19 +176,20 @@
(define expr-to-eval
`(begin
(require (for-syntax racket/base))
(require pollen/private/include-template pollen/cache pollen/private/debug pollen/pagetree)
(require pollen/private/include-template pollen/cache pollen/private/debug pollen/pagetree pollen/core)
,(require-directory-require-files source-path)
(parameterize ([current-pagetree (make-project-pagetree ,(world:current-project-root))])
(let ([,(world:current-main-export) (cached-doc ,(path->string source-path))]
[,(world:current-meta-export) (cached-metas ,(path->string source-path))])
(local-require pollen/template pollen/top pollen/core)
(let ([,(world:current-main-export source-path) (cached-doc ,(path->string source-path))]
[,(world:current-meta-export source-path) (cached-metas ,(path->string source-path))]
[,(world:current-splicing-tag source-path) (λ xs xs)]) ; splice behavior is different in textual context
(local-require pollen/template pollen/top)
(define here (path->pagenode
(or (select-from-metas ',(world:current-here-path-key) ,(world:current-meta-export)) 'unknown)))
(or (select-from-metas ',(world:current-here-path-key source-path) ,(world:current-meta-export source-path)) 'unknown)))
(cond
[(bytes? ,(world:current-main-export)) ,(world:current-main-export)] ; if main export is binary, just pass it through
[(bytes? ,(world:current-main-export source-path)) ,(world:current-main-export source-path)] ; if main export is binary, just pass it through
[else
;; `include-template` is the slowest part of the operation (the eval itself is cheap)
(include-template #:command-char ,(world:current-command-char) (file ,(->string (find-relative-path source-dir template-path))))])))))
(include-template #:command-char ,(world:current-command-char source-path) (file ,(->string (find-relative-path source-dir template-path))))])))))
(time (parameterize ([current-directory (->complete-path source-dir)]) ; because include-template wants to work relative to source location
(render-through-eval expr-to-eval))))
@ -213,7 +214,7 @@
(with-handlers ([exn:fail:contract? (λ _ #f)]) ; in case source-path doesn't work with cached-require
(parameterize ([current-directory (world:current-project-root)])
(let* ([source-metas (cached-metas source-path)]
[template-name-or-names (select-from-metas (world:current-template-meta-key) source-metas)] ; #f or atom or list
[template-name-or-names (select-from-metas (world:current-template-meta-key source-path) source-metas)] ; #f or atom or list
[template-name (cond
[(list? template-name-or-names)
(define result
@ -224,13 +225,13 @@
(define (get-default-template)
(and output-path-ext
(let ([default-template-filename (add-ext (world:current-default-template-prefix) output-path-ext)])
(let ([default-template-filename (add-ext (world:current-default-template-prefix source-path) output-path-ext)])
(find-upward-from source-path default-template-filename file-exists-or-has-source?))))
(define (get-fallback-template)
(and output-path-ext
(build-path (world:current-server-extras-path)
(add-ext (world:current-fallback-template-prefix) output-path-ext))))
(add-ext (world:current-fallback-template-prefix source-path) output-path-ext))))
(or (file-exists-or-has-source? (get-template-from-metas))
(file-exists-or-has-source? (get-default-template))

File diff suppressed because one or more lines are too long

@ -22,6 +22,17 @@ You can retrieve a meta value — even in the same document where you define it
For an introduction to metas, see @secref["Inserting_metas"].
@defform[(\@ arg ...)]
Splicing tag: signals that a list should be merged into its containing expression. You can use something other than @racket[\@] by overriding @racket[world:current-splicing-tag].
@examples[#:eval my-eval
(module splicer pollen/markup
'(div "one" (\@ "two" "three") "four"))
(require 'splicer)
doc
]
@defform[(when/splice condition pollen-args)]
If @racket[_condition] is true, put the @racket[_pollen-args] into the document. Within a template file, usually invoked like so:

File diff suppressed because one or more lines are too long

@ -25,7 +25,7 @@ A parameter that holds the root directory of the current project (e.g., the dire
@defparam[world:current-server-extras-path dir path?]{
A parameter that reports the path to the directory of support files for the project server. Initialized to @racket[#f], but set to a proper value when the server runs.}
@defparam[world:current-poly-target target symbol??]{
@defparam[world:current-poly-target target symbol?]{
A parameter that reports the current rendering target for @racket[poly] source files. Initialized to @racket['html].}
@ -78,8 +78,6 @@ Determines the default HTTP port for the project server. Initialized to @racket[
@defoverridable[meta-tag-name symbol?]{Name of the tag used to mark metas within Pollen source.}
@defoverridable[server-extras-dir string?]{Name of directory where server support files live. Initialized to @tt{server-extras}.}
@defoverridable[extension-escape-char char?]{Character for escaping output-file extensions within source-file names. Initialized to @racket[#\_].}

Loading…
Cancel
Save