2 * Code Syntax Highlighter. Version 1.1.0
\r
3 * Copyright (C) 2004 Dream Projections Inc.
\r
5 * This program is free software; you can redistribute it and/or
\r
6 * modify it under the terms of the GNU General Public License
\r
7 * as published by the Free Software Foundation; either version 2
\r
8 * of the License, or (at your option) any later version.
\r
10 * This program is distributed in the hope that it will be useful,
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 * GNU General Public License for more details.
\r
15 * You should have received a copy of the GNU General Public License
\r
16 * along with this program; if not, write to the Free Software
\r
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
\r
22 * <script src="shCore.js" language="javascript"></script>
\r
23 * <script src="shBrushXml.js" language="javascript"></script>
\r
25 * <textarea name="code" language="html">
\r
26 * <img src="myimage.gif" border="0">
\r
29 * <script>dp.SyntaxHighlighter.HighlightAll('code', 'value');</script>
\r
33 * 1.1.0 - March 23rd, 2005
\r
34 * - split brushes into separate files
\r
35 * - now works in Safari
\r
36 * - added missing strings to PHP matches
\r
38 * 1.0.4 - February 2nd, 2005
\r
39 * - added Delphi & Python
\r
40 * - multi-line comments fixed
\r
41 * - language name can be set through w3c valid 'class' attribute
\r
42 * - HighlightAll(name, [showGutter], [showTools])
\r
44 * 1.0.3 - December 31th, 2004 (added PHP & SQL)
\r
45 * 1.0.2 - December 28th, 2004 (refactoring with namespaces)
\r
46 * 1.0.1 - December 14th, 2004
\r
47 * 1.0.0 - November 13th, 2004
\r
50 // create namespaces
\r
54 Utils : {}, // dp.sh.Utils
\r
55 Brushes : {} // dp.sh.Brushes
\r
61 About : '<html><head><title>About...</title></head><body class="dp-about"><table cellspacing="0"><tr><td class="copy"><div class="para title">dp.SyntaxHighlighter</div><div class="para">Version: {V}</div><div class="para"><a href="http://www.dreamprojections.com/sh/?ref=about" target="_blank">http://www.dreamprojections.com/SyntaxHighlighter</a></div>©2004-2005 Dream Projections Inc. All right reserved.</td></tr><tr><td class="footer"><input type="button" class="close" value="OK" onClick="window.close()"/></td></tr></table></body></html>'
\r
64 dp.SyntaxHighlighter = dp.sh;
\r
68 // opens a new windows and puts the original unformatted source code inside.
\r
69 dp.sh.Utils.ViewSource = function(sender)
\r
71 var code = sender.parentNode.originalCode;
\r
72 var wnd = window.open('', '_blank', 'width=750, height=400, location=0, resizable=1, menubar=0, scrollbars=1');
\r
74 code = code.replace(/</g, '<');
\r
76 wnd.document.write('<pre>' + code + '</pre>');
\r
77 wnd.document.close();
\r
80 // copies the original source code in to the clipboard (IE only)
\r
81 dp.sh.Utils.ToClipboard = function(sender)
\r
83 var code = sender.parentNode.originalCode;
\r
85 // This works only for IE. There's a way to make it work with Mozilla as well,
\r
86 // but it requires security settings changed on the client, which isn't by
\r
87 // default, so 99% of users won't have it working anyways.
\r
88 if(window.clipboardData)
\r
90 window.clipboardData.setData('text', code);
\r
92 alert('The code is in your clipboard now.');
\r
96 // creates an invisible iframe, puts the original source code inside and prints it
\r
97 dp.sh.Utils.PrintSource = function(sender)
\r
99 var td = sender.parentNode;
\r
100 var code = td.processedCode;
\r
101 var iframe = document.createElement('IFRAME');
\r
105 // this hides the iframe
\r
106 iframe.style.cssText = 'position:absolute; width:0px; height:0px; left:-5px; top:-5px;';
\r
108 td.appendChild(iframe);
\r
110 doc = iframe.contentWindow.document;
\r
111 code = code.replace(/</g, '<');
\r
114 doc.write('<pre>' + code + '</pre>');
\r
117 iframe.contentWindow.focus();
\r
118 iframe.contentWindow.print();
\r
120 td.removeChild(iframe);
\r
123 dp.sh.Utils.About = function()
\r
125 var wnd = window.open('', '_blank', 'dialog, width=320, height=150');
\r
126 var doc = wnd.document;
\r
128 var styles = document.getElementsByTagName('style');
\r
129 var links = document.getElementsByTagName('link');
\r
131 doc.write(dp.sh.Config.About.replace('{V}', dp.sh.Config.Version));
\r
133 // copy over ALL the styles from the parent page
\r
134 for(var i = 0; i < styles.length; i++)
\r
135 doc.write('<style>' + styles[i].innerHTML + '</style>');
\r
137 for(var i = 0; i < links.length; i++)
\r
138 if(links[i].rel.toLowerCase() == 'stylesheet')
\r
139 doc.write('<link type="text/css" rel="stylesheet" href="' + links[i].href + '"></link>');
\r
149 // creates a new match object
\r
150 dp.sh.Match = function(value, index, css)
\r
152 this.value = value;
\r
153 this.index = index;
\r
154 this.length = value.length;
\r
162 dp.sh.Highlighter = function()
\r
164 this.addGutter = true;
\r
165 this.addControls = true;
\r
166 this.tabsToSpaces = true;
\r
169 // static callback for the match sorting
\r
170 dp.sh.Highlighter.SortCallback = function(m1, m2)
\r
172 // sort matches by index first
\r
173 if(m1.index < m2.index)
\r
175 else if(m1.index > m2.index)
\r
179 // if index is the same, sort by length
\r
180 if(m1.length < m2.length)
\r
182 else if(m1.length > m2.length)
\r
188 // gets a list of all matches for a given regular expression
\r
189 dp.sh.Highlighter.prototype.GetMatches = function(regex, css)
\r
194 while((match = regex.exec(this.code)) != null)
\r
196 this.matches[this.matches.length] = new dp.sh.Match(match[0], match.index, css);
\r
200 dp.sh.Highlighter.prototype.AddBit = function(str, css)
\r
202 var span = document.createElement('span');
\r
204 str = str.replace(/&/g, '&');
\r
205 str = str.replace(/ /g, ' ');
\r
206 str = str.replace(/</g, '<');
\r
207 str = str.replace(/\n/gm, ' <br>');
\r
209 // when adding a piece of code, check to see if it has line breaks in it
\r
210 // and if it does, wrap individual line breaks with span tags
\r
213 var regex = new RegExp('<br>', 'gi');
\r
215 if(regex.test(str))
\r
217 var lines = str.split(' <br>');
\r
221 for(var i = 0; i < lines.length; i++)
\r
223 span = document.createElement('SPAN');
\r
224 span.className = css;
\r
225 span.innerHTML = lines[i];
\r
227 this.div.appendChild(span);
\r
229 // don't add a <BR> for the last line
\r
230 if(i + 1 < lines.length)
\r
232 this.div.appendChild(document.createElement('BR'));
\r
238 span.className = css;
\r
239 span.innerHTML = str;
\r
240 this.div.appendChild(span);
\r
245 span.innerHTML = str;
\r
246 this.div.appendChild(span);
\r
250 // checks if one match is inside another
\r
251 dp.sh.Highlighter.prototype.IsInside = function(match)
\r
253 if(match == null || match.length == 0)
\r
258 for(var i = 0; i < this.matches.length; i++)
\r
260 var c = this.matches[i];
\r
267 if((match.index > c.index) && (match.index <= c.index + c.length))
\r
276 dp.sh.Highlighter.prototype.ProcessRegexList = function()
\r
278 for(var i = 0; i < this.regexList.length; i++)
\r
280 this.GetMatches(this.regexList[i].regex, this.regexList[i].css);
\r
284 dp.sh.Highlighter.prototype.ProcessSmartTabs = function(code)
\r
286 var lines = code.split('\n');
\r
291 // This function inserts specified amount of spaces in the string
\r
292 // where a tab is while removing that given tab.
\r
293 function InsertSpaces(line, pos, count)
\r
295 var left = line.substr(0, pos);
\r
296 var right = line.substr(pos + 1, line.length); // pos + 1 will get rid of the tab
\r
299 for(var i = 0; i < count; i++)
\r
304 return left + spaces + right;
\r
307 // This function process one line for 'smart tabs'
\r
308 function ProcessLine(line, tabSize)
\r
310 if(line.indexOf(tab) == -1)
\r
317 while((pos = line.indexOf(tab)) != -1)
\r
319 // This is pretty much all there is to the 'smart tabs' logic.
\r
320 // Based on the position within the line and size of a tab,
\r
321 // calculate the amount of spaces we need to insert.
\r
322 var spaces = tabSize - pos % tabSize;
\r
324 line = InsertSpaces(line, pos, spaces);
\r
330 // Go through all the lines and do the 'smart tabs' magic.
\r
331 for(var i = 0; i < lines.length; i++)
\r
333 var line = lines[i];
\r
334 result += ProcessLine(line, tabSize) + '\n';
\r
340 dp.sh.Highlighter.prototype.SwitchToTable = function()
\r
342 // Safari fix: for some reason lowercase <br> isn't getting picked up, even though 'i' is set
\r
343 var lines = this.div.innerHTML.split(/<BR>/gi);
\r
349 // creates an anchor to a utility
\r
350 function UtilHref(util, text)
\r
352 return '<a href="#" onclick="dp.sh.Utils.' + util + '(this); return false;">' + text + '</a>';
\r
355 row = this.table.insertRow(-1);
\r
357 if(this.addGutter == true)
\r
359 cell = row.insertCell(-1);
\r
360 cell.className = 'tools-corner';
\r
363 if(this.addControls == true)
\r
365 cell = row.insertCell(-1);
\r
367 cell.originalCode = this.originalCode;
\r
368 cell.processedCode = this.code;
\r
370 cell.className = 'tools';
\r
371 cell.innerHTML = UtilHref('ViewSource', 'view plain') + pipe + UtilHref('PrintSource', 'print');
\r
373 if(window.clipboardData)
\r
375 cell.innerHTML += pipe + UtilHref('ToClipboard', 'copy to clipboard');
\r
378 cell.innerHTML += pipe + UtilHref('About', '?');
\r
381 for(var i = 0; i < lines.length - 1; i++)
\r
383 row = this.table.insertRow(-1);
\r
385 if(this.addGutter == true)
\r
387 cell = row.insertCell(-1);
\r
388 cell.className = 'gutter';
\r
389 cell.innerHTML = i + 1;
\r
392 cell = row.insertCell(-1);
\r
393 cell.className = 'line';
\r
394 cell.innerHTML = lines[i];
\r
397 this.div.innerHTML = '';
\r
400 dp.sh.Highlighter.prototype.Highlight = function(code)
\r
402 // This function strips all new lines and spaces
\r
403 // from the beging and end of the string .
\r
406 var begining = new RegExp('^[\\s\\n]', 'g');
\r
407 var end = new RegExp('[\\s\\n]$', 'g');
\r
409 while(begining.test(str))
\r
411 str = str.substr(1);
\r
414 while(end.test(str))
\r
416 str = str.substr(0, str.length - 1);
\r
422 // This function returns a portions of the string
\r
423 // from pos1 to pos2 inclusive.
\r
424 function Copy(string, pos1, pos2)
\r
426 return string.substr(pos1, pos2 - pos1);
\r
431 this.originalCode = code;
\r
432 this.code = Trim(code);
\r
433 this.div = document.createElement('DIV');
\r
434 this.table = document.createElement('TABLE');
\r
435 this.matches = new Array();
\r
437 if(this.CssClass != null)
\r
439 this.table.className = this.CssClass;
\r
442 // replace tabs with spaces
\r
443 if(this.tabsToSpaces == true)
\r
445 this.code = this.ProcessSmartTabs(this.code);
\r
448 this.table.border = 0;
\r
449 this.table.cellSpacing = 0;
\r
450 this.table.cellPadding = 0;
\r
452 this.ProcessRegexList();
\r
454 // if no matches found, do nothing
\r
455 if(this.matches.length == 0)
\r
460 // sort the matches
\r
461 this.matches = this.matches.sort(dp.sh.Highlighter.SortCallback);
\r
463 // The following loop checks to see if any of the matches are inside
\r
464 // of other matches. This process would get rid of highligting strings
\r
465 // inside comments, keywords inside strings and so on.
\r
466 for(var i = 0; i < this.matches.length; i++)
\r
468 if(this.IsInside(this.matches[i]))
\r
470 this.matches[i] = null;
\r
474 // Finally, go through the final list of matches and pull the all
\r
475 // together adding everything in between that isn't a match.
\r
476 for(var i = 0; i < this.matches.length; i++)
\r
478 var match = this.matches[i];
\r
480 if(match == null || match.length == 0)
\r
485 this.AddBit(Copy(this.code, pos, match.index), null);
\r
486 this.AddBit(match.value, match.css);
\r
488 pos = match.index + match.length;
\r
491 this.AddBit(this.code.substr(pos), null);
\r
493 this.SwitchToTable();
\r
496 dp.sh.Highlighter.prototype.GetKeywords = function(str)
\r
498 return '\\b' + str.replace(/ /g, '\\b|\\b') + '\\b';
\r
501 // highlightes all elements identified by name and gets source code from specified property
\r
502 dp.sh.HighlightAll = function(name, showGutter /* optional */, showControls /* optional */)
\r
504 var elements = document.getElementsByName(name);
\r
505 var highlighter = null;
\r
506 var registered = new Object();
\r
507 var propertyName = 'value';
\r
509 function FindValue()
\r
513 for(var i = 0; i < a.length; i++)
\r
514 if(a[i] != null && ((typeof(a[i]) == 'string' && a[i] != '') || (typeof(a[i]) == 'object' && a[i].value != '')))
\r
520 if(elements == null)
\r
525 // if showGutter isn't set, default to TRUE
\r
526 if(showGutter == null)
\r
531 // if showControls isn't set, default to TRUE
\r
532 if(showControls == null)
\r
534 showControls = true;
\r
537 // register all brushes
\r
538 for(var brush in dp.sh.Brushes)
\r
540 var aliases = dp.sh.Brushes[brush].Aliases;
\r
542 if(aliases == null)
\r
547 for(var i = 0; i < aliases.length; i++)
\r
549 registered[aliases[i]] = brush;
\r
553 for(var i = 0; i < elements.length; i++)
\r
555 var element = elements[i];
\r
556 var language = FindValue(element.attributes['class'], element.className, element.attributes['language'], element.language);
\r
558 if(language == null)
\r
562 language = language.value;
\r
564 language = (language + '').toLowerCase();
\r
566 if(registered[language] == null)
\r
571 // instantiate a brush
\r
572 highlighter = new dp.sh.Brushes[registered[language]]();
\r
574 // hide the original element
\r
575 element.style.display = 'none';
\r
577 highlighter.addGutter = showGutter;
\r
578 highlighter.addControls = showControls;
\r
579 highlighter.Highlight(element[propertyName]);
\r
581 // place the result table inside a div
\r
582 var div = document.createElement('DIV');
\r
584 div.className = 'dp-highlighter';
\r
585 div.appendChild(highlighter.table);
\r
587 element.parentNode.insertBefore(div, element);
\r