commit 5105c22ed425ce5b0698a7b5e1fab8fa3a25fc12
parent 17956e8f372d7e51f89bb5261570cd7e4778b607
Author: Samir Parikh <noreply@samirparikh.com>
Date: Fri, 10 Feb 2023 21:07:01 +0000
add functionality to now generate basic HTML files for each new post
Diffstat:
M | cgi/spsp.cgi | | | 46 | ++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 46 insertions(+), 0 deletions(-)
diff --git a/cgi/spsp.cgi b/cgi/spsp.cgi
@@ -32,6 +32,7 @@ use POSIX qw( strftime );
use Data::Dumper;
use CGI;
use Text::Markdown;
+use File::Spec::Functions;
use feature qw( signatures );
no warnings qw( experimental::signatures );
@@ -52,6 +53,9 @@ my $POSTS_REL_PATH_FILENAME = "/posts.html";
### temporary HTML file we use to update posts.html based on data from post_data
my $POSTS_TEMP_HTML_FILENAME = "$SPSP_PATH/html/posts_temp.html.$$";
+### path for new HTML file for our most recent post
+my $NEW_POST_HTML_PATH = "$SPSP_PATH/html/";
+
### length of random ID assigned to each post
my $ID_LENGTH = 4;
@@ -184,6 +188,48 @@ unlink $POSTS_HTML_FILENAME
rename $POSTS_TEMP_HTML_FILENAME, $POSTS_HTML_FILENAME
or die "Cannot rename $POSTS_TEMP_HTML_FILENAME to $POSTS_HTML_FILENAME: $!\n";
+### open filehandle to create HTML file for new post
+my $new_post_html = catfile( $NEW_POST_HTML_PATH, "$post{ $ID }.html" );
+open my $new_post_filehandle, '>', $new_post_html
+ or die "Cannot open $new_post_html for writing: $!\n";
+
+### change default output filehandle
+select $new_post_filehandle;
+
+print <<"EOF";
+<!DOCTYPE html>
+<html lang="en">
+
+<head>
+ <title>$HTML_TITLE</title>
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" >
+ <meta name="viewport" content="width=device-width" >
+ <meta name="author" content="$AUTHOR" >
+ <link rel="icon" type="image/png" href="/assets/favicon.png" >
+ <link href="styles/style.css" rel="stylesheet" type="text/css" >
+</head>
+
+<body>
+<h2>$APPLICATION</h2>
+<a href="$POSTS_REL_PATH_FILENAME"><h1>$HEADING</h1></a>
+<hr>
+EOF
+
+print "<p><strong><a href=\"$post{ $ID }.html\">$post{ $TITLE }</a></strong>\n"
+ unless ( $post{ $TITLE } eq '' );
+print "<p>$post{ $STATUS }\n";
+print "<p><a href=\"$post{ $ID }.html\">$post{ $TIMESTAMP }</a>\n";
+print "<hr>\n";
+
+print "</body>\n";
+print "</html>\n";
+
+### change back to default output filehandle
+select STDOUT;
+
+### close the temporary file
+close $new_post_filehandle or die "Error closing $new_post_html: $!\n";
+
print $cgi->redirect( $POSTS_REL_PATH_FILENAME );
exit 0;