package xm::navheaderhrefsfromindex;
use strict;
use xm::o;
"
interpret the input as an index.xml file that is supposed to
be converted to an index.html, and which has therefore some
href-attributes around a title-saying text.
Scan this index.xml, make a table in the order of the hrefs'n'items,
and open each references xml-file to attach a navheader with
hrefs for next,index,prev.
see xm::pfe::wordset2index and xm::pfe::words2index for the
preceding stage that generates xml-files and an index.xml file
which are each supposed to be converted to html.
"}
my $index = shift; my $dir = ".";
if (-d $index)
{
$dir = $index;
$index = "index.xml" ; }
my $xml = "xml";
if ($index =~ m{ (.*)\.(\w+)$ }x)
{
$xml = $2;
$index = $1;
}
my $F;
my $in;
$F = "$dir/$index.$xml";
return "" if not open F,"<$F";
$in = join("",<F>);
close F;
my $markup = "a";
if ($in =~ m{ <(\w[^\s<>]*)(?:\s[^<>]*)?\shref=\"[^<>\"]*\"[^<>]*>
((?:.(?!</?\1[\s>]))*.)
</\1(?:\s[^<>]*)?\shref(?:\s[^<>]*)?> }sx)
{
$markup = $1;
print STDERR ":",$markup,":";
}else{
print STDERR "warning: no xml-markup for hrefs found, using <a>";
}
my %nav;
my $i = 10000;
$in =~ s{ <($markup)(?:\s[^<>]*)?\shref=\"([^<>\"]*)\"[^<>]*>
((?:.(?!</?\1[\s>]))*.)
</\1(?:\s[^<>]*)?> }
{
$nav{"c".$i}{file} = $2;
$nav{"c".$i}{text} = $3;
$nav{"c".$i}{name} = $3;
$i++;
""
}sgex;
print STDERR scalar %nav,":";
$nav{a}{file} = "$index.html";
$nav{e}{file} = "$index.html";
$nav{a}{text} = " ";
$nav{e}{text} = " ";
$nav{a}{name} = "index";
$nav{e}{name} = "index";
# clean the names
my $k;
for $k (keys %nav)
{
$nav{$k}{name} =~ s{<[^<>]*>}{}sg;
$nav{$k}{name} =~ s{\<\>\"}{}sg;
$nav{$k}{name} =~ s{\s+}{ }sg;
$nav{$k}{name} =~ s{ ^\s }{}x;
$nav{$k}{name} =~ s{ \s$ }{}x;
$nav{$k}{text} =~ s{ ^\s }{}x;
$nav{$k}{text} =~ s{ \s$ }{}x;
}
### set the next/prev entries
$i = "a";
for $k (sort keys %nav)
{
$nav{$k}{prev} = $i;
$i = $k;
}
$i = "e";
for $k (reverse sort keys %nav)
{
$nav{$k}{next} = $i;
$i = $k;
}
### open each file and add the header
my $out = "";
for $k (sort keys %nav)
{
next if $k !~ /\d/; # but not the index-file itself
$F = $dir."/".$nav{$k}{file};
$F =~ s{ \.\w+$ } {".".$xml}sex;
next if not open F, "<$F";
$in = join ("",<F>);
close F;
next if $in =~ /<NAVHEADER>/; # do not do it twice
$out = "<NAVHEADER><NAVTITLE>".$nav{$k}{text}."</NAVTITLE>\n";
$out .= "<NAVPREV href=\"".$nav{ $nav{$k}{prev} }{file}."\">"
.$nav{ $nav{$k}{prev} }{name}."</NAVPREV href>\n";
$out .= "<NAVNEXT href=\"".$nav{ $nav{$k}{next} }{file}."\">"
.$nav{ $nav{$k}{next} }{name}."</NAVNEXT href>\n";
$out .= "</NAVHEADER><NAVBODY>\n";
$out .= $in;
$out .= "</NAVBODY>";
next if not open F, ">$F";
print STDERR $F,",";
print F $out;
close F;
}
return $dir."/".$index.".".$xml;
}
my $args = $_[0];
my $the;
my $out;
for $the (@$args)
{
$out .= makeheader($the);
$out .= "\n";
}
return $out;
}
return xm::o::args_command(@_,DESC); }
return DO(xm::o::args_command(@_,DESC)); }
1;