Class: BeautifulCss::RuleParser

Inherits:
Object
  • Object
show all
Defined in:
lib/beautiful-css/rule_parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(str) ⇒ RuleParser

Returns a new instance of RuleParser.



6
7
8
9
# File 'lib/beautiful-css/rule_parser.rb', line 6

def initialize(str)
  @body = str
  @body = @body.gsub( /;base64,/ , '__base64__')
end

Instance Method Details

#bodyObject



41
42
43
# File 'lib/beautiful-css/rule_parser.rb', line 41

def body
  @body
end

#propsObject



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/beautiful-css/rule_parser.rb', line 15

def props
  begin
  p = @body.match(/\{([^}]*)/)[1].split(';').select{|s| !s.strip.empty? }
  p.map do |s|
    first, *rest = s.strip.split(':')
    rest.class == Array ? [first, rest.join(':') ] : [first, rest ]
  end
  rescue
    []
  end
end

#restore_special_strings(str) ⇒ Object



27
28
29
30
# File 'lib/beautiful-css/rule_parser.rb', line 27

def restore_special_strings str
  str = str.gsub( /__base64__/, ';base64,')
  str
end

#selectorsObject



11
12
13
# File 'lib/beautiful-css/rule_parser.rb', line 11

def selectors
  @body.match(/[^{]*/).to_s.split(/,/).map{|s| s.strip}.select{|s| !s.empty? }
end

#to_rulesObject



32
33
34
35
36
37
38
39
# File 'lib/beautiful-css/rule_parser.rb', line 32

def to_rules
  selectors.map do |selector|
    props.map do |prop|
      val = restore_special_strings prop[1]
      BeautifulCss::Rule.new(selector, prop[0], val)
    end
  end.flatten
end