ColorizeScript challenge

wouter wouter.abraham at scarlet.be
Sat May 6 08:57:48 CDT 2006


Hi,

Here is another script for the ColorizeScript challenge thread (for  
what it is worth).
Not as fast as the other entries when total colorizing, but  doing  
blockcomments and faster on large commented out blocks.
Also more complete colorizing, for example inside curved brackets:
      put max(length(varA),length(varB)) into tVar

Doing block comments and badass but legit commenting like in these  
examples:

/*some worst case*/local lSomeLocal--format scenarios
local/*lSomeLocal,*/ lUsefulLocal,/*lAnotherUnusedLocal*/
local/*whatever "/*" comes into*/ /*your*/lSomething/*garbled mind*/
put tVar /*stuffed with garbage*/ into /*yet another*/ tVarB#but very  
stupid var

!!! Watch out for the mail wraps:


local lScriptHTMLColors,lScriptColors,lBlockComment

function colorizeScript pScript -- takes a script, returns colorized  
htmlText
   if the optionkey is down or lScriptHTMLColors["if"] is empty then  
SetupHTMLColors
   replace "&" with "&" in pScript
   replace "<" with "&lt;" in pScript
   replace ">" with "&gt;" in pScript
   put false into lBlockComment
   ### cut the complete script in 3 parts by using the token 1 to -1  
returned by the engine
   put token 1 to -1 of pScript into tBlock2
   if tBlock2 = "" then put graylines(pScript) into tList
   else
     put char 1 to offset(tBlock2,pScript) - 1 of pScript into tBlock1
     put offset(tBlock2,pScript) + length(tBlock2) into tLen
     put char tLen to -1 of pScript into tBlock3
     ### tokenizing removes the quotes,
     ### last quote is not included by token 1 to -1
     ### to colorize the quotes correctly:
     if char 1 of word 1 of tBlock3 = quote then
       put offset(quote,tBlock3) into tOS
       put char 1 to tOS of tBlock3 after tBlock2
       delete char 1 to tOS of tBlock3
     else if char 1 of  tBlock3 = cr then delete char 1 of tBlock3
     put num(lines in tBlock1) into tB1N
     put lineOffset(tBlock2,pScript) into tB2N
     put lineoffset(tBlock3,pScript) into tB3N
     put graylines(tBlock1) into tList
     put colorizeBlock(tBlock2) into tT
     if tB1N = tB2N then
       delete char -5 to -1 of tList
       delete char 1 to 3 of tT
       put tT after tList
     else put tT after tList
     put graylines(tBlock3) into tT
     if tB3N = tB2N + num(lines in tBlock2) - 1 then
       delete char -5 to -1 of tList
       delete char 1 to 3 of tT
       put tT after tList
     else put tT after tList
   end if
   return tList
end colorizeScript

function colorizeBlock x
   repeat for each line L in x
     if lBlockComment then
       if offset("*/",L) > 0 then
         put token 1 to -1 of L into y
         put char offset(y,L) + length(y) to -1 of l into tCheck
         if char 1 of tCheck = quote then delete char 1 of tCheck
         if char 1 of word 1 of tCheck is "#" or char 1 to 2 of word  
1 of tCheck is "--" then
           put "<p><font color=gray60>"& L & "</font></p>" & cr after  
tList
         else
           ### test if the */ is recognized by the engine  
counterbalancing the /*
           get "/*" & L && "test"
           if num(tokens in it) > 0 then
             put offset("*/",L) into tOS
             put "<font color=gray60>"& char 1 to tOS + 1 of L & "</ 
font>" into firstpart
             put false into lBlockComment ### colorizeline can  
trigger the lBlockcomment
             put colorizeLine(char tOS + 2 to -1 of L) into secondPart
             put "<p>" & firstPart & secondPart & "</p>" & cr after  
tList
           end if
         end if
       else
         put "<p><font color=gray60>"& L & "</font></p>" & cr after  
tList
       end if
     else
       put "<p>" & colorizeLine(L) & "</p>" & cr after tList
     end if
   end repeat
   return tList
end colorizeBlock

function grayLines x
   repeat for each line L in x
     put "<p><font color=gray60>"& L & "</font></p>" & cr after tList
   end repeat
   return tList
end grayLines

function colorizeLine x
   if x = "" then return x
   put token 1 to -1 of x into secondpart
   if secondpart = "" then
     if char 1 to 2 of word 1 of x = "/*" then
       put true into lBlockComment
     end if
     return "<font color=gray60>"& x & "</font>"
   end if
   ### cut the line in 3 parts by using the string of tokens returned  
