Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 275
  • Last Modified:

Absolute to Relative HREF Links

Anyone know of a Perl script (or could write one) that will sift throught all the pages in a web site and change absolute hrefs (ie http://www.mysite.com/index.htm) to relative hrefs (ie ../../index.htm or ../index.htm etc).

Many thanks for any help anyone can give me.
0
balabaster
Asked:
balabaster
1 Solution
 
b2piCommented:
Tom Christiansen has one, reltree.pl, at
http://www.perl.com/CPAN-local/authors/ID/TOMC/scripts/reltree.gz

Since it's small, it's right here:

use File::Find;
use URI::URL;

$DIR = shift || die "usage: $0 base [start]\n";
die "missing $DIR" unless -d $DIR;

$START = shift || $DIR;
die "missing $START" unless -d $START;

find ( \&fixup, $START );

sub fixup {

    return unless /.html$/;
    my $filename = $File::Find::name;
    my $BASE = $filename;
    $BASE =~ s,[^/]+$,,;
    die "$file exists " if -e "$filename.orig";
    local @ARGV = $filename;
    local $^I = ".orig";
    warn "@ARGV\n";

    while (<>) {
        s{
            (
                (HREF \s* = \s*)
                (['"])
                ($DIR.*?)
                \3
            )
        }{
            my ($all, $first, $quote, $file) = ($1, $2, $3, $4);
            if ( index($file, $DIR) == 0 ) {
                my $rel = url("file:$file", "file:$BASE")->rel();
                $rel =~ s/#$//;
                $first . $quote . $rel . $quote;
            } else {
                $all;
            }
        }sgex;

    } continue {
        print;
    }

}

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now