Add optional modifiers to factoid variables

Adlib list variables can now accept trailing modifier keywords prefixed with
a colon. These can be chained together to combine their effects.

     :uc - uppercases the expansion
     :lc - lowercases the expansion
     :ucfirst - uppercases the first letter in the expansion
     :title - lowercases the expansion and then uppercases the first letter
              (effectively an alias for :lc:ucfirst)

Examples:
   <pragma_> echo $colors:uc
   <candide> RED
   <pragma_> echo $colors:ucfirst
   <candide> Blue
This commit is contained in:
Pragmatic Software 2015-09-14 10:22:55 -07:00
parent d6319b27c5
commit aed3f34c43
1 changed files with 34 additions and 2 deletions

View File

@ -8,6 +8,9 @@ package PBot::Factoids;
use warnings;
use strict;
use feature 'switch';
no if $] >= 5.018, warnings => "experimental::smartmatch";
use HTML::Entities;
use Time::HiRes qw(gettimeofday);
use Carp ();
@ -348,11 +351,18 @@ sub find_factoid {
sub expand_factoid_vars {
my ($self, $from, $action) = @_;
while ($action =~ /(?<!\\)\$([a-zA-Z0-9_\-]+)/g) {
while ($action =~ /(?<!\\)\$([a-zA-Z0-9_:\-]+)/g) {
my $v = $1;
next if $v =~ m/^(nick|channel|randomnick)$/; # don't override special variables
my $modifier = '';
if ($v =~ s/(:.*)$//) {
$modifier = $1;
}
my @factoids = $self->find_factoid($from, $v, undef, 0, 1);
next if not @factoids;
my ($var_chan, $var) = ($factoids[0]->[0], $factoids[0]->[1]);
if ($self->{factoids}->hash->{$var_chan}->{$var}->{type} eq 'text') {
@ -364,7 +374,27 @@ sub expand_factoid_vars {
}
my $line = int(rand($#mylist + 1));
$mylist[$line] =~ s/"//g;
$action =~ s/\$$var/$mylist[$line]/;
foreach my $mod (split /:/, $modifier) {
given ($mod) {
when ('uc') {
$mylist[$line] = uc $mylist[$line];
}
when ('lc') {
$mylist[$line] = lc $mylist[$line];
}
when ('ucfirst') {
$mylist[$line] = ucfirst $mylist[$line];
}
when ('title') {
$mylist[$line] = lc $mylist[$line];
$mylist[$line] = ucfirst $mylist[$line];
}
}
}
$action =~ s/\$$var$modifier/$mylist[$line]/;
}
}
@ -641,6 +671,8 @@ sub interpreter {
return "/msg $nick $ref_from$keyword is currently disabled.";
}
$action = $self->expand_factoid_vars($from, $action);
$action =~ s/\$nick/$nick/g;
$action =~ s/\$channel/$from/g;
$action =~ s/\$randomnick/my $random = $self->{pbot}->{nicklist}->random_nick($from); $random ? $random : $nick/ge;