http://www.mediawiki.org/wiki/Special:Code/pywikipedia/11320
Revision: 11320 Author: xqt Date: 2013-04-01 15:05:38 +0000 (Mon, 01 Apr 2013) Log Message: ----------- fix templatesWithParams parser bug #3609685,update from rewrite r11319
Modified Paths: -------------- trunk/pywikipedia/pywikibot/textlib.py
Modified: trunk/pywikipedia/pywikibot/textlib.py =================================================================== --- trunk/pywikipedia/pywikibot/textlib.py 2013-04-01 15:03:32 UTC (rev 11319) +++ trunk/pywikipedia/pywikibot/textlib.py 2013-04-01 15:05:38 UTC (rev 11320) @@ -855,18 +855,23 @@
# marker for math marker3 = u'%%' - while marker2 in thistxt: + while marker3 in thistxt: marker3 += u'%'
+ # marker for value parameter + marker4 = u'§§' + while marker4 in thistxt: + marker4 += u'§' + result = [] - inside = {} - count = 0 Rtemplate = re.compile( ur'{{(msg:)?(?P<name>[^{|]+?)(|(?P<params>[^{]+?))?}}') Rmath = re.compile(ur'<math>[^<]+</math>') + Rvalue = re.compile(r'{{{.+?}}}') Rmarker = re.compile(ur'%s(\d+)%s' % (marker, marker)) Rmarker2 = re.compile(ur'%s(\d+)%s' % (marker2, marker2)) Rmarker3 = re.compile(ur'%s(\d+)%s' % (marker3, marker3)) + Rmarker4 = re.compile(ur'%s(\d+)%s' % (marker4, marker4))
# Replace math with markers maths = {} @@ -877,6 +882,16 @@ thistxt = thistxt.replace(text, '%s%d%s' % (marker3, count, marker3)) maths[count] = text
+ values = {} + count = 0 + for m in Rvalue.finditer(thistxt): + count += 1 + text = m.group() + thistxt = thistxt.replace(text, '%s%d%s' % (marker4, count, marker4)) + values[count] = text + + inside = {} + count = 0 while Rtemplate.search(thistxt) is not None: for m in Rtemplate.finditer(thistxt): # Make sure it is not detected again @@ -889,6 +904,8 @@ text = text.replace(m2.group(), inside[int(m2.group(1))]) for m2 in Rmarker3.finditer(text): text = text.replace(m2.group(), maths[int(m2.group(1))]) + for m2 in Rmarker4.finditer(text): + text = text.replace(m2.group(), values[int(m2.group(1))]) inside[count] = text
# Name @@ -931,6 +948,9 @@ for m2 in Rmarker3.finditer(param_val): param_val = param_val.replace(m2.group(), maths[int(m2.group(1))]) + for m2 in Rmarker4.finditer(param_val): + param_val = param_val.replace(m2.group(), + values[int(m2.group(1))]) params[param_name.strip()] = param_val.strip()
# Add it to the result