169 lines
4.2 KiB
Perl
Executable File
169 lines
4.2 KiB
Perl
Executable File
#!/usr/bin/perl
|
|
|
|
=head1 NAME
|
|
|
|
site-generator - HPR Site Generator
|
|
|
|
=head1 SYNOPSIS
|
|
|
|
site-generator [OPTION]... PAGE...
|
|
|
|
-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).
|
|
|
|
Examples:
|
|
|
|
Generate two specific pages:
|
|
site-generator index about
|
|
|
|
Generate the whole site:
|
|
site-generator ALL
|
|
|
|
=head1 DESCRIPTION
|
|
|
|
This is a site generator for the Hacker Public Radio website based upon the Perl Templates Toolkit.
|
|
|
|
=head1 INSTALLATION
|
|
|
|
* Create the sqlite3 database from the files in the _sql directory. The default name for the
|
|
database file is "hpr.db" and should be located in the root of the project directory. The
|
|
name and location can be set in the site.cfg file.
|
|
* Two sql helper scripts are available to generate an empty database or a database filled with test data.
|
|
- For an empty database: `cat Create_Database_Empty.sql | sqlite3 hpr.db`
|
|
- For a database with test data: `cat Create_Database_Test.sql | sqlite3 hpr.db`
|
|
* Install the needed Perl modules using preferred method (distribution packages, CPAN, etc.)
|
|
* GetOpt
|
|
* Pod::Usage
|
|
* Config::Std
|
|
* Template
|
|
* DBI
|
|
* Date::Calc
|
|
|
|
=head1 AUTHOR
|
|
|
|
Roan Horning <roan.horning@no-spam.gmail.com>
|
|
|
|
=head1 LICENSE
|
|
|
|
site-generator -- a static website generator for HPR
|
|
Copyright (C) 2022 Roan Horning
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU Affero General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Affero General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
=cut
|
|
|
|
use strict;
|
|
use warnings;
|
|
|
|
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 $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
|
|
# passed into the generator
|
|
my $ALL = grep { $_ eq 'ALL' } @pages;
|
|
|
|
# Load config file
|
|
read_config "site.cfg" => my %config;
|
|
|
|
my $tt = get_template_html($config{DBI});
|
|
|
|
if ($ALL) {
|
|
@pages = 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;
|
|
};
|
|
foreach my $page (@pages) {
|
|
|
|
if (exists($config{$page})) {
|
|
verbose ($verbose, "Generating page: $page");
|
|
generate_page($tt, $config{root_template}{content}, \%config, $page, $preview);
|
|
|
|
}
|
|
else {
|
|
verbose (1, "\nWarning: Page $page is not defined in the configuration file.");
|
|
}
|
|
}
|
|
verbose (1, "\nFinished processing the files.");
|
|
return 0;
|
|
}
|
|
|
|
sub get_template_html (\%@) {
|
|
# For an HTML based Template file, define the
|
|
# template start and end tags to also function as
|
|
# HTML comments to make the template file valid HTML.
|
|
#
|
|
return Template->new({
|
|
INCLUDE_PATH => './templates',
|
|
OUTPUT_PATH => './public_html',
|
|
EVAL_PERL => 1,
|
|
START_TAG => '<!--%',
|
|
END_TAG => '%-->',
|
|
PRE_CHOMP => 1,
|
|
POST_CHOMP => 1,
|
|
CONSTANTS => {
|
|
driver => $_[0]{driver},
|
|
user => $_[0]{user},
|
|
password => $_[0]{password},
|
|
}
|
|
}) || die $Template::ERROR, "\n";
|
|
|
|
}
|
|
|
|
sub generate_page {
|
|
my ($tt, $root_template, $config, $page, $preview) = @_;
|
|
|
|
my $html;
|
|
if (!$preview) {
|
|
$html = "$page.html";
|
|
}
|
|
$tt->process($root_template, $config->{$page}, $html)
|
|
|| die $tt->error(), "\n";
|
|
|
|
}
|
|
|
|
sub verbose {
|
|
my ($verbose, $message) = @_;
|
|
if ($verbose) {
|
|
print "$message\n";
|
|
}
|
|
else {
|
|
print ".";
|
|
};
|
|
}
|