-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHighlight.js
171 lines (159 loc) · 5.5 KB
/
Highlight.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
(function(G){
/*
简单语法高亮器
作者:蜗眼 <[email protected]>
*/
var Highlight = function(){},
rnewline = /\n/g,
rtabline = /\t/g,
regsafe = /([.*?\/\\])/g,
rregexp = /([^\w\\]|^)(\/.+\/)([igm]*)?([^\w]|$)/,
rnumber = /([^\w]|^)(\d+)([^\w]|$)/g,
rstring = /([^\w\\]|^)(['"].*?['"])([^\w]|$)/g;
Highlight.prototype = {
//注册语法
addSyntax: function(type,syntax){
this['@syntax'] || (this['@syntax']={});
this['@syntax'][type] = syntax;
},
//高亮所有
HighlightAll: function(){
var pres = document.getElementsByTagName('pre'),
type ,item ,idx=0;
while(item = pres[idx++]){
this.Hightight(item,item.getAttribute('class')
|| item.getAttribute('className')||'jscript');
}
},
//初始化高亮
Hightight: function(pre,type,pop){
if (this.nSyntax = this['@syntax'][type]){
this.cutter = (pre.innerHTML+'').
replace(rtabline,' ').split( rnewline );//标记切割后的行
this.replace = [];//标记替换对象
this.multideep = 0;//标记当前多行注释深度
(pop=this.cutter.pop()) && (this.cutter.push(pop));
//多行注释
this.multi = this.nSyntax['multicomments'];
this.multi = this.multi && this.multi.split(/\s+/);
var idx =0,item;
while((item = this.cutter[idx])!==undefined){
this.cutter[idx++] = this.highLine(item);
}
this.rander(pre);
}
},
//缓存这个 正则变量
regexp: function(){
var cache = {};
return function(key,regexp){
return cache[key] || (cache[key]= new RegExp(regexp,'g'))
}
}(),
//正则安全过滤
regsafe: function(){
var cache = {};
return function(text){
return cache[text] || (cache[text]=text.replace(regsafe,"\\$1"))
}
}(),
//格式化字符串
format: function(str,type){
return '<span class="'+type+'">'+str+'</span>';
},
//渲染数据
rander: function(pre){
var html= '<ol><li><div>'+this.cutter.join('</div></li><li><div>')+'</div></li></ol>',
idx = this.replace.length-1 ,item;
//标记数字
html = html.replace(rnumber,function(_,$1,$2,$3){
return $1+Highlighter.format($2,'number')+$3;
});
html = this.builword('keyword',this.builword('builtin',html));
while(item = this.replace[idx]){
html = html.replace('{[{[S'+(idx--)+'E]}]}',this.format(item[1],item[0]));
}
pre.innerHTML = html;
},
//高亮行
highLine: function( line ,h){
//1、处理字符串
line = line.replace(rstring,function(_,$1,$2,$3){
Highlighter.replace.push(['string',$2]);
return $1+'{[{[S'+(Highlighter.replace.length-1)+'E]}]}'+$3;
});
/*
* 单行注释在多行注释内失效
* 2、处理多行注释
*/
//看看有没有多行注释 /*
if (this.multi && line.indexOf(this.multi[0])>-1) {
//如果这里只是多行注释的开始的时候,而不是嵌套注释
var ml = line.match(this.regexp('mutiL',this.regsafe(this.multi[0]))),
mr = line.match(this.regexp('mutiR',this.regsafe(this.multi[1])))||[];
if((this.multideep += ml.length-mr.length) <=1){
line = this.highrep(line,'comments','multiRight',
this.regsafe(this.multi[0])+'.*?(?:'+this.regsafe(this.multi[1])+'|$)');
}
}
if ( this.multideep ) {//如果在多汗注释里面,就直接返回注释
//判断是否具有多行注释结束符号 */
if (line.indexOf(this.multi[1])==-1){
return this.format(line,'comments');
}
this.multideep--;
//还在多行注释里面,将前面的注释过滤掉。
line = this.highrep(line,'comments','multiLeft','^.*'+this.regsafe(this.multi[1]));
}
//3、处理单行注释
if ( this.nSyntax.singlecomments &&
line.indexOf(this.nSyntax.singlecomments)>-1){
line = this.highrep(line,'singlecomments','singlec',
this.regsafe(this.nSyntax.singlecomments)+'.*');
}
//4、处理正则
return line.replace(rregexp,function(_,$1,$2,$3,$4){
$3 && ($2+=$3);
Highlighter.replace.push(['regexp',$2]);
return $1+'{[{[S'+(Highlighter.replace.length-1)+'E]}]}'+$4;
}).replace(/\s/g,' ');
},
builword: function(type,line){
var builword = this.nSyntax[type],
idx =0 ,item;
if ( builword ) {
while (item = builword[idx++]) {
line = line.replace(this.regexp(item,'\\b'+this.regsafe(item)+'\\b'),
function(){
return Highlighter.format(item,type);
});
}
}
return line;
},
highrep: function(line,type,regtype,regexp){
return line.replace(this.regexp(regtype,regexp),
function($1){
Highlighter.replace.push([type,$1]);
return '{[{[S'+(Highlighter.replace.length-1)+'E]}]}';
});
}
};
G.Highlighter = new Highlight;
//加入js语法
Highlighter.addSyntax('jscript',{
//多行注释
multicomments: '/* */',
//单行注释
singlecomments: '//',
//关键字
keyword: 'new,var,function,arguments,return,delete,continue,break,this,prototype,typeof,case,do,if,else,switch,catch,try,null,default,for,finally,debugger,true,false,while,void'.split(','),
//内建对象函数,当标记为true的时候,表示该内建对象为window上的所有元素
builtin: function(keys){
for(var p in window){
keys.push(p);
}
return keys;
}([])
});
})(this)