package html;

use Data::Dump qw/dump/;

sub table {
	my $cols = shift;
	my $th;

	if ( $cols < 0 ) {
		$cols = abs($cols);
		$th .= qq|<th>| . shift(@_) . qq|</td>| foreach ( 1 .. $cols );
		$th .= qq|</tr>\n<tr>|;
	}

	my @td = map { "<td>$_</td>" } @_;
	my $html = qq{<table>\n<tr>$th};
	foreach ( 0 .. $#td ) {
			$html .= $td[$_];
			$html .= qq{</tr>\n<tr>} if ( $_ + 1 ) % $cols == 0;
	}
	$html .= qq{</tr>\n</table>};
}

sub tabs {
	return unless @_;
	my $html = qq{<table border=1>\n<tr>};
	foreach my $row ( @_ ) {
		$row =~ s{\t}{</td><td>}g;
		$html .= qq|<tr><td>$row</td><tr>\n|;
	}
	$html .= qq|</table>|;
	return $html;
}
	

sub tt {
	qq|<tt>| . join(' ', @_) . qq|</tt>| if @_;
}

sub select {
	my $name = shift;
	my $selected_option = shift;
	return join("\n"	
		, qq|<select type=select name=$name>|
		, join("\n", map { my $selected = $_ eq $selected_option ? 'selected' : ''; qq|<option name=$_ $selected>$_</option>| } @_ )
		, qq|</select>|
		, qq|</form>|
	);
}

sub pre {
	qq|<pre>| . join(' ', @_) . qq|</pre>| if @_;
}

sub pre_dump {
	my $data = shift;
	my $dump = dump( $data );
	$dump =~ s{"([^"]+)"(\s*=>)}{$1  $2}gs;
	$dump =~ s{"([^"]*)"}{"<b>$1</b>"}gs;
	qq|<pre>$dump</pre>|;
}

warn "loaded";

1;
