3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-29 23:39:24 +01:00

Factoids: add more :modifiers to $var expansion: index(x), pick(x, y), enumerate, etc

This commit is contained in:
Pragmatic Software 2020-05-29 22:18:07 -07:00
parent 8985620970
commit 669bf7460c

View File

@ -481,9 +481,9 @@ sub expand_factoid_vars {
$self->{pbot}->{logger}->log("action: $const_action\n") if $debug; $self->{pbot}->{logger}->log("action: $const_action\n") if $debug;
while ($const_action =~ /(\ba\s*|\ban\s*)?(?<!\\)\$(?:(\{[a-zA-Z0-9_:#]+\}|[a-zA-Z0-9_:#]+))/gi) { while ($const_action =~ /(\ba\s*|\ban\s*)?(?<!\\)\$(?:(\{[a-zA-Z0-9_#]+(?::[a-zA-Z0-9_(),.#:-]+)?\}|[a-zA-Z0-9_#]+(?::[a-zA-Z0-9_(),.#:-]+)?))/gi) {
my ($a, $v) = ($1, $2); my ($indefinite_article, $v) = ($1, $2);
$a = '' if not defined $a; $indefinite_article = '' if not defined $indefinite_article;
next if not defined $v; next if not defined $v;
my $original_v = $v; my $original_v = $v;
@ -525,46 +525,153 @@ sub expand_factoid_vars {
if ($self->{factoids}->get_data($var_chan, $var, 'type') eq 'text') { if ($self->{factoids}->get_data($var_chan, $var, 'type') eq 'text') {
my $change = $self->{factoids}->get_data($var_chan, $var, 'action'); my $change = $self->{factoids}->get_data($var_chan, $var, 'action');
my @list = $self->{pbot}->{interpreter}->split_line($change); my @list = $self->{pbot}->{interpreter}->split_line($change);
my @mylist; my @mylist;
for (my $i = 0; $i <= $#list; $i++) { push @mylist, $list[$i] if defined $list[$i] and length $list[$i]; } for (my $i = 0; $i <= $#list; $i++) {
my $line = int(rand($#mylist + 1)); push @mylist, $list[$i] if defined $list[$i] and length $list[$i];
if (not $mylist[$line] =~ s/^"(.*)"$/$1/) { $mylist[$line] =~ s/^'(.*)'$/$1/; } }
my %settings;
foreach my $mod (split /:/, $modifier) {
next if not length $mod;
if ($mod eq 'enumerate') {
$settings{'enumerate'} = 1;
next;
}
if ($mod =~ /^sort\+?$/) {
$settings{'sort+'} = 1;
next;
}
if ($mod =~ /^sort\-$/) {
$settings{'sort-'} = 1;
next;
}
if ($mod =~ /^pick\((\d+),(\d+)\)$/) {
$settings{'pick'} = 1;
$settings{'random'} = 1;
$settings{'pick_min'} = $1;
$settings{'pick_max'} = $2;
next;
}
if ($mod =~ /^pick\((\d+)\)$/) {
$settings{'pick'} = 1;
$settings{'pick_min'} = 1;
$settings{'pick_max'} = $1;
next;
}
if ($mod =~ /^pick_unique\((\d+),(\d+)\)$/) {
$settings{'pick'} = 1;
$settings{'random'} = 1;
$settings{'unique'} = 1;
$settings{'pick_min'} = $1;
$settings{'pick_max'} = $2;
next;
}
if ($mod =~ /^pick_unique\((\d+)\)$/) {
$settings{'pick'} = 1;
$settings{'unique'} = 1;
$settings{'pick_min'} = 1;
$settings{'pick_max'} = $1;
next;
}
if ($mod =~ /^index\((\d+)\)$/) {
$settings{'index'} = $1;
next;
}
}
my $replacement;
if ($settings{'index'}) {
my $index = $settings{'index'};
$index = 0 if $index < 0;
$index = $#mylist if $index > $#mylist;
$replacement = $mylist[$index];
} elsif ($settings{'pick'}) {
my $min = $settings{'pick_min'};
my $max = $settings{'pick_max'};
$max = $#mylist if $max > $#mylist;
my $count = $max;
if ($settings{'random'}) {
$count = int rand ($max + 1 - $min) + $min;
}
@list = ();
while ($count-- > 0) {
my $index = int rand @mylist;
my $choice = $mylist[$index];
if ($settings{'unique'}) {
splice @mylist, $index, 1;
}
push @list, $choice;
}
if ($settings{'sort+'}) {
@list = sort { $a cmp $b } @list;
}
if ($settings{'sort-'}) {
@list = sort { $b cmp $a } @list;
}
if ($settings{'enumerate'}) {
$replacement = join ', ', @list;
$replacement =~ s/(.*), /$1 and /;
} else {
$replacement = "@list";
}
} else {
$replacement = $mylist[rand @mylist];
}
# strip outer quotes
if (not $replacement =~ s/^"(.*)"$/$1/) { $replacement =~ s/^'(.*)'$/$1/; }
foreach my $mod (split /:/, $modifier) { foreach my $mod (split /:/, $modifier) {
next if not length $mod; next if not length $mod;
if ($mylist[$line] =~ /^\$\{\$([a-zA-Z0-9_:#]+)\}(.*)$/) { if ($replacement =~ /^\$\{\$([a-zA-Z0-9_:#]+)\}(.*)$/) {
$mylist[$line] = "\${\$$1:$mod}$2"; $replacement = "\${\$$1:$mod}$2";
next; next;
} elsif ($mylist[$line] =~ /^\$\{([a-zA-Z0-9_:#]+)\}(.*)$/) { } elsif ($replacement =~ /^\$\{([a-zA-Z0-9_:#]+)\}(.*)$/) {
$mylist[$line] = "\${$1:$mod}$2"; $replacement = "\${$1:$mod}$2";
next; next;
} elsif ($mylist[$line] =~ /^\$\$([a-zA-Z0-9_:#]+)(.*)$/) { } elsif ($replacement =~ /^\$\$([a-zA-Z0-9_:#]+)(.*)$/) {
$mylist[$line] = "\${\$$1:$mod}$2"; $replacement = "\${\$$1:$mod}$2";
next; next;
} elsif ($mylist[$line] =~ /^\$([a-zA-Z0-9_:#]+)(.*)$/) { } elsif ($replacement =~ /^\$([a-zA-Z0-9_:#]+)(.*)$/) {
$mylist[$line] = "\${$1:$mod}$2"; $replacement = "\${$1:$mod}$2";
next; next;
} }
given ($mod) { given ($mod) {
when ('uc') { $mylist[$line] = uc $mylist[$line]; } when ('uc') { $replacement = uc $replacement; }
when ('lc') { $mylist[$line] = lc $mylist[$line]; } when ('lc') { $replacement = lc $replacement; }
when ('ucfirst') { $mylist[$line] = ucfirst $mylist[$line]; } when ('ucfirst') { $replacement = ucfirst $replacement; }
when ('title') { when ('title') {
$mylist[$line] = ucfirst lc $mylist[$line]; $replacement = ucfirst lc $replacement;
$mylist[$line] =~ s/ (\w)/' ' . uc $1/ge; $replacement =~ s/ (\w)/' ' . uc $1/ge;
} }
when ('json') { $mylist[$line] = $self->escape_json($mylist[$line]); } when ('json') { $replacement = $self->escape_json($replacement); }
} }
} }
my $replacement = $mylist[$line]; if ($indefinite_article) {
my $fixed_article = select_indefinite_article $replacement;
if ($a) { $fixed_article = ucfirst $fixed_article if $indefinite_article =~ m/^A/;
my $fixed_a = select_indefinite_article $mylist[$line]; $replacement = "$fixed_article $replacement";
$fixed_a = ucfirst $fixed_a if $a =~ m/^A/;
$replacement = "$fixed_a $mylist[$line]";
} }
if ($debug and $offset == 0) { $self->{pbot}->{logger}->log(("-" x 40) . "\n"); } if ($debug and $offset == 0) { $self->{pbot}->{logger}->log(("-" x 40) . "\n"); }
@ -572,7 +679,7 @@ sub expand_factoid_vars {
$original_v = quotemeta $original_v; $original_v = quotemeta $original_v;
$original_v =~ s/\\:/:/g; $original_v =~ s/\\:/:/g;
if (not length $mylist[$line]) { if (not length $replacement) {
$self->{pbot}->{logger}->log("No length!\n") if $debug; $self->{pbot}->{logger}->log("No length!\n") if $debug;
if ($debug) { if ($debug) {
$self->{pbot}->{logger}->log("before: v: $original_v, offset: $offset\n"); $self->{pbot}->{logger}->log("before: v: $original_v, offset: $offset\n");
@ -580,7 +687,7 @@ sub expand_factoid_vars {
$self->{pbot}->{logger}->log((" " x $offset) . "^\n"); $self->{pbot}->{logger}->log((" " x $offset) . "^\n");
} }
substr($action, $offset) =~ s/$a\$$original_v ?/$replacement/; substr($action, $offset) =~ s/$indefinite_article\$$original_v ?/$replacement/;
$offset += $-[0] + length $replacement; $offset += $-[0] + length $replacement;
if ($debug) { if ($debug) {
@ -595,7 +702,7 @@ sub expand_factoid_vars {
$self->{pbot}->{logger}->log((" " x $offset) . "^\n"); $self->{pbot}->{logger}->log((" " x $offset) . "^\n");
} }
substr($action, $offset) =~ s/$a\$$original_v/$replacement/; substr($action, $offset) =~ s/$indefinite_article\$$original_v/$replacement/;
$offset += $-[0] + length $replacement; $offset += $-[0] + length $replacement;
if ($debug) { if ($debug) {