Class: AppliedCSS

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

Constant Summary collapse

@@MAX_ATTEMPTS =
3

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ AppliedCSS

Returns a new instance of AppliedCSS.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/applied_css.rb', line 11

def initialize(options = {})
  @options = {:ancestors => false}.merge(options)
  @options[:cache] = 120 unless @options.has_key? :cache
  @options[:cache_type] = :full unless @options.has_key? :cache_type

  @options[:debug] = false unless @options.has_key? :debug
  
  @base_uri = Addressable::URI.parse(@options[:url])
  raise "url must be specified" unless @base_uri

  @doc = @options[:document]

  unless @doc
    html = get(@base_uri.to_s)
    @doc = Nokogiri::HTML.parse(html) unless html.nil?
  end

  puts "Genrating Parser." if @options[:debug]
  @parser = CssParser::Parser.new
  styles.each do |uri, style|
    puts "adding #{style}" if @options[:debug]
    @parser.add_block! style
  end
  @doc.css("style").each do |style_node|
    puts "adding #{style_node.text()}" if @options[:debug]
    @parser.add_block! style_node.text()
  end
end

Instance Method Details

#css(path) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/applied_css.rb', line 40

def css(path)
  css_declarations = {}
  ancestors_and_self(path).each do |node|
    css_declarations.merge!(node_declarations(node))
  end
  css_declarations
end

#docObject



48
49
50
# File 'lib/applied_css.rb', line 48

def doc
  @doc
end

#parserObject



69
70
71
# File 'lib/applied_css.rb', line 69

def parser
  @parser
end

#stylesObject



56
57
58
59
# File 'lib/applied_css.rb', line 56

def styles
  populate_style_hash if @style_hash.nil?
  @style_hash
end

#to_hashObject



61
62
63
64
65
66
67
# File 'lib/applied_css.rb', line 61

def to_hash
  hash = {}
  @parser.each_selector do |selector, declarations, specificity|
    hash[selector] = declarations
  end
  hash
end

#to_sObject



52
53
54
# File 'lib/applied_css.rb', line 52

def to_s
  @parser.to_s
end