Allow defining of filename via configuration file

This commit is contained in:
Roan Horning 2022-07-28 21:59:38 -04:00
parent 1ec288bbb8
commit 873cfda86a
Signed by: rho_n
GPG Key ID: 234AEF20B72D5769
2 changed files with 63 additions and 23 deletions

View File

@ -88,12 +88,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,28 +101,33 @@ 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' } @pages_args;
@page_args= grep { $_ ne 'root_template' } @page_args;
};
foreach my $page (@pages) {
my %pages = get_multiple_pages($page);
if (exists($config{$pages{'page'}})) {
if ($config{$pages{'page'}}{'multipage'}) {
foreach my $id ($pages{'ids'}) {
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'};
if ($page_config->{'multipage'}) {
print "multipage\n";
foreach my $id ($page_config->{'ids'}) {
$page_config->{'id'} = $id;
}
}
else {
verbose ($verbose, "Generating page: $pages{'page'}");
generate_page($tt, $config{root_template}{content}, \%config, $pages{'page'}, $preview);
verbose ($verbose, "Generating page: $page_config->{'page'}");
generate_page($tt, $config{root_template}{content}, \$page_config, $preview);
}
}
else {
verbose (1, "\nWarning: Page $pages{'page'} is not defined in the configuration file.");
verbose (1, "\nWarning: Page $page_arg{'page'} is not defined in the configuration file.");
}
}
verbose (1, "\nFinished processing the files.");
@ -152,13 +157,13 @@ sub get_template_html (\%@) {
}
sub generate_page {
my ($tt, $root_template, $config, $page, $preview) = @_;
my ($tt, $root_template, $config, $preview) = @_;
print Dumper($$config);
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";
}
@ -173,10 +178,10 @@ sub verbose {
};
}
sub get_multiple_pages {
my ($pages) = @_;
sub parse_page_arg {
my ($page_arg) = @_;
# Split page name from page ids if available.
my ($page, $ids) = split(/=/, $pages);
my ($page, $ids) = split(/=/, $page_arg);
my @ids = [];
if(!$ids) {
@ -201,3 +206,29 @@ sub get_multiple_pages {
return ('page', $page, 'ids', @ids);
}
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,17 @@ 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.
[index]
navigation: navigation-main.tpl.html
@ -28,6 +36,7 @@ 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