Revision: 6817
Author: nicdumz
Date: 2009-05-04 01:41:41 +0000 (Mon, 04 May 2009)
Log Message:
-----------
Interwikis in template namespace:
applying patch #2780955, first submitted by Tieump
Pushing those changes in the rewrite branch too.
Modified Paths:
--------------
branches/rewrite/pywikibot/textlib.py
branches/rewrite/scripts/interwiki.py
trunk/pywikipedia/interwiki.py
trunk/pywikipedia/wikipedia.py
Modified: branches/rewrite/pywikibot/textlib.py
===================================================================
--- branches/rewrite/pywikibot/textlib.py 2009-05-04 01:26:35 UTC (rev 6816)
+++ branches/rewrite/pywikibot/textlib.py 2009-05-04 01:41:41 UTC (rev 6817)
@@ -347,7 +347,8 @@
return removeLanguageLinks(text, site, marker)
-def replaceLanguageLinks(oldtext, new, site = None, addOnly = False):
+def replaceLanguageLinks(oldtext, new, site = None, addOnly = False,
+ template = False):
"""Replace interlanguage links in the text with a new set of links.
'new' should be a dict with the Site objects as keys, and Page or Link
@@ -394,7 +395,19 @@
newtext = replaceCategoryLinks(s2, cats, site=site,
addOnly=True)
else:
- newtext = s2.replace(marker,'').strip() + separator + s
+ if template:
+ # Do we have a noinclude at the end of the template?
+ parts = '</noinclude>'.split(s2)
+ lastpart = parts[-1]
+ if re.match('\s*%s' % marker, lastpart):
+ # Put the langlinks back into the noinclude's
+ regexp = re.compile('</noinclude>\s*%s' % marker)
+ newtext = regexp.sub(s + '</noinclude>', s2)
+ else:
+ # Put the langlinks at the end, inside noinclude's
+ newtext = s2.replace(marker,'').strip() + separator + u'<noinclude>\n%s</noinclude>\n' % s
+ else:
+ newtext = s2.replace(marker,'').strip() + separator + s
else:
newtext = s2.replace(marker,'')
return newtext
Modified: branches/rewrite/scripts/interwiki.py
===================================================================
--- branches/rewrite/scripts/interwiki.py 2009-05-04 01:26:35 UTC (rev 6816)
+++ branches/rewrite/scripts/interwiki.py 2009-05-04 01:41:41 UTC (rev 6817)
@@ -1527,7 +1527,9 @@
pywikibot.output(u"Changes to be made: %s" % mods)
oldtext = page.get()
- newtext = pywikibot.replaceLanguageLinks(oldtext, new, site = page.site())
+ newtext = pywikibot.replaceLanguageLinks(oldtext, new,
+ site = page.site(),
+ template = (page.namespace() == 10))
if newtext == oldtext:
return False
if globalvar.debug:
Modified: trunk/pywikipedia/interwiki.py
===================================================================
--- trunk/pywikipedia/interwiki.py 2009-05-04 01:26:35 UTC (rev 6816)
+++ trunk/pywikipedia/interwiki.py 2009-05-04 01:41:41 UTC (rev 6817)
@@ -1524,7 +1524,7 @@
wikipedia.output(u"Changes to be made: %s" % mods)
oldtext = page.get()
- newtext = wikipedia.replaceLanguageLinks(oldtext, new, site = page.site())
+ newtext = wikipedia.replaceLanguageLinks(oldtext, new, site = page.site(), template = (page.namespace() == 10) )
if newtext == oldtext:
return False
if globalvar.debug:
Modified: trunk/pywikipedia/wikipedia.py
===================================================================
--- trunk/pywikipedia/wikipedia.py 2009-05-04 01:26:35 UTC (rev 6816)
+++ trunk/pywikipedia/wikipedia.py 2009-05-04 01:41:41 UTC (rev 6817)
@@ -3658,7 +3658,7 @@
else:
return removeLanguageLinks(text, site, marker)
-def replaceLanguageLinks(oldtext, new, site = None, addOnly = False):
+def replaceLanguageLinks(oldtext, new, site = None, addOnly = False, template = False):
"""Replace interlanguage links in the text with a new set of links.
'new' should be a dict with the Site objects as keys, and Page objects
@@ -3698,7 +3698,19 @@
s2 = removeCategoryLinksAndSeparator(s2.replace(marker,'',cseparatorstripped).strip(), site) + separator + s
newtext = replaceCategoryLinks(s2, cats, site=site, addOnly=True)
else:
- newtext = s2.replace(marker,'').strip() + separator + s
+ if template:
+ # Do we have a noinclude at the end of the template?
+ parts = '</noinclude>'.split(s2)
+ lastpart = parts[-1]
+ if re.match('\s*%s' % marker, lastpart):
+ # Put the langlinks back into the noinclude's
+ regexp = re.compile('</noinclude>\s*%s' % marker)
+ newtext = regexp.sub(s + '</noinclude>', s2)
+ else:
+ # Put the langlinks at the end, inside noinclude's
+ newtext = s2.replace(marker,'').strip() + separator + u'<noinclude>\n%s</noinclude>\n' % s
+ else:
+ newtext = s2.replace(marker,'').strip() + separator + s
else:
newtext = s2.replace(marker,'')
return newtext