Revision: 5747 Author: nicdumz Date: 2008-07-23 15:37:34 +0000 (Wed, 23 Jul 2008)
Log Message: ----------- * Replace all the named refs when a name is changed * Regex fix
Modified Paths: -------------- trunk/pywikipedia/reflinks.py
Modified: trunk/pywikipedia/reflinks.py =================================================================== --- trunk/pywikipedia/reflinks.py 2008-07-21 20:35:21 UTC (rev 5746) +++ trunk/pywikipedia/reflinks.py 2008-07-23 15:37:34 UTC (rev 5747) @@ -229,7 +229,7 @@ """ def __init__(self): # Match references - self.REFS = re.compile(u'(?i)<ref(?P<name>[^>]*)>(?P<content>.*?)</ref>') + self.REFS = re.compile(u'(?i)<ref(?P<name>[^>/]*)>(?P<content>.*?)</ref>') self.NAMES = re.compile(u'(?i).*name\s*=\s*(?P<quote>"?)\s*(?P<name>.*?)\s*(?P=quote).*') self.GROUPS = re.compile(u'(?i).*group\s*=\s*(?P<quote>"?)\s*(?P<group>.*?)\s*(?P=quote).*')
@@ -240,6 +240,8 @@ # values are [name, [list of full ref matches]] foundRefs = {} foundRefNames = [] + # Replace key by value + namedRepl = {}
for match in self.REFS.finditer(text): content = match.group('content') @@ -257,12 +259,14 @@ v[1].append(match.group()) else: v = [None, [match.group()]] - if not v[0]: - n = self.NAMES.match(name) - if n: - n = n.group('name') + n = self.NAMES.match(name) + if n: + n = n.group('name') + if v[0]: + namedRepl[n] = v[0] + else: v[0] = n - foundRefNames.append(n) + foundRefNames.append(n) groupdict[content] = v
id = 1 @@ -294,6 +298,10 @@ for ref in v[1][1:]: end = end.replace(ref, unnamed) text = header + end + + for (k,v) in namedRepl.iteritems(): + # TODO : Support ref groups + text = re.sub(u'<ref name\s*=\s*(?P<quote>"?)\s*%s\s*(?P=quote)\s*/>' % k, u'<ref name="%s" />' % v, text) return text
class ReferencesRobot: