/* Sourcerer Source Viewer v1.1 http://www.andbeyonddesign.com File: Sourcerer.js Author: Anson Airoldi Email: greatunknown@andbeyonddesign.com License: Licensed under MIT http://www.opensource.org/licenses/mit-license.php Notes: Requires the latest version of jQuery http://code.jquery.com/jquery-1.6.min.js */ (function($){ // String prototype that wraps the given string with span tags and a class // of the color that its contents needs to be String.prototype.highlight = function(lang){ var Code = this; if(this.length){ for(var i=0; i$&"); } } } }else{ Code = " "; } return Code; } // returns an array of regular expressions based on the language // that is being highlighted, allows for multiple languages to // be highlighted at once, but also note that some overlapping // of colors may occur function GetLang(sets){ var l = []; if(sets.indexOf('html') !== -1){ l.push(HTMLArr); } if(sets.indexOf('js') !== -1){ l.push(JSArr); } if(sets.indexOf('css') !== -1){ l.push(CSSArr); } if(sets.indexOf('php') !== -1){ l.push(PHPArr); } return (l.length) ? l : false; } // method that replaces some html entities if not already done // this does not prevent the problem with using '<' symbols in // javascript as described in the download page at andbeyonddesign.com/Sourcerer String.prototype.escapeHTML = function () { return( this.replace(/>/g,'>'). replace(/)|(.*-->)|(\s*[A-Za-z])))|(this\\.)|(var\\s)|(return\\s)|(true;)|(false;)|(return;)" : "SC_navy", "([\\(\\)\\{\\}\\*\\[\\]\\+-]{1})" : "SC_blue", "[0-9]+(?![A-Za-z]+)" : "SC_red", "(function|var|this|false|true|return|for|if|else|switch|while|try|catch|do|in)(\\s*[A-Za-z]*\\s*(\\(|\\{))*" : "SC_bold" }; // regular expressions for php highlighting var PHPArr = { "(\\/\\/.*)|(\\/\\*.*\\*\\/)" : "SC_grey", "(((\"|')[a-zA-Z0-9\\s-\\/\\.\\?\\*#;:_',&=%\\)\\(~]*(\"|'))(?!\\>))":"SC_navy", "(((<\\?php)|(\\?>))(?!.*\\*\\/))|([0-9](?![A_Za-z]*\"|'))" : "SC_red", "(([a-zA-Z0-9_]+\\()|(echo)|(\\$_[a-zA-Z0-9]+))(?!.*\\*\\/)" : "SC_green", "[\\(\\)\\{\\}(\\*\\+]{1}(?!.*\\*\\/)" : "SC_blue", "\\$[A-Za-z0-9]+" : "SC_teal" }; $.fn.extend({ sourcerer: function(settings){ var sel = this.selector; var def = { "lang" : false, "numbers" : true, "title" : false }; var sets = {}; return this.each(function() { if(!$(this).children('.SRC_Wrap').length){ settings = (typeof(settings) == 'string') ? {"lang" : settings} : settings; $.extend(sets, def, settings); var Out = []; if(sets.lang){ var Lines = $(this).html().escapeHTML().split('\n'); Out.push("
"); if(sets.title){ Out.push("
"+ sets.title +"
"); } Out.push("
"); for(var j=0; j
" + Lines[j].highlight(GetLang(sets.lang)) + "
"); } Out.push("
"); $(this).html(Out.join('')); if(sets.numbers){ var numbox = $(this).find('.SRC_NumBox'); Out = []; for(var j=0; j
"+(j+1)+"
"); } numbox.html(Out.join('')); }else{ $(this).find('.SRC_CodeBox').width('100%'); $(this).find('.SRC_CodeContent').css('border-left',0); } } else{ throw "::No language given or language was invalid for sourcerer call with selector '" + sel + "'"; } } }); } }); })(jQuery);