Sunday, 15 September 2013

making a self made html fixer handle universal tags

making a self made html fixer handle universal tags

what this following code does is, it takes a set open tag and a end tag,
and if there are any unclosed tags of that type, it will close them. the
problem with this is, it wont handle font tags, as there is more to font
tags then just <font>. i was thinking that there would be a way to use
regex to have it match the tags before hand then pass it to this html
fixer so that way it could handle any type of tag. any suggestions? the
regex would probably look like <+[\w ="\']+?> for the start tag and </+[\w
="\']+?> for the end tag. and this is in 3.x if you are wondering about
the odd syntax i use.
def check_html(otag, etag, text):
ret = ['f', text, otag, etag] if text.count(otag) != text.count(etag)
else ['a', text, otag, etag]
return fix_html(ret)
def fix_html(x):
grade, text, otag, etag = x
ret = [otag + text if text.endswith(etag) else s for s in
text.split()] if grade == 'f' else text
ret = [text + etag if text.startswith(otag) else s for s in ret] if
grade == 'f' else text
return ret[0] if grade == 'f' else ret

No comments:

Post a Comment