| 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 |
html_fmt - Reformat HTML, indented according to structure |
| 20 |
|
| 21 |
=head1 Synopsis |
| 22 |
|
| 23 |
=for Marpa::R2::Display |
| 24 |
ignore: 1 |
| 25 |
|
| 26 |
html_fmt [--avoid-whitespace=[yes|comment|no]] |
| 27 |
[--no-ws-ok-before-end-tag|--no-ws-ok-before-end-tag] |
| 28 |
[--no-ws-ok-after-start-tag|--no-ws-ok-after-start-tag] |
| 29 |
[--added-tag-comment|--no-added-tag-comment] |
| 30 |
[uri|file] |
| 31 |
|
| 32 |
=for Marpa::R2::Display::End |
| 33 |
|
| 34 |
=head1 Description |
| 35 |
|
| 36 |
Writes its input to C<STDOUT>, |
| 37 |
reformatted and |
| 38 |
indented according to the HTML structure. |
| 39 |
With no arguments, |
| 40 |
C<html_fmt> looks for its input |
| 41 |
on C<STDIN>. |
| 42 |
If it has an argument that looks like a URI, |
| 43 |
C<html_fmt> treats that argument |
| 44 |
as a URI. |
| 45 |
If it has an argument that does not |
| 46 |
look like a URI, |
| 47 |
C<html_fmt> uses that argument |
| 48 |
as the name |
| 49 |
of its input file. |
| 50 |
C<html_fmt> considers its argument to |
| 51 |
"look like a URI", |
| 52 |
if it |
| 53 |
starts with alphanumerics followed by a colon. |
| 54 |
|
| 55 |
When reformatting and indenting, |
| 56 |
C<html_fmt> takes measures |
| 57 |
to avoid introducing |
| 58 |
whitespace that will affect the way in which the HTML |
| 59 |
displays. |
| 60 |
The standards differ on whitespace treatment; |
| 61 |
when the standards agree |
| 62 |
they often allow considerable |
| 63 |
latitude to the browsers; |
| 64 |
and specific implementions will |
| 65 |
exploit the allowed latitude in different ways, |
| 66 |
or exceed it. |
| 67 |
All of which is to say that |
| 68 |
C<html_fmt>'s attempts to avoid introducing |
| 69 |
whitespace are not always successful. |
| 70 |
|
| 71 |
C<html_fmt> supplies missing start and end tags. |
| 72 |
If C<html_fmt> cannot find required start and end tags, |
| 73 |
it supplies them. |
| 74 |
C<html_fmt>'s grammar is very liberal in its interpretation |
| 75 |
of what is valid HTML. |
| 76 |
Finally, if this is not sufficient to turn the input |
| 77 |
into valid HTML, |
| 78 |
C<html_fmt> |
| 79 |
will treat problem sections of the input |
| 80 |
as noise or "cruft", |
| 81 |
ignoring them in determining |
| 82 |
the structure of the document. |
| 83 |
C<html_fmt> never rejects its input, |
| 84 |
whatever its contents. |
| 85 |
|
| 86 |
When |
| 87 |
C<html_fmt> adds |
| 88 |
a missing start tag, |
| 89 |
it follows the new start tag with a comment. |
| 90 |
When |
| 91 |
C<html_fmt> adds |
| 92 |
a missing end tag, |
| 93 |
it preceeds the new end tag with a comment. |
| 94 |
When C<html_fmt> classifies characters |
| 95 |
as "cruft", |
| 96 |
it adds a comment to that effect before the "cruft". |
| 97 |
|
| 98 |
C<pre> and C<script> |
| 99 |
elements receive special treatment. |
| 100 |
The contents of |
| 101 |
C<pre> and C<script> elements are not reformatted. |
| 102 |
When missing tags or cruft occur inside a C<pre> element, |
| 103 |
the comments to that effect are placed |
| 104 |
before the C<< <pre> >> start tag. |
| 105 |
The contents of C<script> elements are not |
| 106 |
examined. |
| 107 |
|
| 108 |
=head1 Options |
| 109 |
|
| 110 |
No single set of reformatting choices is anywhere |
| 111 |
near adequate to the variety of standards, |
| 112 |
renderers and application requirements out there. |
| 113 |
Ideally, |
| 114 |
C<html_fmt> would have many options customizing its behavior. |
| 115 |
As of this writing, there are only a few. |
| 116 |
|
| 117 |
=head2 avoid-whitespace |
| 118 |
|
| 119 |
When adding indentation, |
| 120 |
C<html_fmt> runs the risk of introducing |
| 121 |
whitespace that will change |
| 122 |
the way in which the document is displayed. |
| 123 |
How C<html_fmt> deals with this risk |
| 124 |
is controlled by the |
| 125 |
the C<--avoid-whitespace> option, |
| 126 |
which |
| 127 |
may take one of three values: |
| 128 |
C<yes>, C<no> and C<comment>. |
| 129 |
|
| 130 |
If the value |
| 131 |
of the C<--avoid-whitespace> option |
| 132 |
is C<yes>, |
| 133 |
C<html_fmt> does not add indentation |
| 134 |
in whitespace-sensitive places. |
| 135 |
This avoids clutter but, |
| 136 |
because of the missed indentation, |
| 137 |
the structure of the |
| 138 |
document becomes harder to follow. |
| 139 |
C<yes> is the default value. |
| 140 |
|
| 141 |
If the value is C<comment>, |
| 142 |
HTML (SGML) comments are used to achieve proper |
| 143 |
indentation without introducing whitespace. |
| 144 |
This works, but does get a bit cluttered. |
| 145 |
|
| 146 |
If the value is C<no>, |
| 147 |
C<html_fmt> |
| 148 |
adds whitespace whenever it is convenient |
| 149 |
for formatting, regardless of whether or |
| 150 |
not it may change the way in which |
| 151 |
the document displays. |
| 152 |
|
| 153 |
=head2 added-tag-comment |
| 154 |
|
| 155 |
The C<--added-tag-comment> option is a Boolean. |
| 156 |
If set, whenever C<html_fmt> supplies a missing start |
| 157 |
or end tag, C<html_fmt> will include a comment to that effect. |
| 158 |
If the boolean is unset with |
| 159 |
C<--no-added-tag-comment>, |
| 160 |
C<html_fmt> will be silent when supply missing start |
| 161 |
and end tags. |
| 162 |
C<--added-tag-comment> is the default. |
| 163 |
|
| 164 |
=head2 ws-ok-after-start-tag |
| 165 |
|
| 166 |
The C<--ws-ok-after-start-tag> option is a Boolean. |
| 167 |
If set, C<html_fmt> will add whitespace after an start tag when |
| 168 |
that is useful. |
| 169 |
If the SGML standards |
| 170 |
are adhered to, the added whitespace will not |
| 171 |
be rendered. |
| 172 |
However, the HTML standards, while they allow SGML-adherent behavior |
| 173 |
for whitespace after start tags, |
| 174 |
do not require it. |
| 175 |
In some case in practice, |
| 176 |
browsers will render whitespace found after |
| 177 |
start tags. |
| 178 |
|
| 179 |
If C<--ws-ok-after-start-tag> is false, |
| 180 |
C<html_fmt> will not add whitespace after |
| 181 |
start tags. |
| 182 |
But it increases the ability of |
| 183 |
C<html_fmt> to make its output readable, |
| 184 |
but may result in a change in what some |
| 185 |
browsers display. |
| 186 |
|
| 187 |
=head2 ws-ok-before-end-tag |
| 188 |
|
| 189 |
The C<--ws-ok-before-end-tag> option is a Boolean. |
| 190 |
If set, C<html_fmt> will add whitespace before an end tag when |
| 191 |
that is useful. |
| 192 |
If the SGML standards |
| 193 |
are adhered to, the added whitespace will not |
| 194 |
be rendered. |
| 195 |
However, the HTML standards, while they allow SGML-adherent behavior |
| 196 |
for whitespace before end tags, |
| 197 |
do not require it. |
| 198 |
In practice, |
| 199 |
browsers will often render whitespace found before |
| 200 |
end tags. |
| 201 |
|
| 202 |
If C<--ws-ok-before-end-tag> is false, |
| 203 |
C<html_fmt> will not add whitespace before |
| 204 |
end tags. |
| 205 |
C<--ws-ok-before-end-tag> is false by default. |
| 206 |
This is always safe, |
| 207 |
in the sense that it will not change |
| 208 |
what is there to be rendered. |
| 209 |
But it limits the ability of |
| 210 |
C<html_fmt> to make its output readable. |
| 211 |
|
| 212 |
=head1 Example |
| 213 |
|
| 214 |
Given this input: |
| 215 |
|
| 216 |
=for Marpa::R2::Display |
| 217 |
ignore: 1 |
| 218 |
|
| 219 |
<title>Test page<tr>x<head attr="I am cruft"><p>Final graf |
| 220 |
|
| 221 |
=for Marpa::R2::Display::End |
| 222 |
|
| 223 |
to this command: |
| 224 |
|
| 225 |
=for Marpa::R2::Display |
| 226 |
ignore: 1 |
| 227 |
|
| 228 |
html_fmt --avoid-whitespace=comment --ws-ok-before-end-tag |
| 229 |
|
| 230 |
=for Marpa::R2::Display::End |
| 231 |
|
| 232 |
the output is |
| 233 |
|
| 234 |
=for Marpa::R2::Display |
| 235 |
ignore: 1 |
| 236 |
|
| 237 |
<html> |
| 238 |
<!-- html_fmt: Preceeding start tag is replacement for a missing one --> |
| 239 |
<head> |
| 240 |
<!-- html_fmt: Preceeding start tag is replacement for a missing one --> |
| 241 |
<title> |
| 242 |
Test page |
| 243 |
<!-- html_fmt: Following end tag is replacement for a missing one --> |
| 244 |
</title> |
| 245 |
<!-- html_fmt: Following end tag is replacement for a missing one --> |
| 246 |
</head><!-- |
| 247 |
html_fmt: this comment is to avoid introducing whitespace |
| 248 |
--><body> |
| 249 |
<!-- html_fmt: Preceeding start tag is replacement for a missing one --> |
| 250 |
<table> |
| 251 |
<!-- html_fmt: Preceeding start tag is replacement for a missing one --> |
| 252 |
<tbody> |
| 253 |
<!-- html_fmt: Preceeding start tag is replacement for a missing one --> |
| 254 |
<tr> |
| 255 |
<td> |
| 256 |
<!-- html_fmt: Preceeding start tag is replacement for a missing one --> |
| 257 |
x<!-- |
| 258 |
html_fmt: this comment is to avoid introducing whitespace |
| 259 |
--><!-- html_fmt: Next line is cruft --><!-- |
| 260 |
html_fmt: this comment is to avoid introducing whitespace |
| 261 |
--><head attr="I am cruft"><!-- |
| 262 |
html_fmt: this comment is to avoid introducing whitespace |
| 263 |
--><p> |
| 264 |
Final graf |
| 265 |
<!-- html_fmt: Following end tag is replacement for a missing one --> |
| 266 |
</p> |
| 267 |
<!-- html_fmt: Following end tag is replacement for a missing one --> |
| 268 |
</td> |
| 269 |
<!-- html_fmt: Following end tag is replacement for a missing one --> |
| 270 |
</tr> |
| 271 |
<!-- html_fmt: Following end tag is replacement for a missing one --> |
| 272 |
</tbody> |
| 273 |
<!-- html_fmt: Following end tag is replacement for a missing one --> |
| 274 |
</table> |
| 275 |
<!-- html_fmt: Following end tag is replacement for a missing one --> |
| 276 |
</body> |
| 277 |
<!-- html_fmt: Following end tag is replacement for a missing one --> |
| 278 |
</html> |
| 279 |
|
| 280 |
=for Marpa::R2::Display::End |
| 281 |
|
| 282 |
=head1 Acknowledgements |
| 283 |
|
| 284 |
The starting template for this code was |
| 285 |
L<HTML::TokeParser|HTML::TokeParser>, by Gisle Aas. |
| 286 |
|
| 287 |
=head1 Copyright and License |
| 288 |
|
| 289 |
=for Marpa::R2::Display |
| 290 |
ignore: 1 |
| 291 |
|
| 292 |
Copyright 2022 Jeffrey Kegler |
| 293 |
This file is part of Marpa::R2. Marpa::R2 is free software: you can |
| 294 |
redistribute it and/or modify it under the terms of the GNU Lesser |
| 295 |
General Public License as published by the Free Software Foundation, |
| 296 |
either version 3 of the License, or (at your option) any later version. |
| 297 |
|
| 298 |
Marpa::R2 is distributed in the hope that it will be useful, |
| 299 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 300 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 301 |
Lesser General Public License for more details. |
| 302 |
|
| 303 |
You should have received a copy of the GNU Lesser |
| 304 |
General Public License along with Marpa::R2. If not, see |
| 305 |
http://www.gnu.org/licenses/. |
| 306 |
|
| 307 |
=for Marpa::R2::Display::End |
| 308 |
|
| 309 |
=cut |
| 310 |
|
| 311 |
# vim: set expandtab shiftwidth=4: |