HTML-AutoTag
view release on metacpan or search on metacpan
lib/HTML/AutoTag.pm view on Meta::CPAN
$LEVEL++;
$cdata = $NEWLINE;
for (0 .. $#{ $args{cdata} }) {
$cdata .= $self->tag( %{ $args{cdata}[$_] } );
}
$LEVEL--;
} else {
my $str = '';
for (@{ $args{cdata} }) {
$str .= $self->tag( tag => $args{tag}, attr => $attr, cdata => $_);
}
return $str;
}
} elsif (ref($args{cdata}) eq 'HASH') {
$LEVEL++;
$cdata = $NEWLINE . $self->tag( %{ $args{cdata} } );
$LEVEL--;
} else {
$cdata = $ENCODE ? HTML::Entities::encode_entities( $args{cdata}, $ENCODES ) : $args{cdata};
$no_post_indent = 1;
}
return ( $INDENT x $LEVEL )
. "<$args{tag}" . ( defined( $attr_str ) ? $attr_str : scalar( %$attr ) )
. ">$cdata"
. ( $no_post_indent ? '' : ( $INDENT x $LEVEL ) )
. "</$args{tag}>$NEWLINE"
;
}
1;
__END__
=head1 NAME
HTML::AutoTag - Just another HTML tag generator.
=head1 SYNOPSIS
use HTML::AutoTag;
my $auto = HTML::AutoTag->new( indent => ' ', encode => 1 );
my %attr = ( style => { color => [qw(red green)] } );
my @data = qw( one two three four five six seven eight );
print $auto->tag(
tag => 'ol',
attr => {qw( reversed reversed )},
cdata => [
map { tag => 'li', attr => \%attr, cdata => $_ }, @data
]
);
=head1 DESCRIPTION
Generate nested HTML (HTML4, XHTML and HTML5) tags with custom indentation,
custom encoding and automatic attribute value rotation.
=head1 METHODS
=over 4
=item * C<new()>
Accepts the following arguments:
=over 8
=item * C<encode>
Encode HTML entities. Boolean. Defaults to false, which produces no encoding.
If set to true without further specifying a value for C<encodes> (see below),
will encode all control chars, high bit chars and '<', '&', '>', ''' and '"'.
encode => 1
=item * C<encodes>
Encode HTML entities. String. Set value to those characters you wish to
have encoded.
encodes => '<=>'
=item * C<indent>
Pretty print results. Defaults to undef which produces no indentation.
Set value to any number of spaces or tabs and newlines will also be appended.
indent => ' '
indent => "\t"
=item * C<level>
Indentation level to start at. Can be used in conjunction with C<indent>
to set indentation even deeper to match any existing HTML this code
may be injected into.
level => 2
=item * C<sorted>
Sorts the attribute names of the tag alphabetically. This is mostly
useful for ensuring consistancy. The attributes (and potential sorting)
happen within L<Tie::Hash::Attribute>. You most likely will not
need this feature.
sorted => 1
=back
=item * C<tag()>
Accepts three arguments:
=over 8
( run in 2.303 seconds using v1.01-cache-2.11-cpan-119454b85a5 )