<?php

/*
	here you can adapt the colors to your taste ...
*/
define(UBB_CODE_GUTTER_FORE, '#000000'); //fontcolor of the gutter
define(UBB_CODE_GUTTER_BACK, '#BBBBBB'); //backgroundcolor of the gutter

define(UBB_CODE_TEXT_FORE, '#000000'); //fontcolor of the text
define(UBB_CODE_TEXT_BACK, '#FFFFFF'); //backgroundcolor of the text

class UBB_CODE extends UBB
{
	var $allow1 = array('EMPTY', 'STRING');
	var $allow2 = 'STRING';
	
	var $info = array('Name'=>'Code', 'Version'=>0.9, 'Info'=>'Display formatted programmerscode, with some colouring (only limited support for strings ...), and linenumbers.');
	
	var $ext_types = array('STRING'=>'(.|\s)*');
	
	function process($value1, $value2)
	{
		$value2 = htmlspecialchars($value2, ENT_NOQUOTES);
		$value2 = str_replace("\t", str_repeat('&nbsp;', 4), $value2);
		$value2 = str_replace(" ", str_repeat('&nbsp;', 1), $value2);
		
		$lines = split("\n", $value2);
		$lines[] = '';
		
		$ret = '';
		
		$ret.="\n".'<div	style="border:1px solid black;font-family:Courier New;">';
		
		//print title
		if ($value1!='')	$ret.="\n	<div	style='font-family:Verdana,Serif;border-bottom:1px black solid; margin-bottom:5px'>$value1</div>";
		
		//get the gutter (containing linenumbers) and the textarea ...
		$gutter = '';
		$text = '';
		
		for($i=0; $i<count($lines); $i++)
		{
			if ($i!=count($lines)-1)
			{
				$gutter.="\n		".($i+1)."<br />";
				$text.="\n		".$lines[$i]."<br />";
			}
		}
		
		//formatting strings ...
		$text = preg_replace('/("|\')((.|\s)+?)\1/i', "\$1<i	style='color:red'>\$2</i>\$1", $text);
		
		
		$gutter ="\n	<div	style='border-left:1px solid black;background:".UBB_CODE_GUTTER_BACK.";color:".UBB_CODE_GUTTER_FORE.";float:left;width:40px;text-align:right;margin-right:4px;'>".$gutter."\n	</div>";
		
		$text ="\n	<div	style='border-left:1px solid black;background:".UBB_CODE_TEXT_BACK.";color:".UBB_CODE_TEXT_FORE."'>".$text."\n	</div>";
		
		//... and add them to the returnvalue
		$ret.=$gutter.$text;
		$ret.="\n</div>";
		
		return $ret;
	}
}

?>