#!/usr/bin/env perl
#===============================================================================
#
#         FILE: validate_html
#
#        USAGE: ./validate_html file
#
#  DESCRIPTION: Validate HTML show notes using HTML::Valid
#
#      OPTIONS: ---
# REQUIREMENTS: ---
#         BUGS: ---
#        NOTES: Much of this code is as seen in 'htmlok' because the
#               documentation with HTML::Valid is poor.
#       AUTHOR: Dave Morriss (djm), Dave.Morriss@gmail.com
#      VERSION: 0.0.3
#      CREATED: 2018-06-01 16:25:27
#     REVISION: 2019-11-10 21:25:06
#
#===============================================================================

use 5.010;
use strict;
use warnings;
use utf8;

# In the POD, without explanation
use FindBin '$Bin';

# Found in 'htmlok', not documented
use HTML::Valid 'sanitize_errors';
use Path::Tiny;

#
# Version number (manually incremented)
#
our $VERSION = '0.0.3';

#
# Script and directory names
#
( my $PROG = $0 ) =~ s|.*/||mx;
( my $DIR  = $0 ) =~ s|/?[^/]*$||mx;
$DIR = '.' unless $DIR;

#-------------------------------------------------------------------------------
# Declarations
#-------------------------------------------------------------------------------
#
# Constants and other declarations
#
my $basedir = "$ENV{HOME}/HPR/Show_Submission";

#
# Enable Unicode mode
#
binmode STDOUT, ":encoding(UTF-8)";
binmode STDERR, ":encoding(UTF-8)";

#
# Create the object
#
my $htv = HTML::Valid->new(
    quiet          => 1, # omit summary, etc
    doctype        => 'omit',
    show_body_only => 1, # print only the contents of the body tag as an HTML fragment
    show_info      => 1, # test; omit info-level messages
    show_warnings  => 1, # include warnings if set
);

#
# We expect a file as an argument
#
my $file = shift @ARGV;
die "Usage: $PROG filename\n" unless $file;
my $content;
if ( -f $file ) {
    $content = path($file)->slurp_utf8();
    $htv->set_filename($file);
}
else {
    die "$PROG: Cannot find '$file'.\n";
}

#
# Largely undocumented but seen in 'htmlok'
#
my ( undef, $errors ) = $htv->run($content);
if ($errors) {
    print sanitize_errors ($errors);
    exit 1;
}

exit;

# vim: syntax=perl:ts=8:sw=4:et:ai:tw=78:fo=tcrqn21:fdm=marker
