"""classes and functions used by cssutils scripts
"""
__all__ = ['CSSCapture', 'csscombine']
__docformat__ = 'restructuredtext'
__version__ = '$Id: parse.py 1323 2008-07-06 18:13:57Z cthedot $'
import codecs
import errno
import HTMLParser
import logging
import os
import sys
import urllib2
import urlparse
import cssutils
try:
import cssutils.encutils as encutils
except ImportError:
try:
import encutils
except ImportError:
sys.exit("You need encutils from http://cthedot.de/encutils/")
# types of sheets in HTML
LINK = 0 #
STYLE = 1 #
class CSSCaptureHTMLParser(HTMLParser.HTMLParser):
"""CSSCapture helper: Parse given data for link and style elements"""
curtag = u''
sheets = [] # (type, [atts, cssText])
def _loweratts(self, atts):
return dict([(a.lower(), v.lower()) for a, v in atts])
def handle_starttag(self, tag, atts):
if tag == u'link':
atts = self._loweratts(atts)
if u'text/css' == atts.get(u'type', u''):
self.sheets.append((LINK, atts))
elif tag == u'style':
# also get content of style
atts = self._loweratts(atts)
if u'text/css' == atts.get(u'type', u''):
self.sheets.append((STYLE, [atts, u'']))
self.curtag = tag
else:
# close as only intersting