// This file is part of the "jQuery.Syntax" project, and is distributed under the MIT License. // Copyright (c) 2011 Samuel G. D. Williams. if(!RegExp.prototype.indexOf){RegExp.indexOf=function(match,index){return match[0].indexOf(match[index])+match.index;};} if(!RegExp.prototype.escape){RegExp.escape=function(pattern){return pattern.replace(/[\-\[\]{}()*+?.\\\^$|,#\s]/g,"\\$&");};} if(!String.prototype.repeat){String.prototype.repeat=function(l){return new Array(l+1).join(this);};} Syntax.innerText=function(element){var text;if(!element){return"";} if(element.nodeName=='BR'){return'\n';}else if(element.textContent){text=element.textContent;}else if(document.body.innerText){text=element.innerText;} return text.replace(/\r\n?/g,'\n');} Syntax.extractElementMatches=function(elems,offset,tabWidth){var matches=[],current=[elems];offset=offset||0;tabWidth=tabWidth||4;(function(elems){for(var i=0;elems[i];i++){var text=null,elem=elems[i];if(elem.nodeType===3||elem.nodeType===4){offset+=elem.nodeValue.length;}else if(elem.nodeType===1){var text=Syntax.innerText(elem);matches.push(new Syntax.Match(offset,text.length,{klass:elem.className,force:true,element:elem,allow:'*'},text));} if(elem.nodeType!==8&&elem.children){arguments.callee(elem.childNodes,offset);}}})(elems);matches.shift();return matches;} Syntax.layouts.preformatted=function(options,html,container){return html;};Syntax.modeLineOptions={'tab-width':function(name,value,options){options.tabWidth=parseInt(value,10);}};Syntax.convertTabsToSpaces=function(text,tabSize){var space=[],pattern=/\r|\n|\t/g,tabOffset=0,offsets=[],totalOffset=0;tabSize=tabSize||4 for(var i="";i.length<=tabSize;i=i+" "){space.push(i);} text=text.replace(pattern,function(match){var offset=arguments[arguments.length-2];if(match==="\r"||match==="\n"){tabOffset=-(offset+1);return match;}else{var width=tabSize-((tabOffset+offset)%tabSize);tabOffset+=width-1;totalOffset+=width-1 offsets.push([offset,width,totalOffset]);return space[width];}});return{text:text,offsets:offsets};};Syntax.convertToLinearOffsets=function(offsets,length){var current=0,changes=[];for(var i=0;ioffsets[current][0]){if(offsets[current+1]){if(i<=offsets[current+1][0]){changes.push(offsets[current][2]);}else{current+=1;i-=1;}}else{changes.push(offsets[current][2]);}}else{changes.push(changes[changes.length-1]||0);}} return changes;} Syntax.updateMatchesWithOffsets=function(matches,linearOffsets,text){(function(matches){for(var i=0;i0) arguments.callee(match.children);}})(matches);return matches;};Syntax.extractMatches=function(){var rules=arguments;return function(match,expr){var matches=[];for(var i=0;i0){if(rule.brush){matches.push(Syntax.Brush.buildTree(rule,match[index],RegExp.indexOf(match,index)));}else{var expression=jQuery.extend({owner:expr.owner},rule);matches.push(new Syntax.Match(RegExp.indexOf(match,index),match[index].length,expression,match[index]));}}} return matches;};};Syntax.lib.webLinkProcess=function(queryURI,lucky){if(lucky){queryURI="http://www.google.com/search?btnI=I&q="+encodeURIComponent(queryURI+" ");} return function(element,match,options){if(options.linkify===false) return element;var a=document.createElement('a');a.href=queryURI+encodeURIComponent(Syntax.innerText(element));a.className=element.className;while(element.childNodes.length>0) a.appendChild(element.childNodes[0]);return a;};};Syntax.register=function(name,callback){var brush=Syntax.brushes[name]=new Syntax.Brush();brush.klass=name;callback(brush);};Syntax.lib.cStyleComment={pattern:/\/\*[\s\S]*?\*\//gm,klass:'comment',allow:['href']};Syntax.lib.cppStyleComment={pattern:/\/\/.*$/gm,klass:'comment',allow:['href']};Syntax.lib.perlStyleComment={pattern:/#.*$/gm,klass:'comment',allow:['href']};Syntax.lib.perlStyleRegularExpression={pattern:/\B\/([^\/]|\\\/)*?\/[a-z]*(?=\s*($|[^\w\s'"\(]))/gm,klass:'constant',incremental:true};Syntax.lib.cStyleFunction={pattern:/([a-z_][a-z0-9_]*)\s*\(/gi,matches:Syntax.extractMatches({klass:'function'})};Syntax.lib.camelCaseType={pattern:/\b_*[A-Z][\w]*\b/g,klass:'type'};Syntax.lib.cStyleType={pattern:/\b[_a-z][_\w]*_t\b/gi,klass:'type'};Syntax.lib.xmlComment={pattern:/(<|<)!--[\s\S]*?--(>|>)/gm,klass:'comment'};Syntax.lib.webLink={pattern:/\w+:\/\/[\w\-.\/?%&=@:;#]*/g,klass:'href'};Syntax.lib.hexNumber={pattern:/\b0x[0-9a-fA-F]+/g,klass:'constant'};Syntax.lib.decimalNumber={pattern:/\b[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?/g,klass:'constant'};Syntax.lib.doubleQuotedString={pattern:/"([^\\"\n]|\\.)*"/g,klass:'string'};Syntax.lib.singleQuotedString={pattern:/'([^\\'\n]|\\.)*'/g,klass:'string'};Syntax.lib.multiLineDoubleQuotedString={pattern:/"([^\\"]|\\.)*"/g,klass:'string'};Syntax.lib.multiLineSingleQuotedString={pattern:/'([^\\']|\\.)*'/g,klass:'string'};Syntax.lib.stringEscape={pattern:/\\./g,klass:'escape',only:['string']};Syntax.Match=function(offset,length,expression,value){this.offset=offset;this.endOffset=offset+length;this.length=length;this.expression=expression;this.value=value;this.children=[];this.parent=null;this.next=null;};Syntax.Match.prototype.shift=function(offset,text){this.adjust(offset,null,text);for(var i=0;i=this.offset)&&(match.endOffset<=this.endOffset);};Syntax.Match.defaultReduceCallback=function(node,container){if(typeof(node)==='string'){node=document.createTextNode(node);} container.appendChild(node);};Syntax.Match.prototype.reduce=function(append,process){var start=this.offset;var container=document.createElement('span');append=append||Syntax.Match.defaultReduceCallback;if(this.expression&&this.expression.klass){if(container.className.length>0) container.className+=' ';container.className+=this.expression.klass;} for(var i=0;ithis.endOffset){Syntax.log("Syntax Warning: Start position "+start+" exceeds end of value "+this.endOffset);} if(process){container=process(container,this);} return container;};Syntax.Match.prototype.canContain=function(match){if(match.expression.force){return true;} if(this.complete){return false;} if(match.expression.only){return true;} if(typeof(this.expression.allow)==='undefined'){return false;} if(jQuery.isArray(this.expression.disallow)&&jQuery.inArray(match.expression.klass,this.expression.disallow)!==-1){return false;} if(this.expression.allow==='*'){return true;} if(jQuery.isArray(this.expression.allow)&&jQuery.inArray(match.expression.klass,this.expression.allow)!==-1){return true;} return false;};Syntax.Match.prototype.canHaveChild=function(match){var only=match.expression.only;if(only){var cur=this;while(cur!==null){if(jQuery.inArray(cur.expression.klass,only)!==-1){return true;} cur=cur.parent;if(cur&&cur.complete){break;}} return false;} return true;};Syntax.Match.prototype._splice=function(i,match){if(this.canHaveChild(match)){this.children.splice(i,0,match);match.parent=this;if(!match.expression.owner){match.expression.owner=this.expression.owner;} return this;}else{return null;}};Syntax.Match.prototype.insert=function(match,whole){if(!this.contains(match)) return null;if(whole){var top=this,i=0;while(i0){var i=this.children.length-1;var child=this.children[i];if(match.offset=child.endOffset) continue;if(child.contains(match)){return child._insert(match);} var parts=match.bisectAtOffsets([child.offset,child.endOffset]);if(parts[0]){this._splice(i,parts[0])} if(parts[1]){child.insert(parts[1])} if(parts[2]){match=parts[2]}else{return this;}} this._splice(this.children.length,match);} Syntax.Match.prototype.bisectAtOffsets=function(splits){var parts=[],start=this.offset,prev=null,children=jQuery.merge([],this.children);splits=splits.slice(0);splits.push(this.endOffset);splits.sort(function(a,b){return a-b;});for(var i=0;ithis.endOffset){break;} if(offset0){if(children[0].endOffset<=parts[i].endOffset){parts[i].children.push(children.shift());}else{break;}} if(children.length){if(children[0].offset0)all+="|";var p=patterns[i];if(p instanceof RegExp){all+=p.source;}else{all+=Syntax.Brush.convertStringToTokenPattern(p,true);}} all+=")";this.push(jQuery.extend({pattern:new RegExp(all,rule.options||'g')},rule));}else{var rule=arguments[0];if(typeof(rule.pattern)==='string'){rule.string=rule.pattern;rule.pattern=new RegExp(Syntax.Brush.convertStringToTokenPattern(rule.string,true),rule.options||'g')} if(typeof(XRegExp)!=='undefined'){rule.pattern=new XRegExp(rule.pattern);} rule.apply=rule.apply||Syntax.Brush.MatchPattern;if(rule.pattern&&rule.pattern.global||typeof(rule.pattern)=='undefined'){this.rules.push(jQuery.extend({owner:this},rule));}else{Syntax.log("Syntax Error: Malformed rule: ",rule);}}};Syntax.Brush.prototype.getMatchesForRule=function(text,rule){var matches=[],match=null;if(typeof(rule.apply)!='undefined'){matches=rule.apply(text,rule);} if(rule.debug){Syntax.log("Syntax matches:",rule,text,matches);} return matches;};Syntax.Brush.prototype.getRuleForKlass=function(klass){for(var i=0;i0){for(var i=0;i').attr('href',this.innerHTML).text(this.innerHTML));});} callback(html,brush,text,options);});} Syntax.highlight=function(elements,options,callback){if(typeof(options)==='function'){callback=options;options={};} options.layout=options.layout||'preformatted';options.matches=[];if(typeof(options.tabWidth)==='undefined'){options.tabWidth=4;} elements.each(function(){var container=jQuery(this);options.matches=options.matches.concat(Syntax.extractElementMatches(container));var text=Syntax.innerText(this);var match=text.match(/-\*- mode: (.+?);(.*?)-\*-/i);var endOfSecondLine=text.indexOf("\n",text.indexOf("\n")+1);if(match&&match.index