BlogCFC ColdFish Code Block Print Function Update

by Edward on March 17, 2011

Normally I pay quite a bit of attention to my site after I do any major updates or changes but I’ve been quite a slacker lately and I seemed to have missed a few issues that happened after I updated my blog core … Bad me.

Something interesting that I truly thought would be an isolated incident – and my problem – actually wasn’t. It seems that there was a bug where Gecko based browsers – Firefox – don’t seem to like the window.frames[] syntax … So, for those that are using BlogCFC and want to be able to print code blocks from Firefox … You may need to update the formatter.cfc with the new syntax … Here’s the link for the formatter.cfc … on RIAForge … or you can simply copy the update from here …


<!---
Copyright 2009 Jason Delmore
All rights reserved.
jason@cfinsider.com
--->

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License (LGPL) as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see .

//initialize a buffer

// If you're using JDK 1.5 or later and want some extra performance this can be a StringBuilder
//variables.buffer=createObject("java","java.lang.StringBuilder").init();
variables.buffer=createObject("java","java.lang.StringBuffer").init();

// initialize private variables
// TODO : Change the parser state to be a struct rather than individual variables.
variables.isCommented=false;
variables.isTag=false;
variables.isValue=false;
variables.isCFSETTag=false;
variables.isCFScript=false;
variables.isCFQueryTag=false;
variables.isOneLineComment=false;
variables.isMXML=false;
variables.isActionscript=false;
variables.isSQL=false;
variables.isSQLValue=false;
variables.initialparser="";
variables.spansOpened = 0;
variables.spansClosed = 0;

var BIstream = createObject("java","java.io.StringBufferInputStream").init(arguments.code);
var IStream = createObject("java","java.io.InputStreamReader").init(BIstream);
var reader = createObject("java","java.io.BufferedReader").init(IStream);
var line = reader.readLine();
var linenumber = 0;

if (arguments.parser neq "") {
"variables.is#arguments.parser#" = true;
}

if (getConfig().getShowToolbar()) {
variables.buffer.append(getToolbarHTML(arguments.code,arguments.codesig));
}

variables.buffer.append("<span id="'formatted_code_&quot;">");
while (isdefined("line")) {
if (getConfig().getShowLineNumbers()) {
linenumber = linenumber + 1;
variables.buffer.append("<span>" &amp; linenumber &amp; "</span>");
}
formatLine(line);
line = reader.readLine();
}
// there appears to be more spans created than cleaned up... closing up any extras... will need to review to see what is keeping extra spans
while (variables.spansOpened gt variables.spansClosed) {
variables.spansClosed = variables.spansClosed + 1;
variables.buffer.append("</span>");
}
variables.buffer.append("");
reader.close();

return variables.buffer;

<iframe id="'print_frame_#arguments.codesig#'" style="" frameborder="'0'" width="320" height="240"></iframe>
<script type="text/javascript">// <![CDATA[
    				function toggle_view_#arguments.codesig#() { 					var temp = document.getElementById(&apos;htmlencoded_plain_#arguments.codesig#&apos;).style.display; 					document.getElementById(&apos;htmlencoded_plain_#arguments.codesig#&apos;).style.display=document.getElementById(&apos;formatted_code_#arguments.codesig#&apos;).style.display; 					document.getElementById(&apos;formatted_code_#arguments.codesig#&apos;).style.display=temp; 					if (temp==&apos;none&apos;) { 						document.getElementById(&apos;view_#arguments.codesig#&apos;).innerHTML=&apos;view formatted&apos;; 					} else { 						document.getElementById(&apos;view_#arguments.codesig#&apos;).innerHTML=&apos;view plain&apos;; 					} 				} 				function copy_to_clipboard_#arguments.codesig#() { 					var code=unescape(document.getElementById(&apos;htmlencoded_plain_#arguments.codesig#&apos;).innerHTML).replace(/&lt;/g, &apos;\x3C&apos;).replace(/&gt;/g, &apos;\x3E&apos;).replace(/&amp;/g, &apos;\x26&apos;).replace(/\x3Cbr\x3E/gi, &apos;\r\n&apos;).replace(new RegExp(&apos;&nbsp;&nbsp;&nbsp;&nbsp;&apos;, &apos;gi&apos;), &apos;\t&apos;); 					window.clipboardData.setData(&apos;text&apos;,code); 				} 				function print_#arguments.codesig#() { 					document.getElementById("print_frame_#arguments.codesig#").contentWindow.document.body.innerHTML = document.getElementById(&apos;formatted_code_#arguments.codesig#&apos;).innerHTML; 					document.getElementById("print_frame_#arguments.codesig#").contentWindow.focus(); 					document.getElementById("print_frame_#arguments.codesig#").contentWindow.print(); 				} 				function show_about_#arguments.codesig#() { 					document.getElementById(&apos;about_#arguments.codesig#&apos;).style.display=&apos;inline&apos;; 					window.setTimeout(&apos;hide_about_#arguments.codesig#();&apos;, 4000); 				} 				function hide_about_#arguments.codesig#() { 					document.getElementById(&apos;about_#arguments.codesig#&apos;).style.display=&apos;none&apos;; 				}
// ]]></script>
<div><!--- Toggle code view --->
<a id="'view_#arguments.codesig#'" href="'javascript:toggle_view_#arguments.codesig#()'">view plain</a><!--- Copy to clipboard --->
<a id="'view_copy_to_clipboard_link_#arguments.codesig#'" href="'javascript:copy_to_clipboard_#arguments.codesig#()'">copy to clipboard</a>
<!--- The cross-browser copy to clipboard methods out there are hacky and only work on certain browsers... if the browser handles it, then the link show up... --->
<script type="text/javascript">// <![CDATA[
    if(window.clipboardData) { document.getElementById(&apos;view_copy_to_clipboard_link_#arguments.codesig#&apos;).style.display=&apos;inline&apos;;}
// ]]></script><!--- Print --->
<a href="'javascript:print_#arguments.codesig#()'">print</a><!--- About --->
<a href="'javascript:show_about_#arguments.codesig#()'">about</a></div>
<span id="'about_#arguments.codesig#'"><span> </span> ColdFISH is developed by Jason Delmore. Source code and license information available at <a href="'http://coldfish.riaforge.org/'">coldfish.riaforge.org</a>
</span>
<span id="'htmlencoded_plain_#arguments.codesig#'">#REReplace(REReplace(htmleditformat(arguments.code), "\n", "
", "ALL"),"\t","    ","ALL")#</span>

var character = "";
var thisLine=arguments.line;
var i = 0;
var endtagPos = 0;
var startAttributePos = 0;
var keywordskip = 0;

if (variables.isOneLineComment) endOneLineComment();

for (i=0; i LT thisLine.length(); i=i+1)
{
character=thisLine.charAt(javacast('int',i));
if (character EQ ' {
if (variables.isCFScript AND NOT variables.isValue)
endCFScript();
if (regionMatches(thisLine, 1, i+1, "!--", 0, 3))
{
if (regionMatches(thisLine, 1, i+4, "-", 0, 1))
{
startComment("CF");
} else {
startComment("HTML");
}
} else {
if (regionMatches(thisLine, 1, i+1, "CF", 0, 2) OR regionMatches(thisLine, 1, i+1, "/CF", 0, 3))
{
startTag("CF");
if (regionMatches(thisLine, 1, i+3, "SET", 0, 3) AND NOT regionMatches(thisLine, 1, i+6, "T", 0, 1)) // CFSET Tag
{
variables.buffer.append(thisLine.substring(javacast('int',i+1), javacast('int',i+6)));
i=i+5;
startCFSET();
}
else if (regionMatches(thisLine, 1, i+3, "SCRIPT&gt;", 0, 6)) // CFSCRIPT TAG
{
variables.buffer.append(thisLine.substring(javacast('int',i+1), javacast('int',i+9)) &amp; "&gt;");
i=i+9;
startCFScript();
}
else if (regionMatches(thisLine, 1, i+3, "QUERY", 0, 5)) // START CFQUERY TAG
{ // TODO: This sets the value color immediately to match SQL values including the CFQuery tag...
variables.isCFQueryTag = true;
} else if (regionMatches(thisLine, 1, i+4, "QUERY", 0, 5)) // END CFQUERY TAG
{
variables.isCFQueryTag = false;
endSQL();
}
}
else if (
regionMatches(thisLine, 1, i+1, "TA", 0, 2) OR
regionMatches(thisLine, 1, i+1, "/TA", 0, 3) OR
regionMatches(thisLine, 1, i+1, "TB", 0, 2) OR
regionMatches(thisLine, 1, i+1, "/TB", 0, 3) OR
regionMatches(thisLine, 1, i+1, "TD", 0, 2) OR
regionMatches(thisLine, 1, i+1, "/TD", 0, 3) OR
regionMatches(thisLine, 1, i+1, "TF", 0, 2) OR
regionMatches(thisLine, 1, i+1, "/TF", 0, 3) OR
regionMatches(thisLine, 1, i+1, "TH", 0, 2) OR
regionMatches(thisLine, 1, i+1, "/TH", 0, 3) OR
regionMatches(thisLine, 1, i+1, "TR", 0, 2) OR
regionMatches(thisLine, 1, i+1, "/TR", 0, 3)
) // HTML TABLE
{
startTag("HTMLTABLES");
}
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
// TODO: Do separate syntax highlighting for stuff inside style
{
startTag("HTMLSTYLES");
}
else if (
regionMatches(thisLine, 1, i+1, "FORM", 0, 4) OR
regionMatches(thisLine, 1, i+1, "/FORM", 0, 5) OR
regionMatches(thisLine, 1, i+1, "INPUT", 0, 5) OR
regionMatches(thisLine, 1, i+1, "/INPUT", 0, 5) OR
regionMatches(thisLine, 1, i+1, "TEXT", 0, 4) OR
regionMatches(thisLine, 1, i+1, "/TEXT", 0, 5) OR
regionMatches(thisLine, 1, i+1, "SELECT", 0, 6) OR
regionMatches(thisLine, 1, i+1, "/SELECT", 0, 7) OR
regionMatches(thisLine, 1, i+1, "OPT", 0, 3) OR
regionMatches(thisLine, 1, i+1, "/OPT", 0, 3)
)
{
startTag("HTMLFORMS");
}
else if (
regionMatches(thisLine, 1, i+1, "MX:", 0, 3) OR
regionMatches(thisLine, 1, i+1, "/MX:", 0, 4)
)
{
if (regionMatches(thisLine, 1, i+4, "SCRIPT&gt;", 0, 6)) // MX:SCRIPT TAG
{
startTag("ACTIONSCRIPTTAG");
variables.buffer.append(thisLine.substring(javacast('int',i+1), javacast('int',i+10)) &amp; "&gt;");
i=i+10;
startActionscript();
} else if (regionMatches(thisLine, 1, i+5, "SCRIPT&gt;", 0, 6)) // END MX:SCRIPT TAG
{
endActionscript();
startTag("ACTIONSCRIPTTAG");
variables.buffer.append(thisLine.substring(javacast('int',i+1), javacast('int',i+11)));
i=i+12;
endTag();
} else {
startTag("MXML");
startAttributePos=find(' ',thisLine,i+1); //start finding the next space from current position
endtagPos=find('&gt;',thisLine,i+1); //start finding the end tag from current position
if (startAttributePos neq 0) { // start attribute colors
variables.buffer.append(thisLine.substring(javacast('int',i+1), javacast('int',startAttributePos)));
i=startAttributePos-1;
startMXMLTag();
} else {
if (endtagPos neq 0) { // found &gt;
variables.buffer.append(thisLine.substring(javacast('int',i+1), javacast('int',endtagPos-1)));
i=i+endtagPos;
variables.buffer.append("&gt;");
endHighlight();
}
}
}
} else {
if (variables.isActionscript or variables.isSQL) {
variables.buffer.append("&lt;");
} else {
startTag("HTML");
}
}
}
}
else if (character EQ '&gt;')
{
if (variables.isCommented AND regionMatches(thisLine, 1, i-2, "--", 0, 2))
{
if (regionMatches(thisLine, 1, i-3, "-", 0, 1))
{
endComment("CF");
} else {
endComment("HTML");
}
} else {
if (variables.isCFSETTag) {
endCFSET();
} else if (variables.isActionscript) {
//This is where a CDATA for AS ends
variables.buffer.append("&gt;");
} else if (variables.isSQL) {
variables.buffer.append("&gt;");
} else if (variables.isCFQueryTag) {
endTag();
startSQL();
} else if (variables.isMXML) {
endMXMLTag();
} else {
endTag();
}
}
}
else if (character EQ '"')
{
if (variables.isTag OR variables.isCFScript OR variables.isActionscript)
{
if (NOT variables.isValue) {
startValue();
variables.buffer.append('"');
} else {
variables.buffer.append('"');
endValue();
}
} else {
variables.buffer.append('"');
}
}
else if (character EQ '{')
{
startBind();
variables.buffer.append("{");
endBind();
}
else if (character EQ '}')
{
startBind();
variables.buffer.append("}");
endBind();
}
else if (character EQ '/')
{
if ((variables.isCFScript OR variables.isActionscript) AND regionMatches(thisLine, 1, i+1, "/", 0, 1) AND NOT variables.isCommented)
{
if (variables.isActionscript) {
startOneLineComment("MXMLCOMMENT");
variables.buffer.append("/");
} else {
startOneLineComment("HTMLCOMMENT");
variables.buffer.append("/");
}
}
else if (variables.isCommented)
{
if (regionMatches(thisLine, 1, i-1, "*", 0, 1))
{
endComment("SCRIPT");
} else {
variables.buffer.append("/");
}
} else {
if (regionMatches(thisLine, 1, i+1, "*", 0, 1))
{
startComment("SCRIPT");
} else {
variables.buffer.append("/");
}
}
}
else if (variables.isSQL AND character EQ '-')
{
if (regionMatches(thisLine, 1, i+1, "-", 0, 1) AND NOT variables.isCommented)
{
startOneLineComment("SQLCOMMENT");
variables.buffer.append("-");
} else {
variables.buffer.append("-");
}
}
else if (variables.isSQL AND character EQ "'" AND NOT variables.isCommented)
{
if (NOT variables.isValue) {
startValue();
variables.buffer.append("'");
} else {
variables.buffer.append("'");
endValue();
}
}

// straight up replacements
else if (character EQ '\t' OR character EQ ' ')
{
variables.buffer.append("    ");
}
else if (character EQ ' ')
{
variables.buffer.append(" ");
}
else if (character EQ '&amp;')
{
if (regionMatches(thisLine, 1, i+1, "##", 0, 1)) {
variables.buffer.append("&amp;");
} else {
variables.buffer.append("&amp;");
}
} else {
if (not variables.isCommented AND not variables.isValue and (i eq 0 OR NOT listcontainsnocase('a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,@', thisLine.substring(javacast('int',i-1),javacast('int',i))))) {
keywordskip = 0;
// would like this to be much more generic rather than checking "is"
if (variables.isActionscript) {
keywordskip = keywordsearch(thisLine,i,"Actionscript");
} else if (variables.isCFscript or variables.isCFSetTag) {
keywordskip = keywordsearch(thisLine,i,"CFscript");
} else if (variables.isSQL) {
keywordskip = keywordsearch(thisLine,i,"sql");
}
if (keywordskip) {
i = i + keywordskip;
} else {
variables.buffer.append(character.toString());
}
} else {
variables.buffer.append(character.toString());
}
}
}
variables.buffer.append("
");

startHighlight(type);
variables.isOneLineComment=true;
variables.isCommented=true;

endHighlight();
variables.isOneLineComment=false;
variables.isCommented=false;

if (type EQ "CF") {
startHighlight("CFCOMMENT");
variables.buffer.append("&lt;");
} else if (type EQ "HTML") {
if (variables.isMXML) {
startHighlight("MXMLCOMMENT");
} else {
startHighlight("HTMLCOMMENT");
}
variables.buffer.append("&lt;");
} else {
if (variables.isActionscript) {
startHighlight("ACTIONSCRIPTCOMMENT");
} else if (variables.isSQL) {
startHighlight("SQLCOMMENT");
} else {
startHighlight("CFSCRIPTCOMMENT");
}
variables.buffer.append("/");
}
variables.isCommented=true;

if (type EQ "SCRIPT") {
variables.buffer.append("/");
} else {
variables.buffer.append("&gt;");
}
endHighlight();
variables.isCommented=false;

if (NOT variables.isCommented AND NOT variables.isValue) {
if (type EQ "CF") {
startHighlight("CFTAG");
} else if (type EQ "HTMLSTYLES") {
startHighlight("HTMLSTYLES");
} else if (type EQ "HTMLTABLES") {
startHighlight("HTMLTABLES");
} else if (type EQ "HTMLFORMS") {
startHighlight("HTMLFORMS");
} else if (type EQ "MXML") {
startHighlight("MXML");
} else if (type EQ "ACTIONSCRIPTTAG") {
startHighlight("ACTIONSCRIPTTAG");
} else { // type is HTML
startHighlight("HTML");
}
variables.isTag=true;
}
variables.buffer.append("&lt;");

variables.buffer.append("&gt;");
if (NOT variables.isCommented AND NOT variables.isValue) {
endHighlight();
variables.isTag=false;
}

if (NOT variables.isCommented) {
if (variables.isCFSETTag OR variables.isCFScript) {
startHighlight("CFSCRIPTVALUE");
} else if (variables.isActionscript) {
startHighlight("ACTIONSCRIPTVALUE");
} else if (variables.isMXML) {
startHighlight("MXMLVALUE");
} else if (variables.isSQL) {
startHighlight("SQLVALUE");
} else {
startHighlight("VALUE");
}
variables.isValue=true;
}

if (NOT variables.isCommented) {
endHighlight();
variables.isValue=false;
}

if (NOT variables.isCommented) {
startHighlight("BIND");
}

if (NOT variables.isCommented) {
endHighlight();
}

if (NOT variables.isCommented) {
startHighlight("CFSET");
variables.isCFSETTag=true;
}

if (NOT variables.isCommented) {
endHighlight();
variables.buffer.append("&gt;");
endHighlight();
variables.isCFSETTag=false;
} else {
variables.buffer.append("&gt;");
}

if (NOT variables.isCommented) {
startHighlight("MXMLATTRIBUTES");
// TODO: Add in MXML Value colors.
// setStyle("VALUE","color:##900");
variables.isMXML=true;
}

if (NOT variables.isCommented) {
endHighlight();
variables.buffer.append("&gt;");
endHighlight();
// TODO: Add in MXML Value colors.
// setStyle("VALUE","color:##0000CC");
variables.isMXML=false;
} else {
variables.buffer.append("&gt;");
}

if (NOT variables.isCommented) {
endHighlight();
startHighlight("CFSCRIPT");
variables.isCFScript=true;
}

if (NOT variables.isCommented) {
endHighlight();
variables.isCFScript=false;
}

if (NOT variables.isCommented) {
endHighlight();
startHighlight("ACTIONSCRIPT");
variables.isActionscript=true;
}

if (NOT variables.isCommented) {
variables.isActionscript=false;
}

if (NOT variables.isCommented) {
variables.isSQL=true;
}

if (NOT variables.isCommented) {
variables.isSQL=false;
}
<!--- configuration methods --->

I’d like to extend a special thanks to Ray Camden & Jason Delmore for their prompt replies and expeditious changes … Much Appreciated.

{ 0 comments… add one now }

Leave a Comment

Previous post:

Next post: