From: Giuseppe Bilotta Date: Mon, 23 Sep 2013 20:20:11 +0000 (+0200) Subject: Put size-loading code in own routine, init revacros and acros at the same time X-Git-Url: http://git.oblomov.eu/svg-meme/commitdiff_plain Put size-loading code in own routine, init revacros and acros at the same time --- diff --git a/src/meme.fcgi b/src/meme.fcgi index f47dfe9..9c2d8a7 100755 --- a/src/meme.fcgi +++ b/src/meme.fcgi @@ -16,7 +16,7 @@ use File::Basename; use Scalar::Util qw(looks_like_number); my %sizes; # meme base image sizes -my %acros; # meme base acronyms (BLB => bad-luck-brian.jpg) +my (%acros, %revacros); # meme base acronyms (BLB => bad-luck-brian.jpg) and reverse # Are we running as CGI or from the command-line? # TODO better detection @@ -26,38 +26,39 @@ my $script_path = $is_cgi ? dirname($ENV{'SCRIPT_FILENAME'}) : dirname($0); my $sz_fname = $script_path . '/meme-sizes.lst'; -open FILE, $sz_fname or die $!; - -while (my $line = ) { - chomp($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 shortening; 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"; +sub load_sizes() { + open FILE, $sz_fname or die $!; + + while (my $line = ) { + chomp($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 shortening; 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; + $revacros{$fname} = $acro; + } else { + print STDERR "Trying to redefined acronym $acro from $acros{$acro} to ${fname}\n"; + } } -} - -my %revacros = reverse %acros; -close FILE; + close FILE; +} # params: img, width, height, font-size, text @@ -217,6 +218,8 @@ sub make_svg(%) { my %p; +load_sizes(); + while (my $q = new CGI::Fast) { my (@t, @fs);