ColorizeScript challenge

Richard Gaskin ambassador at fourthworld.com
Wed Apr 26 19:03:26 CDT 2006


Dave Cragg wrote:
> 
> On 26 Apr 2006, at 20:06, J. Landman Gay wrote:
>>
>> I played with this for about an hour last night. I was using the same 
>> technique, converting the script to htmltext using the replace 
>> command. I had it mostly working but got stuck on exactly the issue 
>> you mention. So let's let Geoff do it. :)
> 
> I probably should, but a challenge is hard to resist.
> 
> I think the script below gets round the html entity issue. It also 
> preserves spaces more safely than my earlier attempt. And being a 
> nervous type, I added a check at the end so it won't change the script 
> if the new version doesn't match the old version. (I'm not sure if the 
> "answer" dialog is appropriate for this script.)
> 
> It still has limitations. It won't allow the coloring of ampersands. The 
> original didn't do this either, but it could have been added to the 
> tokencolors array. It doesn't color function names that are followed by 
> parentheses. It also depends on the engine's non-standard handling of 
> html with respect to multiple spaces and other non-ASCII characters.
> 
> One thing that neither the original or this version do is color tokens 
> that have no space between them. Things like x&y. But it's a bad habit 
> anyway. :-)
> 
> But why DarkOrchid4? Isn't that the name of an Empire battle cruiser?
> 
> Cheers
> Dave
> ------------------------------------------------
> 
> on colorizescript_2
>   local s, tAddReturn, tNL, tEachLine, tLine, tOff1, tOff2
>   local tComment, w, tNS
> 
>   if tokencolors["if"] is empty then setupcolors
> 
>   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
>     replace "&" with "&" in 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 return  in tLine
> 
>     repeat for each line w in tLine
> 
>       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
>       put space after tNL
>     end repeat
>     if char -1 of tLine <> return then delete char -1 of 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
>   if the text of field "Editor Field"  <> s then
>     answer "Unable to colorize script."
>     put s into field "Editor Field"
>   end if
> end colorizescript_2

Dude, you rock.  I haven't tried Geoff's yet so I don't have a 
comparative benchmark, but the subjective experience is astonishing: 
with the old routine it took several seconds to colorize libURL, but 
with your script it's almost instantaneous.

I'll benchmark both and put the fastest in the MC IDE, but whichever is 
faster this is a noteworthy effort.  Thanks!

--
  Richard Gaskin
  Managing Editor, revJournal
  _______________________________________________________
  Rev tips, tutorials and more: http://www.revJournal.com


More information about the metacard mailing list