why did I do this
This commit is contained in:
parent
551a80abbe
commit
fa2870c4b3
1 changed files with 110 additions and 69 deletions
167
staticgen.pl
Normal file → Executable file
167
staticgen.pl
Normal file → Executable file
|
@ -1,92 +1,133 @@
|
|||
#!/usr/bin/env perl
|
||||
#!/usr/bin/perl
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
use Carp;
|
||||
#use HTML::Template;
|
||||
|
||||
$SIG{__WARN__} = sub { die @_; };
|
||||
|
||||
sub DisplayUsage
|
||||
{
|
||||
my $options_ref = shift;
|
||||
my @options = @$options_ref;
|
||||
|
||||
=begin comment
|
||||
print "Usage:\n";
|
||||
for my $opt (@options)
|
||||
{
|
||||
my $opt_str = " " . (defined $opt->{sopt} ? "-$opt->{sopt}, " : " ");
|
||||
$opt_str .= (defined $opt->{lopt} ? "--$opt->{lopt}" : "");
|
||||
|
||||
HOW THE ARGUMENTS ARE PROBABLY GONNA LOOK:
|
||||
if (lc $opt->{arg} eq "required")
|
||||
{
|
||||
$opt_str .= "=<value>";
|
||||
}
|
||||
elsif (lc $opt->{arg} eq "optional")
|
||||
{
|
||||
$opt_str .= "[=<value>]";
|
||||
}
|
||||
|
||||
--new-post
|
||||
--build=(all|modified)
|
||||
--publish
|
||||
[...]
|
||||
print $opt_str . "\n " . ($opt->{desc} // "") . "\n\n";
|
||||
}
|
||||
|
||||
TODO:
|
||||
exit 0;
|
||||
}
|
||||
|
||||
[ ] everything
|
||||
sub HandleArgs
|
||||
{
|
||||
my ($args_ref, $options_ref) = @_;
|
||||
|
||||
=end comment
|
||||
my @args = @$args_ref;
|
||||
my @options = @$options_ref;
|
||||
|
||||
=cut
|
||||
for my $arg (@args)
|
||||
{
|
||||
if ($arg =~ m@^(-(?<sopts>\w{2,})|-(?<sopt>\w)|-+(?<lopt>[\w-]+))(=(?<optarg>.+))?\z@i)
|
||||
{
|
||||
my @soptlist;
|
||||
@soptlist = split //, $+{sopts} if defined $+{sopts};
|
||||
|
||||
my $HELP_TEXT = << "END_HELP_TEXT";
|
||||
Usage: staticgen.pl [options]
|
||||
my $optarg;
|
||||
my $handled = 1;
|
||||
|
||||
Put the stuff here when arguments are actually implemented.
|
||||
END_HELP_TEXT
|
||||
DisplayUsage($options_ref)
|
||||
if ((defined $+{sopt} and $+{sopt} eq "h") or
|
||||
(defined $+{lopt} and $+{lopt} eq "help"));
|
||||
|
||||
for my $opt (@options)
|
||||
{
|
||||
die "No option name to handle!"
|
||||
unless defined $opt->{lopt} or defined $opt->{sopt};
|
||||
|
||||
if (defined $opt->{sopt})
|
||||
{
|
||||
if (grep { $_ eq $opt->{sopt} } @soptlist)
|
||||
{
|
||||
die "Option -$opt->{sopt} cannot be in a list as it requires an argument!\n"
|
||||
if $opt->{arg} eq "required";
|
||||
|
||||
sub LoadConfig
|
||||
$opt->{handler}();
|
||||
}
|
||||
elsif (defined $+{sopt} and $+{sopt} // "" eq $opt->{sopt})
|
||||
{
|
||||
$optarg = $+{optarg} // ();
|
||||
$opt->{handler}($optarg);
|
||||
}
|
||||
}
|
||||
|
||||
if (defined $opt->{lopt} and $+{lopt} // "" eq $opt->{lopt})
|
||||
{
|
||||
$optarg = $+{optarg} // ();
|
||||
$opt->{handler}($optarg);
|
||||
}
|
||||
}
|
||||
|
||||
die "Unrecognized option: $arg\n"
|
||||
unless $handled;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sub HandleConf
|
||||
{
|
||||
my $filename = shift;
|
||||
open my $cfh, "<$filename" or die "Couldn't open file!";
|
||||
print "Loading configuration from: $filename\n";
|
||||
}
|
||||
|
||||
while (!eof($cfh))
|
||||
my @optArray = (
|
||||
{
|
||||
my $l = <$cfh>;
|
||||
# ^_^
|
||||
}
|
||||
|
||||
close $cfh;
|
||||
}
|
||||
lopt => "config",
|
||||
sopt => "c",
|
||||
desc => "Loads a custom config file at a specified path.",
|
||||
arg => "required",
|
||||
handler => \&HandleConf
|
||||
},
|
||||
{
|
||||
lopt => "new-post",
|
||||
sopt => "n",
|
||||
desc => "Create a new post.",
|
||||
arg => "none",
|
||||
handler => sub { print "do something\n"; }
|
||||
},
|
||||
{
|
||||
lopt => "build",
|
||||
sopt => "b",
|
||||
desc => "Build all posts and templates into HTML.",
|
||||
arg => "optional",
|
||||
handler => sub { print "do something\n"; }
|
||||
},
|
||||
{
|
||||
lopt => "publish",
|
||||
sopt => "p",
|
||||
desc => "Publish built files.",
|
||||
arg => "none",
|
||||
handler => sub { print "do something\n"; }
|
||||
},
|
||||
);
|
||||
|
||||
sub Main
|
||||
{
|
||||
for my $arg (@ARGV)
|
||||
{
|
||||
if ($arg =~ m@^(-n|-+new-post)\z@i)
|
||||
{
|
||||
# something
|
||||
next;
|
||||
}
|
||||
|
||||
if ($arg =~ m@^(-b|-+build)(=(?<bopt>.+))?\z@i)
|
||||
{
|
||||
if (defined $+{bopt})
|
||||
{
|
||||
if (lc $+{bopt} eq "all") { next; }
|
||||
elsif (lc $+{bopt} eq "modified") { next; }
|
||||
else { die "Invalid option for --build!"; }
|
||||
}
|
||||
else
|
||||
{
|
||||
next;
|
||||
}
|
||||
}
|
||||
|
||||
if ($arg =~ m@^-+config=(?<conf>.+)\z@i)
|
||||
{
|
||||
die "Specified config file doesn't exist!"
|
||||
unless -f $+{conf};
|
||||
|
||||
LoadConfig($+{conf});
|
||||
next;
|
||||
}
|
||||
|
||||
if ($arg =~ m@^(-p|-+publish)\z@i)
|
||||
{
|
||||
# something
|
||||
next;
|
||||
}
|
||||
|
||||
die $HELP_TEXT if ($arg =~ m@^(-h|-+help)\z@i);
|
||||
}
|
||||
HandleArgs(\@ARGV, \@optArray);
|
||||
}
|
||||
|
||||
&Main();
|
||||
|
|
Loading…
Add table
Reference in a new issue