root/trunk/website/org/delmore/coldfish.cfc

Revision 5, 16.3 kB (checked in by DanWilson, 17 years ago)

Initial Commit Of ModelGlue? Website (upgrade to blogcfc 511)

Line 
1<!---
2        Copyright 2008 Jason Delmore
3    All rights reserved.
4    jason@cfinsider.com
5       
6    This program is free software: you can redistribute it and/or modify
7    it under the terms of the GNU Lesser General Public License (LGPL) as published by
8    the Free Software Foundation, either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18        --->
19<!---
20       
21        History         Build   Notes
22        2/24/2008       1.0             Created by Jason Delmore
23        2/27/2008       1.0.1   Fixed defect when trying to append special characters
24                                                Fixed issues with form elements and simplified some of the logic
25        5/4/2008        1.0.6   Changed to use a StringBuilder (performance)
26        5/23/2008       1.0.7   Fixed bug with end-of-line directly after html comments
27        5/23/2008       1.0.8   Fixed bug when using multiple formatted strings in the same template
28        5/27/2008       1.0.9   Made it works with older version of CF... not very happy about implementing the old syntax... may rewrite to use native CF functions...
29       
30        6/4/2008        1.0.12  Fixed cfsetting coloring defect, also replaced use of CharAt function as it throws if there is no character at that reference
31       
32        --->
33<cfcomponent output="false">
34        <cffunction name="init" access="public" hint="This function initializes all of the variables needed for the component." output="false">
35                <cfset initializeColors()/>
36        <cfreturn this/>
37    </cffunction>
38   
39    <cffunction name="initializeColors" access="private" returntype="void" hint="This function initializes all of the colors used for highlighting." output="false">
40                <cfscript>
41                        variables.colors=structnew();
42                       
43                        // Initialize styles for various types of elements
44                        //
45                        // Default text color that is outside a tag
46                        setStyle("TEXT","color:##000000");
47                       
48                        // Comments
49                        // Greyed out
50                        setStyle("HTMLCOMMENT","color:##008000");
51                        // Yellow highlight with black text
52                        setStyle("CFCOMMENT","color:##333333;background-color:##FFFF00");
53                       
54                        // CF Tags
55                        setStyle("CFTAG","color:##990033");
56                        setStyle("CFSET","color:##000000");
57                        setStyle("SCRIPT","color:##000000");
58                       
59                        // HTML Tags
60                        setStyle("HTML","color:##008080");
61                        setStyle("HTMLSTYLES","color:##990099");
62                        setStyle("HTMLTABLES","color:##009999");
63                        setStyle("HTMLFORMS","color:##FF9900");
64                       
65                        // VALUES
66                        setStyle("VALUE","color:##0000CC");
67                        setStyle("SCRIPTVALUE","color:##006600");
68                       
69                        // BINDS
70                        setStyle("BIND","color:##000099");
71                </cfscript>
72    </cffunction>
73   
74    <cffunction name="initializeVariables" access="private" returntype="void" hint="This function initializes all of the variables needed for the component." output="false">
75                <cfscript>
76                        //initialize a buffer
77                               
78                        // If you're using JDK 1.5 or later and want some extra performance this can be a StringBuilder
79                        //variables.buffer=createObject("java","java.lang.StringBuilder").init();
80                        variables.buffer=createObject("java","java.lang.StringBuffer").init();
81                       
82                        // initialize private variables
83                        variables.isCommented=false;
84                        variables.isTag=false;
85                        variables.isValue=false;
86                        variables.isCFSETTag=false;
87                        variables.isScript=false;
88                        variables.isOneLineComment=false;
89                </cfscript>
90    </cffunction>
91   
92   
93    <cffunction name="getStyle" access="public" hint="This function can be used to get the style used in conjunction with a type of language element." output="false">
94        <cfargument name="element" type="string"/>
95                <cfreturn variables.colors[arguments.element]/>
96    </cffunction>
97    <cffunction name="setStyle" access="public" hint="This function can be used to set the style used in conjunction with a type of language element.  The value submitted should be a valid CSS black; (i.e. 'color:black;background-color:yellow;')" output="false">
98        <cfargument name="element" type="string"/>
99        <cfargument name="style" type="string"/>
100        <cfset variables.colors[arguments.element]=arguments.style/>
101    </cffunction>
102    <cffunction name="getStyles" access="public" hint="This function returns all of the styles used for each type of language element." output="false">
103                <cfreturn variables.colors/>
104    </cffunction>
105   
106        <cffunction name="formatString" access="public" hint="This function accepts a block of code and formats it into syntax highlighted HTML." output="false">
107        <cfargument name="code" type="string"/>
108                <cfscript>
109            var BIstream = createObject("java","java.io.StringBufferInputStream").init(code);
110            var IStream = createObject("java","java.io.InputStreamReader").init(BIstream);
111            var reader = createObject("java","java.io.BufferedReader").init(IStream);
112            var line = reader.readLine();
113                        initializeVariables();
114                       
115                        bufferAppend("<span style='" & getStyle("TEXT") & "'>"); // start the default text color
116                        while (isdefined("line")) {
117                formatLine(line);
118                line = reader.readLine();
119            }
120                        bufferAppend("</span>");
121                        reader.close();
122
123            return buffer;
124        </cfscript>
125        </cffunction>
126    <cffunction name="formatFile" access="public" hint="This function accepts a file path, reads in the file and formats it into syntax highlighted HTML." output="false">
127        <cfargument name="filePath" type="string"/>
128        <cfset var fileRead = "">
129        <cffile action="read" file="#arguments.filepath#" variable="fileRead">
130        <cfreturn formatString(fileRead)/>
131        </cffunction>
132        <cffunction name="formatLine" access="private" hint="This function takes a single line of code and formats it into syntax highlighted HTML." output="false">
133        <cfargument name="line" type="any"/>
134        <cfscript>
135                        var character = "";
136                        var thisLine=arguments.line;
137                        var i =0;
138                       
139                        if (variables.isOneLineComment) endOneLineComment();
140                       
141                        for (i=0; i LT thisLine.length(); i=i+1)
142                        {
143                                character=thisLine.charAt(javacast('int',i));
144                                if (character EQ '<')
145                                {
146                                        if (variables.isScript AND NOT variables.isValue)
147                                                endScript();
148                                        if (regionMatches(thisLine, 1, i+1, "!--", 0, 3))
149                                        {
150                                                if (regionMatches(thisLine, 1, i+4, "-", 0, 1))
151                                                {
152                                                        startComment("CF");
153                                                } else {
154                                                        startComment("HTML");
155                                                }
156                                        } else {
157                                                if (regionMatches(thisLine, 1, i+1, "CF", 0, 2) OR regionMatches(thisLine, 1, i+1, "/CF", 0, 3))
158                                                {
159                                                        startTag("CF");
160                                                        if (regionMatches(thisLine, 1, i+3, "SET", 0, 3) AND NOT regionMatches(thisLine, 1, i+6, "T", 0, 1)) // CFSET Tag
161                                                        {
162                                                                bufferAppend(substring(thisLine,i+1,i+6));
163                                                                i=i+5;
164                                                                startCFSET();
165                                                        }
166                                                        else if (regionMatches(thisLine, 1, i+3, "SCRIPT>", 0, 6)) // SCRIPT TAG
167                                                        {
168                                                                bufferAppend(substring(thisLine, i+1, i+9) & "&gt;");
169                                                                i=i+9;
170                                                                startScript();
171                                                        }
172                                                }
173                                                else if (
174                                                                        regionMatches(thisLine, 1, i+1, "TA", 0, 2) OR
175                                                                        regionMatches(thisLine, 1, i+1, "/TA", 0, 3) OR
176                                                                        regionMatches(thisLine, 1, i+1, "TB", 0, 2) OR
177                                                                        regionMatches(thisLine, 1, i+1, "/TB", 0, 3) OR
178                                                                        regionMatches(thisLine, 1, i+1, "TD", 0, 2) OR
179                                                                        regionMatches(thisLine, 1, i+1, "/TD", 0, 3) OR
180                                                                        regionMatches(thisLine, 1, i+1, "TF", 0, 2) OR
181                                                                        regionMatches(thisLine, 1, i+1, "/TF", 0, 3) OR
182                                                                        regionMatches(thisLine, 1, i+1, "TH", 0, 2) OR
183                                                                        regionMatches(thisLine, 1, i+1, "/TH", 0, 3) OR
184                                                                        regionMatches(thisLine, 1, i+1, "TR", 0, 2) OR
185                                                                        regionMatches(thisLine, 1, i+1, "/TR", 0, 3)
186                                                                ) // HTML TABLE
187                                                {
188                                                        startTag("HTMLTABLES");
189                                                }
190                                                else if (regionMatches(thisLine, 1, i+1, "IMG", 0, 3) OR regionMatches(thisLine, 1, i+1, "STY", 0, 3) OR regionMatches(thisLine, 1, i+1, "/STY", 0, 4)) //IMG or STYLE Tag
191                                                // TODO: Do separate syntax highlighting for stuff inside style
192                                                {
193                                                        startTag("HTMLSTYLES");
194                                                }
195                                                else if (
196                                                                        regionMatches(thisLine, 1, i+1, "FORM", 0, 4) OR
197                                                                        regionMatches(thisLine, 1, i+1, "/FORM", 0, 5) OR
198                                                                        regionMatches(thisLine, 1, i+1, "INPUT", 0, 5) OR
199                                                                        regionMatches(thisLine, 1, i+1, "/INPUT", 0, 5) OR
200                                                                        regionMatches(thisLine, 1, i+1, "TEXT", 0, 4) OR
201                                                                        regionMatches(thisLine, 1, i+1, "/TEXT", 0, 5) OR
202                                                                        regionMatches(thisLine, 1, i+1, "SELECT", 0, 6) OR
203                                                                        regionMatches(thisLine, 1, i+1, "/SELECT", 0, 7) OR
204                                                                        regionMatches(thisLine, 1, i+1, "OPT", 0, 3) OR
205                                                                        regionMatches(thisLine, 1, i+1, "/OPT", 0, 3)
206                                                                )
207                                                {
208                                                        startTag("HTMLFORMS");
209                                                } else {
210                                                        startTag("HTML");
211                                                }
212                                        }
213                                }
214                                else if (character EQ '>')
215                                {
216                                        if (variables.isCommented AND regionMatches(thisLine, 1, i-2, "--", 0, 2))
217                                        {
218                                                if (regionMatches(thisLine, 1, i-3, "-", 0, 1))
219                                                {
220                                                        endComment("CF");
221                                                } else {
222                                                        endComment("HTML");
223                                                }
224                                        } else {
225                                                if (variables.isCFSETTag) {
226                                                        endCFSET();
227                                                } else {
228                                                        endTag();
229                                                }
230                                        }
231                                }
232                                else if (character EQ '"')
233                                {
234                                        if (variables.isTag OR variables.isScript)
235                                        {
236                                                if (NOT variables.isValue) {
237                                                        startValue();
238                                                        bufferAppend('"');
239                                                } else {
240                                                        bufferAppend('"');
241                                                        endValue();
242                                                }
243                                        } else {
244                                                bufferAppend('"');
245                                        }
246                                }
247                                else if (character EQ '{')
248                                {
249                                        startBind();
250                                        bufferAppend("{");
251                                        endBind();
252                                }
253                                else if (character EQ '}')
254                                {
255                                        startBind();
256                                        bufferAppend("}");
257                                        endBind();
258                                }
259                                else if (character EQ '/')
260                                {
261                                        if (variables.isScript AND regionMatches(thisLine, 1, i+1, "/", 0, 1))
262                                        {
263                                                startOneLineComment();
264                                        }
265                                        else if (variables.isCommented)
266                                        {
267                                                if (regionMatches(thisLine, 1, i-1, "*", 0, 1))
268                                                {
269                                                        endComment("SCRIPT");
270                                                } else {
271                                                        bufferAppend("/");
272                                                }
273                                        } else {
274                                                if (regionMatches(thisLine, 1, i+1, "*", 0, 1))
275                                                {
276                                                        startComment("SCRIPT");
277                                                } else {
278                                                        bufferAppend("/");
279                                                }
280                                        }
281                                }
282                               
283                                // straight up replacements
284                                else if (character EQ '\t' OR character EQ '    ')
285                                {
286                                        bufferAppend("&nbsp;&nbsp;&nbsp;&nbsp;");
287                                }
288                                else if (character EQ ' ')
289                                {
290                                         bufferAppend("&##32;");
291                                } else {
292                                        bufferAppend(character.toString());
293                                }
294                        }
295                        bufferAppend("<br />");
296                </cfscript>
297    </cffunction>
298    <cffunction name="regionMatches" access="private" hint="This function checks if a regionMatches." output="false">
299                <cfargument name="string1" type="any"/>
300        <cfargument name="caseInsensitive" type="boolean" default="true"/>
301        <cfargument name="startPosition1" type="numeric"/>
302        <cfargument name="string2" type="any"/>
303        <cfargument name="startPosition2" type="numeric"/>
304        <cfargument name="endPosition2" type="numeric"/>
305                <cfreturn arguments.string1.regionMatches(arguments.caseInsensitive, javacast('int',arguments.startPosition1), arguments.string2, javacast('int',arguments.startPosition2), javacast('int',arguments.endPosition2))/>
306    </cffunction>
307    <cffunction name="substring" access="private" hint="This function gets a substring from a line." output="false">
308                <cfargument name="string" type="any"/>
309        <cfargument name="startPosition" type="numeric"/>
310        <cfargument name="endPosition" type="numeric"/>
311                <cfreturn arguments.string.substring(javacast('int',arguments.startPosition), javacast('int',arguments.endPosition))/>
312    </cffunction>
313    <cffunction name="bufferAppend" access="private" hint="This function appends the buffer." output="false">
314        <cfargument name="string" type="string"/>
315                <cfreturn variables.buffer.append(arguments.string)/>
316    </cffunction>
317    <cffunction name="startHighlight" access="private" hint="" output="false">
318        <cfargument name="element" type="string"/>
319                <cfset bufferAppend("<span style='" & variables.colors[arguments.element]& "'>")/>
320    </cffunction>
321    <cffunction name="endHighlight" access="private" hint="" output="false">
322                <cfargument name="line" type="any"/>
323                <cfset bufferAppend("</span>")/>
324    </cffunction>
325    <cffunction name="startOneLineComment" access="private" output="false">
326        <cfargument name="line" type="any"/>
327                <cfscript>
328                startHighlight("HTMLCOMMENT");
329                bufferAppend("/");
330                variables.isOneLineComment=true;
331                variables.isCommented=true;
332                </cfscript>     
333    </cffunction>
334    <cffunction name="endOneLineComment" access="private" output="false">
335        <cfargument name="line" type="any"/>
336                <cfscript>
337                endHighlight();
338                variables.isOneLineComment=false;
339                variables.isCommented=false;
340                </cfscript>     
341    </cffunction>
342        <cffunction name="startComment" access="private" output="false">
343        <cfargument name="type" type="string"/>
344        <cfscript>
345                if (type  EQ  "CF") {
346                        startHighlight("CFCOMMENT");
347                        bufferAppend("&lt;");
348                } else if (type  EQ  "HTML") {
349                        startHighlight("HTMLCOMMENT");
350                        bufferAppend("&lt;");
351                } else if (type  EQ  "SCRIPT") {
352                        startHighlight("HTMLCOMMENT");
353                        bufferAppend("/");
354                }
355                variables.isCommented=true;
356                </cfscript>
357    </cffunction>
358        <cffunction name="endComment" access="private" output="false">
359        <cfargument name="type" type="string"/>
360        <cfscript>
361                if (type  EQ  "SCRIPT") {
362                        bufferAppend("/");
363                } else {
364                        bufferAppend("&gt;");
365                }
366                endHighlight();
367                variables.isCommented=false;
368                </cfscript>
369    </cffunction>
370        <cffunction name="startTag" access="private" output="false">
371        <cfargument name="type" type="string"/>
372        <cfscript>
373                if (NOT variables.isCommented AND NOT variables.isValue) {
374                        if (type  EQ  "CF") {
375                                startHighlight("CFTAG");
376                        } else if (type  EQ  "HTMLSTYLES") {
377                                startHighlight("HTMLSTYLES");
378                        } else if (type  EQ  "HTMLTABLES") {
379                                startHighlight("HTMLTABLES");
380                        } else if (type  EQ  "HTMLFORMS") {
381                                startHighlight("HTMLFORMS");
382                        } else { // type is HTML
383                                startHighlight("HTML");
384                        }
385                        variables.isTag=true;
386                }
387                bufferAppend("&lt;");
388                </cfscript>
389    </cffunction>
390        <cffunction name="endTag" access="private" output="false">
391        <cfscript>
392                bufferAppend("&gt;");
393                if (NOT variables.isCommented AND NOT variables.isValue) {
394                        endHighlight();
395                        variables.isTag=false;
396                }
397                </cfscript>
398    </cffunction>
399        <cffunction name="startValue" access="private" output="false">
400        <cfscript>
401                if (NOT variables.isCommented) {
402                        if (variables.isCFSETTag OR variables.isScript) {
403                                startHighlight("SCRIPTVALUE");
404                        } else {
405                                startHighlight("VALUE");
406                        }
407                        variables.isValue=true;
408                }
409                </cfscript>
410    </cffunction>
411        <cffunction name="endValue" access="private" output="false">
412        <cfscript>
413                if (NOT variables.isCommented) {
414                        endHighlight();
415                        variables.isValue=false;
416                }
417                </cfscript>
418        </cffunction>
419        <cffunction name="startBind" access="private" output="false">
420        <cfscript>
421                if (NOT variables.isCommented) {
422                        startHighlight("BIND");
423                        bindDef=true;
424                }
425                </cfscript>
426    </cffunction>
427        <cffunction name="endBind" access="private" output="false">
428        <cfscript>
429                if (NOT variables.isCommented) {
430                        endHighlight();
431                        bindDef=false;
432                }
433                </cfscript>
434        </cffunction>
435        <cffunction name="startCFSET" access="private" output="false">
436        <cfscript>
437                if (NOT variables.isCommented) {
438                        startHighlight("CFSET");
439                        variables.isCFSETTag=true;
440                }
441                </cfscript>
442    </cffunction>
443        <cffunction name="endCFSET" access="private" output="false">
444        <cfscript>
445                if (NOT variables.isCommented) {
446                        endHighlight();
447                        bufferAppend("&gt;");
448                        endHighlight();
449                        variables.isCFSETTag=false;
450                } else {
451                        bufferAppend("&gt;");
452                }
453                </cfscript>
454    </cffunction>
455        <cffunction name="startScript" access="private" output="false">
456        <cfscript>
457                if (NOT variables.isCommented) {
458                        endHighlight();
459                        startHighlight("SCRIPT");
460                        variables.isScript=true;
461                }
462                </cfscript>
463        </cffunction>
464        <cffunction name="endScript" access="private" output="false">
465        <cfscript>
466                if (NOT variables.isCommented) {
467                        endHighlight();
468                        variables.isScript=false;
469                }
470                </cfscript>
471        </cffunction>
472</cfcomponent>
Note: See TracBrowser for help on using the browser.