line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package HTML::AutoTag; |
2
|
8
|
|
|
8
|
|
241316
|
use 5.006; |
|
8
|
|
|
|
|
24
|
|
3
|
8
|
|
|
8
|
|
33
|
use strict; |
|
8
|
|
|
|
|
12
|
|
|
8
|
|
|
|
|
179
|
|
4
|
8
|
|
|
8
|
|
29
|
use warnings FATAL => 'all'; |
|
8
|
|
|
|
|
16
|
|
|
8
|
|
|
|
|
486
|
|
5
|
|
|
|
|
|
|
our $VERSION = '1.06'; |
6
|
|
|
|
|
|
|
|
7
|
8
|
|
|
8
|
|
3930
|
use HTML::Entities; |
|
8
|
|
|
|
|
33313
|
|
|
8
|
|
|
|
|
557
|
|
8
|
8
|
|
|
8
|
|
3250
|
use Tie::Hash::Attribute; |
|
8
|
|
|
|
|
5489
|
|
|
8
|
|
|
|
|
4074
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our( $INDENT, $NEWLINE, $LEVEL, $ENCODE, $ENCODES, $SORTED ); |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub new { |
13
|
15
|
|
|
15
|
1
|
409857
|
my $self = shift; |
14
|
15
|
|
|
|
|
42
|
my $args = {@_}; |
15
|
15
|
100
|
|
|
|
50
|
$ENCODE = defined $args->{encode} ? $args->{encode} : exists $args->{encodes}; |
16
|
15
|
100
|
|
|
|
35
|
$ENCODES = defined $args->{encodes} ? $args->{encodes} : ''; |
17
|
15
|
100
|
|
|
|
34
|
$INDENT = defined $args->{indent} ? $args->{indent} : ''; |
18
|
15
|
100
|
|
|
|
31
|
$NEWLINE = defined $args->{indent} ? "\n" : ''; |
19
|
15
|
|
50
|
|
|
75
|
$LEVEL = $args->{level} || 0; |
20
|
15
|
|
|
|
|
20
|
$SORTED = $args->{sorted}; |
21
|
15
|
|
|
|
|
48
|
bless {}, $self; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub tag { |
25
|
267
|
|
|
267
|
1
|
982
|
my $self = shift; |
26
|
267
|
|
|
|
|
340
|
my %args = @_; |
27
|
267
|
|
|
|
|
200
|
my $attr = $args{attr}; |
28
|
|
|
|
|
|
|
|
29
|
267
|
50
|
|
|
|
354
|
return '' unless defined $args{tag}; |
30
|
|
|
|
|
|
|
|
31
|
267
|
|
|
|
|
173
|
my $attr_str; |
32
|
267
|
100
|
|
|
|
443
|
if (grep ref($_), values %$attr) { |
33
|
|
|
|
|
|
|
# complex attrs use a tied hash |
34
|
47
|
50
|
|
|
|
340
|
unless (grep /^-/, keys %$attr) { |
35
|
47
|
|
|
|
|
244
|
tie my %attr, 'Tie::Hash::Attribute', sorted => $SORTED; |
36
|
47
|
|
|
|
|
281
|
%attr = %$attr; |
37
|
47
|
|
|
|
|
480
|
$attr = \%attr; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
} else { |
40
|
|
|
|
|
|
|
# simple attrs can bypass being tied |
41
|
220
|
|
|
|
|
163
|
$attr_str = ''; |
42
|
220
|
100
|
|
|
|
333
|
my @keys = $SORTED ? sort keys %$attr : keys %$attr; |
43
|
220
|
|
|
|
|
253
|
for my $key (@keys) { |
44
|
79
|
|
|
|
|
315
|
$attr_str .= ' ' . Tie::Hash::Attribute::_key( $key ) . '="' . Tie::Hash::Attribute::_val( $attr->{$key} ) . '"'; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
# emtpy tag |
49
|
267
|
100
|
|
|
|
740
|
unless (defined $args{cdata}) { |
50
|
35
|
50
|
|
|
|
148
|
return ( $INDENT x $LEVEL ) |
51
|
|
|
|
|
|
|
. "<$args{tag}" |
52
|
|
|
|
|
|
|
. ( defined( $attr_str ) ? $attr_str : scalar( %$attr ) ) |
53
|
|
|
|
|
|
|
. " />$NEWLINE" |
54
|
|
|
|
|
|
|
; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
232
|
|
|
|
|
160
|
my $cdata; |
58
|
|
|
|
|
|
|
my $no_post_indent; |
59
|
232
|
100
|
|
|
|
401
|
if (ref($args{cdata}) eq 'ARRAY') { |
|
|
100
|
|
|
|
|
|
60
|
|
|
|
|
|
|
|
61
|
12
|
100
|
|
|
|
33
|
if (ref($args{cdata}[0]) eq 'HASH') { |
62
|
|
|
|
|
|
|
|
63
|
4
|
|
|
|
|
5
|
$LEVEL++; |
64
|
4
|
|
|
|
|
6
|
$cdata = $NEWLINE; |
65
|
4
|
|
|
|
|
6
|
for (0 .. $#{ $args{cdata} }) { |
|
4
|
|
|
|
|
15
|
|
66
|
18
|
|
|
|
|
293
|
$cdata .= $self->tag( %{ $args{cdata}[$_] } ); |
|
18
|
|
|
|
|
65
|
|
67
|
|
|
|
|
|
|
} |
68
|
4
|
|
|
|
|
51
|
$LEVEL--; |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
} else { |
71
|
8
|
|
|
|
|
8
|
my $str = ''; |
72
|
8
|
|
|
|
|
7
|
for (@{ $args{cdata} }) { |
|
8
|
|
|
|
|
15
|
|
73
|
36
|
|
|
|
|
882
|
$str .= $self->tag( tag => $args{tag}, attr => $attr, cdata => $_); |
74
|
|
|
|
|
|
|
} |
75
|
8
|
|
|
|
|
196
|
return $str; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
} elsif (ref($args{cdata}) eq 'HASH') { |
79
|
14
|
|
|
|
|
13
|
$LEVEL++; |
80
|
14
|
|
|
|
|
11
|
$cdata = $NEWLINE . $self->tag( %{ $args{cdata} } ); |
|
14
|
|
|
|
|
78
|
|
81
|
14
|
|
|
|
|
17
|
$LEVEL--; |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
} else { |
84
|
206
|
100
|
|
|
|
243
|
$cdata = $ENCODE ? HTML::Entities::encode_entities( $args{cdata}, $ENCODES ) : $args{cdata}; |
85
|
206
|
|
|
|
|
526
|
$no_post_indent = 1; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
224
|
100
|
|
|
|
921
|
return ( $INDENT x $LEVEL ) |
|
|
100
|
|
|
|
|
|
89
|
|
|
|
|
|
|
. "<$args{tag}" . ( defined( $attr_str ) ? $attr_str : scalar( %$attr ) ) |
90
|
|
|
|
|
|
|
. ">$cdata" |
91
|
|
|
|
|
|
|
. ( $no_post_indent ? '' : ( $INDENT x $LEVEL ) ) |
92
|
|
|
|
|
|
|
. "$args{tag}>$NEWLINE" |
93
|
|
|
|
|
|
|
; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
1; |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
__END__ |