MCPcopy Create free account
hub / github.com/commonmark/cmark / MyHTMLParser

Class MyHTMLParser

test/normalize.py:22–129  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

20significant_attrs = ["alt", "href", "src", "title"]
21whitespace_re = re.compile(r"\s+")
22class MyHTMLParser(HTMLParser):
23 def __init__(self):
24 HTMLParser.__init__(self)
25 self.convert_charrefs = False
26 self.last = "starttag"
27 self.in_pre = False
28 self.output = ""
29 self.last_tag = ""
30 def handle_data(self, data):
31 after_tag = self.last == "endtag" or self.last == "starttag"
32 after_block_tag = after_tag and self.is_block_tag(self.last_tag)
33 if after_tag and self.last_tag == "br":
34 data = data.lstrip('\n')
35 if not self.in_pre:
36 data = whitespace_re.sub(' ', data)
37 if after_block_tag and not self.in_pre:
38 if self.last == "starttag":
39 data = data.lstrip()
40 elif self.last == "endtag":
41 data = data.strip()
42 self.output += data
43 self.last = "data"
44 def handle_endtag(self, tag):
45 if tag == "pre":
46 self.in_pre = False
47 elif self.is_block_tag(tag):
48 self.output = self.output.rstrip()
49 self.output += "</" + tag + ">"
50 self.last_tag = tag
51 self.last = "endtag"
52 def handle_starttag(self, tag, attrs):
53 if tag == "pre":
54 self.in_pre = True
55 if self.is_block_tag(tag):
56 self.output = self.output.rstrip()
57 self.output += "<" + tag
58 # For now we don't strip out 'extra' attributes, because of
59 # raw HTML test cases.
60 # attrs = filter(lambda attr: attr[0] in significant_attrs, attrs)
61 if attrs:
62 attrs.sort()
63 for (k,v) in attrs:
64 self.output += " " + k
65 if v in ['href','src']:
66 self.output += ("=" + '"' +
67 urllib.quote(urllib.unquote(v), safe='/') + '"')
68 elif v != None:
69 self.output += ("=" + '"' + html.escape(v,quote=True) + '"')
70 self.output += ">"
71 self.last_tag = tag
72 self.last = "starttag"
73 def handle_startendtag(self, tag, attrs):
74 """Ignore closing tag for self-closing """
75 self.handle_starttag(tag, attrs)
76 self.last_tag = tag
77 self.last = "endtag"
78 def handle_comment(self, data):
79 self.output += '<!--' + data + '-->'

Callers 1

normalize_htmlFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected