欢迎访问 水平网    今天是:2017年11月19日 注册 | 登录 | 订阅 | 收藏
>> JavaScript >> 代码高亮控件
推荐代码
热点代码

代码高亮控件

作者:王集鹄,  来源:网络,  阅读:2048,  发布时间:2014-05-14  【放入收藏夹
完整演示代码:http://www.renrousousuo.com/scripts/Highlight.html

包括了:html(xml)、javascript、php、css、csharp、delphi
代码:javascript复制
var Highlight = {
	analyze: function(code, lang) {
		if (!lang) return null;
		var result = [];
		var find = true;
		var last = "";
		while (find && code) {
			find = false;
			var j;
			for (var i = 0; i < lang.syntaxs.length; i++) {
				if (lang.syntaxs[i].start) {
					var exists = false;
					for (j = 0; j < lang.syntaxs[i].start.length; j++) {
						if (last == lang.syntaxs[i].start[j]) {
							exists = true;
							break;
						}
					}
					if (!exists) continue;
				}
				var match = lang.syntaxs[i].pattren.exec(code);
				if (match) {
					var methods = lang.syntaxs[i].methods;
					if (methods) {
						for (j = 0; j < methods.length; j++) {
							var method = methods[j];
							if (!method) continue;
							if (!match[j + 1]) continue;
							if (this[method] && this[method].syntaxs) {
								var items = this.analyze(match[j + 1], this[method]);
								for (var k = 0; k < items.length; k++) {
									result.push(items[k]);
								}
							} else result.push({ name: method, text: match[j + 1] });
						}
					} else {
						if (lang.syntaxs[i].name) {
							var item = { name: lang.syntaxs[i].name, text: match[0] };
							if (item.name == "Identifier" && lang.identifiers) {
								for (j = 0; j < lang.identifiers.length; j++)
									if (lang.identifiers[j].pattren.test(item.text))
										item.name = lang.identifiers[j].name;
							}
							if (!/^(Whitespace|LineComment|MultiComment)$/i.test(item.name)
								&& !/\)$/.test(item.text)) last = item.name;
							result.push(item);
						}
					}
					code = code.substr(match[0].length);
					find = true;
					break;
				}
			}
		}
		if (!find && code) result.push({ name: "Unknown", text: code });
		return result;
	}
	, exportHtml: function(code, lang) {
		var items = this.analyze(code, lang);
		var html = [];
		html.push("<pre class=\"Code\"><code>");
		for (var i = 0; i < items.length; i++)
			html.push("<span class=\"" + items[i].name + "\">"
				+ items[i].text.replace(/</g, "&lt;").replace(/>/g, "&gt;") 
				+ "</span>");
		html.push("</pre></code>");
		return html.join("");
	}
	, attribute: {
		syntaxs: [			   
			{ pattren: /^\s+/, name: "Whitespace" }
			, { pattren: /^(=|:|;|\/)/, name: "Symbol" }
			, { pattren: /^('[^']*'|"[^"]*")/, name: "String" }
			, { pattren: /^[\w\/\-_]+/, name: "Variant" }
		]
	}
	, property: {
		syntaxs: [			   
			{ pattren: /^\s+/, name: "Whitespace" }
			, { pattren: /^\/\*[\s\S]*?\*\//, name: "MultiComment" }
			, { pattren: /^([a-zA-Z0-9_\-]+)(\s*)(:)(\s*)([^;]*)(;|$)/, methods: ["Variant", "Whitespace", "Symbol", "Whitespace", "String", "Symbol"] }
		]
	}
	, css: {
		syntaxs: [			   
			{ pattren: /^\s+/, name: "Whitespace" }
			, { pattren: /^\/\*[\s\S]*?\*\//, name: "MultiComment" }
			, { pattren: /^(((\.|#)[a-zA-Z0-9_\-]+[\s,]*)+)(\{)([\s\S]*?)(\})/,  methods: ["Function", null, null, "Symbol", "property", "Symbol"] }
			
		]
	}
	, javascript: {
		syntaxs: [
			{ pattren: /^\s+/, name: "Whitespace" }
			, { pattren: /^\/\/.*/, name: "LineComment" }
			, { pattren: /^\/\*[\s\S]*?\*\//, name: "MultiComment" }
			, { pattren: /^\/([^\\\/\n\r]*(\\.)*)*\/(i|m|g)*/, name: "Regex", start: ["", "Symbol"] }
			, { pattren: /^(~|\+|\-|\*|\(|\)|\[|\]|\{|\}|\||\;|:|=|<|>|\.|\,|\%|\!|&|\?|\^)+/, name: "Symbol" }
			, { pattren: /^\//, name: "Symbol" }
			, { pattren: /^(\d+(?!\.|x|e|d|m)u?)|^0x([\da-fA-F]+(?!\.|x|m)u?)|^NaN/, name: "Number" }
			, { pattren: /^(\d+)?\.\d+((\+|\-)?e\d+)?(m|d|f)?|^\d+((\+|\-)?e(\+|\-)?\d+)?(m|d|f)?/, name: "Float" }
			, { pattren: /^"([^\\"]*(\\.)*)*"/, name: "String" }			  
			, { pattren: /^'([^\\']*(\\.)*)*'/, name: "String" }
			, { pattren: /^[\w$_]+/, name: "Identifier" }
		]
		, identifiers: [
			{ pattren: /^(break|delete|function|return|typeof|case|do|if|switch|var|catch|else|in|this|void|continue|false|instanceof|throw|while|debugger)$/, name: "Reserved" }
			, { pattren: /^(finally|new|true|with|default|for|null|try|abstract|double|goto|native|static|boolean|enum|implements|package|super|byte|prototype)$/, name: "Reserved" }
			, { pattren: /^(export|import|private|synchronized|char|extends|int|protected|throws|class|final|interface|public|transient|const|float|long|short|volatile)$/, name: "Reserved" }
			, { pattren: /^(Array|String|Boolean|undefined|Object|Enumerator|Error|Math)$/, name: "Type" }
		]
	}
	, xhtml: {
		syntaxs: [			   
			{ pattren: /^\s+/, name: "Whitespace" }
			, { pattren: /^<!--[\s\S]*?-->/, name: "MultiComment" }
			, { pattren: /^(<!)([\s\S]*?)(>)/, methods: ["Symbol", "attribute", "Symbol"] }
			, { pattren: /^(<\?)(xml\b)([\s\S]*?)(\?>)/, methods: ["Symbol", "Reserved", "attribute", "Symbol"] }
			, { pattren: /^(<\?)(php\b)(.*?)(\?>)/, methods: ["Symbol", "Reserved", "php", "Symbol"] }
			, { pattren: /^(<\?)(php\b)([\s\S]*?)(\n\?>|$)/, methods: ["Symbol", "Reserved", "php", "Symbol"] }
			, { pattren: /^(<%@)([\s\S]*?)(%>)/, methods: ["Symbol", "attribute", "Symbol"] }
			, { pattren: /^(<%)([\s\S]*?)(%>)/, methods: ["Symbol", "Text", "Symbol"] }
			, { pattren: /^(<)(style\b)(([^>"']*(".*?"|'.*?')*)*?)(>)([\s\S]*?)(<\/)(style)(>)/i, methods: ["Symbol", "Type", "attribute", null, null, "Symbol", "css", "Symbol", "Type", "Symbol"] }
			, { pattren: /^(<)(script\b)(([^>"']*(".*?"|'.*?')*)*?)(>)([\s\S]*?)(<\/)(script)(>)/i, methods: ["Symbol", "Type", "attribute", null, null, "Symbol", "javascript", "Symbol", "Type", "Symbol"] }
			, { pattren: /^(<\/?)([\w\-]+)(([^>"']*(".*?"|'.*?')*)*?)(>)/i, methods: ["Symbol", "Type", "attribute", null, null, "Symbol"] }
			, { pattren: /^[^<]+/i, name: "Text" }
		]					 
	}
	, php: {
		syntaxs: [
			{ pattren: /^\s+/, name: "Whitespace" }
			, { pattren: /^(\/\/|#).*/, name: "LineComment" }
			, { pattren: /^\/\*[\s\S]*?\*\//, name: "MultiComment" }
			, { pattren: /^(~|\+|\-|\*|\(|\)|\[|\]|\{|\}|\||\;|:|=|<|>|\.|\,|\%|\!|&|\?|\^|@)+/, name: "Symbol" }
			, { pattren: /^\//, name: "Symbol" }
			, { pattren: /^(\d+(?!\.|x|e|d|m)u?)|^0x([\da-fA-F]+(?!\.|x|m)u?)|^NaN/, name: "Number" }
			, { pattren: /^(\d+)?\.\d+((\+|\-)?e\d+)?(m|d|f)?|^\d+((\+|\-)?e(\+|\-)?\d+)?(m|d|f)?/, name: "Float" }
			, { pattren: /^"([^\\"]*(\\.)*)*"/, name: "String" }			  
			, { pattren: /^'([^\\']*(\\.)*)*'/, name: "String" }
			, { pattren: /^\$[\w_]*/, name: "Variant" }
			, { pattren: /^[\w_]+/, name: "Identifier" }
		]
		, identifiers: [
			{ pattren: /^(require_once|include|and|or|xor|__FILE__|exception|php_user_filter|__LINE__|array|as|break|case|cfunction|class|const|continue|declare|default|die|do|each|echo|else|elseif|empty)$/, name: "Reserved" }
			, { pattren: /^(enddeclare|endfor|endforeach|endif|endswitch|endwhile|eval|exit|extends|for|foreach|function|global|if|isset|list|new|old_function|print|return|static|switch|unset|use|var|while|__FUNCTION__|__CLASS__|__METHOD__)$/, name: "Reserved" }
			, { pattren: /^(NULL|FALSE|TRUE)$/, name: "Variant" }
		]
	}
};
Tags:高亮
评论【共有0条评论】查看所有评论
昵称:(*)   邮箱:   QQ:   验证码: 看不清楚?点击刷新验证码