Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

URL path shortening

by nop (Hermit)
on May 23, 2002 at 21:17 UTC ( [id://168899]=perlquestion: print w/replies, xml ) Need Help??

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

Hi --

I have an app that uses TT2 to write a slew of static HTML files, many which link to one another. I've organized the resulting subsite into a series of directories. To keep my filenames straight, I've put all my name code in one function (see ugly code below).

The routine can return a real filename (as TT2 needs a name to write a real physical file), or can return a URL (as the HTML needs URLs, not filenames). My code works fine, but only deals with absolute URLs.

I'd like to modify my routine to return relative URLs. This would have 2 advantages: it'd save space (many files have many links); and it'd make the whole project easily relocatable. (The only URL "shortening" recoding I've done so far is chop off everything but the filename if the requesting and requested document live in the same directory.

Can anyone point me towards a module or code that will help with URL shortening?

For example, if a file living at
 http://blah.com/html/report/today.htm
wants to reference
 http://blah.com/html/report/yesterday.htm
it can do so with the relative
<a href="yesterday.htm"> yesterday </a>
link. That case is easy, but how about if
 http://blah.com/html/report/today.htm
wants to reference
 http://blah.com/html/details/fun.htm?
I'd like to be able to return a relative link like this
<a href="../details/fun.htm"> fun </a>.

Anyone have any suggestions? Didn't see anything on perlmonks or CPAN.

Thanks -- nop

please excuse this ugly code...
it will become OO in the next version,
getting rid of the horrid if elsif elsif stuff

package FileName; use FindBin; use lib "$FindBin::Bin/modules"; use Common; use Data::Dumper; use strict; sub name { my ($type, $relativeto, $attrib) = @_; $attrib = {} if !defined($attrib); die 'attrib must be hash' unless ref($attrib) eq 'HASH'; my %a = %$attrib; my $fname = ''; if ($type eq 'root') { $fname = $ENV{HTMDIR} . "/dash.htm"; } elsif ($type eq 'graph') { $a{biz} = 'ALL' if $a{biz} eq '*'; $fname = $ENV{HTMDIR} . '/graphs/graph-' . $a{stat} . "-" . $a{biz} . ".png"; } elsif ($type eq 'statbizchan') { $a{biz} = 'ALL' if $a{biz} eq '*'; $a{chan} = 'ALL' if $a{chan} eq '*'; $fname = $ENV{HTMDIR} . '/details/stat-' . $a{stat} .'-' . $a{biz} . "-" . $a{chan} . ".htm"; } elsif ($type eq 'statbizchandata') { $a{biz} = 'ALL' if $a{biz} eq '*'; $a{chan} = 'ALL' if $a{chan} eq '*'; $fname = $ENV{HTMDIR} . '/details/data-' . $a{stat} . "-" . $a{biz} . "-" . $a{chan} . ".htm"; } elsif ($type eq 'sql') { $fname = $ENV{HTMDIR} . '/details/sql-' . $a{stat} . ".htm"; } elsif ($type eq 'week') { $fname = $ENV{HTMDIR} . "/reports/" . $a{nickname} . "-" . $a{baseday} . ".htm"; } else { die "unknown type=$type"; } die 'fname not set' . Dumper(\@_) unless $fname; # wants it as a filename return $fname if ($relativeto eq ''); # wants it as a URL relative to filename $relativeurl debug3 "relto=$relativeto\n"; $relativeto = &asURL($relativeto); # make it a URL debug3 "relto=$relativeto\n"; debug3 "fname=$fname\n"; $fname = &asURL($fname); # make it a URL debug3 "fname=$fname\n"; # only want the directory of base $relativeto =~ s/[^\/]+$//i; debug3 "relto=$relativeto\n"; # now knock this off off $fname $fname =~ s/$relativeto//; debug3 "returning fname=$fname\n"; return $fname; } sub asURL { my ($fname) = @_; $fname =~ s/$ENV{HTMDIR}/$ENV{URLPATH}/i; return $fname; } 1;

Replies are listed 'Best First'.
•Re: URL path shortening
by merlyn (Sage) on May 24, 2002 at 00:25 UTC
Re: URL path shortening
by grinder (Bishop) on May 23, 2002 at 21:44 UTC
    What you need is the URI module. Funny thing is, I said exactly the same thing to someone else not more than two days ago in this thread: Using HTML templates.

    As for your code, I can't decide whether it's clunky or not... the formatting make it hard to understand what's going on. Perltidy might come in handy for laying out your code in a more orthodox manner :) It looks like you might want a helper sub to factor out how you build your path components, but it hurts my brain to look at this for too long.


    print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u'
Re: URL path shortening
by nop (Hermit) on May 24, 2002 at 02:27 UTC
    Somehow I missed ever learning about URI::URL... thanks for the info!  URI::URL->new($ref)->rel($base)
    :)
    nop

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2024-04-23 17:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found