make `codeblock' work without a #lang line

but revise docs to clarify that a #lang line is really expected
 Closes PR 11385
This commit is contained in:
Matthew Flatt 2011-01-16 19:11:13 -07:00
parent 21eee45a48
commit c8e3d45dae
2 changed files with 33 additions and 17 deletions

View File

@ -62,7 +62,13 @@
(if context
(replace-context context stx)
stx)))
(read-syntax 'prog (open-input-bytes bstr))))]
(let ([p (open-input-bytes bstr)])
(let loop ()
(let ([v (read-syntax 'prog p)])
(cond
[expand v]
[(eof-object? v) null]
[else (datum->syntax #f (cons v (loop)) v v)]))))))]
[ids (let loop ([e e])
(cond
[(and (identifier? e)
@ -113,7 +119,8 @@
(apply append
(map loop (syntax->list #'(form ...))))]
[else null]))]
[language (if (regexp-match? #rx"^#lang " bstr)
[has-hash-lang? (regexp-match? #rx"^#lang " bstr)]
[language (if has-hash-lang?
(let ([m (regexp-match #rx"^#lang ([-a-zA-Z/._+]+)" bstr)])
(if m
(link-mod
@ -128,8 +135,10 @@
mods
language
(filter (lambda (x) (not (eq? (car x) 'symbol)))
;; Drop #lang entry:
(cdr tokens)))
(if has-hash-lang?
;; Drop #lang entry:
(cdr tokens)
tokens)))
(lambda (a b)
(or (< (cadr a) (cadr b))
(and (= (cadr a) (cadr b))

View File

@ -31,19 +31,30 @@ includes a @racket[latex-defaults] @tech{style property}.
@section[#:tag "scribble:manual:code"]{Typesetting Code}
@defform/subs[(codeblock option ... str-expr ...+)
([option (code:line #:indent indent-expr)
([option (code:line #:keep-lang-line? keep-expr)
(code:line #:indent indent-expr)
(code:line #:expand expand-expr)
(code:line #:context context-expr)
(code:line #:keep-lang-line? keep-expr)])
#:contracts ([indent-expr exact-nonnegative-integer?]
(code:line #:context context-expr)])
#:contracts ([keep-expr any/c]
[indent-expr exact-nonnegative-integer?]
[expand-expr (or/c #f (syntax-object? . -> . syntax-object?))]
[context-expr syntax-object?]
[keep-expr any/c])]{
[context-expr syntax-object?])]{
Parses the code formed by the strings produced by the
@racket[str-expr]s as a Racket module and produces a @tech{block} that
typesets the code. The code is indented by the amount specified by
@racket[indent-expr], which defaults to @racket[2].
@racket[str-expr]s as a Racket module (roughly) and produces a
@tech{block} that typesets the code. The @racket[str-expr]s should
normally start with @hash-lang[] to determine the reader syntax for
the module, but the resulting ``module'' need not expand or
compile---except as needed by @racket[expand-expr]. If
@racket[expand-expr] is omitted or produces false, then the input
formed by @racket[str-expr] is read until an end-of-file is
encountered, otherwise a single form is read from the input.
When @racket[keep-expr] produces a true value (the default), the first
line in the input (which is typically @hash-lang[]) is preserved in
the typeset output, otherwise the first line is dropped. The typeset
code is indented by the amount specified by @racket[indent-expr],
which defaults to @racket[2].
When @racket[expand-expr] produces @racket[#f] (which is the default),
identifiers in the typeset code are colored and linked based on
@ -55,10 +66,6 @@ When @racket[expand-expr] produces a procedure, it is used to
macro-expand the parsed program, and syntax coloring is based on the
parsed program.
When @racket[keep-lang-line?-expr] produces a true value (the
default), the @hash-lang[] line in the input is preserved in the
typeset output, otherwise the first line is dropped.
For example,
@codeblock[#:keep-lang-line? #f]|<|{