Solved

Absolute to Relative HREF Links

Posted on 1998-10-13
1
235 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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

728 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now