def error_104_quote_marks_in_refs(text):
"""Fix the error and return (new_text, replacements_count) tuple."""
count3 = 0
def quote_ref(match):
"""Quote ref name if it's neccessary."""
#pylint: disable=undefined-variable
nonlocal count3
name = match.group(2)
if re.search(r"group\s*=", name):
return match.group(0)
if "\"" in name or re.match(r"^'.*'$", name):
# ref is already quotetd (all is ok) or has a quote (dangerous to fix)
return match.group(0)
if re.search(r"['/\\=?#\s]", name):
count3 += 1
return match.group(1) + "\"" + name + "\"" + match.group(3)
return match.group(0)
(text, count1) = re.subn(r"(<ref\s+name\s*=\s*\"[^\">]+?)(\s*/?>)", "\\1\"\\2", text)
(text, count2) = re.subn(r"(<ref\s+name\s*=\s*)([^\">]+?\"\s*/?>)", "\\1\"\\2", text)
text = re.sub(r"(<ref\s+name\s*=\s*)(.*?)(\s*/?>)", quote_ref, text)
return (text, count1 + count2 + count3)
评论列表
文章目录