| 1 | #!/usr/local/bin/perl | ||||||||||||||||||||||||||||
| 2 | # Copyright 2022 Jeffrey Kegler | ||||||||||||||||||||||||||||
| 3 | # This file is part of Marpa::R2. Marpa::R2 is free software: you can | ||||||||||||||||||||||||||||
| 4 | # redistribute it and/or modify it under the terms of the GNU Lesser | ||||||||||||||||||||||||||||
| 5 | # General Public License as published by the Free Software Foundation, | ||||||||||||||||||||||||||||
| 6 | # either version 3 of the License, or (at your option) any later version. | ||||||||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||||||||
| 8 | # Marpa::R2 is distributed in the hope that it will be useful, | ||||||||||||||||||||||||||||
| 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||||||||||||||||||||
| 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||||||||||||||||||||||||||
| 11 | # Lesser General Public License for more details. | ||||||||||||||||||||||||||||
| 12 | # | ||||||||||||||||||||||||||||
| 13 | # You should have received a copy of the GNU Lesser | ||||||||||||||||||||||||||||
| 14 | # General Public License along with Marpa::R2. If not, see | ||||||||||||||||||||||||||||
| 15 | # http://www.gnu.org/licenses/. | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 2 | ▶ | use 5.010001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 2 | ▶ | use English qw( -no_match_vars ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 2 | ▶ | use Marpa::R2::HTML; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 2 | ▶ | use List::Util qw(sum); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 2 | ▶ | use Fatal qw(open close); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 2 | ▶ | use Getopt::Long; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | sub usage { | ||||||||||||||||||||||||||||
| 27 | 0 | ▶ | say {*STDERR} "$PROGRAM_NAME html_score [uri|file]" or die "say failed: $ERRNO"; | ||||||||||||||||||||||||||
|
Subroutine
usage: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | 0 | exit 1; | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 2 | my $html_flag = 0; | |||||||||||||||||||||||||||
| 32 | 2 | my $help_flag = 0; | |||||||||||||||||||||||||||
| 33 | 2 | my $dump_config_flag = 0; | |||||||||||||||||||||||||||
| 34 | 2 | my $dump_AHFA_flag = 0; | |||||||||||||||||||||||||||
| 35 | 2 | my $trace_terminals_flag = 0; | |||||||||||||||||||||||||||
| 36 | 2 | my $trace_cruft_flag = 0; | |||||||||||||||||||||||||||
| 37 | 2 | my $trace_values_flag = 0; | |||||||||||||||||||||||||||
| 38 | 2 | my $compile_flag; | |||||||||||||||||||||||||||
| 39 | 2 | ▶ | usage() unless GetOptions( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 'html' => \$html_flag, | ||||||||||||||||||||||||||||
| 41 | 'help' => \$help_flag, | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | # undocumented | ||||||||||||||||||||||||||||
| 44 | 'dump-config' => \$dump_config_flag, | ||||||||||||||||||||||||||||
| 45 | 'dump-AHFA' => \$dump_AHFA_flag, | ||||||||||||||||||||||||||||
| 46 | 'compile=s' => \$compile_flag, | ||||||||||||||||||||||||||||
| 47 | 'trace-terminals' => \$trace_terminals_flag, | ||||||||||||||||||||||||||||
| 48 | 'trace-cruft' => \$trace_cruft_flag, | ||||||||||||||||||||||||||||
| 49 | 'trace-values' => \$trace_values_flag, | ||||||||||||||||||||||||||||
| 50 | ); | ||||||||||||||||||||||||||||
| 51 | 2 | ▶ | usage() unless $help_flag or 1 >= scalar @ARGV; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$help_flag or 1 >= scalar @ARGV
MC/DC0%0/2
$help_flag or 1 >= scalar @ARGV
$help_flag
1 >= scalar @ARGV
Truth table33%1/3
A or B
A= $help_flagB= 1 >= scalar @ARGV
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 2 | my $locator = shift; | |||||||||||||||||||||||||||
| 54 | 2 | my $document; | |||||||||||||||||||||||||||
| 55 | GET_DOCUMENT: { | ||||||||||||||||||||||||||||
| 56 | 2 | ▶ | if ( not defined $locator ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 0 | $locator = 'STDIN'; # for possible display, later | |||||||||||||||||||||||||||
| 58 | 0 | local $RS = undef; | |||||||||||||||||||||||||||
| 59 | ## no critic(InputOutput::ProhibitExplicitStdin) | ||||||||||||||||||||||||||||
| 60 | 0 | $document = <STDIN>; | |||||||||||||||||||||||||||
| 61 | 0 | last GET_DOCUMENT; | |||||||||||||||||||||||||||
| 62 | } ## end if ( not defined $locator ) | ||||||||||||||||||||||||||||
| 63 | 2 | ▶ | if ( $locator =~ /\A [[:alnum:]]+ [:] /xms ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 0 | require WWW::Mechanize; | |||||||||||||||||||||||||||
| 65 | 0 | my $mech = WWW::Mechanize->new( autocheck => 1 ); | |||||||||||||||||||||||||||
| 66 | 0 | $mech->get($locator); | |||||||||||||||||||||||||||
| 67 | 0 | $document = $mech->content; | |||||||||||||||||||||||||||
| 68 | 0 | undef $mech; | |||||||||||||||||||||||||||
| 69 | 0 | last GET_DOCUMENT; | |||||||||||||||||||||||||||
| 70 | } ## end if ( $locator =~ /\A [[:alnum:]]+ [:] /xms ) | ||||||||||||||||||||||||||||
| 71 | { | ||||||||||||||||||||||||||||
| 72 | 2 | local $RS = undef; | |||||||||||||||||||||||||||
| 73 | 2 | open my $fh, q{<}, $locator; | |||||||||||||||||||||||||||
| 74 | 2 | $document = <$fh>; | |||||||||||||||||||||||||||
| 75 | 2 | close $fh; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | } ## end GET_DOCUMENT: | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | sub calculate_max_depths { | ||||||||||||||||||||||||||||
| 80 | 29 | ▶ | my ($descendant_data) = @_; | ||||||||||||||||||||||||||
|
Subroutine
calculate_max_depths: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 81 | 29 | my %return_depths = ( ANY => 0 ); | |||||||||||||||||||||||||||
| 82 | 29 | for my $child_value ( grep { ref $_ } map { $_->[0] } @{$descendant_data} ) { | |||||||||||||||||||||||||||
| 83 | 34 | my $depths = $child_value->{depths}; | |||||||||||||||||||||||||||
| 84 | 34 | CHILD_TAGNAME: for my $child_tagname ( keys %{$depths} ) { | |||||||||||||||||||||||||||
| 85 | 103 | my $depth = $depths->{$child_tagname}; | |||||||||||||||||||||||||||
| 86 | 103 | ▶ | if ( $depth > ( $return_depths{$child_tagname} // 0 ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$return_depths{$child_tagname} // 0
MC/DC100%1/1
$return_depths{$child_tagname} // 0
$return_depths{$child_tagname}
Truth table100%2/2
A
A= $return_depths{$child_tagname}
| |||||||||||||||||||||||||||||
| 87 | 79 | $return_depths{$child_tagname} = $depth; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | 103 | ▶ | if ( $depth > $return_depths{ANY} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 7 | $return_depths{ANY} = $depth; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | } ## end for my $child_tagname ( keys %{$depths} ) | ||||||||||||||||||||||||||||
| 93 | } ## end for my $child_value ( grep { ref $_ } map { $_->[0] }...) | ||||||||||||||||||||||||||||
| 94 | 29 | return \%return_depths; | |||||||||||||||||||||||||||
| 95 | } ## end sub calculate_max_depths | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | sub calculate_length { | ||||||||||||||||||||||||||||
| 98 | 34 | ▶ | my ($descendant_data) = @_; | ||||||||||||||||||||||||||
|
Subroutine
calculate_length: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 99 | 34 | my $length = 0; | |||||||||||||||||||||||||||
| 100 | 34 | for my $descendant_data ( @{$descendant_data} ) { | |||||||||||||||||||||||||||
| 101 | 81 | my ( $value, $literal ) = @{$descendant_data}; | |||||||||||||||||||||||||||
| 102 | 81 | my $this_length; | |||||||||||||||||||||||||||
| 103 | 81 | ▶ | if (defined $value) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 34 | $this_length = $value->{length}; | |||||||||||||||||||||||||||
| 105 | } else { | ||||||||||||||||||||||||||||
| 106 | 47 | (my $no_whitespace_literal = $literal) =~ s/\s//xmsg; | |||||||||||||||||||||||||||
| 107 | 47 | $this_length = length $no_whitespace_literal; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | 81 | $length += $this_length; | |||||||||||||||||||||||||||
| 110 | } ## end for my $descendant_data ( @{$descendant_data} ) | ||||||||||||||||||||||||||||
| 111 | 34 | return $length; | |||||||||||||||||||||||||||
| 112 | } ## end sub calculate_length | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 2 | my %flags = ( | |||||||||||||||||||||||||||
| 115 | trace_terminals => $trace_terminals_flag, | ||||||||||||||||||||||||||||
| 116 | trace_cruft => $trace_cruft_flag, | ||||||||||||||||||||||||||||
| 117 | trace_values => $trace_values_flag, | ||||||||||||||||||||||||||||
| 118 | dump_config => $dump_config_flag, | ||||||||||||||||||||||||||||
| 119 | dump_AHFA => $dump_AHFA_flag, | ||||||||||||||||||||||||||||
| 120 | ); | ||||||||||||||||||||||||||||
| 121 | 2 | ▶ | if (defined $compile_flag) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 0 | open my $fh, q{<}, $compile_flag; | |||||||||||||||||||||||||||
| 123 | 0 | my $source = join q{}, <$fh>; | |||||||||||||||||||||||||||
| 124 | 0 | close $fh; | |||||||||||||||||||||||||||
| 125 | 0 | $flags{compile} = \$source; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | my ( $instance, $value ) = @{ | ||||||||||||||||||||||||||||
| 129 | 2 | Marpa::R2::HTML::html( | |||||||||||||||||||||||||||
| 130 | \$document, | ||||||||||||||||||||||||||||
| 131 | 2 | ▶ | { ':COMMENT' => sub { return { depths => {}, length => 0 } }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 132 | q{*} => sub { | ||||||||||||||||||||||||||||
| 133 | 27 | ▶ | my $descendant_data = | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 134 | Marpa::R2::HTML::descendants('value,literal'); | ||||||||||||||||||||||||||||
| 135 | 27 | my $tagname = Marpa::R2::HTML::tagname(); | |||||||||||||||||||||||||||
| 136 | 27 | my $length = calculate_length($descendant_data); | |||||||||||||||||||||||||||
| 137 | 27 | $Marpa::R2::HTML::INSTANCE->{count}->{$tagname}++; | |||||||||||||||||||||||||||
| 138 | 27 | $Marpa::R2::HTML::INSTANCE->{length}->{$tagname} += $length; | |||||||||||||||||||||||||||
| 139 | 27 | my $return_depths = calculate_max_depths($descendant_data); | |||||||||||||||||||||||||||
| 140 | 27 | ▶ | ( $return_depths->{$tagname} //= 0 )++; | ||||||||||||||||||||||||||
|
Condition50%1/2
$return_depths->{$tagname} //= 0
MC/DC0%0/1
$return_depths->{$tagname} //= 0
$return_depths->{$tagname}
Truth table50%1/2
A
A= $return_depths->{$tagname}
| |||||||||||||||||||||||||||||
| 141 | 27 | $return_depths->{ANY}++; | |||||||||||||||||||||||||||
| 142 | return { | ||||||||||||||||||||||||||||
| 143 | 27 | depths => $return_depths, | |||||||||||||||||||||||||||
| 144 | length => $length, | ||||||||||||||||||||||||||||
| 145 | }; | ||||||||||||||||||||||||||||
| 146 | }, | ||||||||||||||||||||||||||||
| 147 | ':CRUFT' => sub { | ||||||||||||||||||||||||||||
| 148 | 5 | ▶ | my $descendant_data = | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 149 | Marpa::R2::HTML::descendants('value,literal'); | ||||||||||||||||||||||||||||
| 150 | 5 | my $return_depths = { '[CRUFT]' => 1 }; | |||||||||||||||||||||||||||
| 151 | 5 | my $length = calculate_length($descendant_data); | |||||||||||||||||||||||||||
| 152 | 5 | $Marpa::R2::HTML::INSTANCE->{count}->{'[CRUFT]'}++; | |||||||||||||||||||||||||||
| 153 | 5 | $Marpa::R2::HTML::INSTANCE->{length}->{'[CRUFT]'} += $length; | |||||||||||||||||||||||||||
| 154 | return { | ||||||||||||||||||||||||||||
| 155 | 5 | depths => $return_depths, | |||||||||||||||||||||||||||
| 156 | length => $length, | ||||||||||||||||||||||||||||
| 157 | }; | ||||||||||||||||||||||||||||
| 158 | }, | ||||||||||||||||||||||||||||
| 159 | ':TOP' => sub { | ||||||||||||||||||||||||||||
| 160 | 2 | ▶ | my $descendant_data = | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 161 | Marpa::R2::HTML::descendants('value,literal'); | ||||||||||||||||||||||||||||
| 162 | return [ | ||||||||||||||||||||||||||||
| 163 | 2 | $Marpa::R2::HTML::INSTANCE, | |||||||||||||||||||||||||||
| 164 | { depths => calculate_max_depths($descendant_data), | ||||||||||||||||||||||||||||
| 165 | length => calculate_length($descendant_data), | ||||||||||||||||||||||||||||
| 166 | }, | ||||||||||||||||||||||||||||
| 167 | ]; | ||||||||||||||||||||||||||||
| 168 | }, | ||||||||||||||||||||||||||||
| 169 | }, | ||||||||||||||||||||||||||||
| 170 | 2 | \\%flags | |||||||||||||||||||||||||||
| 171 | ), | ||||||||||||||||||||||||||||
| 172 | }; | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 2 | my $length_by_element = $instance->{length}; | |||||||||||||||||||||||||||
| 175 | 2 | my $count_by_element = $instance->{count}; | |||||||||||||||||||||||||||
| 176 | 2 | my $html_length = $length_by_element->{html}; | |||||||||||||||||||||||||||
| 177 | 2 | my $total_lengths = List::Util::sum values %{ $length_by_element }; | |||||||||||||||||||||||||||
| 178 | 2 | my $complexity = 0; | |||||||||||||||||||||||||||
| 179 | 2 | ▶ | if ( $html_length >= 1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | 2 | $complexity = sprintf "%.3f", | |||||||||||||||||||||||||||
| 181 | ( $total_lengths / ( $html_length * log($html_length) ) ); | ||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | 2 | my $max_depths = $value->{depths}; | |||||||||||||||||||||||||||
| 184 | 2 | my $max_element_depth = $max_depths->{ANY}; | |||||||||||||||||||||||||||
| 185 | 2 | delete $max_depths->{ANY}; | |||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 2 | ▶ | if ($html_flag) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | 0 | print qq{<table cellpadding="3" border="1">} | |||||||||||||||||||||||||||
| 189 | . qq{<thead>\n} | ||||||||||||||||||||||||||||
| 190 | . qq{<tr><th colspan="5">$locator</tr>\n} | ||||||||||||||||||||||||||||
| 191 | . qq{<tr><th colspan="5">Complexity Score = $complexity</tr>\n} | ||||||||||||||||||||||||||||
| 192 | . qq{<tr><th colspan="5">Maximum Depth = $max_element_depth</tr>\n} | ||||||||||||||||||||||||||||
| 193 | . qq{<tr>} | ||||||||||||||||||||||||||||
| 194 | . qq{<th>Element} | ||||||||||||||||||||||||||||
| 195 | . qq{<th>Maximum<br>Nesting} | ||||||||||||||||||||||||||||
| 196 | . qq{<th>Number of<br>Elements} | ||||||||||||||||||||||||||||
| 197 | . qq{<th>Size in<br>Characters</th>} | ||||||||||||||||||||||||||||
| 198 | . qq{<th>Average<br>Size</th>} | ||||||||||||||||||||||||||||
| 199 | . qq{</tr>\n} | ||||||||||||||||||||||||||||
| 200 | . qq{</thead>\n}; | ||||||||||||||||||||||||||||
| 201 | } else { | ||||||||||||||||||||||||||||
| 202 | 2 | say $locator; | |||||||||||||||||||||||||||
| 203 | 2 | say "Complexity Score = ", $complexity; | |||||||||||||||||||||||||||
| 204 | 2 | say "Maximum Depth = ", $max_element_depth; | |||||||||||||||||||||||||||
| 205 | 2 | printf "%11s%11s%11s%11s%11s\n", q{}, 'Maximum ', 'Number of', 'Size in ', 'Average'; | |||||||||||||||||||||||||||
| 206 | 2 | printf "%11s%11s%11s%11s%11s\n", 'Element ', 'Nesting ', 'Elements ', 'Characters', 'Size '; | |||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | 2 | for my $element ( sort keys %{$max_depths} ) { | |||||||||||||||||||||||||||
| 210 | 20 | my $count = $count_by_element->{$element}; | |||||||||||||||||||||||||||
| 211 | 20 | my $size = $length_by_element->{$element}; | |||||||||||||||||||||||||||
| 212 | 20 | ▶ | my $average = $count ? int( $size / $count ) : q{-}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 213 | 20 | ▶ | if ($html_flag) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 214 | print join q{}, | ||||||||||||||||||||||||||||
| 215 | q{<tr>}, | ||||||||||||||||||||||||||||
| 216 | qq{<td>$element</td>}, | ||||||||||||||||||||||||||||
| 217 | 0 | q{<td align="right">}, $max_depths->{$element}, q{</td>}, | |||||||||||||||||||||||||||
| 218 | qq{<td align="right">$count</td>}, | ||||||||||||||||||||||||||||
| 219 | qq{<td align="right">$size</td>}, | ||||||||||||||||||||||||||||
| 220 | qq{<td align="right">$average</td>}, | ||||||||||||||||||||||||||||
| 221 | "</tr>\n"; | ||||||||||||||||||||||||||||
| 222 | } else { | ||||||||||||||||||||||||||||
| 223 | 20 | printf "%-11s%11d%11d%11d%11d\n", $element, $max_depths->{$element}, $count, $size, $average; | |||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||
| 225 | } ## end for my $element ( sort keys %{$max_depths} ) | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | 2 | ▶ | $html_flag and print qq{</table>\n}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | 2 | exit 0; | |||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | # vim: set expandtab shiftwidth=4: | ||||||||||||||||||||||||||||