设为首页 收藏本站
查看: 488|回复: 0

[经验分享] JS实现的PHP语法加亮函数

[复制链接]

尚未签到

发表于 2017-4-3 11:49:29 | 显示全部楼层 |阅读模式
  function highlight_string( str )
{
//add a new prototype function to array
Array.prototype.exist = function(v)
{
for(k=0;k<this.length;k++)
{
if(this[k].toLowerCase() == v.toLowerCase())
return true;
}
return false;
}

//base variable
var operator = "><=,()[].+-*/!&|^~?{};:";
var keyword= ['and','or','__FILE__','exception','__LINE__','array','as','break','case','class','const',
'continue','declare','default','die','do','echo','else','elseif','empty','enddeclare','endfor',
'endforeach','endif','endswitch','endwhile','eval','exit','extends','for','foreach','function',
'global','if','include','include_once','isset','list','new','old_function','print','require',
'require_once','return', 'static','switch','unset','var','while','__FUNCTION__','__CLASS__',
'__METHOD__','true','false','null'];
var inString = false;
var inSLComment = false; //single line comment
var inMLComment = false; //multiline comment
var delimiter = null;
var startPos = null;
var word= "";
var res = "";
//start to format
for(i=0;i<str.length;i++)
{
if( inString ) //we are in string
{
//the word cache will be clear
if(word != "") //we check the word cache if it the key word
{
if( keyword.exist(word) ) //its php reversed keyword,rend color
res+= rendColor(word, 'keyword');
else
res+= word;
word = "";
}
//alert('inString,pos is '+ i+',char is '+c );
fromPos = startPos+1;
while(1)
{
//we find the end of current string
p = str.indexOf( delimiter, fromPos );

//we got the end of the code
if( p == -1 )
{
curstr = str.substr( startPos );
res += rendColor( curstr, 'string' );
i = str.length;
break;
}
if( p != -1 && str.charAt(p-1) != "//" )
{
i = p+1;
curstr= str.substring(startPos, i ); //get the current string
res += rendColor( curstr, 'string' ); //rend color for it and add it to the result
inString = false; //we have go out of the string
startPos = null;
break;
}
else
{
fromPos = p+1;
}
}
}
if( inSLComment ) //we are in Single line comment
{
if(word != "") //we check the word cache if it the key word
{
if( keyword.exist(word) ) //its php reversed keyword,rend color
res+= rendColor(word, 'keyword');
else
res+= word;
word = "";
}
//alert('inSLComment,pos is '+ i+',char is '+c );
p = str.indexOf( "/n", i );
if( p != -1 ) //we find the end of line
{
i = p;
curstr = str.substring( startPos, p );
res += rendColor( curstr, 'comment' );
startPos = null;
inSLComment = false;
}
else
{
curstr = str.substr( startPos );
res += rendColor( curstr, 'comment' );
i = str.length;
}
}
if( inMLComment ) //we are in multiline comment
{
if(word != "") //we check the word cache if it the key word
{
if( keyword.exist(word) ) //its php reversed keyword,rend color
res+= rendColor(word, 'keyword');
else
res+= word;
word = "";
}
//alert('inMLComment,pos is '+ i+',char is '+c );
p = str.indexOf( "*/", startPos+2 );
if( p != -1 ) //we find the end of line
{
i = p+2;
curstr = str.substring(startPos, i );
res += rendColor( curstr, 'comment' );
startPos = null;
inMLComment = false;
}
else
{
curstr = str.substr( startPos );
res += rendColor( curstr, 'comment' );
i = str.length;
}
}
var c= str.charAt(i); //current char
var nc = str.charAt(i+1);//next char
switch( c )
{
case '/':
if( nc == '*' ) // we go into the multiline comment
{
inMLComment = true;
startPos = i;
}
if( nc == "/" ) //we are in single line comment
{
inSLComment = true;
startPos = i;
}
//alert('we are in switch,pos is '+i+', and char is'+ c);
break;
case '#':
inSLComment = true; //we go into the single line comment
startPos = i;
break;
case '"':
inString = true;
delimiter = '"';
startPos = i;
break;
case "'":
inString = true;
delimiter = "'";
startPos = i;
break;
default:
if( /[/w$]/.test(c) )//the keyword only contains continuous common char
{
word += c; //cache the current char
}
else
{
if(word != "") //we check the word cache if it the key word
{
if( keyword.exist(word) ) //its php reversed keyword,rend color
res+= rendColor(word, 'keyword');
else
res+= word;
word = "";
}
//now the current char is not common char, we process it
if( operator.indexOf(c) != -1 ) // the char is a operator
res += rendColor(c, 'operator' );
else
res += c;
}
break;
}
}
$t = "";
$b = " ";
res = res.replace(/^( +)/g, function($1){c = $1.length;str="";while(--c>=0)str+=$b;return str});
res = res.replace(/(/t| ){2,}/g, function($0){c=$0.length;str="";while(--c>=0){if($0.charAt(c)=='/t')str+=$t;else str+=$b;}return str;});
res = res.replace(//t/g,$t);
res = res.replace(//n/g, "/n</li><li>");
res = '<ol><li>' + res + '</li></ol>';
//alert(res);
return res;
}
//对字符串中的HTML代码编码
function HTMLEncode( str )
{
str = str.replace(/&/g, '&');
str = str.replace(/</g, '&lt;');
str = str.replace(/>/g, '&gt;');
return str;
}
//根据字符串所属类型渲染不同的着色
function rendColor( str, type )
{
var commentColor = "#FF8000";
var stringColor= "#DD0000";
var operatorColor= "#007700";
var keywordColor = "#007700";
var commonColor= "#0000BB";
var useColor= null;
str = HTMLEncode( str );

//we will rend what color?
switch( type )
{
case 'comment':
useColor= commentColor;
break;
case 'string':
useColor = stringColor;
break;
case 'operator':
useColor= operatorColor;
break;
case 'keyword':
useColor= keywordColor;
break;
default:
useColor= commonColor;
break;
}
if( str.indexOf("/n") != -1 ) //there are more than one line
{
arr = str.split("/n");
for(j=0;j<arr.length;j++)
{
arr[j] = "<span style='color:"+ useColor +"'>"+ arr[j] + "</span>";
}
return arr.join("/n");
}
else
{
str = "<span style='color:"+ useColor +"'>"+ str + "</span>";
return str;
}
}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-359551-1-1.html 上篇帖子: php开发笔记9-重写,重载,继承 下篇帖子: php截取中英文字符串操作
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表