root / trunk / web / dojo / dojox / highlight / _base.js
History | View | Annotate | Download (7.59 KB)
1 | 9 | andrej.cim | /*
|
---|---|---|---|
2 | Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
|
||
3 | Available via Academic Free License >= 2.1 OR the modified BSD license.
|
||
4 | see: http://dojotoolkit.org/license for details
|
||
5 | */
|
||
6 | |||
7 | |||
8 | if(!dojo._hasResource["dojox.highlight._base"]){ |
||
9 | dojo._hasResource["dojox.highlight._base"]=true; |
||
10 | dojo.provide("dojox.highlight._base");
|
||
11 | (function(){
|
||
12 | var dh=dojox.highlight,_1="\\b(0x[A-Za-z0-9]+|\\d+(\\.\\d+)?)"; |
||
13 | dh.constants={IDENT_RE:"[a-zA-Z][a-zA-Z0-9_]*",UNDERSCORE_IDENT_RE:"[a-zA-Z_][a-zA-Z0-9_]*",NUMBER_RE:"\\b\\d+(\\.\\d+)?",C_NUMBER_RE:_1,APOS_STRING_MODE:{className:"string",begin:"'",end:"'",illegal:"\\n",contains:["escape"],relevance:0},QUOTE_STRING_MODE:{className:"string",begin:"\"",end:"\"",illegal:"\\n",contains:["escape"],relevance:0},BACKSLASH_ESCAPE:{className:"escape",begin:"\\\\.",end:"^",relevance:0},C_LINE_COMMENT_MODE:{className:"comment",begin:"//",end:"$",relevance:0},C_BLOCK_COMMENT_MODE:{className:"comment",begin:"/\\*",end:"\\*/"},HASH_COMMENT_MODE:{className:"comment",begin:"#",end:"$"},C_NUMBER_MODE:{className:"number",begin:_1,end:"^",relevance:0}}; |
||
14 | function _2(_3){ |
||
15 | return _3.replace(/&/gm,"&").replace(/</gm,"<").replace(/>/gm,">"); |
||
16 | }; |
||
17 | function _4(_5){ |
||
18 | return dojo.every(_5.childNodes,function(_6){ |
||
19 | return _6.nodeType==3||String(_6.nodeName).toLowerCase()=="br"; |
||
20 | }); |
||
21 | }; |
||
22 | function _7(_8){ |
||
23 | var _9=[];
|
||
24 | dojo.forEach(_8.childNodes,function(_a){
|
||
25 | if(_a.nodeType==3){ |
||
26 | _9.push(_a.nodeValue); |
||
27 | }else{
|
||
28 | if(String(_a.nodeName).toLowerCase()=="br"){ |
||
29 | _9.push("\n");
|
||
30 | }else{
|
||
31 | throw "Complex markup"; |
||
32 | } |
||
33 | } |
||
34 | }); |
||
35 | return _9.join(""); |
||
36 | }; |
||
37 | function _b(_c){ |
||
38 | if(!_c.keywordGroups){
|
||
39 | for(var _d in _c.keywords){ |
||
40 | var kw=_c.keywords[_d];
|
||
41 | if(kw instanceof Object){ |
||
42 | _c.keywordGroups=_c.keywords; |
||
43 | }else{
|
||
44 | _c.keywordGroups={keyword:_c.keywords};
|
||
45 | } |
||
46 | break;
|
||
47 | } |
||
48 | } |
||
49 | }; |
||
50 | function _e(_f){ |
||
51 | if(_f.defaultMode&&_f.modes){
|
||
52 | _b(_f.defaultMode); |
||
53 | dojo.forEach(_f.modes,_b); |
||
54 | } |
||
55 | }; |
||
56 | var _10=function(_11,_12){ |
||
57 | this.langName=_11;
|
||
58 | this.lang=dh.languages[_11];
|
||
59 | this.modes=[this.lang.defaultMode]; |
||
60 | this.relevance=0; |
||
61 | this.keywordCount=0; |
||
62 | this.result=[];
|
||
63 | if(!this.lang.defaultMode.illegalRe){ |
||
64 | this.buildRes();
|
||
65 | _e(this.lang);
|
||
66 | } |
||
67 | try{
|
||
68 | this.highlight(_12);
|
||
69 | this.result=this.result.join(""); |
||
70 | } |
||
71 | catch(e){
|
||
72 | if(e=="Illegal"){ |
||
73 | this.relevance=0; |
||
74 | this.keywordCount=0; |
||
75 | this.partialResult=this.result.join(""); |
||
76 | this.result=_2(_12);
|
||
77 | }else{
|
||
78 | throw e;
|
||
79 | } |
||
80 | } |
||
81 | }; |
||
82 | dojo.extend(_10,{buildRes:function(){ |
||
83 | dojo.forEach(this.lang.modes,function(_13){ |
||
84 | if(_13.begin){
|
||
85 | _13.beginRe=this.langRe("^"+_13.begin); |
||
86 | } |
||
87 | if(_13.end){
|
||
88 | _13.endRe=this.langRe("^"+_13.end); |
||
89 | } |
||
90 | if(_13.illegal){
|
||
91 | _13.illegalRe=this.langRe("^(?:"+_13.illegal+")"); |
||
92 | } |
||
93 | },this);
|
||
94 | this.lang.defaultMode.illegalRe=this.langRe("^(?:"+this.lang.defaultMode.illegal+")"); |
||
95 | },subMode:function(_14){ |
||
96 | var _15=this.modes[this.modes.length-1].contains; |
||
97 | if(_15){
|
||
98 | var _16=this.lang.modes; |
||
99 | for(var i=0;i<_15.length;++i){ |
||
100 | var _17=_15[i];
|
||
101 | for(var j=0;j<_16.length;++j){ |
||
102 | var _18=_16[j];
|
||
103 | if(_18.className==_17&&_18.beginRe.test(_14)){
|
||
104 | return _18;
|
||
105 | } |
||
106 | } |
||
107 | } |
||
108 | } |
||
109 | return null; |
||
110 | },endOfMode:function(_19){ |
||
111 | for(var i=this.modes.length-1;i>=0;--i){ |
||
112 | var _1a=this.modes[i]; |
||
113 | if(_1a.end&&_1a.endRe.test(_19)){
|
||
114 | return this.modes.length-i; |
||
115 | } |
||
116 | if(!_1a.endsWithParent){
|
||
117 | break;
|
||
118 | } |
||
119 | } |
||
120 | return 0; |
||
121 | },isIllegal:function(_1b){ |
||
122 | var _1c=this.modes[this.modes.length-1].illegalRe; |
||
123 | return _1c&&_1c.test(_1b);
|
||
124 | },langRe:function(_1d,_1e){ |
||
125 | var _1f="m"+(this.lang.case_insensitive?"i":"")+(_1e?"g":""); |
||
126 | return new RegExp(_1d,_1f); |
||
127 | },buildTerminators:function(){ |
||
128 | var _20=this.modes[this.modes.length-1],_21={}; |
||
129 | if(_20.contains){
|
||
130 | dojo.forEach(this.lang.modes,function(_22){ |
||
131 | if(dojo.indexOf(_20.contains,_22.className)>=0){ |
||
132 | _21[_22.begin]=1;
|
||
133 | } |
||
134 | }); |
||
135 | } |
||
136 | for(var i=this.modes.length-1;i>=0;--i){ |
||
137 | var m=this.modes[i]; |
||
138 | if(m.end){
|
||
139 | _21[m.end]=1;
|
||
140 | } |
||
141 | if(!m.endsWithParent){
|
||
142 | break;
|
||
143 | } |
||
144 | } |
||
145 | if(_20.illegal){
|
||
146 | _21[_20.illegal]=1;
|
||
147 | } |
||
148 | var t=[];
|
||
149 | for(i in _21){ |
||
150 | t.push(i); |
||
151 | } |
||
152 | _20.terminatorsRe=this.langRe("("+t.join("|")+")"); |
||
153 | },eatModeChunk:function(_23,_24){ |
||
154 | var _25=this.modes[this.modes.length-1]; |
||
155 | if(!_25.terminatorsRe){
|
||
156 | this.buildTerminators();
|
||
157 | } |
||
158 | _23=_23.substr(_24); |
||
159 | var _26=_25.terminatorsRe.exec(_23);
|
||
160 | if(!_26){
|
||
161 | return {buffer:_23,lexeme:"",end:true}; |
||
162 | } |
||
163 | return {buffer:_26.index?_23.substr(0,_26.index):"",lexeme:_26[0],end:false}; |
||
164 | },keywordMatch:function(_27,_28){ |
||
165 | var _29=_28[0]; |
||
166 | if(this.lang.case_insensitive){ |
||
167 | _29=_29.toLowerCase(); |
||
168 | } |
||
169 | for(var _2a in _27.keywordGroups){ |
||
170 | if(_29 in _27.keywordGroups[_2a]){ |
||
171 | return _2a;
|
||
172 | } |
||
173 | } |
||
174 | return ""; |
||
175 | },buildLexemes:function(_2b){ |
||
176 | var _2c={};
|
||
177 | dojo.forEach(_2b.lexems,function(_2d){
|
||
178 | _2c[_2d]=1;
|
||
179 | }); |
||
180 | var t=[];
|
||
181 | for(var i in _2c){ |
||
182 | t.push(i); |
||
183 | } |
||
184 | _2b.lexemsRe=this.langRe("("+t.join("|")+")",true); |
||
185 | },processKeywords:function(_2e){ |
||
186 | var _2f=this.modes[this.modes.length-1]; |
||
187 | if(!_2f.keywords||!_2f.lexems){
|
||
188 | return _2(_2e);
|
||
189 | } |
||
190 | if(!_2f.lexemsRe){
|
||
191 | this.buildLexemes(_2f);
|
||
192 | } |
||
193 | _2f.lexemsRe.lastIndex=0;
|
||
194 | var _30=[],_31=0,_32=_2f.lexemsRe.exec(_2e); |
||
195 | while(_32){
|
||
196 | _30.push(_2(_2e.substr(_31,_32.index-_31))); |
||
197 | var _33=this.keywordMatch(_2f,_32); |
||
198 | if(_33){
|
||
199 | ++this.keywordCount;
|
||
200 | _30.push("<span class=\""+_33+"\">"+_2(_32[0])+"</span>"); |
||
201 | }else{
|
||
202 | _30.push(_2(_32[0]));
|
||
203 | } |
||
204 | _31=_2f.lexemsRe.lastIndex; |
||
205 | _32=_2f.lexemsRe.exec(_2e); |
||
206 | } |
||
207 | _30.push(_2(_2e.substr(_31,_2e.length-_31))); |
||
208 | return _30.join(""); |
||
209 | },processModeInfo:function(_34,_35,end){ |
||
210 | var _36=this.modes[this.modes.length-1]; |
||
211 | if(end){
|
||
212 | this.result.push(this.processKeywords(_36.buffer+_34)); |
||
213 | return;
|
||
214 | } |
||
215 | if(this.isIllegal(_35)){ |
||
216 | throw "Illegal"; |
||
217 | } |
||
218 | var _37=this.subMode(_35); |
||
219 | if(_37){
|
||
220 | _36.buffer+=_34; |
||
221 | this.result.push(this.processKeywords(_36.buffer)); |
||
222 | if(_37.excludeBegin){
|
||
223 | this.result.push(_35+"<span class=\""+_37.className+"\">"); |
||
224 | _37.buffer="";
|
||
225 | }else{
|
||
226 | this.result.push("<span class=\""+_37.className+"\">"); |
||
227 | _37.buffer=_35; |
||
228 | } |
||
229 | this.modes.push(_37);
|
||
230 | this.relevance+=typeof _37.relevance=="number"?_37.relevance:1; |
||
231 | return;
|
||
232 | } |
||
233 | var _38=this.endOfMode(_35); |
||
234 | if(_38){
|
||
235 | _36.buffer+=_34; |
||
236 | if(_36.excludeEnd){
|
||
237 | this.result.push(this.processKeywords(_36.buffer)+"</span>"+_35); |
||
238 | }else{
|
||
239 | this.result.push(this.processKeywords(_36.buffer+_35)+"</span>"); |
||
240 | } |
||
241 | while(_38>1){ |
||
242 | this.result.push("</span>"); |
||
243 | --_38; |
||
244 | this.modes.pop();
|
||
245 | } |
||
246 | this.modes.pop();
|
||
247 | this.modes[this.modes.length-1].buffer=""; |
||
248 | return;
|
||
249 | } |
||
250 | },highlight:function(_39){ |
||
251 | var _3a=0; |
||
252 | this.lang.defaultMode.buffer=""; |
||
253 | do{
|
||
254 | var _3b=this.eatModeChunk(_39,_3a); |
||
255 | this.processModeInfo(_3b.buffer,_3b.lexeme,_3b.end);
|
||
256 | _3a+=_3b.buffer.length+_3b.lexeme.length; |
||
257 | }while(!_3b.end);
|
||
258 | if(this.modes.length>1){ |
||
259 | throw "Illegal"; |
||
260 | } |
||
261 | }}); |
||
262 | function _3c(_3d,_3e,_3f){ |
||
263 | if(String(_3d.tagName).toLowerCase()=="code"&&String(_3d.parentNode.tagName).toLowerCase()=="pre"){ |
||
264 | var _40=document.createElement("div"),_41=_3d.parentNode.parentNode; |
||
265 | _40.innerHTML="<pre><code class=\""+_3e+"\">"+_3f+"</code></pre>"; |
||
266 | _41.replaceChild(_40.firstChild,_3d.parentNode); |
||
267 | }else{
|
||
268 | _3d.className=_3e; |
||
269 | _3d.innerHTML=_3f; |
||
270 | } |
||
271 | }; |
||
272 | function _42(_43,str){ |
||
273 | var _44=new _10(_43,str); |
||
274 | return {result:_44.result,langName:_43,partialResult:_44.partialResult}; |
||
275 | }; |
||
276 | function _45(_46,_47){ |
||
277 | var _48=_42(_47,_7(_46));
|
||
278 | _3c(_46,_46.className,_48.result); |
||
279 | }; |
||
280 | function _49(str){ |
||
281 | var _4a="",_4b="",_4c=2,_4d=str; |
||
282 | for(var key in dh.languages){ |
||
283 | if(!dh.languages[key].defaultMode){
|
||
284 | continue;
|
||
285 | } |
||
286 | var _4e=new _10(key,_4d),_4f=_4e.keywordCount+_4e.relevance,_50=0; |
||
287 | if(!_4a||_4f>_50){
|
||
288 | _50=_4f; |
||
289 | _4a=_4e.result; |
||
290 | _4b=_4e.langName; |
||
291 | } |
||
292 | } |
||
293 | return {result:_4a,langName:_4b}; |
||
294 | }; |
||
295 | function _51(_52){ |
||
296 | var _53=_49(_7(_52));
|
||
297 | if(_53.result){
|
||
298 | _3c(_52,_53.langName,_53.result); |
||
299 | } |
||
300 | }; |
||
301 | dojox.highlight.processString=function(str,_54){ |
||
302 | return _54?_42(_54,str):_49(str);
|
||
303 | }; |
||
304 | dojox.highlight.init=function(_55){ |
||
305 | _55=dojo.byId(_55); |
||
306 | if(dojo.hasClass(_55,"no-highlight")){ |
||
307 | return;
|
||
308 | } |
||
309 | if(!_4(_55)){
|
||
310 | return;
|
||
311 | } |
||
312 | var _56=_55.className.split(/\s+/),_57=dojo.some(_56,function(_58){ |
||
313 | if(_58.charAt(0)!="_"&&dh.languages[_58]){ |
||
314 | _45(_55,_58); |
||
315 | return true; |
||
316 | } |
||
317 | return false; |
||
318 | }); |
||
319 | if(!_57){
|
||
320 | _51(_55); |
||
321 | } |
||
322 | }; |
||
323 | dh.Code=function(p,n){ |
||
324 | dh.init(n); |
||
325 | }; |
||
326 | })(); |
||
327 | } |