Revision: 4233 Author: wikipedian Date: 2007-09-11 12:38:49 +0000 (Tue, 11 Sep 2007)
Log Message: ----------- skip existing references sections that are commented out created wikipedia.isDisabled() to find out if a certain part of a page is commented out
Modified Paths: -------------- trunk/pywikipedia/noreferences.py trunk/pywikipedia/wikipedia.py
Modified: trunk/pywikipedia/noreferences.py =================================================================== --- trunk/pywikipedia/noreferences.py 2007-09-11 12:29:26 UTC (rev 4232) +++ trunk/pywikipedia/noreferences.py 2007-09-11 12:38:49 UTC (rev 4233) @@ -171,10 +171,13 @@ sectionR = re.compile(r'\r\n=+ *%s *=+\r\n' % section) match = sectionR.search(oldText) if match: - wikipedia.output(u'Adding references tag to existing %s section...\n' % section) - newText = oldText[:match.end()] + u'\n<references/>\n' + oldText[match.end():] - self.save(page, newText) - return + if wikipedia.isDisabled(oldText, match.start()): + wikipedia.output('Existing %s section is commented out, skipping.' % section) + else: + wikipedia.output(u'Adding references tag to existing %s section...\n' % section) + newText = oldText[:match.end()] + u'\n<references/>\n' + oldText[match.end():] + self.save(page, newText) + return
# Create a new section for the references tag for section in wikipedia.translate(wikipedia.getSite(), placeBeforeSections):
Modified: trunk/pywikipedia/wikipedia.py =================================================================== --- trunk/pywikipedia/wikipedia.py 2007-09-11 12:29:26 UTC (rev 4232) +++ trunk/pywikipedia/wikipedia.py 2007-09-11 12:38:49 UTC (rev 4233) @@ -2642,7 +2642,7 @@ text = text[:markerpos] + marker + text[markerpos:] return text
-def removeDisabledParts(text, parts=['*']): +def removeDisabledParts(text, tags = ['*']): """ Removes those parts of a wiki text where wiki markup is disabled, i.e. * HTML comments @@ -2659,11 +2659,26 @@ 'nowiki': r'<nowiki>.*?</nowiki>', 'pre': r'<pre>.*?</pre>', } - if '*' in parts: - parts = regexes.keys() - toRemoveR = re.compile('|'.join([regexes[p] for p in parts]), re.IGNORECASE | re.DOTALL) + if '*' in tags: + tags = regexes.keys() + toRemoveR = re.compile('|'.join([regexes[tag] for tag in tags]), re.IGNORECASE | re.DOTALL) return toRemoveR.sub('', text)
+def isDisabled(text, index, tags = ['*']): + """ + Checks whether the text part at the given location is disabled, e.g. + by a comment or by nowiki tags. + + For the tags parameter, see removeDisabledParts() above. + """ + # Find a marker that is not already in the text. + marker = '@@' + while marker in text: + marker += '@' + text = text[:index] + marker + text[index:] + text = removeDisabledParts(text, tags) + return (marker not in text) + # Part of library dealing with interwiki links
def getLanguageLinks(text, insite = None, pageLink = "[[]]"):