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

Change subject: [bugfix] Don't add a second blank line before bottom templates
......................................................................

[bugfix] Don't add a second blank line before bottom templates

noreferences.py left one blank line between the new references section
and a template at the bottom of the page if a single line break
preceded the template, but two blank lines if a blank line preceded it.
Collapse the leading line breaks of the remaining text so that always
one blank line is left.

Bug: T365340
Change-Id: I37158f80dc391a717e1b0846b42ea55bd7ed8602
---
M scripts/noreferences.py
M tests/noreferences_tests.py
2 files changed, 40 insertions(+), 1 deletion(-)

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




diff --git a/scripts/noreferences.py b/scripts/noreferences.py
index ef5d3dc..c54493c 100755
--- a/scripts/noreferences.py
+++ b/scripts/noreferences.py
@@ -786,7 +786,10 @@
         else:
             raise TranslationError(f'{Path(__file__).name} script is not '
                                    f'localized for {self.site}')
-        return oldText[:index].rstrip() + ref_section + oldText[index:]
+        # ref_section ends with a line break; do not add another blank
+        # line if the remaining text starts with several line breaks
+        text_after = re.sub(r'\A(?:\r?\n){2,}', '\n', oldText[index:])
+        return oldText[:index].rstrip() + ref_section + text_after

     def skip_page(self, page) -> bool:
         """Check whether the page could be processed."""
diff --git a/tests/noreferences_tests.py b/tests/noreferences_tests.py
index dfc6024..6cf07ea 100755
--- a/tests/noreferences_tests.py
+++ b/tests/noreferences_tests.py
@@ -54,5 +54,41 @@
         self.assertEqual(new_text, expected)


+class TestReferencesSpacing(TestCase):
+
+    """Test the blank lines around a newly created references section."""
+
+    family = 'wikipedia'
+    code = 'cs'
+    dry = True
+
+    def add_references(self, text: str) -> str:
+        """Return text with a references section added."""
+        bot = NoReferencesBot(site=self.site)
+        return bot.addReferences(text)
+
+    def test_bottom_template(self) -> None:
+        """Test one blank line before a bottom template."""
+        expected = 'Text.\n\n== Reference ==\n<references />\n\n{{tmpl}}\n'
+        for newlines in ('\n', '\n\n', '\n\n\n'):
+            with self.subTest(newlines=newlines):
+                text = f'Text.{newlines}{{{{tmpl}}}}\n'
+                self.assertEqual(self.add_references(text), expected)
+
+    def test_category(self) -> None:
+        """Test one blank line before a category."""
+        expected = ('Text.\n\n== Reference ==\n<references />\n\n'
+                    '[[Category:Foo]]\n')
+        for newlines in ('\n', '\n\n'):
+            with self.subTest(newlines=newlines):
+                text = f'Text.{newlines}[[Category:Foo]]\n'
+                self.assertEqual(self.add_references(text), expected)
+
+    def test_no_following_text(self) -> None:
+        """Test that nothing is added after the references section."""
+        self.assertEqual(self.add_references('Text.'),
+                         'Text.\n\n== Reference ==\n<references />\n')
+
+
 if __name__ == '__main__':
     unittest.main()

--
To view, visit 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1358209?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: I37158f80dc391a717e1b0846b42ea55bd7ed8602
Gerrit-Change-Number: 1358209
Gerrit-PatchSet: 2
Gerrit-Owner: AkashKumar7902 <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot
Gerrit-CC: Mahveotm <[email protected]>
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to