diff options
Diffstat (limited to 'ssg')
-rwxr-xr-x | ssg | 77 |
1 files changed, 42 insertions, 35 deletions
@@ -7,15 +7,16 @@ use Path::Class; use Getopt::Std; # Root in directory. -my $dir; +my $indir; +my $outdir; my $VERSION = "v0.1"; sub help { print<<EOF -$0 [switches] +$0 [switches] input-directory -v print version and exits -h print this help and exits - -d set the input directory (by default in/) + -o set the output directory (by default site.static/) EOF } @@ -44,7 +45,7 @@ sub markdown { sub generate_page { my $file = $_[0]; - my $sidebar = transverse($dir, $file); + my $sidebar = transverse($indir, $file); my $styles = styles; my $body = markdown $file; my $page = <<EOF @@ -97,7 +98,7 @@ sub generate_link { } # remove the input directory. - $path =~ s/^$dir//g; + $path =~ s/^$indir//g; if($file->is_dir()) { $str = "$str/" if $file->is_dir(); } else { @@ -115,8 +116,9 @@ sub generate_link { } sub transverse { - my ($directory, $file_needed) = @_; + my ($dirname, $file_needed) = @_; my $retval = ""; + my $directory = dir($dirname); $retval = "<ul>"; while(my $file = $directory->next) { @@ -147,21 +149,24 @@ sub transverse { } sub run_on { - my $mddir = $_[0] || dir($dir); - remove_tree $mddir; + my $dirname = $_[0] or die "Cannot stat input directory"; + my $mddir = dir($dirname); while(my $file = $mddir->next) { my $outfile = $file->stringify; - # Some conditions - next if $outfile =~ /(\.|\.\.)$/; + # Some conditions: + # 1) next if file is . or .. + # 2) next if outfile = directory with markdown + # 3) die if the output file is in the input directory. next if $outfile eq $mddir; - - $outfile =~ s/$dir\//out\//g; - !($outfile =~ m/^$dir/) or die "$outfile =~ m/$dir/"; + next if $outfile =~ m/(\.|\.\.)($|\/)/; + $outfile =~ s/^$indir\//$outdir\//g; + $outfile !~ m/^$indir/ or die "trying to write in the same directory"; if($file->is_dir()) { # make a directory + print STDERR "calling on directory $outfile\n"; make_path $outfile; run_on($file); next; @@ -171,34 +176,36 @@ sub run_on { print STDERR "* writing: $outfile\n"; my $contents = generate_page $file; - open FD, ">$outfile"; + open FD, ">$outfile" or die " [ERROR] cannot open > $outfile: $!";; - print FD $contents; + print FD $contents or die " [ERROR] Cannot write $outfile: $!"; - close FD; + close FD or die " [ERROR] Cannot close $outfile: $!"; } } -sub main { - my %opts = ( - 'v' => 0, - 'h' => 0, - 'd' => "in" - ); - getopts('vhd:', \%opts); - - if($opts{h}) { - help; - exit 0; - } elsif($opts{v}) { - version; - exit 0; - } elsif($opts{d}) { - $dir = dir $opts{d}; - } +my %opts = ( + 'v' => 0, + 'h' => 0, + 'o' => "site.static" +); +getopts('vho:', \%opts); +$indir = shift @ARGV; + +if($opts{h}) { + help; + exit 0; +} elsif($opts{v}) { + version; + exit 0; +} - run_on dir($dir); +if(-d $opts{o}) { + print STDERR "removing $opts{o}\n"; + remove_tree($opts{o}); } -main @ARGV; +mkdir $opts{o}; +$outdir = $opts{o}; +run_on $indir; |