From 15313e924f42df28c541eefe921c6d2f43e67279 Mon Sep 17 00:00:00 2001 From: peteyboy Date: Wed, 20 Apr 2022 06:16:27 +0000 Subject: [PATCH] Making sdf branch out of current wordle-life-x.cgi, contains regexes for Duppy's wordum guessum On branch sdf Changes to be committed: modified: wordle-life.cgi --- wordle-life.cgi | 148 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 118 insertions(+), 30 deletions(-) diff --git a/wordle-life.cgi b/wordle-life.cgi index 9f85bc5..af8f8c2 100755 --- a/wordle-life.cgi +++ b/wordle-life.cgi @@ -9,16 +9,18 @@ use lib qw( /usr/pkg/lib/perl5/5.24.0 /meta/p/peteyboy/perl5/lib/perl5 ); use CGI::Tiny; use Mojo::Template; use Mojo::Loader 'data_section'; -#use Routes::Tiny; #don't even need use Readonly; -use List::Util qw(first); +#use List::Util qw(first); +use URI; +use experimental 'smartmatch'; #=== # MAKE SURE ORIGIN PAGE MATCHES FILENAME (especially if you are testing changes)! # #=== - my $ORIGIN_PAGE = "/wordle-life.cgi"; + my $ORIGIN_PAGE = "/wordle-life-x.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"; @@ -26,11 +28,10 @@ use List::Util qw(first); #error handler from the CGI:Tiny cookbook my $cgi = $_; - $cgi->set_error_handler(sub { my ($cgi, $error, $rendered) = @_; warn $error; - unless ($rendered) { + unless ($rendered) { if ($cgi->response_status_code == 413) { $cgi->render(json => {error => 'Request body limit exceeded'}); }elsif ($cgi->response_status_code == 400) { @@ -46,6 +47,8 @@ use List::Util qw(first); my $rle = ""; my $isshare = 0; #flag for sharing, default false my $sharenote = ""; #a note someone can tack onto their share + my $navigate = 0; #flag to go to different page, default false + #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 @@ -53,8 +56,13 @@ use List::Util qw(first); $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} ){ + #Check path_info to see if we are navigating (ignoring sharing query params if there?) + #if (first { $_ eq $PATH_PARAM } @{$cgi->query_param_names} ){ + if ( $PATH_PARAM ~~ @{$cgi->query_param_names} ){ + $navigate = mock_path_info($cgi->query_param($PATH_PARAM)); #true and populated + } + #if there are sharing query parameters put them in $wordle and activate share page logic + elsif ($WORDLE_INPUT_PARAM ~~ @{$cgi->query_param_names} ){ $wordle = $cgi->query_param($WORDLE_INPUT_PARAM); $isshare = 1; #true $sharenote = $cgi->query_param($SHARENOTE_PARAM); @@ -73,9 +81,6 @@ use List::Util qw(first); }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 }); @@ -84,22 +89,36 @@ use List::Util qw(first); exit; } - die "Invalid wordle parameter" unless length $wordle; - + #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}); - + my $template; + my $output; + + #TODO: this is ugly; clean up + if ($method eq 'GET' && $navigate ) { + my $template_name = template_from_path_info ($navigate); + #TODO: Check against a list of valid templates! + #if ( grep ( /^$template_name$/, ('index.html.ep', 'about.html.ep') )) { + if ( $template_name ~~ ['index.html.ep', 'about.html.ep'] ) { + $template = data_section __PACKAGE__, $template_name; + $output = $mt->render($template, { 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 = data_section __PACKAGE__, 'index.html.ep'; + $output = $mt->render($template, { WORDLE_INPUT_PARAM => $WORDLE_INPUT_PARAM, ORIGIN_PAGE => $ORIGIN_PAGE, defaulttext => $defaulttext, wordle => $wordle, rle => $rle, isshare => $isshare, sharenote => $sharenote }); + } + }else{ + $template = data_section __PACKAGE__, 'index.html.ep'; + $output = $mt->render($template, { WORDLE_INPUT_PARAM => $WORDLE_INPUT_PARAM, ORIGIN_PAGE => $ORIGIN_PAGE, defaulttext => $defaulttext, wordle => $wordle, rle => $rle, isshare => $isshare, sharenote => $sharenote }); + } $cgi->render(html => $output ); }; #Try to Generate the RLE. Makes a lot of assumptions -sub generate_rle { +sub generate_rle (\$) { my $headline = ""; my $rleline = ""; @@ -132,6 +151,7 @@ sub generate_rle { $myline =~ s/:green_square:/o/g; $myline =~ s/:blue_square:/o/g; #HC equiv yellow $myline =~ s/:orange_square:/o/g; #HC equiv green + $myline =~ s/:red_square:/o/g; #wordum red fails $myline =~ s/:[a-z_]*square:/b/g; @@ -140,6 +160,7 @@ sub generate_rle { $myline =~ s/\N{U+1f7e9}/o/g; #green hit $myline =~ s/\N{U+1F7E6}/o/g; #blue $myline =~ s/\N{U+1F7E7}/o/g; #orange + $myline =~ s/\N{U+1F7E5}/o/g; #red $myline =~ s/\N{U+2B1B}/b/g; #black @@ -152,6 +173,7 @@ sub generate_rle { $myline =~ s/green_square/o/g; $myline =~ s/blue_square/o/g; $myline =~ s/orange_square/o/g; + $myline =~ s/red_square/o/g; #wordum red fails @@ -167,6 +189,29 @@ sub generate_rle { return "$headline\n$row_prefix$col_prefix$rowcount\n$rleline\n$lifeviewer_settings"; } + + + +#Because SDF's nginx config is broken, we have to use query params to *navigate* +sub mock_path_info { + my $uri = URI->new (shift); + return $uri->path; +} + + +sub template_from_path_info { + my $template=''; + my $path = shift; + if ($path =~ /^\//){ + $template = $path; + $template =~ s/^.//; + $template =~ tr/\//./; + $template = $template . ".html.ep"; + } + return $template; +} + + __DATA__ @@ index.html.ep @@ -178,7 +223,7 @@ __DATA__ @@ -188,12 +233,13 @@ __DATA__

Wordle->Life

+

about % if ($isshare) {

Hey, someone wants to share their Wordle->Life score with you!

%if ($sharenote){ -

They said: <%= $sharenote =%>

+

They said:<%= $sharenote %>

%};

Skip to step 2 of the instructions below and see the shared wordle-life happen!

@@ -230,15 +276,16 @@ __DATA__
- -

If the RLE is correct, it should be loaded in the Life Viewer, and so you are ready to go!

+ @@ -258,15 +305,16 @@ __DATA__ % if ($rle ne ""){ +

Share your wordle-life!

+

You can copy/paste the RLE into your favorite social media post or email

+

OR

+

Click this button to go to a share page where you can copy/paste the url to someone

-

Share your wordle-life! -

You can copy/paste the RLE into your favorite social media post or email -
OR
- Click this button to go to a share page where you can copy/paste the URL to someone:

- +<%=$rle %> + +
% };
@@ -456,3 +504,43 @@ Hosted by SDF.org

SDF.org + + +@@ about.html.ep + + +About Wordle-Life + + + + + + +

Wordle->Life

+ +% my $home_path= "$ORIGIN_PAGE"; +% my $current_path = "$home_path?path_info=/about"; + +

+

This is an about page.

+ +

Click >here to go Home

+

Click >here to stay put

+ +

+ +


+Disclaimer: I made this! People and/or corporations may own their marks or copyrights, etc, on words mentioned on this page I don't claim to + +

+Questions, kudos or comments, mail me @sdf.org +

+Thanks to hankchizljaw for making his basic css publicly available + +

Made in February 2022 +

+

+Hosted by SDF.org +

SDF.org +

+