Archived
4
2

Compare commits

..

3 Commits

Author SHA1 Message Date
75cd3e3e85
Allow multiple pages to be generated from one template
Feature to allow generation of host and episode pages.
2022-07-28 22:04:15 -04:00
873cfda86a
Allow defining of filename via configuration file 2022-07-28 21:59:38 -04:00
1ec288bbb8
Initial add of multipage template processing
For the individual host pages, allow the ids to be passed
in on the command line.
2022-07-21 23:58:30 -04:00
5 changed files with 154 additions and 23 deletions

View File

@ -6,14 +6,14 @@
=head1 SYNOPSIS
site-generator [OPTION]... PAGE...
site-generator [OPTION]... PAGE|PAGE=<comma separated list of ids>...
-p, --preview print generated pages to standard out
-v, --verbose use verbose mode
--help print this help message
Where I<PAGE> is a file name of a web page
or the special ALL (to generate all pages).
or the special I<ALL> (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.
@ -88,12 +91,12 @@ sub main {
'verbose' => \$verbose,
) or pod2usage(1);
pod2usage(1) unless @ARGV;
my (@pages) = @ARGV;
my (@page_args) = @ARGV;
# Set flag indicating whether or not to generate all pages.
# The flag is set to true if the special argument ALL is
# passed into the generator
my $ALL = grep { $_ eq 'ALL' } @pages;
my $ALL = grep { $_ eq 'ALL' } @page_args;
# Load config file
read_config "site.cfg" => my %config;
@ -101,22 +104,37 @@ sub main {
my $tt = get_template_html($config{DBI});
if ($ALL) {
@pages = keys %config;
@page_args = keys %config;
# Remove non page sections of the configuration file
# from the generated list of pages.
@pages= grep { $_ ne 'DBI' } @pages;
@pages= grep { $_ ne 'root_template' } @pages;
@page_args= grep { $_ ne 'DBI' } @page_args;
@page_args= grep { $_ ne 'root_template' } @page_args;
};
foreach my $page (@pages) {
if (exists($config{$page})) {
verbose ($verbose, "Generating page: $page");
generate_page($tt, $config{root_template}{content}, \%config, $page, $preview);
foreach my $page_arg (@page_args) {
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'} && $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 {
verbose ($verbose, "Generating page: $page_config->{'page'}");
generate_page($tt, $config{root_template}{content}, \$page_config, $preview);
}
}
else {
verbose (1, "\nWarning: Page $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.");
@ -146,13 +164,12 @@ sub get_template_html (\%@) {
}
sub generate_page {
my ($tt, $root_template, $config, $page, $preview) = @_;
my ($tt, $root_template, $config, $preview) = @_;
my $html;
if (!$preview) {
$html = "$page.html";
$html = get_filename($$config);
}
$tt->process($root_template, $config->{$page}, $html)
$tt->process($root_template, $$config, $html)
|| die $tt->error(), "\n";
}
@ -166,3 +183,73 @@ sub verbose {
print ".";
};
}
sub parse_page_arg {
my ($page_arg) = @_;
# Split page name from page ids if available.
my ($page, $ids) = split(/=/, $page_arg);
my @ids = [];
if(!$ids) {
$ids = "";
}
else {
# Parse the page ids and push them onto @ids array
my @ids_by_comma = split(/\,/, $ids);
foreach my $id_by_comma (@ids_by_comma) {
my @ids_for_range = split(/\.\./, $id_by_comma);
if ((scalar @ids_for_range) == 2) {
push @ids, $ids_for_range[0]..$ids_for_range[1];
}
elsif ((scalar @ids_for_range) == 1) {
push @ids, $ids_for_range[0];
}
else {
verbose (1, "\nWarning: Page $page id range $id_by_comma could not be parsed.");
}
}
}
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
# <comma><identifier><comma><identifier>...
#
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 {
my ($config) = @_;
my $filename = "output.html";
my $base_path = "";
if ($$config{'filename'}) {
if (substr($$config{'filename'}, -1) eq '/') {
$base_path = $$config{'filename'};
}
else {
$filename = $$config{'filename'};
my $padded_index = sprintf("%04d", $$config{'id'});
$filename =~ s/\[id\]/$padded_index/;
return $filename;
}
}
# Default naming if full filename configuration is not supplied.
if ($$config{'multipage'} && $$config{'multipage'} eq 'true') {
my $padded_index = sprintf("%04d", $$config{'id'});
$filename = "$base_path$$config{'page'}${padded_index}.html";
}
else {
$filename = "$base_path$$config{'page'}.html";
}
return $filename;
}

View File

@ -13,9 +13,20 @@ content: page.tpl.html
# Configure the navigation menu and the content templates for each page
# of the site:
# [page_name]
# navigation: <name of navigation template>
# content: <name of page content template>
# Configure filename if default name is not desired
# (Default is "ROOT/[page_name].html"):
#
# filename: OPTIONAL <relative path from ROOT -- optional>
# * <directory path> -- Must end in forward slash. File will be created
# in this path with the default naming scheme.
# * <file name> -- May include a relative path. Should include the file
# 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
@ -28,9 +39,14 @@ content: content-about.tpl.html
[correspondents]
navigation: navigation-about.tpl.html
content: content-correspondents.tpl.html
filename: correspondents/index.html
[contact]
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

View File

@ -0,0 +1,14 @@
<article>
<h2 class="title">Correspondent</h2>
<h2>Rho`n</h2>
<p><img src="./images/hosts/293.png" height="80" alt="Host Image" /><br>
<label>Host ID</label>:<!--% id %--><br><br>
<label>email:</label> <u>roan.horning.nospam@nospam.gmail.com</u><br>
<label>episodes:</label> <strong>12</strong>
</p>
<h3><a href="eps.php?id=3647">hpr3647 :: Weekend projects</a></h3>
<p class="meta"><strong>Released:</strong> 2022-07-26. <strong>Duration:</strong> 00:16:44. <strong>Flag:</strong> Clean. <strong>Series:</strong> <a href="series.php?id=0">general</a>. <br>
<strong>Tags:</strong> <em>diy, repairs, umbrella, basketball</em>.<br>
Rho`n rambles about some weekend projects</p>
<p><a href="contribute.php">Become a Correspondent</a></p>
</article>

View File

@ -0,0 +1,7 @@
<!--% USE DBI(constants.driver, constants.user, constants.password) %-->
<!--% FOREACH host IN DBI.query(
'select h.hostid from hosts as h'
) %-->
,<!--% host.hostid %-->
<!--% END %-->

View File

@ -1,3 +1,9 @@
<!--% IF config.multipage == "true" && config.ids.count() > 0 %-->
<!--% ELSIF config.multipage == "true" %-->
<!--% ELSE %-->
<!--% page_ids = ['0'] %-->
<!--% END %-->
<!--% FOREACH page_id IN page_ids %-->
<!DOCTYPE HTML>
<html lang="en">
<head>
@ -21,7 +27,7 @@
<link rel="alternate" type="application/rss+xml" title="Hacker Public Radio MP3 RSS" href="/hpr_mp3_rss.php" />
<link rel="alternate" type="application/rss+xml" title="Hacker Public Radio Comments RSS" href="/comments_rss.php" />
<link rel="license" title="cc by 3.0" href="https://creativecommons.org/licenses/by-sa/3.0/" />
<link href="/css/hpr.css" rel="stylesheet" />
<link href="./css/hpr.css" rel="stylesheet" />
<!--[if IE]>
<link rel="stylesheet" href="css/hpr.css" media="screen" type="text/css" />
<script src="/JavaScript/html5.js"></script>
@ -124,3 +130,4 @@
</footer>
</div>
<!-- shadow -->
<!--% END %-->