Skip to content

Instantly share code, notes, and snippets.

@clemesha
Created May 25, 2009 11:12
Show Gist options
  • Save clemesha/117497 to your computer and use it in GitHub Desktop.
Save clemesha/117497 to your computer and use it in GitHub Desktop.
#!/usr/bin/python2.5
import xml.etree.ElementTree as ET
import sys
root = ET.Element("html")
head = ET.Element("head")
title = ET.Element("title")
title.text = " "
css = ET.Element("link")
css.set("rel", "stylesheet")
css.set("type", "text/css")
css.set("href", "/static/style.css")
css.text = " "
script = ET.Element("script")
script.set("src", "/static/script.js")
script.set("type", "text/javascript")
script.text = " "
for elem in [title, css, script]:
head.append(elem)
root.append(head)
body = ET.Element("body")
for id in ["header", "container", "footer"]:
div = ET.Element("div")
div.set("id", id)
div.text = "\n "
body.append(div)
root.append(body)
def indent(elem, level=0):
i = "\n" + level*" "
if len(elem):
if not elem.text or not elem.text.strip():
elem.text = i + " "
for e in elem:
indent(e, level+1)
if not e.tail or not e.tail.strip():
e.tail = i + " "
if not e.tail or not e.tail.strip():
e.tail = i
else:
if level and (not elem.tail or not elem.tail.strip()):
elem.tail = i
if __name__ == "__main__":
indent(root)
tree = ET.ElementTree(root)
tree.write(sys.stdout, "utf-8")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment