From f8bdb18fd92ee60370893cc670fa5094062dbcbd Mon Sep 17 00:00:00 2001 From: Matias Linares Date: Wed, 24 Feb 2016 04:14:46 -0300 Subject: Add options for the output directory. This commit changes the command line changing the -d flag to -o, and the input directory is passed on $ARGV[1]. Also update readme :) --- README.md | 32 +++++++++++++++++++++++++- ssg | 77 ++++++++++++++++++++++++++++++++++----------------------------- 2 files changed, 73 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index aaded25..87492f9 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,33 @@ # SSG -Static site generator written in perl. +Minimal **S**tatic **S**ite **G**enerator written in perl. This software was +inspired in [sw](https://github.com/jroimartin/sw), a suckless webframework. + +## Installation + +There's no installation method for now. The *ssh* and *styles.css* should be on the same +directory, and the result directory will be there as well. + +## Usage + +ssg [switches] input-directory + +where switches can be: + +* -v print version and exits +* -h print this help and exits +* -o set the input directory (by default site.static/) + +By default the output directory is *./site.static/* (on the same directory as +this program + +## Generation + upload + +You can automate all the process creating the following script. + + ./ssg -o mysite input-dir + rsync -avz mysite/ your.site:/path/to/www + +## Author + +Matias Linares diff --git a/ssg b/ssg index 5b522fc..e5b0476 100755 --- a/ssg +++ b/ssg @@ -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<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 = "
    "; 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; -- cgit v1.2.3-70-g09d2