Package cssutils :: Module parse' |
[hide private]
[frames] | no frames]

# Source Code for Module cssutils.parse'

      1  #!/usr/bin/env python
      2  """a validating CSSParser
      3
      4  Usage::
      5
      6      parser = CSSParser()
      7      stylesheet = p.parse('test1.css', 'ascii')
      8
      9      print stylesheet.cssText
     10
     11  """
     12  __all__ = ['CSSParser']
     13  __docformat__ = 'restructuredtext'
     14  __author__ = '$LastChangedBy: doerwalter $'
     15  __date__ = '$LastChangedDate: 2007-08-09 21:41:10 +0200 (Do, 09 Aug 2007) $'
     16  __version__ = '0.9.2a2 $LastChangedRevision: 210 $'
     17
     18  import codecs
     19
     20  import cssutils
     21  from cssutils import stylesheets
     22
     23
     24 -class CSSParser(object):
     25      """
     26      parses a CSS StyleSheet string or file and
     27      returns a DOM Level 2 CSS StyleSheet object
     28      """
     29
     30 -    def __init__(self, log=None, loglevel=None, raiseExceptions=False):
     31          """
     32          log
     33              logging object
     34          loglevel
     35              logging loglevel
     36          raiseExceptions
     37              if log should log (default) or raise
     38          """
     39          if log is not None:
     40              cssutils.log.setlog(log)
     41          if loglevel is not None:
     42              cssutils.log.setloglevel(loglevel)
     43
     44          cssutils.log.raiseExceptions = raiseExceptions
     45
     46
     47 -    def parseString(self, cssText, href=None, media=None):
     48          """
     49          parse a CSSStyleSheet string
     50          returns the parsed CSS as a CSSStyleSheet object
     51
     52          cssText
     53              CSS string to parse
     54          href
     55              The href attribute to assign to the generated stylesheet
     56          media
     57              The media attribute to assign to the generated stylesheet
     58              (may be a MediaList, list or a string)
     59          """
     60          stylesheet = cssutils.css.CSSStyleSheet()
     61          stylesheet.cssText = cssText
     62          stylesheet.href = href
     63          stylesheet.media = stylesheets.MediaList(media)
     64          return stylesheet
     65
     66
     67 -    def parse(self, filename, encoding=None, href=None, media=None):
     68          """
     69          parse a CSSStyleSheet file
     70          returns the parsed CSS as a CSSStyleSheet object
     71
     72          filename
     73              name of the CSS file to parse
     74          encoding
     75              of the CSS file
     76          href
     77              The href attribute to assign to the generated stylesheet
     78          media
     79              The media attribute to assign to the generated stylesheet
     80              (may be a MediaList or a string)
     81
     82          TODO:
     83              - parse encoding from CSS file? (@charset if given)
     84
     85          When a style sheet resides in a separate file, user agents must
     86          observe the following priorities when determining a style sheet's
     87          character encoding (from highest priority to lowest):
     88
     89          1. An HTTP "charset" parameter in a "Content-Type" field
     90             (or similar parameters in other protocols)
     91          2. BOM and/or @charset (see below)
     92          3. <link charset=""> or other metadata from the linking mechanism
     93             (if any)
     94          4. charset of referring style sheet or document (if any)
     95          5. Assume UTF-8
     96          """
     97          if not encoding:
     98              encoding = 'utf-8'
     99          cssText = codecs.open(filename, 'r', encoding).read()
    100
    101          # utf-8 BOM
    102          if cssText.startswith(u'\ufeff'):
    103              cssText = cssText[1:]
    104
    105          return self.parseString(cssText, href=href, media=media)
    106