| 
									
										
										
										
											2022-06-28 05:25:36 -04:00
										 |  |  | #!/usr/bin/perl | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | =head1 NAME | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-03 17:14:58 -04:00
										 |  |  | 	site-generator - HPR Site Generator | 
					
						
							| 
									
										
										
										
											2022-06-28 05:25:36 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | =head1 SYNOPSIS | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-03 17:14:58 -04:00
										 |  |  | 	site-generator [OPTION]... PAGE... | 
					
						
							| 
									
										
										
										
											2022-06-28 05:25:36 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-03 17:14:58 -04:00
										 |  |  | 	-p, --preview  print generated pages to standard out | 
					
						
							| 
									
										
										
										
											2022-06-28 05:25:36 -04:00
										 |  |  | 	-v, --verbose  use verbose mode | 
					
						
							|  |  |  | 	--help         print this help message | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-01 22:02:25 -04:00
										 |  |  | 	Where I<PAGE> is a file name of a web page  | 
					
						
							|  |  |  | 	or the special ALL (to generate all pages). | 
					
						
							| 
									
										
										
										
											2022-06-28 05:25:36 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	Examples: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		Generate two specific pages: | 
					
						
							| 
									
										
										
										
											2022-07-02 12:52:50 -04:00
										 |  |  | 		site-generator index about | 
					
						
							| 
									
										
										
										
											2022-06-28 05:25:36 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		Generate the whole site: | 
					
						
							| 
									
										
										
										
											2022-07-02 12:52:50 -04:00
										 |  |  | 		site-generator ALL | 
					
						
							| 
									
										
										
										
											2022-06-28 05:25:36 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | =head1 DESCRIPTION | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-02 12:52:50 -04:00
										 |  |  | 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. | 
					
						
							| 
									
										
										
										
											2022-07-03 19:03:14 -04:00
										 |  |  | 	* 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` | 
					
						
							| 
									
										
										
										
											2022-07-02 12:52:50 -04:00
										 |  |  | 	* Install the needed Perl modules using preferred method (distribution packages, CPAN, etc.) | 
					
						
							|  |  |  | 		* GetOpt | 
					
						
							|  |  |  | 		* Pod::Usage | 
					
						
							|  |  |  | 		* Config::Std | 
					
						
							|  |  |  | 		* Template | 
					
						
							|  |  |  | 		* DBI | 
					
						
							| 
									
										
										
										
											2022-06-28 05:25:36 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | =head1 AUTHOR | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-03 17:14:58 -04:00
										 |  |  | 	Roan Horning <roan.horning@no-spam.gmail.com> | 
					
						
							| 
									
										
										
										
											2022-06-28 05:25:36 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-13 23:16:44 -04:00
										 |  |  | =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/>.	 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-28 05:25:36 -04:00
										 |  |  | =cut | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use strict; | 
					
						
							|  |  |  | use warnings; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use Getopt::Long qw(:config auto_help); | 
					
						
							|  |  |  | use Pod::Usage; | 
					
						
							|  |  |  | use Config::Std; | 
					
						
							| 
									
										
										
										
											2022-06-28 10:46:52 -04:00
										 |  |  | use Template; | 
					
						
							| 
									
										
										
										
											2022-07-03 17:14:58 -04:00
										 |  |  | use Data::Dumper; | 
					
						
							| 
									
										
										
										
											2022-06-28 05:25:36 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | exit main(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | sub main { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-03 17:14:58 -04:00
										 |  |  | 	# Argument parsing | 
					
						
							|  |  |  | 	my $preview; | 
					
						
							|  |  |  | 	my $verbose; | 
					
						
							|  |  |  | 	GetOptions( | 
					
						
							|  |  |  | 		'preview' => \$preview, | 
					
						
							|  |  |  | 		'verbose'  => \$verbose, | 
					
						
							|  |  |  | 	) or pod2usage(1); | 
					
						
							|  |  |  | 	pod2usage(1) unless @ARGV; | 
					
						
							|  |  |  | 	my (@pages) = @ARGV; | 
					
						
							| 
									
										
										
										
											2022-06-28 05:25:36 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-01 22:02:25 -04:00
										 |  |  | 	# 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; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-28 05:25:36 -04:00
										 |  |  | 	# Load config file | 
					
						
							|  |  |  | 	read_config "site.cfg" => my %config; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-29 10:42:10 -04:00
										 |  |  | 	my $tt = get_template_html($config{DBI}); | 
					
						
							| 
									
										
										
										
											2022-06-28 05:25:36 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-01 22:02:25 -04:00
										 |  |  | 	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"); | 
					
						
							| 
									
										
										
										
											2022-07-03 17:14:58 -04:00
										 |  |  | 			generate_page($tt, $config{root_template}{content}, \%config, $page, $preview); | 
					
						
							| 
									
										
										
										
											2022-07-01 22:02:25 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else { | 
					
						
							|  |  |  | 			verbose (1, "\nWarning: Page $page is not defined in the configuration file."); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  |     verbose (1, "\nFinished processing the files."); | 
					
						
							| 
									
										
										
										
											2022-06-28 05:25:36 -04:00
										 |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-29 10:42:10 -04:00
										 |  |  | sub get_template_html (\%@)  { | 
					
						
							| 
									
										
										
										
											2022-06-28 10:46:52 -04:00
										 |  |  | 	# 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({ | 
					
						
							| 
									
										
										
										
											2022-07-03 17:14:58 -04:00
										 |  |  | 			INCLUDE_PATH => './templates', | 
					
						
							|  |  |  | 			OUTPUT_PATH	 => './public_html', | 
					
						
							|  |  |  | 			EVAL_PERL    => 1, | 
					
						
							|  |  |  | 			START_TAG	 => '<!--%', | 
					
						
							|  |  |  | 			END_TAG		 => '%-->', | 
					
						
							|  |  |  | 			CONSTANTS    => { | 
					
						
							|  |  |  | 				driver   => $_[0]{driver}, | 
					
						
							|  |  |  | 				user     => $_[0]{user}, | 
					
						
							|  |  |  | 				password => $_[0]{password}, | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}) || die $Template::ERROR, "\n"; | 
					
						
							| 
									
										
										
										
											2022-06-28 10:46:52 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-29 11:03:32 -04:00
										 |  |  | sub generate_page  { | 
					
						
							| 
									
										
										
										
											2022-07-03 17:14:58 -04:00
										 |  |  | 	my ($tt, $root_template, $config, $page, $preview) = @_; | 
					
						
							| 
									
										
										
										
											2022-06-28 10:46:52 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-03 17:14:58 -04:00
										 |  |  | 	my $html; | 
					
						
							|  |  |  | 	if (!$preview) { | 
					
						
							|  |  |  | 		$html = "$page.html"; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	$tt->process($root_template, $config->{$page}, $html) | 
					
						
							|  |  |  | 	|| die $tt->error(), "\n"; | 
					
						
							| 
									
										
										
										
											2022-06-28 10:46:52 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-01 22:02:25 -04:00
										 |  |  | sub verbose { | 
					
						
							|  |  |  | 	my ($verbose, $message) = @_; | 
					
						
							|  |  |  | 	if ($verbose) { | 
					
						
							|  |  |  | 		print "$message\n"; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							|  |  |  | 		print "."; | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | } |