Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a command line argument that I've been using on my mac to clean a folder of html files. I've been trying to replicate it using a perl script. Here's the command line code:

find /Users/tlialin/desktop/01282016/Test -type f -name "*.html" -exec + tidy -f errors.txt -m -utf8 -i {} \;

With the help of several here I've been able to get the following perl script together:

use HTML::Tidy; my $call_dir = "Test"; my $contents_of_file = 1; #my $tidy = HTML::Tidy->new(); my $tidy = HTML::Tidy->new({config_file => 'config.txt'}); #my $tidy = HTML::Tidy->new( {config_file => 'path/to/config'} ); my @files = glob "$call_dir/*.html"; printf "Got %d files\n", scalar @files; for my $file (@files) { open my $in_fh, '<', $file or die "Could not open $file : $!"; my $contents_of_file = do { local $/;<$in_fh> }; close $in_fh; $tidy->parse( $file, $contents_of_file ); $tidy->clean( $file); #parse( $filename, $str [, $str...] ) #or warn "Error parsing $file :$!"; for my $message ( $tidy->messages ) { print $message->as_string; } }

It runs and I can see output in terminal but it's hard to tell if it's cleaning the html files. The command line argument added the following meta tags to all of the files:

<meta name="generator" content= "HTML Tidy for Mac OS X (vers 31 October 2006 - Apple Inc. build 15. +15), see www.w3.org">

I think my problem is currently with the config file. I looked on line and found something but I'm not sure if the content or format is correct. Here's my current config file:

output-xhtml: yes add-xml-decl: no doctype: strict char-encoding: ascii indent: auto wrap: 76 repeated-attributes: keep-last error-file: errs.txt

Do I even need to use the config file? I couldn't find a way to pass the arguments in the script. Any guidance and or help is greatly appreciated

Replies are listed 'Best First'.
Re: HTML::Tidy help
by AnomalousMonk (Archbishop) on Feb 02, 2016 at 18:23 UTC

    This thread appears to be a continuation of the discussion in Perl HTML::Tidy. It is helpful to include a link to such previous and related discussions.


    Give a man a fish:  <%-{-{-{-<

Re: HTML::Tidy help
by poj (Abbot) on Feb 02, 2016 at 19:06 UTC

    I don't know about all the configs but you should be able to add into the constructor , change dashes to underscores

    my $tidy = HTML::Tidy->new({ tidy_mark => 1, output_xhtml => 1, # yes add_xml_decl => 0, # no wrap => 76, });

    Update : This node might be of interest to you ; Re^3: HTML::Tidy and mysterious HTML::Tidy::Document

    poj

      Thanks poj! All of your help if very much appreciated. I fincally found all of the variables on line and have modified by script as follows:

      use HTML::Tidy; my $call_dir = "Html3"; my $contents_of_file = 1; #my $tidy = HTML::Tidy->new(); my $tidy = HTML::Tidy->new({ tidy_mark => 1, #output_xhtml => 1, # yes output_html => 1, # yes add_xml_decl => 1, # no wrap => 76, error_file => errs.txt, char_encoding => utf8, indent_cdata => 1 }); #my $tidy = HTML::Tidy->new({config_file => 'config3.txt'}); #my $tidy = HTML::Tidy->new( {config_file => 'path/to/config'} ); my @files = glob "$call_dir/*.html"; printf "Got %d files\n", scalar @files; for my $file (@files) { open my $in_fh, '<', $file or die "Could not open $file : $!"; my $contents_of_file = do { local $/;<$in_fh> }; close $in_fh; $tidy->parse( $file, $contents_of_file ); $tidy->clean( $file); #parse( $filename, $str [, $str...] ) #or warn "Error parsing $file :$!"; for my $message ( $tidy->messages ) { #print $message->as_string; } }

      The problem that I have now is that running the common line: find /Users/tlialin/desktop/Tidy_Script/Html3 -type f -name "*.html" -exec tidy -f errors.txt -m -utf8 -i {} \; produces a header that looks like this:

      <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta name="generator" content= "HTML Tidy for Mac OS X (vers 31 October 2006 - Apple Inc. build 15. +15), see www.w3.org"> <title>Aberdeen%20Animal%20trait%20analysis , News Search | Ask.com</title>

      and the perl script produces this:

      http://www.ask.com/news?q=Aberdeen%2520Animal%2520trait%2520analysis+& +qsrc=167&qo=channelNavigation&o=0&l=dir <!DOCTYPE html> <!--[if IE 7 ]> <html lang='en' class='ie7'> <![endif]--> <!--[if IE 8 ]> <html lang='en' class='ie8'> <![endif]--> <!--[if IE 9 ]> <html lang='en' class='ie9'> <![endif]--> <!--[if (gt IE 9)|!(IE)]><!--> <html lang='en'> <!--<![endif]--> <html> <head> <title>Aberdeen%20Animal%20trait%20analysis , News Search | Ask.com</t +itle>

      I'm now questioning if the perl script is writing anything at all. Ides or suggestion very appreciated

        $tidy->clean( $file); does not clean the file it returns a clean file. You need to do
        open OUT,'>','output.html' or die "$!"; print OUT $tidy->clean( $file);
        poj