#!/usr/bin/perl
# ylink.pl - create elements from directory structure to make it
# easier to browse big document hierarchies
# 1.0.0 - 2004-07-02 - Morten Wulff
use strict;
use warnings;
$|++;
use File::Find;
# config ----------------------------------------------------------------------
my @input_dirs = qw(
d:/projekter/psyke.org/source/main/about
d:/projekter/psyke.org/source/main/articles
d:/projekter/psyke.org/source/main/bookstore
d:/projekter/psyke.org/source/main/coping
d:/projekter/psyke.org/source/main/faqs
d:/projekter/psyke.org/source/main/personal
d:/projekter/psyke.org/source/main/pictures
d:/projekter/psyke.org/source/main/poetry
);
# d:/projekter/psyke.org/source/mirror/download
# d:/projekter/psyke.org/source/mirror/forum
# d:/projekter/psyke.org/source/mirror/pictures
my @exclude = qw(^_ img \.tmpl$ ~$);
my $link_file = 'link-browse.tmpl';
my $link_file_prev = 'nav-trail-prev.tmpl';
my $link_file_next = 'nav-trail-next.tmpl';
# init ------------------------------------------------------------------------
my $depth = 0;
my @paths = ();
# main ------------------------------------------------------------------------
foreach my $input_dir (@input_dirs) {
$depth = 0;
@paths = ();
print "$input_dir\n";
find(
{
preprocess => \&preprocess,
wanted => \&wanted,
postprocess => \&postprocess
},
$input_dir
);
foreach my $path ( @paths ) {
$path->{name} =~ s/$input_dir//;
$path->{name} =~ s/\/$//;
$path->{name} =~ s/^\///;
}
foreach my $i ( 0 .. $#paths ) {
my $rel_next = $i < $#paths
? '../' x $paths[$i]->{depth} . $paths[$i+1]->{name}
: '';
my $rel_prev = $i > 0
? '../' x $paths[$i]->{depth} . $paths[$i-1]->{name}
: '';
my $file = join '/', $input_dir, "$paths[$i]->{name}", $link_file;
open(OUT, ">$file") or die "Can't open file '$file': $!";
print OUT "\t \n" if $rel_prev ne '';
print OUT "\t " if $rel_next ne '';
close(OUT);
$rel_prev .= '/' unless $rel_prev =~ /\/$/;
$rel_next .= '/' unless $rel_next =~ /\/$/;
$file = join '/', $input_dir, "$paths[$i]->{name}", $link_file_prev;
open(OUT, ">$file") or die "Can't open file '$file': $!";
if ($rel_prev ne '') {
print OUT "« Prev ";
}
else {
print OUT '';
}
close(OUT);
$file = join '/', $input_dir, "$paths[$i]->{name}", $link_file_next;
open(OUT, ">$file") or die "Can't open file '$file': $!";
if ($rel_next ne '') {
print OUT "Next » ";
}
else {
print OUT '';
}
close(OUT);
}
}
# subs ------------------------------------------------------------------------
sub preprocess {
my @files = sort { $a cmp $b } @_;
my $dir = $File::Find::dir;
### remove unwanted elements from the file list
@files = grep {
my $file = $_;
my $path = join '/', $dir, $file;
!/^\.{1,2}/ and
!grep {$file =~ /$_/ or $path =~ /$_/} @exclude
} @files;
$depth++;
return reverse @files;
}
sub wanted {
return unless -d;
push @paths, { name => $File::Find::name, depth => $depth };
}
sub postprocess {
$depth--;
}