Update: Doh! Misconstrued the question to mean remove JavaScript. Leaving the code anyway---it might be useful to someone.

Aristotle helped me with a similar project a while back in another forum. Here's the result. It uses HTML::TokeParser::Simple to do the stripping and works in a very intuitive manner.

#!/usr/bin/perl use strict; use HTML::TokeParser::Simple;
use constant SKIP => 0; use constant COPY => 1; my $new_folder = 'stripped/'; mkdir $new_folder unless -d $new_folder; die "Could not create dir $new_folder: $!" unless -d $new_folder; my $state = COPY; foreach my $doc ( glob("*.html") ) { print "Converting $doc\n"; my $new_file = "$new_folder$doc"; my $p = HTML::TokeParser::Simple->new( $doc ); open OUTFILE, "> $new_file" or die "Cannot open $new_file for writ +ing: $!"; while ( my $token = $p->get_token ) { if ( $token->is_start_tag ('script') ) { $state = SKIP; # stop copying if script } if ($token->is_end_tag ('script') ) { $state = COPY; # start copying again after script } elsif ( $state == SKIP ) { next; } elsif ( $state == COPY ) { print OUTFILE $token->as_is; } } close OUTFILE; }

--
Allolex


In reply to Re: strip JS-comments by allolex
in thread strip JS-comments by Skeeve

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.