Skip to content

Instantly share code, notes, and snippets.

@JKirchartz
Created August 1, 2011 15:30
Show Gist options
  • Save JKirchartz/1118350 to your computer and use it in GitHub Desktop.
Save JKirchartz/1118350 to your computer and use it in GitHub Desktop.
Generate a CSS document based on the content of a HTML file.
#! /usr/bin/env python
from BeautifulSoup import BeautifulSoup
import os.path
import re
def html2css(FILEsource, OUTPUT):
fname = os.path.join(os.curdir, FILEsource)
FILE, OUT = open(fname,'r'), open(OUTPUT,'w')
source, Tags_regexp, HTML, CSS = FILE.read(), "(?i)<\/?\w+((\s+\w+(\s*=\s*(?:\".*?\"|'.*?'|[^'\">\s]+))?)+\s*|\s*)\/?>", '', ''
IDlist, CLASSlist, TAGlist = [], [], []
for match in re.finditer(Tags_regexp, source):
HTML += str(match.group())
soup = BeautifulSoup(HTML)
for ids in soup.findAll(attrs={'id' : True}):
IDlist.append(ids['id'])
for classes in soup.findAll(attrs={'class' : True}):
CLASSlist.append(classes['class'])
for tags in soup.body.findAll(True):
TAGlist.append(tags.name)
IDlist = list(set(IDlist))
IDlist.sort()
CLASSlist = list(set(CLASSlist))
CLASSlist.sort()
TAGlist = list(set(TAGlist))
TAGlist.sort()
for tgs in TAGlist:
CSS += tgs + ' { }\n'
for ids in IDlist:
CSS += '#' + ids + ' { }\n'
for cls in CLASSlist:
CSS += '.' + cls + ' { }\n'
OUT.write(CSS)
OUT.close()
FILE.close()
html2css("in.html","out.css")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment