Solved

Absolute to Relative HREF Links

Posted on 1998-10-13
1
252 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…

820 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