| 1 | package App::WRT::HTML; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | no warnings 'uninitialized'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 1 | ▶ | use Exporter; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | our @ISA = qw(Exporter); | |||||||||
| 9 | |||||||||||
| 10 | 1 | 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 | 1 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | |||||||||
| 20 | 1 | our @EXPORT = qw( ); | |||||||||
| 21 | |||||||||||
| 22 | 1 | ▶ | use HTML::Entities qw(encode_entities); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 23 | |||||||||||
| 24 | # Generate subs for these: | ||||||||||
| 25 | 1 | 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 | 1 | 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 | 1 | ▶ | { no strict 'refs'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 54 | |||||||||||
| 55 | 1 | for my $key (keys %tags) { | |||||||||
| 56 | 15 | my $subname = $tagmap{$key}; | |||||||||
| 57 | 15 | ▶ | $subname = $key unless ($subname); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | |||||||||||
| 59 | 2 | ▶ | *{ $subname } = sub { $tags{$key}->($key, @_); }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 60 | } | ||||||||||
| 61 | |||||||||||
| 62 | } | ||||||||||
| 63 | |||||||||||
| 64 | # handle most HTML tags: | ||||||||||
| 65 | 2 | ▶ | sub tag { | ||||||||
|
Subroutine
tag: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||
| 66 | 2 | my ($tag) = shift; | |||||||||
| 67 | |||||||||||
| 68 | 2 | my ($attr_string, $text); | |||||||||
| 69 | |||||||||||
| 70 | 2 | for my $param (@_) { | |||||||||
| 71 | |||||||||||
| 72 | 4 | ▶ | if (ref($param)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | 2 | foreach my $attr (sort keys %{ $param }) { | |||||||||
| 77 | 3 | my $value = encode_entities( ${ $param }{$attr} ); | |||||||||
| 78 | 3 | $attr_string .= ' ' . $attr . '="' . $value . '"'; | |||||||||
| 79 | } | ||||||||||
| 80 | } | ||||||||||
| 81 | else { | ||||||||||
| 82 | # Text that goes inside the content of the tag. | ||||||||||
| 83 | 2 | ▶ | $text .= "\n" if length($text) > 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | 2 | $text .= $param; | |||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | } | ||||||||||
| 88 | |||||||||||
| 89 | # Voila, an X(HT)ML tag, pretty much: | ||||||||||
| 90 | 2 | return '<' . $tag . $attr_string . '>' . $text . '</' . $tag . '>'; | |||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | # Special cases and higher-level markup | ||||||||||
| 94 | |||||||||||
| 95 | 0 | ▶ | sub entry_markup { | ||||||||
|
Subroutine
entry_markup: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 96 | 0 | return qq{\n\n<article><div class="entry">} | |||||||||
| 97 | . $_[0] | ||||||||||
| 98 | . "</div></article>\n\n"; | ||||||||||
| 99 | } | ||||||||||
| 100 | |||||||||||
| 101 | 0 | ▶ | sub heading { | ||||||||
|
Subroutine
heading: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 102 | 0 | my ($text, $level) = @_; | |||||||||
| 103 | 0 | my $h = "h$level"; | |||||||||
| 104 | 0 | return tag($h, $text); | |||||||||
| 105 | } | ||||||||||
| 106 | |||||||||||
| 107 | 1 | 1; | |||||||||