#!/perl -w
# NAME: tidytxt.pl
# AIM: To use Tidy to check some text
# try to get stdin working ...
# 03/09/2007 geoff mclane - http://geoffair.net/mperl
#use strict;
use warnings;
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 ...\n" );
my $zfil = 'temp1.zip';

my $params = '-f temptidy.txt --tidy-mark no --doctype omit --show-body-only yes';
my $txt = "<p>&#160; paragraph</p>";

my $ntxt = get_tidy_text( $txt );
prt( "Bgn:[$txt], Returned:[$ntxt] ...\n" );

#my $zt = get_zip_txt2($zfil);
#prt( "Got [$zt]\n" );

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

######################################
######## subs
sub get_tidy_text {
	my ($tx) = shift;
	my $inf = 'temptidy2.htm';
	my $ntx = '';
	write2file($tx,$inf);
	if (open (TDY, "tidydev $params $inf|")) {
		my @arr = <TDY>;
		close TDY;
		foreach my $ln (@arr) {
			chomp $ln;
			if( length($ln) ) {
				$ntx .= "\n" if length($ntx);
				$ntx .= $ln;
			}
		}
		###$ntx = join('',@arr);
	} else {
		prt( "FAILED! ... $! ...\n" );
		$ntx = $tx;
	}
	return $ntx;
}

sub get_tidy_text2 {
	my ($tx) = shift;
	my $ntx = '';
	#print STDIN $tx;
	print "$tx\n";
	if (open (TDY, "tidycvs $params |")) {
		#print "$tx".0x2a;
		my @arr = <TDY>;
		close TDY;
		foreach my $ln (@arr) {
			chomp $ln;
			$ntx .= "\n" if length($ntx);
			$ntx .= $ln;
		}
		###$ntx = join('',@arr);
	} else {
		$ntx = "FAILED! ... $! ...";
		prt( "$ntx\n" );
	}
	return $ntx;
}


sub get_zip_txt {
	my ($f) = shift; # = '..\Perl20070127.zip';
	my $rd = 'tempzip2.txt';
	my @zargs = ('zip8','-vb',$f, '>', $rd);
	my $result = system(@zargs);
	prt( "Got results [$result]\n" );
	open ZIN, "<$rd" or mydie( "Unable to open $rd ...\n" );
	my @arr = <ZIN>;
	close ZIN;
	my $ztxt = join('', @arr);
	return $ztxt;
}

sub get_zip_txt2 {
	my ($f) = shift; # = '..\Perl20070127.zip';
	my @zargs = ('zip8','-vb',$f, '|');
	my $ztxt = "FAILED";
	my $result = system(@zargs);
	my @arr = <STDIN>;
	#if (open( ZIN, system(@zargs))) {
	#if (open( ZIN, STDIN)) {
	#	my @arr = <ZIN>;
	#	close ZIN;
	$ztxt = join('', @arr);
	return $ztxt;
}

# eof
