
/*
	$config = array (
#		'/ /'            => '&nbsp;&nbsp;',
#		'/(&nbsp;)+$/'   => '',
		'/\t/'           => '&nbsp;&nbsp;&nbsp;&nbsp;',
		"/^[^\[](.+)$/e" => '$this->pop_state("\\1")',
		"/^$/e"          => "\$this->pop_state('<p>')",
		"/^\[\s+$/"   => '<div class=code>&nbsp;</div>',
		"/^\[\s(.*)$/"  => '<div class=code>\\1</div>'
#		"/^\[(&nbsp;)+$/"   => '<div class=code>&nbsp;</div>',
#		"/^\[&nbsp;(.*)$/"  => '<div class=code>\\1</div>'
	;

*/

#	$transform = array(
#		'/^\*\*\*\*\s(.*)\s\*\*\*\*/m' => "<h1> $1 </h1>\n",
#		'/^\*\*\*\s(.*)\s\*\*\*/m'     => "<h2> $1 </h2>\n",
#		'/^\*\*\s(.*)\s\*\*/m'         => "<h3> $1 </h3>\n",
#		'/^\*\s([^\*]+)\s\*\s*$/m'     => "<h4> $1 </h4>\n",
#		'/\s_((\n|.)*?)_\s/' => ' <i>$1</i> ',
#		'/\n<xx>((\n|.)*)<\/xx>/' => '<b>$1</b>',
#		'/\n-\s+((\n|.)*?)-/' => '<ul><li>$1</li></ul>',
#		'/mmm((.|\n)*)\n\n/' => '<b>$1</b>',
#		'/^\s*$/m' => '<p>',
#		'/^(.{10,30})$/m' => '$1<br>'
#	);


	
	$always = array (
		'/\s_(.*)_\s/' => '<i>$1</i>',
		'/\*([^\s\*]+[^\*]*[^\s])\*/' => '<b>$1</b>',
		'/\[(.*?)\|(.*?)\]/' => '<a href="$2">$1</a>',
		'/(^|\s)(.*?)@(.*?)/' => '$1$2<!-- -->@<!-- -->$3',
		'/^---\s*$/' => '<table border=0 cellpadding=0 cellspacing=0 width="100%" bgcolor=black><tr><td><img height=1 width=2></td></tr></table>',
		"!(^|[^/]|\s)((www\.)([-a-z0-9.]{2,}\.[a-z]{2,4}(:[0-9]+)?)((/([^\s]*[^\s.,\"'])?)?)((\?([^\s]*[^\s.,\"'])?)?))!i" => '$1<a href="http://$2">$2</a>',
		"!(^|[^\"]|\s)(((http(s?)|ftp)://)([-a-z0-9.]{2,}\.[a-z]{2,4}(:[0-9]+)?)((/([^\s]*[^\s.,\"'])?)?)((\?([^\s]*[^\s.,\"'])?)?))!i" => '$1<a href="$2">$6$8</a>',
	);
	
	$nostate = array (
		'/^\*\*\*\*\s(.*)\s\*\*\*\*/' => '<h1>$1</h1>',
		'/\*\*\*\s(.*)\s\*\*\*/'     => "<h2>$1</h2>\n",
		'/^\*\*\s(.*)\s\*\*/'         => '<h3>$1</h3>',
		'/^\*\s([^\*]+)\s\*\s*$/'     => '<h4>$1</h4>',
		"/^\s*$/"       => '<br>',
#		"/^\s+(.*)/e"   => "\$this->push_state('blockquote', '\\1')",
#		"/^-\s+(.*)/e"  => "\$this->push_state('bullet','<li>\\1')",
#		"/^#\s+(.*)/e"  => "\$this->push_state('number','<li>\\1')",
#		"/^\{\{$/e"     => "\$this->push_state('code')",
#		"/^'\s+(.*)/e"  => "\$this->push_state('pre','\\1')",
#		"/^>\s(.*)/e"   => "\$this->push_state('prompt','<div class=code>\\0</div>')",
#		"/^\[\s(.*)/e"  => "\$this->begin_config('\\1')",
#		"/^\[\[$/e"     => "\$this->begin_config('\\1',true)",
	);
	
	$blockquote = array (
		"/^$/e" => "\$this->pop_state('')",
		"/^([^\s].*)$/e" => "\$this->pop_state('\\1')"
	);

	$pre = array (
		"/^[^'](.*)$/e"  => "\$this->pop_state('\\1')",
		"/^'(.*)$/"  => '\\1'
	);
	
	$bullet = array (
		"/^-\s+(.*)/" => "<li>\\1",
		"/^$/e" => "\$this->pop_state('')"
	);
	
	$number = array (
		"/^#\s+(.*)/" => "<li>\\1",
		"/^$/e" => "\$this->pop_state('')"
	);


	$code = array (
		"/^\}\}$/e" => "\$this->pop_state()",
#		"/^(.+)$/"  => '<pre>\\0</pre>',
#		"/^$/"      => '<p>',
	);

	$prompt = array (
		"/^[^>](.+)$/e" => "\$this->pop_state('\\0')",
		"/^$/e"         => "\$this->pop_state('<p>')",
		"/^(.+)$/"      => '<div class=code>\\0</div>'
	);

	$config = array (
		"/^[^\[](.+)$/e" => "\$this->end_config('\\1')",
		"/^$/e"          => "\$this->end_config('<p>')",
		"/^\[\[$/e"      => "\$this->end_config('<p>')",
		"/^\[\s(.*)$/e"  => "\$this->do_config('\\1')"
	);
	
	$arrays = array('always','nostate','blockquote','pre','bullet','number','code','prompt','config');
	foreach($arrays as $array) {
		$this->transforms[$array] = array();
		$this->transforms[$array]['search'] = array_keys($$array);
		$this->transforms[$array]['replace'] = array_values($$array);
	}


function begin_config($line) {
	$this->buffer = array();
	$this->buffer[] = $line;
	$this->push_state('config', '<!-- -->');
	return '<!-- -->';
#	return $this->push_state('config',"<div class=code>$line</div>");
}

function end_config($line) {
	$count = count($this->buffer);
	$html  = "<table cellpadding=0 cellspacing=0 border=0><form><tr><td>";
	$html .= "<textarea class=code cols=60 rows=$count>";
	$html .= join("\n",$this->buffer);
	$html .= "</textarea></td></tr></form></table>";
	$html .= $line;
	return $this->pop_state($html);
}

function do_config($line) {
	$this->buffer[] = $line;
	return '<!-- -->';
}

#########################################################
# state functions

function state() {
	if (count($this->state) == 0)
		return '';
	else 
		return $this->state[count($this->state)-1];
}

function push_state($newstate, $str='') {
	$this->state[] = $newstate;
	return $this->states[$newstate]['open'] . stripslashes($str);
}

function pop_state($str='') {
	$s = array_pop($this->state);
	return $this->states[$s]['close'] . stripslashes($str);
}

