From 75cd3e3e8521d3f996379b1080e4463f926f0956 Mon Sep 17 00:00:00 2001 From: Roan Horning Date: Thu, 28 Jul 2022 22:04:15 -0400 Subject: [PATCH] Allow multiple pages to be generated from one template Feature to allow generation of host and episode pages. --- site-generator | 49 +++++++++++++++++------- site.cfg | 9 ++++- templates/content-correspondent.tpl.html | 14 +++++++ templates/ids-correspondent.tpl.html | 7 ++++ templates/page.tpl.html | 9 ++++- 5 files changed, 72 insertions(+), 16 deletions(-) create mode 100644 templates/content-correspondent.tpl.html create mode 100644 templates/ids-correspondent.tpl.html diff --git a/site-generator b/site-generator index d8d4716..ad43184 100755 --- a/site-generator +++ b/site-generator @@ -6,14 +6,14 @@ =head1 SYNOPSIS - site-generator [OPTION]... PAGE... + site-generator [OPTION]... PAGE|PAGE=... -p, --preview print generated pages to standard out -v, --verbose use verbose mode --help print this help message Where I is a file name of a web page - or the special ALL (to generate all pages). + or the special I (to generate all pages). Examples: @@ -23,6 +23,9 @@ Generate the whole site: site-generator ALL + Generate pages based on the same template: + site-generator correspondent=1,3,5..10 + =head1 DESCRIPTION This is a site generator for the Hacker Public Radio website based upon the Perl Templates Toolkit. @@ -105,20 +108,24 @@ sub main { # Remove non page sections of the configuration file # from the generated list of pages. - @page_args= grep { $_ ne 'DBI' } @pages_args; + @page_args= grep { $_ ne 'DBI' } @page_args; @page_args= grep { $_ ne 'root_template' } @page_args; }; foreach my $page_arg (@page_args) { - my %page_arg = parse_page_arg($page_arg); - - if (exists($config{$page_arg{'page'}})) { - my $page_config = $config{$page_arg{'page'}}; - $page_config->{'page'} = $page_arg{'page'}; + my %parsed_arg = parse_page_arg($page_arg); + if (exists($config{$parsed_arg{'page'}})) { + my $page_config = $config{$parsed_arg{'page'}}; + $page_config->{'page'} = $parsed_arg{'page'}; - if ($page_config->{'multipage'}) { - print "multipage\n"; - foreach my $id ($page_config->{'ids'}) { + if ($page_config->{'multipage'} && $page_config->{'multipage'} eq 'true') { + if (scalar @{$parsed_arg{'ids'}} == 1) { + @{$parsed_arg{'ids'}} = get_ids_from_db($tt, \$page_config); + } + foreach my $id (@{$parsed_arg{'ids'}}) { $page_config->{'id'} = $id; + verbose ($verbose, "Generating page: $page_config->{'page'} with id: $id"); + generate_page($tt, $config{root_template}{content}, \$page_config, $preview); + print "$page_config->{'page'} $page_config->{'id'}\n"; } } else { @@ -127,7 +134,7 @@ sub main { } } else { - verbose (1, "\nWarning: Page $page_arg{'page'} is not defined in the configuration file."); + verbose (1, "\nWarning: Page $parsed_arg{'page'} is not defined in the configuration file."); } } verbose (1, "\nFinished processing the files."); @@ -158,7 +165,6 @@ sub get_template_html (\%@) { sub generate_page { my ($tt, $root_template, $config, $preview) = @_; - print Dumper($$config); my $html; if (!$preview) { $html = get_filename($$config); @@ -203,7 +209,22 @@ sub parse_page_arg { } } } - return ('page', $page, 'ids', @ids); + return ('page' => $page, 'ids' => [@ids]); +} + +sub get_ids_from_db { + # Use a template to generate a string of page identifiers. + # The template should return the string in the form of + # ... + # + my ($tt, $config) = @_; + my $selected_ids = ""; + my $id_template = "ids-$$config->{'page'}.tpl.html"; + + $tt->process($id_template, $$config, \$selected_ids) + || die $tt->error(), "\n"; + + return split(/,/, substr($selected_ids, 1)); } sub get_filename { diff --git a/site.cfg b/site.cfg index d6998c3..942825b 100644 --- a/site.cfg +++ b/site.cfg @@ -24,6 +24,9 @@ content: page.tpl.html # extension. May have [id] marker in path or name which # will be substituted with a padded page id. +# Configure pages which use the same content template: +# +# multipage: OPTIONAL true | false (DEFAULT = false) [index] navigation: navigation-main.tpl.html @@ -42,4 +45,8 @@ filename: correspondents/index.html navigation: navigation-about.tpl.html content: content-contact.tpl.html - +[correspondent] +navigation: navigation-about.tpl.html +content: content-correspondent.tpl.html +multipage: true +filename: correspondents/[id]/index.html diff --git a/templates/content-correspondent.tpl.html b/templates/content-correspondent.tpl.html new file mode 100644 index 0000000..f69c6e2 --- /dev/null +++ b/templates/content-correspondent.tpl.html @@ -0,0 +1,14 @@ +
+

Correspondent

+

Rho`n

+

Host Image
+ :

+ roan.horning.nospam@nospam.gmail.com
+ 12 +

+

hpr3647 :: Weekend projects

+

Released: 2022-07-26. Duration: 00:16:44. Flag: Clean. Series: general.
+Tags: diy, repairs, umbrella, basketball.
+Rho`n rambles about some weekend projects

+

Become a Correspondent

+
diff --git a/templates/ids-correspondent.tpl.html b/templates/ids-correspondent.tpl.html new file mode 100644 index 0000000..fc60043 --- /dev/null +++ b/templates/ids-correspondent.tpl.html @@ -0,0 +1,7 @@ + + +, + + diff --git a/templates/page.tpl.html b/templates/page.tpl.html index f21179c..be3b665 100644 --- a/templates/page.tpl.html +++ b/templates/page.tpl.html @@ -1,3 +1,9 @@ + + + + + + @@ -21,7 +27,7 @@ - + +