jenkins-bot has submitted this change. ( 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1354178?usp=email )

Change subject: textlib: Recompute category position after removing templates
......................................................................

textlib: Recompute category position after removing templates

Find the last category after removing a stub or Personendaten template.
Removing a template before the categories shifts their positions, so the
previous offset could insert the template into the following interwiki
block and join it to a link.

Change-Id: I74da25c99eca0a97c5bc124332d09181c4da0cb4
---
M pywikibot/textlib.py
M tests/textlib_tests.py
2 files changed, 14 insertions(+), 3 deletions(-)

Approvals:
  jenkins-bot: Verified
  Xqt: Looks good to me, approved




diff --git a/pywikibot/textlib.py b/pywikibot/textlib.py
index f995a4f..05f1c45 100644
--- a/pywikibot/textlib.py
+++ b/pywikibot/textlib.py
@@ -2005,15 +2005,15 @@

     if under_categories:
         category = get_regexes('category', site)[0]
-        last = deque(category.finditer(newtext), maxlen=1)
-        if not last:
+        if not category.search(newtext):
             return newtext.strip()
-        last_category = last[0]
         for reg in under_categories:
             special = reg.search(newtext)
             if special and not isDisabled(newtext, special.start()):
                 newtext = (newtext[:special.start()].strip()
                            + newtext[special.end():])
+                last_category = deque(
+                    category.finditer(newtext), maxlen=1)[0]
                 newtext = (newtext[:last_category.end()].strip() + '\n' * 2
                            + special.group() + newtext[last_category.end():])

diff --git a/tests/textlib_tests.py b/tests/textlib_tests.py
index 8ce4ab5..a22816a 100755
--- a/tests/textlib_tests.py
+++ b/tests/textlib_tests.py
@@ -302,6 +302,17 @@
                 'foo\n[[Category:Foo]]\n{{foo-stub}}', [], site=self.site),
             'foo\n{{foo-stub}}')

+    def test_stub_before_categories(self) -> None:
+        """Test moving a stub preserves the following interwiki links."""
+        stub = '{{US-bio-stub}}'
+        categories = '[[Category:Alpha]]\n[[Category:Beta]]'
+        interwiki = '[[de:Artikel]]\n[[fr:Article]]'
+        old = f'foo\n\n{stub}\n\n{categories}\n\n{interwiki}'
+        self.assertEqual(
+            textlib.replaceCategoryLinks(
+                old, [], site=self.site, add_only=True),
+            f'foo\n\n{categories}\n\n{stub}\n\n{interwiki}')
+

 class TestTemplatesInCategory(TestCase):


--
To view, visit 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1354178?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.wikimedia.org/r/settings?usp=email

Gerrit-MessageType: merged
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I74da25c99eca0a97c5bc124332d09181c4da0cb4
Gerrit-Change-Number: 1354178
Gerrit-PatchSet: 4
Gerrit-Owner: Mahveotm <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to