Tidy01.pl to HTML.

index -|- end

Generated: Sat Oct 24 16:35:30 2020 from Tidy01.pl 2017/05/22 2.9 KB. text copy

#!/perl -w
# NAME: Tidy01.pl
# AIM: To test a little, the HTML::Tidy interface ...
use strict;
use warnings;
my $tidy_dir = "F:/Projects/html-tidy-pet-fork/lib";
my $tidy_pm = $tidy_dir."/HTML/Tidy.pm";
my $perl_dir = 'C:/GTools/perl';
if ( ! -f $tidy_pm) {
    die "Unable to locate `$tidy_pm'! *** FIX ME ***\n";
}
if ( ! -d $perl_dir) {
    die "Unable to locate `$perl_dir'! *** FIX ME ***\n";
}
#########################################
#use lib $perl_dir;
#use lib "$tidy_dir";
#BEGIN {
#unshift(@INC, $perl_dir);
unshift(@INC, $tidy_dir);
#unshift(@INC, $tidy_pm);
#};

#use HTML::Tidy;
require 'logfile.pl' or die "Unable to load logfile.pl ...\n";
# log file stuff
my ($LF);
my $pgmname = $0;
if ($pgmname =~ /\w{1}:\\.*/) {
    my @tmpsp = split(/\\/,$pgmname);
    $pgmname = $tmpsp[-1];
}
my $outfile = "temp.$pgmname.txt";
open_log($outfile);
prt( "$0 ... Hello, World ...Testing HTML::Tidy ...\n" );

my $err_file = "temperrors.txt";
my $out_file = "temp.tidied.htm";

my $doc = <<"End_of_doc;";
<html>
<head>
  <title>foo</title>
</head>
<body>
  <p>Hello Tidy!</p>
</body>
</html>
End_of_doc;

# get a new document ...
#########################################
#BEGIN {
##unshift(@INC, $perl_dir);
##unshift(@INC, $tidy_dir);
#push(@INC, $perl_dir);
#push(@INC, $tidy_dir);
#print join(" ",@INC)."\n";
#require HTML::Tidy;
#}
########################################
eval { require HTML::Tidy; };

if ( !$@ ) {
    prt("Loaded HTML::Tidy...\n");
} else {
    die "Failed to loaded HTML::Tidy...\n $@ ...\n";
}

#########################################
my $tidy = HTML::Tidy::Document->new();
if (! $tidy) {
    mydie( "ERROR: Failed to load HTML::Tidy ... $! ...\n" );
} else {
    prt( "Got new Tidy document ...\n" );
}

my $stat = $tidy->Create(); # create a context ...
if ($stat < 0) {
    mydie( "ERROR: Failed to Create HTML::Tidy ... $! ...\n" );
} else {
    prt( "Created new context ...\n" );
}

$stat = $tidy->SetErrorFile( $err_file );
if ($stat < 0) {
    mydie( "ERROR: Failed to set error file ... $! ...\n" );
} else {
    prt( "Set error file to $err_file ...\n" );
}

$stat = $tidy->ParseString($doc);
if ($stat < 0) {
    mydie( "ERROR: ParseString Failed ... $! ...\n" );
} else {
    prt( "Parsed string [$doc] ...\n" );
}

$stat = $tidy->CleanAndRepair();
if ($stat < 0) {
    mydie( "ERROR: CleanAndRepair Failed ... $! ...\n" );
} else {
    prt( "Done clean and repare ...\n" );
}

$stat = $tidy->RunDiagnostics();
if ($stat < 0) {
    mydie( "ERROR: RunDiagnostics Failed ... $! ...\n" );
} else {
    prt( "Ran diagnostics ...\n" );
}

$stat = $tidy->SaveFile( $out_file );
if ($stat < 0) {
    mydie( "ERROR: SaveFile Failed ... $! ...\n" );
} else {
    prt( "Tidied text written to $out_file ...\n" );
}

$tidy->Release();

prt( "Loading output into default browser ...\n" );

system ( $out_file );

close_log($outfile,0);
exit(0);

# eof

index -|- top

checked by tidy  Valid HTML 4.01 Transitional