⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 highlighter.js

📁 //--- 开发背景------------// 在javascript开发过程中
💻 JS
📖 第 1 页 / 共 5 页
字号:
/*
 * SyCODE Syntax Highlighter
 * Version 1.0.0
 * Copyright (C) 2007-2008 Muchool.com
 * http://www.muchool.com
 *
 * SyCODE Syntax Highlighter是一个基于javascript实现的语法高亮程序,实现方式借鉴了著名的db.SyntaxHighlighter采用
 * 正则表达式进行关键字匹配,处理速度比db.SyntaxHighlighter高出 5~10倍,是目前处理速度最快的javascript语法高亮程
 * 序。
 *	
 * SyCODE Syntax Highlighter具有速度高可扩展性强的特点,在一台当前主流PC机上可以轻松完成32KB的代码高亮处理,能够
 * 一次性完成64KB的代码高亮处理而不出现脚本缓慢提示,通过添加不同的正则表达式可以实现任何一种编程语言的语法高亮处
 * 理。
 *
 * SyCODE Syntax Highlighter 1.0 内部已经实现二十于种语系的语法高亮,包括:
 * 1. C/C++
 * 2. C#
 * 3. CSS
 * 4. Delphi/Kylix
 * 5. Pascal
 * 6. Java
 * 7. Vb/Vb.net
 * 8. J(ava)Script
 * 9. ActionScript
 * 10. Php
 * 11. Python
 * 12. Ruby/Rails
 * 13. Perl
 * 14. Assembly
 * 15. Bat 批处理
 * 16. UNIX Shell
 * 18. AWK
 * 19. Sql
 * 20. xml/xhtml
 *
 * example: Highlighter.Execute(cleanCode, language);
 *
 * This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General 
 * Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) 
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied 
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this library;if not, write to 
 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 */


var Highlighter = {
	Brushes: {},
	registered : null,
    hts : new Array(),
    ht : null,
	RegexLib: {
		MultiLineCComments : new RegExp('/\\*[\\s\\S]*?\\*/', 'gm'),
		SingleLineCComments : new RegExp('//.*$', 'gm'),
		SingleLinePerlComments : new RegExp('#.*$', 'gm'),
		DoubleQuotedString : new RegExp('"(?:\\.|(\\\\\\")|[^\\""\\n])*"','g'),
		SingleQuotedString : new RegExp("'(?:\\.|(\\\\\\')|[^\\''\\n])*'", 'g')
	},
	Match: function(value, index, css) {
		this.value = value;
		this.index = index;
		this.length = value.length;
		this.css = css;
	},
	Execute: function(str,lang) {
		if(this.registered == null){
			this.registered = new Object();
			for(var brush in Highlighter.Brushes){
				var aliases = Highlighter.Brushes[brush].Aliases;
				if(aliases == null) continue;
				for(var i=0;i<aliases.length;i++) this.registered[aliases[i].toLowerCase()] = brush;
			};
		}
		if(this.registered[lang.toLowerCase()]) {
            var isNotRegisted = true;
            for(var j=0;j<this.hts.length;j++){
                if(this.hts[j] == lang.toLowerCase()){
                    isNotRegisted = false;
                    break;
                }
            }
            if(isNotRegisted){
                ht = new Highlighter.Brushes[this.registered[lang.toLowerCase()]]();
                this.hts[this.hts.length] = lang.toLowerCase();
            }
			return ht.Highlight(str);
		}
		else {
			str = str.replace(/&/g, '&amp;');
			str = str.replace(/</g, '&lt;');
			str = str.replace(/>/g, '&gt;');
			str = str.replace(/\t/g, '&nbsp;&nbsp;&nbsp;&nbsp;');
			str = str.replace(/[ ]{2}/g, '&nbsp;&nbsp;');
			return str.replace(/\n/g, '<br/>');
		}
	}
};



Highlighter.Brushe = new Function();
Highlighter.Brushe.SortCallback = function(m1, m2) {
	if(m1.index < m2.index)
		return -1;
	else if(m1.index > m2.index)
		return 1;
	else
	{
		if(m1.length < m2.length)
			return -1;
		else if(m1.length > m2.length)
			return 1;
	}
	return 0;
}
Highlighter.Brushe.prototype = {
	GetMatches: function(regex, css) {
		var index = 0;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -