fix escaping of the & (helping #2)

This commit is contained in:
math2001
2016-12-10 08:08:05 +11:00
parent a9cfdd107f
commit e6ea13f0a2
4 changed files with 102 additions and 0 deletions

38
escape_amp.py Normal file
View File

@ -0,0 +1,38 @@
# -*- encoding: utf-8 -*-
import re
__all__ = [
'escape_amp'
]
RE_REPLACE_AMPERSAND = re.compile(r'&(\w*)(;)?')
def replace(matchobj):
if matchobj.group(2):
return matchobj.group(0)
else:
return matchobj.group(0).replace('&', '&')
def escape_amp(text):
return RE_REPLACE_AMPERSAND.sub(replace, text)
def run_tests():
tests = [
['&', '&'],
['&amp', '&amp'],
['&', '&'],
['& &hello &bonjour;', '& &hello &bonjour;']
]
fails = 0
for i, (subject, result) in enumerate(tests):
if RE_REPLACE_AMPERSAND.sub(replace, subject) != result:
print('TEST FAIL ({i}): {subject!r} escaped did not match {result!r}'.format(**locals()))
fails += 1
if fails == 0:
print("SUCCESS: every tests ({}) passed successfully!".format(len(tests)))
else:
print("{} test{} failed".format(fails, 's' if fails > 1 else ''))
if __name__ == '__main__':
run_tests()