| 1 | package App::Followme::Web; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 21 | ▶ | use 5.008005; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 21 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 21 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 21 | ▶ | use integer; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 21 | ▶ | use lib '../..'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 21 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 21 | require Exporter; | |||||||||||||||||||||||||||
| 12 | 21 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 13 | 21 | our @EXPORT = qw(web_has_variables web_is_tag web_match_tags web_parse_sections | |||||||||||||||||||||||||||
| 14 | web_same_tag web_substitute_sections web_substitute_tags | ||||||||||||||||||||||||||||
| 15 | web_parse_tag web_only_tags web_only_text web_split_at_tags | ||||||||||||||||||||||||||||
| 16 | web_titled_sections); | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 21 | our $VERSION = "2.03"; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 21 | # Extract a list of parsed tags from a text | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 259 | ▶ | sub web_extract_tags { | ||||||||||||||||||||||||||
|
Subroutine
web_extract_tags: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 24 | 259 | my ($text) = @_; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 259 | my @tokens = web_split_at_tags($text); | |||||||||||||||||||||||||||
| 27 | 259 | return web_only_tags(@tokens); | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 31 | # Return true if any of a list of variables is present in a template | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 15 | ▶ | sub web_has_variables { | ||||||||||||||||||||||||||
|
Subroutine
web_has_variables: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 34 | 15 | my ($text, @search_variables) = @_; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 15 | my %template_variables; | |||||||||||||||||||||||||||
| 37 | 15 | foreach my $var ($text =~ /([\$\@]\w+)/g) { | |||||||||||||||||||||||||||
| 38 | 163 | $var =~ s/_by_\w+$//; | |||||||||||||||||||||||||||
| 39 | 163 | $template_variables{$var} = 1; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 15 | for my $var (@search_variables) { | |||||||||||||||||||||||||||
| 43 | 27 | ▶ | return 1 if $template_variables{$var}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 9 | return 0; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 50 | # Is the token an html tag? | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 9046 | ▶ | sub web_is_tag { | ||||||||||||||||||||||||||
|
Subroutine
web_is_tag: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 53 | 9046 | my ($token) = @_; | |||||||||||||||||||||||||||
| 54 | 9046 | ▶ | return $token =~ /^<[^!]/ ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 58 | # Call a function after a set of tags is matched | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 220 | ▶ | sub web_match_tags { | ||||||||||||||||||||||||||
|
Subroutine
web_match_tags: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 61 | 220 | my ($pattern, $text, $matcher, $metadata, $global) = @_; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 220 | my @tokens; | |||||||||||||||||||||||||||
| 64 | 220 | my $in = 0; | |||||||||||||||||||||||||||
| 65 | 220 | my $match_count = 0; | |||||||||||||||||||||||||||
| 66 | 220 | my @matches = web_extract_tags($pattern); | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 220 | foreach my $token (web_split_at_tags($text)) { | |||||||||||||||||||||||||||
| 69 | 2193 | ▶ | if (web_is_tag($token)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 1093 | my $tag = web_parse_tag($token); | |||||||||||||||||||||||||||
| 71 | 1093 | ▶ | if (web_same_tag($matches[$in], $tag)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 483 | $in += 1; | |||||||||||||||||||||||||||
| 73 | 483 | ▶ | if ($in >= @matches) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 326 | push(@tokens, $token); | |||||||||||||||||||||||||||
| 75 | 326 | $matcher->($metadata, @tokens); | |||||||||||||||||||||||||||
| 76 | 326 | $match_count += 1; | |||||||||||||||||||||||||||
| 77 | 326 | @tokens = (); | |||||||||||||||||||||||||||
| 78 | 326 | $in = 0; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 326 | ▶ | last unless $global; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | 2036 | ▶ | push(@tokens, $token) if $in > 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 220 | return $match_count; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 91 | # Extract a list of parsed tags from a set of tokens | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 429 | ▶ | sub web_only_tags { | ||||||||||||||||||||||||||
|
Subroutine
web_only_tags: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 94 | 429 | my (@tokens) = @_; | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 429 | my @tags; | |||||||||||||||||||||||||||
| 97 | 429 | foreach my $token (@tokens) { | |||||||||||||||||||||||||||
| 98 | 631 | ▶ | if (web_is_tag($token)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 626 | push(@tags, web_parse_tag($token)); | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 429 | return @tags; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 107 | # Parse a text string from a set of tokens | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 215 | ▶ | sub web_only_text { | ||||||||||||||||||||||||||
|
Subroutine
web_only_text: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 110 | 215 | my (@tokens) = @_; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 215 | my @text; | |||||||||||||||||||||||||||
| 113 | 215 | foreach my $token (@tokens) { | |||||||||||||||||||||||||||
| 114 | 677 | ▶ | push(@text, $token) unless web_is_tag($token); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 215 | my $text = join(' ', @text); | |||||||||||||||||||||||||||
| 118 | 215 | $text =~ s/\s+/ /g; | |||||||||||||||||||||||||||
| 119 | 215 | $text =~ s/^\s+//; | |||||||||||||||||||||||||||
| 120 | 215 | $text =~ s/\s+$//; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 215 | return $text; | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 126 | # Extract sections from file, store in hash | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 57 | ▶ | sub web_parse_sections { | ||||||||||||||||||||||||||
|
Subroutine
web_parse_sections: called CC=8 SCAR=21.3
Pod
documented
| |||||||||||||||||||||||||||||
| 129 | 57 | my ($text) = @_; | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 57 | my $name; | |||||||||||||||||||||||||||
| 132 | my %section; | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | # Extract sections from input | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 57 | my @tokens = split (/(<!--\s*(?:section|endsection)\s+.*?-->)/, $text); | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 57 | foreach my $token (@tokens) { | |||||||||||||||||||||||||||
| 139 | 717 | ▶ | if ($token =~ /^<!--\s*section\s+(\w+).*?-->/) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 140 | 165 | ▶ | if (defined $name) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | 0 | die "Nested sections in input: $token\n"; | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | 165 | $name = $1; | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | } elsif ($token =~ /^<!--\s*endsection\s+(\w+).*?-->/) { | ||||||||||||||||||||||||||||
| 146 | 165 | ▶ | if ($name ne $1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | 0 | die "Nested sections in input: $token\n"; | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | 165 | undef $name; | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | } elsif (defined $name) { | ||||||||||||||||||||||||||||
| 152 | 165 | $section{$name} = $token; | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 57 | ▶ | die "Unmatched section (<!-- section $name -->)\n" if $name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | 57 | return \%section; | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 161 | # Parse a web tag into attributes and their values | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 3002 | ▶ | sub web_parse_tag { | ||||||||||||||||||||||||||
|
Subroutine
web_parse_tag: called CC=12 SCAR=25.2
Pod
documented
| |||||||||||||||||||||||||||||
| 164 | 3002 | my ($tag) = @_; | |||||||||||||||||||||||||||
| 165 | 3002 | ▶ | croak "Not a tag: ($tag)" unless web_is_tag($tag); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 3002 | my @pattern; | |||||||||||||||||||||||||||
| 168 | 3002 | my $side = 0; | |||||||||||||||||||||||||||
| 169 | 3002 | my @pair = (undef, undef); | |||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 3002 | ▶ | while ($tag =~ /(=|"[^"]*"|[^<>="\s]+)/gs) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | 6941 | my $token = $1; | |||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 6941 | ▶ | if ($token eq '=') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 175 | 1158 | $side = 1; | |||||||||||||||||||||||||||
| 176 | 1158 | undef $token; | |||||||||||||||||||||||||||
| 177 | } elsif ($token =~ /^"/) { | ||||||||||||||||||||||||||||
| 178 | 1046 | $token =~ s/"//g; | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 6941 | ▶ | if (defined $token) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 182 | 5783 | ▶ | if (defined $pair[$side]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | 1623 | ▶ | if (defined $pair[0]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | 1623 | push(@pattern, @pair); | |||||||||||||||||||||||||||
| 185 | 1623 | @pair = (undef, undef); | |||||||||||||||||||||||||||
| 186 | 1623 | $side = 0; | |||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 5783 | ▶ | $token = lc($token) if $side == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 191 | 5783 | $pair[$side] = $token; | |||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 3002 | ▶ | push(@pattern, @pair) if defined $pair[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 3002 | ▶ | if (@pattern < 2 || defined $pattern[1]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@pattern < 2 or defined $pattern[1]
MC/DC0%0/2
@pattern < 2 or defined $pattern[1]
@pattern < 2
defined $pattern[1]
Truth table33%1/3
A or B
A= @pattern < 2B= defined $pattern[1]
| |||||||||||||||||||||||||||||
| 198 | 0 | unshift(@pattern, undef, undef); | |||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 3002 | $tag = shift @pattern; | |||||||||||||||||||||||||||
| 202 | 3002 | shift @pattern; | |||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 3002 | my %pattern = ('_', $tag, @pattern); | |||||||||||||||||||||||||||
| 205 | 3002 | return \%pattern; | |||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 209 | # Test if two parsed tags have the same name and attributes. | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | 2371 | ▶ | sub web_same_tag { | ||||||||||||||||||||||||||
|
Subroutine
web_same_tag: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 212 | 2371 | my ($match, $tag) = @_; | |||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 2371 | ▶ | croak "Match not parsed: $match" unless ref $match; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 215 | 2371 | ▶ | croak "Tag not parsed: $tag" unless ref $tag; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | 2371 | foreach my $name (keys %$match) { | |||||||||||||||||||||||||||
| 218 | 2722 | ▶ | return 0 unless exists $tag->{$name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 219 | 2572 | my $value = $match->{$name}; | |||||||||||||||||||||||||||
| 220 | 2572 | ▶ | return 0 if $value ne '*' && $tag->{$name} ne $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$value ne "*" and $tag->{$name} ne $value
MC/DC100%2/2
$value ne "*" and $tag->{$name} ne $value
$value ne "*"
$tag->{$name} ne $value
Truth table100%3/3
A and B
A= $value ne "*"B= $tag->{$name} ne $value
| |||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | 599 | return 1; | |||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 227 | # Return a list of tokens, split at tag boundaries | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | 526 | ▶ | sub web_split_at_tags { | ||||||||||||||||||||||||||
|
Subroutine
web_split_at_tags: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 230 | 526 | my ($text) = @_; | |||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | 526 | my @tokens; | |||||||||||||||||||||||||||
| 233 | 526 | ▶ | if ($text) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 234 | 522 | @tokens = split(/(<!--.*?-->|<[^">]*(?:"[^"]*")*[^>]*>)/s, $text); | |||||||||||||||||||||||||||
| 235 | 522 | @tokens = grep {length} @tokens; | |||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | 526 | return @tokens; | |||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 242 | # Substitue comment delimeted sections for same blocks in template | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | 64 | ▶ | sub web_substitute_sections { | ||||||||||||||||||||||||||
|
Subroutine
web_substitute_sections: called CC=8 SCAR=21.2
Pod
documented
| |||||||||||||||||||||||||||||
| 245 | 64 | my ($text, $section) = @_; | |||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | 64 | my $name; | |||||||||||||||||||||||||||
| 248 | my @output; | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | 64 | my @tokens = split (/(<!--\s*(?:section|endsection)\s+.*?-->)/, $text); | |||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | 64 | foreach my $token (@tokens) { | |||||||||||||||||||||||||||
| 253 | 744 | ▶ | if ($token =~ /^<!--\s*section\s+(\w+).*?-->/) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 254 | 170 | ▶ | if (defined $name) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 255 | 0 | die "Nested sections in template: $name\n"; | |||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | 170 | $name = $1; | |||||||||||||||||||||||||||
| 259 | 170 | push(@output, $token); | |||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | } elsif ($token =~ /^\s*<!--\s*endsection\s+(\w+).*?-->/) { | ||||||||||||||||||||||||||||
| 262 | 170 | ▶ | if ($name ne $1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 263 | 0 | die "Nested sections in template: $name\n"; | |||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | 170 | undef $name; | |||||||||||||||||||||||||||
| 267 | 170 | push(@output, $token); | |||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | } elsif (defined $name) { | ||||||||||||||||||||||||||||
| 270 | 170 | ▶ | $section->{$name} ||= $token; | ||||||||||||||||||||||||||
|
Condition66%2/3
$section->{$name} ||= $token
MC/DC0%0/2
$section->{$name} ||= $token
$section->{$name}
$token
Truth table66%2/3
A ||= B
A= $section->{$name}B= $token
| |||||||||||||||||||||||||||||
| 271 | 170 | push(@output, $section->{$name}); | |||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | } else { | ||||||||||||||||||||||||||||
| 274 | 234 | push(@output, $token); | |||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | 64 | return join('', @output); | |||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 282 | # Call a function after a set of tags is matched to generate substitute | ||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | 36 | ▶ | sub web_substitute_tags { | ||||||||||||||||||||||||||
|
Subroutine
web_substitute_tags: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||
| 285 | 36 | my ($pattern, $text, $substituter, $output, $global) = @_; | |||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | 36 | my @tokens; | |||||||||||||||||||||||||||
| 288 | my @all_tokens; | ||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | 36 | my $in = 0; | |||||||||||||||||||||||||||
| 291 | 36 | ▶ | my $match_count = $global ? 99999 : 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 292 | 36 | my @matches = web_extract_tags($pattern); | |||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | 36 | foreach my $token (web_split_at_tags($text)) { | |||||||||||||||||||||||||||
| 295 | 1060 | ▶ | if (web_is_tag($token)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 296 | 502 | my $tag = web_parse_tag($token); | |||||||||||||||||||||||||||
| 297 | 502 | ▶ | if (web_same_tag($matches[$in], $tag)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 298 | 58 | ▶ | $in += 1 if $match_count; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 299 | 58 | ▶ | if ($in >= @matches) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 300 | 35 | push(@tokens, $token); | |||||||||||||||||||||||||||
| 301 | 35 | $token = $substituter->($output, @tokens); | |||||||||||||||||||||||||||
| 302 | 35 | $match_count -= 1; | |||||||||||||||||||||||||||
| 303 | 35 | @tokens = (); | |||||||||||||||||||||||||||
| 304 | 35 | $in = 0; | |||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | 1060 | ▶ | if ($in > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 310 | 46 | push(@tokens, $token); | |||||||||||||||||||||||||||
| 311 | } else { | ||||||||||||||||||||||||||||
| 312 | 1014 | push(@all_tokens, $token); | |||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | 36 | ▶ | push(@all_tokens, @tokens) if @tokens; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 317 | 36 | return join('', @all_tokens); | |||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 321 | # Divide html into sections based on contents of header tags | ||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | 3 | ▶ | sub web_titled_sections { | ||||||||||||||||||||||||||
|
Subroutine
web_titled_sections: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||||||||||||||||
| 324 | 3 | my ($pattern, $text, $titler) = @_; | |||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | 3 | my $title; | |||||||||||||||||||||||||||
| 327 | my @tokens; | ||||||||||||||||||||||||||||
| 328 | 3 | my %section; | |||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | 3 | my $in = 0; | |||||||||||||||||||||||||||
| 331 | 3 | my @matches = web_extract_tags($pattern); | |||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||
| 333 | 3 | foreach my $token (web_split_at_tags($text)) { | |||||||||||||||||||||||||||
| 334 | 1480 | ▶ | if (web_is_tag($token)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 335 | 772 | my $tag = web_parse_tag($token); | |||||||||||||||||||||||||||
| 336 | 772 | ▶ | if (web_same_tag($matches[$in], $tag)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 337 | 56 | $in += 1; | |||||||||||||||||||||||||||
| 338 | 56 | ▶ | if ($in >= @matches) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 339 | 28 | push(@tokens, $token); | |||||||||||||||||||||||||||
| 340 | 28 | $title = $titler->(@tokens); | |||||||||||||||||||||||||||
| 341 | 28 | @tokens = (); | |||||||||||||||||||||||||||
| 342 | 28 | undef $token; | |||||||||||||||||||||||||||
| 343 | 28 | $in = 0; | |||||||||||||||||||||||||||
| 344 | } | ||||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||
| 346 | } | ||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | 1480 | ▶ | if ($in > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 349 | 56 | push(@tokens, $token); | |||||||||||||||||||||||||||
| 350 | 56 | undef $title; | |||||||||||||||||||||||||||
| 351 | 56 | undef $token; | |||||||||||||||||||||||||||
| 352 | } | ||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | 1480 | ▶ | if (defined $token && defined $title) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $token and defined $title
MC/DC50%1/2
defined $token and defined $title
defined $token
defined $title
Truth table66%2/3
A and B
A= defined $tokenB= defined $title
| |||||||||||||||||||||||||||||
| 355 | 1396 | ▶ | $section{$title} = [] unless exists $section{$title}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 356 | 1396 | push(@{$section{$title}}, $token); | |||||||||||||||||||||||||||
| 357 | } | ||||||||||||||||||||||||||||
| 358 | } | ||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | 3 | foreach my $title (keys %section) { | |||||||||||||||||||||||||||
| 361 | 28 | $section{$title} = join('', @{$section{$title}}); | |||||||||||||||||||||||||||
| 362 | 28 | $section{$title} =~ s/^\s+//; | |||||||||||||||||||||||||||
| 363 | } | ||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | 3 | return \%section; | |||||||||||||||||||||||||||
| 366 | } | ||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | 21 | 1; | |||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | =pod | ||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | =encoding utf-8 | ||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | =head1 NAME | ||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | App::Followme::Web - Functions to parse html | ||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | use App::Followme::Web; | ||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||
| 384 | This module contains the subroutines followme uses to parse html. The code | ||||||||||||||||||||||||||||
| 385 | is placed in a separate module because it is used by more than one other | ||||||||||||||||||||||||||||
| 386 | module. | ||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | =head1 SUBROUTINES | ||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | =over 4 | ||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||
| 392 | =item $flag = web_has_variables($text, @search_variables); | ||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||
| 394 | Return true if any of a list of variables is present in a template. The | ||||||||||||||||||||||||||||
| 395 | variable names must include the sigil. | ||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | =item $match_count = web_match_tags($pattern, $text, $matcher, | ||||||||||||||||||||||||||||
| 398 | $metadata, $global); | ||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | Match a tag pattern ($pattern) in a text ($text), pass the matched text to a | ||||||||||||||||||||||||||||
| 401 | function ($matcher), which processes it and places it in a hash ($metadata). | ||||||||||||||||||||||||||||
| 402 | Repeat this process for the entire text if the flag ($global) is set. | ||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||
| 404 | =item $section = web_parse_sections($text); | ||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||
| 406 | Place the text inside section tags into a hash indexed by the section names. | ||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||
| 408 | =item $parsed_tag = web_parse_tag($tag); | ||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||
| 410 | Parse a single html tag into a hash indexed by attribute name. | ||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||
| 412 | =item $tags = web_only_tags(@tokens); | ||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | Extract the tags from a text that has been split into tokens. | ||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | =item $text = web_only_text(@tokens); | ||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | Extract the text from a text that has been split into tokens. | ||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||
| 420 | =item $text = web_substitute_sections($text, $section); | ||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||
| 422 | Replace sections in a text by sections of the same name stored in a hash. | ||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||
| 424 | =item $text = web_substitute_tags($pattern, $text, $substituter, | ||||||||||||||||||||||||||||
| 425 | $output, $global); | ||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||
| 427 | Match a tag pattern ($pattern) in a text ($text), pass the matched text to a | ||||||||||||||||||||||||||||
| 428 | function ($substituter), which processes it and places it in a hash ($output) as | ||||||||||||||||||||||||||||
| 429 | well as replaces the matched text. Repeat this process for the entire text if | ||||||||||||||||||||||||||||
| 430 | the flag ($global) is set. Return the text with the substitutions. | ||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | =item $sections = web_titled_sections($pattern, $text, $titler); | ||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||
| 434 | Return a hash of sections from html, where the name of each section is derived | ||||||||||||||||||||||||||||
| 435 | from the header tags that precede it. The title is built by calling the | ||||||||||||||||||||||||||||
| 436 | subroutine passed in as $titler. It is passed the set of tags matched by | ||||||||||||||||||||||||||||
| 437 | $pattern. A hash of sections that preceded by a matching set of header tags is | ||||||||||||||||||||||||||||
| 438 | returned. | ||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||
| 440 | =back | ||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||
| 442 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||
| 444 | Copyright (C) Bernie Simon. | ||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||
| 446 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 447 | it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||
| 449 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||
| 451 | Bernie Simon E<lt>bernie.simon@gmail.comE<gt> | ||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||
| 453 | =cut | ||||||||||||||||||||||||||||