jenkins-bot has submitted this change and it was merged.
Change subject: [FIX] Textlib: Expand category if necessary ......................................................................
[FIX] Textlib: Expand category if necessary
If the category name contains a template, it needs to be expanded. The regex now only fails if there are ]] within template parameters like '[[Category:{{P2|]]muhahahaha|legit}}]]'.
Change-Id: I1cbc1a3865f93c07909056e1f24af8bd333d5f34 --- M pywikibot/textlib.py M tests/textlib_tests.py 2 files changed, 44 insertions(+), 5 deletions(-)
Approvals: Mpaa: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/textlib.py b/pywikibot/textlib.py index d11154e..3af27e1 100644 --- a/pywikibot/textlib.py +++ b/pywikibot/textlib.py @@ -664,15 +664,21 @@ # and HTML comments text = removeDisabledParts(text) catNamespace = '|'.join(site.category_namespaces()) - R = re.compile(r'[[\s*(?P<namespace>%s)\s*:\s*(?P<catName>.+?)' - r'(?:|(?P<sortKey>.*?))?]]' + R = re.compile(r'[[\s*(?P<namespace>%s)\s*:\s*(?P<rest>.+?)]]' % catNamespace, re.I) for match in R.finditer(text): + if '{{' in match.group('rest'): + rest = site.expand_text(match.group('rest')) + else: + rest = match.group('rest') + if '|' in rest: + title, sortKey = rest.split('|', 1) + else: + title, sortKey = rest, None cat = pywikibot.Category(pywikibot.Link( - '%s:%s' % (match.group('namespace'), - match.group('catName')), + '%s:%s' % (match.group('namespace'), title), site), - sortKey=match.group('sortKey')) + sortKey=sortKey) result.append(cat) return result
diff --git a/tests/textlib_tests.py b/tests/textlib_tests.py index 42ed6d9..11513f1 100644 --- a/tests/textlib_tests.py +++ b/tests/textlib_tests.py @@ -162,6 +162,39 @@ self.assertEqual(old, new) config.line_separator = sep # restore the default separator
+ +class TestTemplatesInCategory(PywikibotTestCase): + + """Tests to verify that templates in category links are handled.""" + + @classmethod + def setUpClass(cls): + cls.site = pywikibot.Site('en', 'wikipedia') + + def test_templates(self): + self.assertEqual(textlib.getCategoryLinks( + '[[Category:{{P1|Foo}}]]', self.site), + [pywikibot.page.Category(self.site, 'Foo')]) + self.assertEqual(textlib.getCategoryLinks( + '[[Category:{{P1|Foo}}|bar]]', self.site), + [pywikibot.page.Category(self.site, 'Foo', sortKey='bar')]) + self.assertEqual(textlib.getCategoryLinks( + '[[Category:{{P1|{{P2|L33t|Foo}}}}|bar]]', self.site), + [pywikibot.page.Category(self.site, 'Foo', sortKey='bar')]) + self.assertEqual(textlib.getCategoryLinks( + '[[Category:Foo{{!}}bar]]', self.site), + [pywikibot.page.Category(self.site, 'Foo', sortKey='bar')]) + self.assertEqual(textlib.getCategoryLinks( + '[[Category:Foo{{!}}bar]][[Category:Wiki{{P2||pedia}}]]', + self.site), + [pywikibot.page.Category(self.site, 'Foo', sortKey='bar'), + pywikibot.page.Category(self.site, 'Wikipedia')]) + self.assertEqual(textlib.getCategoryLinks( + '[[Category:Foo{{!}}and{{!}}bar]]', self.site), + [pywikibot.page.Category(self.site, 'Foo', sortKey='and|bar')]) + self.assertRaises(pywikibot.InvalidTitle, textlib.getCategoryLinks, + '[[Category:nasty{{{!}}]]', self.site) + if __name__ == '__main__': try: unittest.main()