Skip to content

Instantly share code, notes, and snippets.

@jonasws
Last active August 19, 2016 09:22
Show Gist options
  • Save jonasws/48c3fc4cd4bfef7774d3c0fc45814991 to your computer and use it in GitHub Desktop.
Save jonasws/48c3fc4cd4bfef7774d3c0fc45814991 to your computer and use it in GitHub Desktop.
Filter that converts HTML to something XML compliant (closing tags++)
#!/usr/bin/env python3
'''
NOTE: This script requires beautifulsoup4 to be installed.
Use this script as a filter to convert HTML to XML compliant markup. Useful when pasting raw HTML where XML is expected.
Combine with tools like clipcopy to make for a nice copy-and-paste utility, for example:
./convert_to_valid_xml.py < index.html | clipcopy
'''
import sys
from bs4 import BeautifulSoup
soup = BeautifulSoup(sys.stdin, 'html.parser')
print(soup.prettify(formatter='xml'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment