If you don't mind a suggestion, Nic, I think it is better to use the Wiki server to resolve magic words than to do it on the client side; otherwise, we continue to face the problem of our client struggling to keep up with changes in namespaces, etc. Why not use the API "action=expandtemplates" feature here? Or, better yet, stick with the rewrite branch. :-)
Russ ----- Original Message ----- From: <[email protected]> To: <[email protected]> Sent: Sunday, June 21, 2009 9:58 AM Subject: [Pywikipedia-svn] SVN: [6978] trunk/pywikipedia/wikipedia.py > 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): > > > > _______________________________________________ > Pywikipedia-svn mailing list > [email protected] > https://lists.wikimedia.org/mailman/listinfo/pywikipedia-svn > _______________________________________________ Pywikipedia-l mailing list [email protected] https://lists.wikimedia.org/mailman/listinfo/pywikipedia-l
