I thought a function that would be the complement to build-tag was in order.
So here's my attempt.

Comments appreciated.
Suggestions on improvements welcomed.
Now where did that bed get to....

Oh, anyone thought up a dom (or just structured) approach to HTML in rebol
yet?

Brett.

REBOL [
    Author: "Brett Handley"
    Title: "Parse-tag"
    Date: 30-June-2000
    Note: {Feeds off xml-language for some code.
           I didn't know if there was an easier way to do this - and without
           clutter of a object cloning. Probably better just copying and
pasting but I thought I'd highlight the commonalities.}
    Note2: {I'd be more happy if this sort of (or better) functionality was
part of the package. :) }
]

unset [tag-language-defn tag-language]

tag-language-defn: [
    unquoted-data: complement charset reduce [ " " tab ]

    tag-rule: [(tag-name: none) copy tag-name name (append result to-word
tag-name) any [sp attribute] sp?]
    attribute: [copy attr-name name (append result to-word attr-name) opt
[eq attr-value (append result attr-data)]]

    parse-tag: func[ a-tag [tag!] ] [
       result: make block! 1
       parse/all/case a-tag tag-rule
       result
    ]
]

foreach field [
    space name-first name-chars data-chars qt1 qt2
    data-chars-qt1 data-chars-qt2 name sp sp? eq attr-value
][
    append tag-language-defn reduce [to-set-word :field copy get in
xml-language :field ]
]

; Add rule for handling unquoted attribute data
append tag-language-defn [
    append attr-value '| append/only attr-value [ copy attr-data some
unquoted-data ]
]

tag-language: make object! tag-language-defn

parse-tag: func[ t [tag!] ] [ tag-language/parse-tag t ]


; Examples
;
;    Use it like so...
;    parse-tag <a href="http://....">
;
;    Grab an attribute value
;    select parse-tag <a href=http://localhost/ id="a1"> 'id
;
;    or maybe...
;    html: load/markup http://www.rebol.com
;    foreach e html [
;        either tag? e [
;            if select parse-tag e 'src [print select parse-tag e 'alt]
;        ][ false ]
;    ]
;
;    This one might be handy for tidying up those nasty unquoted values...
;    build-tag parse-tag <input type=radio name='theAnimal' value="cat"
CHECKED>
;
;    parse-tag </a>    ; returns empty block - don't know if this is the
best or not.


Reply via email to