dpsyntaxhighlighter.js
来自「拓扑图基于SNMP数据采集模块的设计和实现」· JavaScript 代码 · 共 736 行 · 第 1/2 页
JS
736 行
cell.processedCode = this.code; cell.className = 'tools'; if(this.collapse == true) { tBody.className = 'hide'; cell.innerHTML += '<span><b>' + UtilHref('Expand', dp.sh.Strings.ExpandCode) + '</b>' + pipe + '</span>'; } cell.innerHTML += UtilHref('ViewSource', dp.sh.Strings.ViewPlain) ; // IE has this clipboard object which is easy enough to use if(window.clipboardData) cell.innerHTML += pipe + UtilHref('ToClipboard', dp.sh.Strings.CopyToClipboard); } for(var i = 0; i < lines.length - 1; i++) { row = tBody.insertRow(-1); if(this.addGutter == true) { cell = row.insertCell(-1); cell.className = 'gutter'; cell.innerHTML = i + 1; } cell = row.insertCell(-1); cell.className = 'line' + (i % 2 + 1); // uses .line1 and .line2 css styles for alternating lines cell.innerHTML = lines[i]; } this.div.innerHTML = '';}dp.sh.Highlighter.prototype.Highlight = function(code){ function Trim(str) { return str.replace(/^\s*(.*?)[\s\n]*$/g, '$1'); } function Chop(str) { return str.replace(/\n*$/, '').replace(/^\n*/, ''); } function Unindent(str) { var lines = str.split('\n'); var indents = new Array(); var regex = new RegExp('^\\s*', 'g'); var min = 1000; // go through every line and check for common number of indents for(var i = 0; i < lines.length && min > 0; i++) { if(Trim(lines[i]).length == 0) continue; var matches = regex.exec(lines[i]); if(matches != null && matches.length > 0) min = Math.min(matches[0].length, min); } // trim minimum common number of white space from the begining of every line if(min > 0) for(var i = 0; i < lines.length; i++) lines[i] = lines[i].substr(min); return lines.join('\n'); } // This function returns a portions of the string from pos1 to pos2 inclusive function Copy(string, pos1, pos2) { return string.substr(pos1, pos2 - pos1); } var pos = 0; this.originalCode = code; this.code = Chop(Unindent(code)); this.div = document.createElement('DIV'); this.table = document.createElement('TABLE'); this.matches = new Array(); if(this.CssClass != null) this.table.className = this.CssClass; // replace tabs with spaces if(this.tabsToSpaces == true) this.code = this.ProcessSmartTabs(this.code); this.table.border = 0; this.table.cellSpacing = 0; this.table.cellPadding = 0; this.ProcessRegexList(); // if no matches found, add entire code as plain text if(this.matches.length == 0) { this.AddBit(this.code, null); this.SwitchToTable(); return; } // sort the matches this.matches = this.matches.sort(dp.sh.Highlighter.SortCallback); // The following loop checks to see if any of the matches are inside // of other matches. This process would get rid of highligting strings // inside comments, keywords inside strings and so on. for(var i = 0; i < this.matches.length; i++) if(this.IsInside(this.matches[i])) this.matches[i] = null; // Finally, go through the final list of matches and pull the all // together adding everything in between that isn't a match. for(var i = 0; i < this.matches.length; i++) { var match = this.matches[i]; if(match == null || match.length == 0) continue; this.AddBit(Copy(this.code, pos, match.index), null); this.AddBit(match.value, match.css); pos = match.index + match.length; } this.AddBit(this.code.substr(pos), null); this.SwitchToTable();}dp.sh.Highlighter.prototype.GetKeywords = function(str) { return '\\b' + str.replace(/ /g, '\\b|\\b') + '\\b';}// highlightes all elements identified by name and gets source code from specified propertydp.sh.HighlightAll = function(name, showGutter /* optional */, showControls /* optional */, collapseAll /* optional */){ function FindValue() { var a = arguments; for(var i = 0; i < a.length; i++) { if(a[i] == null) continue; if(typeof(a[i]) == 'string' && a[i] != '') return a[i] + ''; if(typeof(a[i]) == 'object' && a[i].value != '') return a[i].value + ''; } return null; } function IsOptionSet(value, list) { for(var i = 0; i < list.length; i++) if(list[i] == value) return true; return false; } var elements = document.getElementsByName(name); var highlighter = null; var registered = new Object(); var propertyName = 'value'; // if no code blocks found, leave if(elements == null) return; // register all brushes for(var brush in dp.sh.Brushes) { var aliases = dp.sh.Brushes[brush].Aliases; if(aliases == null) continue; for(var i = 0; i < aliases.length; i++) registered[aliases[i]] = brush; } for(var i = 0; i < elements.length; i++) { var element = elements[i]; var options = FindValue( element.attributes['class'], element.className, element.attributes['language'], element.language ); var language = ''; if(options == null) continue; options = options.split(':'); language = options[0].toLowerCase(); if(registered[language] == null) continue; // instantiate a brush highlighter = new dp.sh.Brushes[registered[language]](); // hide the original element element.style.display = 'none'; highlighter.addGutter = (showGutter == null) ? !IsOptionSet('nogutter', options) : showGutter; highlighter.addControls = (showControls == null) ? !IsOptionSet('nocontrols', options) : showControls; highlighter.collapse = (collapseAll == null) ? IsOptionSet('collapse', options) : collapseAll; highlighter.Highlight(element[propertyName]); // place the result table inside a div var div = document.createElement('DIV'); div.className = 'dp-highlighter'; div.appendChild(highlighter.table); element.parentNode.insertBefore(div, element); } }dp.sh.Brushes.JScript = function(){ var keywords = 'abstract boolean break byte case catch char class const continue debugger ' + 'default delete do double else enum export extends false final finally float ' + 'for function goto if implements import in instanceof int interface long native ' + 'new null package private protected public return short static super switch ' + 'synchronized this throw throws transient true try typeof var void volatile while with'; this.regexList = [ { regex: new RegExp('//.*$', 'gm'), css: 'comment' }, // one line comments { regex: new RegExp('/\\*[\\s\\S]*?\\*/', 'g'), css: 'comment' }, // multiline comments { regex: new RegExp('"(?:[^"\n]|[\"])*"', 'g'), css: 'string' }, // double quoted strings { regex: new RegExp("'(?:[^'\n]|[\'])*'", 'g'), css: 'string' }, // single quoted strings { regex: new RegExp('^\\s*#.*', 'gm'), css: 'preprocessor' }, // preprocessor tags like #region and #endregion { regex: new RegExp(this.GetKeywords(keywords), 'gm'), css: 'keyword' } // keywords ]; this.CssClass = 'dp-c';}dp.sh.Brushes.JScript.prototype = new dp.sh.Highlighter();dp.sh.Brushes.JScript.Aliases = ['js', 'jscript', 'javascript'];dp.sh.Brushes.Php = function(){ var keywords = 'and or xor __FILE__ __LINE__ array as break case ' + 'cfunction 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 use ' + 'var while __FUNCTION__ __CLASS__'; this.regexList = [ { regex: new RegExp('//.*$', 'gm'), css: 'comment' }, // one line comments { regex: new RegExp('/\\*[\\s\\S]*?\\*/', 'g'), css: 'comment' }, // multiline comments { regex: new RegExp('"(?:[^"\n]|[\"])*"', 'g'), css: 'string' }, // double quoted strings { regex: new RegExp("'(?:[^'\n]|[\'])*'", 'g'), css: 'string' }, // single quoted strings { regex: new RegExp('\\$\\w+', 'g'), css: 'vars' }, // variables { regex: new RegExp(this.GetKeywords(keywords), 'gm'), css: 'keyword' } // keyword ]; this.CssClass = 'dp-c';}dp.sh.Brushes.Php.prototype = new dp.sh.Highlighter();dp.sh.Brushes.Php.Aliases = ['php'];dp.sh.Brushes.Xml = function(){ this.CssClass = 'dp-xml';}dp.sh.Brushes.Xml.prototype = new dp.sh.Highlighter();dp.sh.Brushes.Xml.Aliases = ['xml', 'xhtml', 'xslt', 'html', 'xhtml'];dp.sh.Brushes.Xml.prototype.ProcessRegexList = function(){ function push(array, value) { array[array.length] = value; } /* If only there was a way to get index of a group within a match, the whole XML could be matched with the expression looking something like that: (<!\[CDATA\[\s*.*\s*\]\]>) | (<!--\s*.*\s*?-->) | (<)*(\w+)*\s*(\w+)\s*=\s*(".*?"|'.*?'|\w+)(/*>)* | (</?)(.*?)(/?>) */ var index = 0; var match = null; var regex = null; // Match CDATA in the following format <![ ... [ ... ]]> // <\!\[[\w\s]*?\[(.|\s)*?\]\]> this.GetMatches(new RegExp('<\\!\\[[\\w\\s]*?\\[(.|\\s)*?\\]\\]>', 'gm'), 'cdata'); // Match comments // <!--\s*.*\s*?--> this.GetMatches(new RegExp('<!--\\s*.*\\s*?-->', 'gm'), 'comments'); // Match attributes and their values // (\w+)\s*=\s*(".*?"|\'.*?\'|\w+)* regex = new RegExp('([\\w-\.]+)\\s*=\\s*(".*?"|\'.*?\'|\\w+)*', 'gm'); while((match = regex.exec(this.code)) != null) { push(this.matches, new dp.sh.Match(match[1], match.index, 'attribute')); // if xml is invalid and attribute has no property value, ignore it if(match[2] != undefined) { push(this.matches, new dp.sh.Match(match[2], match.index + match[0].indexOf(match[2]), 'attribute-value')); } } // Match opening and closing tag brackets // </*\?*(?!\!)|/*\?*> this.GetMatches(new RegExp('</*\\?*(?!\\!)|/*\\?*>', 'gm'), 'tag'); // Match tag names // </*\?*\s*(\w+) regex = new RegExp('</*\\?*\\s*([\\w-\.]+)', 'gm'); while((match = regex.exec(this.code)) != null) { push(this.matches, new dp.sh.Match(match[1], match.index + match[0].indexOf(match[1]), 'tag-name')); }}dp.sh.Brushes.CSS = function(){ var keywords = 'link over active visited'; this.regexList = [ { regex: new RegExp('/\\*[\\s\\S]*?\\*/', 'g'), css: 'comment' }, // multiline comments { regex: new RegExp('"(?:[^"\n]|[\"])*"', 'g'), css: 'string' }, // double quoted strings { regex: new RegExp("'(?:[^'\n]|[\'])*'", 'g'), css: 'string' }, // single quoted strings { regex: new RegExp('^\\s*#.*', 'gm'), css: 'preprocessor' }, // preprocessor tags like #region and #endregion { regex: new RegExp(this.GetKeywords(keywords), 'gm'), css: 'keyword' } // keywords ]; this.CssClass = 'dp-c';}dp.sh.Brushes.CSS.prototype = new dp.sh.Highlighter();dp.sh.Brushes.CSS.Aliases = ['css'];
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?