Add write to file functionality to the site-generator

Add preview option to display generated html in standard out. Default
output of generated html is now written to a file in the public_html
directory with the name of the page with a .html extension.
This commit is contained in:
Roan Horning 2022-07-03 17:14:58 -04:00
parent e9911f99f9
commit d0443f0a7d
No known key found for this signature in database
GPG Key ID: 6E07059BD168E395

View File

@ -2,12 +2,13 @@
=head1 NAME
siteGenerator - HPR Site Generator
site-generator - HPR Site Generator
=head1 SYNOPSIS
siteGenerator [OPTION]... PAGE...
site-generator [OPTION]... PAGE...
-p, --preview print generated pages to standard out
-v, --verbose use verbose mode
--help print this help message
@ -40,7 +41,7 @@ This is a site generator for the Hacker Public Radio website based upon the Perl
=head1 AUTHOR
Roan Horning <roan.horning@no-spam.gmail.com>
Roan Horning <roan.horning@no-spam.gmail.com>
=cut
@ -51,18 +52,21 @@ use Getopt::Long qw(:config auto_help);
use Pod::Usage;
use Config::Std;
use Template;
use Data::Dumper;
exit main();
sub main {
# Argument parsing
my $verbose;
GetOptions(
'verbose' => \$verbose,
) or pod2usage(1);
pod2usage(1) unless @ARGV;
my (@pages) = @ARGV;
# Argument parsing
my $preview;
my $verbose;
GetOptions(
'preview' => \$preview,
'verbose' => \$verbose,
) or pod2usage(1);
pod2usage(1) unless @ARGV;
my (@pages) = @ARGV;
# Set flag indicating whether or not to generate all pages.
# The flag is set to true if the special argument ALL is
@ -86,7 +90,7 @@ sub main {
if (exists($config{$page})) {
verbose ($verbose, "Generating page: $page");
generate_page($tt, $config{root_template}{content}, $config{$page}{navigation}, $config{$page}{content});
generate_page($tt, $config{root_template}{content}, \%config, $page, $preview);
}
else {
@ -103,28 +107,29 @@ sub get_template_html (\%@) {
# HTML comments to make the template file valid HTML.
#
return Template->new({
INCLUDE_PATH => './templates',
EVAL_PERL => 1,
START_TAG => '<!--%',
END_TAG => '%-->',
CONSTANTS => {
driver => $_[0]{driver},
user => $_[0]{user},
password => $_[0]{password},
}
}) || die $Template::ERROR, "\n";
INCLUDE_PATH => './templates',
OUTPUT_PATH => './public_html',
EVAL_PERL => 1,
START_TAG => '<!--%',
END_TAG => '%-->',
CONSTANTS => {
driver => $_[0]{driver},
user => $_[0]{user},
password => $_[0]{password},
}
}) || die $Template::ERROR, "\n";
}
sub generate_page {
my ($tt, $page, $navigation, $content) = @_;
my $tt_vars = {
navigation => $navigation,
content => $content
};
my ($tt, $root_template, $config, $page, $preview) = @_;
$tt->process($page, $tt_vars)
|| die $tt->error(), "\n";
my $html;
if (!$preview) {
$html = "$page.html";
}
$tt->process($root_template, $config->{$page}, $html)
|| die $tt->error(), "\n";
}