Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stripping everything except html tags using perl

Tags:

html

regex

perl

I have been searching for a way to strip everything out of an html document leaving ONLY the html tags. Is anyone aware of a method for this? I have experience with many perl modules and have searched this site thoroughly.

I want to pass html as a string to my perl script and remove everything except the tags. Here is an example:

Incoming:

<!doctype html>
<html>
<head>
<title>Example Domain</title>

<meta charset="utf-8" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style type="text/css">
body {
    background-color: #f0f0f2;
    margin: 0;
    padding: 0;
    font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;

}
div {
    width: 600px;
    margin: 5em auto;
    padding: 50px;
    background-color: #fff;
    border-radius: 1em;
}
a:link, a:visited {
    color: #38488f;
    text-decoration: none;
}
@media (max-width: 700px) {
    body {
        background-color: #fff;
    }
    div {
        width: auto;
        margin: 0 auto;
        border-radius: 0;
        padding: 1em;
    }
}
</style>    
</head>

<body>
<div>
    website content ....
</div>
</body>
</html>

Becomes:

<html><head><title></title><meta><meta><meta><style></style></head><body><div><h1></h1>       <p></p><p><a></a></p></div></body></html>
like image 940
user2421267 Avatar asked May 26 '13 00:05

user2421267


1 Answers

#!/usr/bin/perl --
use strict;
use warnings;
use XML::Twig;

Main( @ARGV );
exit( 0 );

sub Main {
    if( @_ ){
        nothing_but_tags("$_") for @_;
    } else {
        nothing_but_tags(q{<NoTe
KunG="FoO"
ChOp="SuEy"> 
NoteKungFo0Ch0pSuEy
<To KunG="FoO">ToKungFo0 
<Person KunG="FoO">Satan</Person>
</To>
<Beef KunG="FoO"> BeefKunGFoO <SaUsAGe KunG="FoO">is Tasty
</SaUsAGe>
</Beef>
</NoTe>},
        );
    }
}

sub nothing_but_tags
{
    my( $input, %opt ) = @_;

    $opt{pretty_print}  ||= 'indented' ;

    my $t = XML::Twig->new(
        %opt,
        force_end_tag_handlers_usage => 1,
        start_tag_handlers => {
            _all_ =>  sub {
                if( $_->has_atts ){
                    $_->set_atts ({});
                }
                return;
            },
        },
        end_tag_handlers => { _all_ =>  sub { $_->flush; return }, },
        char_handler => sub { '' },
    );
    $t->xparse( $_[0] );
    $t->flush();
    ();
}
__END__
<NoTe>
  <To>
    <Person></Person>
  </To>
  <Beef>
    <SaUsAGe></SaUsAGe>
  </Beef>
</NoTe>
like image 74
optional Avatar answered Sep 30 '22 05:09

optional