by the engine
   put offset(secondpart,x) into tOS
   put char 1 to tOS - 1 of x into firstpart
   put char tOS + length(secondpart) to -1 of x into thirdpart
   ### tokenizing removes the quotes,
   ### last quote is not included by token 1 to -1
   ### to colorize the quotes correctly:
   if char 1 of thirdpart = quote then
     put quote after secondpart
     delete char 1 of thirdpart
   end if
   put secondPart into y
   ### treating part 1
   if num(words in firstpart) > 0 then
     put "<font color=gray60>"& firstpart & "</font>" into tCLine
     if char 1 to 2 of word 1 of firstpart = "/*"  then
       get firstpart && "test" ### making sure string x is not */  
terminated
       if num(tokens in it) = 0 then put true into lBlockComment
     end if
   else
     put firstpart into tCLine
   end if
   ### treating part 2
   if num(tokens in secondpart) > 0 then
     ### make a "in between quote tokens" skiplist
     put 0 into tC
     put "" into tSkiplist
     put 0 into tSkip
     put "" into tGrayList
     if "/*" is in secondpart then
       repeat with i = num(tokens in y) down to 1
         put tab into token i of y
       end repeat
       set the itemdel to tab
       put quote & tab & ";" into tExlusions
       repeat for each item i in y
         if num(words in i) > 0 then
           if word 1 of i is among the items of tExlusions then
           else put i & cr before tGrayList
         end if
       end repeat
       if quote is in y then
         repeat
           add 1 to tC
           put offset(quote,tT,tSkip) into tOS
           if tOS = 0 then exit repeat
           else put tOS + tSkip into tSkip
           if tC = 2 then
             put num(items in char 1 to tSkip of tT)  & cr after  
tSkiplist
             put 0 into tC
           end if
         end repeat
       end if
     else if quote is in secondpart then
       repeat
         add 1 to tC
         put offset(quote,secondpart,tSkip) into tOS
         if tOS = 0 then exit repeat
         else put tOS + tSkip into tSkip
         if tC = 2 then
           put num(tokens in char 1 to tSkip of secondpart) & cr  
after tSkiplist
           put 0 into tC
         end if
       end repeat
     end if
     ### make a list to colorize the tokens in reverse order
     put 0 into tC
     put "" into tCList
     repeat for each token i in secondpart
       add 1 to tC
       if lScriptHTMLColors[i] <> "" and tC is not among the lines of  
tSkipList then put lScriptHTMLColors[i]& tab & tC & cr before tCList
     end repeat
     ### start colorizing from list
     if tCList <> "" then
       set the itemdel to tab
       repeat for each line i in tCList
         put item 1 of i into token (item 2 of i) of secondpart
       end repeat
     end if
   end if
   if tGrayList <> "" then
     repeat for each line i in tGrayList
       put offset(i,secondpart) into tStart
       put length(i) - 1 into tLen
       put "<font color=gray60>"& i & "</font>" into char tStart to  
tStart + tLen of secondpart
     end repeat
   end if
   put secondpart after tCLine
   ### treating part 3
   put false into tGray
   if num(words in thirdpart) > 0 then
     if char 1 of word 1 of thirdpart is "#" or char 1 to 2 of word 1  
of thirdpart = "--" then put true into tGray
     else if char 1 to 2 of word 1 of thirdpart = "/*" then
       put true into tGray
       get thirdpart && "test" ## making sure string x is not */  
terminated
       if num(tokens in it) = 0 then
         put true into lBlockComment
       end if
     end if
     if tGray then
       put "<font color=gray60>"& thirdpart & "</font>" after tCLine
     else put thirdpart after tCLine
   else
     put thirdpart after tCLine
   end if
   return  tCLine
end colorizeLine

### Geoff's colorizing array without revs
on setupHTMLColors -- initializes lScriptHTMLColors global
   if lScriptColors["if"] is empty then SetupColors
   delete variable lScriptHTMLColors
   put lScriptColors into tColors
   combine tColors with cr and tab
   set the itemdelimiter to tab
   repeat for each line L in tColors
     put format("%s\t<font color=\"%s\">%s</font>\n",item 1 of L,item  
2 of L,item 1 of L) after lScriptHTMLColors
   end repeat
   split lScriptHTMLColors with cr and tab
end setupHTMLColors

on setupcolors
   repeat for each word w in the commandNames
     put "blue" into lScriptColors[w]
   end repeat
   repeat for each word w in "if then else repeat for with while on  
end switch case getprop setprop or and"
     put "Brown" into lScriptColors[w]
   end repeat
   repeat for each word w in the functionNames
     put "DarkOrange" into lScriptColors[w]
   end repeat
   repeat for each word w in the propertyNames
     put "red" into lScriptColors[w]
   end repeat
end setupcolors


Try it on scripts with half a block comment /*
Greetings,
Wouter


More information about the metacard mailing list