Adding documentation and tests for xml/path

This commit is contained in:
Jay McCarthy 2011-07-24 16:45:04 -04:00
parent 75631c6f2a
commit 6e59cbf1d7
3 changed files with 65 additions and 10 deletions

View File

@ -2,7 +2,32 @@
(require xml/path (require xml/path
tests/eli-tester) tests/eli-tester)
(test (define some-page
(se-path*/list '(p) '(html (body (p "Hey") (p "Bar")))) => (list "Hey" "Bar") '(html (body (p ([class "awesome"]) "Hey") (p "Bar"))))
(se-path* '(p) '(html (body (p "Hey")))) => "Hey" (test (se-path*/list '(p) some-page)
(se-path* '(p #:bar) '(html (body (p ([bar "Zog"]) "Hey")))) => "Zog") => '("Hey" "Bar")
(se-path* '(p) some-page)
=> "Hey"
(se-path* '(p #:class) some-page)
=> "awesome"
(se-path*/list '(body) some-page)
=> '((p ((class "awesome")) "Hey") (p "Bar"))
(se-path*/list '() '(p ((class "awesome")) "Hey"))
=> '((p ((class "awesome")) "Hey")
"Hey")
(se-path*/list '() some-page)
=> '((html (body (p ((class "awesome")) "Hey") (p "Bar")))
(body (p ((class "awesome")) "Hey") (p "Bar"))
(p ((class "awesome")) "Hey")
"Hey"
(p "Bar")
"Bar")
(se-path*/list '(p) '(html (body (p "Hey") (p "Bar"))))
=> (list "Hey" "Bar")
(se-path* '(p) '(html (body (p "Hey"))))
=> "Hey"
(se-path* '(p #:bar) '(html (body (p ([bar "Zog"]) "Hey"))))
=> "Zog")

View File

@ -33,7 +33,7 @@
[(list-rest (? symbol?) _) [(list-rest (? symbol?) _)
(match x (match x
[(list-rest (list (list (? symbol?) (? string?)) ...) rs) [(list-rest (list (list (? symbol?) (? string?)) ...) rs)
(se-path/tag-body p rs)] (append-map (curry se-path/xexpr p) rs)]
[(? list?) [(? list?)
(append-map (curry se-path/xexpr p) x)] (append-map (curry se-path/xexpr p) x)]
[_ [_
@ -51,10 +51,10 @@
(define (se-path*/list p x) (define (se-path*/list p x)
(append (se-path/xexpr p x) (append (se-path/xexpr p x)
(match x (match x
[(list-rest (list (cons (? symbol?) (? string?)) ...) rs) [(list (? symbol? tag) (list (list (? symbol?) (? string?)) ...) rs ...)
(se-path*/list p rs)] (append-map (curry se-path*/list p) rs)]
[(? list?) [(list (? symbol? tag) rs ...)
(append-map (curry se-path*/list p) x)] (append-map (curry se-path*/list p) rs)]
[_ [_
empty]))) empty])))
(define (se-path* p x) (define (se-path* p x)

View File

@ -475,6 +475,36 @@ looks like the following, if re-formatted by:
@section{Simple X-expression Path Queries} @section{Simple X-expression Path Queries}
@(require (for-label xml/path))
@defmodule[xml/path] @defmodule[xml/path]
XXX This library provides a simple path query library for X-expressions.
@defthing[se-path? contract?]{
A sequence of symbols followed by an optional keyword.
The prefix of symbols specifies a path of tags from the leaves with an implicit any sequence to the root. The final, optional keyword specifies an attribute.
}
@defproc[(se-path*/list [p se-path?] [xe xexpr?])
(listof any/c)]{
Returns a list of all values specified by the path @racket[p] in the X-expression @racket[xe].
}
@defproc[(se-path* [p se-path?] [xe xexpr?])
any/c]{
Returns the first answer from @racket[(se-path*/list p xe)].
}
@(define path-eval (make-base-eval))
@interaction-eval[#:eval path-eval (require xml/path)]
@examples[
#:eval path-eval
(define some-page
'(html (body (p ([class "awesome"]) "Hey") (p "Bar"))))
(se-path*/list '(p) some-page)
(se-path* '(p) some-page)
(se-path* '(p #:class) some-page)
(se-path*/list '(body) some-page)
(se-path*/list '() some-page)
]