Class: StyleSheetParser
- Inherits:
-
Object
- Object
- StyleSheetParser
- 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:
-
The file is read into an array (@raw_data)
-
White space is removed from the beginnig and end of each field in the @raw_data
-
Comments are sepatated into there own array fields as they could be mixed into style code
-
All fields with comments and empty fields are removed from @raw_data
-
All style rules are put in their own array field
-
Each field has any white space removed
-
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
-
#initialize(file) ⇒ StyleSheetParser
constructor
Creates new StyleSheetParser to parse the file file.
-
#style_sheet ⇒ Object
Get parsed style sheet.
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_sheet ⇒ Object
Get parsed style sheet
63 64 65 |
# File 'lib/csspress/style_sheet_parser.rb', line 63 def style_sheet @style_sheet.dup end |