Solved

Absolute to Relative HREF Links

Posted on 1998-10-13
1
246 Views
Last Modified: 2010-03-05
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
Comment
Question by:balabaster
1 Comment
 
LVL 5

Accepted Solution

by:
b2pi earned 50 total points
ID: 1205323
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

774 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question