Skip to content

Instantly share code, notes, and snippets.

@unhammer
Created April 29, 2014 14:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unhammer/11401866 to your computer and use it in GitHub Desktop.
Save unhammer/11401866 to your computer and use it in GitHub Desktop.
parse html, print it out as-is (python3 boilerplate for html template like stuff)
#!/usr/bin/python3
from html.parser import HTMLParser
from sys import stdin
def p(*value):
print(*value, end='')
class MyHTMLParser(HTMLParser):
def handle_startendtag(self, tag, attrs):
p(self.get_starttag_text())
def handle_starttag(self, tag, attrs):
p(self.get_starttag_text())
def handle_endtag(self, tag):
p("</%s>" % (tag,))
def handle_data(self, data):
p(data)
def handle_comment(self, data):
p("<!--%s-->" % (data,))
def handle_entityref(self, name):
p("&%s;" % (name,))
def handle_charref(self, name):
p("&#%s;" % (name,))
def handle_decl(self, data):
p("<!%s>" % (data,))
def handle_pi(self, data):
p("<?%s>" % (data,))
parser = MyHTMLParser(convert_charrefs=False)
for line in stdin:
parser.feed(line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment