#
# reformat html inside xml back to html (basically, support for tables)
#
# filed #-# field ###
# ...

sub back2html {
	my $html = shift;
	my $url_params = shift;

	$html =~ s/</&lt;/g;
	$html =~ s/>/&gt;/g;
	my $out;
	foreach $line (split(/\s*###\s*/,$html)) {
		$out .= "<tr><td valign=\"top\" class=\"htmlLeft\">";
		my @items = split(/\s*#-#\s*/,$line);
		# convert spaces in left field to non-breaking spaces
		$items[0] =~ s#\s+#&nbsp;#g;
		# try to make link on right field
		sub mkurl($) {
			my $url = shift || die "mkurl needs url as argument";
			# chop URLS longer than ... characters
			my $txturl = substr($1,0,40);
			my $title = '';
			if (length($1) > 40) {
				$txturl .= "...";
				$title = " title=\"$url\"";
			}
			return "<a href=\"$url\"$title>$txturl</a>";
		}
		$items[1] =~ s#(http://[\S]+)#mkurl($1)#gie;
		$out .= join("</td><td>",@items);
		$out .= "</td></tr>\n";
	}
	# put <br> and <b> back into html
	$out =~ s,&lt;br/*&gt;,<br/>,gi;
	$out =~ s,&lt;b&gt;,<b>,gi;
	$out =~ s,&lt;/b&gt;,</b>,gi;
	# put <a href=xxx>yyyy</a> back into html
	sub mkurl_param($$$$$) {
		my ($a_href,$url,$text,$a_end, $url_params) = @_;
		if ($url_params) {
			if ($url =~ m#\?#) {
				$url .= "&".$url_params;
			} else {
				$url .= "?".$url_params;
			}
		}
		return '<'.$a_href.'"'.$url.'">'.$text.'<'.$a_end.'>';
	}
	$out =~ s#&lt;(a\s+href=)['"]*(.+?)['"]*&gt;(.+?)&lt;(/a)&gt;#mkurl_param($1,$2,$3,$4,$url_params)#gsie;
	return $out;
}

1;
