#!/usr/bin/perl 
# Copyright 2022 Jeffrey Kegler
# This file is part of Marpa::R2.  Marpa::R2 is free software: you can
# redistribute it and/or modify it under the terms of the GNU Lesser
# General Public License as published by the Free Software Foundation,
# either version 3 of the License, or (at your option) any later version.
#
# Marpa::R2 is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser
# General Public License along with Marpa::R2.  If not, see
# http://www.gnu.org/licenses/.

use 5.010001;
use strict;
use warnings;
use English qw( -no_match_vars );
use Marpa::R2::HTML;
use Fatal qw(open close);
use Getopt::Long;

sub usage
{
    say {*STDERR} "$PROGRAM_NAME html_fmt [uri|file]" or die "say failed: $ERRNO";
    exit 1;
}

my $help_flag              = 0;
my $allowed_avoid_whitespace_flags = { map { ( $_, 1) } qw(comment yes no) };
my $avoid_whitespace_flag  = 'yes';
local $main::ADDED_TAG_COMMENT_FLAG = 1;
my $ws_ok_before_end_tag_flag = 0;
my $ws_ok_after_start_tag_flag = 1;
my $dump_config_flag = 0;
my $dump_AHFA_flag = 0;
my $trace_terminals_flag = 0;
my $trace_cruft_flag = 0;
my $trace_values_flag = 0;
my $compile_flag;
usage()
    if not Getopt::Long::GetOptions(
    'help'               => \$help_flag,
    'avoid-whitespace=s' => \$avoid_whitespace_flag,
    'ws-ok-before-end-tag!' => \$ws_ok_before_end_tag_flag,
    'ws-ok-after-start-tag!' => \$ws_ok_after_start_tag_flag,
    'added-tag-comment!' => \$main::ADDED_TAG_COMMENT_FLAG,

    # undocumented
    'dump-config' => \$dump_config_flag,
    'dump-AHFA' => \$dump_AHFA_flag,
    'compile=s' => \$compile_flag,
    'trace-terminals' => \$trace_terminals_flag,
    'trace-cruft' => \$trace_cruft_flag,
    'trace-values' => \$trace_values_flag,
    );
usage() if $help_flag or scalar @ARGV > 1;

if ( not $allowed_avoid_whitespace_flags->{$avoid_whitespace_flag} ) {
    die "Bad avoid-whitespace flag\n",
        'avoid-whitespace must be one of the following: ', join q{ },
        keys %{$allowed_avoid_whitespace_flags};
}

my $locator = shift;
my $document;
GET_DOCUMENT: {
    if ( not defined $locator ) {
        local $RS = undef;
        ## no critic(InputOutput::ProhibitExplicitStdin)
        $document = <STDIN>;
        last GET_DOCUMENT;
    }
    if ( $locator =~ /\A [[:alnum:]]+ [:] /xms ) {
        require WWW::Mechanize;
        my $mech = WWW::Mechanize->new( autocheck => 1 );
        $mech->get($locator);
        $document = $mech->content;
        undef $mech;
        last GET_DOCUMENT;
    } ## end if ( $locator =~ /\A [[:alnum:]]+ [:] /xms )
    {
        local $RS = undef;
        open my $fh, q{<}, $locator;
        $document = <$fh>;
        close $fh;
    }
} ## end GET_DOCUMENT:

sub post_process {
    my ($value) = @_;
    my @text_pieces = ();
    DATUM: for my $line_data ( map { @{$_} } @{$value} ) {
        my ( $type, $indent ) = @{$line_data};
        if ( $type eq 'text' ) {
            my $text = $line_data->[2];
            my $has_trailing_ws = $text =~ s/ \s+ \z//xms;
            if ( $text =~ s/ \A \s+ //xms ) {
                push @text_pieces, [ 'whitespace', $indent ];
            }
            my @lines = grep { $_ =~ /\S/xms } split /\n/xms, $text;
            for my $line_no ( 0 .. $#lines ) {
                my $line = $lines[$line_no];
                $line =~ s/\A[ \t]+//xms;
                $line =~ s/[ \t]+\z//xms;
                $line =~ s/[ \t]+/ /xms;
                push @text_pieces, [ 'whitespace', $indent ] if $line_no;
                push @text_pieces, [ 'text', $indent, $line ];
            } ## end for my $line_no ( 0 .. $#lines )
            push @text_pieces, [ 'whitespace', $indent ] if $has_trailing_ws;
            next DATUM;
        } ## end if ( $type eq 'text' )
        if ( $type eq 'msg: missing start tag' ) {
            my $location = $line_data->[2];
            my $tagname  = $line_data->[3];
            GIVEN_LOCATION: {
                if ( $location eq 'preceeding' ) {
                    push @text_pieces,
                        [
                        'html_fmt comment',
                        $indent + 1,
                        'Preceeding start tag is replacement for a missing one'
                        ];
                    last GIVEN_LOCATION;
                } ## end if ( $location eq 'preceeding' )
                if ( $location eq 'following pre' ) {
                    push @text_pieces,
                        [
                        'html_fmt comment',
                        $indent,
                        "Inside following <pre>, a start tag is missing: <$tagname>"
                        ];
                    last GIVEN_LOCATION;
                } ## end if ( $location eq 'following pre' )
                Carp::croak(
                    "Internal error: unprovided-for missing start tag location: $_"
                );
            } ## end GIVEN_LOCATION:
            next DATUM;
        } ## end if ( $type eq 'msg: missing start tag' )
        if ( $type eq 'msg: missing end tag' ) {
            my $location = $line_data->[2];
            my $tagname  = $line_data->[3];
            GIVEN_LOCATION: {
                if ( $location eq 'following' ) {
                    push @text_pieces,
                        [
                        'html_fmt comment',
                        $indent + 1,
                        'Following end tag is replacement for a missing one'
                        ];
                    last GIVEN_LOCATION;
                } ## end if ( $location eq 'following' )
                if ( $location eq 'following pre' ) {
                    push @text_pieces,
                        [
                        'html_fmt comment',
                        $indent,
                        "Inside following <pre>, an end tag is missing: </$tagname>"
                        ];
                    last GIVEN_LOCATION;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        