Debian Codegraph @

html/script/marpa_r2_html_fmt

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 Fatal qw(open close);
23 use Getopt::Long;
24
25 sub usage
26 {
27 say {*STDERR} "$PROGRAM_NAME html_fmt [uri|file]" or die "say failed: $ERRNO";
28 exit 1;
29 }
30
31 my $help_flag = 0;
32 my $allowed_avoid_whitespace_flags = { map { ( $_, 1) } qw(comment yes no) };
33 my $avoid_whitespace_flag = 'yes';
34 local $main::ADDED_TAG_COMMENT_FLAG = 1;
35 my $ws_ok_before_end_tag_flag = 0;
36 my $ws_ok_after_start_tag_flag = 1;
37 my $dump_config_flag = 0;
38 my $dump_AHFA_flag = 0;
39 my $trace_terminals_flag = 0;
40 my $trace_cruft_flag = 0;
41 my $trace_values_flag = 0;
42 my $compile_flag;
43 usage()
44 if not Getopt::Long::GetOptions(
45 'help' => \$help_flag,
46 'avoid-whitespace=s' => \$avoid_whitespace_flag,
47 'ws-ok-before-end-tag!' => \$ws_ok_before_end_tag_flag,
48 'ws-ok-after-start-tag!' => \$ws_ok_after_start_tag_flag,
49 'added-tag-comment!' => \$main::ADDED_TAG_COMMENT_FLAG,
50
51 # undocumented
52 'dump-config' => \$dump_config_flag,
53 'dump-AHFA' => \$dump_AHFA_flag,
54 'compile=s' => \$compile_flag,
55 'trace-terminals' => \$trace_terminals_flag,
56 'trace-cruft' => \$trace_cruft_flag,
57 'trace-values' => \$trace_values_flag,
58 );
59 usage() if $help_flag or scalar @ARGV > 1;
60
61 if ( not $allowed_avoid_whitespace_flags->{$avoid_whitespace_flag} ) {
62 die "Bad avoid-whitespace flag\n",
63 'avoid-whitespace must be one of the following: ', join q{ },
64 keys %{$allowed_avoid_whitespace_flags};
65 }
66
67 my $locator = shift;
68 my $document;
69 GET_DOCUMENT: {
70 if ( not defined $locator ) {
71 local $RS = undef;
72 ## no critic(InputOutput::ProhibitExplicitStdin)
73 $document = <STDIN>;
74 last GET_DOCUMENT;
75 }
76 if ( $locator =~ /\A [[:alnum:]]+ [:] /xms ) {
77 require WWW::Mechanize;
78 my $mech = WWW::Mechanize->new( autocheck => 1 );
79 $mech->get($locator);
80 $document = $mech->content;
81 undef $mech;
82 last GET_DOCUMENT;
83 } ## end if ( $locator =~ /\A [[:alnum:]]+ [:] /xms )
84 {
85 local $RS = undef;
86 open my $fh, q{<}, $locator;
87 $document = <$fh>;
88 close $fh;
89 }
90 } ## end GET_DOCUMENT:
91
92 sub post_process {
93 my ($value) = @_;
94 my @text_pieces = ();
95 DATUM: for my $line_data ( map { @{$_} } @{$value} ) {
96 my ( $type, $indent ) = @{$line_data};
97 if ( $type eq 'text' ) {
98 my $text = $line_data->[2];
99 my $has_trailing_ws = $text =~ s/ \s+ \z//xms;
100 if ( $text =~ s/ \A \s+ //xms ) {
101 push @text_pieces, [ 'whitespace', $indent ];
102 }
103 my @lines = grep { $_ =~ /\S/xms } split /\n/xms, $text;
104 for my $line_no ( 0 .. $#lines ) {
105 my $line = $lines[$line_no];
106 $line =~ s/\A[ \t]+//xms;
107 $line =~ s/[ \t]+\z//xms;
108 $line =~ s/[ \t]+/ /xms;
109 push @text_pieces, [ 'whitespace', $indent ] if $line_no;
110 push @text_pieces, [ 'text', $indent, $line ];
111 } ## end for my $line_no ( 0 .. $#lines )
112 push @text_pieces, [ 'whitespace', $indent ] if $has_trailing_ws;
113 next DATUM;
114 } ## end if ( $type eq 'text' )
115 if ( $type eq 'msg: missing start tag' ) {
116 my $location = $line_data->[2];
117 my $tagname = $line_data->[3];
118 GIVEN_LOCATION: {
119 if ( $location eq 'preceeding' ) {
120 push @text_pieces,
121 [
122 'html_fmt comment',
123 $indent + 1,
124 'Preceeding start tag is replacement for a missing one'
125 ];
126 last GIVEN_LOCATION;
127 } ## end if ( $location eq 'preceeding' )
128 if ( $location eq 'following pre' ) {
129 push @text_pieces,
130 [
131 'html_fmt comment',
132 $indent,
133 "Inside following <pre>, a start tag is missing: <$tagname>"
134 ];
135 last GIVEN_LOCATION;
136 } ## end if ( $location eq 'following pre' )
137 Carp::croak(
138 "Internal error: unprovided-for missing start tag location: $_"
139 );
140 } ## end GIVEN_LOCATION:
141 next DATUM;
142 } ## end if ( $type eq 'msg: missing start tag' )
143 if ( $type eq 'msg: missing end tag' ) {
144 my $location = $line_data->[2];
145 my $tagname = $line_data->[3];
146 GIVEN_LOCATION: {
147 if ( $location eq 'following' ) {
148 push @text_pieces,
149 [
150 'html_fmt comment',
151 $indent + 1,
152 'Following end tag is replacement for a missing one'
153 ];
154 last GIVEN_LOCATION;
155 } ## end if ( $location eq 'following' )
156 if ( $location eq 'following pre' ) {
157 push @text_pieces,
158 [
159 'html_fmt comment',
160 $indent,
161 "Inside following <pre>, an end tag is missing: </$tagname>"
162 ];
163 last GIVEN_LOCATION;
164 } ## end if ( $location eq 'following pre' )
165 Carp::croak(
166 "Internal error: unprovided-for missing end tag location: $_"
167 );
168 } ## end GIVEN_LOCATION:
169 next DATUM;
170 } ## end if ( $type eq 'msg: missing end tag' )
171 if ( $type eq 'msg: missing pre end tag' ) {
172 push @text_pieces,
173 [
174 'html_fmt comment',
175 $indent,
176 'An missing end tag was added to close the preceding <pre> element'
177 ];
178 next DATUM;
179 } ## end if ( $type eq 'msg: missing pre end tag' )
180 if ( $type eq 'msg: missing pre start tag' ) {
181 push @text_pieces,
182 [
183 'html_fmt comment',
184 $indent,
185 'A missing start tag was added in front of the following <pre> element'
186 ];
187 next DATUM;
188 } ## end if ( $type eq 'msg: missing pre start tag' )
189 if ( $type eq 'msg: cruft' ) {
190 my $location = $line_data->[2];
191 my $cruft = $line_data->[3];
192 my $token_type = $line_data->[4];
193 my $node_desc =
194 $token_type eq 'S' ? 'start tag'
195 : $token_type eq 'E' ? 'end tag'
196 : $token_type eq 'T' ? 'text'
197 : 'node';
198 GIVEN_LOCATION: {
199 if ( $location eq 'following' ) {
200 push @text_pieces,
201 [
202 'html_fmt comment',
203 $indent,
204 "Next $node_desc is cruft"
205 ];
206 last GIVEN_LOCATION;
207 } ## end if ( $location eq 'following' )
208 if ( $location eq 'following pre' ) {
209
210 # Make sure the cruft quoted inside
211 # the HTML comment does not
212 # disrupt the comment.
213 ( my $safe_cruft = $cruft ) =~ s/--/- -/xms;
214 push @text_pieces,
215 [
216 'html_fmt comment',
217 $indent,
218 qq{Inside the following <pre>, $node_desc is cruft:\n$safe_cruft}
219 ];
220 last GIVEN_LOCATION;
221 } ## end if ( $location eq 'following pre' )
222 Carp::croak(
223 "Internal error: unprovided-for cruft location: $_");
224 } ## end GIVEN_LOCATION:
225 next DATUM;
226 } ## end if ( $type eq 'msg: cruft' )
227 push @text_pieces, [ $type, $indent, $line_data->[2] ];
228 } ## end for my $line_data ( map { @{$_} } @{$value} )
229 my @ws_unsafe;
230 $ws_unsafe[ scalar @text_pieces ] = 0; # size the array
231 $ws_unsafe[0] = 0;
232 my $last_non_comment_ix = scalar @text_pieces;
233 my $safe_after_piece = 0;
234 my $first_body_tag;
235 my $last_body_tag;
236 TEXT_PIECE: for my $text_piece_ix ( 0 .. $#text_pieces ) {
237 my ( $type, $indent, $text ) = @{ $text_pieces[$text_piece_ix] };
238 my $safe_before_piece = 0;
239 WHITESPACE_BY_TYPE: {
240
241 # If a comment, after-piece is whatever current value
242 # of $safe_after_piece,
243 # leave before as-is
244 last WHITESPACE_BY_TYPE if $type eq 'html_fmt comment';
245 last WHITESPACE_BY_TYPE if $type eq 'comment';
246 if ( $type eq 'whitespace' ) {
247
248 # Safe before and after other whitespace
249 $safe_before_piece = $safe_after_piece = 1;
250 last WHITESPACE_BY_TYPE;
251 } ## end if ( $type eq 'whitespace' )
252 if ( $type eq 'end tag' ) {
253
254 if ( $text =~ /\A [^[:alnum:]]+ body [^[:alnum:]]+ \z /xmsi )
255 {
256 $last_body_tag = $text_piece_ix;
257 }
258
259 # Unsafe after an end tag, but safe before it
260 $safe_before_piece = $ws_ok_before_end_tag_flag;
261 $safe_after_piece = 0;
262 last WHITESPACE_BY_TYPE;
263 } ## end if ( $type eq 'end tag' )
264 if ( $type eq 'start tag' ) {
265
266 if ( not defined $first_body_tag
267 and $text
268 =~ /\A [^[:alnum:]]+ body [^[:alnum:]]+ \z /xmsi )
269 {
270 $first_body_tag = $text_piece_ix;
271 } ## end if ( not defined $first_body_tag and $text =~ ...)
272
273 # Leave safe status as-is before start tag
274 # Whitespace safe after a start tag
275 $safe_after_piece = $ws_ok_after_start_tag_flag;
276 last WHITESPACE_BY_TYPE;
277 } ## end if ( $type eq 'start tag' )
278 if ( $type eq 'text' ) {
279
280 # Leave safe status as-is before text
281 # Whitespace not safe after text
282 $safe_after_piece = 0;
283 last WHITESPACE_BY_TYPE;
284 } ## end if ( $type eq 'text' )
285 # Blocks can occur inline, etc.
286 # So with everything else, be conservative.
287 # Whitespace is unsafe after, and left as-is before.
288 $safe_after_piece = 0;
289 } ## end WHITESPACE_BY_TYPE:
290 $ws_unsafe[ $text_piece_ix + 1 ] = $safe_after_piece ? 0 : 1;
291 if ($safe_before_piece) {
292 for my $ix ( $last_non_comment_ix + 1 .. $text_piece_ix ) {
293 $ws_unsafe[$ix] = 0;
294 }
295 }
296 if ( $type ne 'comment' and $type ne 'html_fmt comment' ) {
297 $last_non_comment_ix = $text_piece_ix;
298 }
299 } ## end TEXT_PIECE: for my $text_piece_ix ( 0 .. $#text_pieces )
300
301 # Whitespace safe before the body
302 $first_body_tag //= -1;
303 for (
304 my $text_piece_ix = 0;
305 $text_piece_ix <= $first_body_tag;
306 $text_piece_ix++
307 )
308 {
309 $ws_unsafe[$text_piece_ix] = 0;
310 } ## end for ( my $text_piece_ix = 0; $text_piece_ix <= $first_body_tag...)
311
312 # Whitespace safe after the body,
313 # or at least at the EOF if no end tag
314 $last_body_tag //= $#text_pieces;
315 for (
316 my $text_piece_ix = scalar @text_pieces;
317 $text_piece_ix > $last_body_tag;
318 $text_piece_ix--
319 )
320 {
321 $ws_unsafe[$text_piece_ix] = 0;
322 } ## end for ( my $text_piece_ix = scalar @text_pieces; $text_piece_ix...)
323
324 my @output = ();
325 CREATE_OUTPUT: {
326 if ( $avoid_whitespace_flag eq 'comment' ) {
327 TEXT_PIECE: for my $text_piece_ix ( 0 .. $#text_pieces ) {
328 my ( $type, $indent, $text ) =
329 @{ $text_pieces[$text_piece_ix] };
330 next TEXT_PIECE if $type eq 'whitespace';
331 my $suffix = $ws_unsafe[ $text_piece_ix + 1 ] ? '<!--' : q{};
332 my $indentation = q{ } x $indent;
333 if ( $type eq 'html_fmt comment' ) {
334 push @output,
335 $indentation
336 . ' html_fmt: '
337 . $text . ' -->'
338 . $suffix;
339 next TEXT_PIECE;
340 } ## end if ( $type eq 'html_fmt comment' )
341 if ( $ws_unsafe[$text_piece_ix] ) {
342 $indentation =
343 $indentation
344 . ' html_fmt: this comment is to avoid introducing whitespace'
345 . "\n"
346 . $indentation . '-->';
347 } ## end if ( $ws_unsafe[$text_piece_ix] )
348 if ( $type eq 'text' and $text =~ /\S/xms ) {
349 push @output, $indentation . $text . $suffix;
350 next TEXT_PIECE;
351 }
352 push @output, $indentation . $text . $suffix;
353 } ## end TEXT_PIECE: for my $text_piece_ix ( 0 .. $#text_pieces )
354 last CREATE_OUTPUT;
355 } ## end if ( $avoid_whitespace_flag eq 'comment' )
356 {
357 # $avoid_whitespace eq 'yes' or $avoid_whitespace eq 'no'
358 my $avoid_whitespace_is_yes = $avoid_whitespace_flag ne 'no';
359 TEXT_PIECE: for my $text_piece_ix ( 0 .. $#text_pieces ) {
360 my ( $type, $indent, $text ) =
361 @{ $text_pieces[$text_piece_ix] };
362 next TEXT_PIECE if $type eq 'whitespace';
363 if ( $avoid_whitespace_is_yes and $ws_unsafe[$text_piece_ix] )
364 {
365 my $indentation = q{ } x ($indent);
366 if ( $type eq 'html_fmt comment' ) {
367 $text =
368 qq{<!--\n}
369 . $indentation
370 . ' html_fmt: '
371 . $text . "\n"
372 . $indentation . ' -->';
373 } ## end if ( $type eq 'html_fmt comment' )
374 $output[-1] .= $text;
375 next TEXT_PIECE;
376 } ## end if ( $avoid_whitespace_is_yes and $ws_unsafe[...])
377 my $indentation = q{ } x $indent;
378 if ( $type eq 'text' and $text =~ /\S/xms ) {
379 push @output, $indentation . $text;
380 next TEXT_PIECE;
381 }
382 if ( $type eq 'html_fmt comment' ) {
383 push @output,
384 $indentation . '<!-- html_fmt: ' . $text . ' -->';
385 next TEXT_PIECE;
386 }
387 push @output, $indentation . $text;
388 } ## end TEXT_PIECE: for my $text_piece_ix ( 0 .. $#text_pieces )
389 last CREATE_OUTPUT;
390 }
391 } ## end CREATE_OUTPUT:
392 return join "\n", @output, q{};
393 } ## end sub post_process
394
395 sub do_pre {
396 my @new_line_data = ();
397 my $start_tag = Marpa::R2::HTML::start_tag();
398 my $end_tag = Marpa::R2::HTML::end_tag();
399
400 CHILD:
401 for my $value ( @{ Marpa::R2::HTML::values() } ) {
402 for my $line_data ( @{$value} ) {
403 if ( $line_data->[0] =~ /\A msg [:] /xms ) {
404 push @new_line_data,
405 [
406 $line_data->[0], 0,
407 'following pre', @{$line_data}[ 3 .. $#{$line_data} ]
408 ];
409 } ## end if ( $line_data->[0] =~ /\A msg [:] /xms )
410 } ## end for my $line_data ( @{$value} )
411 } ## end CHILD: for my $value ( @{ Marpa::R2::HTML::values() } )
412
413 if ( not defined $start_tag ) {
414 push @new_line_data, [ 'msg: missing pre start tag', 0 ]
415 if $main::ADDED_TAG_COMMENT_FLAG;
416 push @new_line_data, [ 'start tag', 0, '<pre>' ];
417 }
418
419 my $original = Marpa::R2::HTML::original();
420 push @new_line_data, [ 'pre', 0, $original ];
421
422 if ( not defined $end_tag ) {
423 push @new_line_data, [ 'end tag', 0, '</pre>' ]
424 if $main::ADDED_TAG_COMMENT_FLAG;
425 push @new_line_data, [ 'msg: missing pre end tag', 0 ];
426 }
427 return \@new_line_data;
428 } ## end sub do_pre
429
430 sub do_cruft {
431 my $literal = Marpa::R2::HTML::literal();
432 my @new_line_data = ( [ 'msg: cruft', 0, 'following', $literal, Marpa::R2::HTML::token_type() ] );
433 push @new_line_data, [ 'cruft', 0, $literal ];
434 return \@new_line_data;
435 } ## end sub do_cruft
436
437 sub do_comment {
438 my $literal = Marpa::R2::HTML::literal();
439 return [ [ 'comment', 0, $literal ] ];
440 }
441
442 sub do_default {
443 my $tagname = Marpa::R2::HTML::tagname();
444 my @new_line_data = ();
445
446 my $start_tag = Marpa::R2::HTML::start_tag();
447 my $end_tag = Marpa::R2::HTML::end_tag();
448 my $is_empty_element = Marpa::R2::HTML::is_empty_element();
449 if ($is_empty_element) {
450 if ( defined $start_tag ) {
451 push @new_line_data, [ 'empty element', 0, $start_tag ];
452 }
453 else {
454 push @new_line_data, [ 'empty element', 0, '<' . $tagname . '>' ];
455 push @new_line_data,
456 [ 'msg: empty element missing start tag', 0, 'preceeding', $tagname ]
457 if $main::ADDED_TAG_COMMENT_FLAG;
458 } ## end else [ if ( defined $start_tag ) ]
459 return \@new_line_data;
460 }
461
462 {
463 my $tag_type = 'start tag';
464 if ( defined $start_tag ) {
465 push @new_line_data, [ $tag_type, 0, $start_tag ];
466 }
467 else {
468 push @new_line_data, [ $tag_type, 0, '<' . $tagname . '>' ];
469 push @new_line_data,
470 [ 'msg: missing start tag', 0, 'preceeding', $tagname ]
471 if $main::ADDED_TAG_COMMENT_FLAG;
472 } ## end else [ if ( defined $start_tag ) ]
473 }
474
475 my @descendant_data = @{ Marpa::R2::HTML::descendants('value,original') };
476 my $first_content_child = defined $start_tag ? 1 : 0;
477 my $last_content_child = $#descendant_data - ( defined $end_tag ? 1 : 0 );
478 CHILD:
479 for my $descendant_data_ix ( $first_content_child .. $last_content_child )
480 {
481 my ( $value, $original ) = @{ $descendant_data[$descendant_data_ix] };
482 if ( defined $value ) {
483 for my $line_data ( @{$value} ) {
484 my ( $type, $indent, @data ) = @{$line_data};
485 push @new_line_data, [ $type, $indent + 1, @data ];
486 }
487 next CHILD;
488 } ## end if ( defined $value )
489 push @new_line_data, [ 'text', 1, $original ];
490 } ## end CHILD: for my $descendant_data_ix ( $first_content_child .. ...)
491
492 if ( not $is_empty_element ) {
493 if ( defined $end_tag ) {
494 push @new_line_data, [ 'end tag', 0, $end_tag ];
495 }
496 else {
497 push @new_line_data,
498 [ 'msg: missing end tag', 0, 'following', $tagname ]
499 if $main::ADDED_TAG_COMMENT_FLAG;
500 push @new_line_data, [ 'end tag', 0, '</' . $tagname . '>' ];
501 } ## end else [ if ( defined $end_tag ) ]
502 } ## end if ( not $is_empty_element )
503
504 return \@new_line_data;
505 } ## end sub do_default
506
507 my %html_args = (
508 'script' => sub {
509 return [ [ 'script', 0, Marpa::R2::HTML::original() ] ];
510 },
511 ':CRUFT' => \&do_cruft,
512 ':COMMENT' => \&do_comment,
513 'pre' => \&do_pre,
514 q{*} => \&do_default,
515 ':TOP' => sub { return Marpa::R2::HTML::values(); }
516 );
517
518 my %flags = (
519 trace_terminals => $trace_terminals_flag,
520 trace_cruft => $trace_cruft_flag,
521 trace_values => $trace_values_flag,
522 dump_config => $dump_config_flag,
523 dump_AHFA => $dump_AHFA_flag,
524 );
525 if (defined $compile_flag) {
526 open my $fh, q{<}, $compile_flag;
527 my $source = join q{}, <$fh>;
528 close $fh;
529 $flags{compile} = \$source;
530 }
531
532 my $value_ref = Marpa::R2::HTML::html( \$document, \%html_args, \\%flags );
533 die 'Internal error: no parse' if not defined $value_ref;
534 if ($dump_config_flag or $dump_AHFA_flag) {
535 print ${$value_ref};
536 exit 0;
537 }
538
539 print post_process($value_ref) or die "print failed: $ERRNO";
540
541 exit 0;
542
543 # vim: set expandtab shiftwidth=4: