Ricordisamoa has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/148553

Change subject: fix textlib.getCategoryLinks() for adjacent categories
......................................................................

fix textlib.getCategoryLinks() for adjacent categories

the regex has been fixed (take #3):
- characters in the sortKey are now matched in non-greedy mode
- spaces at the end of the sortKey are not ignored anymore

test_adjoining_links() has been added to TestCategoryRearrangement
to make sure that the regular expression always works well.

Change-Id: Ia7dbe5007c34af8a457e8952ef727928e02f8d4d
---
M pywikibot/textlib.py
M tests/textlib_tests.py
2 files changed, 15 insertions(+), 5 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core 
refs/changes/53/148553/1

diff --git a/pywikibot/textlib.py b/pywikibot/textlib.py
index f9a06b0..585ba77 100644
--- a/pywikibot/textlib.py
+++ b/pywikibot/textlib.py
@@ -663,7 +663,7 @@
     text = removeDisabledParts(text)
     catNamespace = '|'.join(site.category_namespaces())
     R = re.compile(r'\[\[\s*(?P<namespace>%s)\s*:\s*(?P<catName>.+?)'
-                   r'(?:\|(?P<sortKey>.*))?\s*\]\]'
+                   r'(?:\|(?P<sortKey>.*?))?\]\]'
                    % catNamespace, re.I)
     for match in R.finditer(text):
         cat = pywikibot.Category(pywikibot.Link(
diff --git a/tests/textlib_tests.py b/tests/textlib_tests.py
index 72958c7..edcead9 100644
--- a/tests/textlib_tests.py
+++ b/tests/textlib_tests.py
@@ -134,7 +134,8 @@
 
     """
     Tests to ensure that sorting keys are not being lost when
-    using .getCategoryLinks() and .replaceCategoryLinks().
+    using .getCategoryLinks() and .replaceCategoryLinks(),
+    with both a newline and an empty string as separators.
     """
 
     @classmethod
@@ -143,12 +144,21 @@
         cls.old = ('[[Category:Cat1]]%(LS)s[[Category:Cat2|]]%(LS)s'
                    '[[Category:Cat1| ]]%(LS)s[[Category:Cat2|key]]'
                    % {'LS': config.LS})
+        cls.cats = textlib.getCategoryLinks(cls.old, site=cls.site)
 
-    def test_replace_category_links(self):
-        cats = textlib.getCategoryLinks(self.old, site=self.site)
-        new = textlib.replaceCategoryLinks(self.old, cats, site=self.site)
+    def test_standard_links(self):
+        new = textlib.replaceCategoryLinks(self.old, self.cats, site=self.site)
         self.assertEqual(self.old, new)
 
+    def test_adjoining_links(self):
+        old = self.old.replace(config.LS, '')
+        cats = textlib.getCategoryLinks(old, site=self.site)
+        self.assertEqual(self.cats, cats)
+        sep = config.LS
+        config.line_separator = ''  # use an empty separator temporarily
+        new = textlib.replaceCategoryLinks(old, cats, site=self.site)
+        self.assertEqual(old, new)
+        config.line_separator = sep  # restore the default separator
 
 if __name__ == '__main__':
     try:

-- 
To view, visit https://gerrit.wikimedia.org/r/148553
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ia7dbe5007c34af8a457e8952ef727928e02f8d4d
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Ricordisamoa <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to