| 
									
										
										
										
											2022-06-28 05:25:36 -04:00
										 |  |  | #!/usr/bin/perl | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | =head1 NAME | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | siteGenerator - HPR Site Generator | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | =head1 SYNOPSIS | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	siteGenerator [OPTION]... PAGE... | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	-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: | 
					
						
							|  |  |  | 		siteGenerator index about | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		Generate the whole site: | 
					
						
							|  |  |  | 		siteGenerator ALL | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | =head1 DESCRIPTION | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | This is a site generator based upon the Perl Templates Toolkit. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | =head1 AUTHOR | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Roan Horning <roan.horning@no-spam.gmail.com> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | =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-06-28 05:25:36 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | exit main(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | sub main { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Argument parsing | 
					
						
							|  |  |  |     my $verbose; | 
					
						
							|  |  |  |     GetOptions( | 
					
						
							|  |  |  |         'verbose'  => \$verbose, | 
					
						
							|  |  |  |     ) or pod2usage(1); | 
					
						
							|  |  |  |     pod2usage(1) unless @ARGV; | 
					
						
							|  |  |  |     my (@pages) = @ARGV; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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"); | 
					
						
							|  |  |  | 			generate_page($tt, $config{root_template}{content}, $config{$page}{navigation}, $config{$page}{content}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		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({ | 
					
						
							|  |  |  | 		INCLUDE_PATH => './templates', | 
					
						
							|  |  |  | 		EVAL_PERL    => 1, | 
					
						
							|  |  |  | 		START_TAG	 => '<!--%', | 
					
						
							|  |  |  | 		END_TAG		 => '%-->', | 
					
						
							| 
									
										
										
										
											2022-06-29 10:42:10 -04:00
										 |  |  | 		CONSTANTS    => { | 
					
						
							|  |  |  | 			driver   => $_[0]{driver}, | 
					
						
							|  |  |  | 			user     => $_[0]{user}, | 
					
						
							|  |  |  | 			password => $_[0]{password}, | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2022-06-28 10:46:52 -04:00
										 |  |  | 	}) || die $Template::ERROR, "\n"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-29 11:03:32 -04:00
										 |  |  | sub generate_page  { | 
					
						
							|  |  |  | 	my ($tt, $page, $navigation, $content) = @_; | 
					
						
							| 
									
										
										
										
											2022-06-28 10:46:52 -04:00
										 |  |  | 	my $tt_vars = { | 
					
						
							|  |  |  | 		navigation => $navigation,  | 
					
						
							|  |  |  | 		content => $content  | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-29 11:03:32 -04:00
										 |  |  | 	$tt->process($page, $tt_vars) | 
					
						
							| 
									
										
										
										
											2022-06-28 10:46:52 -04:00
										 |  |  | 		|| die $tt->error(), "\n"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-01 22:02:25 -04:00
										 |  |  | sub verbose { | 
					
						
							|  |  |  | 	my ($verbose, $message) = @_; | 
					
						
							|  |  |  | 	if ($verbose) { | 
					
						
							|  |  |  | 		print "$message\n"; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							|  |  |  | 		print "."; | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | } |