ColorizeScript challenge

Dave Cragg dcragg at lacscentre.co.uk
Wed Apr 26 06:34:08 CDT 2006


On 26 Apr 2006, at 08:06, Richard Gaskin wrote:

> Geoff Canyon wrote:
>> On Apr 25, 2006, at 4:00 PM, Richard Gaskin wrote:
>>> Anyone have ideas on how to speed up MC's script colorizing?
>> Funny you should ask. I did this once as a thought experiment,  
>> with an eye to never storing the colorized version of the script  
>> but generating it on the fly.
>> I changed the script to get the text of the script into a  
>> variable, then build the HTMLtext necessary, then set the HTMLtext  
>> of the field.
>> The code is about 100 times as fast as the existing code -- it  
>> does the libURL script in about half a second on my PowerBook.
>> I can send the stack if you like -- it's thought experiment  
>> quality i.e. no documentation and not guaranteed.
>
> Please do. If I can work that into the MC IDE I'll not only add you  
> to the credit list, but I'll buy you a beer as well (one of the  
> benefits of local collaboration <g>).

In a bid to steal Geoff's beer, I took a look at using the htmlText  
to do this. It's certainly much faster. (A first attempt below.)

But it raised a couple of issues.

-- The "<" and ">" operators need to be converted to "&lt;" and  
"&gt;" for the htmlText. But it seems that quotes and "&" can be left  
as they are. But should those be converted anyway in case future  
engine changes enforce a stricter html encoding? A similar issue with  
multiple spaces, which the engine seems to preserve, but normal html  
rendering reduces to a single space. There may be other html-type  
things I've overlooked.

-- I was wondering if more tokens could be colorized. It seems  
possible to add prepositions to the list, but constants (as returned  
by the constantNames) seem trickier as some of those overlap with  
commands. (e.g. return). I gave up on this.

Below is the script I have now. It preserves the original spaces. It  
manages libUrl in about 500ms compared to 20 seconds for the  
original. I'm not sure if " [ " is a safe string to temporarily  
replace spaces with.

Cheers
Dave
	
on colorizescript_2
   if tokencolors["if"] is empty then setupcolors
   local s
   put field "Editor Field" into s

   if char -1 of s = return then put true into tAddReturn

   repeat for each line tEachLine in s
     put empty into tNL
     put tEachLine into tLine
     if "<" is in tLine then replace "<" with "&lt;" in tLine
     if ">" is in tLine then replace ">" with "&gt;" in tLine

     put offset("#",tLine) into tOff1
     put offset("--",tLine) into tOff2

     if tOff1 > 0 or tOff2 > 0 then
       if tOff1 = 0 then
         put tOff2 into tOff1
       else if tOff2 = 0 then
         put tOff1 into tOff2
       end if
       put char min(tOff1,tOff2) to -1 of tLine into tComment
       delete char min(tOff1,tOff2) to -1 of tLine
     else
       put empty into tComment
     end if

     replace space with " [ " in tLine
     repeat for each word w in tLine
       if w = "[" then
         put " [ " after tNL
         next repeat
       end if

       if tokencolors[w] is not empty then
         put "<font color=" & tokencolors[w] & ">"after tNL
         put w after tNL
         put "</font>" after tNL
       else
         put w after tNL
       end if
     end repeat
     replace " [ " with space in tNL
     if tComment <> empty then
       put "<font color=DarkOrchid4>" & tComment & "</font>" after tNL
     end if

     put "<p>" & tNL & "</p>" after tNS
   end repeat
   if tAddReturn then put "<p></p>" after tNS
   set the htmlText of field "Editor Field" to tNS

end colorizescript_2




More information about the metacard mailing list