jenkins-bot has submitted this change and it was merged.
Change subject: [IMPROV] cosmetic_changes: Dynamic header fixer ......................................................................
[IMPROV] cosmetic_changes: Dynamic header fixer
Instead of iterating over the text for each of the allowed levels it could just dynamically react on each header instance via a replacement method.
Also instead of using the %-notation which doesn't allow reusing indexed parameters it can use `str.format` to generate the header wiki text.
Change-Id: I04a627e2d3f99ca093c1970a7ffa7d9b4f1630a1 --- M pywikibot/cosmetic_changes.py 1 file changed, 9 insertions(+), 8 deletions(-)
Approvals: John Vandenberg: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/cosmetic_changes.py b/pywikibot/cosmetic_changes.py index 485769d..6b4af85 100755 --- a/pywikibot/cosmetic_changes.py +++ b/pywikibot/cosmetic_changes.py @@ -731,6 +731,10 @@ return text
def fixHtml(self, text): + def replace_header(match): + depth = int(match.group(1)) + return r'{0} {1} {0}'.format('=' * depth, match.group(2)) + # Everything case-insensitive (?i) # Keep in mind that MediaWiki automatically converts <br> to <br /> exceptions = ['nowiki', 'comment', 'math', 'pre', 'source', @@ -748,14 +752,11 @@ r'<hr \1 />', exceptions) # a header where only spaces are in the same line - for level in range(1, 7): - equals = '\1%s \2 %s\3' % ("=" * level, "=" * level) - text = textlib.replaceExcept( - text, - r'(?i)([\r\n]) *<h%d> *([^<]+?) *</h%d> *([\r\n])' - % (level, level), - r'%s' % equals, - exceptions) + text = textlib.replaceExcept( + text, + r'(?i)(?<=[\r\n]) *<h([1-7])> *([^<]+?) *</h\1> *(?=[\r\n])', + replace_header, + exceptions) # TODO: maybe we can make the bot replace <p> tags with \r\n's. return text