Skip to content

Instantly share code, notes, and snippets.

@samba
Created February 4, 2015 00:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samba/b0879ffcefdec729d298 to your computer and use it in GitHub Desktop.
Save samba/b0879ffcefdec729d298 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
## Adjusts HTML structure and applies style attributes (etc) to static HTML.
## Conceptually inspired by jQuery, of course.
import sys
from xml.etree import ElementTree as tree
class QueryResult(object):
def __init__(self, matches):
self.matches = matches
def find(self, expression):
result = []
for i in self.matches:
result += i.findall(expression)
return QueryResult(Set(result))
def attr(self, **attribs):
for i in self.matches:
for name, value in attribs.iteritems():
i.set(name, value)
return self
def rename(self, new_name):
for i in self.matches:
i.tag = new_name
return self
class Document(object):
def __init__(self, filename):
self.doc = tree.parse(filename)
self.root = self.doc.getroot()
def query(self, expression):
return QueryResult(self.doc.findall(expression))
def write(self, output, method = 'html'):
self.doc.write(output, method = method)
def main(filename):
doc = Document(filename)
doc.query('table').attr(id = 'block-detailed', style = "border-collapse: inherit")
doc.query('table//tr[1]/td').rename('th')
doc.query('table//tr/td[2]').attr(style = 'font-weight: bold')
doc.query("table//td[@colspan='3'][1]/../td").attr(style = 'font-weight: bold; border-top: 1px solid #939393; padding-bottom: 2em;')
doc.write(sys.stdout, method = "html")
if __name__ == '__main__':
main(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment