PageRenderTime 26ms CodeModel.GetById 13ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/syntaxhighlighter_3.0.83/src/shLegacy.js

#
JavaScript | 157 lines | 116 code | 23 blank | 18 comment | 22 complexity | dcf9a5a97e44fc0e0b547d7ef7f2c9a1 MD5 | raw file
  1/**
  2 * SyntaxHighlighter
  3 * http://alexgorbatchev.com/SyntaxHighlighter
  4 *
  5 * SyntaxHighlighter is donationware. If you are using it, please donate.
  6 * http://alexgorbatchev.com/SyntaxHighlighter/donate.html
  7 *
  8 * @version
  9 * 3.0.83 (July 02 2010)
 10 * 
 11 * @copyright
 12 * Copyright (C) 2004-2010 Alex Gorbatchev.
 13 *
 14 * @license
 15 * Dual licensed under the MIT and GPL licenses.
 16 */
 17var dp = {
 18	SyntaxHighlighter : {}
 19};
 20
 21dp.SyntaxHighlighter = {
 22	parseParams: function(
 23						input,
 24						showGutter, 
 25						showControls, 
 26						collapseAll, 
 27						firstLine, 
 28						showColumns
 29						)
 30	{
 31		function getValue(list, name)
 32		{
 33			var regex = new XRegExp('^' + name + '\\[(?<value>\\w+)\\]$', 'gi'),
 34				match = null
 35				;
 36			
 37			for (var i = 0; i < list.length; i++) 
 38				if ((match = regex.exec(list[i])) != null)
 39					return match.value;
 40			
 41			return null;
 42		};
 43		
 44		function defaultValue(value, def)
 45		{
 46			return value != null ? value : def;
 47		};
 48		
 49		function asString(value)
 50		{
 51			return value != null ? value.toString() : null;
 52		};
 53
 54		var parts = input.split(':'),
 55			brushName = parts[0],
 56			options = {},
 57			straight = { 'true' : true }
 58			reverse = { 'true' : false },
 59			result = null,
 60			defaults = SyntaxHighlighter.defaults
 61			;
 62		
 63		for (var i in parts)
 64			options[parts[i]] = 'true';
 65
 66		showGutter = asString(defaultValue(showGutter, defaults.gutter));
 67		showControls = asString(defaultValue(showControls, defaults.toolbar));
 68		collapseAll = asString(defaultValue(collapseAll, defaults.collapse)); 
 69		showColumns = asString(defaultValue(showColumns, defaults.ruler));
 70		firstLine = asString(defaultValue(firstLine, defaults['first-line'])); 
 71
 72		return {
 73			brush			: brushName,
 74			gutter			: defaultValue(reverse[options.nogutter], showGutter),
 75			toolbar			: defaultValue(reverse[options.nocontrols], showControls),
 76			collapse		: defaultValue(straight[options.collapse], collapseAll),
 77			// ruler			: defaultValue(straight[options.showcolumns], showColumns),
 78			'first-line'	: defaultValue(getValue(parts, 'firstline'), firstLine)
 79		};
 80	},
 81	
 82	HighlightAll: function(
 83						name, 
 84						showGutter /* optional */, 
 85						showControls /* optional */, 
 86						collapseAll /* optional */, 
 87						firstLine /* optional */, 
 88						showColumns /* optional */
 89						)
 90	{
 91		function findValue()
 92		{
 93			var a = arguments;
 94			
 95			for (var i = 0; i < a.length; i++) 
 96			{
 97				if (a[i] === null) 
 98					continue;
 99				
100				if (typeof(a[i]) == 'string' && a[i] != '') 
101					return a[i] + '';
102				
103				if (typeof(a[i]) == 'object' && a[i].value != '') 
104					return a[i].value + '';
105			}
106			
107			return null;
108		};
109
110		function findTagsByName(list, name, tagName)
111		{
112			var tags = document.getElementsByTagName(tagName);
113			
114			for (var i = 0; i < tags.length; i++) 
115				if (tags[i].getAttribute('name') == name) 
116					list.push(tags[i]);
117		}
118		
119		var elements = [],
120			highlighter = null,
121			registered = {},
122			propertyName = 'innerHTML'
123			;
124		
125		// for some reason IE doesn't find <pre/> by name, however it does see them just fine by tag name... 
126		findTagsByName(elements, name, 'pre');
127		findTagsByName(elements, name, 'textarea');
128
129		if (elements.length === 0)
130			return;
131		
132		for (var i = 0; i < elements.length; i++)
133		{
134			var element = elements[i],
135				params = findValue(
136					element.attributes['class'], element.className, 
137					element.attributes['language'], element.language
138					),
139				language = ''
140				;
141			
142			if (params === null) 
143				continue;
144
145			params = dp.SyntaxHighlighter.parseParams(
146				params,
147				showGutter, 
148				showControls, 
149				collapseAll, 
150				firstLine, 
151				showColumns
152				);
153
154			SyntaxHighlighter.highlight(params, element);
155		}
156	}
157};