Class: StyleSheetParser

Inherits:
Object
  • Object
show all
Defined in:
lib/csspress/style_sheet_parser.rb

Overview

This class is responsible for processing a CSS file. It creates a StyleSheet object from the content of the file. The StyleSheet can be obtained using the style_sheet() method.

Example:

ssp = StyleSheetParser("foo.css")
stylesheet = ssp.style_sheet
puts stylesheet

– Due to the flexible nature of CSS syntax most of the work performed by the parser is envolved in normalizing the text into an array of style rules with the comments striped out.

This parser uses patern matching and string manupulation rather than analysing the file byte, by byte. This may or may not be proved to be a good thing.

At present all ‘@import’ statements with in the CSS file are ignored!

A file is processed as follows:

  1. The file is read into an array (@raw_data)

  2. White space is removed from the beginnig and end of each field in the @raw_data

  3. Comments are sepatated into there own array fields as they could be mixed into style code

  4. All fields with comments and empty fields are removed from @raw_data

  5. All style rules are put in their own array field

  6. Each field has any white space removed

  7. A style sheet object is populated with the parsed rules

Data clean!

Only the file in gerneral is parsed. Parsing of Rules and Declarations is handled by internaly by nested classes. StyleSheet parser breaks a file down into indevidule rules. It then uses a RuleParse object to parse each of these rules. The rule parser then breaks up the rule into a name and declaration block. It the uses a Declaration Parser on each declaration. ++

Defined Under Namespace

Classes: DeclarationParser, RuleParser

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ StyleSheetParser

Creates new StyleSheetParser to parse the file file



54
55
56
57
58
59
# File 'lib/csspress/style_sheet_parser.rb', line 54

def initialize( file )
  @file = file
  @raw_data = []
  @style_sheet = nil
  parse
end

Instance Method Details

#style_sheetObject

Get parsed style sheet



63
64
65
# File 'lib/csspress/style_sheet_parser.rb', line 63

def style_sheet
  @style_sheet.dup
end