/**
 * A quick script to add colour coding and line numbers to Apache configuration syntax - eg as in httpd.conf, .htaccess, etc.
 *
 * @author Nick Nettleton <nick@loftdigital.com>
 * @version 0.1 alpha - bound to be bugs
 * @license GPL
 * @copyright Nick Nettleton 22 April 2009
 *
 * Usage:
 *
 * <script type="text/javascript" src="/path/to/apache_prettify.js"></script>
 * <link rel="stylesheet" type="text/css" href="/path/to/apache_prettify.css" />
 * <script type="text/javascript">
 *
 * window.onload = function()
 * {
 * 	// brutal - prettify all elements with a particular tagname
 * 	apache_prettify_all('pre') ;
 *
 * 	// fine-grained - pick yr element
 * 	apache_prettify(document.getElementByID('id'))
 * }
 *
 * </script>
 *
 * History:
 *
 * 22 April 2007 - First release
 *
 * Todos:
 *
 * - Test on Macs and FF3
 * - Make compatible with prettify.js
 * - Do we have to use tables? (IE not playing nice, & that's what Google does too for other languages)
 * - Fix copy+paste for IE
 * - Fix indentations for IE
 * - Yes, it's all IE IE IE
 */

function apache_prettify_all(tagname)
{
	var els = document.getElementsByTagName(tagname) ;
	for(var i=0,l=els.length;i<l;i++){
		apache_prettify(els[i]) ;
	}
	return true ;
}

function apache_prettify(el)
{
	
	var map	= [
		// comment
		{
			f : /(#.*)/ ,
			r : '<span class=\'com\'>$1</span>'
		},
		// tag
		{
			f : /(&lt;(\/?)(.*?)(&gt;|>))/g , // > included for safari compat
			r : '<span class=\'tag\'>$1</span>'
		},
		// string
		{
		//	f : /("(.*?)")/g ,
			f : /((")(?:\\\2|.)*?\2)/g, // ide parse fix "
			r : '<span class=\'str\'>$1</span>'
		},
		// file path - treat as string?
		{
			f : /([\s=])([0-9a-z\.\-\_\/]*\/[0-9a-z\.\-\_\/]+)/gi ,
			r : '$1<span class=\'str\'>$2</span>'
		},
		// %{var} - ??
//		{
//			f : /(%{\S+})/gi ,
//			r : '<span class=\'lit\'>$1</span>'
//		},
		// rewrite rule
		{
			f : /(RewriteRule\s+)(\S+)/gi ,
			r : '$1<span class=\'regex\'>$2</span>'
		},
		// keyword
		{
			f : /(^\s*)(\w+)/ ,
			r : '$1<span class=\'kwd\'>$2</span>'
		}
	] ;
	
	var trimmer = /^\s+|\s+$/g ;
	var splitter = /\n<br[^>a-z]*>|<br[^>a-z]*>\n|<br[^>a-z]*>|\n/i ;
	var lines = el.innerHTML.replace(trimmer, '').split(splitter) ;
	var html = '' ;
	var html2 = '' ;
	
	for(var i=0,l=lines.length;i<l;i++){
		var line = lines[i] ;
		
		// if the line starts with a comment, just do that and move on
		if(line.match(/^\s*#/)){
			line = line.replace(map[0].f, map[0].r) ;
			
		// otherwise do all the regexs
		} else {	
			for(var j=0,m=map.length;j<m;j++){
				line = line.replace(map[j].f, map[j].r) ;
			}
		}
		
		// ie doesn't work with divs
		html += '<tr><td class="number-line">' + (line) + '<br /></td></tr>' ;
		html2 += '<tr><td class="code-line">' + (i+1) + '</td></tr>' ;
	}
	html = ''
		+ '<div class="code-box">'
		+ '<table cellspacing="0" cellpadding="0"><tr valign="top">'
		+ '<td class="number-col"><table cellspacing="0" cellpadding="0">' + html2 + '</table></td>'
		+ '<td  class="code-col"><table cellspacing="0" cellpadding="0">' + html + '</table></td>'
		+ '</tr></table>'
		+ '</div>' ;
	el.innerHTML = html ;
}
