3 * http://alexgorbatchev.com/SyntaxHighlighter
5 * SyntaxHighlighter is donationware. If you are using it, please donate.
6 * http://alexgorbatchev.com/SyntaxHighlighter/donate.html
9 * 3.0.83 (July 02 2010)
12 * Copyright (C) 2004-2010 Alex Gorbatchev.
15 * Dual licensed under the MIT and GPL licenses.
18 // Begin anonymous function. This is used to contain local scope variables without polutting global scope.
20 var SyntaxHighlighter = function() {
23 if (typeof(require) != 'undefined' && typeof(XRegExp) == 'undefined')
25 XRegExp = require('XRegExp').XRegExp;
28 // Shortcut object which will be assigned to the SyntaxHighlighter variable.
29 // This is a shorthand for local reference in order to avoid long namespace
30 // references to SyntaxHighlighter.whatever...
33 /** Additional CSS class names to be added to highlighter elements. */
36 /** First line number. */
40 * Pads line numbers. Possible values are:
42 * false - don't pad line numbers.
43 * true - automaticaly pad numbers with minimum required number of leading zeroes.
44 * [int] - length up to which pad line numbers.
46 'pad-line-numbers' : false,
48 /** Lines to highlight. */
51 /** Title to be displayed above the code block. */
54 /** Enables or disables smart tabs. */
57 /** Gets or sets tab size. */
60 /** Enables or disables gutter. */
63 /** Enables or disables toolbar. */
66 /** Enables quick code copy and paste from double click. */
69 /** Forces code view to be collapsed. */
72 /** Enables or disables automatic links. */
75 /** Gets or sets light mode. Equavalent to turning off gutter and toolbar. */
84 /** Enables use of <SCRIPT type="syntaxhighlighter" /> tags. */
87 /** Blogger mode flag. */
92 /** Name of the tag that SyntaxHighlighter will automatically look for. */
96 expandSource : 'expand source',
98 alert: 'SyntaxHighlighter\n\n',
99 noBrush : 'Can\'t find brush for: ',
100 brushNotHtmlScript : 'Brush wasn\'t configured for html-script option: ',
102 // this is populated by the build script
103 aboutDialog : '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>About SyntaxHighlighter</title></head><body style="font-family:Geneva,Arial,Helvetica,sans-serif;background-color:#fff;color:#000;font-size:1em;text-align:center;"><div style="text-align:center;margin-top:1.5em;"><div style="font-size:xx-large;">SyntaxHighlighter</div><div style="font-size:.75em;margin-bottom:3em;"><div>version 3.0.83 (July 02 2010)</div><div><a href="http://alexgorbatchev.com/SyntaxHighlighter" target="_blank" style="color:#005896">http://alexgorbatchev.com/SyntaxHighlighter</a></div><div>JavaScript code syntax highlighter.</div><div>Copyright 2004-2010 Alex Gorbatchev.</div></div><div>If you like this script, please <a href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=2930402" style="color:#005896">donate</a> to <br/>keep development active!</div></div></body></html>'
107 /** Internal 'global' variables. */
109 discoveredBrushes : null,
113 /** This object is populated by user included external brush files. */
116 /** Common regular expressions. */
118 multiLineCComments : /\/\*[\s\S]*?\*\//gm,
119 singleLineCComments : /\/\/.*$/gm,
120 singleLinePerlComments : /#.*$/gm,
121 doubleQuotedString : /"([^\\"\n]|\\.)*"/g,
122 singleQuotedString : /'([^\\'\n]|\\.)*'/g,
123 multiLineDoubleQuotedString : new XRegExp('"([^\\\\"]|\\\\.)*"', 'gs'),
124 multiLineSingleQuotedString : new XRegExp("'([^\\\\']|\\\\.)*'", 'gs'),
125 xmlComments : /(<|<)!--[\s\S]*?--(>|>)/gm,
126 url : /\w+:\/\/[\w-.\/?%&=:@;]*/g,
129 phpScriptTags : { left: /(<|<)\?=?/g, right: /\?(>|>)/g },
132 aspScriptTags : { left: /(<|<)%=?/g, right: /%(>|>)/g },
134 /** <script></script> tags. */
135 scriptScriptTags : { left: /(<|<)\s*script.*?(>|>)/gi, right: /(<|<)\/\s*script\s*(>|>)/gi }
140 * Generates HTML markup for the toolbar.
141 * @param {Highlighter} highlighter Highlighter instance.
142 * @return {String} Returns HTML markup.
144 getHtml: function(highlighter)
146 var html = '<div class="toolbar">',
147 items = sh.toolbar.items,
151 function defaultGetHtml(highlighter, name)
153 return sh.toolbar.getButtonHtml(highlighter, name, sh.config.strings[name]);
156 for (var i = 0; i < list.length; i++)
157 html += (items[list[i]].getHtml || defaultGetHtml)(highlighter, list[i]);
165 * Generates HTML markup for a regular button in the toolbar.
166 * @param {Highlighter} highlighter Highlighter instance.
167 * @param {String} commandName Command name that would be executed.
168 * @param {String} label Label text to display.
169 * @return {String} Returns HTML markup.
171 getButtonHtml: function(highlighter, commandName, label)
173 return '<span><a href="#" class="toolbar_item'
174 + ' command_' + commandName
176 + '">' + label + '</a></span>'
181 * Event handler for a toolbar anchor.
185 var target = e.target,
186 className = target.className || ''
189 function getValue(name)
191 var r = new RegExp(name + '_(\\w+)'),
192 match = r.exec(className)
195 return match ? match[1] : null;
198 var highlighter = getHighlighterById(findParentElement(target, '.syntaxhighlighter').id),
199 commandName = getValue('command')
202 // execute the toolbar command
203 if (highlighter && commandName)
204 sh.toolbar.items[commandName].execute(highlighter);
206 // disable default A click behaviour
210 /** Collection of toolbar items. */
212 // Ordered lis of items in the toolbar. Can't expect `for (var n in items)` to be consistent.
213 list: ['expandSource', 'help'],
216 getHtml: function(highlighter)
218 if (highlighter.getParam('collapse') != true)
221 var title = highlighter.getParam('title');
222 return sh.toolbar.getButtonHtml(highlighter, 'expandSource', title ? title : sh.config.strings.expandSource);
225 execute: function(highlighter)
227 var div = getHighlighterDivById(highlighter.id);
228 removeClass(div, 'collapsed');
232 /** Command to display the about dialog window. */
234 execute: function(highlighter)
236 var wnd = popup('', '_blank', 500, 250, 'scrollbars=0'),
240 doc.write(sh.config.strings.aboutDialog);
249 * Finds all elements on the page which should be processes by SyntaxHighlighter.
251 * @param {Object} globalParams Optional parameters which override element's
252 * parameters. Only used if element is specified.
254 * @param {Object} element Optional element to highlight. If none is
255 * provided, all elements in the current document
256 * are returned which qualify.
258 * @return {Array} Returns list of <code>{ target: DOMElement, params: Object }</code> objects.
260 findElements: function(globalParams, element)
262 var elements = element ? [element] : toArray(document.getElementsByTagName(sh.config.tagName)),
267 // support for <SCRIPT TYPE="syntaxhighlighter" /> feature
268 if (conf.useScriptTags)
269 elements = elements.concat(getSyntaxHighlighterScriptTags());
271 if (elements.length === 0)
274 for (var i = 0; i < elements.length; i++)
278 // local params take precedence over globals
279 params: merge(globalParams, parseParams(elements[i].className))
282 if (item.params['brush'] == null)
292 * Shorthand to highlight all elements on the page that are marked as
293 * SyntaxHighlighter source code.
295 * @param {Object} globalParams Optional parameters which override element's
296 * parameters. Only used if element is specified.
298 * @param {Object} element Optional element to highlight. If none is
299 * provided, all elements in the current document
302 highlight: function(globalParams, element)
304 var elements = this.findElements(globalParams, element),
305 propertyName = 'innerHTML',
310 if (elements.length === 0)
313 for (var i = 0; i < elements.length; i++)
315 var element = elements[i],
316 target = element.target,
317 params = element.params,
318 brushName = params.brush,
322 if (brushName == null)
325 // Instantiate a brush
326 if (params['html-script'] == 'true' || sh.defaults['html-script'] == true)
328 highlighter = new sh.HtmlScript(brushName);
329 brushName = 'htmlscript';
333 var brush = findBrush(brushName);
336 highlighter = new brush();
341 code = target[propertyName];
343 // remove CDATA from <SCRIPT/> tags if it's present
344 if (conf.useScriptTags)
345 code = stripCData(code);
347 // Inject title if the attribute is present
348 if ((target.title || '') != '')
349 params.title = target.title;
351 params['brush'] = brushName;
352 highlighter.init(params);
353 element = highlighter.getDiv(code);
356 if ((target.id || '') != '')
357 element.id = target.id;
359 target.parentNode.replaceChild(element, target);
364 * Main entry point for the SyntaxHighlighter.
365 * @param {Object} params Optional params to apply to all highlighted elements.
367 all: function(params)
372 function() { sh.highlight(params); }
378 sh['highlight'] = sh.highlight;
381 * Checks if target DOM elements has specified CSS class.
382 * @param {DOMElement} target Target DOM element to check.
383 * @param {String} className Name of the CSS class to check for.
384 * @return {Boolean} Returns true if class name is present, false otherwise.
386 function hasClass(target, className)
388 return target.className.indexOf(className) != -1;
392 * Adds CSS class name to the target DOM element.
393 * @param {DOMElement} target Target DOM element.
394 * @param {String} className New CSS class to add.
396 function addClass(target, className)
398 if (!hasClass(target, className))
399 target.className += ' ' + className;
403 * Removes CSS class name from the target DOM element.
404 * @param {DOMElement} target Target DOM element.
405 * @param {String} className CSS class to remove.
407 function removeClass(target, className)
409 target.className = target.className.replace(className, '');
413 * Converts the source to array object. Mostly used for function arguments and
414 * lists returned by getElementsByTagName() which aren't Array objects.
415 * @param {List} source Source list.
416 * @return {Array} Returns array.
418 function toArray(source)
422 for (var i = 0; i < source.length; i++)
423 result.push(source[i]);
429 * Splits block of text into lines.
430 * @param {String} block Block of text.
431 * @return {Array} Returns array of lines.
433 function splitLines(block)
435 return block.split('\n');
439 * Generates HTML ID for the highlighter.
440 * @param {String} highlighterId Highlighter ID.
441 * @return {String} Returns HTML ID.
443 function getHighlighterId(id)
445 var prefix = 'highlighter_';
446 return id.indexOf(prefix) == 0 ? id : prefix + id;
450 * Finds Highlighter instance by ID.
451 * @param {String} highlighterId Highlighter ID.
452 * @return {Highlighter} Returns instance of the highlighter.
454 function getHighlighterById(id)
456 return sh.vars.highlighters[getHighlighterId(id)];
460 * Finds highlighter's DIV container.
461 * @param {String} highlighterId Highlighter ID.
462 * @return {Element} Returns highlighter's DIV element.
464 function getHighlighterDivById(id)
466 return document.getElementById(getHighlighterId(id));
470 * Stores highlighter so that getHighlighterById() can do its thing. Each
471 * highlighter must call this method to preserve itself.
472 * @param {Highilghter} highlighter Highlighter instance.
474 function storeHighlighter(highlighter)
476 sh.vars.highlighters[getHighlighterId(highlighter.id)] = highlighter;
480 * Looks for a child or parent node which has specified classname.
481 * Equivalent to jQuery's $(container).find(".className")
482 * @param {Element} target Target element.
483 * @param {String} search Class name or node name to look for.
484 * @param {Boolean} reverse If set to true, will go up the node tree instead of down.
485 * @return {Element} Returns found child or parent element on null.
487 function findElement(target, search, reverse /* optional */)
492 var nodes = reverse != true ? target.childNodes : [ target.parentNode ],
493 propertyToFind = { '#' : 'id', '.' : 'className' }[search.substr(0, 1)] || 'nodeName',
498 expectedValue = propertyToFind != 'nodeName'
500 : search.toUpperCase()
503 // main return of the found node
504 if ((target[propertyToFind] || '').indexOf(expectedValue) != -1)
507 for (var i = 0; nodes && i < nodes.length && found == null; i++)
508 found = findElement(nodes[i], search, reverse);
514 * Looks for a parent node which has specified classname.
515 * This is an alias to <code>findElement(container, className, true)</code>.
516 * @param {Element} target Target element.
517 * @param {String} className Class name to look for.
518 * @return {Element} Returns found parent element on null.
520 function findParentElement(target, className)
522 return findElement(target, className, true);
526 * Finds an index of element in the array.
528 * @param {Object} searchElement
529 * @param {Number} fromIndex
530 * @return {Number} Returns index of element if found; -1 otherwise.
532 function indexOf(array, searchElement, fromIndex)
534 fromIndex = Math.max(fromIndex || 0, 0);
536 for (var i = fromIndex; i < array.length; i++)
537 if(array[i] == searchElement)
544 * Generates a unique element ID.
546 function guid(prefix)
548 return (prefix || '') + Math.round(Math.random() * 1000000).toString();
552 * Merges two objects. Values from obj2 override values in obj1.
553 * Function is NOT recursive and works only for one dimensional objects.
554 * @param {Object} obj1 First object.
555 * @param {Object} obj2 Second object.
556 * @return {Object} Returns combination of both objects.
558 function merge(obj1, obj2)
560 var result = {}, name;
563 result[name] = obj1[name];
566 result[name] = obj2[name];
572 * Attempts to convert string to boolean.
573 * @param {String} value Input string.
574 * @return {Boolean} Returns true if input was "true", false if input was "false" and value otherwise.
576 function toBoolean(value)
578 var result = { "true" : true, "false" : false }[value];
579 return result == null ? value : result;
583 * Opens up a centered popup window.
584 * @param {String} url URL to open in the window.
585 * @param {String} name Popup name.
586 * @param {int} width Popup width.
587 * @param {int} height Popup height.
588 * @param {String} options window.open() options.
589 * @return {Window} Returns window instance.
591 function popup(url, name, width, height, options)
593 var x = (screen.width - width) / 2,
594 y = (screen.height - height) / 2
597 options += ', left=' + x +
602 options = options.replace(/^,/, '');
604 var win = window.open(url, name, options);
610 * Adds event handler to the target object.
611 * @param {Object} obj Target object.
612 * @param {String} type Name of the event.
613 * @param {Function} func Handling function.
615 function attachEvent(obj, type, func, scope)
619 e = e || window.event;
623 e.target = e.srcElement;
624 e.preventDefault = function()
626 this.returnValue = false;
630 func.call(scope || window, e);
635 obj.attachEvent('on' + type, handler);
639 obj.addEventListener(type, handler, false);
645 * @param {String} str String to display.
649 window.alert(sh.config.strings.alert + str);
653 * Finds a brush by its alias.
655 * @param {String} alias Brush alias.
656 * @param {Boolean} showAlert Suppresses the alert if false.
657 * @return {Brush} Returns bursh constructor if found, null otherwise.
659 function findBrush(alias, showAlert)
661 var brushes = sh.vars.discoveredBrushes,
670 for (var brush in sh.brushes)
672 var info = sh.brushes[brush],
673 aliases = info.aliases
679 // keep the brush name
680 info.brushName = brush.toLowerCase();
682 for (var i = 0; i < aliases.length; i++)
683 brushes[aliases[i]] = brush;
686 sh.vars.discoveredBrushes = brushes;
689 result = sh.brushes[brushes[alias]];
691 if (result == null && showAlert != false)
692 alert(sh.config.strings.noBrush + alias);
698 * Executes a callback on each line and replaces each line with result from the callback.
699 * @param {Object} str Input string.
700 * @param {Object} callback Callback function taking one string argument and returning a string.
702 function eachLine(str, callback)
704 var lines = splitLines(str);
706 for (var i = 0; i < lines.length; i++)
707 lines[i] = callback(lines[i], i);
709 return lines.join('\n');
713 * This is a special trim which only removes first and last empty lines
714 * and doesn't affect valid leading space on the first line.
716 * @param {String} str Input string
717 * @return {String} Returns string without empty first and last lines.
719 function trimFirstAndLastLines(str)
721 return str.replace(/^[ ]*[\n]+|[\n]*[ ]*$/g, '');
725 * Parses key/value pairs into hash object.
727 * Understands the following formats:
729 * - name: [word, word];
734 * name1: value; name2: [value, value]; name3: 'value'
736 * @param {String} str Input string.
737 * @return {Object} Returns deserialized object.
739 function parseParams(str)
743 arrayRegex = new XRegExp("^\\[(?<values>(.*?))\\]$"),
748 "[\\w-%#]+|" + // word
749 "\\[.*?\\]|" + // [] array
750 '".*?"|' + // "" string
751 "'.*?'" + // '' string
757 while ((match = regex.exec(str)) != null)
759 var value = match.value
760 .replace(/^['"]|['"]$/g, '') // strip quotes from end of strings
763 // try to parse array value
764 if (value != null && arrayRegex.test(value))
766 var m = arrayRegex.exec(value);
767 value = m.values.length > 0 ? m.values.split(/\s*,\s*/) : [];
770 result[match.name] = value;
777 * Wraps each line of the string into <code/> tag with given style applied to it.
779 * @param {String} str Input string.
780 * @param {String} css Style name to apply to the string.
781 * @return {String} Returns input string with each line surrounded by <span/> tag.
783 function wrapLinesWithCode(str, css)
785 if (str == null || str.length == 0 || str == '\n')
788 str = str.replace(/</g, '<');
790 // Replace two or more sequential spaces with leaving last space untouched.
791 str = str.replace(/ {2,}/g, function(m)
795 for (var i = 0; i < m.length - 1; i++)
796 spaces += sh.config.space;
801 // Split each line and apply <span class="...">...</span> to them so that
802 // leading spaces aren't included.
804 str = eachLine(str, function(line)
806 if (line.length == 0)
811 line = line.replace(/^( | )+/, function(s)
817 if (line.length == 0)
820 return spaces + '<code class="' + css + '">' + line + '</code>';
827 * Pads number with zeros until it's length is the same as given length.
829 * @param {Number} number Number to pad.
830 * @param {Number} length Max string length with.
831 * @return {String} Returns a string padded with proper amount of '0'.
833 function padNumber(number, length)
835 var result = number.toString();
837 while (result.length < length)
838 result = '0' + result;
844 * Replaces tabs with spaces.
846 * @param {String} code Source code.
847 * @param {Number} tabSize Size of the tab.
848 * @return {String} Returns code with all tabs replaces by spaces.
850 function processTabs(code, tabSize)
854 for (var i = 0; i < tabSize; i++)
857 return code.replace(/\t/g, tab);
861 * Replaces tabs with smart spaces.
863 * @param {String} code Code to fix the tabs in.
864 * @param {Number} tabSize Number of spaces in a column.
865 * @return {String} Returns code with all tabs replaces with roper amount of spaces.
867 function processSmartTabs(code, tabSize)
869 var lines = splitLines(code),
874 // Create a string with 1000 spaces to copy spaces from...
875 // It's assumed that there would be no indentation longer than that.
876 for (var i = 0; i < 50; i++)
877 spaces += ' '; // 20 spaces * 50
879 // This function inserts specified amount of spaces in the string
880 // where a tab is while removing that given tab.
881 function insertSpaces(line, pos, count)
883 return line.substr(0, pos)
884 + spaces.substr(0, count)
885 + line.substr(pos + 1, line.length) // pos + 1 will get rid of the tab
889 // Go through all the lines and do the 'smart tabs' magic.
890 code = eachLine(code, function(line)
892 if (line.indexOf(tab) == -1)
897 while ((pos = line.indexOf(tab)) != -1)
899 // This is pretty much all there is to the 'smart tabs' logic.
900 // Based on the position within the line and size of a tab,
901 // calculate the amount of spaces we need to insert.
902 var spaces = tabSize - pos % tabSize;
903 line = insertSpaces(line, pos, spaces);
913 * Performs various string fixes based on configuration.
915 function fixInputString(str)
917 var br = /<br\s*\/?>|<br\s*\/?>/gi;
919 if (sh.config.bloggerMode == true)
920 str = str.replace(br, '\n');
922 if (sh.config.stripBrs == true)
923 str = str.replace(br, '');
929 * Removes all white space at the begining and end of a string.
931 * @param {String} str String to trim.
932 * @return {String} Returns string without leading and following white space characters.
936 return str.replace(/^\s+|\s+$/g, '');
940 * Unindents a block of text by the lowest common indent amount.
941 * @param {String} str Text to unindent.
942 * @return {String} Returns unindented text block.
944 function unindent(str)
946 var lines = splitLines(fixInputString(str)),
947 indents = new Array(),
952 // go through every line and check for common number of indents
953 for (var i = 0; i < lines.length && min > 0; i++)
957 if (trim(line).length == 0)
960 var matches = regex.exec(line);
962 // In the event that just one line doesn't have leading white space
963 // we can't unindent anything, so bail completely.
967 min = Math.min(matches[0].length, min);
970 // trim minimum common number of white space from the begining of every line
972 for (var i = 0; i < lines.length; i++)
973 lines[i] = lines[i].substr(min);
975 return lines.join('\n');
979 * Callback method for Array.sort() which sorts matches by
980 * index position and then by length.
982 * @param {Match} m1 Left object.
983 * @param {Match} m2 Right object.
984 * @return {Number} Returns -1, 0 or -1 as a comparison result.
986 function matchesSortCallback(m1, m2)
988 // sort matches by index first
989 if(m1.index < m2.index)
991 else if(m1.index > m2.index)
995 // if index is the same, sort by length
996 if(m1.length < m2.length)
998 else if(m1.length > m2.length)
1006 * Executes given regular expression on provided code and returns all
1007 * matches that are found.
1009 * @param {String} code Code to execute regular expression on.
1010 * @param {Object} regex Regular expression item info from <code>regexList</code> collection.
1011 * @return {Array} Returns a list of Match objects.
1013 function getMatches(code, regexInfo)
1015 function defaultAdd(match, regexInfo)
1023 func = regexInfo.func ? regexInfo.func : defaultAdd
1026 while((match = regexInfo.regex.exec(code)) != null)
1028 var resultMatch = func(match, regexInfo);
1030 if (typeof(resultMatch) == 'string')
1031 resultMatch = [new sh.Match(resultMatch, match.index, regexInfo.css)];
1033 matches = matches.concat(resultMatch);
1040 * Turns all URLs in the code into <a/> tags.
1041 * @param {String} code Input code.
1042 * @return {String} Returns code with </a> tags.
1044 function processUrls(code)
1046 var gt = /(.*)((>|<).*)/;
1048 return code.replace(sh.regexLib.url, function(m)
1054 // We include < and > in the URL for the common cases like <http://google.com>
1055 // The problem is that they get transformed into <http://google.com>
1056 // Where as > easily looks like part of the URL string.
1058 if (match = gt.exec(m))
1064 return '<a href="' + m + '">' + m + '</a>' + suffix;
1069 * Finds all <SCRIPT TYPE="syntaxhighlighter" /> elementss.
1070 * @return {Array} Returns array of all found SyntaxHighlighter tags.
1072 function getSyntaxHighlighterScriptTags()
1074 var tags = document.getElementsByTagName('script'),
1078 for (var i = 0; i < tags.length; i++)
1079 if (tags[i].type == 'syntaxhighlighter')
1080 result.push(tags[i]);
1086 * Strips <![CDATA[]]> from <SCRIPT /> content because it should be used
1087 * there in most cases for XHTML compliance.
1088 * @param {String} original Input code.
1089 * @return {String} Returns code without leading <![CDATA[]]> tags.
1091 function stripCData(original)
1093 var left = '<![CDATA[',
1095 // for some reason IE inserts some leading blanks here
1096 copy = trim(original),
1098 leftLength = left.length,
1099 rightLength = right.length
1102 if (copy.indexOf(left) == 0)
1104 copy = copy.substring(leftLength);
1108 var copyLength = copy.length;
1110 if (copy.indexOf(right) == copyLength - rightLength)
1112 copy = copy.substring(0, copyLength - rightLength);
1116 return changed ? copy : original;
1121 * Quick code mouse double click handler.
1123 function quickCodeHandler(e)
1125 var target = e.target,
1126 highlighterDiv = findParentElement(target, '.syntaxhighlighter'),
1127 container = findParentElement(target, '.container'),
1128 textarea = document.createElement('textarea'),
1132 if (!container || !highlighterDiv || findElement(container, 'textarea'))
1135 highlighter = getHighlighterById(highlighterDiv.id);
1137 // add source class name
1138 addClass(highlighterDiv, 'source');
1140 // Have to go over each line and grab it's text, can't just do it on the
1141 // container because Firefox loses all \n where as Webkit doesn't.
1142 var lines = container.childNodes,
1146 for (var i = 0; i < lines.length; i++)
1147 code.push(lines[i].innerText || lines[i].textContent);
1149 // using \r instead of \r or \r\n makes this work equally well on IE, FF and Webkit
1150 code = code.join('\r');
1152 // inject <textarea/> tag
1153 textarea.appendChild(document.createTextNode(code));
1154 container.appendChild(textarea);
1156 // preselect all text
1160 // set up handler for lost focus
1161 attachEvent(textarea, 'blur', function(e)
1163 textarea.parentNode.removeChild(textarea);
1164 removeClass(highlighterDiv, 'source');
1171 sh.Match = function(value, index, css)
1175 this.length = value.length;
1177 this.brushName = null;
1180 sh.Match.prototype.toString = function()
1186 * Simulates HTML code with a scripting language embedded.
1188 * @param {String} scriptBrushName Brush name of the scripting language.
1190 sh.HtmlScript = function(scriptBrushName)
1192 var brushClass = findBrush(scriptBrushName),
1194 xmlBrush = new sh.brushes.Xml(),
1195 bracketsRegex = null,
1197 methodsToExpose = 'getDiv getHtml init'.split(' ')
1200 if (brushClass == null)
1203 scriptBrush = new brushClass();
1205 for(var i = 0; i < methodsToExpose.length; i++)
1206 // make a closure so we don't lose the name after i changes
1208 var name = methodsToExpose[i];
1210 ref[name] = function()
1212 return xmlBrush[name].apply(xmlBrush, arguments);
1216 if (scriptBrush.htmlScript == null)
1218 alert(sh.config.strings.brushNotHtmlScript + scriptBrushName);
1222 xmlBrush.regexList.push(
1223 { regex: scriptBrush.htmlScript.code, func: process }
1226 function offsetMatches(matches, offset)
1228 for (var j = 0; j < matches.length; j++)
1229 matches[j].index += offset;
1232 function process(match, info)
1234 var code = match.code,
1236 regexList = scriptBrush.regexList,
1237 offset = match.index + match.left.length,
1238 htmlScript = scriptBrush.htmlScript,
1242 // add all matches from the code
1243 for (var i = 0; i < regexList.length; i++)
1245 result = getMatches(code, regexList[i]);
1246 offsetMatches(result, offset);
1247 matches = matches.concat(result);
1250 // add left script bracket
1251 if (htmlScript.left != null && match.left != null)
1253 result = getMatches(match.left, htmlScript.left);
1254 offsetMatches(result, match.index);
1255 matches = matches.concat(result);
1258 // add right script bracket
1259 if (htmlScript.right != null && match.right != null)
1261 result = getMatches(match.right, htmlScript.right);
1262 offsetMatches(result, match.index + match[0].lastIndexOf(match.right));
1263 matches = matches.concat(result);
1266 for (var j = 0; j < matches.length; j++)
1267 matches[j].brushName = brushClass.brushName;
1274 * Main Highlither class.
1277 sh.Highlighter = function()
1279 // not putting any code in here because of the prototype inheritance
1282 sh.Highlighter.prototype = {
1284 * Returns value of the parameter passed to the highlighter.
1285 * @param {String} name Name of the parameter.
1286 * @param {Object} defaultValue Default value.
1287 * @return {Object} Returns found value or default value otherwise.
1289 getParam: function(name, defaultValue)
1291 var result = this.params[name];
1292 return toBoolean(result == null ? defaultValue : result);
1296 * Shortcut to document.createElement().
1297 * @param {String} name Name of the element to create (DIV, A, etc).
1298 * @return {HTMLElement} Returns new HTML element.
1300 create: function(name)
1302 return document.createElement(name);
1306 * Applies all regular expression to the code and stores all found
1307 * matches in the `this.matches` array.
1308 * @param {Array} regexList List of regular expressions.
1309 * @param {String} code Source code.
1310 * @return {Array} Returns list of matches.
1312 findMatches: function(regexList, code)
1316 if (regexList != null)
1317 for (var i = 0; i < regexList.length; i++)
1318 // BUG: length returns len+1 for array if methods added to prototype chain (oising@gmail.com)
1319 if (typeof (regexList[i]) == "object")
1320 result = result.concat(getMatches(code, regexList[i]));
1322 // sort and remove nested the matches
1323 return this.removeNestedMatches(result.sort(matchesSortCallback));
1327 * Checks to see if any of the matches are inside of other matches.
1328 * This process would get rid of highligted strings inside comments,
1329 * keywords inside strings and so on.
1331 removeNestedMatches: function(matches)
1333 // Optimized by Jose Prado (http://joseprado.com)
1334 for (var i = 0; i < matches.length; i++)
1336 if (matches[i] === null)
1339 var itemI = matches[i],
1340 itemIEndPos = itemI.index + itemI.length
1343 for (var j = i + 1; j < matches.length && matches[i] !== null; j++)
1345 var itemJ = matches[j];
1349 else if (itemJ.index > itemIEndPos)
1351 else if (itemJ.index == itemI.index && itemJ.length > itemI.length)
1353 else if (itemJ.index >= itemI.index && itemJ.index < itemIEndPos)
1362 * Creates an array containing integer line numbers starting from the 'first-line' param.
1363 * @return {Array} Returns array of integers.
1365 figureOutLineNumbers: function(code)
1368 firstLine = parseInt(this.getParam('first-line'))
1371 eachLine(code, function(line, index)
1373 lines.push(index + firstLine);
1380 * Determines if specified line number is in the highlighted list.
1382 isLineHighlighted: function(lineNumber)
1384 var list = this.getParam('highlight', []);
1386 if (typeof(list) != 'object' && list.push == null)
1389 return indexOf(list, lineNumber.toString()) != -1;
1393 * Generates HTML markup for a single line of code while determining alternating line style.
1394 * @param {Integer} lineNumber Line number.
1395 * @param {String} code Line HTML markup.
1396 * @return {String} Returns HTML markup.
1398 getLineHtml: function(lineIndex, lineNumber, code)
1402 'number' + lineNumber,
1403 'index' + lineIndex,
1404 'alt' + (lineNumber % 2 == 0 ? 1 : 2).toString()
1407 if (this.isLineHighlighted(lineNumber))
1408 classes.push('highlighted');
1410 if (lineNumber == 0)
1411 classes.push('break');
1413 return '<div class="' + classes.join(' ') + '">' + code + '</div>';
1417 * Generates HTML markup for line number column.
1418 * @param {String} code Complete code HTML markup.
1419 * @param {Array} lineNumbers Calculated line numbers.
1420 * @return {String} Returns HTML markup.
1422 getLineNumbersHtml: function(code, lineNumbers)
1425 count = splitLines(code).length,
1426 firstLine = parseInt(this.getParam('first-line')),
1427 pad = this.getParam('pad-line-numbers')
1431 pad = (firstLine + count - 1).toString().length;
1432 else if (isNaN(pad) == true)
1435 for (var i = 0; i < count; i++)
1437 var lineNumber = lineNumbers ? lineNumbers[i] : firstLine + i,
1438 code = lineNumber == 0 ? sh.config.space : padNumber(lineNumber, pad)
1441 html += this.getLineHtml(i, lineNumber, code);
1448 * Splits block of text into individual DIV lines.
1449 * @param {String} code Code to highlight.
1450 * @param {Array} lineNumbers Calculated line numbers.
1451 * @return {String} Returns highlighted code in HTML form.
1453 getCodeLinesHtml: function(html, lineNumbers)
1457 var lines = splitLines(html),
1458 padLength = this.getParam('pad-line-numbers'),
1459 firstLine = parseInt(this.getParam('first-line')),
1461 brushName = this.getParam('brush')
1464 for (var i = 0; i < lines.length; i++)
1466 var line = lines[i],
1467 indent = /^( |\s)+/.exec(line),
1469 lineNumber = lineNumbers ? lineNumbers[i] : firstLine + i;
1474 spaces = indent[0].toString();
1475 line = line.substr(spaces.length);
1476 spaces = spaces.replace(' ', sh.config.space);
1481 if (line.length == 0)
1482 line = sh.config.space;
1484 html += this.getLineHtml(
1487 (spaces != null ? '<code class="' + brushName + ' spaces">' + spaces + '</code>' : '') + line
1495 * Returns HTML for the table title or empty string if title is null.
1497 getTitleHtml: function(title)
1499 return title ? '<caption>' + title + '</caption>' : '';
1503 * Finds all matches in the source code.
1504 * @param {String} code Source code to process matches in.
1505 * @param {Array} matches Discovered regex matches.
1506 * @return {String} Returns formatted HTML with processed mathes.
1508 getMatchesHtml: function(code, matches)
1512 brushName = this.getParam('brush', '')
1515 function getBrushNameCss(match)
1517 var result = match ? (match.brushName || brushName) : brushName;
1518 return result ? result + ' ' : '';
1521 // Finally, go through the final list of matches and pull the all
1522 // together adding everything in between that isn't a match.
1523 for (var i = 0; i < matches.length; i++)
1525 var match = matches[i],
1529 if (match === null || match.length === 0)
1532 matchBrushName = getBrushNameCss(match);
1534 result += wrapLinesWithCode(code.substr(pos, match.index - pos), matchBrushName + 'plain')
1535 + wrapLinesWithCode(match.value, matchBrushName + match.css)
1538 pos = match.index + match.length + (match.offset || 0);
1541 // don't forget to add whatever's remaining in the string
1542 result += wrapLinesWithCode(code.substr(pos), getBrushNameCss() + 'plain');
1548 * Generates HTML markup for the whole syntax highlighter.
1549 * @param {String} code Source code.
1550 * @return {String} Returns HTML markup.
1552 getHtml: function(code)
1555 classes = [ 'syntaxhighlighter' ],
1561 // process light mode
1562 if (this.getParam('light') == true)
1563 this.params.toolbar = this.params.gutter = false;
1565 className = 'syntaxhighlighter';
1567 if (this.getParam('collapse') == true)
1568 classes.push('collapsed');
1570 if ((gutter = this.getParam('gutter')) == false)
1571 classes.push('nogutter');
1573 // add custom user style name
1574 classes.push(this.getParam('class-name'));
1576 // add brush alias to the class name for custom CSS
1577 classes.push(this.getParam('brush'));
1579 code = trimFirstAndLastLines(code)
1580 .replace(/\r/g, ' ') // IE lets these buggers through
1583 tabSize = this.getParam('tab-size');
1585 // replace tabs with spaces
1586 code = this.getParam('smart-tabs') == true
1587 ? processSmartTabs(code, tabSize)
1588 : processTabs(code, tabSize)
1591 // unindent code by the common indentation
1592 code = unindent(code);
1595 lineNumbers = this.figureOutLineNumbers(code);
1597 // find matches in the code using brushes regex list
1598 matches = this.findMatches(this.regexList, code);
1599 // processes found matches into the html
1600 html = this.getMatchesHtml(code, matches);
1601 // finally, split all lines so that they wrap well
1602 html = this.getCodeLinesHtml(html, lineNumbers);
1604 // finally, process the links
1605 if (this.getParam('auto-links'))
1606 html = processUrls(html);
1608 if (typeof(navigator) != 'undefined' && navigator.userAgent && navigator.userAgent.match(/MSIE/))
1612 '<div id="' + getHighlighterId(this.id) + '" class="' + classes.join(' ') + '">'
1613 + (this.getParam('toolbar') ? sh.toolbar.getHtml(this) : '')
1614 + '<table border="0" cellpadding="0" cellspacing="0">'
1615 + this.getTitleHtml(this.getParam('title'))
1618 + (gutter ? '<td class="gutter">' + this.getLineNumbersHtml(code) + '</td>' : '')
1619 + '<td class="code">'
1620 + '<div class="container">'
1634 * Highlights the code and returns complete HTML.
1635 * @param {String} code Code to highlight.
1636 * @return {Element} Returns container DIV element with all markup.
1638 getDiv: function(code)
1645 var div = this.create('div');
1648 div.innerHTML = this.getHtml(code);
1650 // set up click handlers
1651 if (this.getParam('toolbar'))
1652 attachEvent(findElement(div, '.toolbar'), 'click', sh.toolbar.handler);
1654 if (this.getParam('quick-code'))
1655 attachEvent(findElement(div, '.code'), 'dblclick', quickCodeHandler);
1661 * Initializes the highlighter/brush.
1663 * Constructor isn't used for initialization so that nothing executes during necessary
1664 * `new SyntaxHighlighter.Highlighter()` call when setting up brush inheritence.
1666 * @param {Hash} params Highlighter parameters.
1668 init: function(params)
1672 // register this instance in the highlighters list
1673 storeHighlighter(this);
1675 // local params take precedence over defaults
1676 this.params = merge(sh.defaults, params || {})
1678 // process light mode
1679 if (this.getParam('light') == true)
1680 this.params.toolbar = this.params.gutter = false;
1684 * Converts space separated list of keywords into a regular expression string.
1685 * @param {String} str Space separated keywords.
1686 * @return {String} Returns regular expression string.
1688 getKeywords: function(str)
1691 .replace(/^\s+|\s+$/g, '')
1692 .replace(/\s+/g, '|')
1695 return '\\b(?:' + str + ')\\b';
1699 * Makes a brush compatible with the `html-script` functionality.
1700 * @param {Object} regexGroup Object containing `left` and `right` regular expressions.
1702 forHtmlScript: function(regexGroup)
1705 left : { regex: regexGroup.left, css: 'script' },
1706 right : { regex: regexGroup.right, css: 'script' },
1708 "(?<left>" + regexGroup.left.source + ")" +
1710 "(?<right>" + regexGroup.right.source + ")",
1715 }; // end of Highlighter
1718 }(); // end of anonymous function
1721 typeof(exports) != 'undefined' ? exports['SyntaxHighlighter'] = SyntaxHighlighter : null;