Added the sharing functionality and did a bunch of tweaks to the template to add conditional HTML bits for sharing.

TODO: trigger the POST functionality on a share instead of just occupying wordle-share box.
 Changes to be committed:
	modified:   wordle-life.cgi
This commit is contained in:
peteyboy 2022-02-23 21:21:21 +00:00
parent 44d88a02a3
commit 0dee763f5f
1 changed files with 80 additions and 26 deletions

View File

@ -11,6 +11,9 @@ use Mojo::Template;
use Mojo::Loader 'data_section';
#use Routes::Tiny; #don't even need
use Readonly;
use List::Util qw(first);
#use HTML::Entities;
#use URL::Encode;
#===
# MAKE SURE ORIGIN PAGE MATCHES FILENAME (especially if you are testing changes)!
@ -18,6 +21,8 @@ use Readonly;
#===
my $ORIGIN_PAGE = "/wordle-life.cgi";
my $defaulttext = "Paste your wordle share here (replace this text)";
my $WORDLE_INPUT_PARAM = "wordle-result";
my $SHARENOTE_PARAM = "share-note";
cgi {
@ -41,17 +46,26 @@ use Readonly;
my $wordle;
my $method = $cgi->method;
my $rle = "";
my $isshare = 0; #flag for sharing, default false
my $sharenote = ""; #a note someone can tack onto their share
#Simple switch: make a one page app, and always go back to the same page.
#GET or HEAD loads the page blank, POST runs the Life file maker
if ($method eq 'GET' or $method eq 'HEAD') {
if ($method eq 'HEAD') {
$wordle = $defaulttext;
#$wordle = "";
} elsif ($method eq 'GET') {
#TODO: if there are query parameters put them in $wordle and activate share page logic
if (first { $_ eq $WORDLE_INPUT_PARAM } @{$cgi->query_param_names} ){
$wordle = $cgi->query_param($WORDLE_INPUT_PARAM);
$isshare = 1; #true
$sharenote = $cgi->query_param($SHARENOTE_PARAM);
}else{
$wordle = $defaulttext;
}
} elsif ($method eq 'POST') {
#textarea name, pull the value from POST and reload into textarea
$wordle = $cgi->body_param('wordle-result');
$wordle = $cgi->body_param($WORDLE_INPUT_PARAM);
#if input textarea not changed from default text, do nothing
unless ($wordle =~ /\Q$defaulttext\E/ or $wordle eq '') {
#[Cc] /){ #cheat, if they put in something that looks like an RLE, pass output through
@ -61,7 +75,11 @@ use Readonly;
}else{
$rle = generate_rle($wordle);
}
#$sharequery= "?" . $WORDLE_INPUT_PARAM . "=" . url_escape($rle);
#$template = data_section __PACKAGE__, 'sharepage.html.ep';
#$output = $mt->render($template, { ORIGIN_PAGE =>$ORIGIN_PAGE, defaulttext => $defaulttext, wordle => $wordle, rle => $rle });
}
#$output = $mt->render($template, { WORDLE_INPUT_PARAM => $WORDLE_INPUT_PARAM, ORIGIN_PAGE => $ORIGIN_PAGE, defaulttext => $defaulttext, wordle => $wordle, rle => $rle, sharenote => $sharenote });
} else { #some other request? PUT, DELETE?
$cgi->set_response_status(405)->render;
@ -71,9 +89,11 @@ use Readonly;
#die "Invalid wordle parameter" unless length $wordle;
#Load template from DATA section and output
my $mt = Mojo::Template->new(auto_escape => 1, vars => 1);
my $template = data_section __PACKAGE__, 'index.html.ep';
my $output = $mt->render($template, { ORIGIN_PAGE =>$ORIGIN_PAGE, defaulttext => $defaulttext, wordle => $wordle, rle => $rle });
#my $mt = Mojo::Template->new(auto_escape => 1, vars => 1);
my $mt = Mojo::Template->new(vars => 1);
my $template = data_section __PACKAGE__, 'index.html.ep';
my $output = $mt->render($template, { WORDLE_INPUT_PARAM => $WORDLE_INPUT_PARAM, ORIGIN_PAGE => $ORIGIN_PAGE, defaulttext => $defaulttext, wordle => $wordle, rle => $rle, isshare => $isshare, sharenote => $sharenote });
#my $output = $mt->render($template, { ORIGIN_PAGE =>$ORIGIN_PAGE, defaulttext => $defaulttext, wordle => $wordle, rle => $rle });
#my $output = $mt->render($template, {wordle => $wordle});
$cgi->render(html => $output );
@ -91,7 +111,8 @@ sub generate_rle {
Readonly my $headline_prefix => "#C "; #Part of RLE spec https://conwaylife.com/wiki/Run_Length_Encoded
Readonly my $row_prefix => "x = 5"; #for wordle always 5
Readonly my $col_prefix => ",y = ";
Readonly my $lifeviewer_settings => "\n[[\n GPS 3\n ZOOM 23\n COLOR ALIVE LIME\n COLOR BACKGROUND BLUE\n STOP 100\n GRID\n]]"; #Part of Viewer Spec
Readonly my $lifeviewer_settings => "\n[[\n GPS 3\n ZOOM 23\n COLOR ALIVE LIME\n COLOR BACKGROUND MIDNIGHTBLUE\n STOP 100\n GRID\n]]"; #Part of Viewer Spec
#Readonly my $lifeviewer_settings => "\n[[\n GPS 3\n ZOOM 23\n THEME INVERSE\n STOP 100\n GRID\n]]"; #Part of Viewer Spec
my $wordle = shift; # this is how you get variables in your function call!
$rowcount = 0;
@ -148,7 +169,6 @@ sub generate_rle {
return "$headline\n$row_prefix$col_prefix$rowcount\n$rleline\n$lifeviewer_settings";
}
__DATA__
@@ index.html.ep
@ -160,7 +180,7 @@ __DATA__
<style>
.lv-rle{
min-height:140px;
min-height:200px;
}
</style>
<meta name="LifeViewer" content="viewer textarea"> <!--required tag-->
@ -170,8 +190,22 @@ __DATA__
<body><h1>Wordle->Life</h1>
% if ($isshare) {
<span>
<h3> Hey, someone wants to share their Wordle->Life score with you!</h3>
%if ($sharenote){
<p>They said: <em><%= $sharenote =%></em></p>
%};
<p><strong>Skip to step 2 of the instructions below and see the shared wordle-life happen!</strong></p>
</span>
%}else{
<span>
<p><em>Play your Wordle Share score in <a href=https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life> Conway's Life!</a></em></p>
</span>
% };
<p><em>Play your Wordle Share score in <a href=https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life> Conway's Life!</a></em></p>
<p>
<p> Hey, nerds, isn't it funny how the Wordle Scores can look like a glider in that old computer programming exercise, Conway's Life?</p> <img src="https://conwaylife.com/w/images/e/e2/Lwss.png" alt="Glider image from Conwaylife.org, it looks like an 8-bit staple gun" align= float />
@ -180,11 +214,11 @@ __DATA__
<p> Paste your wordle share below and submit to convert it to a Conway's Life file so you can run it (to learn more about Conway's Life, read <a href=https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life> this)</a>.</p>
<form id="wordle-form" name="wordle-form" action="<%= $ORIGIN_PAGE %>" method="POST" class="flow">
<form id="wordle-form" name="wordle-form" action="<%== $ORIGIN_PAGE %>" method="POST" class="flow">
<label for="wordle">Wordle Share Chart:</label>
<textarea id="wordle" name="wordle-result" rows="9" cols="50" placeholder="<%= $defaulttext %>" style="vertical-align: middle class="lv-rle"">
<%= $wordle %></textarea>
<textarea id="wordle" name="wordle-result" rows="12" cols="50" placeholder="<%== $defaulttext %>" style="vertical-align: middle" class="lv-rle" class="flow">
<%== $wordle %></textarea>
</p>
<br>
</li>
@ -198,16 +232,15 @@ __DATA__
<!--viewer container-->
<div class="viewer" class="flow">
<label for="wordle">
<p>If the conversion is successful, you should see your Wordle converted to a Run Length Encoded Conway Life file in this box:</p>
<p>If the conversion is successful, you should see your Wordle converted to a Run Length Encoded Conway Life file (RLE) in this box:</p>
</label>
<textarea id="life" name="life-file" rows="9" cols="50" style="vertical-align: middle" class="lv-rle" class="flow">
<%= $rle %></textarea>
<label for="wordle">Converted RLE:</label>
<textarea id="life" name="life-file" rows="12" cols="50" style="vertical-align: middle" class="lv-rle" class="flow">
<%== $rle %></textarea>
<p>If the RLE is correct, it should be loaded in the Life Viewer, and so you are ready to go!</p>
<ul>
<li><p>For best results with the Life Viewer, you may adjust the default Zoom and the playback speed, then press the Play button:</p>
<canvas height=400 width=400></canvas></li>
<canvas height=400 width=450></canvas></li>
<li><p>Press "[Pause]" when it seems stable. </p>
</li>
</ul>
@ -221,6 +254,27 @@ __DATA__
<p> This should work with the share/copy button in wordle, or copy/pasting your friends' wordles out of Discord or wherever as well. See whose Wordle Score runs the coolest!
<p>
% if ($rle ne ""){
<span>
<form id="life-share" name="life-share" action="<%== $ORIGIN_PAGE %>" method="GET" class="flow">
<p><strong> Share your wordle-life!</strong>
<p> You can copy/paste the RLE into your favorite social media post or email
<br><em>OR</em><br>
Click this button to go to a share page where you can copy/paste the URL to someone:</p>
<textarea id="share-rle" name="<%= $WORDLE_INPUT_PARAM %>" rows="9" cols="50" style="display:none;">
<%=$rle %></textarea>
<input id="share-note" type="text" name="share-note" placeholder="Include a note, if you'd like" style="vertical-align: bottom">
<input type="submit" value="Go to sharing page">
</form>
% };
</span>
</p>
<p>
<strong>If it doesn't seem to be working:</strong>
<ol>
@ -242,7 +296,7 @@ GRID
</pre code>
<p>(the main thing that needs to be right are the first 3 lines. If the 3rd line has any : symbols, just try deleting them. The stuff between double brackets aren't part of the RLE but are comments that set some defaults for the Life Viewer)</p>
<p>(the main thing that needs to be right are the first 3 lines. If the 3rd line has any colon <strong> :</strong> symbols, just try deleting them. The stuff between double brackets aren't part of the RLE but are comments that set some defaults for the Life Viewer)</p>
<p>For details about the Life RLE file format above, read <a href=https://conwaylife.com/wiki/Run_Length_Encoded>this</a>.</p>
</li>
@ -315,11 +369,11 @@ Hosted by SDF.org
<p>
<form id="wordle-form" name="wordle-form" action="<%= $ORIGIN_PAGE =%>" method="POST" class="flow">
<form id="wordle-form" name="wordle-form" action="<%== $ORIGIN_PAGE =%>" method="POST" class="flow">
<label for="wordle">Wordle Share Chart:</label>
<textarea id="wordle" name="wordle-result" rows="9" cols="50" placeholder="<%= $defaulttext =%>" style="vertical-align: middle; border:none;">
<%= $wordle %>
<textarea id="wordle" name="wordle-result" rows="9" cols="50" placeholder="<%== $defaulttext =%>" style="vertical-align: middle; border:none;">
<%== $wordle %>
</textarea>
</p>
<br>
@ -332,7 +386,7 @@ Hosted by SDF.org
</ol>
<label for="life">Your wordle as Conway Life RLE file:</label>
<textarea id="life" name="Life file" rows="9" cols="50" style="vertical-align: middle; border:none;">
<%= $rle %>
<%== $rle %>
</textarea>