jenkins-bot has submitted this change and it was merged.
Change subject: Page.title() may return labeled links like [[link|label]] ......................................................................
Page.title() may return labeled links like [[link|label]]
withNamespaces parameter is also availlable with asLink=True. This means the link is labeled with it's _link.title: [[link|label]] [[link#section|label]] [[site:link|label]] except left and right side is the same e.g. for article namespace
Change-Id: Ic5c19d7c3f504c634ad63ff564477e4e54e532bd --- M pywikibot/page.py M tests/page_tests.py 2 files changed, 50 insertions(+), 23 deletions(-)
Approvals: Pyfisch: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/page.py b/pywikibot/page.py index b18b875..1b9ec62 100644 --- a/pywikibot/page.py +++ b/pywikibot/page.py @@ -131,10 +131,14 @@ as_filename=False, insite=None): """Return the title of this Page, as a Unicode string.
- @param underscore: if true, replace all ' ' characters with '_' - @param withNamespace: if false, omit the namespace prefix + @param underscore: (not used with asLink) if true, replace all ' ' + characters with '_' + @param withNamespace: if false, omit the namespace prefix. If this + option is false and used together with asLink return a labeled + link like [[link|label]] @param withSection: if false, omit the section - @param asUrl: if true, quote title as if in an URL + @param asUrl: (not used with asLink) if true, quote title as if in an + URL @param asLink: if true, return the title in the form of a wikilink @param allowInterwiki: (only used if asLink is true) if true, format the link as an interwiki link if necessary @@ -142,16 +146,19 @@ format the link as an interwiki link @param textlink: (only used if asLink is true) if true, place a ':' before Category: and Image: links - @param as_filename: if true, replace any characters that are unsafe - in filenames + @param as_filename: (not used with asLink) if true, replace any + characters that are unsafe in filenames @param insite: (only used if asLink is true) a site object where the title is to be shown. default is the current family/lang given by -family and -lang option i.e. config.family and config.mylang
""" title = self._link.canonical_title() + label = self._link.title if withSection and self._link.section: - title = title + "#" + self._link.section + section = u"#" + self._link.section + else: + section = u'' if asLink: if insite: target_code = insite.code @@ -165,22 +172,25 @@ or self.site.code != target_code)): if self.site.family.name != target_family \ and self.site.family.name != self.site.code: - return u'[[%s:%s:%s]]' % (self.site.family.name, - self.site.code, - title) + title = u'%s:%s:%s' % (self.site.family.name, + self.site.code, + title) else: # use this form for sites like commons, where the # code is the same as the family name - return u'[[%s:%s]]' % (self.site.code, - title) + title = u'%s:%s' % (self.site.code, title) elif textlink and (self.isImage() or self.isCategory()): - return u'[[:%s]]' % title + title = u':%s' % title + elif self.namespace() == 0 and not section: + withNamespace = True + if withNamespace: + return u'[[%s%s]]' % (title, section) else: - return u'[[%s]]' % title + return u'[[%s%s|%s]]' % (title, section, label) if not withNamespace and self.namespace() != 0: - title = self._link.title - if withSection and self._link.section: - title = title + "#" + self._link.section + title = label + section + else: + title += section if underscore or asUrl: title = title.replace(u' ', u'_') if asUrl: diff --git a/tests/page_tests.py b/tests/page_tests.py index c242482..2ae3b26 100644 --- a/tests/page_tests.py +++ b/tests/page_tests.py @@ -141,12 +141,21 @@ u"Test page") self.assertEqual(p1.title(asUrl=True), "Help%3ATest_page%23Testing") - self.assertEqual(p1.title(asLink=True), + self.assertEqual(p1.title(asLink=True, insite=site), u"[[Help:Test page#Testing]]") - self.assertEqual(p1.title(asLink=True, forceInterwiki=True), + self.assertEqual(p1.title(asLink=True, forceInterwiki=True, insite=site), u"[[en:Help:Test page#Testing]]") - self.assertEqual(p1.title(asLink=True, textlink=True), - p1.title(asLink=True)) + self.assertEqual(p1.title(asLink=True, textlink=True, insite=site), + p1.title(asLink=True, textlink=False, insite=site)) + self.assertEqual(p1.title(asLink=True, withNamespace=False, insite=site), + u"[[Help:Test page#Testing|Test page]]") + self.assertEqual(p1.title(asLink=True, forceInterwiki=True, + withNamespace=False, insite=site), + u"[[en:Help:Test page#Testing|Test page]]") + self.assertEqual(p1.title(asLink=True, textlink=True, + withNamespace=False, insite=site), + p1.title(asLink=True, textlink=False, + withNamespace=False, insite=site)) # also test a page with non-ASCII chars and a different namespace p2 = pywikibot.Page(site, u"File:Jean-Léon Gérôme 003.jpg") self.assertEqual(p2.title(), @@ -161,14 +170,22 @@ u"Jean-Léon Gérôme 003.jpg") self.assertEqual(p2.title(asUrl=True), u"File%3AJean-L%C3%A9on_G%C3%A9r%C3%B4me_003.jpg") - self.assertEqual(p2.title(asLink=True), + self.assertEqual(p2.title(asLink=True, insite=site), u"[[File:Jean-Léon Gérôme 003.jpg]]") - self.assertEqual(p2.title(asLink=True, forceInterwiki=True), + self.assertEqual(p2.title(asLink=True, forceInterwiki=True, insite=site), u"[[en:File:Jean-Léon Gérôme 003.jpg]]") - self.assertEqual(p2.title(asLink=True, textlink=True), + self.assertEqual(p2.title(asLink=True, textlink=True, insite=site), u"[[:File:Jean-Léon Gérôme 003.jpg]]") self.assertEqual(p2.title(as_filename=True), u"File_Jean-Léon_Gérôme_003.jpg") + self.assertEqual(p2.title(asLink=True, withNamespace=False, insite=site), + u"[[File:Jean-Léon Gérôme 003.jpg|Jean-Léon Gérôme 003.jpg]]") + self.assertEqual(p2.title(asLink=True, forceInterwiki=True, + withNamespace=False, insite=site), + u"[[en:File:Jean-Léon Gérôme 003.jpg|Jean-Léon Gérôme 003.jpg]]") + self.assertEqual(p2.title(asLink=True, textlink=True, + withNamespace=False, insite=site), + u"[[:File:Jean-Léon Gérôme 003.jpg|Jean-Léon Gérôme 003.jpg]]")
def testSection(self): """Test section() method."""
pywikibot-commits@lists.wikimedia.org