Sunday, December 30, 2007

Syntax Highlighting

December 30, 2007 Posted by Jason No comments

[Edit: The slowdown caused by parsing the main chunk of javascript function was getting out of hand so i temporarily removed formatting from this block. It will now look terribly ugly but i plan to update the formatting mechanism at the weekend to allow collapsed text to be formatted on expansion rather than page-load.

I was recently asked about the code syntax highlighting on my blog. I can't take any credit for it (except for the little time it took me to mash somebody else's code into a single page. The code itself is courtesy of Alex Gorbatchev and is available in it's entirety at under the terms o the GNU Lesser General Public License.

Please beware (if you haven't noticed already) that highlighting big blocks of code requires a relatively large amount of client-side processing, thus slowing the page when loading and causing a little havoc. This, alas, is the nature of the beast. This syntax highlighter allows for a collapsed mode where textareas can be collapsed, thus making things a little neater. Perhaps a nice addition would be deferring the syntax highlighting for a particular region based on whether it is hidden or not....

A syntax highlighter written in Javascript, it allows keywords to be highlighted by creating a textarea, specifying its CSS class (based on the language you wish to highlight) and popping your code into said textarea as follows:

The zip archive download from the above site includes multiple .js and .css files. Since blogger (and most other blogs) only allow for a single HTML page, it was necessary to mash the code from all of these source files together and insert them into this solitary page. I did so as follows (using version 1.5.1):

  1. Compiled all of the Javascript Functions into one script region, inserted into the <HEAD> of my document.
  2. Compiled all of the CSS attributes etc. into a single style region, also added to the <HEAD> of my document
  3. Created another Javascript area at the very bottom of my document right before the </BODY> tag to call the relevant functions and begin syntax highlighting.

    The result is, as can be seen below, some beautifully highlighted code. Thanks to Alex Gorbatchev for his wonderful work.

  1. Javascript Functions


CopyToClipboard: {
label: 'copy to clipboard',
check: function() { return window.clipboardData != null || != null; },
func: function(sender, highlighter)
var code =

window.clipboardData.setData('text', code);
else if( != null)
var flashcopier = highlighter.flashCopier;

if(flashcopier == null)
flashcopier = document.createElement('div');
highlighter.flashCopier = flashcopier;

FlashVars="clipboard='+encodeURIComponent(code)+'" width="0" height="0" type="application/x-shockwave-flash">';

PrintSource: {
label: 'print',
func: function(sender, highlighter)
var iframe = document.createElement('IFRAME');
var doc = null; = 'position:absolute;width:0px;height:0px;left:-500px;top:-500px;';

doc = iframe.contentWindow.document;, window.document);
' + highlighter.div.innerHTML + '




About: {
label: '?',
func: function(highlighter)
var wnd ='', '_blank', 'dialog,width=300,height=150,scrollbars=0');
var doc = wnd.document;, window.document);

}; = function(highlighter)
var div = document.createElement('DIV');

div.className = 'tools';

for(var name in
var cmd =[name];

if(cmd.check != null && !cmd.check(highlighter))

div.innerHTML += '' + cmd.label + '';

return div;
} = function(name, sender)
var n = sender;

while(n != null && n.className.indexOf('dp-highlighter') == -1)
n = n.parentNode;

if(n != null)[name].func(sender, n.highlighter);
} = function(destDoc, sourceDoc)
var links = sourceDoc.getElementsByTagName('link');

for(var i = 0; i < links.length; i++)
if(links[i].rel.toLowerCase() == 'stylesheet')
} = function(str)
return ( == true) ? str.replace(/|<br\s*\/?>/gi, '\n') : str;
} = {
MultiLineCComments : new RegExp('/\\*[\\s\\S]*?\\*/', 'gm'),
SingleLineCComments : new RegExp('//.*$', 'gm'),
SingleLinePerlComments : new RegExp('#.*$', 'gm'),
DoubleQuotedString : new RegExp('"(?:\\.|(\\\\\\")|[^\\""\\n])*"','g'),
SingleQuotedString : new RegExp("'(?:\\.|(\\\\\\')|[^\\''\\n])*'", 'g')
}; = function(value, index, css)
this.value = value;
this.index = index;
this.length = value.length;
this.css = css;
} = function()
this.noGutter = false;
this.addControls = true;
this.collapse = false;
this.tabsToSpaces = true;
this.wrapColumn = 80;
this.showColumns = true;
} = function(m1, m2)
// sort matches by index first
if(m1.index < m2.index)
return -1;
else if(m1.index > m2.index)
return 1;
if(m1.length < m2.length)
return -1;
else if(m1.length > m2.length)
return 1;
return 0;
} = function(name)
var result = document.createElement(name);
result.highlighter = this;
return result;
} = function(regex, css)
var index = 0;
var match = null;

while((match = regex.exec(this.code)) != null)
this.matches[this.matches.length] = new[0], match.index, css);
} = function(str, css)
if(str == null || str.length == 0)

var span = this.CreateElement('SPAN');

str = str.replace(/ /g, ' ');
str = str.replace(/str = str.replace(/\n/gm, ' 

if(css != null)
var lines = str.split(' 

for(var i = 0; i < lines.length; i++)
span = this.CreateElement('SPAN');
span.className = css;
span.innerHTML = lines[i];


if(i + 1 < lines.length)
span.className = css;
span.innerHTML = str;
span.innerHTML = str;
} = function(match)
if(match == null || match.length == 0)
return false;

for(var i = 0; i < this.matches.length; i++)
var c = this.matches[i];

if(c == null)

if((match.index > c.index) && (match.index < c.index + c.length))
return true;

return false;
} = function()
for(var i = 0; i < this.regexList.length; i++)
this.GetMatches(this.regexList[i].regex, this.regexList[i].css);
} = function(code)
var lines = code.split('\n');
var result = '';
var tabSize = 4;
var tab = '\t';

function InsertSpaces(line, pos, count)
var left = line.substr(0, pos);
var right = line.substr(pos + 1, line.length); // pos + 1 will get rid of the tab
var spaces = '';

for(var i = 0; i < count; i++)
spaces += ' ';

return left + spaces + right;

function ProcessLine(line, tabSize)
if(line.indexOf(tab) == -1)
return line;

var pos = 0;

while((pos = line.indexOf(tab)) != -1)
var spaces = tabSize - pos % tabSize;

line = InsertSpaces(line, pos, spaces);

return line;

for(var i = 0; i < lines.length; i++)
result += ProcessLine(lines[i], tabSize) + '\n';

return result;
} = function()
var html = this.div.innerHTML.replace(/<(br)\/?>/gi, '\n');
var lines = html.split('\n');

if(this.addControls == true);

var div = this.CreateElement('div');
var columns = this.CreateElement('div');
var showEvery = 10;
var i = 1;

while(i <= 150)
if(i % showEvery == 0)
div.innerHTML += i;
i += (i + '').length;
div.innerHTML += '·';

columns.className = 'columns';

for(var i = 0, lineIndex = this.firstLine; i < lines.length - 1; i++, lineIndex++)
var li = this.CreateElement('LI');
var span = this.CreateElement('SPAN');

// uses .line1 and .line2 css styles for alternating lines
li.className = (i % 2 == 0) ? 'alt' : '';
span.innerHTML = lines[i] + ' ';


this.div.innerHTML = '';
} = function(code)
function Trim(str)
return str.replace(/^\s*(.*?)[\s\n]*$/g, '$1');

function Chop(str)
return str.replace(/\n*$/, '').replace(/^\n*/, '');

function Unindent(str)
var lines ='\n');
var indents = new Array();
var regex = new RegExp('^\\s*', 'g');
var min = 1000;

for(var i = 0; i < lines.length && min > 0; i++)
if(Trim(lines[i]).length == 0)

var matches = regex.exec(lines[i]);

if(matches != null && matches.length > 0)
min = Math.min(matches[0].length, min);

if(min > 0)
for(var i = 0; i < lines.length; i++)
lines[i] = lines[i].substr(min);

return lines.join('\n');

function Copy(string, pos1, pos2)
return string.substr(pos1, pos2 - pos1);

var pos = 0;

if(code == null)
code = '';

this.originalCode = code;
this.code = Chop(Unindent(code));
this.div = this.CreateElement('DIV'); = this.CreateElement('DIV');
this.ol = this.CreateElement('OL');
this.matches = new Array();

this.div.className = 'dp-highlighter';
this.div.highlighter = this; = 'bar';

this.ol.start = this.firstLine;

if(this.CssClass != null)
this.ol.className = this.CssClass;

this.div.className += ' collapsed';

this.div.className += ' nogutter';

if(this.tabsToSpaces == true)
this.code = this.ProcessSmartTabs(this.code);


if(this.matches.length == 0)
this.AddBit(this.code, null);

this.matches = this.matches.sort(;

for(var i = 0; i < this.matches.length; i++)
this.matches[i] = null;

for(var i = 0; i < this.matches.length; i++)
var match = this.matches[i];

if(match == null || match.length == 0)

this.AddBit(Copy(this.code, pos, match.index), null);
this.AddBit(match.value, match.css);

pos = match.index + match.length;

this.AddBit(this.code.substr(pos), null);

} = function(str)
return '\\b' + str.replace(/ /g, '\\b|\\b') + '\\b';
} = function()
{ = true;
} = function(name, showGutter /* optional */, showControls /* optional */, collapseAll /* optional */, firstLine /* optional */, showColumns /* optional */)

function FindValue()
var a = arguments;

for(var i = 0; i < a.length; i++)
if(a[i] == null)

if(typeof(a[i]) == 'string' && a[i] != '')
return a[i] + '';

if(typeof(a[i]) == 'object' && a[i].value != '')
return a[i].value + '';

return null;

function IsOptionSet(value, list)
for(var i = 0; i < list.length; i++)
if(list[i] == value)
return true;

return false;

function GetOptionValue(name, list, defaultValue)
var regex = new RegExp('^' + name + '\\[(\\w+)\\]$', 'gi');
var matches = null;

for(var i = 0; i < list.length; i++)
if((matches = regex.exec(list[i])) != null)
return matches[1];

return defaultValue;

function FindTagsByName(list, name, tagName)
var tags = document.getElementsByTagName(tagName);

for(var i = 0; i < tags.length; i++)
if(tags[i].getAttribute('name') == name)

var elements = [];
var highlighter = null;
var registered = {};
var propertyName = 'innerHTML';

FindTagsByName(elements, name, 'pre');
FindTagsByName(elements, name, 'textarea');

if(elements.length == 0)

for(var brush in
var aliases =[brush].Aliases;

if(aliases == null)

for(var i = 0; i < aliases.length; i++)
registered[aliases[i]] = brush;

for(var i = 0; i < elements.length; i++)
var element = elements[i];
var options = FindValue(
element.attributes['class'], element.className,
element.attributes['language'], element.language
var language = '';

if(options == null)

options = options.split(':');

language = options[0].toLowerCase();

if(registered[language] == null)

highlighter = new[registered[language]](); = 'none';

highlighter.noGutter = (showGutter == null) ? IsOptionSet('nogutter', options) : !showGutter;
highlighter.addControls = (showControls == null) ? !IsOptionSet('nocontrols', options) : showControls;
highlighter.collapse = (collapseAll == null) ? IsOptionSet('collapse', options) : collapseAll;
highlighter.showColumns = (showColumns == null) ? IsOptionSet('showcolumns', options) : showColumns;

var headNode = document.getElementsByTagName('head')[0];
if(highlighter.Style && headNode)
var styleNode = document.createElement('style');
styleNode.setAttribute('type', 'text/css');

styleNode.styleSheet.cssText = highlighter.Style;
var textNode = document.createTextNode(highlighter.Style);


highlighter.firstLine = (firstLine == null) ? parseInt(GetOptionValue('firstline', options, 1)) : firstLine;


highlighter.source = element;

element.parentNode.insertBefore(highlighter.div, element);

* Code Syntax Highlighter for C++(Windows Platform).
* Version 0.0.2
* Copyright (C) 2006 Shin, YoungJin.
* This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General
* Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
* This library 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 library; if not, write to
* the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/ = function()
var datatypes =
'char bool short int __int32 __int64 __int8 __int16 long float double __wchar_t ' +
'clock_t _complex _dev_t _diskfree_t div_t ldiv_t _exception _EXCEPTION_POINTERS ' +
'FILE _finddata_t _finddatai64_t _wfinddata_t _wfinddatai64_t __finddata64_t ' +
'__wfinddata64_t _FPIEEE_RECORD fpos_t _HEAPINFO _HFILE lconv intptr_t ' +
'jmp_buf mbstate_t _off_t _onexit_t _PNH ptrdiff_t _purecall_handler ' +
'sig_atomic_t size_t _stat __stat64 _stati64 terminate_function ' +
'time_t __time64_t _timeb __timeb64 tm uintptr_t _utimbuf ' +
'va_list wchar_t wctrans_t wctype_t wint_t signed';

var keywords =
'break case catch class const __finally __exception __try ' +
'const_cast continue private public protected __declspec ' +
'default delete deprecated dllexport dllimport do dynamic_cast ' +
'else enum explicit extern if for friend goto inline ' +
'mutable naked namespace new noinline noreturn nothrow ' +
'register reinterpret_cast return selectany ' +
'sizeof static static_cast struct switch template this ' +
'thread throw true false try typedef typeid typename union ' +
'using uuid virtual void volatile whcar_t while';

this.regexList = [
{ regex:,css: 'comment' },
{ regex:,css: 'comment' },
{ regex:,css: 'string' },
{ regex:,css: 'string' },
{ regex: new RegExp('^ *#.*', 'gm'),css: 'preprocessor' },
{ regex: new RegExp(this.GetKeywords(datatypes), 'gm'),css: 'datatypes' },
{ regex: new RegExp(this.GetKeywords(keywords), 'gm'),css: 'keyword' }

this.CssClass = 'dp-cpp';
this.Style = '.dp-cpp .datatypes { color: #2E8B57; font-weight: bold; }';
} = new; = ['cpp', 'c', 'c++']; = function()
var keywords = 'abstract as base bool break byte case catch char checked class const ' +
'continue decimal default delegate do double else enum event explicit ' +
'extern false finally fixed float for foreach get goto if implicit in int ' +
'interface internal is lock long namespace new null object operator out ' +
'override params private protected public readonly ref return sbyte sealed set ' +
'short sizeof stackalloc static string struct switch this throw true try ' +
'typeof uint ulong unchecked unsafe ushort using virtual void while write-host Write-Host sleep Read-Host trap' +

this.regexList = [
// There's a slight problem with matching single line comments and figuring out
// a difference between // and ///. Using lookahead and lookbehind solves the
// problem, unfortunately JavaScript doesn't support lookbehind. So I'm at a
// loss how to translate that regular expression to JavaScript compatible one.
// { regex: new RegExp('(?// { regex: new RegExp('(?
{ regex:,css: 'comment' }, // one line comments
{ regex:,css: 'comment' }, // multiline comments
{ regex:,css: 'string' }, // strings
{ regex:,css: 'string' }, // strings
{ regex: new RegExp('^\\s*#.*', 'gm'),css: 'preprocessor' }, // preprocessor tags like #region and #endregion
{ regex: new RegExp(this.GetKeywords(keywords), 'gm'),css: 'keyword' } // c# keyword

this.CssClass = 'dp-c';
this.Style = '.dp-c .vars { color: #d00; }';
} = new; = ['c#', 'c-sharp', 'csharp']; = function()
var keywords = 'ascent azimuth background-attachment background-color background-image background-position ' +
'background-repeat background baseline bbox border-collapse border-color border-spacing border-style border-top ' +
'border-right border-bottom border-left border-top-color border-right-color border-bottom-color border-left-color ' +
'border-top-style border-right-style border-bottom-style border-left-style border-top-width border-right-width ' +
'border-bottom-width border-left-width border-width border cap-height caption-side centerline clear clip color ' +
'content counter-increment counter-reset cue-after cue-before cue cursor definition-src descent direction display ' +
'elevation empty-cells float font-size-adjust font-family font-size font-stretch font-style font-variant font-weight font ' +
'height letter-spacing line-height list-style-image list-style-position list-style-type list-style margin-top ' +
'margin-right margin-bottom margin-left margin marker-offset marks mathline max-height max-width min-height min-width orphans ' +
'outline-color outline-style outline-width outline overflow padding-top padding-right padding-bottom padding-left padding page ' +
'page-break-after page-break-before page-break-inside pause pause-after pause-before pitch pitch-range play-during position ' +
'quotes richness size slope src speak-header speak-numeral speak-punctuation speak speech-rate stemh stemv stress ' +
'table-layout text-align text-decoration text-indent text-shadow text-transform unicode-bidi unicode-range units-per-em ' +
'vertical-align visibility voice-family volume white-space widows width widths word-spacing x-height z-index';

var values = 'above absolute all always aqua armenian attr aural auto avoid baseline behind below bidi-override black blink block blue bold bolder '+
'both bottom braille capitalize caption center center-left center-right circle close-quote code collapse compact condensed '+
'continuous counter counters crop cross crosshair cursive dashed decimal decimal-leading-zero default digits disc dotted double '+
'embed embossed e-resize expanded extra-condensed extra-expanded fantasy far-left far-right fast faster fixed format fuchsia '+
'gray green groove handheld hebrew help hidden hide high higher icon inline-table inline inset inside invert italic '+
'justify landscape large larger left-side left leftwards level lighter lime line-through list-item local loud lower-alpha '+
'lowercase lower-greek lower-latin lower-roman lower low ltr marker maroon medium message-box middle mix move narrower '+
'navy ne-resize no-close-quote none no-open-quote no-repeat normal nowrap n-resize nw-resize oblique olive once open-quote outset '+
'outside overline pointer portrait pre print projection purple red relative repeat repeat-x repeat-y rgb ridge right right-side '+
'rightwards rtl run-in screen scroll semi-condensed semi-expanded separate se-resize show silent silver slower slow '+
'small small-caps small-caption smaller soft solid speech spell-out square s-resize static status-bar sub super sw-resize '+
'table-caption table-cell table-column table-column-group table-footer-group table-header-group table-row table-row-group teal '+
'text-bottom text-top thick thin top transparent tty tv ultra-condensed ultra-expanded underline upper-alpha uppercase upper-latin '+
'upper-roman url visible wait white wider w-resize x-fast x-high x-large x-loud x-low x-slow x-small x-soft xx-large xx-small yellow';

var fonts = '[mM]onospace [tT]ahoma [vV]erdana [aA]rial [hH]elvetica [sS]ans-serif [sS]erif';

this.regexList = [
{ regex:, css: 'comment' }, // multiline comments
{ regex:,css: 'string' }, // double quoted strings
{ regex:,css: 'string' }, // single quoted strings
{ regex: new RegExp('\\#[a-zA-Z0-9]{3,6}', 'g'),css: 'value' }, // html colors
{ regex: new RegExp('(-?\\d+)(\.\\d+)?(px|em|pt|\:|\%|)', 'g'), css: 'value' }, // sizes
{ regex: new RegExp('!important', 'g'),css: 'important' }, // !important
{ regex: new RegExp(this.GetKeywordsCSS(keywords), 'gm'),css: 'keyword' }, // keywords
{ regex: new RegExp(this.GetValuesCSS(values), 'g'),css: 'value' }, // values
{ regex: new RegExp(this.GetValuesCSS(fonts), 'g'),css: 'value' } // fonts

this.CssClass = 'dp-css';
this.Style = '.dp-css .value { color: black; }' +
'.dp-css .important { color: red; }'
} = function(str)
return '\\b([a-z_]|)' + str.replace(/ /g, '(?=:)\\b|\\b([a-z_\\*]|\\*|)') + '(?=:)\\b';
} = function(str)
return '\\b' + str.replace(/ /g, '(?!-)(?!:)\\b|\\b()') + '\:\\b';
} = new; = ['css'];

