commit f31438d1ff967d27e574f6f01e924f6cb92e098e
parent 091faaf8039947f91dc862217b7e4c5d94dcc6f5
Author: Samir Parikh <noreply@samirparikh.com>
Date: Mon, 6 Feb 2023 02:48:20 +0000
update spsp.cgi to:
- add comments around declared constants
- add constants to support creation of posts.html file
- rename some of the file and filehandle variable names
- provide functionality to start updating the actual posts.html file
Diffstat:
M | cgi/spsp.cgi | | | 100 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------- |
1 file changed, 81 insertions(+), 19 deletions(-)
diff --git a/cgi/spsp.cgi b/cgi/spsp.cgi
@@ -35,19 +35,39 @@ use Text::Markdown;
use feature qw( signatures );
no warnings qw( experimental::signatures );
-### declare constants
-my $POST_DATA_FILENAME = "/home/compiler/programs/spsp/html/post_data";
-my $TEMP_FILE = "/home/compiler/programs/spsp/html/tmp.$$";
-my $ID_LENGTH = 4;
-my $ID = "id";
-my $TITLE = "title";
-my $STATUS = "status";
-my $TIMESTAMP = "timestamp";
-my $HTML_TITLE = "SPSP";
-my $AUTHOR = "Samir Parikh";
-my $H1_HEADING = "Samir's Updates";
-
-my $cgi = CGI->new;
+# declare constants
+### path to spsp directory
+my $SPSP_PATH = "/home/compiler/programs/spsp";
+
+### file to store json-formatted post history in reverse chronological order
+my $POST_DATA_FILENAME = "$SPSP_PATH/html/post_data";
+
+### temporary file used to create updated post_data json file
+my $POST_DATA_TEMP_FILENAME = "$SPSP_PATH/html/post_data_temp.$$";
+
+### HTML file for all fo our posts
+my $POSTS_HTML_FILENAME = "$SPSP_PATH/html/posts.html";
+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.$$";
+
+### length of random ID assigned to each post
+my $ID_LENGTH = 4;
+
+### hash key constants
+my $ID = "id";
+my $TITLE = "title";
+my $STATUS = "status";
+my $TIMESTAMP = "timestamp";
+
+### HTML constants
+my $HTML_TITLE = "SPSP";
+my $APPLICATION = "Simple Perl Status Poster";
+my $AUTHOR = "Samir Parikh";
+my $HEADING = "Samir's Updates";
+
+my $cgi = CGI->new;
sub generate_post_id ( $id_length )
{
@@ -95,23 +115,65 @@ unshift @posts => \%post;
# write updated posts to temporary file
### open temporary file for writing
-open my $temp_filehandle, '>', $TEMP_FILE
- or die "Cannot open $TEMP_FILE for writing: $!\n";
+open my $temp_filehandle, '>', $POST_DATA_TEMP_FILENAME
+ or die "Cannot open $POST_DATA_TEMP_FILENAME for writing: $!\n";
my $posts = $json_posts->pretty->encode( \@posts );
print $temp_filehandle $posts;
### close the temporary file
-close $temp_filehandle or die "Error closing $TEMP_FILE: $!\n";
+close $temp_filehandle or die "Error closing $POST_DATA_TEMP_FILENAME: $!\n";
### delete original post data file
unlink $POST_DATA_FILENAME
or die "Cannot delete original $POST_DATA_FILENAME: $!\n";
### Rename temporary file to replace original post data file
-rename $TEMP_FILE, $POST_DATA_FILENAME
- or die "Cannot rename $TEMP_FILE to $POST_DATA_FILENAME: $!\n";
+rename $POST_DATA_TEMP_FILENAME, $POST_DATA_FILENAME
+ or die "Cannot rename $POST_DATA_TEMP_FILENAME to $POST_DATA_FILENAME: $!\n";
+
+### recreate new posts.html file by first opening and creating temporary file
+open $temp_filehandle, '>', $POSTS_TEMP_HTML_FILENAME
+ or die "Cannot open $POSTS_TEMP_HTML_FILENAME for writing: $!\n";
+
+print $temp_filehandle <<"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, initial-scale=1">
+ <meta name="author" content="$AUTHOR" >
+ <link rel="icon" type="image/png" href="/assets/favicon.png" >
+ <link rel="stylesheet" href="notsoplainnew.css" type="text/css" >
+</head>
+
+<body>
+<h2>$APPLICATION</h2>
+<h1>$HEADING</h1>
+<hr>
+<p>My first paragraph.</p>
+<p>My second paragraph</p>
+</body>
+
+</html>
+EOF
+
+### TODO: populate the rest of the temporary posts html file using the data
+### already stored in @posts
+
+### close the temporary file
+close $temp_filehandle or die "Error closing $POSTS_TEMP_HTML_FILENAME: $!\n";
+
+### delete original posts.html file
+unlink $POSTS_HTML_FILENAME
+ or die "Cannot delete original $POSTS_HTML_FILENAME: $!\n";
+
+### rename temporary posts html file to replace original post data file
+rename $POSTS_TEMP_HTML_FILENAME, $POSTS_HTML_FILENAME
+ or die "Cannot rename $POSTS_TEMP_HTML_FILENAME to $POSTS_HTML_FILENAME: $!\n";
-print $cgi->redirect( "/post_data" );
+print $cgi->redirect( $POSTS_REL_PATH_FILENAME );
exit 0;