Compare commits
3 Commits
4e56289338
...
75cd3e3e85
Author | SHA1 | Date | |
---|---|---|---|
75cd3e3e85 | |||
873cfda86a | |||
1ec288bbb8 |
121
site-generator
121
site-generator
@ -6,14 +6,14 @@
|
|||||||
|
|
||||||
=head1 SYNOPSIS
|
=head1 SYNOPSIS
|
||||||
|
|
||||||
site-generator [OPTION]... PAGE...
|
site-generator [OPTION]... PAGE|PAGE=<comma separated list of ids>...
|
||||||
|
|
||||||
-p, --preview print generated pages to standard out
|
-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
|
||||||
|
|
||||||
Where I<PAGE> is a file name of a web page
|
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:
|
Examples:
|
||||||
|
|
||||||
@ -23,6 +23,9 @@
|
|||||||
Generate the whole site:
|
Generate the whole site:
|
||||||
site-generator ALL
|
site-generator ALL
|
||||||
|
|
||||||
|
Generate pages based on the same template:
|
||||||
|
site-generator correspondent=1,3,5..10
|
||||||
|
|
||||||
=head1 DESCRIPTION
|
=head1 DESCRIPTION
|
||||||
|
|
||||||
This is a site generator for the Hacker Public Radio website based upon the Perl Templates Toolkit.
|
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,
|
'verbose' => \$verbose,
|
||||||
) or pod2usage(1);
|
) or pod2usage(1);
|
||||||
pod2usage(1) unless @ARGV;
|
pod2usage(1) unless @ARGV;
|
||||||
my (@pages) = @ARGV;
|
my (@page_args) = @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
|
||||||
# passed into the generator
|
# passed into the generator
|
||||||
my $ALL = grep { $_ eq 'ALL' } @pages;
|
my $ALL = grep { $_ eq 'ALL' } @page_args;
|
||||||
|
|
||||||
# Load config file
|
# Load config file
|
||||||
read_config "site.cfg" => my %config;
|
read_config "site.cfg" => my %config;
|
||||||
@ -101,22 +104,37 @@ sub main {
|
|||||||
my $tt = get_template_html($config{DBI});
|
my $tt = get_template_html($config{DBI});
|
||||||
|
|
||||||
if ($ALL) {
|
if ($ALL) {
|
||||||
@pages = keys %config;
|
@page_args = keys %config;
|
||||||
|
|
||||||
# Remove non page sections of the configuration file
|
# Remove non page sections of the configuration file
|
||||||
# from the generated list of pages.
|
# from the generated list of pages.
|
||||||
@pages= grep { $_ ne 'DBI' } @pages;
|
@page_args= grep { $_ ne 'DBI' } @page_args;
|
||||||
@pages= grep { $_ ne 'root_template' } @pages;
|
@page_args= grep { $_ ne 'root_template' } @page_args;
|
||||||
};
|
};
|
||||||
foreach my $page (@pages) {
|
foreach my $page_arg (@page_args) {
|
||||||
|
my %parsed_arg = parse_page_arg($page_arg);
|
||||||
if (exists($config{$page})) {
|
if (exists($config{$parsed_arg{'page'}})) {
|
||||||
verbose ($verbose, "Generating page: $page");
|
my $page_config = $config{$parsed_arg{'page'}};
|
||||||
generate_page($tt, $config{root_template}{content}, \%config, $page, $preview);
|
$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 {
|
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.");
|
verbose (1, "\nFinished processing the files.");
|
||||||
@ -146,13 +164,12 @@ sub get_template_html (\%@) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sub generate_page {
|
sub generate_page {
|
||||||
my ($tt, $root_template, $config, $page, $preview) = @_;
|
my ($tt, $root_template, $config, $preview) = @_;
|
||||||
|
|
||||||
my $html;
|
my $html;
|
||||||
if (!$preview) {
|
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";
|
|| die $tt->error(), "\n";
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -166,3 +183,73 @@ sub verbose {
|
|||||||
print ".";
|
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;
|
||||||
|
}
|
||||||
|
24
site.cfg
24
site.cfg
@ -13,9 +13,20 @@ content: page.tpl.html
|
|||||||
|
|
||||||
# Configure the navigation menu and the content templates for each page
|
# Configure the navigation menu and the content templates for each page
|
||||||
# of the site:
|
# of the site:
|
||||||
# [page_name]
|
|
||||||
# navigation: <name of navigation template>
|
# Configure filename if default name is not desired
|
||||||
# content: <name of page content template>
|
# (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]
|
[index]
|
||||||
navigation: navigation-main.tpl.html
|
navigation: navigation-main.tpl.html
|
||||||
@ -28,9 +39,14 @@ content: content-about.tpl.html
|
|||||||
[correspondents]
|
[correspondents]
|
||||||
navigation: navigation-about.tpl.html
|
navigation: navigation-about.tpl.html
|
||||||
content: content-correspondents.tpl.html
|
content: content-correspondents.tpl.html
|
||||||
|
filename: correspondents/index.html
|
||||||
|
|
||||||
[contact]
|
[contact]
|
||||||
navigation: navigation-about.tpl.html
|
navigation: navigation-about.tpl.html
|
||||||
content: content-contact.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
|
||||||
|
14
templates/content-correspondent.tpl.html
Normal file
14
templates/content-correspondent.tpl.html
Normal 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>
|
7
templates/ids-correspondent.tpl.html
Normal file
7
templates/ids-correspondent.tpl.html
Normal 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 %-->
|
||||||
|
|
@ -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>
|
<!DOCTYPE HTML>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<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 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="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 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]>
|
<!--[if IE]>
|
||||||
<link rel="stylesheet" href="css/hpr.css" media="screen" type="text/css" />
|
<link rel="stylesheet" href="css/hpr.css" media="screen" type="text/css" />
|
||||||
<script src="/JavaScript/html5.js"></script>
|
<script src="/JavaScript/html5.js"></script>
|
||||||
@ -124,3 +130,4 @@
|
|||||||
</footer>
|
</footer>
|
||||||
</div>
|
</div>
|
||||||
<!-- shadow -->
|
<!-- shadow -->
|
||||||
|
<!--% END %-->
|
||||||
|
Reference in New Issue
Block a user