Created
March 25, 2014 00:30
-
-
Save bennadel/9752494 to your computer and use it in GitHub Desktop.
How My ColdFusion Code Snippet Color Coding Works
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cffunction | |
name="ColorCode" | |
access="public" | |
returntype="string" | |
output="false" | |
hint="This takes code samples and color codes for display."> | |
<!--- Define arguments. ---> | |
<cfargument name="Code" type="string" required="true" /> | |
<cfscript> | |
// Define the local scope. | |
var LOCAL = StructNew(); | |
// Create a pattern for parts of the code display. | |
LOCAL.Pattern = CreateObject( "java", "java.util.regex.Pattern" ).Compile( | |
"(?i)(</?div[^>]*>)|(</?li[^>]*>)|(<!--)|(-->)|(</?[a-z]+)|(/?>)|("")" | |
); | |
// Create a pattern matcher based on our input. | |
LOCAL.Matcher = LOCAL.Pattern.Matcher( ARGUMENTS.Code ); | |
// Create a buffer to build our return text. | |
LOCAL.Buffer = CreateObject( "java", "java.lang.StringBuffer" ).Init(); | |
// Create some state values so that we can determine which actions | |
// we are supposed to take for each group. | |
LOCAL.State = StructNew(); | |
LOCAL.State.InCF = false; | |
LOCAL.State.InHTML = false; | |
LOCAL.State.InAttribute = false; | |
LOCAL.State.InComment = false; | |
LOCAL.State.InScript = false; | |
// Loop over the matcher. | |
while( LOCAL.Matcher.Find() ){ | |
// Get the gurrent group value. | |
LOCAL.Value = LOCAL.Matcher.Group(); | |
// Check to see if we in script. If we are, we need | |
// to do things very differently. | |
if (LOCAL.State.InScript){ | |
// Check to see if found a cloase cfscript tag and that we | |
// NOT in a comment tag. | |
if (REFindNoCase( "^</cfscript", LOCAL.Value )){ | |
// Add the span. | |
LOCAL.Value = ("<span class=""cfmlcodecolor"">" & LOCAL.Value); | |
// Update the state. | |
LOCAL.State.InCF = true; | |
LOCAL.State.InScript = false; | |
} | |
// Check to see if we found a close tag. | |
if ( | |
REFindNoCase( "/?>$", LOCAL.Value ) AND | |
LOCAL.State.InCF | |
){ | |
// Add the span. | |
LOCAL.Value = (LOCAL.Value & "</span>"); | |
// Update the state. | |
LOCAL.State.InCF = false; | |
} | |
} else { | |
// Check to see if we found a close list item. | |
if (REFindNoCase( "^</li", LOCAL.Value )){ | |
// Close all existing span tags. Check states | |
// to see what we have going on. | |
if (LOCAL.State.InComment){ | |
LOCAL.Value = ("</span>" & LOCAL.Value); | |
} | |
if (LOCAL.State.InCF){ | |
LOCAL.Value = ("</span>" & LOCAL.Value); | |
} | |
if (LOCAL.State.InHTML){ | |
LOCAL.Value = ("</span>" & LOCAL.Value); | |
} | |
if (LOCAL.State.InAttribute){ | |
LOCAL.Value = ("</span>" & LOCAL.Value); | |
} | |
} | |
// Check to see if we found an open list item. | |
// If this is the case, we have to re-start any tags | |
// that we closed in the previous close list item. | |
if (REFindNoCase( "^<li", LOCAL.Value )){ | |
// Re-start any existing span tags. Check states | |
// to see what we have going on. | |
if (LOCAL.State.InComment){ | |
LOCAL.Value = (LOCAL.Value & "<span class=""commentcodecolor"">"); | |
} | |
if (LOCAL.State.InCF){ | |
LOCAL.Value = (LOCAL.Value & "<span class=""cfmlcodecolor"">"); | |
} | |
if (LOCAL.State.InHTML){ | |
LOCAL.Value = (LOCAL.Value & "<span class=""htmlcodecolor"">"); | |
} | |
if (LOCAL.State.InAttribute){ | |
LOCAL.Value = (LOCAL.Value & "<span class=""attributecodecolor"">"); | |
} | |
} | |
// Check to see if found a comment. | |
if (LOCAL.Value EQ "<!--"){ | |
// Add the span. | |
LOCAL.Value = ("<span class=""commentcodecolor"">" & LOCAL.Value); | |
// Update the state. | |
LOCAL.State.InComment = true; | |
} | |
// Check to see if found a close comment and that we | |
// are already in a comment. | |
if ( | |
(LOCAL.Value EQ "-->") AND | |
LOCAL.State.InComment | |
){ | |
// Add the span. | |
LOCAL.Value = (LOCAL.Value & "</span>"); | |
// Update the state. | |
LOCAL.State.InComment = false; | |
} | |
// Check to see if found a cf tag and that we are NOT in | |
// a comment tag. | |
if ( | |
REFindNoCase( "^<cf", LOCAL.Value ) AND | |
(NOT LOCAL.State.InComment) | |
){ | |
// Check to see if we started a script tag. | |
if (REFindNoCase( "^<cfscript", LOCAL.Value )){ | |
// Update the state. | |
LOCAL.State.InScript = true; | |
} | |
// Add the span. | |
LOCAL.Value = ("<span class=""cfmlcodecolor"">" & LOCAL.Value); | |
// Update the state. | |
LOCAL.State.InCF = true; | |
} | |
// Check to see if found a cloase cf tag and that we | |
// NOT in a comment tag. | |
if ( | |
REFindNoCase( "^</cf", LOCAL.Value ) AND | |
(NOT LOCAL.State.InComment) | |
){ | |
// Add the span. | |
LOCAL.Value = ("<span class=""cfmlcodecolor"">" & LOCAL.Value); | |
// Update the state. | |
LOCAL.State.InCF = true; | |
} | |
// Check to see if found an open HTML tag and that we | |
// are not in a comment. | |
if( | |
REFindNoCase( "^<(?!cf)", LOCAL.Value ) AND | |
(NOT LOCAL.State.InComment) | |
){ | |
// Add the span. | |
LOCAL.Value = ("<span class=""htmlcodecolor"">" & LOCAL.Value); | |
// Update the state. | |
LOCAL.State.InHTML = true; | |
} | |
// Check to see if we found a close tag. | |
if ( | |
REFindNoCase( "/?>$", LOCAL.Value ) AND | |
(NOT LOCAL.State.InComment) | |
){ | |
// Check to make sure that we are NOT in an attribute | |
// and NOT in a comment. | |
if ( | |
(NOT LOCAL.State.InAttribute) AND | |
(NOT LOCAL.State.InComment) | |
){ | |
// Check to see if we are in a CF tag. | |
if (LOCAL.State.InCF){ | |
// Add the span. | |
LOCAL.Value = (LOCAL.Value & "</span>"); | |
// Update the state. | |
LOCAL.State.InCF = false; | |
} | |
// Check to see if we are in a HTML tag. | |
if (LOCAL.State.InHTML){ | |
// Add the span. | |
LOCAL.Value = (LOCAL.Value & "</span>"); | |
// Update the state. | |
LOCAL.State.InHTML = false; | |
} | |
} | |
} | |
// Check to see if we found a quote. | |
if (LOCAL.Value EQ """"){ | |
// Check to see if we are in a tag. This is the only time | |
// that we are going to want to do anything with it. | |
if ( | |
LOCAL.State.InCF OR | |
LOCAL.State.InHTML | |
){ | |
// Now, check to see if we are starting or ending an | |
// attribute value. | |
if (NOT LOCAL.State.InAttribute){ | |
// Add the span. | |
LOCAL.Value = ("<span class=""attributecodecolor"">" & LOCAL.Value); | |
// Update the state. | |
LOCAL.State.InAttribute = true; | |
} else { | |
// Add the span. | |
LOCAL.Value = (LOCAL.Value & "</span>"); | |
// Update the state. | |
LOCAL.State.InAttribute = false; | |
} | |
} | |
} | |
} | |
// Add the replacement text. | |
LOCAL.Matcher.AppendReplacement( | |
LOCAL.Buffer, | |
LOCAL.Value.ReplaceAll( "([\\\$])", "\\$1" ) | |
); | |
} | |
// Add the reset of the stuff to buffer. | |
LOCAL.Matcher.AppendTail( | |
LOCAL.Buffer | |
); | |
// Return the string buffer. | |
return( LOCAL.Buffer.ToString() ); | |
</cfscript> | |
</cffunction> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment