Help with search/replace code
[1/2] from: nigelb:anix at: 27-Nov-2000 9:11
OK, I'm still trying to write a general search/replace code block for use
with this Perl BBS I'm working on.
What I have written takes the bbs '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). I'm sure there must be a better way
of achieving this, the code here seems very inelegant and is limited. For
example it would be nice if it wasn't restricted to a limited number of
'find blks' (find1 - find4).
Can anyone 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: chaz:innocent at: 27-Nov-2000 22:25
How about something like this?
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]
parse post [ any [to "[quote]" quotestart: thru "[quote]" copy first to "["
nextbrace: (change/part quotestart compose [ "<blockquote><hr><font
size=^"1^" face=^"verdana, helvetica^">" (first) "</font>"] nextbrace)]]
parse post [ any [to "[/quote]" endquotestart: thru "[/quote]" endquoteend:
(change/part endquotestart "</blockquote>" endquoteend)]]
parse post [ any [to "[size=" sizestart: thru "[size=" copy sizevalue to
]
thru "]" sizeend: (change/part sizestart compose [ "<font size = "
(sizevalue) ">" ] sizeend)]]
parse post [ any [to "[/size]" endsizestart: thru "[/size]" endsizeend:
(change/part endsizestart "</font>" endsizeend)]]
print post
At 09:11 AM 11/27/00 -0000, you wrote: