diff --git a/wordle-life.cgi b/wordle-life.cgi index 9f85bc5..12f64b6 100755 --- a/wordle-life.cgi +++ b/wordle-life.cgi @@ -1,102 +1,91 @@ -#!/usr/pkg/bin/perl -#Above is the particular location for system binaries in SDF's metaarray, not the usual spot +#!/usr/bin/perl use strict; use warnings; use utf8; -#add my user perl library path, this is particular to each person's setup -use lib qw( /usr/pkg/lib/perl5/5.24.0 /meta/p/peteyboy/perl5/lib/perl5 ); -#make request for all of the following -use CGI::Tiny; -use Mojo::Template; -use Mojo::Loader 'data_section'; -#use Routes::Tiny; #don't even need +use Mojolicious::Lite -signatures; use Readonly; use List::Util qw(first); +use experimental 'smartmatch'; + #=== # MAKE SURE ORIGIN PAGE MATCHES FILENAME (especially if you are testing changes)! # #=== - 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"; +my $ORIGIN_PAGE = "/cgi-bin/wordle-life-mojo.cgi"; +my $defaulttext = "Paste your wordle share here (replace this text)"; +my $PATH_PARAM = "PATH_INFO"; +my $WORDLE_INPUT_PARAM = "wordle-result"; +my $SHARENOTE_PARAM = "share-note"; - cgi { +my $wordle; +my $rle = ""; +my $isshare = 0; #flag for sharing, default false +my $sharenote = ""; #a note someone can tack onto their share - #error handler from the CGI:Tiny cookbook - my $cgi = $_; +get '/' => sub ($cgi) { - $cgi->set_error_handler(sub { - my ($cgi, $error, $rendered) = @_; - warn $error; - unless ($rendered) { - if ($cgi->response_status_code == 413) { - $cgi->render(json => {error => 'Request body limit exceeded'}); - }elsif ($cgi->response_status_code == 400) { - $cgi->render(json => {error => 'Bad request'}); - } else { - $cgi->render(json => {error => 'Internal server error'}); - } - } - }); - - 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 'HEAD') { + #if there are sharing query parameters put them in $wordle and activate share page logic + if (first { $_ eq $WORDLE_INPUT_PARAM } @{$cgi->req->query_params} ){ + $wordle = $cgi->param($WORDLE_INPUT_PARAM); + $isshare = 1; #true + $sharenote = $cgi->param($SHARENOTE_PARAM); + }else{ $wordle = $defaulttext; + } - } 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_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 - if ($wordle =~ /^#[Cc] /){ + my $template_name = 'index'; + $cgi->stash( WORDLE_INPUT_PARAM => $WORDLE_INPUT_PARAM, ORIGIN_PAGE => $ORIGIN_PAGE, defaulttext => $defaulttext, wordle => $wordle, rle => $rle, isshare => $isshare, sharenote => $sharenote ); + $cgi->render(template => $template_name); + + +}; + +get '/*path_info' => sub ($cgi) { + my $template_name = $cgi->stash("path_info"); + #TODO: Check against a list of valid templates! + #if ( grep ( /^$template_name$/, ('index', 'about') )) { + if ( $template_name ~~ ['about'] ) { + $cgi->stash( ORIGIN_PAGE => $ORIGIN_PAGE); #this isn't general purpose, what template needs what variables? + }else{ #TODO: fix so as not to need copied code from the else below? + $template_name = 'index'; + $cgi->stash( WORDLE_INPUT_PARAM => $WORDLE_INPUT_PARAM, ORIGIN_PAGE => $ORIGIN_PAGE, defaulttext => $defaulttext, wordle => $wordle, rle => $rle, isshare => $isshare, sharenote => $sharenote ); + } + $cgi->render(template => $template_name); +}; + +post '/' => sub($cgi) { + #textarea name, pull the value from POST and reload into textarea + $wordle = $cgi->param($WORDLE_INPUT_PARAM); + #if input textarea not changed from default text, do nothing + unless ($wordle =~ /\Q$defaulttext\E/ or $wordle eq '') { + #if they put in something that looks like an RLE, pass output through, for share or to just use the life viewer + if ($wordle =~ /^#[Cc] /){ $rle = $wordle; #if none of that, do the thing: generate RLE - }else{ + }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; - exit; - } - - 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 $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 ); - - }; + $cgi->stash( WORDLE_INPUT_PARAM => $WORDLE_INPUT_PARAM, ORIGIN_PAGE => $ORIGIN_PAGE, defaulttext => $defaulttext, wordle => $wordle, rle => $rle, isshare => $isshare, sharenote => $sharenote ); + $cgi->render(template =>'index'); +}; + +app->start; + + + +sub template_from_path_info { + my $template=''; + my $path = shift; + if ($path =~ /^\//){ + $template = $path; + $template =~ s/^.//; + $template =~ tr/\//./; + } + return $template; +} #Try to Generate the RLE. Makes a lot of assumptions sub generate_rle { @@ -177,17 +166,21 @@ __DATA__ - - + -
- -
-
- -
-Questions, kudos or comments, mail me @sdf.org -
-Thanks to the community at conwaylife.com, and the friendly folks in the forums there, - and for their Javascript Life Viewer, which I use here. -
- --Thanks to hankchizljaw for making his basic css publicly available -
-Made in February 2022
--
Play your Wordle Share score in Conway's Life!
-
Hey, nerds, isn't it funny how the Wordle Scores can look like a glider in that old computer programming exercise, Conway's Life?
+This is an about page.
-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 this -
Click >here to return to Wordle-Life page
+Click here to go to website Home
- -
if there is extra text or lines in your paste, you might not get a valid file here, so look and make sure it looks like a proper RLE file, for example: -
-
-#C Wordle 235 3/6 -x = 5,y = 3 -boobo$bobbo$ooooo$ -- -
For details about the Life RLE file format above, read this. - -
-Next steps: - - - - - - - -
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 looks coolest! -
- - -
-
-
-Questions, kudos or comments, mail me @sdf.org
Thanks to hankchizljaw for making his basic css publicly available -
Made in February 2022 +
Made in February 2022, revised April 2022 for this site, using Perl Mojolicious