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

Change subject: [bugfix] Find headings followed by a multiline comment
......................................................................

[bugfix] Find headings followed by a multiline comment

The header regex let a heading end at "=" signs inside a comment
after it. If that comment spans lines, as in
"== Odkazy ==<!-- === Reference ===\n-->", the match ended inside the
comment and the heading was dropped, so extract_sections() did not
find the section. The earlier fix for this task named this case as
still not working.

Match a comment inside a heading only as a whole. The heading then
ends before the comment, and the trailing comment part of the regex
takes the comment, as MediaWiki does.

Bug: T209712
Change-Id: I7948f9c442fe6ec657f89f2cad738f76d938a9d8
---
M pywikibot/textlib.py
M tests/textlib_tests.py
2 files changed, 22 insertions(+), 1 deletion(-)

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




diff --git a/pywikibot/textlib.py b/pywikibot/textlib.py
index 5107520..4782bc8 100644
--- a/pywikibot/textlib.py
+++ b/pywikibot/textlib.py
@@ -282,7 +282,8 @@
         # section headers
         'header': re.compile(
             r'(?:(?<=\n)|\A)(?s:<!--.*?-->)*'
-            r'(=(?:[^\n]|(?s:<!--.*?-->))+=)'
+            # a comment is only matched as a whole (T209712)
+            r'(=(?:[^\n<]|<(?!!--)|(?s:<!--.*?-->))+=)'
             r' *(?s:<!--.*?--> *)*(?=\n|\Z)'),
         # external links
         'hyperlink': compileLinkR(),
diff --git a/tests/textlib_tests.py b/tests/textlib_tests.py
index 87b7200..7253bcf 100755
--- a/tests/textlib_tests.py
+++ b/tests/textlib_tests.py
@@ -1262,6 +1262,11 @@
                                                ['header'], site=self.site),
                          '\n==x<!--\n'
                          'comment-->==\n')
+        self.assertEqual(textlib.replaceExcept('\n==x==<!--==\n'
+                                               'x-->\n', 'x', 'y',
+                                               ['header'], site=self.site),
+                         '\n==x==<!--==\n'
+                         'x-->\n')
         self.assertEqual(textlib.replaceExcept('\n{|\n x \n|}\n', 'x', 'y',
                                                ['table'], site=self.site),
                          '\n{|\n x \n|}\n')
@@ -1759,6 +1764,21 @@
              ('== title 2 ==', ' <!-- trailing comment -->\ncontent 2')]
         )

+    def test_with_multiline_trailing_comment(self) -> None:
+        """Test section headers followed by a multiline comment."""
+        text = ('text\n\n'
+                '== title ==<!-- == not title ==\n-->\n'
+                'content\n\n'
+                '== title 2 == <!-- === not title ===\n-->\n'
+                'content 2')
+        result = extract_sections(text, self.site)
+        self._extract_sections_tests(
+            result,
+            'text\n\n',
+            [('== title ==', '<!-- == not title ==\n-->\ncontent\n\n'),
+             ('== title 2 ==', ' <!-- === not title ===\n-->\ncontent 2')]
+        )
+
     def test_long_comment(self) -> None:
         r"""Test for text having a long expanse of white space.


--
To view, visit 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1358447?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: I7948f9c442fe6ec657f89f2cad738f76d938a9d8
Gerrit-Change-Number: 1358447
Gerrit-PatchSet: 2
Gerrit-Owner: AkashKumar7902 <[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