3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-13 23:49:31 +01:00
pbot/Plugins/FuncSed.pm

64 lines
1.7 KiB
Perl
Raw Normal View History

2020-02-14 07:41:51 +01:00
# File: FuncSed.pm
# Author: pragma-
#
# Purpose: Registers the sed Function
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
package Plugins::FuncSed;
use parent 'Plugins::Plugin';
use warnings; use strict;
use feature 'unicode_strings';
sub initialize {
2020-02-15 23:38:32 +01:00
my ($self, %conf) = @_;
$self->{pbot}->{functions}->register(
'sed',
{
desc => 'a sed-like stream editor',
usage => 'sed s/<regex>/<replacement>/[Pig]; P preserve case; i ignore case; g replace all',
subref => sub { $self->func_sed(@_) }
}
);
2020-02-14 07:41:51 +01:00
}
sub unload {
2020-02-15 23:38:32 +01:00
my $self = shift;
$self->{pbot}->{functions}->unregister('sed');
2020-02-14 07:41:51 +01:00
}
# near-verbatim insertion of krok's `sed` factoid
no warnings;
2020-02-15 23:38:32 +01:00
sub func_sed {
my $self = shift;
my $text = "@_";
if ($text =~ /^s(.)(.*?)(?<!\\)\1(.*?)(?<!\\)\1(\S*)\s+(.*)/p) {
my ($a, $r, $g, $m, $t) = ($5, "'\"$3\"'", index($4, "g") != -1, $4, $2);
2020-05-02 05:59:51 +02:00
print "a: $a, r: $r, g: $g, m: $m, t: $t\n";
print "text: [$text]\n";
2020-02-15 23:38:32 +01:00
if ($m =~ /P/) {
$r =~ s/^'"(.*)"'$/$1/;
$m =~ s/P//g;
if ($g) { $a =~ s|(?$m)($t)|$1=~/^[A-Z][^A-Z]/?ucfirst$r:($1=~/^[A-Z]+$/?uc$r:$r)|gie; }
else { $a =~ s|(?$m)($t)|$1=~/^[A-Z][^A-Z]/?ucfirst$r:($1=~/^[A-Z]+$/?uc$r:$r)|ie; }
} else {
if ($g) { $a =~ s/(?$m)$t/$r/geee; }
else { $a =~ s/(?$m)$t/$r/eee; }
}
return $a;
2020-02-14 07:41:51 +01:00
} else {
2020-02-15 23:38:32 +01:00
return "sed: syntax error";
2020-02-14 07:41:51 +01:00
}
}
use warnings;
1;