[REBOL] Re: Parse problem
From: greggirwin:mindspring at: 7-Oct-2005 7:26
Hi Patrick,
PP> A parse problem. I would like to parse a text to replace any url in it.
PP> Example
I hacked the link parser out of the IOS Conference reblet at one
point, but I didn't do any text replacement with it, I just made it a
little more general in that you could pass in a callback function. It
still has some issues that should be addressed (e.g. it includes
trailing commas on URLs), but it might give you something to build on.
-- Gregg
Pardon the formatting; watch for wrap.
REBOL []
link-parser: context [
white-space: charset reduce [#" " newline tab cr #"<" #">"]
non-white-space: complement white-space
to-space: [some non-white-space | end]
skip-to-next-word: [some non-white-space some white-space]
link-rule: copy []
callback: none
make-action: func [link] [
compose [
mark:
(link) (either string? link [[to-space end-mark:]] [])
(to-paren compose [callback copy/part mark end-mark])
any white-space
]
]
make-link-rules: func [schemes] [
clear link-rule
foreach scheme schemes [
repend link-rule [make-action scheme '|]
]
append link-rule 'skip-to-next-word
use [mark end-mark text offset] [bind link-rule 'mark]
]
set 'parse-links func [
input [any-string!]
action [any-function!]
/with
schemes [block!] "Block of scheme patterns to look for"
][
make-link-rules any [
schemes
["https://" "http://" "www." "ftp://" "ftp."]
]
callback: :action
error? try [parse/all input [any link-rule]]
]
]
s: {Check out
http://www.rebol.org
And if you like that, you'll really like www.rebol.com
Then, for more excitement, port your data to ftp://blah-blah-blah
}
parse-links s func [url] [print url]
print "^/This pass will only look for FTP links^/"
s: {Check out
http://www.rebol.org
And if you like that, you'll really like www.rebol.com
Then, for more excitement, port your data to ftp://blah-blah-blah
}
parse-links/with s func [url] [print url] ["ftp://"]
halt