Perl: Edit hyperlinks in nested tags that aren't on seperate lines

Posted by user305801 on Stack Overflow See other posts from Stack Overflow or by user305801
Published on 2010-03-31T08:13:41Z Indexed on 2010/03/31 8:53 UTC
Read the original article Hit count: 318

Filed under:
|
|

I have an interesting problem. I wrote the following perl script to recursively loop through a directory and in all html files for img/script/a tags do the following:

  • Convert the entire url to lowercase
  • Replace spaces and %20 with underscores

The script works great except when an image tag in wrapped with an anchor tag. Is there a way to modify the current script to also be able to manipulate the links for nested tags that are not on separate lines? Basically if I have <a href="..."><img src="..."></a> the script will only change the link in the anchor tag but skip the img tag.


#!/usr/bin/perl

use File::Find;

$input="/var/www/tecnew/";

sub process {
        if (-T and m/.+\.(htm|html)/i) {
                #print "htm/html: $_\n";

                open(FILE,"+<$_") or die "couldn't open file $!\n";
                $out = '';
                while(<FILE>) {
                        $cur_line = $_;
                        if($cur_line =~ m/<a.*>/i) {
                                print "cur_line (unaltered) $cur_line\n";
                                $cur_line =~ /(^.* href=\")(.+?)(\".*$)/i;
                                $beg = $1;
                                $link = html_clean($2);
                                $end = $3;
                                $cur_line = $beg.$link.$end;
                                print "cur_line (altered) $cur_line\n";

                        }
                        if($cur_line =~ m/(<img.*>|<script.*>)/i) {
                                print "cur_line (unaltered) $cur_line\n";
                                $cur_line =~ /(^.* src=\")(.+?)(\".*$)/i;
                                $beg = $1;
                                $link = html_clean($2);
                                $end = $3;
                                $cur_line = $beg.$link.$end;
                                print "cur_line (altered) $cur_line\n";
                        }
                        $out .= $cur_line;

                }
                seek(FILE, 0, 0) or die "can't seek to start of file: $!";
                print FILE $out or die "can't print to file: $1";
                truncate(FILE, tell(FILE)) or die "can't truncate file: $!";
                close(FILE) or die "can't close file: $!";
        } } find(\&process, $input);

sub html_clean {
        my($input_string) = @_;
        $input_string = lc($input_string);
        $input_string =~ s/%20|\s/_/g;
        return $input_string; 
}

© Stack Overflow or respective owner

Related posts about perl

Related posts about linux