Debian Codegraph @

html/script/marpa_r2_html_score

raw
1 #!/usr/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 use 5.010001;
18 use strict;
19 use warnings;
20 use English qw( -no_match_vars );
21 use Marpa::R2::HTML;
22 use List::Util qw(sum);
23 use Fatal qw(open close);
24 use Getopt::Long;
25
26 sub usage {
27 say {*STDERR} "$PROGRAM_NAME html_score [uri|file]" or die "say failed: $ERRNO";
28 exit 1;
29 }
30
31 my $html_flag = 0;
32 my $help_flag = 0;
33 my $dump_config_flag = 0;
34 my $dump_AHFA_flag = 0;
35 my $trace_terminals_flag = 0;
36 my $trace_cruft_flag = 0;
37 my $trace_values_flag = 0;
38 my $compile_flag;
39 usage() unless GetOptions(
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 usage() unless $help_flag or 1 >= scalar @ARGV;
52
53 my $locator = shift;
54 my $document;
55 GET_DOCUMENT: {
56 if ( not defined $locator ) {
57 $locator = 'STDIN'; # for possible display, later
58 local $RS = undef;
59 ## no critic(InputOutput::ProhibitExplicitStdin)
60 $document = <STDIN>;
61 last GET_DOCUMENT;
62 } ## end if ( not defined $locator )
63 if ( $locator =~ /\A [[:alnum:]]+ [:] /xms ) {
64 require WWW::Mechanize;
65 my $mech = WWW::Mechanize->new( autocheck => 1 );
66 $mech->get($locator);
67 $document = $mech->content;
68 undef $mech;
69 last GET_DOCUMENT;
70 } ## end if ( $locator =~ /\A [[:alnum:]]+ [:] /xms )
71 {
72 local $RS = undef;
73 open my $fh, q{<}, $locator;
74 $document = <$fh>;
75 close $fh;
76 }
77 } ## end GET_DOCUMENT:
78
79 sub calculate_max_depths {
80 my ($descendant_data) = @_;
81 my %return_depths = ( ANY => 0 );
82 for my $child_value ( grep { ref $_ } map { $_->[0] } @{$descendant_data} ) {
83 my $depths = $child_value->{depths};
84 CHILD_TAGNAME: for my $child_tagname ( keys %{$depths} ) {
85 my $depth = $depths->{$child_tagname};
86 if ( $depth > ( $return_depths{$child_tagname} // 0 ) ) {
87 $return_depths{$child_tagname} = $depth;
88 }
89 if ( $depth > $return_depths{ANY} ) {
90 $return_depths{ANY} = $depth;
91 }
92 } ## end for my $child_tagname ( keys %{$depths} )
93 } ## end for my $child_value ( grep { ref $_ } map { $_->[0] }...)
94 return \%return_depths;
95 } ## end sub calculate_max_depths
96
97 sub calculate_length {
98 my ($descendant_data) = @_;
99 my $length = 0;
100 for my $descendant_data ( @{$descendant_data} ) {
101 my ( $value, $literal ) = @{$descendant_data};
102 my $this_length;
103 if (defined $value) {
104 $this_length = $value->{length};
105 } else {
106 (my $no_whitespace_literal = $literal) =~ s/\s//xmsg;
107 $this_length = length $no_whitespace_literal;
108 }
109 $length += $this_length;
110 } ## end for my $descendant_data ( @{$descendant_data} )
111 return $length;
112 } ## end sub calculate_length
113
114 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 if (defined $compile_flag) {
122 open my $fh, q{<}, $compile_flag;
123 my $source = join q{}, <$fh>;
124 close $fh;
125 $flags{compile} = \$source;
126 }
127
128 my ( $instance, $value ) = @{
129 Marpa::R2::HTML::html(
130 \$document,
131 { ':COMMENT' => sub { return { depths => {}, length => 0 } },
132 q{*} => sub {
133 my $descendant_data =
134 Marpa::R2::HTML::descendants('value,literal');
135 my $tagname = Marpa::R2::HTML::tagname();
136 my $length = calculate_length($descendant_data);
137 $Marpa::R2::HTML::INSTANCE->{count}->{$tagname}++;
138 $Marpa::R2::HTML::INSTANCE->{length}->{$tagname} += $length;
139 my $return_depths = calculate_max_depths($descendant_data);
140 ( $return_depths->{$tagname} //= 0 )++;
141 $return_depths->{ANY}++;
142 return {
143 depths => $return_depths,
144 length => $length,
145 };
146 },
147 ':CRUFT' => sub {
148 my $descendant_data =
149 Marpa::R2::HTML::descendants('value,literal');
150 my $return_depths = { '[CRUFT]' => 1 };
151 my $length = calculate_length($descendant_data);
152 $Marpa::R2::HTML::INSTANCE->{count}->{'[CRUFT]'}++;
153 $Marpa::R2::HTML::INSTANCE->{length}->{'[CRUFT]'} += $length;
154 return {
155 depths => $return_depths,
156 length => $length,
157 };
158 },
159 ':TOP' => sub {
160 my $descendant_data =
161 Marpa::R2::HTML::descendants('value,literal');
162 return [
163 $Marpa::R2::HTML::INSTANCE,
164 { depths => calculate_max_depths($descendant_data),
165 length => calculate_length($descendant_data),
166 },
167 ];
168 },
169 },
170 \\%flags
171 ),
172 };
173
174 my $length_by_element = $instance->{length};
175 my $count_by_element = $instance->{count};
176 my $html_length = $length_by_element->{html};
177 my $total_lengths = List::Util::sum values %{ $length_by_element };
178 my $complexity = 0;
179 if ( $html_length >= 1 ) {
180 $complexity = sprintf "%.3f",
181 ( $total_lengths / ( $html_length * log($html_length) ) );
182 }
183 my $max_depths = $value->{depths};
184 my $max_element_depth = $max_depths->{ANY};
185 delete $max_depths->{ANY};
186
187 if ($html_flag) {
188 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 say $locator;
203 say "Complexity Score = ", $complexity;
204 say "Maximum Depth = ", $max_element_depth;
205 printf "%11s%11s%11s%11s%11s\n", q{}, 'Maximum ', 'Number of', 'Size in ', 'Average';
206 printf "%11s%11s%11s%11s%11s\n", 'Element ', 'Nesting ', 'Elements ', 'Characters', 'Size ';
207 }
208
209 for my $element ( sort keys %{$max_depths} ) {
210 my $count = $count_by_element->{$element};
211 my $size = $length_by_element->{$element};
212 my $average = $count ? int( $size / $count ) : q{-};
213 if ($html_flag) {
214 print join q{},
215 q{<tr>},
216 qq{<td>$element</td>},
217 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 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 $html_flag and print qq{</table>\n};
228
229 exit 0;
230
231 # vim: set expandtab shiftwidth=4: