From c212ee0e5e8ad717cb28e87d2f7ce962c1766fba Mon Sep 17 00:00:00 2001 From: James Lu Date: Thu, 2 Apr 2020 09:59:13 -0700 Subject: [PATCH] SedRegex: allow matching text with the trailing suffix missing From: https://github.com/jlu5/SupyPlugins/commit/866875ec5d5742471d56cdcf4d2a2f9ff0e650ce --- plugins/SedRegex/__init__.py | 2 ++ plugins/SedRegex/constants.py | 26 ++++++++++++++++++++++++++ plugins/SedRegex/plugin.py | 5 ++--- plugins/SedRegex/test.py | 30 ++++++++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 3 deletions(-) create mode 100755 plugins/SedRegex/constants.py diff --git a/plugins/SedRegex/__init__.py b/plugins/SedRegex/__init__.py index c0c17b85b..775dd9ebc 100644 --- a/plugins/SedRegex/__init__.py +++ b/plugins/SedRegex/__init__.py @@ -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 diff --git a/plugins/SedRegex/constants.py b/plugins/SedRegex/constants.py new file mode 100755 index 000000000..3610e40fd --- /dev/null +++ b/plugins/SedRegex/constants.py @@ -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.+?)[:,] )?" + + # Match and save the delimiter (any one symbol) as a named group + r"s(?P[^\w\s])" + + # Match the pattern to replace, which can be any string up to the first instance of the delimiter + r"(?P(?:(?!(?P=delim)).)*)(?P=delim)" + + # Ditto with the replacement + r"(?P(?:(?!(?P=delim)).)*)" + + # Optional final delimiter plus flags at the end + r"(?:(?P=delim)(?P[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) diff --git a/plugins/SedRegex/plugin.py b/plugins/SedRegex/plugin.py index f0fd7228f..83ec209ed 100644 --- a/plugins/SedRegex/plugin.py +++ b/plugins/SedRegex/plugin.py @@ -1,6 +1,6 @@ ### # Copyright (c) 2015, Michael Daniel Telatynski -# Copyright (c) 2015-2019, James Lu +# Copyright (c) 2015-2020, James Lu # 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.+?)[:,] )?s(?P[^\w\s])(?P.*?)(?P=delim)" - r"(?P.*?)(?P=delim)(?P[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'}) diff --git a/plugins/SedRegex/test.py b/plugins/SedRegex/test.py index 37736256a..4b341eda9 100644 --- a/plugins/SedRegex/test.py +++ b/plugins/SedRegex/test.py @@ -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 ') + m = self.getMsg(' ') + self.assertIn('see you later, bye', str(m)) + # TODO: test ignores # vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: