Colored code samples - now with sample code!
Not only I 'believe' in the power of modern so-called RAD IDEs, especially in intellisense, support of commenting and use of those comments as internal help in the place of code editing (and more intellisense ;)), code navigation through definitions, but I also believe that colored code is easier to read, and easier to understand it faster.
So when I've started this programming blog, I've liked to give code samples not only formatted right, but colored. But I think it would have been a bit lame to parse from clear code text, because that's how code usually is, and write out html (even with fancy spans and divs and css) (and ruling out the method when I type all the color codes also), I don't want to do this process with every update involving code. And since I have no control how these hosted pages are parsed out, I couldn't automatize such a process.
So I've decided to write a little javascript hack, which will colorize parts on a web page marked as code area.
The javascript colorization itself won't be perfect, because for now I've made the colorizer code to work with C (and D, more on later), and maybe C# keywords...
The entry point is placed onto the page onload event:
<body onload="format()">
function format()
{
var c = document.getElementsByName('code');
if(c != undefined)
{
if (c.length == undefined)
{
formatcode(c);
}
else
{
for(i=0; i<c.length; i++)
{
formatcode(c[i]);
}
}
}
}
// keyword
kwS = "<font color='#3333FF'>";
kwE = "</font>";
// comment block
commS = "<font color='#339900'>";
commE = "</font>";
// string literal
strS = "<font color='#FF0000'>";
strE = "</font>";
function formatcode(c)
{
ct = c.innerHTML;
//we're just doing a mass search and replace
// replacing blogger included br-s
ct = ct.replace(/<br>/g, "\n");
ct = ct.replace(/<BR>/g, "\n");
// keywords
ct = ct.replace(/((\s|\()(break|case|char|class|
default|else|for|foreach|if|int|import|
public|private|return|struct|switch|typeof|
void(\s|;|:|\[|\())/g, kwS + '$1' + kwE );
ct = ct.replace(/(>)(break|case|char|class|
default|else|for|foreach|if|int|import|
public|private|return|struct|switch|typeof|
void)(\s|;|:|\[|\(<)/g, '$1' + kwS + '$2$3'
+ kwE );
// string literals " "
ct = ct.replace(/(\"[^\"$]*\")/g, strS +
'$1' + strE );
// /* */ block comment
ct = ct.replace(/\/\* /g, commS + "/* ");
ct = ct.replace(/ \*\//g, " */" + commE);
ct = ct.replace(/\n\*\//g, "\n*/" + commE);
// // style comment
ct = ct.replace(/\/\/ /g, commS + "// ");
ct = ct.replace(/(\/\/ .*)($)/gm, '$1' + commE);
c.innerHTML = ct;
}
0 Comments:
Post a Comment
<< Home