Better search/replace script?
[1/2] from: nigel::awardsplus::co::uk at: 24-Nov-2000 13:42
OK, I'm still trying to write a general search/replace code block for use
with this Perl BBS I'm working with.
What I have written takes the 'post' with it's custom tags and applies
search/replace rules on it as defined in 'rule_blk'. The resultant post is
displayable html.
The code I've written doesn't yet do all that I need it to do, but it's a
start (only 2 rules are shown here). One problem is that it's limited to 4
'find blks' (find1 - find4) the other is that I'm sure it can be written in
a lot fewer lines and a lot better than what I've done here.
Can ayone suggest a better script?
Thanks - Nigel
rebol []
p1: "----- [quote]first[quote]second[/quote]third[/quote] ---- ++and
again++ ---- [quote]first/2[quote]second/2[/quote]third/2[/quote] ---------"
p2: "Normal size text [size=4] large text[/size] back to normal text"
post: join p1 [p2]
rule_blk: [
["[quote]*[quote]*[/quote]*[/quote]" "<blockquote><hr><font size=^"1^"
face=^"verdana, helvetica^">*1</font><blockquote><hr><font size=^"1^"
face=^"verdana, helvetica^">*2</font><hr></blockquote><font size=^"1^"
face=^"verdana, helvetica^">*3</font><hr></blockquote>"]
["[size*=*]*[/size]" "<font size = *2> *3 </font>"]
]
replace_blk: []
foreach [blk] rule_blk [
find_blk: to-block parse first blk "*"
parse post [any [
(find1: find_blk/1 find2: find_blk/2 find3: find_blk/3 find4: find_blk/4)
thru find1 copy text1 to find2 (if text1 = none [text1: ""])
thru find2 copy text2 to find3 (if text2 = none [text2: ""])
thru find3 copy text3 to find4 (if text3 = none [text3: ""]
replace_string: rejoin [to-string find1 text1 to-string find2 text2
to-string find3 text3 to-string find4]
new_string: to-string second blk
replace new_string "*1" text1
replace new_string "*2" text2
replace new_string "*3" text3
replace post replace_string new_string
replace_string: ""
)
]
to end
]
]
print post
halt
[2/2] from: arolls:bigpond:au at: 28-Nov-2000 18:19
Hi Nigel,
> OK, I'm still trying to write a general search/replace code block for use
> with this Perl BBS I'm working with.
How about this:
rebol []
post: {---- [quote]first[quote]second[/quote]third[/quote] --- ++and
again++ --- [quote]first/2[quote]second/2[/quote]third/2[/quote] ----
Normal size text [size=4] large text[/size] normal text again
}
post: join p1 [p2]
out: copy "" ; this is where the output goes.
size: [
"[size=" copy s to "]" skip (append out reduce ["<font size=" s ">"])
[begin: text] |
[end: "[/size]" (append out "</font>")]
]
quote: [
"[quote]" (append out
{^/<blockquote><hr><font size="1" face="verdana, helvetica">})
[begin: text] |
[end: "[/quote]" (append out "</font><hr></blockquote>^/")]
]
; set of all ascii characters 0 - 255
character: charset compose [(to-char 0) - (to-char 255)]
text: [
some [
[quote] |
[size] |
[copy c [character] (append out c)] ; let a character go through
]
]
parse/all post text ; /all refinement - consider spaces too
write %NigelBooker.html rejoin ["<html><body>" out "</body></html>"]
browse %NigelBooker.html
-Anton.