Sherm said:
[..]
That's a *very* poor occasion on which to attempt a learning exercise.
When deadlines are tight and looming, stick with the tools you know.
True enough! I found a simple way to produce what is needed by importing an
html list from a text file and replacing the relevant parts by regex based
on the current URL (filename) in a series of if-blocks and returning the
modified output, which is nearly like typing each html page out separately:
--------- menu.pl -------
#!/usr/bin/perl -w
use warnings;
use strict;
# used for CGI
$this:
age = $ENV{DOCUMENT_NAME} || 'undefined';
# test on command line, eg. 'menu.pl page_2.2.html'
if ($this:
age eq 'undefined'){
$this:
age = "@ARGV";
}
print "Content-Type: text/html\n\n";
my $filename = "menu.txt" ;
my $filedata ;
open my $fh, '<', $filename
or die "open $filename: $!" ;
{
local $/ ;
$filedata = <$fh> ;
}
close $fh
or die "close $filename: $!" ;
$_ = $filedata; # assign filedata to $_
my $complete_string = $filedata;
if ($this:
age eq "page_1.1.html"){
$complete_string =~ s/<a
href="page_1.1.html">page_1.1.html<\/a>/page_1.1.html/;
}
if ($this:
age eq "page_2.1.html"){
$complete_string =~ s/<a
href="page_2.1.html">page_2.1.html<\/a>/page_2.1.html/;
$complete_string =~ s/page_1.1.html"/page_1.1.html" class="node"/;
}
if ($this:
age eq "page_2.2.html"){
$complete_string =~ s/<a
href="page_2.2.html">page_2.2.html<\/a>/page_2.2.html/;
$complete_string =~ s/page_1.1.html"/page_1.1.html" class="node"/;
}
if ($this:
age eq "page_3.1.html"){
$complete_string =~ s/<a
href="page_3.1.html">page_3.1.html<\/a>/page_3.1.html/;
$complete_string =~ s/page_1.1.html"/page_1.1.html" class="node"/;
$complete_string =~ s/page_2.2.html"/page_2.2.html" class="node"/;
}
if ($this:
age eq "page_3.2.html"){
$complete_string =~ s/<a
href="page_3.2.html">page_3.2.html<\/a>/page_3.2.html/;
$complete_string =~ s/page_2.2.html"/page_2.2.html" class="node"/;
$complete_string =~ s/page_1.1.html"/page_1.1.html" class="node"/;
}
if ($this:
age eq "page_2.3.html"){
$complete_string =~ s/<a
href="page_2.3.html">page_2.3.html<\/a>/page_2.3.html/;
$complete_string =~ s/page_1.1.html"/page_1.1.html" class="node"/;
}
print $complete_string;
------
The unmodified list in menu.txt looks as follows:
<style type="text/css">
..node {
font-style: italic;
}
</style>
<ul>
<li><a href="page_1.1.html">page_1.1.html</a>
<ul>
<li><a href="page_2.1.html">page_2.1.html</a></li>
<li><a href="page_2.2.html">page_2.2.html</a>
<ul>
<li><a href="page_3.1.html">page_3.1.html</a></li>
<li><a href="page_3.2.html">page_3.2.html</a></li>
</ul>
</li>
<li><a href="page_2.3.html">page_2.3.html</a></li>
</ul>
</li>
</ul>
So the example perl code is for only six pages, just imagine what it will
look like with 50 or more pages and what kind of hassle that can be to
maintain. In other words, the code could surely win the ugliest perl script
posted here contest. That said, it actually does what is needed in a
roundabout way and given the facts that changes won't be very frequent and
that it's only an html-preprocessing procedure, since static versions will
be served online, it doesn't need to be easy to maintain or even system
resource friendly. In other words, that the code is bloated and a bit hard
to manage won't affect the end result. Nevertheless, this is my primitive
solution by the tools I happen to know, which all they do is to produce
some modified output based on the current URL (if one is matching) and
adding the css html class to some nodes at the right levels for each link.
Any ideas how to improve the code are of course more than welcome, although
I guess the only *real* improvement would mean a nested data structure,
recursion and complex references etc., although that may be easy to most of
you here, I must avoid that for now due to my inability to bugfix such code.
Thanks for any feedback!
Tuxedo