Solved

Absolute to Relative HREF Links

Posted on 1998-10-13
1
265 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Six Sigma Control Plans

623 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