aboutsummaryrefslogtreecommitdiff
path: root/ssg
diff options
context:
space:
mode:
authorMatias Linares <matiaslina@openmailbox.org>2016-02-24 04:14:46 -0300
committerMatias Linares <matiaslina@openmailbox.org>2016-02-24 04:14:46 -0300
commitf8bdb18fd92ee60370893cc670fa5094062dbcbd (patch)
treef8fb1ce634a213c80ceff2d1bdacb41d8db7da81 /ssg
parent9b21febff64925a894763cd12f09f1cb49dd3f56 (diff)
downloadssg-f8bdb18fd92ee60370893cc670fa5094062dbcbd.tar.gz
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 :)
Diffstat (limited to 'ssg')
-rwxr-xr-xssg77
1 files changed, 42 insertions, 35 deletions
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<<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;