|
|
@ -11,16 +11,21 @@
|
|
|
|
(provide (all-defined-out))
|
|
|
|
(provide (all-defined-out))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; split list into list of sublists using test
|
|
|
|
;; split list into list of sublists using test-proc
|
|
|
|
;; todo: contract & unit tests
|
|
|
|
(define/contract (splitf-at* xs test-proc)
|
|
|
|
(define (splitf-at* pieces test)
|
|
|
|
(list? procedure? . -> . (λ(i) (match i [(list (? list?) ...) #t][else #f])))
|
|
|
|
(define (splitf-at*-inner pieces [acc '()]) ; use acc for tail recursion
|
|
|
|
(define (&splitf-at* pieces [acc '()]) ; use acc for tail recursion
|
|
|
|
(if (empty? pieces)
|
|
|
|
(if (empty? pieces)
|
|
|
|
acc
|
|
|
|
acc
|
|
|
|
(let-values ([(item rest)
|
|
|
|
(let-values ([(item rest)
|
|
|
|
(splitf-at (dropf pieces test) (compose1 not test))])
|
|
|
|
(splitf-at (dropf pieces test-proc) (compose1 not test-proc))])
|
|
|
|
(splitf-at*-inner rest `(,@acc ,item)))))
|
|
|
|
(&splitf-at* rest `(,@acc ,item)))))
|
|
|
|
(splitf-at*-inner (trim pieces test)))
|
|
|
|
(&splitf-at* (trim xs test-proc)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(module+ test
|
|
|
|
|
|
|
|
(check-equal? (splitf-at* '(1 2 3 4 5 6) even?) '((1)(3)(5)))
|
|
|
|
|
|
|
|
(check-equal? (splitf-at* '("foo" " " "bar" "\n" "\n" "ino") whitespace?) '(("foo")("bar")("ino"))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; Find adjacent newline characters in a list and merge them into one item
|
|
|
|
;; Find adjacent newline characters in a list and merge them into one item
|
|
|
@ -64,20 +69,17 @@
|
|
|
|
;; decode triple newlines to list items
|
|
|
|
;; decode triple newlines to list items
|
|
|
|
|
|
|
|
|
|
|
|
;; is the tagged-xexpr a block element (as opposed to inline)
|
|
|
|
;; is the tagged-xexpr a block element (as opposed to inline)
|
|
|
|
|
|
|
|
;; tags are inline unless they're registered as block tags.
|
|
|
|
(define/contract (block-xexpr? x)
|
|
|
|
(define/contract (block-xexpr? x)
|
|
|
|
(any/c . -> . boolean?)
|
|
|
|
(any/c . -> . boolean?)
|
|
|
|
;; this is a change in behavior since first pollen
|
|
|
|
|
|
|
|
;; blocks are only the ones on the html block tag list.
|
|
|
|
|
|
|
|
;; todo: make sure this is what I want.
|
|
|
|
|
|
|
|
;; this is, however, more consistent with browser behavior
|
|
|
|
|
|
|
|
;; (browsers assume that tags are inline by default)
|
|
|
|
|
|
|
|
((tagged-xexpr? x) . and . (->boolean ((tagged-xexpr-tag x) . in . block-tags))))
|
|
|
|
((tagged-xexpr? x) . and . (->boolean ((tagged-xexpr-tag x) . in . block-tags))))
|
|
|
|
|
|
|
|
|
|
|
|
(module+ test
|
|
|
|
(module+ test
|
|
|
|
(check-true (block-xexpr? '(p "foo")))
|
|
|
|
(check-true (block-xexpr? '(p "foo")))
|
|
|
|
(check-true (block-xexpr? '(div "foo")))
|
|
|
|
(check-true (block-xexpr? '(div "foo")))
|
|
|
|
(check-false (block-xexpr? '(em "foo")))
|
|
|
|
(check-false (block-xexpr? '(em "foo")))
|
|
|
|
(check-false (block-xexpr? '(barfoo "foo"))))
|
|
|
|
(check-false (block-xexpr? '(barfoo "foo")))
|
|
|
|
|
|
|
|
(check-true (begin (register-block-tag 'barfoo) (block-xexpr? '(barfoo "foo")))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; convert numbers to strings
|
|
|
|
;; convert numbers to strings
|
|
|
@ -121,11 +123,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
;; test for well-formed meta
|
|
|
|
;; test for well-formed meta
|
|
|
|
(define/contract (meta-xexpr? x)
|
|
|
|
(define/contract (meta-xexpr? x)
|
|
|
|
(any/c . -> . (λ(i) (or (boolean? i) (list? i))))
|
|
|
|
(any/c . -> . boolean?)
|
|
|
|
(match x
|
|
|
|
(match x
|
|
|
|
[`(meta ,(? string? key) ,(? string? value)) (list key value)]
|
|
|
|
[`(meta ,(? string? key) ,(? string? value)) #t]
|
|
|
|
[else #f]))
|
|
|
|
[else #f]))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(module+ test
|
|
|
|
|
|
|
|
(check-true (meta-xexpr? '(meta "key" "value")))
|
|
|
|
|
|
|
|
(check-false (meta-xexpr? '(meta "key" "value" "foo")))
|
|
|
|
|
|
|
|
(check-false (meta-xexpr? '(meta))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; function to strip metas (or any tag)
|
|
|
|
;; function to strip metas (or any tag)
|
|
|
@ -140,7 +146,7 @@
|
|
|
|
(set! matches (cons x matches))
|
|
|
|
(set! matches (cons x matches))
|
|
|
|
empty)]
|
|
|
|
empty)]
|
|
|
|
[(tagged-xexpr? x) (let-values([(tag attr body) (break-tagged-xexpr x)])
|
|
|
|
[(tagged-xexpr? x) (let-values([(tag attr body) (break-tagged-xexpr x)])
|
|
|
|
(make-tagged-xexpr tag attr (extract-tag body)))]
|
|
|
|
(make-tagged-xexpr tag attr (extract-tag body)))]
|
|
|
|
[(xexpr-elements? x) (filter-not empty? (map extract-tag x))]
|
|
|
|
[(xexpr-elements? x) (filter-not empty? (map extract-tag x))]
|
|
|
|
[else x]))
|
|
|
|
[else x]))
|
|
|
|
(values (extract-tag nx) (reverse matches)))
|
|
|
|
(values (extract-tag nx) (reverse matches)))
|
|
|
@ -165,13 +171,13 @@
|
|
|
|
#:meta-proc [meta-proc (λ(x)x)])
|
|
|
|
#:meta-proc [meta-proc (λ(x)x)])
|
|
|
|
;; use xexpr/c for contract because it gives better error messages
|
|
|
|
;; use xexpr/c for contract because it gives better error messages
|
|
|
|
((xexpr/c) (#:exclude-xexpr-tags (λ(i) (or (symbol? i) (list? i)))
|
|
|
|
((xexpr/c) (#:exclude-xexpr-tags (λ(i) (or (symbol? i) (list? i)))
|
|
|
|
#:xexpr-tag-proc procedure?
|
|
|
|
#:xexpr-tag-proc procedure?
|
|
|
|
#:xexpr-attr-proc procedure?
|
|
|
|
#:xexpr-attr-proc procedure?
|
|
|
|
#:xexpr-elements-proc procedure?
|
|
|
|
#:xexpr-elements-proc procedure?
|
|
|
|
#:block-xexpr-proc procedure?
|
|
|
|
#:block-xexpr-proc procedure?
|
|
|
|
#:inline-xexpr-proc procedure?
|
|
|
|
#:inline-xexpr-proc procedure?
|
|
|
|
#:string-proc procedure?
|
|
|
|
#:string-proc procedure?
|
|
|
|
#:meta-proc procedure?)
|
|
|
|
#:meta-proc procedure?)
|
|
|
|
. ->* . tagged-xexpr?)
|
|
|
|
. ->* . tagged-xexpr?)
|
|
|
|
(when (not (tagged-xexpr? nx))
|
|
|
|
(when (not (tagged-xexpr? nx))
|
|
|
|
(error (format "decode: ~v not a full tagged-xexpr" nx)))
|
|
|
|
(error (format "decode: ~v not a full tagged-xexpr" nx)))
|
|
|
@ -181,13 +187,13 @@
|
|
|
|
(define (&decode x)
|
|
|
|
(define (&decode x)
|
|
|
|
(cond
|
|
|
|
(cond
|
|
|
|
[(tagged-xexpr? x) (let-values([(tag attr elements) (break-tagged-xexpr x)])
|
|
|
|
[(tagged-xexpr? x) (let-values([(tag attr elements) (break-tagged-xexpr x)])
|
|
|
|
(if (tag . in . (->list excluded-xexpr-tags))
|
|
|
|
(if (tag . in . (->list excluded-xexpr-tags))
|
|
|
|
x
|
|
|
|
x
|
|
|
|
(let ([decoded-xexpr
|
|
|
|
(let ([decoded-xexpr
|
|
|
|
(apply make-tagged-xexpr (map &decode (list tag attr elements)))])
|
|
|
|
(apply make-tagged-xexpr (map &decode (list tag attr elements)))])
|
|
|
|
((if (block-xexpr? decoded-xexpr)
|
|
|
|
((if (block-xexpr? decoded-xexpr)
|
|
|
|
block-xexpr-proc
|
|
|
|
block-xexpr-proc
|
|
|
|
inline-xexpr-proc) decoded-xexpr))))]
|
|
|
|
inline-xexpr-proc) decoded-xexpr))))]
|
|
|
|
[(xexpr-tag? x) (xexpr-tag-proc x)]
|
|
|
|
[(xexpr-tag? x) (xexpr-tag-proc x)]
|
|
|
|
[(xexpr-attr? x) (xexpr-attr-proc x)]
|
|
|
|
[(xexpr-attr? x) (xexpr-attr-proc x)]
|
|
|
|
[(xexpr-elements? x) (map &decode (xexpr-elements-proc x))]
|
|
|
|
[(xexpr-elements? x) (map &decode (xexpr-elements-proc x))]
|
|
|
|