Help required for a little parsing problem
[1/8] from: christian:ensel:gmx at: 18-Nov-2000 21:37
Hello Nigel,
> Whilst I could probably write a (long) solution I'm sure there must be an
> elegant couple of line Rebol solution - one problem is that there may be any
> number of links in the post.
I'm not sure whether it's elegant :)
At least it's a couple of lines and does the trick for multiple occurences
of [url]...[/url] brackets:
;------------------------------------------------
post: {
This is a post - Perl is good [url]http://www.perl.com[/url], Rebol
is better [url]http://www.rebol.com[/url]!
This is a post - Perl is good [url]http://www.perl.com[/url], Rebol
is better [url]http://www.rebol.com[/url]!
This is a post - Perl is good [url]http://www.perl.com[/url], Rebol
is better [url]http://www.rebol.com[/url]!
}
parse post [
any [
to "[url]" tag: 5 skip copy url to "[/url]" (
remove/part tag add length? url 11
insert tag rejoin [
{<a href="} url {" target="_blank">} url {</a>}
]
)
]
to end
]
;------------------------------------------------
Hope this helps,
Christian
[Christian--Ensel--GMX--De]
[2/8] from: al:bri:xtra at: 19-Nov-2000 9:42
Nigel wrote:
> Whilst I could probably write a (long) solution I'm sure there must be an
elegant couple of line Rebol solution - one problem is that there may be any
number of links in the post.
Process: function [String [string!]
][
UrlStart Url UrlEnd
][
parse String [
any [
to "[url]" UrlStart: thru "[url]" copy Url to "[/url]" thru
[/url]
UrlEnd: (
change/part UrlStart rejoin [
"" join <a> [
{ href="} Url {" target="_blank"}
] Url</a>
] UrlEnd
)
] to end
]
String
]
BulletinBoard: Process Post
Andrew Martin
ICQ: 26227169
http://members.nbci.com/AndrewMartin/
-><-
All on one line (Caution: Line may be broken by email):
Process: function [String [string!]][UrlStart Url UrlEnd][parse String [any
[to "[url]" UrlStart: thru "[url]" copy Url to "[/url]" thru "[/url]"
UrlEnd: (change/part UrlStart rejoin ["" join <a> [{ href="} Url {"
target="_blank"}]Url</a>] UrlEnd)]to end]String]
[3/8] from: christian:ensel:gmx at: 18-Nov-2000 21:59
Hallo Nigel,
Andrew's version is even shorter than mine, doing it by just
'change -ing instead of 'remove -ing and 'insert -ing.
Nevertheless, here's my version with additinal comments:
parse post [
any ; apply the following rule 0 or more times
[
to "[url]" ; jumps to the first occurence of "[url]"
; we're right ahead of it now
tag: ; remember the position in the string
5 skip ; skip 5 characters ("[url]")
copy url to "[/url]" ; copy everthing till we encounter a
; string "[/url]"
; 'url now holds the url, but not the brackets
; all the above is the 'parse dialect
( ; start of code (non-dialect) section
remove/part tag add length? url 11
; remove length? url plus additional 11
; chars ("[url]" = 5 + "[/url]" = 6)
; from the input string,
; counting from the position we told
; 'parse to remember in 'tag
insert tag rejoin [
{<a href="} url {" target="_blank">} url {</a>}
]
; composes the html-tag and inserts
; it at the position 'tag
; voila!
) ; end of code section
]
to end
]
If you want to learn about 'parse see the new user guide on rebol.com,
chapter 14. Have fun!
Christian
[Christian--Ensel--GMX--De]
[4/8] from: nigelb:awardsplus at: 19-Nov-2000 8:18
Thanks Andrew & Christian for the replies to the parsing problem.
I'll try them later - I'm sure it will save me a big headache....
Thanks
Nigel
[5/8] from: al:bri:xtra at: 19-Nov-2000 21:43
Nigel wrote:
> I'll try them later - I'm sure it will save me a big headache....
To save yourself from more headaches, why not drop the "[url]" notation, and
directly generate the HTML link by recognising the http:// url? So your
example becomes:
post: {
This is a post - Perl is good http://www.perl.com,
Rebol is better http://www.rebol.com.
}
result: {
This is a post - Perl is good <a
href="http://www.perl.com">http://www.perl.com</a>,
Rebol is better <a
href="http://www.rebol.com">http://www.rebol.com</a>.
}
Then you could go one step further yet again:
post: {
This is a post - Perl is good: http://www.perl.com,
Rebol is better: http://www.rebol.com.
}
result: {
This is a post - <a href="http://www.perl.com">Perl is good</a>,
<a href="http://www.rebol.com">Rebol is better</a>.
}
Want an asprin? :-)
Andrew Martin
ICQ: 26227169
http://members.nbci.com/AndrewMartin/
[6/8] from: chaz:innocent at: 19-Nov-2000 1:43
parse post [
any [ to "[url]" url_start:
thru "[url]" copy href to "[/url]"
thru "[/url]" url_end:
(change/part url_start
compose [("<a href = ^"") (href) ("^" target = ^"_blank^">") (href)
("</a>")]
url_end)
]
]
Go through the post.
Move to the beginning of the '[url] tag and save that position into the
word 'url_start
copy the value of the url into 'href
move to the end of the '[/url] tag and save that position in 'url_end
Replace everything between 'url_start and 'url_end
with a new string composed of three strings and two instances of the value
of the word 'href
('compose concatenates and evaluates)
Please email me off-list with the Perl equivalent.
chaz
P.S. Thanks to Gabriele for his Expression Evaluator.
At 04:41 PM 11/18/00 -0000, you wrote:
[7/8] from: g:santilli:tiscalinet:it at: 19-Nov-2000 11:50
Hello Nigel!
On 18-Nov-00, you wrote:
NB> Whilst I could probably write a (long) solution I'm sure
NB> there must be an elegant couple of line Rebol solution - one
NB> problem is that there may be any number of links in the post.
NB> Any suggestions?
Just an idea:
post-rule: [some [link | text]]
link: ["[url]" copy url to "[/url]" 6 skip (emit-link url)]
text: [copy chars [to "[url]" | skip to end] (emit chars)]
emit-link: func [url] [
emit build-tag [A HREF (url) TARGET "_blank"]
emit url
emit </A>
]
emit: func [text] [
insert tail output text
]
output: ""
parse-post: func [post] [
clear output
if parse/all post post-rule [copy output]
]
>> print parse-post "This is a post - Perl is good [url]http://www.perl.com[/url], Rebol
is better [url]http://www.rebol.com[/url]"
This is a post - Perl is good <A HREF="http://www.perl.com" TARGET="_blank">http://www.perl.com</A>,
Rebol is better <A HREF="http://www.rebol.com" TARGET="_blank">http://www.rebol.com</A>
(Perhaps it's a bit long...)
Regards,
Gabriele.
--
Gabriele Santilli <[giesse--writeme--com]> - Amigan - REBOL programmer
Amiga Group Italia sez. L'Aquila -- http://www.amyresource.it/AGI/
[8/8] from: nigelb::awardsplus::co::uk at: 18-Nov-2000 16:41
Hi
I'm writing an add-on for a bulletin board and need a bit of help in writing
some elegant code to do the following:
a variable has the content of a post -eg:
post: "This is a post - Perl is good [url]http://www.perl.com[/url], Rebol
is better [url]http://www.rebol.com[/url]"
when this is posted to the board the text between the [url] and [/url] ends
up as a link.
So what I need to do is change the above text to:
This is a post - Perl is good <a href = ^
http://www.perl.com^" target ^"_blank^">http://www.perl.com</a>, Rebol is better <a
href=^"http://www.rebol.com^" target=^"_blank^">http://www.rebol.com</a>"
Whilst I could probably write a (long) solution I'm sure there must be an
elegant couple of line Rebol solution - one problem is that there may be any
number of links in the post.
Any suggestions?
Thanks in advance
Nigel