db.sh

js 代码
 
  1. /** 
  2.  * Code Syntax Highlighter. 
  3.  * Version 1.3.0 
  4.  * Copyright (C) 2004 Alex Gorbatchev. 
  5.  * http://www.dreamprojections.com/syntaxhighlighter/ 
  6.  *  
  7.  * This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General  
  8.  * Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option)  
  9.  * any later version. 
  10.  * 
  11.  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied  
  12.  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more  
  13.  * details. 
  14.  * 
  15.  * You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to  
  16.  * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA  
  17.  */  
  18.   
  19. //  
  20. // create namespaces  
  21. //  
  22. var dp = {  
  23.     sh :  
  24.     {  
  25.         Toolbar : {},  
  26.         Utils   : {},  
  27.         RegexLib: {},  
  28.         Brushes : {},  
  29.         Strings : {},  
  30.         Version : '1.4.1'  
  31.     }  
  32. };  
  33.   
  34. dp.sh.Strings = {  
  35.     AboutDialog : '<html><head><title>About...</title></head><body class="dp-about"><table cellspacing="0"><tr><td class="copy"><p class="title">dp.SyntaxHighlighter</div><div class="para">Version: {V}</p><p><a href="http://www.dreamprojections.com/syntaxhighlighter/?ref=about" target="_blank">http://www.dreamprojections.com/SyntaxHighlighter</a></p>&copy;2004-2005 Alex Gorbatchev. All right reserved.</td></tr><tr><td class="footer"><input type="button" class="close" value="OK" onClick="window.close()"/></td></tr></table></body></html>'  
  36. };  
  37.   
  38. dp.SyntaxHighlighter = dp.sh;  
  39.   
  40. //  
  41. // Toolbar functions  
  42. //  
  43.   
  44. dp.sh.Toolbar.Commands = {  
  45.     ExpandSource: {  
  46.         label: '+ expand source',  
  47.         check: function(highlighter) { return highlighter.collapse; },  
  48.         func: function(sender, highlighter)  
  49.         {  
  50.             sender.parentNode.removeChild(sender);  
  51.             highlighter.div.className = highlighter.div.className.replace('collapsed', '');  
  52.         }  
  53.     },  
  54.       
  55.     // opens a new windows and puts the original unformatted source code inside.  
  56.     ViewSource: {  
  57.         label: 'view plain',  
  58.         func: function(sender, highlighter)  
  59.         {  
  60.             var code = highlighter.originalCode.replace(/</g, '&lt;');  
  61.             var wnd = window.open('', '_blank', 'width=750, height=400, location=0, resizable=1, menubar=0, scrollbars=1');  
  62.             wnd.document.write('<textarea style="width:99%;height:99%">' + code + '</textarea>');  
  63.             wnd.document.close();  
  64.         }  
  65.     },  
  66.       
  67.     // copies the original source code in to the clipboard (IE only)  
  68.     CopyToClipboard: {  
  69.         label: 'copy to clipboard',  
  70.         check: function() { return window.clipboardData != null; },  
  71.         func: function(sender, highlighter)  
  72.         {  
  73.             window.clipboardData.setData('text', highlighter.originalCode);  
  74.             alert('The code is in your clipboard now');  
  75.         }  
  76.     },  
  77.       
  78.     // creates an invisible iframe, puts the original source code inside and prints it  
  79.     PrintSource: {  
  80.         label: 'print',  
  81.         func: function(sender, highlighter)  
  82.         {  
  83.             var iframe = document.createElement('IFRAME');  
  84.             var doc = null;  
  85.   
  86.             // this hides the iframe  
  87.             iframe.style.cssText = 'position:absolute;width:0px;height:0px;left:-500px;top:-500px;';  
  88.               
  89.             document.body.appendChild(iframe);  
  90.             doc = iframe.contentWindow.document;  
  91.   
  92.             dp.sh.Utils.CopyStyles(doc, window.document);  
  93.             doc.write('<div class="' + highlighter.div.className.replace('collapsed', '') + ' printing">' + highlighter.div.innerHTML + '</div>');  
  94.             doc.close();  
  95.   
  96.             iframe.contentWindow.focus();  
  97.             iframe.contentWindow.print();  
  98.               
  99.             alert('Printing...');  
  100.               
  101.             document.body.removeChild(iframe);  
  102.         }  
  103.     },  
  104.       
  105.     About: {  
  106.         label: '?',  
  107.         func: function(highlighter)  
  108.         {  
  109.             var wnd = window.open('', '_blank', 'dialog,width=300,height=150,scrollbars=0');  
  110.             var doc = wnd.document;  
  111.   
  112.             dp.sh.Utils.CopyStyles(doc, window.document);  
  113.               
  114.             doc.write(dp.sh.Strings.AboutDialog.replace('{V}', dp.sh.Version));  
  115.             doc.close();  
  116.             wnd.focus();  
  117.         }  
  118.     }  
  119. };  
  120.   
  121. // creates a <div /> with all toolbar links  
  122. dp.sh.Toolbar.Create = function(highlighter)  
  123. {  
  124.     var div = document.createElement('DIV');  
  125.       
  126.     div.className = 'tools';  
  127.       
  128.     for(var name in dp.sh.Toolbar.Commands)  
  129.     {  
  130.         var cmd = dp.sh.Toolbar.Commands[name];  
  131.           
  132.         if(cmd.check != null && !cmd.check(highlighter))  
  133.             continue;  
  134.           
  135.         div.innerHTML += '<a href="#" οnclick="dp.sh.Toolbar.Command(\'' + name + '\',this);return false;">' + cmd.label + '</a>';  
  136.     }  
  137.       
  138.     return div;  
  139. }  
  140.   
  141. // executes toolbar command by name  
  142. dp.sh.Toolbar.Command = function(name, sender)  
  143. {  
  144.     var n = sender;  
  145.       
  146.     while(n != null && n.className.indexOf('dp-highlighter') == -1)  
  147.         n = n.parentNode;  
  148.       
  149.     if(n != null)  
  150.         dp.sh.Toolbar.Commands[name].func(sender, n.highlighter);  
  151. }  
  152.   
  153. // copies all <link rel="stylesheet" /> from 'target' window to 'dest'  
  154. dp.sh.Utils.CopyStyles = function(destDoc, sourceDoc)  
  155. {  
  156.     var links = sourceDoc.getElementsByTagName('link');  
  157.   
  158.     for(var i = 0; i < links.length; i++)  
  159.         if(links[i].rel.toLowerCase() == 'stylesheet')  
  160.             destDoc.write('<link type="text/css" rel="stylesheet" href="' + links[i].href + '"></link>');  
  161. }  
  162.   
  163. //  
  164. // Common reusable regular expressions  
  165. //  
  166. dp.sh.RegexLib = {  
  167.     MultiLineCComments : new RegExp('/\\*[\\s\\S]*?\\*/', 'gm'),  
  168.     SingleLineCComments : new RegExp('//.*$', 'gm'),  
  169.     SingleLinePerlComments : new RegExp('#.*$', 'gm'),  
  170.     DoubleQuotedString : new RegExp('"(?:\\.|(\\\\\\")|[^\\""])*"','g'), 
  171.     SingleQuotedString : new RegExp("'(?:\\.|(\\\\\\')|[^\\''])*'", 'g') 
  172. }; 
  173.  
  174. // 
  175. // Match object 
  176. // 
  177. dp.sh.Match = function(value, index, css) 
  178. { 
  179.     this.value = value; 
  180.     this.index = index; 
  181.     this.length = value.length; 
  182.     this.css = css; 
  183. } 
  184.  
  185. // 
  186. // Highlighter object 
  187. // 
  188. dp.sh.Highlighter = function() 
  189. { 
  190.     this.noGutter = false; 
  191.     this.addControls = true; 
  192.     this.collapse = false; 
  193.     this.tabsToSpaces = true; 
  194.     this.wrapColumn = 80; 
  195.     this.showColumns = true; 
  196. } 
  197.  
  198. // static callback for the match sorting 
  199. dp.sh.Highlighter.SortCallback = function(m1, m2) 
  200. { 
  201.     // sort matches by index first 
  202.     if(m1.index < m2.index) 
  203.         return -1; 
  204.     else if(m1.index > m2.index) 
  205.         return 1; 
  206.     else 
  207.     { 
  208.         // if index is the same, sort by length 
  209.         if(m1.length < m2.length) 
  210.             return -1; 
  211.         else if(m1.length > m2.length) 
  212.             return 1; 
  213.     } 
  214.     return 0; 
  215. } 
  216.  
  217. dp.sh.Highlighter.prototype.CreateElement = function(name) 
  218. { 
  219.     var result = document.createElement(name); 
  220.     result.highlighter = this; 
  221.     return result; 
  222. } 
  223.  
  224. // gets a list of all matches for a given regular expression 
  225. dp.sh.Highlighter.prototype.GetMatches = function(regex, css) 
  226. { 
  227.     var index = 0; 
  228.     var match = null; 
  229.  
  230.     while((match = regex.exec(this.code)) != null) 
  231.         this.matches[this.matches.length] = new dp.sh.Match(match[0], match.index, css); 
  232. } 
  233.  
  234. dp.sh.Highlighter.prototype.AddBit = function(str, css) 
  235. { 
  236.     if(str == null || str.length == 0) 
  237.         return; 
  238.  
  239.     var span = this.CreateElement('SPAN'); 
  240.      
  241.     str = str.replace(/&/g, '&amp;'); 
  242.     str = str.replace(/ /g, '&nbsp;'); 
  243.     str = str.replace(/</g, '&lt;'); 
  244.     str = str.replace(/\n/gm, '&nbsp;<br>'); 
  245.  
  246.     // when adding a piece of code, check to see if it has line breaks in it  
  247.     // and if it does, wrap individual line breaks with span tags 
  248.     if(css != null) 
  249.     { 
  250.         var regex = new RegExp('<br>', 'gi'); 
  251.          
  252.         if(regex.test(str)) 
  253.         { 
  254.             var lines = str.split('&nbsp;<br>'); 
  255.              
  256.             str = ''; 
  257.              
  258.             for(var i = 0; i < lines.length; i++) 
  259.             { 
  260.                 span = this.CreateElement('SPAN'); 
  261.                 span.className = css; 
  262.                 span.innerHTML = lines[i]; 
  263.                  
  264.                 this.div.appendChild(span); 
  265.                  
  266.                 // don't add a <BR> for the last line 
  267.                 if(i + 1 < lines.length) 
  268.                     this.div.appendChild(this.CreateElement('BR')); 
  269.             } 
  270.         } 
  271.         else 
  272.         { 
  273.             span.className = css; 
  274.             span.innerHTML = str; 
  275.             this.div.appendChild(span); 
  276.         } 
  277.     } 
  278.     else 
  279.     { 
  280.         span.innerHTML = str; 
  281.         this.div.appendChild(span); 
  282.     } 
  283. } 
  284.  
  285. // checks if one match is inside any other match 
  286. dp.sh.Highlighter.prototype.IsInside = function(match) 
  287. { 
  288.     if(match == null || match.length == 0) 
  289.         return false; 
  290.      
  291.     for(var i = 0; i < this.matches.length; i++) 
  292.     { 
  293.         var c = this.matches[i]; 
  294.          
  295.         if(c == null) 
  296.             continue; 
  297.  
  298.         if((match.index > c.index) && (match.index < c.index + c.length)) 
  299.             return true; 
  300.     } 
  301.      
  302.     return false; 
  303. } 
  304.  
  305. dp.sh.Highlighter.prototype.ProcessRegexList = function() 
  306. { 
  307.     for(var i = 0; i < this.regexList.length; i++) 
  308.         this.GetMatches(this.regexList[i].regex, this.regexList[i].css); 
  309. } 
  310.  
  311. dp.sh.Highlighter.prototype.ProcessSmartTabs = function(code) 
  312. { 
  313.     var lines   = code.split('\n'); 
  314.     var result  = ''; 
  315.     var tabSize = 4; 
  316.     var tab     = '\t'; 
  317.  
  318.     // This function inserts specified amount of spaces in the string 
  319.     // where a tab is while removing that given tab.  
  320.     function InsertSpaces(line, pos, count) 
  321.     { 
  322.         var left    = line.substr(0, pos); 
  323.         var right   = line.substr(pos + 1, line.length);    // pos + 1 will get rid of the tab 
  324.         var spaces  = ''; 
  325.          
  326.         for(var i = 0; i < count; i++) 
  327.             spaces += ' '; 
  328.          
  329.         return left + spaces + right; 
  330.     } 
  331.  
  332.     // This function process one line for 'smart tabs' 
  333.     function ProcessLine(line, tabSize) 
  334.     { 
  335.         if(line.indexOf(tab) == -1) 
  336.             return line; 
  337.  
  338.         var pos = 0; 
  339.  
  340.         while((pos = line.indexOf(tab)) != -1) 
  341.         { 
  342.             // This is pretty much all there is to the 'smart tabs' logic. 
  343.             // Based on the position within the line and size of a tab,  
  344.             // calculate the amount of spaces we need to insert. 
  345.             var spaces = tabSize - pos % tabSize; 
  346.              
  347.             line = InsertSpaces(line, pos, spaces); 
  348.         } 
  349.          
  350.         return line; 
  351.     } 
  352.  
  353.     // Go through all the lines and do the 'smart tabs' magic. 
  354.     for(var i = 0; i < lines.length; i++) 
  355.         result += ProcessLine(lines[i], tabSize) + '\n'; 
  356.      
  357.     return result; 
  358. } 
  359.  
  360. dp.sh.Highlighter.prototype.SwitchToList = function() 
  361. { 
  362.     // thanks to Lachlan Donald from SitePoint.com for this <br/> tag fix. 
  363.     var html = this.div.innerHTML.replace(/<(br)\/?>/gi, '\n'); 
  364.     var lines = html.split('\n'); 
  365.      
  366.     if(this.addControls == true) 
  367.         this.bar.appendChild(dp.sh.Toolbar.Create(this)); 
  368.  
  369.     // add columns ruler 
  370.     if(this.showColumns) 
  371.     { 
  372.         var div = this.CreateElement('div'); 
  373.         var columns = this.CreateElement('div'); 
  374.         var showEvery = 10; 
  375.         var i = 1; 
  376.          
  377.         while(i <= 150) 
  378.         { 
  379.             if(i % showEvery == 0) 
  380.             { 
  381.                 div.innerHTML += i; 
  382.                 i += (i + '').length; 
  383.             } 
  384.             else 
  385.             { 
  386.                 div.innerHTML += '&middot;'; 
  387.                 i++; 
  388.             } 
  389.         } 
  390.          
  391.         columns.className = 'columns'; 
  392.         columns.appendChild(div); 
  393.         this.bar.appendChild(columns); 
  394.     } 
  395.  
  396.     for(var i = 0, lineIndex = this.firstLine; i < lines.length - 1; i++, lineIndex++) 
  397.     { 
  398.         var li = this.CreateElement('LI'); 
  399.         var span = this.CreateElement('SPAN'); 
  400.          
  401.         // uses .line1 and .line2 css styles for alternating lines 
  402.         li.className = (i % 2 == 0) ? 'alt' : ''; 
  403.         span.innerHTML = lines[i] + '&nbsp;'; 
  404.  
  405.         li.appendChild(span); 
  406.         this.ol.appendChild(li); 
  407.     } 
  408.      
  409.     this.div.innerHTML  = ''; 
  410. } 
  411.  
  412. dp.sh.Highlighter.prototype.Highlight = function(code) 
  413. { 
  414.     function Trim(str) 
  415.     { 
  416.         return str.replace(/^\s*(.*?)[\s\n]*$/g, '$1'); 
  417.     } 
  418.      
  419.     function Chop(str) 
  420.     { 
  421.         return str.replace(/\n*$/, '').replace(/^\n*/, ''); 
  422.     } 
  423.  
  424.     function Unindent(str) 
  425.     { 
  426.         var lines = str.split('\n'); 
  427.         var indents = new Array(); 
  428.         var regex = new RegExp('^\\s*', 'g'); 
  429.         var min = 1000; 
  430.  
  431.         // go through every line and check for common number of indents 
  432.         for(var i = 0; i < lines.length && min > 0; i++) 
  433.         { 
  434.             if(Trim(lines[i]).length == 0) 
  435.                 continue; 
  436.                  
  437.             var matches = regex.exec(lines[i]); 
  438.  
  439.             if(matches != null && matches.length > 0) 
  440.                 min = Math.min(matches[0].length, min); 
  441.         } 
  442.  
  443.         // trim minimum common number of white space from the begining of every line 
  444.         if(min > 0) 
  445.             for(var i = 0; i < lines.length; i++) 
  446.                 lines[i] = lines[i].substr(min); 
  447.  
  448.         return lines.join('\n'); 
  449.     } 
  450.      
  451.     // This function returns a portions of the string from pos1 to pos2 inclusive 
  452.     function Copy(string, pos1, pos2) 
  453.     { 
  454.         return string.substr(pos1, pos2 - pos1); 
  455.     } 
  456.  
  457.     var pos = 0; 
  458.      
  459.     this.originalCode = code; 
  460. &nb
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值