From 4fca53347f82c5f3ec6d50ff5fb940e1b7b76e81 Mon Sep 17 00:00:00 2001 From: Giuseppe Bilotta Date: Sun, 22 Sep 2013 08:43:43 +0200 Subject: [PATCH] Meme short forms --- meme.fcgi | 54 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 41 insertions(+), 13 deletions(-) diff --git a/meme.fcgi b/meme.fcgi index 939a8f4..0296168 100755 --- a/meme.fcgi +++ b/meme.fcgi @@ -8,7 +8,8 @@ use HTML::Entities; use File::Basename; -my %sizes; +my %sizes; # meme base image sizes +my %acros; # meme base acronyms (BLB => bad-luck-brian.jpg) my $script_path = $ENV{'SCRIPT_FILENAME'} ? dirname($ENV{'SCRIPT_FILENAME'}) : dirname($0); @@ -21,6 +22,26 @@ while (my $line = ) { next unless $line; my ($width, $height, $fname) = split(/ /, $line, 3); $sizes{$fname} = [$width, $height]; + + # Find a potential short form (acronym for multiword, no extension otherwise) + my $acro = ''; + + # remove article for the purpose of the shrotening; we don't care if it's + # in the middle of a word because we only care about initials anyway + # FIXME this actually fails in the case of XXXthe-XXX, let's care about that + # when we actually come across it + my $the = $fname; + $the =~ s/the-//g; + if ($the =~ /-/) { + $acro = join('', map { uc(substr($_, 0, 1)) } split(/-/, $the )); + } else { + $acro = (split(/\./, $the))[0] + } + if (!defined $acros{$acro}) { + $acros{$acro} = $fname; + } else { + print STDERR "Trying to redefined acronym $acro from $acros{$acro} to ${fname}\n"; + } } close FILE; @@ -49,20 +70,27 @@ TXT while (my $q = new CGI::Fast) { my $img = $q->param('m') || (keys %sizes)[0]; if (!defined $sizes{$img}) { - print $q->header(-status=>404), - $q->start_html("Unknown meme base"), - $q->h1("Unknown meme base!"); - print "

Sorry, '" . encode_entities($img) . "' is not a known meme base. ". + if (!defined $acros{$img}) { + print $q->header(-status=>404), + $q->start_html("Unknown meme base"), + $q->h1("Unknown meme base!"); + print "

Sorry, '" . encode_entities($img) . "' is not a known meme base. ". "You want one of the following instead:

"; + # foreach (keys %ENV) { + # print "

$_=$ENV{$_}

" + # } + print $q->end_html(); + next; + } else { + $img = $acros{$img}; } - print ""; - # foreach (keys %ENV) { - # print "

$_=$ENV{$_}

" - # } - print $q->end_html(); - next; } print $q->header( -- 2.32.0.93.g670b81a890