Hi Patrick,

I had a quick attempt at your problem when you first posted it, and have to 
agree, it's not so easy. ;-)

I did expect someone to come up with a good method using PARSE though, and am 
surprised they haven't.  Anyway, I've had another go tonight and have worked 
out a method - one that uses a recursive approach.

Essentially, to find the end of an URL, it looks for a space, return character 
or the end of the file and copies from the URL start to there.  Then, if it 
didn't find the end of the file the copied text is itself parsed.  This finds 
the URLs, but they may of course have commas and so on attached to their ends.  
It's an easy matter to strip them off though, giving you the true URL which you 
can then modify to replace the original.

So, here's the code:  There's three parts to it - a parse rule where the 
recursion is performed, a function to call it, strip any extra characters and 
replace the original and the main parsing routing which calls the function...


str: {Hey go visit http://www.me.org, it's great! Or http://www.you.org
or http://www.them.org or even http://www.us.org!}

rule: [
    some [
        to "http://"; copy text to " " (parse text rule) |
        to "http://"; copy text to "^/" (parse text rule) |
        to "http://"; copy text to end
    ]
]

modify: does [
    parse text rule
    ;-- Add characters to the FIND string in the next line to increase
    ;-- the types of characters you need to remove from the end of an URL.
    while [find {.,!)"} last text][remove back tail text]
    new-text: rejoin [{<a href="} text {">} text </a>]
    change/part s new-text length? text
    s: skip s -1 + length? new-text
]

parse str [
    some [
        to "http://"; s: copy text to " " (modify) :s |
        to "http://"; s: copy text to "^/" (modify) :s |
        to "http://"; s: copy text to end (modify) :s
    ]
    to end
]

print str


Let me know if this behaves for you! :-)

I've just thought of one problem - if there's already an URL inside tags within 
the text, it'd screw them up.  So this isn't a universal solution.  And it 
should really be made into a nice, tide function.

-- Carl Read.
 

On Friday, 7-October-2005 at 14:19:52 Patrick Philipot wrote,

>Hi List
>
>A parse problem. I would like to parse a text to replace any url in it.
>Example 
>
>Initial text :
>
>"Hey go visit http://www.me.org, it's great!"
>
>Result :
>"Hey go visit <a href="http://www.me.org";>http://www.me.org</a>, it's great!"
>
>Not so easy, isn't it ?
>
>--
>Bye 
>Pat665

-- 
To unsubscribe from the list, just send an email to 
lists at rebol.com with unsubscribe as the subject.

Reply via email to