improvements to ->html

pull/15/merge
Matthew Butterick 11 years ago
parent bab5a427ed
commit 02133ed006

File diff suppressed because one or more lines are too long

@ -28,7 +28,7 @@ Convert @racket[_xexpr] to an HTML string. Similar to @racket[xexpr->string], bu
(->html tx)
]
The optional keyword arguments @racket[_html-tag] and @racket[_html-attrs] let you replace the tag and attributes in the generated HTML.
The optional keyword arguments @racket[_html-tag] and @racket[_html-attrs] let you set the outer tag and attributes for the generated HTML. If @racket[_xexpr] already has an outer tag or attributes, they will be replaced.
@examples[#:eval my-eval
(define tx '(root ((id "huff")) "Bunk beds"))
@ -38,12 +38,29 @@ The optional keyword arguments @racket[_html-tag] and @racket[_html-attrs] let y
(->html tx #:tag 'div #:attrs '((id "doback")))
]
The @racket[_splice-html?] option will strip the outer tag from the generated HTML.
Whereas if @racket[_xexpr] has no tag or attributes, they will be added. If you supply attributes without a tag, you'll get an error.
@examples[#:eval my-eval
(define tx '(root (p "Orange marmalade")))
(define x "Drum kit")
(->html x)
(->html x #:tag 'div)
(->html x #:tag 'div #:attrs '((id "doback")))
(->html x #:attrs '((id "doback")))
]
If the generated HTML has an outer tag, the @racket[_splice-html?] option will strip it off. Otherwise this option has no effect.
@examples[#:eval my-eval
(define tx '(root (p "Chicken nuggets")))
(->html tx)
(->html tx #:splice #t)
(define x "Fancy sauce")
(->html x)
(code:comment @#,t{This next one won't do anything})
(->html x #:splice #t)
(code:comment @#,t{Adds the outer tag, but then #:splice removes it})
(->html x #:tag 'div #:attrs '((id "doback")) #:splice #t)
]

@ -77,13 +77,20 @@
(define+provide/contract (->html x #:tag [tag #f] #:attrs [attrs #f] #:splice [splice? #f])
((xexpr?) (#:tag (or/c #f txexpr-tag?) #:attrs (or/c #f txexpr-attrs?) #:splice boolean?) . ->* . string?)
(when (and (not (txexpr? x)) attrs (not tag))
(error '->html "can't use attribute list '~a without a #:tag argument" attrs))
(if (or tag (txexpr? x))
(let ()
(define html-tag (or tag (get-tag x)))
(define html-attrs (or attrs (get-attrs x)))
(define html (xexpr->html (make-txexpr html-tag html-attrs (get-elements x))))
(define html-attrs (or attrs (and (txexpr? x) (get-attrs x)) null))
(define html-elements (or (and (txexpr? x) (get-elements x)) (list x)))
(define html (xexpr->html (make-txexpr html-tag html-attrs html-elements)))
(if splice?
(trim-outer-tag html)
html))
(xexpr->html x)))
(provide when/block)
(define-syntax (when/block stx)

@ -2,11 +2,20 @@
(require rackunit)
(require "../template.rkt")
(define x '(root (p "hello")))
(check-equal? (->html x) "<root><p>hello</p></root>")
(check-equal? (->html #:tag 'brennan x) "<brennan><p>hello</p></brennan>")
(check-equal? (->html #:attrs '((id "dale")) x) "<root id=\"dale\"><p>hello</p></root>")
(check-equal? (->html #:splice #t x) "<p>hello</p>")
(check-equal? (->html #:tag 'brennan #:attrs '((id "dale")) x) "<brennan id=\"dale\"><p>hello</p></brennan>")
(check-equal? (->html #:tag 'brennan #:attrs '((id "dale")) #:splice #t x) "<p>hello</p>")
(define tx '(root (p "hello")))
(check-equal? (->html tx) "<root><p>hello</p></root>")
(check-equal? (->html #:tag 'brennan tx) "<brennan><p>hello</p></brennan>")
(check-equal? (->html #:attrs '((id "dale")) tx) "<root id=\"dale\"><p>hello</p></root>")
(check-equal? (->html #:splice #t tx) "<p>hello</p>")
(check-equal? (->html #:tag 'brennan #:attrs '((id "dale")) tx) "<brennan id=\"dale\"><p>hello</p></brennan>")
(check-equal? (->html #:tag 'brennan #:attrs '((id "dale")) #:splice #t tx) "<p>hello</p>")
(define x "hello")
(check-equal? (->html x) "hello")
(check-equal? (->html #:tag 'brennan x) "<brennan>hello</brennan>")
(check-exn exn:fail? (λ() (->html #:attrs '((id "dale")) x) "hello")) ;; won't work without tag
(check-equal? (->html #:splice #t x) "hello")
(check-equal? (->html #:tag 'brennan #:attrs '((id "dale")) x) "<brennan id=\"dale\">hello</brennan>")
(check-equal? (->html #:tag 'brennan #:attrs '((id "dale")) #:splice #t x) "hello")

Loading…
Cancel
Save