SedRegex: allow matching text with the trailing suffix missing

From: 866875ec5d
This commit is contained in:
James Lu 2020-04-02 09:59:13 -07:00
parent c399272173
commit c212ee0e5e
4 changed files with 60 additions and 3 deletions

View File

@ -49,10 +49,12 @@ __url__ = 'https://github.com/ProgVal/Limnoria/tree/master/plugins/SedRegex'
from . import config
from . import plugin
from . import constants
from importlib import reload
reload(config)
reload(plugin)
reload(constants)
if world.testing:
from . import test

26
plugins/SedRegex/constants.py Executable file
View File

@ -0,0 +1,26 @@
#!/usr/bin/env python3
import re
SED_REGEX = re.compile(
# This part matches an optional nick followed by ":" or ",", used to direct replacement
# at a particular user.
r"^(?:(?P<nick>.+?)[:,] )?"
# Match and save the delimiter (any one symbol) as a named group
r"s(?P<delim>[^\w\s])"
# Match the pattern to replace, which can be any string up to the first instance of the delimiter
r"(?P<pattern>(?:(?!(?P=delim)).)*)(?P=delim)"
# Ditto with the replacement
r"(?P<replacement>(?:(?!(?P=delim)).)*)"
# Optional final delimiter plus flags at the end
r"(?:(?P=delim)(?P<flags>[a-z]*))?"
)
if __name__ == '__main__':
print("This is the full regex used by the plugin; paste it into your favourite regex tester "
"for debugging:")
print(SED_REGEX)

View File

@ -1,6 +1,6 @@
###
# Copyright (c) 2015, Michael Daniel Telatynski <postmaster@webdevguru.co.uk>
# Copyright (c) 2015-2019, James Lu <james@overdrivenetworks.com>
# Copyright (c) 2015-2020, James Lu <james@overdrivenetworks.com>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
@ -52,8 +52,7 @@ if sys.version_info[0] < 3:
'supports Python 2, consult the python2-legacy branch at '
'https://github.com/jlu5/SupyPlugins/tree/python2-legacy')
SED_REGEX = re.compile(r"^(?:(?P<nick>.+?)[:,] )?s(?P<delim>[^\w\s])(?P<pattern>.*?)(?P=delim)"
r"(?P<replacement>.*?)(?P=delim)(?P<flags>[a-z]*)$")
from .constants import *
# Replace newlines and friends with things like literal "\n" (backslash and "n")
axe_spaces = utils.str.MultipleReplacer({'\n': '\\n', '\t': '\\t', '\r': '\\r'})

View File

@ -187,6 +187,36 @@ class SedRegexTestCase(ChannelPluginTestCase):
m = self.getMsg(' ', timeout=1)
self.assertIn('timed out', str(m))
def testMissingTrailingSeparator(self):
# Allow the plugin to work if you miss the trailing /
self.feedMsg('hello world')
self.feedMsg('s/world/everyone')
m = self.getMsg(' ')
self.assertIn('hello everyone', str(m))
# Make sure it works if there's a space in the replacement
self.feedMsg('hello world')
self.feedMsg('s@world@how are you')
m = self.getMsg(' ')
self.assertIn('hello how are you', str(m))
# Ditto with a space in the original text
self.feedMsg("foo bar @ baz")
self.feedMsg('s/bar @/and')
m = self.getMsg(' ')
self.assertIn('foo and baz', str(m))
def testIgnoreTextAfterTrailingSeparator(self):
# https://github.com/jlu5/SupyPlugins/issues/59
self.feedMsg('see you ltaer')
self.feedMsg('s/ltaer/later/ this text will be ignored')
m = self.getMsg(' ')
self.assertIn('see you later', str(m))
self.feedMsg('s/LTAER/later, bye/i <extra text>')
m = self.getMsg(' ')
self.assertIn('see you later, bye', str(m))
# TODO: test ignores
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: