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
@ -51,14 +52,17 @@ 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 $preview;
my $verbose; my $verbose;
GetOptions( GetOptions(
'preview' => \$preview,
'verbose' => \$verbose, 'verbose' => \$verbose,
) or pod2usage(1); ) or pod2usage(1);
pod2usage(1) unless @ARGV; pod2usage(1) unless @ARGV;
@ -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 {
@ -104,6 +108,7 @@ sub get_template_html (\%@) {
# #
return Template->new({ return Template->new({
INCLUDE_PATH => './templates', INCLUDE_PATH => './templates',
OUTPUT_PATH => './public_html',
EVAL_PERL => 1, EVAL_PERL => 1,
START_TAG => '<!--%', START_TAG => '<!--%',
END_TAG => '%-->', END_TAG => '%-->',
@ -117,13 +122,13 @@ sub get_template_html (\%@) {
} }
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;
if (!$preview) {
$html = "$page.html";
}
$tt->process($root_template, $config->{$page}, $html)
|| die $tt->error(), "\n"; || die $tt->error(), "\n";
} }