From d0443f0a7dabe3b51893f9868d7e8811b6fd9fa4 Mon Sep 17 00:00:00 2001 From: Roan Horning Date: Sun, 3 Jul 2022 17:14:58 -0400 Subject: [PATCH] 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. --- site-generator | 61 +++++++++++++++++++++++++++----------------------- 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/site-generator b/site-generator index a5559f9..da1351f 100755 --- a/site-generator +++ b/site-generator @@ -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 =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 => '', - 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 => '', + 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"; }