fix converter

pull/178/head
Matthew Butterick 7 years ago
parent fcc7c3cb3e
commit 09df7982a7

@ -1 +1 @@
1525128463 1527899366

@ -58,18 +58,18 @@ You are kitty}")
(define/contract+provide (html->xexpr html-string) (define/contract+provide (html->xexpr html-string)
(string? . -> . xexpr?) (string? . -> . xexpr?)
(use-html-spec #f) (use-html-spec #f)
(define xexpr-results (define xexpr-results
(let loop ([elem (read-html-as-xml (open-input-string html-string))]) ; loop result will be a list with whitespace elements, so strip those out
(match elem (filter-not (conjoin string? whitespace?)
[(struct pcdata (start stop string)) string] (let loop ([elem (read-html-as-xml (open-input-string html-string))])
[(struct entity (start stop entity)) entity] (match elem
[(struct attribute (start stop key value)) (list key value)] [(struct pcdata (start stop string)) string]
[(struct element (start stop name attributes content)) `(,name ,@(if (empty? attributes) empty (list (map loop attributes))) ,@(map loop content))] [(struct entity (start stop entity)) entity]
[(list elements ...) (map loop elements)] [(struct attribute (start stop key value)) (list key value)]
[else (format "unknown item: ~a" elem)]))) [(struct element (start stop name attributes content)) `(,name ,@(if (empty? attributes) empty (list (map loop attributes))) ,@(map loop content))]
[(list elements ...) (map loop elements)]
; xexpr-results will be a list with whitespace elements, so strip those out [else (format "unknown item: ~a" elem)]))))
(car (filter-not (conjoin string? whitespace?) xexpr-results))) (if (pair? xexpr-results) (car xexpr-results) ""))
(module+ test (module+ test
(define (hx-identity xexpr) (define (hx-identity xexpr)
@ -77,7 +77,8 @@ You are kitty}")
(check-true (hx-identity '(p "You are puppy"))) (check-true (hx-identity '(p "You are puppy")))
(check-true (hx-identity '(p ((class "foo")) "You are puppy"))) (check-true (hx-identity '(p ((class "foo")) "You are puppy")))
(check-true (hx-identity '(p))) (check-true (hx-identity '(p)))
(check-true (hx-identity '(p ((class "foo")) "You are " (em "so") " puppy")))) (check-true (hx-identity '(p ((class "foo")) "You are " (em "so") " puppy")))
(check-equal? (html->xexpr "\n") ""))
(define/contract+provide (html->pollen html-string #:white-p? [white-p? #f]) (define/contract+provide (html->pollen html-string #:white-p? [white-p? #f])

Loading…
Cancel
Save