File Coverage

blib/lib/App/WRT/HTML.pm
Criterion Covered Total %
statement 35 35 100.0
branch 4 4 100.0
condition n/a
subroutine 10 10 100.0
pod 0 3 0.0
total 49 52 94.2


line stmt bran cond sub pod time code
1             package App::WRT::HTML;
2              
3 9     9   101668 use strict;
  9         27  
  9         255  
4 9     9   42 use warnings;
  9         14  
  9         236  
5 9     9   39 no warnings 'uninitialized';
  9         14  
  9         278  
6              
7 9     9   55 use Exporter;
  9         14  
  9         1279  
8             our @ISA = qw(Exporter);
9              
10             our %EXPORT_TAGS = ( 'all' => [ qw(a div p em small strong table
11             table_row table_cell entry_markup
12             heading article nav section
13             unordered_list ordered_list list_item) ],
14              
15             'highlevel' => [ qw(a p em small strong table
16             table_row table_cell
17             entry_markup heading) ] );
18              
19             our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
20             our @EXPORT = qw( );
21              
22 9     9   577 use HTML::Entities qw(encode_entities);
  9         5889  
  9         1460  
23              
24             # Generate subs for these:
25             my %tags = (
26             p => \&tag,
27             em => \&tag,
28             small => \&tag,
29             strong => \&tag,
30             table => \&tag,
31             tr => \&tag,
32             td => \&tag,
33             a => \&tag,
34             div => \&tag,
35             article => \&tag,
36             nav => \&tag,
37             section => \&tag,
38             ul => \&tag,
39             ol => \&tag,
40             li => \&tag,
41             );
42              
43             # ...but map these tags to different sub names:
44             my %tagmap = (
45             tr => 'table_row',
46             td => 'table_cell',
47             ul => 'unordered_list',
48             ol => 'ordered_list',
49             li => 'list_item',
50             );
51              
52             # Install appropriate subs in symbol table:
53 9     9   66 { no strict 'refs';
  9         32  
  9         3811  
54              
55             for my $key (keys %tags) {
56             my $subname = $tagmap{$key};
57             $subname = $key unless ($subname);
58              
59 1660     1660   4581 *{ $subname } = sub { $tags{$key}->($key, @_); };
60             }
61              
62             }
63              
64             # handle most HTML tags:
65             sub tag {
66 1720     1720 0 2183 my ($tag) = shift;
67              
68 1720         1823 my ($attr_string, $text);
69              
70 1720         2102 for my $param (@_) {
71              
72 3070 100       4118 if (ref($param)) {
73             # A hashref containing one or more attribute => value pairs. We sort
74             # these by key because, if using each, order is random(ish), and this can
75             # lead to different HTML for the same input.
76 1270         1310 foreach my $attr (sort keys %{ $param }) {
  1270         3220  
77 1905         1949 my $value = encode_entities( ${ $param }{$attr} );
  1905         3369  
78 1905         22982 $attr_string .= ' ' . $attr . '="' . $value . '"';
79             }
80             }
81             else {
82             # Text that goes inside the content of the tag.
83 1800 100       3012 $text .= "\n" if length($text) > 0;
84 1800         2329 $text .= $param;
85             }
86              
87             }
88              
89             # Voila, an X(HT)ML tag, pretty much:
90 1720         6162 return '<' . $tag . $attr_string . '>' . $text . '';
91             }
92              
93             # Special cases and higher-level markup
94              
95             sub entry_markup {
96 240     240 0 1330 return qq{\n\n
}
97             . $_[0]
98             . "\n\n";
99             }
100              
101             sub heading {
102 60     60 0 153 my ($text, $level) = @_;
103 60         102 my $h = "h$level";
104 60         117 return tag($h, $text);
105             }
106              
107             1;