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;

In reply to URL path shortening by nop

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.