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