jenkins-bot has submitted this change and it was merged.
Change subject: Allow relative links to be handled in Link constructor
......................................................................
Allow relative links to be handled in Link constructor
This patch allows a BasePage object to be passed in lieu of a Site
to the Link constructor. In this case, the link text will be handled
relative to the passed Page. This will allow for subpage links to be
parsed when relative links are used on the page. A new test, test_relative,
has been added to tests/link_tests.py to test this change.
Bug: T57113
Change-Id: Idd6366e82216c130b5143c9356245ff840af41ed
---
M pywikibot/page.py
M tests/link_tests.py
2 files changed, 29 insertions(+), 5 deletions(-)
Approvals:
John Vandenberg: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/page.py b/pywikibot/page.py
index 90efda1..16a69d0 100644
--- a/pywikibot/page.py
+++ b/pywikibot/page.py
@@ -4461,17 +4461,23 @@
@type text: unicode
@param source: the Site on which the link was found (not necessarily
the site to which the link refers)
- @type source: Site
+ @type source: Site or BasePage
@param defaultNamespace: a namespace to use if the link does not
contain one (defaults to 0)
@type defaultNamespace: int
"""
- assert source is None or isinstance(source, pywikibot.site.BaseSite), \
- "source parameter should be a Site object"
+ source_is_page = isinstance(source, BasePage)
+
+ assert source is None or source_is_page or isinstance(source,
pywikibot.site.BaseSite), \
+ "source parameter should be either a Site or Page object"
+
+ if source_is_page:
+ self._source = source.site
+ else:
+ self._source = source or pywikibot.Site()
self._text = text
- self._source = source or pywikibot.Site()
self._defaultns = defaultNamespace
# preprocess text (these changes aren't site-dependent)
@@ -4514,6 +4520,9 @@
t = t.replace(u"\u200e", u"").replace(u"\u200f", u"")
self._text = t
+ if source_is_page:
+ self._text = source.title(withSection=False) + self._text
+
def __repr__(self):
"""Return a more complete string representation."""
return "pywikibot.page.Link(%r, %r)" % (self.title, self.site)
diff --git a/tests/link_tests.py b/tests/link_tests.py
index 83020ff..29d216f 100644
--- a/tests/link_tests.py
+++ b/tests/link_tests.py
@@ -11,7 +11,7 @@
import pywikibot
from pywikibot import config2 as config
-from pywikibot.page import Link
+from pywikibot.page import Link, Page
from pywikibot.exceptions import Error, InvalidTitle
from tests.aspects import (
unittest,
@@ -108,6 +108,21 @@
self.assertRaises(InvalidTitle, Link('Category: ',
self.get_site()).parse)
self.assertRaises(InvalidTitle, Link('Category: #bar',
self.get_site()).parse)
+ def test_relative(self):
+ """Test that relative links are handled properly."""
+ # Subpage
+ p = Page(self.get_site(), 'Foo')
+ l = Link('/bar', p)
+ self.assertEquals(l.title, 'Foo/bar')
+ self.assertEquals(l.site, self.get_site())
+ # Subpage of Page with section
+ p = Page(self.get_site(), 'Foo#Baz')
+ l = Link('/bar', p)
+ self.assertEquals(l.title, 'Foo/bar')
+ self.assertEquals(l.site, self.get_site())
+ # Non-subpage link text beginning with slash
+ l = Link('/bar', self.get_site())
+ self.assertEquals(l.title, '/bar')
# ---- The first set of tests are explicit links, starting with a ':'.
--
To view, visit https://gerrit.wikimedia.org/r/213816
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: Idd6366e82216c130b5143c9356245ff840af41ed
Gerrit-PatchSet: 7
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Happy5214 <[email protected]>
Gerrit-Reviewer: Happy5214 <[email protected]>
Gerrit-Reviewer: John Vandenberg <[email protected]>
Gerrit-Reviewer: Ladsgroup <[email protected]>
Gerrit-Reviewer: Merlijn van Deen <[email protected]>
Gerrit-Reviewer: XZise <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits