Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

converting absolute to relative links

by coolmichael (Deacon)
on Feb 03, 2001 at 10:10 UTC ( #56206=perlquestion: print w/replies, xml ) Need Help??

coolmichael has asked for the wisdom of the Perl Monks concerning the following question:

I have a bunch of web pages that need to change directory. The problem is that some of the links and image tags are absolute

img src="myserver.com/images/img.jpg

and don't work anymore after moving the files. One solution would be to not move the images, but part of the goal is move the entire site to a new domain. I'm sure there must be a wheel for this one invented already, but I can't seem to find anything for it in supersearch, or cpan, or categorized q&a. Maybe I'm looking for the wrong thing.

Any help would be greatfully appreciated.

Michael.

Replies are listed 'Best First'.
Re: converting absolute to relative links
by repson (Chaplain) on Feb 03, 2001 at 10:52 UTC
    The main wheels I would suggest would be HTML::Parser or any of several others in the HTML:: tree, and URI.

    use URI; use HTML::Parser; my $parse = new HTML::Parser( default_h => [ sub {print shift}, 'text' + ], start_h => [ sub { my ($tag, $attr, $origtext) = @_; if (($tag eq 'img') and ($attr->{src} =~ m#^(?:http://)?myserver\. +#)) { $attr->{img} = URI->new($attr->{img})->abs('http://myserver.com' +); print "<$tag " . join (' ', map {$_.'="' . $attr->{$_} . '"'} ke +ys %$attr) . '>'; } else { print $origtext; } }, "tagname, attr, text"] ); $parse->parse(join('',<>));
    However this code is untested and may well need some work. It currently would take input on STDIN and produce results on STDOUT.
Re: converting absolute to relative links
by ColonelPanic (Friar) on Feb 03, 2001 at 20:09 UTC
    If all the HTML pages are in the base directory, a single line like below would do the trick:
    s|(img src=")myserver\.com/|$1|g; before: img src="blahblah.com/images/pic.gif" after: img src="images/pic.gif"
    Just run it on each file, and it should be good to go.

    When's the last time you used duct tape on a duct? --Larry Wall
Re: converting absolute to relative links
by belize (Deacon) on Feb 03, 2001 at 21:53 UTC
    Maybe I am missing something, but why won't a simple "Find and Replace" within a text editor?

    I use BBEdit on a Mac for coding, and it is as simple as finding:

    src="myserver.com/images/

    and replacing with:

    src="../images/

    or whatever is the correct path. You can perfoem this across multiple files/directories with specific suffixes so that you can zero in on the exact files that need the change.

    I don't code on a PC, but I am sure that there must be as powerful a search and replace function on one of the text/code editors.

      A text editor substitution works well when you have 1-5 files. Suppose that the monk in question is dealing with a webserver that serves 500 documents with this problem. Perl /is/ your friend. (Of course, this brings about the template argument, but we'll save that for another day.. =])

      -marius
        I am speaking as a relative newcomer to Perl. So with all respect I can say that I have used BBEdit to do a "Find and Replace" on over 100 files in multiple directories. It is fast (~5 files a second), accurate on changes to a small number of characters (15-20 charaters at a time), and will provide a summary of changes as well as automatically save or allow you to manually save.

        By the way, I have no conection to BBEdit in any shape or form.

Re: converting absolute to relative links
by dkubb (Deacon) on Feb 05, 2001 at 02:51 UTC
      That was the exact wheel I was looking for. Thank you. If I was anywhere near you, I'd buy you a beer.

      Thanks for the wheel.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://56206]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (12)
As of 2019-07-16 16:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?