jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/699333 )
Change subject: [IMPR] add add_text function to textlib ......................................................................
[IMPR] add add_text function to textlib
textlib.add_text adds text to a page content above categories and interwiki links.
Add add_text function from add_text script to textlib but without reading/writing the page. The trivial cases of adding a text on top or on bottom of the page aren't supported.
Bug: T284388 Change-Id: Ia1b378c37272d0f8ba057f21633b306340a45360 --- M pywikibot/textlib.py M tests/textlib_tests.py 2 files changed, 42 insertions(+), 0 deletions(-)
Approvals: Damian: Looks good to me, but someone else must approve JJMC89: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/textlib.py b/pywikibot/textlib.py index 534cb78..9deac38 100644 --- a/pywikibot/textlib.py +++ b/pywikibot/textlib.py @@ -832,6 +832,36 @@ return text
+def add_text(text: str, add: str, *, site=None) -> str: + """Add text to a page content above categories and interwiki. + + :param text: The page content to add text to. + :param add: Text to add. + :param site: The site that the text is coming from. Required for + reorder of categories and interlanguage links. Te default site + is used otherwise. + :type site: pywikibot.Site + """ + # Translating the \n (e.g. from command line) into binary \n + add = add.replace('\n', '\n') + + # Getting the categories + categories_inside = getCategoryLinks(text, site) + # Deleting the categories + text = removeCategoryLinks(text, site) + # Getting the interwiki + interwiki_inside = getLanguageLinks(text, site) + # Removing the interwiki + text = removeLanguageLinks(text, site) + + # Adding the text + text += '\n' + add + # Reputting the categories + text = replaceCategoryLinks(text, categories_inside, site, addOnly=True) + # Adding the interwiki + return replaceLanguageLinks(text, interwiki_inside, site) + + # ------------------------------- # Functions dealing with sections # ------------------------------- diff --git a/tests/textlib_tests.py b/tests/textlib_tests.py index 02bcbaa..80f7274 100644 --- a/tests/textlib_tests.py +++ b/tests/textlib_tests.py @@ -173,6 +173,18 @@ textlib.categoryFormat(data, self.site))
+class TestAddText(DefaultDrySiteTestCase): + + """Test add_text function.""" + + def test_add_text(self): + """Test adding text.""" + self.assertEqual( + textlib.add_text('foo\n[[Category:Foo]]', 'bar', site=self.site), + 'foo\nbar\n\n[[Category:Foo]]' + ) + + class TestCategoryRearrangement(DefaultDrySiteTestCase):
"""