def fix_pair_tag(text, tag, recursive=False):
"""
Fix self-closing pair tags and return (new_text, replacements_count) tuple.
tag parameter must contains only name of the tag, for example, "b" for <b>.
recursive flag must be True if nested tags are correct. The default value is False.
Checks tag balance: if something going wrong, function willn't change anything.
Used in 2nd error.
"""
old_text = text
correct_tag = "</{}>".format(tag)
(text, fixed1) = re.subn(r"<[ ]*{}[ ]*[/\\]>".format(tag), correct_tag, text, flags=re.I)
(text, fixed2) = re.subn(r"<\\[ ]*{}[ ]*>".format(tag), correct_tag, text, flags=re.I)
if check_tag_balance(text, tag, recursive):
return (text, fixed1 + fixed2)
else:
return (old_text, 0)
评论列表
文章目录