Adding customization of command-char to web-server/template
This entails adding it to include/text and read-syntax-inside as well.
This commit is contained in:
parent
b3e2d35be9
commit
73232afd99
|
@ -645,5 +645,8 @@
|
|||
(provide read-inside read-syntax-inside)
|
||||
(define (read-inside [inp (current-input-port)])
|
||||
(syntax->datum ((make-default-at-reader/inside) default-src inp)))
|
||||
(define (read-syntax-inside [src default-src] [inp (current-input-port)])
|
||||
((make-default-at-reader/inside) src inp))
|
||||
(define (read-syntax-inside [src default-src] [inp (current-input-port)]
|
||||
#:command-char [command-char ch:command])
|
||||
(((readtable-cached
|
||||
(lambda (rt) (make-at-reader #:inside? #t #:command-char command-char #:readtable rt))))
|
||||
src inp))
|
||||
|
|
|
@ -172,15 +172,25 @@
|
|||
#'(process-begin/text begin/collect begin expr ...)]))
|
||||
|
||||
;; include for templates
|
||||
(require (for-syntax scheme/base (prefix-in scribble: "../reader.rkt"))
|
||||
(require (for-syntax scheme/base (prefix-in scribble: "../reader.rkt") syntax/parse)
|
||||
scheme/include)
|
||||
(define-syntax-rule (include/text path-spec)
|
||||
(define-syntax (include/text stx)
|
||||
(syntax-case stx ()
|
||||
[(_ path-spec)
|
||||
(syntax/loc stx
|
||||
(include/text #:command-char #f path-spec))]
|
||||
[(_ #:command-char command-char path-spec)
|
||||
(syntax/loc stx
|
||||
(begin/text
|
||||
(include-at/relative-to/reader path-spec path-spec path-spec
|
||||
(let ([xs #f])
|
||||
(include-at/relative-to/reader
|
||||
path-spec path-spec path-spec
|
||||
(let ([xs #f]
|
||||
[command-char-v command-char])
|
||||
(λ (src inp)
|
||||
(unless xs
|
||||
(set! xs (scribble:read-syntax-inside src inp))
|
||||
(set! xs (if command-char-v
|
||||
(scribble:read-syntax-inside #:command-char command-char-v src inp)
|
||||
(scribble:read-syntax-inside src inp)))
|
||||
(when (syntax? xs) (set! xs (or (syntax->list xs) (list xs)))))
|
||||
(if (null? xs)
|
||||
eof
|
||||
|
@ -190,4 +200,4 @@
|
|||
(let ([p (syntax-property x 'scribble)])
|
||||
(and (pair? p) (eq? (car p) 'newline))))
|
||||
eof ; throw away the last newline from the included file
|
||||
x))))))))
|
||||
x))))))))]))
|
||||
|
|
|
@ -188,10 +188,12 @@ for reading.
|
|||
|
||||
@defproc[(read-inside [in input-port? (current-input-port)]) any]{}
|
||||
@defproc[(read-syntax-inside [source-name any/c (object-name in)]
|
||||
[in input-port? (current-input-port)])
|
||||
[in input-port? (current-input-port)]
|
||||
[#:command-char command-char char? #\@])
|
||||
(or/c syntax? eof-object?)]{
|
||||
These @racketid[-inside] variants parse as if starting inside a
|
||||
@litchar["@{"]...@litchar["}"], and they return a (syntactic) list.
|
||||
The @racket[command-char] is used to customize the readtable.
|
||||
Useful for implementing languages that are textual by default (see
|
||||
@filepath{docreader.rkt} for example).
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#lang racket
|
||||
(require web-server/templates)
|
||||
(require web-server/templates
|
||||
rackunit)
|
||||
|
||||
(include-template "static.html")
|
||||
|
||||
|
@ -36,3 +37,6 @@
|
|||
(if-template #:monkeys 1
|
||||
#:monkey-limit 10
|
||||
#:monkey-minimum 2)
|
||||
|
||||
(check-equal? (include-template #:command-char #\$ "diff.html")
|
||||
"This is the number: 42\nThis is not the number: @(+ 2 40)")
|
||||
|
|
|
@ -359,12 +359,14 @@ the template to be unescaped, then create a @racket[cdata] structure:
|
|||
|
||||
@section{API Details}
|
||||
|
||||
@defform[(include-template path-spec)]{
|
||||
Compiles the template at @racket[path-spec] using the @at-reader-ref syntax within the enclosing lexical context. The @racket[path-spec] is the same format used by @racket[include].
|
||||
@defform*[((include-template path-spec)
|
||||
(include-template #:command-char command-char path-spec))]{
|
||||
Compiles the template at @racket[path-spec] using the @at-reader-ref syntax within the enclosing lexical context. The @racket[path-spec] is the same format used by @racket[include]. Use the @racket[command-char] keyword to customize the escape character.
|
||||
|
||||
Example:
|
||||
Examples:
|
||||
@racketblock[
|
||||
(include-template "static.html")
|
||||
(include-template #:command-char #\$ "dollar-static.html")
|
||||
]
|
||||
}
|
||||
|
||||
|
|
|
@ -1,19 +1,26 @@
|
|||
#lang racket/base
|
||||
(require xml
|
||||
scribble/text
|
||||
(for-syntax racket/base
|
||||
racket/list
|
||||
syntax/parse)
|
||||
racket/port)
|
||||
|
||||
(define-syntax include-template
|
||||
(syntax-rules ()
|
||||
[(_ p)
|
||||
(define-syntax (include-template stx)
|
||||
(syntax-parse stx
|
||||
[(_ (~optional (~seq #:command-char command-char:expr)) p:expr)
|
||||
(quasisyntax/loc stx
|
||||
(with-output-to-string
|
||||
(lambda ()
|
||||
(output (include/text p))))]))
|
||||
(output (include/text #,@(if (attribute command-char)
|
||||
(list #'#:command-char #'command-char)
|
||||
empty)
|
||||
p)))))]))
|
||||
|
||||
(define-syntax include-template/xexpr
|
||||
(syntax-rules ()
|
||||
[(_ p)
|
||||
(string->xexpr (include-template p))]))
|
||||
[(_ . p)
|
||||
(string->xexpr (include-template . p))]))
|
||||
|
||||
(define-syntax in
|
||||
(syntax-rules ()
|
||||
|
|
Loading…
Reference in New Issue
Block a user