| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package WRT::HTML; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 4 |  |  | 4 |  | 61280 | use strict; | 
|  | 4 |  |  |  |  | 15 |  | 
|  | 4 |  |  |  |  | 106 |  | 
| 4 | 4 |  |  | 4 |  | 22 | use warnings; | 
|  | 4 |  |  |  |  | 6 |  | 
|  | 4 |  |  |  |  | 109 |  | 
| 5 | 4 |  |  | 4 |  | 18 | no  warnings 'uninitialized'; | 
|  | 4 |  |  |  |  | 29 |  | 
|  | 4 |  |  |  |  | 142 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 4 |  |  | 4 |  | 18 | use Exporter; | 
|  | 4 |  |  |  |  | 7 |  | 
|  | 4 |  |  |  |  | 523 |  | 
| 9 |  |  |  |  |  |  | our @ISA = qw(Exporter); | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | our %EXPORT_TAGS = ( 'all'       => [ qw(a div p em small strong table | 
| 12 |  |  |  |  |  |  | table_row table_cell entry_markup | 
| 13 |  |  |  |  |  |  | heading article nav section | 
| 14 |  |  |  |  |  |  | unordered_list ordered_list list_item) ], | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | 'highlevel' => [ qw(a p em small strong table | 
| 17 |  |  |  |  |  |  | table_row table_cell | 
| 18 |  |  |  |  |  |  | entry_markup heading) ] ); | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | 
| 21 |  |  |  |  |  |  | our @EXPORT = qw( ); | 
| 22 |  |  |  |  |  |  |  | 
| 23 | 4 |  |  | 4 |  | 289 | use HTML::Entities qw(encode_entities); | 
|  | 4 |  |  |  |  | 4301 |  | 
|  | 4 |  |  |  |  | 635 |  | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | # Generate subs for these: | 
| 26 |  |  |  |  |  |  | my %tags = ( | 
| 27 |  |  |  |  |  |  | p       => \&tag, | 
| 28 |  |  |  |  |  |  | em      => \&tag, | 
| 29 |  |  |  |  |  |  | small   => \&tag, | 
| 30 |  |  |  |  |  |  | strong  => \&tag, | 
| 31 |  |  |  |  |  |  | table   => \&tag, | 
| 32 |  |  |  |  |  |  | tr      => \&tag, | 
| 33 |  |  |  |  |  |  | td      => \&tag, | 
| 34 |  |  |  |  |  |  | a       => \&tag, | 
| 35 |  |  |  |  |  |  | div     => \&tag, | 
| 36 |  |  |  |  |  |  | article => \&tag, | 
| 37 |  |  |  |  |  |  | nav     => \&tag, | 
| 38 |  |  |  |  |  |  | section => \&tag, | 
| 39 |  |  |  |  |  |  | ul      => \&tag, | 
| 40 |  |  |  |  |  |  | ol      => \&tag, | 
| 41 |  |  |  |  |  |  | li      => \&tag, | 
| 42 |  |  |  |  |  |  | ); | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | # ...but map these tags to different sub names: | 
| 45 |  |  |  |  |  |  | my %tagmap = ( | 
| 46 |  |  |  |  |  |  | tr => 'table_row', | 
| 47 |  |  |  |  |  |  | td => 'table_cell', | 
| 48 |  |  |  |  |  |  | ul => 'unordered_list', | 
| 49 |  |  |  |  |  |  | ol => 'ordered_list', | 
| 50 |  |  |  |  |  |  | li => 'list_item', | 
| 51 |  |  |  |  |  |  | ); | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | # Install appropriate subs in symbol table: | 
| 54 | 4 |  |  | 4 |  | 29 | { no strict 'refs'; | 
|  | 4 |  |  |  |  | 8 |  | 
|  | 4 |  |  |  |  | 1607 |  | 
| 55 |  |  |  |  |  |  |  | 
| 56 |  |  |  |  |  |  | for my $key (keys %tags) { | 
| 57 |  |  |  |  |  |  | my $subname = $tagmap{$key}; | 
| 58 |  |  |  |  |  |  | $subname = $key unless ($subname); | 
| 59 |  |  |  |  |  |  |  | 
| 60 | 233 |  |  | 233 |  | 1210 | *{ $subname } = sub { $tags{$key}->($key, @_); }; | 
| 61 |  |  |  |  |  |  | } | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | } | 
| 64 |  |  |  |  |  |  |  | 
| 65 |  |  |  |  |  |  | # handle most HTML tags: | 
| 66 |  |  |  |  |  |  | sub tag { | 
| 67 | 243 |  |  | 243 | 0 | 325 | my ($tag) = shift; | 
| 68 | 243 |  |  |  |  | 337 | my (@params) = @_; | 
| 69 |  |  |  |  |  |  |  | 
| 70 | 243 |  |  |  |  | 212 | my ($attr_string, $text); | 
| 71 |  |  |  |  |  |  |  | 
| 72 | 243 |  |  |  |  | 268 | for my $param (@params) { | 
| 73 |  |  |  |  |  |  |  | 
| 74 | 440 | 100 |  |  |  | 584 | if (ref($param)) { | 
| 75 |  |  |  |  |  |  | # We sort these because, if using each, order is random(ish), and | 
| 76 |  |  |  |  |  |  | # this can lead to different HTML for the same input: | 
| 77 | 193 |  |  |  |  | 188 | foreach my $name (sort keys %{ $param }) { | 
|  | 193 |  |  |  |  | 467 |  | 
| 78 | 311 |  |  |  |  | 266 | my $value = encode_entities( ${ $param }{$name} ); | 
|  | 311 |  |  |  |  | 503 |  | 
| 79 | 311 |  |  |  |  | 3656 | $attr_string .= qq{ $name="$value"}; | 
| 80 |  |  |  |  |  |  | } | 
| 81 |  |  |  |  |  |  | } | 
| 82 |  |  |  |  |  |  | else { | 
| 83 | 247 | 100 |  |  |  | 384 | $text .= "\n" if length($text) > 0; | 
| 84 | 247 |  |  |  |  | 282 | $text .= $param; | 
| 85 |  |  |  |  |  |  | } | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | } | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  | # voila, an X(HT)ML tag: | 
| 90 | 243 |  |  |  |  | 949 | return "<${tag}${attr_string}>$text</$tag>"; | 
| 91 |  |  |  |  |  |  | } | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  | # Special cases and higher-level markup | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | sub entry_markup { | 
| 96 | 16 |  |  | 16 | 0 | 36 | my ($text) = @_; | 
| 97 | 16 |  |  |  |  | 99 | return "\n\n" . article(div($text, { class => 'entry' })) . "\n\n"; | 
| 98 |  |  |  |  |  |  | } | 
| 99 |  |  |  |  |  |  |  | 
| 100 |  |  |  |  |  |  | sub heading { | 
| 101 | 10 |  |  | 10 | 0 | 19 | my ($text, $level) = @_; | 
| 102 | 10 |  |  |  |  | 19 | my $h = "h$level"; | 
| 103 | 10 |  |  |  |  | 25 | return tag($h, $text); | 
| 104 |  |  |  |  |  |  | } | 
| 105 |  |  |  |  |  |  |  | 
| 106 |  |  |  |  |  |  | 1; |