/* Delphi brush is contributed by Eddie Shipman */ = function()
var keywords = 'abs addr and ansichar ansistring array as asm begin boolean byte cardinal ' +
'case char class comp const constructor currency destructor div do double ' +
'downto else end except exports extended false file finalization finally ' +
'for function goto if implementation in inherited int64 initialization ' +
'integer interface is label library longint longword mod nil not object ' +
'of on or packed pansichar pansistring pchar pcurrency pdatetime pextended ' +
'pint64 pointer private procedure program property pshortstring pstring ' +
'pvariant pwidechar pwidestring protected public published raise real real48 ' +
'record repeat set shl shortint shortstring shr single smallint string then ' +
'threadvar to true try type unit until uses val var varirnt while widechar ' +
'widestring with word write writeln xor';

this.regexList = [
{ regex: new RegExp('\\(\\*[\\s\\S]*?\\*\\)', 'gm'),css: 'comment' }, // multiline comments (* *)
{ regex: new RegExp('{(?!\\$)[\\s\\S]*?}', 'gm'), css: 'comment' }, // multiline comments { }
{ regex:, css: 'comment' }, // one line
{ regex:, css: 'string' }, // strings
{ regex: new RegExp('\\{\\$[a-zA-Z]+ .+\\}', 'g'), css: 'directive' }, // Compiler Directives and Region tags
{ regex: new RegExp('\\b[\\d\\.]+\\b', 'g'),css: 'number' }, // numbers 12345
{ regex: new RegExp('\\$[a-zA-Z0-9]+\\b', 'g'),css: 'number' }, // numbers $F5D3
{ regex: new RegExp(this.GetKeywords(keywords), 'gm'),css: 'keyword' } // keyword

this.CssClass = 'dp-delphi';
this.Style = '.dp-delphi .number { color: blue; }' +
'.dp-delphi .directive { color: #008284; }' +
'.dp-delphi .vars { color: #000; }';
} = new; = ['delphi', 'pascal']; = function()
var keywords = 'abstract assert boolean break byte case catch char class const ' +
'continue default do double else enum extends ' +
'false final finally float for goto if implements import ' +
'instanceof int interface long native new null ' +
'package private protected public return ' +
'short static strictfp super switch synchronized this throw throws true ' +
'transient try void volatile while';

this.regexList = [
{ regex:,css: 'comment' },
{ regex:,css: 'comment' },
{ regex:,css: 'string' },
{ regex:,css: 'string' },
{ regex: new RegExp('\\b([\\d]+(\\.[\\d]+)?|0x[a-f0-9]+)\\b', 'gi'), css: 'number' },
{ regex: new RegExp('(?!\\@interface\\b)\\@[\\$\\w]+\\b', 'g'),css: 'annotation' },
{ regex: new RegExp('\\@interface\\b', 'g'),css: 'keyword' },
{ regex: new RegExp(this.GetKeywords(keywords), 'gm'),css: 'keyword' }

this.CssClass = 'dp-j';
this.Style = '.dp-j .annotation { color: #646464; }' +
'.dp-j .number { color: #C00000; }';
} = new; = ['java']; = function()
var keywords = 'abstract boolean break byte case catch char class const continue debugger ' +
'default delete do double else enum export extends false final finally float ' +
'for function goto if implements import in instanceof int interface long native ' +
'new null package private protected public return short static super switch ' +
'synchronized this throw throws transient true try typeof var void volatile while with';

this.regexList = [
{ regex:,css: 'comment' },
{ regex:,css: 'comment' },
{ regex:,css: 'string' },
{ regex:,css: 'string' },
{ regex: new RegExp('^\\s*#.*', 'gm'),css: 'preprocessor' },
{ regex: new RegExp(this.GetKeywords(keywords), 'gm'),css: 'keyword' }

this.CssClass = 'dp-c';
} = new; = ['js', 'jscript', 'javascript']; = function()
var funcs = 'abs acos acosh addcslashes addslashes ' +
'array_change_key_case array_chunk array_combine array_count_values array_diff '+
'array_diff_assoc array_diff_key array_diff_uassoc array_diff_ukey array_fill '+
'array_filter array_flip array_intersect array_intersect_assoc array_intersect_key '+
'array_intersect_uassoc array_intersect_ukey array_key_exists array_keys array_map '+
'array_merge array_merge_recursive array_multisort array_pad array_pop array_product '+
'array_push array_rand array_reduce array_reverse array_search array_shift '+
'array_slice array_splice array_sum array_udiff array_udiff_assoc '+
'array_udiff_uassoc array_uintersect array_uintersect_assoc '+
'array_uintersect_uassoc array_unique array_unshift array_values array_walk '+
'array_walk_recursive atan atan2 atanh base64_decode base64_encode base_convert '+
'basename bcadd bccomp bcdiv bcmod bcmul bindec bindtextdomain bzclose bzcompress '+
'bzdecompress bzerrno bzerror bzerrstr bzflush bzopen bzread bzwrite ceil chdir '+
'checkdate checkdnsrr chgrp chmod chop chown chr chroot chunk_split class_exists '+
'closedir closelog copy cos cosh count count_chars date decbin dechex decoct '+
'deg2rad delete ebcdic2ascii echo empty end ereg ereg_replace eregi eregi_replace error_log '+
'error_reporting escapeshellarg escapeshellcmd eval exec exit exp explode extension_loaded '+
'feof fflush fgetc fgetcsv fgets fgetss file_exists file_get_contents file_put_contents '+
'fileatime filectime filegroup fileinode filemtime fileowner fileperms filesize filetype '+
'floatval flock floor flush fmod fnmatch fopen fpassthru fprintf fputcsv fputs fread fscanf '+
'fseek fsockopen fstat ftell ftok getallheaders getcwd getdate getenv gethostbyaddr gethostbyname '+
'gethostbynamel getimagesize getlastmod getmxrr getmygid getmyinode getmypid getmyuid getopt '+
'getprotobyname getprotobynumber getrandmax getrusage getservbyname getservbyport gettext '+
'gettimeofday gettype glob gmdate gmmktime ini_alter ini_get ini_get_all ini_restore ini_set '+
'interface_exists intval ip2long is_a is_array is_bool is_callable is_dir is_double '+
'is_executable is_file is_finite is_float is_infinite is_int is_integer is_link is_long '+
'is_nan is_null is_numeric is_object is_readable is_real is_resource is_scalar is_soap_fault '+
'is_string is_subclass_of is_uploaded_file is_writable is_writeable mkdir mktime nl2br '+
'parse_ini_file parse_str parse_url passthru pathinfo readlink realpath rewind rewinddir rmdir '+
'round str_ireplace str_pad str_repeat str_replace str_rot13 str_shuffle str_split '+
'str_word_count strcasecmp strchr strcmp strcoll strcspn strftime strip_tags stripcslashes '+
'stripos stripslashes stristr strlen strnatcasecmp strnatcmp strncasecmp strncmp strpbrk '+
'strpos strptime strrchr strrev strripos strrpos strspn strstr strtok strtolower strtotime '+
'strtoupper strtr strval substr substr_compare';

var keywords = 'and or xor __FILE__ __LINE__ array as break case ' +
'cfunction class const continue declare default die do else ' +
'elseif empty enddeclare endfor endforeach endif endswitch endwhile ' +
'extends for foreach function include include_once global if ' +
'new old_function return static switch use require require_once ' +
'var while __FUNCTION__ __CLASS__ ' +
'__METHOD__ abstract interface public implements extends private protected throw';

this.regexList = [
{ regex:, css: 'comment' }, // one line comments
{ regex:, css: 'comment' }, // multiline comments
{ regex:, css: 'string' }, // double quoted strings
{ regex:, css: 'string' }, // single quoted strings
{ regex: new RegExp('\\$\\w+', 'g'), css: 'vars' }, // variables
{ regex: new RegExp(this.GetKeywords(funcs), 'gmi'), css: 'func' }, // functions
{ regex: new RegExp(this.GetKeywords(keywords), 'gm'), css: 'keyword' } // keyword

this.CssClass = 'dp-c';
} = new; = ['php'];

/* Python 2.3 syntax contributed by Gheorghe Milas */ = function()
var keywords = 'and assert break class continue def del elif else ' +
'except exec finally for from global if import in is ' +
'lambda not or pass print raise return try yield while';

var special = 'None True False self cls class_'

this.regexList = [
{ regex:, css: 'comment' },
{ regex: new RegExp("^\\s*@\\w+", 'gm'), css: 'decorator' },
{ regex: new RegExp("(['\"]{3})([^\\1])*?\\1", 'gm'), css: 'comment' },
{ regex: new RegExp('"(?!")(?:\\.|\\\\\\"|[^\\""\\n\\r])*"', 'gm'), css: 'string' },
{ regex: new RegExp("'(?!')*(?:\\.|(\\\\\\')|[^\\''\\n\\r])*'", 'gm'), css: 'string' },
{ regex: new RegExp("\\b\\d+\\.?\\w*", 'g'), css: 'number' },
{ regex: new RegExp(this.GetKeywords(keywords), 'gm'), css: 'keyword' },
{ regex: new RegExp(this.GetKeywords(special), 'gm'), css: 'special' }

this.CssClass = 'dp-py';
this.Style = '.dp-py .builtins { color: #ff1493; }' +
'.dp-py .magicmethods { color: #808080; }' +
'.dp-py .exceptions { color: brown; }' +
'.dp-py .types { color: brown; font-style: italic; }' +
'.dp-py .commonlibs { color: #8A2BE2; font-style: italic; }';
} = new; = ['py', 'python'];

/* Ruby 1.8.4 syntax contributed by Erik Peterson */ = function()
var keywords = 'alias and BEGIN begin break case class def define_method defined do each else elsif ' +
'END end ensure false for if in module new next nil not or raise redo rescue retry return ' +
'self super then throw true undef unless until when while yield';

var builtins = 'Array Bignum Binding Class Continuation Dir Exception FalseClass File::Stat File Fixnum Fload ' +
'Hash Integer IO MatchData Method Module NilClass Numeric Object Proc Range Regexp String Struct::TMS Symbol ' +
'ThreadGroup Thread Time TrueClass'

this.regexList = [
{ regex:, css: 'comment' }, // one line comments
{ regex:, css: 'string' }, // double quoted strings
{ regex:, css: 'string' }, // single quoted strings
{ regex: new RegExp(':[a-z][A-Za-z0-9_]*', 'g'), css: 'symbol' }, // symbols
{ regex: new RegExp('(\\$|@@|@)\\w+', 'g'), css: 'variable' }, // $global, @instance, and @@class variables
{ regex: new RegExp(this.GetKeywords(keywords), 'gm'), css: 'keyword' }, // keywords
{ regex: new RegExp(this.GetKeywords(builtins), 'gm'), css: 'builtin' } // builtins

this.CssClass = 'dp-rb';
this.Style = '.dp-rb .symbol { color: #a70; }' +
'.dp-rb .variable { color: #a70; font-weight: bold; }';
} = new; = ['ruby', 'rails', 'ror']; = function()
var funcs = 'abs avg case cast coalesce convert count current_timestamp ' +
'current_user day isnull left lower month nullif replace right ' +
'session_user space substring sum system_user upper user year';

var keywords = 'absolute action add after alter as asc at authorization begin bigint ' +
'binary bit by cascade char character check checkpoint close collate ' +
'column commit committed connect connection constraint contains continue ' +
'create cube current current_date current_time cursor database date ' +
'deallocate dec decimal declare default delete desc distinct double drop ' +
'dynamic else end end-exec escape except exec execute false fetch first ' +
'float for force foreign forward free from full function global goto grant ' +
'group grouping having hour ignore index inner insensitive insert instead ' +
'int integer intersect into is isolation key last level load local max min ' +
'minute modify move name national nchar next no numeric of off on only ' +
'open option order out output partial password precision prepare primary ' +
'prior privileges procedure public read real references relative repeatable ' +
'restrict return returns revoke rollback rollup rows rule schema scroll ' +
'second section select sequence serializable set size smallint static ' +
'statistics table temp temporary then time timestamp to top transaction ' +
'translation trigger true truncate uncommitted union unique update values ' +
'varchar varying view when where with work';

var operators = 'all and any between cross in join like not null or outer some';

this.regexList = [
{ regex:, css: 'string' }, // double quoted strings
{ regex:, css: 'string' }, // single quoted strings
{ regex: new RegExp(this.GetKeywords(funcs), 'gmi'), css: 'func' }, // functions
{ regex: new RegExp(this.GetKeywords(operators), 'gmi'), css: 'op' }, // operators and such
{ regex: new RegExp(this.GetKeywords(keywords), 'gmi'), css: 'keyword' } // keyword

this.CssClass = 'dp-sql';
this.Style = '.dp-sql .func { color: #ff1493; }' +
'.dp-sql .op { color: #808080; }';
} = new; = ['sql']; = function()
var keywords = 'AddHandler AddressOf AndAlso Alias And Ansi As Assembly Auto ' +
'Boolean ByRef Byte ByVal Call Case Catch CBool CByte CChar CDate ' +
'CDec CDbl Char CInt Class CLng CObj Const CShort CSng CStr CType ' +
'Date Decimal Declare Default Delegate Dim DirectCast Do Double Each ' +
'Else ElseIf End Enum Erase Error Event Exit False Finally For Friend ' +
'Function Get GetType GoSub GoTo Handles If Implements Imports In ' +
'Inherits Integer Interface Is Let Lib Like Long Loop Me Mod Module ' +
'MustInherit MustOverride MyBase MyClass Namespace New Next Not Nothing ' +
'NotInheritable NotOverridable Object On Option Optional Or OrElse ' +
'Overloads Overridable Overrides ParamArray Preserve Private Property ' +
'Protected Public RaiseEvent ReadOnly ReDim REM RemoveHandler Resume ' +
'Return Select Set Shadows Shared Short Single Static Step Stop String ' +
'Structure Sub SyncLock Then Throw To True Try TypeOf Unicode Until ' +
'Variant When While With WithEvents WriteOnly Xor';

this.regexList = [
{ regex: new RegExp('\'.*$', 'gm'), css: 'comment' },
{ regex:, css: 'string' },
{ regex: new RegExp('^\\s*#.*', 'gm'), css: 'preprocessor' },
{ regex: new RegExp(this.GetKeywords(keywords), 'gm'), css: 'keyword' }

this.CssClass = 'dp-vb';
} = new; = ['vb', ''];


  1. CSS Attributes etc.

  1. Call Relevant Functions

Tuesday, December 25, 2007

Merry Christmas To All

December 25, 2007 Posted by Jason No comments
Merry Christmas Everybody - including but not limited to those in Wisconsin and everyone back home!

Best wishes for 2008

Sunday, December 23, 2007

Vista Media Center Two Cents

December 23, 2007 Posted by Jason , 1 comment

I mentioned in a previous (less happy) post that I had switched to media center having formerly used media portal. Frankly, I hadn't used my PC as a media center in quite some time, so when I purchased a new PC and got a copy of Vista Ultimate x64, I decided to brush off my old dual tuner and give Vista Media Center a test drive. I only have basic cable as I tend to watch TV series and movies in bulk a la Netflix, but for newer series, sports events, etc., I've gotten into a routine of recording it using media center, removing the commercials and watching it on my 19 inch monitor.

Media center is a beautifully polished application that has become part of my daily life. Its search features are awesome and everything just feels like a real DVR - not an application running on a Windows box. Personally, I would have liked a fully fledged Picture-in-Picture mode, otherwise very little is missing from the setup. I do not own, nor have I ever owned, a TiVo, so I can't compare the two…however, I am so happy with MCE that I don't feel the need to change.

Fortunately, Media Center also has quite a large community backing, with users creating all sorts of plug-ins - from Netflix and eBay browsers, to actual applications that extend the capability of media center. With this in mind, below are the Media Center plug-ins that I just couldn't live without:

ShowAnalyzer/Comskip ( and respectively)

The chances are that if you've used any dvr software on a Windows box you've run across Showanalyzer and Comskip. They aim to do the same thing, and are both (in my experience) quite effective in doing it - they scan recorded TV shows for commercials, outputting commercial information to a text file which can be read by other applications. Due to advertising revenue, TV stations seem a little reluctant to embrace commercial skipping - and to the best of my knowledge TiVo does not offer any automatic commercial skipping .

Comskip may be the publics favorite, since it is the free to use. I've found the out-of-the-box results to be pretty good on most stations and the wealth of configuration options will drive any technically-able user mad with excitement. Configuration is all done via a text file and the application itself is command based. Comskip is accompanied by Comclean and DVRCut which allow the detected commercials to be permanently removed.

ShowAnalyzer on the other hand is (at the time of writing) $20, though I personally purchased it a few days before the price was hiked from $10. It does the same thing as Comskip and again the results were pretty good out of the box. Showanalyzer supports the real-time detection of commercials and is packed with an application called SchoolHouse which is used to tune your commercial skipping settings. There is also a small configuration application which I prefer to Comskip's config file approach. ShowAnalyzer has its own UI, compared to Comskip's dos based approach - though I'd like to be able to select multiple files to scan from the UI (right now only single file selection is supported).

