Tags, anyone?
[1/6] from: ammon::rcslv::com at: 15-Apr-2002 16:18
Hi,
Has anyone delt with the Tag! datatype? I am trying to do some XML stuff, &
I figured that REBOL probably has some neat things for dealing with tags
seems on how they have a tag! datatype. I didn't see much in RCUG 2.3 on
tags so I thought I might ask someone who knows. ;-) So what's neat about
tags?
Thanks!!
Ammon
[2/6] from: brett:codeconscious at: 16-Apr-2002 10:07
Hi Ammon,
I know there is the BUILD-TAG function.
And that
>> series? <test>
== true
So you can do:
>> find <test href="blah"> 'href
== <href="blah">
Also note that because tag is a series it is easy to get caught with
rejoin when forming a string from tags:
>> rejoin [<test> "color"]
== <testcolor>
So my habit is to use join where the first argument dictates the
datatype of my result:
>> join {} [<test> "color" </test>]
== "<test>color</test>"
You can load/markup...
>> load/markup {<test>some text </test>}
== [<test> "some text " </test>]
...and use tag in parse:
parse load/markup {<test>some text </test>} [
tag! string! tag!
]
Ages ago I wrote some stuff to play with markup and change tags
into blocks using load/markup and the fact that tags are series:
http://www.codeconscious.com/rebsite/rebol-library/tag-tool.r
http://www.codeconscious.com/rebsite/rebol-library/markup-tools.r
Tag-tool.r gives the import-tag function:
>> import-tag <body bgcolor=white>
== [body bgcolor "white"]
Which can be used as input to build-tag:
>> build-tag [body bgcolor "white"]
== <body bgcolor="white">
markup-tools.r gives the load-markup function which produce a "flat"
representation of the markup where tags are made into blocks.
>> load-markup {<text>some <b>text</b></text>}
== [[text] "some " [b] "text" [/b] [/text]]
There is also form-markup which converts this representation back into a
string:
>> form-markup [ [p] "paragraph 1" [p] "paragraph2" ]
== "<p>paragraph 1<p>paragraph2"
I did this to give a basic markup manipulation facility:
m: load-markup {<body bgcolor="white">some <b>text</b></body>}
change next find first m 'bgcolor "blue"
form-markup m
== {<body bgcolor="blue">some <b>text</b></body>}
For XML stuff there is Gavin's great work:
http://www3.sympatico.ca/gavin.mckenzie/
Regards,
Brett.
[3/6] from: chalz:earthlink at: 15-Apr-2002 23:30
> Also note that because tag is a series it is easy to get caught with
> rejoin when forming a string from tags:
>
> >> rejoin [<test> "color"]
> == <testcolor>
Actually, when you think about it for a second, that's very obvious and
logical behavior. (Works same with join.) It's taking the second argument and
basically adding it to the first, transmuting it to a matching datatype. So if
you did:
>> join "color" <test>
It casts the second arg into the type of the first - so <test> becomes
<test>
and they're joined together.
> So my habit is to use join where the first argument dictates the
> datatype of my result:
>
> >> join {} [<test> "color" </test>]
> == "<test>color</test>"
There's always form:
>> form [<tag> "color" </tag>]
== "<tag> color </tag>"
Of course, since this is REBOL, there are at least 5 simple ways to do it,
and 50 more complicated ways.
> Ages ago I wrote some stuff to play with markup and change tags
> into blocks using load/markup and the fact that tags are series:
>
> http://www.codeconscious.com/rebsite/rebol-library/tag-tool.r
> http://www.codeconscious.com/rebsite/rebol-library/markup-tools.r
Cool.
> There is also form-markup which converts this representation back into a
> string:
>
> >> form-markup [ [p] "paragraph 1" [p] "paragraph2" ]
> == "<p>paragraph 1<p>paragraph2"
Quick question: Why? You could just as easily work with <p> instead, since
it's valid.
> I did this to give a basic markup manipulation facility:
>
> m: load-markup {<body bgcolor="white">some <b>text</b></body>}
> change next find first m 'bgcolor "blue"
> form-markup m
You could also use your import-tag and build-tag for the same purposes, if I
follow correctly.
And how would load-markup display the value of m there?
I'm not critiquing so much as asking for verifications and exaplanations.
Thanks for the work, Brett!
--Charles
[4/6] from: brett:codeconscious at: 16-Apr-2002 16:39
> > There is also form-markup which converts this representation back into a
> > string:
> >
> > >> form-markup [ [p] "paragraph 1" [p] "paragraph2" ]
> > == "<p>paragraph 1<p>paragraph2"
> Quick question: Why? You could just as easily work with <p> instead,
since
> it's valid.
It is the opposite of my load-markup function. My example was poor.
> > I did this to give a basic markup manipulation facility:
> >
> > m: load-markup {<body bgcolor="white">some <b>text</b></body>}
> > change next find first m 'bgcolor "blue"
> > form-markup m
> You could also use your import-tag and build-tag for the same purposes,
if I
> follow correctly.
Yep. These are what load-markup and form-markup actually use to produce
their results.
All I am doing with load-markup is to first do LOAD/MARKUP an to change each
tag!
into a block! so that manipulating the attributes of the tag is much easier.
form-markup just converts the block back to a string so that you can for
example
write out that html file you just edited.
> And how would load-markup display the value of m there?
You could have tried that for yourself! :^)
>> m: load-markup {<body bgcolor="white">some <b>text</b></body>}
== [[body bgcolor "white"] "some " [b] "text" [/b] [/body]]
>> change next find first m 'bgcolor "blue"
== []
>> m
== [[body bgcolor "blue"] "some " [b] "text" [/b] [/body]]
>> form-markup m
== {<body bgcolor="blue">some <b>text</b></body>}
> I'm not critiquing so much as asking for verifications and
exaplanations.
> Thanks for the work, Brett!
You're most welcome.
Brett.
[5/6] from: chris:ross-gill at: 16-Apr-2002 8:29
Hi Ammon,
> Has anyone delt with the Tag! datatype? I am trying to do some XML stuff,
> & I figured that REBOL probably has some neat things for dealing with tags
> seems on how they have a tag! datatype. I didn't see much in RCUG 2.3 on
> tags so I thought I might ask someone who knows. ;-) So what's neat about
> tags?
I like 'build-tag. Here's a one-liner I have to clean bad markup. It pulls
apart a tag, 'tag and makes it into an editable block, 'blk and back into an
xml tag. Easily extensible.
tag: <x val = 123> build-tag blk: to-block replace/all tag "=" " "
- Chris
[6/6] from: chalz:earthlink at: 17-Apr-2002 1:39
Just a brief response:
> > And how would load-markup display the value of m there?
>
> You could have tried that for yourself! :^)
Yeah, I could have, save that I wasn't on a machine with a REBOL
installation ;) I know, it's one of those things like, "What's the difference
between 'prin and 'print?", except I didn't have REBOL around with which to
test.
And thanks for the example :)
--Charles