Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

[REBOL] Better search/replace script?

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