DVRMSToolbox (

DVRMSToolbox is the most complete set of tools available for dealing with Microsoft's DVR-MS filetype. It interacts with Comskip or Showanalyzer - enabling the detection and/or removal of commercials, and more pre and post-processing options than you can imagine - including conversion to mpeg, wmv etc. It has a file-watching service which allows the monitoring of a specific directory whose contents will be scanned when a new file is added. DVRMSToolbox's true beauty lies in the myriad of configuration options and the ability to create profiles to perform almost any sequence of tasks that you could possibly imagine on a recorded file. Another free application, DVRMSToolbox is the ultimate package for anyone interested in interacting with their recorded media.

Lifextender (

Lifextender is a beautiful little application which, similarly to DVRMSToolbox, uses Comskip* to detect commercials in DVR-MS files. On the surface Lifextender offers fewer configuration options than DVR-MS toolbox. Its beauty, in my humble opinion, is better support for the 10-Foot Interface with which many Media Center users are familiar. Interaction with DVRMSToolbox requires a PC setup, but if using your media center box as a dedicated PVR, remote control interaction will be much more straightforward with Lifextender. It allows the creation of rules for commercial removing based on theTv channel, the show's title etc. Furthermore, it allows scans to start automatically when Media Center stops recording.

*At the time of writing Lifextender does not support Showanalyzer. However, the developers of this application have included it in the latest beta version!

Webguide (

For me Webguide is worth it's weight in gold. Previously a licensed application, in September of 2007 Microsoft and Webguide's creator Doug Berret came to a deal with Doug joining the Media Center team while making Webguide available for free! What does this mean to the man on the street??? It means that we now have a high quality and free plug-in for media center which allows for seamless remote viewing and scheduling of media. Live or recorded TV (as well as all manner of music/pictures and even DVDs can be streamed to a browser over the internet. When on the road you can schedule TV shows to be recorded from the comfort of your laptop or even pocket pc or Smartphone.

I have used Webguide for approximately 6 months now and am completely blown away by this application - from its streaming options, to the beautiful TV guide interface, this app is a must have for any self-respecting MCE user. And its price-tag isn't too shabby either!

mceBackup (

If, like me, you are running media center and are a little concerned at the possibility of your settings/recorded shows being blown away the next time you fool around with your PC configuration, then mceBackup by Ian Dixon, is a wonderful little nugget. It allows for the scheduled backup (and if necessary the immediate restoration) of your settings, recorded tv and 'extra folder' and runs as a service with a simple but pretty graphical interface. Frankly this is an application that I don't interact with too often, but one that I rely on every day (actually, every night at 2 A.M.) to keep the state of my media center up to date. Like some of these other entries, it's free as in beer.

Thursday, December 20, 2007

Tuner Issues...

December 20, 2007 Posted by Jason , , , No comments

A while back I decided to go for the whole Media Center experience, and while I didn't have a copy of Windows MCE at the time, I used and open source alternative - MediaPortal ( I bought a dual tuner - specifically a Hauppauge PVR 500 MCE, which worked a treat and allowed me to watch one show while recording another - perfect!

Lately I've bought a new PC - Quad Core, 4GB ram, dual monitors, Vista Ultimate x64...a pretty nice machine for my purposes! Similarly to most computer folks out there, I relish the painstaking labor of a fresh install - loading all my apps and my settings and configuring my new box just right. All went well and in a matter of hours I was able to run Vista's in-built media center - a beautifully designed piece of software (though PiP would have been a nice addition…). Watching tv shows wasn't an issue, and all was well…or so I thought.

Days went by before I decided to watch my first scheduled recording - but to my horror I had nothing but a few minutes of green screen. The same went for the other shows I had set up to record. So I did everything conceivable, reinstalling a bunch of drivers for my Hauppauge TV tuner. At first everything worked ok. I installed, I saw tv - happy days. But once I rebooted (regardless of the driver I installed) the green screen came back!

I ripped my configuration apart but finally came upon a post which offered a solution - necessitating a fairly large tradeoff. Apparently many users have been having this issue and the hauppauge forums are flooded with complaints by angry users such as myself. Hauppauge seem to blame Windows, specifically a memory issue - the only fix I can find is as follows:

Yes, you read that correctly:

"The only 100% guaranteed fix is to reduce the total system memory so that it is below a certain amount (i.e. < 3 GB). This can be done either by pulling RAM sticks from the machine or by using a command prompt in administrator mode with the command bcdedit /set {current} removememory X where X is the amount in MB to hide from total system RAM."

I used bcedit, removing a gigabyte of ram from my system - and MCE has worked without an issue for a few months! Meaning that I have been running on only 3 of my 4 cylinders for a few months - I am pretty annoyed. Apparently this may (read "I don't believe this for a second") be fixed in Vista SP1, but from the later user posts in the above forum, it appears that the issue continues, only this time with a black screen. I plan to test this theory as I want my memory back!

Wednesday, December 19, 2007

Powershell Password Tool

December 19, 2007 Posted by Jason No comments

In the dark days before Windows Authentication was so embedded in (non-microsoft) mainstream applications, Windows users had to remember a myriad of passwords for a plethora of applications. Application development has changed - but relics from previous software eras remain. On desktops all over the world lay applications that were not developed to plug into any external authorization system. Passwords must still be remembered.

At work, I have access to a decent number of passwords that, due to the intermittent nature of their use or their necessary complexity, I just cannot remember. The ideal way to store these would be in a secure repository to which only I have access. An issue remains - sometimes it is necessary to input a password when another user is present - a user with lesser privileges in the given system. The tendency is to ask the user to turn around while I sneak the password from the repository and paste it into the login form…an effective solution but not very elegant.

So I came up with a little powershell script, which allows me to select a password category (e.g. the admin password for a source control system), pulls the password from the SQL repository and outputs it to the clipboard. It took no more than 30 minutes of programming, and I have been using it effectively ever since.

Here's the breakdown:

  1. I created a simple SQL2005 repository, with one table containing the menus (for instance SQL Admin passwords, Source Control Admin Passwords etc.) and another containing a server name and the password for that server. I did not use any encryption in this example, but you can see how easy it would be to do so. Here's a simple DB diagram:

  2. I downloaded and installed PowerSMO (, allowing my powershell script to talk to the SQL database that I just created.
  3. I created the below script to pull the password in question from the database and piped it to the clip command - an inbuilt vista executable which allows the programmatic passing of objects to the windows clipboard.

    Note: the clip executable is located in the System32 directory in Windows Server 2003/Windows Vista. While I cannot attest to the legality of (and therefore cannot recommend) copying clip.exe from your copy of Vista into the same folder on your Windows XP box, the clip.exe functionality can be mimicked on an XP box by doing so. Technically speaking, this script should work on an XP box also.

  4. Actually, there is no 4. Now when I'm in an awkward situation, I crack open powershell and pass my password to the clipboard! It's as simple as that. Please feel free to test this script out and let me know how it works!

Monday, December 17, 2007

Free Visual Studio 2008 Training???

December 17, 2007 Posted by Jason , , No comments
A colleague of mine sent me a link for some free Visual Studio 2008 Training. Neither he nor I have ever used innerworkings content, nor do we have any affiliation or experience with the organization, so i can in no way endorse this software. However, if you are adventurous like me, and are interested in learning a little more about 2008, this may be interesting.

The promotion is split into 3 optional components:

* New Features in C# 3.0 (1 hour)
* New Features in VB 9.0 (1 hour)
* LINQ to SQL (1 hour)

Once I have taken the training i will blog my opinions/comments. However, if you are interested in doing so before then (and apparently this is a limited time offer) go ahead and take a look at the link below: