1/* 2 Sourcerer Source Viewer v1.1 3 http://www.andbeyonddesign.com 4 5 File: Sourcerer.js 6 Author: Anson Airoldi 7 Email: [email protected] 8 License: Licensed under MIT 9 http://www.opensource.org/licenses/mit-license.php 10 11 Notes: Requires the latest version of jQuery 12 http://code.jquery.com/jquery-1.6.min.js 13*/ 14 15(function($){ 16 17 // String prototype that wraps the given string with span tags and a class 18 // of the color that its contents needs to be 19 String.prototype.highlight = function(lang){ 20 var Code = this; 21 if(this.length){ 22 for(var i=0; i<lang.length; i++){ 23 for(var OB in lang[i]){ 24 var NeExp = new RegExp(OB, "gi"); 25 var Pos = Code.search(NeExp); 26 var Compos = Code.indexOf('//'); 27 if(Pos <= Compos || Compos === -1 || Compos === 0){ 28 Code = Code.replace(NeExp, "<span class='" + lang[i][OB] + "'>$&</span>"); 29 } 30 } 31 } 32 }else{ 33 Code = " "; 34 } 35 return Code; 36 } 37 38 // returns an array of regular expressions based on the language 39 // that is being highlighted, allows for multiple languages to 40 // be highlighted at once, but also note that some overlapping 41 // of colors may occur 42 function GetLang(sets){ 43 var l = []; 44 if(sets.indexOf('html') !== -1){ l.push(HTMLArr); } 45 if(sets.indexOf('js') !== -1){ l.push(JSArr); } 46 if(sets.indexOf('css') !== -1){ l.push(CSSArr); } 47 if(sets.indexOf('php') !== -1){ l.push(PHPArr); } 48 return (l.length) ? l : false; 49 } 50 51 // method that replaces some html entities if not already done 52 // this does not prevent the problem with using '<' symbols in 53 // javascript as described in the download page at andbeyonddesign.com/Sourcerer 54 String.prototype.escapeHTML = function () { 55 return( 56 this.replace(/>/g,'>'). 57 replace(/</g,'<'). 58 replace(/<!--\?php/g, '<?php' ). 59 replace(/\?-->/g, '?>') 60 ); 61 }; 62 63 // regular expressions for html highlighting 64 var HTMLArr = { 65 "<!--.*-->" : "SC_grey", 66 "<\\/*(div|p|br|hr|link|em|meta|html|h2|h1|h3|ul|!DOCTYPE html|li|ol|body|select|span|head|title|canvas|video|audio|source|pre)\\s*(([A-Za-z]+)=(\"|')[a-zA-Z0-9\\.\\/\\(\\)\\?,&'_#\\s;:%=\\-~]*(\"|')\\s*)*\\s*\\/*>(?!.*-->)" : "SC_blue", 67 "<\\/*script\\s*(([A-Za-z]+)=\"[a-zA-Z0-9\\.\\/]*\"\\s*)*\\s*\\/*>(?!.*-->)" : "SC_red", 68 "<\\/*a\\s*(([A-Za-z]+)=\"[a-zA-Z0-9\\.\\/\\(\\)\\[\\]\\?\\s\\$=,'_#;:&~(<)(>)-]*\"\\s*)*\\s*\\/*>(?!.*-->)" : "SC_green", 69 "<img\\s+(([A-Za-z]+)=\"[a-zA-Z0-9\\.\\/\\(\\),\\s'_#;:]+\"\\s*)*\\s*\\/*>(?!.*-->)" : "SC_red", 70 "<\\/*(form|input|textarea|button|option|select)\\s*(([A-Za-z]+)=\"[a-zA-Z0-9\\(\\);,':\\s\\.]*\"\\s*)*\\s*\\/*>(?!.*-->)" : "SC_gold", 71 "<\\/*(td|tr|table|tbody)\\s*(([A-Za-z]+)=\"[a-zA-Z0-9\\.\\/\\(\\)\\s,'_%#;:~-]*\"\\s*)*\\s*\\/*>(?!.*-->)" : "SC_teal", 72 "\"[a-zA-Z0-9\\s\\/\\.\\?\\[\\]\\$#;:_',&=%\\)\\(~-]*\"(?!(.*-->)|(\s*[A-Za-z]))" : "SC_navy" 73 }; 74 75 // regular expressions for css highlighting 76 var CSSArr = { 77 "((#|\\.)*[a-zA-Z0-9_:,\\s\\.\\*#]+\\s*\\{)|(\\})" : "SC_pink", 78 "\\/\\*.*\\*\\/" : "SC_grey", 79 "[a-zA-Z0-9-]+:(?![a-zA-Z0-9_:\\s\\.]+\\{)" : "SC_blue", 80 "[a-zA-Z0-9,'_#\\(\\)\\/%@\\s\\.\"-]+;" : "SC_green" 81 }; 82 83 // regular expressions for javascript highlighting 84 var JSArr = { 85 "\\/\\/.*" : "SC_grey", 86 "((function|var|this|for|if|else|switch|while|try|catch)\\s*(\\{|\\())" : "SC_blue", 87 "(\\.[a-zA-Z]+(?=\\())|(alert\\()|((String|Date|Array|Number)\\.)" : "SC_teal", 88 "(((\"|')[a-zA-Z0-9\\s-\\/\\.\\?#!;:_',&=%\\)\\(~]*(\"|'))(?!(\\>)|(.*-->)|(\s*[A-Za-z])))|(this\\.)|(var\\s)|(return\\s)|(true;)|(false;)|(return;)" : "SC_navy", 89 "([\\(\\)\\{\\}\\*\\[\\]\\+-]{1})" : "SC_blue", 90 "[0-9]+(?![A-Za-z]+)" : "SC_red", 91 "(function|var|this|false|true|return|for|if|else|switch|while|try|catch|do|in)(\\s*[A-Za-z]*\\s*(\\(|\\{))*" : "SC_bold" 92 }; 93 94 // regular expressions for php highlighting 95 var PHPArr = { 96 "(\\/\\/.*)|(\\/\\*.*\\*\\/)" : "SC_grey", 97 "(((\"|')[a-zA-Z0-9\\s-\\/\\.\\?\\*#;:_',&=%\\)\\(~]*(\"|'))(?!\\>))":"SC_navy", 98 "(((<\\?php)|(\\?>))(?!.*\\*\\/))|([0-9](?![A_Za-z]*\"|'))" : "SC_red", 99 "(([a-zA-Z0-9_]+\\()|(echo)|(\\$_[a-zA-Z0-9]+))(?!.*\\*\\/)" : "SC_green", 100 "[\\(\\)\\{\\}(\\*\\+]{1}(?!.*\\*\\/)" : "SC_blue", 101 "\\$[A-Za-z0-9]+" : "SC_teal" 102 }; 103 104 $.fn.extend({ 105 sourcerer: function(settings){ 106 var sel = this.selector; 107 var def = { 108 "lang" : false, 109 "numbers" : true, 110 "title" : false 111 }; 112 var sets = {}; 113 114 return this.each(function() { 115 if(!$(this).children('.SRC_Wrap').length){
116 settings = (typeof(settings) == 'string') ? {"lang" : settings} : settings; 117 $.extend(sets, def, settings); 118 var Out = []; 119 if(sets.lang){ 120 var Lines = $(this).html().escapeHTML().split('\n'); 121 Out.push("<div class='SRC_Wrap'>"); 122 if(sets.title){ Out.push("<div class='SRC_Title'>"+ sets.title +"</div>"); } 123 Out.push("<div class='SRC_NumBox'></div><div class='SRC_CodeBox'>"); 124 for(var j=0; j<Lines.length; j++){ 125 Out.push("<div class='SRC_Line'><pre class='SRC_CodeContent'>" + Lines[j].highlight(GetLang(sets.lang)) + "</pre></div>"); 126 } 127 128 Out.push("</div><div style='clear:both;'></div></div>"); 129 $(this).html(Out.join('')); 130 if(sets.numbers){ 131 var numbox = $(this).find('.SRC_NumBox'); 132 Out = []; 133 for(var j=0; j<Lines.length; j++){ 134 var h = $($('.SRC_Line')[j]).height(); 135 Out.push("<div class='SRC_Line' style='height:"+h+"px'><div class='SRC_NumContent'>"+(j+1)+"</div></div>"); 136 } 137 numbox.html(Out.join('')); 138 }else{ 139 $(this).find('.SRC_CodeBox').width('100%'); 140 $(this).find('.SRC_CodeContent').css('border-left',0); 141 } 142 } 143 else{
144 throw "::No language given or language was invalid for sourcerer call with selector '" + sel + "'"; 145 } 146 } 147 }); 148 } 149 }); 150})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.