Revision: 6978 Author: nicdumz Date: 2009-06-21 13:58:28 +0000 (Sun, 21 Jun 2009)
Log Message: ----------- Adding a Site.resolvemagicwords member. For now, only resolves {{ns:xx}} words, but is meant to be expanded.
This solves issue #2805572
Modified Paths: -------------- trunk/pywikipedia/wikipedia.py
Modified: trunk/pywikipedia/wikipedia.py =================================================================== --- trunk/pywikipedia/wikipedia.py 2009-06-21 10:46:10 UTC (rev 6977) +++ trunk/pywikipedia/wikipedia.py 2009-06-21 13:58:28 UTC (rev 6978) @@ -1811,6 +1811,9 @@ # from text before processing thistxt = removeDisabledParts(thistxt)
+ # resolve {{ns:-1}} or {{ns:Help}} + thistxt = self.site().resolvemagicwords(thistxt) + for match in Rlink.finditer(thistxt): title = match.group('title') title = title.replace("_", " ").strip(" ") @@ -5898,6 +5901,32 @@ + '\s*:?\s*[[(.+?)(?:|.*?)?]]', re.IGNORECASE | re.UNICODE | re.DOTALL)
+ def resolvemagicwords(self, wikitext): + """Replace the {{ns:xx}} marks in a wikitext with the namespace names""" + + defaults = [] + for namespace in self.family.namespaces.itervalues(): + value = namespace.get('_default', None) + if value: + if isinstance(value, list): + defaults += value + else: + defaults.append(value) + + named = re.compile(u'{{ns:(' + '|'.join(defaults) + ')}}', re.I) + + def replacenamed(match): + return self.normalizeNamespace(match.group(1)) + + wikitext = named.sub(replacenamed, wikitext) + + numbered = re.compile('{{ns:(-?\d{1,2})}}', re.I) + + def replacenumbered(match): + return self.namespace(int(match.group(1))) + + return named.sub(replacenumbered, wikitext) + # The following methods are for convenience, so that you can access # methods of the Family class easier. def encoding(self):