http://www.mediawiki.org/wiki/Special:Code/pywikipedia/9225
Revision: 9225 Author: cydeweys Date: 2011-05-09 18:40:55 +0000 (Mon, 09 May 2011) Log Message: ----------- An improvement that removes the entire line (rather than just the text of a line) when a category is being removed and the line has nothing other than whitespace on it. This prevents category removals from leaving gaps in category listings.
Modified Paths: -------------- trunk/pywikipedia/pywikibot/textlib.py
Modified: trunk/pywikipedia/pywikibot/textlib.py =================================================================== --- trunk/pywikipedia/pywikibot/textlib.py 2011-05-08 15:38:45 UTC (rev 9224) +++ trunk/pywikipedia/pywikibot/textlib.py 2011-05-09 18:40:55 UTC (rev 9225) @@ -617,8 +617,17 @@ title = title.replace(r"\ ", "[ _]+").replace(r"_", "[ _]+") categoryR = re.compile(r'[[\s*(%s)\s*:\s*%s\s*((?:|[^]]+)?]])' % (catNamespace, title), re.I) + categoryRN = re.compile(r'^[^\S\n]*[[\s*(%s)\s*:\s*%s\s*((?:|[^]]+)?]])[^\S\n]*\n' + % (catNamespace, title), re.I | re.M) if newcat is None: - text = replaceExcept(oldtext, categoryR, '', + """ First go through and try the more restrictive regex that removes + an entire line, if the category is the only thing on that line (this + prevents blank lines left over in category lists following a removal.) + """ + + text = replaceExcept(oldtext, categoryRN, '', + ['nowiki', 'comment', 'math', 'pre', 'source']) + text = replaceExcept(text, categoryR, '', ['nowiki', 'comment', 'math', 'pre', 'source']) else: text = replaceExcept(oldtext, categoryR,