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: cthedot $' 
 15  __date__ = '$LastChangedDate: 2007-11-25 20:25:58 +0100 (So, 25 Nov 2007) $' 
 16  __version__ = '$LastChangedRevision: 704 $' 
 17   
 18  import codec 
 19  import codecs 
 20  import cssutils 
 21  import cssutils.tokenize2 
 22  from cssutils import stylesheets 
 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 - def parseString(self, cssText, href=None, media=None):
47 """ 48 parse a CSSStyleSheet string 49 returns the parsed CSS as a CSSStyleSheet object 50 51 cssText 52 CSS string to parse 53 href 54 The href attribute to assign to the generated stylesheet 55 media 56 The media attribute to assign to the generated stylesheet 57 (may be a MediaList, list or a string) 58 """ 59 tokenizer = cssutils.tokenize2.Tokenizer().tokenize(cssText, fullsheet=True) 60 stylesheet = cssutils.css.CSSStyleSheet() 61 stylesheet.cssText = tokenizer 62 stylesheet.href = href 63 stylesheet.media = stylesheets.MediaList(media) 64 return stylesheet
65
66 - def parse(self, filename, encoding=None, href=None, media=None):
67 """ 68 parse a CSSStyleSheet file 69 returns the parsed CSS as a CSSStyleSheet object 70 71 filename 72 name of the CSS file to parse 73 encoding 74 of the CSS file, defaults to 'css' codec encoding 75 href 76 The href attribute to assign to the generated stylesheet 77 media 78 The media attribute to assign to the generated stylesheet 79 (may be a MediaList or a string) 80 81 TODO: 82 - parse encoding from CSS file? (@charset if given) 83 84 When a style sheet resides in a separate file, user agents must 85 observe the following priorities when determining a style sheet's 86 character encoding (from highest priority to lowest): 87 88 1. An HTTP "charset" parameter in a "Content-Type" field 89 (or similar parameters in other protocols) 90 2. BOM and/or @charset (see below) 91 3. <link charset=""> or other metadata from the linking mechanism 92 (if any) 93 4. charset of referring style sheet or document (if any) 94 5. Assume UTF-8 95 """ 96 if not encoding: 97 encoding = 'css' 98 cssText = codecs.open(filename, 'r', encoding).read() 99 100 # utf-8 BOM 101 #if cssText.startswith(u'\ufeff'): 102 # cssText = cssText[1:] 103 104 return self.parseString(cssText, href=href, media=media)
105