jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/434033 )
Change subject: [bugfix] Fix empty section regex ......................................................................
[bugfix] Fix empty section regex
Bug: T195075 Change-Id: I5f2d3461c38e4b5cce69008cca11f126558b8f99 --- M pywikibot/cosmetic_changes.py M tests/cosmetic_changes_tests.py 2 files changed, 21 insertions(+), 9 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/cosmetic_changes.py b/pywikibot/cosmetic_changes.py index 9c3a541..37c790a 100755 --- a/pywikibot/cosmetic_changes.py +++ b/pywikibot/cosmetic_changes.py @@ -642,7 +642,7 @@ return text
def removeEmptySections(self, text): - """Cleanup multiple empty sections.""" + """Cleanup empty sections.""" exceptions = ['comment', 'pre', 'source', 'nowiki', 'code', 'startspace']
@@ -654,15 +654,24 @@ if self.site.code in skip_templates: for template in skip_templates[self.site.code]: skip_regexes.append( - re.compile(r'{{\s*' + template + r'\s*}}', re.I)) - skip_regexes.append(re.compile(r'\s*')) + re.compile(r'{{\s*%s\s*}}' % template, re.I)) + stripped_text = str(text) + for reg in skip_regexes: + stripped_text = reg.sub(r'', stripped_text)
- pattern = re.compile(r'\n(=+) *[^=]+? *\1(?:' - + '|'.join(x.pattern for x in skip_regexes) - + r')+(?=\1 *[^=]+? *\1)', re.I) - text = textlib.replaceExcept(text, pattern, r'\n', - exceptions=exceptions, - caseInsensitive=True) + stripped_pattern = re.compile( + r'\n((=+) *[^\n=]+? *\2) *\n\s*(?=(\2 *[^\n=]+? *\2))') + pos = 0 + while True: + match = stripped_pattern.search(stripped_text[pos:]) + if not match: + break + pattern = re.compile(r'\n{}.+?(?={})'.format( + match.group(1), match.group(3)), re.DOTALL) + text = textlib.replaceExcept(text, pattern, r'\n', + exceptions=exceptions) + pos = match.end() + return text
def removeUselessSpaces(self, text): diff --git a/tests/cosmetic_changes_tests.py b/tests/cosmetic_changes_tests.py index 9695aae..77418d0 100644 --- a/tests/cosmetic_changes_tests.py +++ b/tests/cosmetic_changes_tests.py @@ -73,6 +73,9 @@ self.assertEqual( '\n==Bar==', self.cct.removeEmptySections('\n==Foo==\n<!-- Baz -->\n==Bar==')) + # comments and content between + testcase = '\n== Foo ==\n<!-- Baz -->\nBaz\n<!-- Foo -->\n== Bar ==' + self.assertEqual(testcase, self.cct.removeEmptySections(testcase)) # inside comment self.assertEqual( '<!--\n==Foo==\n\n==Bar==\n-->',
pywikibot-commits@lists.wikimedia.org