| 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 |
=head1 Name |
| 18 |
|
| 19 |
C<html_score> - Show complexity metric and other stats for web page |
| 20 |
|
| 21 |
=head1 Synopsis |
| 22 |
|
| 23 |
=for Marpa::R2::Display |
| 24 |
ignore: 1 |
| 25 |
|
| 26 |
html_score [--html] [uri|file] |
| 27 |
|
| 28 |
=for Marpa::R2::Display::End |
| 29 |
|
| 30 |
=head1 Examples |
| 31 |
|
| 32 |
=for Marpa::R2::Display |
| 33 |
ignore: 1 |
| 34 |
|
| 35 |
html_score http://perl.org |
| 36 |
|
| 37 |
html_score --html http://perl6.org |
| 38 |
|
| 39 |
=for Marpa::R2::Display::End |
| 40 |
|
| 41 |
=head1 Description |
| 42 |
|
| 43 |
Given a URI or a file name, |
| 44 |
treats its referent as HTML |
| 45 |
and prints a complexity metric, |
| 46 |
the maximum element depth, and per-element statistics. |
| 47 |
The per-element statistics appear in rows, one per tag name. |
| 48 |
For each tag name, its row contains: |
| 49 |
|
| 50 |
=over 4 |
| 51 |
|
| 52 |
=item * |
| 53 |
The maximum nesting depth of elements with |
| 54 |
that tag name. |
| 55 |
This is per-tag-name nesting depth, |
| 56 |
and does not take |
| 57 |
into account nesting within other elements with |
| 58 |
other tag names. |
| 59 |
|
| 60 |
=item * |
| 61 |
A count of the elements with that tag name in the document. |
| 62 |
|
| 63 |
=item * |
| 64 |
The total number of characters in elements with that tag name. |
| 65 |
Characters in nested elements are counted multiple times. |
| 66 |
For example, if a page contains a table within a table, |
| 67 |
characters in the inner table will be counted twice. |
| 68 |
|
| 69 |
=item * |
| 70 |
The average size of elements with this tag name, in characters. |
| 71 |
|
| 72 |
=back |
| 73 |
|
| 74 |
The argument to L<html_score> can be either a URI or a file |
| 75 |
name. If it starts with alphanumerics followed by a colon, it is treated |
| 76 |
as a URI. Otherwise it is treated as file name. |
| 77 |
If the C<--html> option is specified, the output is written |
| 78 |
as an HTML table. |
| 79 |
|
| 80 |
The complexity metric is the average depth (or nesting level), in elements, of a character, |
| 81 |
divided by the logarithm of the length of the HTML. |
| 82 |
Whitespace and comments are ignored in calculating the complexity metric. |
| 83 |
The division by the logarithm of the HTML length is based on the idea that, |
| 84 |
all else being equal, |
| 85 |
it is reasonable for the nesting to increase logarithmically as |
| 86 |
a web page grows in length. |
| 87 |
|
| 88 |
=head1 Sample output |
| 89 |
|
| 90 |
Here is the first part of the output for C<http://perl.org>. |
| 91 |
|
| 92 |
=for Marpa::R2::Display |
| 93 |
ignore: 1 |
| 94 |
|
| 95 |
http://perl.org |
| 96 |
Complexity Score = 0.873 |
| 97 |
Maximum Depth = 12 |
| 98 |
Maximum Number of Size in Average |
| 99 |
Element Nesting Elements Characters Size |
| 100 |
a 1 56 3533 63 |
| 101 |
body 1 1 7615 7615 |
| 102 |
div 5 30 24695 823 |
| 103 |
em 1 1 13 13 |
| 104 |
h1 1 1 37 37 |
| 105 |
h4 1 11 559 50 |
| 106 |
|
| 107 |
=for Marpa::R2::Display::End |
| 108 |
|
| 109 |
=head1 Interpreting the complexity metric |
| 110 |
|
| 111 |
With caution, |
| 112 |
the complexity metric can be used as a self-assessment |
| 113 |
of website quality. |
| 114 |
Well designed websites often have low numbers, |
| 115 |
particularly if fast loading is an important goal. |
| 116 |
But high values of the complexity metric do not necessarily mean low quality. |
| 117 |
Everything depends on what the mission is, and how well |
| 118 |
complexity is being used to serve the site's mission. |
| 119 |
|
| 120 |
=head1 Purpose |
| 121 |
|
| 122 |
This program is a demo of a demo. |
| 123 |
It purpose is to show how easy it is to write applications which look |
| 124 |
at the structure of web pages using L<Marpa::R2::HTML>. |
| 125 |
And the purpose of L<Marpa::R2::HTML> |
| 126 |
is to demonstrate the power of its parse engine, |
| 127 |
L<Marpa::R2>. |
| 128 |
L<Marpa::R2::HTML> was written in a few days, |
| 129 |
and its logic |
| 130 |
is a straightforward, |
| 131 |
natural expression of the structure of HTML. |
| 132 |
|
| 133 |
=head1 Acknowledgements |
| 134 |
|
| 135 |
The starting template for this code was |
| 136 |
L<HTML::TokeParser>, by Gisle Aas. |
| 137 |
|
| 138 |
=head1 Copyright and License |
| 139 |
|
| 140 |
=for Marpa::R2::Display |
| 141 |
ignore: 1 |
| 142 |
|
| 143 |
Copyright 2022 Jeffrey Kegler |
| 144 |
This file is part of Marpa::R2. Marpa::R2 is free software: you can |
| 145 |
redistribute it and/or modify it under the terms of the GNU Lesser |
| 146 |
General Public License as published by the Free Software Foundation, |
| 147 |
either version 3 of the License, or (at your option) any later version. |
| 148 |
|
| 149 |
Marpa::R2 is distributed in the hope that it will be useful, |
| 150 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 151 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 152 |
Lesser General Public License for more details. |
| 153 |
|
| 154 |
You should have received a copy of the GNU Lesser |
| 155 |
General Public License along with Marpa::R2. If not, see |
| 156 |
http://www.gnu.org/licenses/. |
| 157 |
|
| 158 |
=for Marpa::R2::Display::End |
| 159 |
|
| 160 |
=cut |