ColorizeScript challenge

Geoff Canyon gcanyon at inspiredlogic.com
Thu Apr 27 06:31:08 CDT 2006


On Apr 26, 2006, at 2:17 PM, 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.

Attempting to take back the beer (I don't drink, so I'm not sure why  
I'm bothering...)

I tidied up my script and added support for some comment forms. It's  
fractionally (<5%) slower than before.

Having assessed a bit, I think this:

  -- preserves < > & and whitespace
  -- colorizes of course
  -- handles standard comments
  -- does libURL in 23 ticks on my 1ghz machine, of which over half  
is in the single step of setting the htmlText of the field.

It does not handle block comments. It can also be fooled by lines of  
the form:

   put ("oh no, #this will be a comment")

Everything after the # would be colored as a comment. I didn't know  
until now that the above breaks the "quoted text is one word" rule.  
There are eight words in that line according to Revolution.

Here's the script. Note that I broke out the HTML form of the  
colorization values into a global, and put the initialization of that  
into a separate handler. Also, forgive that I speak Transcript with a  
Revolution accent ;-)

function colorizeScript pScript -- takes a script, returns colorized  
htmlText
   global gREVScriptHTMLColors
   if gREVScriptHTMLColors["if"] is empty then revSetupHTMLColors
   replace "&" with "&amp;" in pScript
   replace "<" with "&lt;" in pScript
   replace ">" with "&gt;" in pScript
   repeat for each line L in pScript
     put 0 into i
     repeat for each word W in L
       add 1 to i
       if gREVScriptHTMLColors[W] is not empty then
         put gREVScriptHTMLColors[W] into word i of L
         add 1 to i
       else if char 1 of W is "#" or char 1 to 2 of W is "--" then
         put "<font color=DarkOrchid4>" before word i of L
         put "</font>" after L
         exit repeat
       end if
     end repeat
     put "<p>" & L & "</p>" & cr after tReturn
   end repeat
   return tReturn
end colorizeScript

on revSetupHTMLColors -- initializes gREVScriptHTMLColors global
   global gREVScriptColors,gREVScriptHTMLColors
   if gREVScriptColors["if"] is empty then revSetupColors
   delete variable gREVScriptHTMLColors
   put gREVScriptColors into tColors
   combine tColors with cr and tab
   set the itemdelimiter to tab
   repeat for each line L in tColors
     -- watch out for text wrap with this line:
     put format("%s\t<font color=\"%s\">%s</font>\n",item 1 of L,item  
2 of L,item 1 of L) after gREVScriptHTMLColors
   end repeat
   split gREVScriptHTMLColors with cr and tab
end revSetupHTMLColors



More information about the metacard mailing list