#! /usr/bin/perl -w
# Author : Gautam Iyer <gautam@math.uchicago.edu>
# Created : Wed 24 Nov 2005 10:23:55 AM CST
# Modified : Sun 17 Sep 2006 10:33:51 PM PDT
# Licence : GNU General Programers Licence. See the file Licence for the
# exact licence.
package funroll::Directory;
# use base qw(funroll::Page);
use funroll::Page;
our @ISA=qw(funroll::Page);
use strict;
use Term::ANSIColor qw(:constants);
use Cwd;
use FindBin qw($RealBin);
use File::Basename;
sub new {
my $class = shift;
my $this = {};
# Do the initialisations we need here. SUPER::new init's too many vars
$this->{options} = {};
$this->{tokens} = {};
$this->{lists} = {};
$this->{shared} = {};
bless( $this, $class);
$this->init( @_) if( @_);
return $this;
}
sub funroll_page {
my $this = shift;
my (@files, @folders);
my (@fileref, @foldref, @broref);
my ($i, $filename);
# Brothers
# @broref = @_ if( @_);
# Essential defaults. This class will not work if these are unset.
my %defaults = (
# Output directories.
output_dir => './',
shared_outdir => "-", # Where to place shared output files by default.
# input_dir => "", # Where images / other input files should be
# img_outdir
base_dir => "$RealBin/", # Replacement for =. Base dir of the program
templates_dir => "=templates/", # Where template files are found
include_dir => "=include/", # Where to look if any default input file is not found
shared_indir => "=shared/", # Where to look for shared input files.
glob => '.*\(jpg\|mov\)',
exclude_dirs => '.*\(lowres\|thumbnails\|Icons\)',
config_file => '+dir.fuss',
pass_lists => 1,
cwd => getcwd() . '/',
norecurse => undef
);
# clean_values is bad, since it causes undef tokens to be forgotten. Causes problems when we recurse :)
$this->clean_values( \%defaults);
# Delete 'undef' values from my options
# foreach my $key (keys( %defaults)) {
# delete $this->{options}{$key}
# if( exists( $this->{options}{$key}) && $this->{options}{$key} eq 'undef');
# }
# Now merge in our options, and we're all set.
# merge_hash( $this->{options}, \%defaults, 1);
# Get list of files in current directory
@files = split( '\n',
`find $this->{options}{cwd} -maxdepth 1 -mindepth 1 -type f -iregex "$this->{options}{glob}" | sort`);
# If no folders are given, read the current directory.
if( !exists( $this->{lists}{folders})) {
@folders = split( '\n', `find $this->{options}{cwd} -maxdepth 1 -mindepth 1 -type d -not -iregex "$this->{options}{exclude_dirs}"`);
for( $i=0; $i < scalar(@folders); $i++) {
$foldref[$i]{name} = $folders[$i];
$foldref[$i]{name} =~ s/^(?:\.\/|$this->{options}{cwd})//;
}
} else {
@foldref = @{$this->{lists}{folders}};
}
# print STDERR "Glob: $this->{options}{glob}, Cwd: $this->{options}{cwd}\n";
# print STDERR BLUE, "Folders: ", join( ',', @folders), RESET, "\n";
# print STDERR "Files: ", join( ',', @files), "\n";
# Ignore empty directories. Seems like it's better to generate a "empty" index.html than ignore.
# return unless( @files || @folders || exists( $this->{lists}{folders}));
# Put folders and files in correct format for funroll::Page
for( $i=0; $i < scalar(@files); $i++) {
$fileref[$i]{filename} = $files[$i];
$fileref[$i]{filename} =~ s/^(?:\.\/|$this->{options}{cwd})//;
}
# Recurse directories
unless( exists( $this->{options}{norecurse} ) ) {
foreach my $folder (@foldref) {
my $name = $this->{options}{cwd} . $folder->{name};
my $page = funroll::Directory->new( $this->{options} );
print STDERR "Processing folder $name.\n";
$page->set_option( output_dir => "$this->{options}{output_dir}$folder->{name}/");
$page->set_option( cwd => $name . "/");
$page->set_tokens( $this->{tokens});
$page->set_tokens( {
parent => basename( substr( $this->{options}{cwd}, 0, -1)),
current=> $folder->{name}
});
$page->set_tokens( {desc=> $folder->{desc}}) if( exists($folder->{desc}));
$page->set_list( brothers => \@foldref) if( $#foldref);
$page->funroll_page();
}
}
# print RED, "Files in $this->{options}{cwd}: ";
# for( $i=0; $i < scalar( @fileref) && $i < 5; $i++) {
# print "$fileref[$i]{filename} ";
# }
# print RESET, "\n";
# foreach my $i (@foldref) {
# print STDERR "Folder: $i->{name}\n";
# }
$filename = $this->resolve_infile( $this->{options}{config_file}, $this->{options}{include_dir});
if( exists( $this->{options}{pass_lists}) || $filename =~ m/(?:^|\/)dir\.fuss$/ ) {
# Pass lists as options
$this->set_list( files => \@fileref) if( @fileref);
$this->set_list( folders => \@foldref) if( scalar(@foldref) && !exists( $this->{lists}{folders}));
$this->set_list( brothers => \@broref) if( @broref);
}
# TODO: If a config file exists for this directory, then use that instead.
&::parse_config_file( $filename, $this->{options}{cwd},
$this->{options}, $this->{tokens}, $this->{lists}, $this->{shared});
}
1;