bliper

static html page generation in perl
git clone git://git.2f30.org/bliper.git
Log | Files | Refs | README | LICENSE

bliper-generate.pl (4749B)


      1 #!/usr/bin/perl -w
      2 
      3 use strict;
      4 use Cwd;
      5 
      6 my $main_dir	= getcwd;
      7 my $main_page	= 'output/index.html';
      8 my $indir	= 'main-articles';				# The input directory name
      9 my $outdir	= 'output';					# The output directory name
     10 my $pagedir	= 'pages';					# Individual pages directory
     11 my $pages_b	= 'pages-b';					# Pages that do not show at menu
     12 my @maint	= qx(cat template/main-top);			# Top section for main page
     13 my @mainb	= qx(cat template/main-bottom);			# Bottom section for main page
     14 my @footer	= qx(cat template/footer);			# Footer for every page incl main
     15 my @paget	= qx(cat template/page-top);			# Top section for every page in pages/
     16 my @pageb	= qx(cat template/page-bottom);			# Bottom section for every page in pages/
     17 my @articlet	= qx(cat template/article-top);			# Top section for every article in main-articles/
     18 my @articleb	= qx(cat template/article-bottom);		# Bottom section for every article in main-articles/
     19 my $previewnum	= 3;						# Article number preview for frontpage
     20 my $cssfile	= 'output/style.css';
     21 
     22 unlink glob "output/*.html"; 					# Remove old pages
     23 unlink "output/tmp";						# File which stores menu
     24 open (OUT, '>', "$outdir/index.html") or die $!;		# Create the index.html page
     25 print OUT @maint;
     26 
     27 chdir($pagedir) or die "Can't change path to $pagedir $!\n";
     28 print "\n\t\t\tblipper (BLog In PERl) v0.1\n\n";
     29 print "Entering " . getcwd . "\n\n";
     30 open (OUT, '>>', "../$outdir/tmp") or die $!;			# Begin creating menu for individual pages in a file
     31 print OUT "<ul class=\"menu\">"; 				# Begin list for menu items
     32 print OUT "<li><a href=\"index.html\">home</a></li>";
     33 
     34 my @pages = <*>;						# Create individual pages
     35 foreach my $page (@pages) {
     36 	print "Generating page: ";
     37 	printf("%-30s", $page);
     38 	my @array = qx(cat $page);
     39 	my $html = qx(Markdown.pl $page);
     40 	$page =~ s/\.[^\.]*$//;
     41 	print "\t==> output page:\t" . "../$outdir/$page.html\n";
     42 	open (OUT, '>', "../$outdir/$page.html") or die $!;
     43 	print OUT @paget;
     44 	print OUT $html;
     45 	print OUT @pageb;
     46 	close OUT;
     47 
     48 	open (OUT, '>>', "../$outdir/tmp") or die $!;		# Append page links to tmp file
     49 	print OUT "<li><a href=\"$page.html\">$page</a></li>";
     50 }
     51 
     52 print OUT "</ul>";
     53 print OUT "<br>";
     54 close OUT or die $!;
     55 
     56 chdir($main_dir) or die "Can't change path to $main_dir $!\n";
     57 # open (OUT, '>>', "$outdir/index.html") or die $!;		# Create the index.html page
     58 # print OUT "<p class=\"latart\">Latest articles:</p>";
     59 # close OUT or die $!;
     60 print "\nIndividual page generation completed. ";
     61 chdir($indir) or die "Can't change path to $indir $!\n";
     62 print "\nEntering " . getcwd . "\n\n";
     63 
     64 my @files = <*>;
     65 @files = reverse @files;
     66 my $i = 0;
     67 foreach my $file (@files) {					# Read each file(post) from indir, cat to $outdir/$file.html
     68 	print "Processing file: ";
     69 	printf ("%-30s", $file);
     70 	my $array = qx(cat $file);
     71 	my $html = qx(Markdown.pl $file);
     72 	$file =~ s/\.[^\.]*$//;
     73 	print "\t==> output file:\t" . "../$outdir/$file.html\n";
     74 
     75 	open (OUT, '>', "../$outdir/$file.html") or die $!;
     76 	print OUT @articlet;
     77 	print OUT $html;					# Append content
     78 	print OUT @articleb;
     79 	close OUT;
     80 
     81 	my @preview_array = split(/\n{2,}/, $html);		# Pull title and first paragraph from article.
     82 	$preview_array[0] =~ s/h1>/h3>/g;			# Change h1 to h3 for the index page.
     83 	$preview_array[0] =~ s/<h3>/<a href=\"$file.html\"><h3>/g;
     84 	$preview_array[0] =~ s/<\/h3>/<\/h3><\/a>/g;
     85 
     86 	if ($i < $previewnum) {					# Append links to stories in index.html
     87 		open (OUT, '>>', "../$outdir/index.html") or die $!;
     88 		print OUT "<div class=\"preview\">
     89 				$preview_array[0]
     90 				$preview_array[1]<br>
     91 				$preview_array[2]<br>
     92 				<p><a class=\"previewlink\" href=\"$file.html\">
     93 				<em>read more... →</em></a></p></div>";
     94 		print OUT "\n";
     95 		print OUT "<br>";
     96 	}
     97 	$i++;
     98 }
     99 
    100 chdir($main_dir) or die "Can't change path to $main_dir $!\n";
    101 print "\nArticle generation completed. ";
    102 chdir($pages_b) or die "Can't change path to $indir $!\n";
    103 print "\nEntering " . getcwd . "\n\n";
    104 
    105 my @ind_pages = <*>;
    106 foreach my $file (@ind_pages) {					# Read each page that we do not want to add in our menu
    107 	print "Processing pages:";
    108 	printf ("%-30s", $file);
    109 	my @array = qx(cat $file);
    110 	my $html = qx(Markdown.pl $file);			# Markdown to html
    111 	$file =~ s/\.[^\.]*$//;
    112 	print "\t==> output file:\t" . "../$outdir/$file.html\n";
    113 	open (OUT, '>', "../$outdir/$file.html") or die $!;
    114 	print OUT @articlet;
    115 	print OUT $html;					# Append content
    116 	print OUT @articleb;
    117 	close OUT;
    118 }
    119 
    120 open (OUT, '>>', "../$outdir/index.html") or die $!;		# Close all tags in index.html
    121 print OUT @mainb;
    122 print OUT @footer;
    123 close OUT;
    124 
    125 print "\nPages in pages_b/ generation completed. ";
    126 print "\nLeaving " . getcwd . "\n";
    127 chdir($main_dir) or die "Can't change path to $main_dir $!\n";
    128 print "Site generation completed. Files are in output/ dir.\n";