package tag;
use strict;
use html;
sub p {
my $ac=scalar(@_);
my $tagName=shift;
my $attrs=shift;
my $inline=shift;
my $res= &rawHead ."$tagName ";
while(my ($k,$v)=each %{$attrs}) {
my $e=&getExist($v);
if ($e == 2) {
$res.=" $k ";
}
if ($e == 0) {
$res.= qq($k="@{[&html::escape($v)]}" );
}
}
$res .= &rawTail;
if ($ac<3) {
return $res;
}
if (!ref $inline) {
$res.=$inline;
} else {
$res .=join("",@{$inline});
}
$res.= &rawHead. "/$tagName". &rawTail;
$res;
}
sub new {
my $c=shift;
bless {name=>shift, inline=>"",attrs=>{}};
}
sub setAttr{
my $t=shift;
my $name=shift;
my $value=shift;
$t->{attrs}->{$name}=$value;
$t;
}
sub addInline{
my $t=shift;
my $c=shift;
$t->{inline}.=$c;
$t;
}
sub add {
my $t=shift;
my $c=shift;
if (ref $c) {$t->addInline($c->out);}
else {$t->addInline($c);}
$t;
}
sub out {
my $t=shift;
&tag::p($t->{name},$t->{attrs},$t->{inline});
}
sub exist {
my $e=shift;
[$e];
}
sub getExist {
my $v=shift;
if ( (ref $v) eq "ARRAY") {
return 2 if ($v->[0]) ;
return 1;
}
return 0;
}
my $rh;
sub rawHead {
return "<";
if (!$rh) {
$rh="QXh".(1000+int(rand(1000)));
}
$rh;
}
my $rt;
sub rawTail {
return ">";
if (!$rt) {
$rt="QXt".(1000+int(rand(1000)));
}
$rt;
}
1;