[go: up one dir, main page]

Menu

[r1]: / trunk / funroll / Directory.pm  Maximize  Restore  History

Download this file

154 lines (122 with data), 5.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#! /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;