Eric Chatonet wrote:

Hi all,

I want to change the colour of a string in some styled text.
As the string may be any string like "font", "size", etc. that appear in html tags,
I use the following code that works but I'm not satisfied with it...
Any more elegant solution?
Thanks.

Some thoughts - to use on their own, or in combination - or not at all, since they are far short of "elegance" In fact, you might feel these sacrifice elegance for speed - that's a habit of mine :-)

Basically what you are doing is
  a set of "replace"s to hide html tags
  the real replace
  a set of "replace"s to restore the html tags.

Thought 1. Simplify the 3rd set of replaces, by doing

 put numToChar(3) into c3
 replace "<font" with "<fo" & c3 & "nt" in tText
 replace "face=" with "fa"&c3&"ce=" in tText
 etc.    -- i.e. put a single c3 character into the middle of each html tag
 then do the real replace, and then
 replace c3 with empty in tText

Thought 2. There is a speed optimization to replace a sub-string by another sub-string *of the same length*.
AND note that the tags to be replaced all contain either "e" or "o", so

put numToChar(3) into c3  -- to replace o
put numToChar(8) into c8  -- to replace e
replace "<font" with "<f" & c3 & "nt" in tText
replace "face=" with "fac" & c8 & "=" in tText
replace "size=" with "siz" & c8 & "=" in tText
etc.
then do the real replace, and then
replace c3 with "o" in tText
replace c8 with "e" in tText


Thought 3. Use a list of replacements, in a script local variable.

local lReplacements = \
"<font" & comma & "<f" & c3 & "nt" & cr \
"face=" & comma & "fac" & c8 & "=" & cr \
"size=" & comma &"siz" & c8 & "=" & cr \
"color=" with "c" & c3 & "lor=" & cr \
"</font>" with "</f" & c3 & "nt>"

Then do
  repeat for each line L in lReplacements
     replace item 1 of L with item 2 of L in tText
  end repeat
  do the real replace
replace c3 with "o" in tText
replace c8 with "e" in tText


--
Alex Tweedly       http://www.tweedly.net



--
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.1.362 / Virus Database: 267.13.2/170 - Release Date: 15/11/2005

_______________________________________________
use-revolution mailing list
[email protected]
Please visit this url to subscribe, unsubscribe and manage your subscription 
preferences:
http://lists.runrev.com/mailman/listinfo/use-revolution

Reply via email to