From 5e384b1980de17ad7b8f0df303e391af80511dc6 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Mon, 1 Aug 2016 13:02:02 -0700 Subject: [PATCH] add `txexpr*` (closes #4) --- txexpr/base.rkt | 5 +++++ txexpr/scribblings/txexpr.scrbl | 18 ++++++++++++++++++ txexpr/tests.rkt | 6 ++++++ 3 files changed, 29 insertions(+) diff --git a/txexpr/base.rkt b/txexpr/base.rkt index 470fa30..cab6beb 100644 --- a/txexpr/base.rkt +++ b/txexpr/base.rkt @@ -164,6 +164,11 @@ [else ""])))) +(define+provide+safe (txexpr* tag [attrs null] . elements) + ((symbol?) (txexpr-attrs?) #:rest txexpr-elements? . ->* . txexpr?) + (txexpr tag attrs elements)) + + (define make-txexpr txexpr) ; for backward compatability (provide+safe make-txexpr) diff --git a/txexpr/scribblings/txexpr.scrbl b/txexpr/scribblings/txexpr.scrbl index bc0aac3..2e0f41d 100644 --- a/txexpr/scribblings/txexpr.scrbl +++ b/txexpr/scribblings/txexpr.scrbl @@ -228,6 +228,24 @@ Assemble a @racket[_txexpr] from its parts. If you don't have attributes, but yo (get-attrs tx) (get-elements tx)) ] +@defproc[ +(txexpr* +[tag txexpr-tag?] +[attrs txexpr-attrs? @empty] +[element txexpr-element?] ...) +txexpr?] +Like @racket[txexpr], but takes an indefinite number of @racket[_element] arguments, which together are treated as the list of elements for the resulting @racket[_txexpr]. A notational convenience. + +@examples[#:eval my-eval +(txexpr* 'div) +(txexpr* 'div '() "Hello" '(p "World")) +(txexpr* 'div '((id "top"))) +(txexpr* 'div '((id "top")) "Hello" '(p "World")) +(define tx '(div ((id "top")) "Hello" (p "World"))) +(apply txexpr* (get-tag tx) +(get-attrs tx) (get-elements tx)) +] + @deftogether[( @defproc[ diff --git a/txexpr/tests.rkt b/txexpr/tests.rkt index aee9cbf..8dc742c 100644 --- a/txexpr/tests.rkt +++ b/txexpr/tests.rkt @@ -89,6 +89,12 @@ (check-txexprs-equal? (txexpr 'p null '("foo" "bar")) '(p "foo" "bar")) (check-txexprs-equal? (txexpr 'p '((key "value")) (list "foo" "bar")) '(p ((key "value")) "foo" "bar")) + + (check-txexprs-equal? (txexpr* 'p) '(p)) + (check-txexprs-equal? (txexpr* 'p '((key "value"))) '(p ((key "value")))) + (check-txexprs-equal? (txexpr* 'p null "foo" "bar") '(p "foo" "bar")) + (check-txexprs-equal? (txexpr* 'p '((key "value")) "foo" "bar") + '(p ((key "value")) "foo" "bar")) (check-equal? (values->list (txexpr->values '(p))) (values->list (values 'p null null)))