Class: WebpageArchivist::StylesheetDocument

Inherits:
Object
  • Object
show all
Defined in:
lib/webpage-archivist/stylesheet_document.rb

Overview

Wrapper around css_parser

Constant Summary collapse

CONVERTER =
Iconv.new('UTF-8//IGNORE//TRANSLIT', 'ASCII//IGNORE//TRANSLIT')

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content, base_uri, charset = nil) ⇒ StylesheetDocument

Returns a new instance of StylesheetDocument.



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/webpage-archivist/stylesheet_document.rb', line 63

def initialize content, base_uri, charset = nil
  @base_uri = base_uri
  @parser = CssParser::Parser.new :import => false

  @charset = charset
  unless @charset
    content = CONVERTER.iconv(content)
    @charset = 'ASCII-8BIT'
  end

  parser.add_block_archivist(content, {:base_uri => base_uri, :charset => @charset})
  @expanded = false
end

Instance Attribute Details

#charsetObject (readonly)

Returns the value of attribute charset.



61
62
63
# File 'lib/webpage-archivist/stylesheet_document.rb', line 61

def charset
  @charset
end

#parserObject (readonly)

Returns the value of attribute parser.



61
62
63
# File 'lib/webpage-archivist/stylesheet_document.rb', line 61

def parser
  @parser
end

Instance Method Details

#each_image(&block) ⇒ Object

Call a block for each image Block call parameter will be the image uri, if the block return something it will replace the uri



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/webpage-archivist/stylesheet_document.rb', line 99

def each_image &block
  expand_if_needed
  parser.each_rule_set do |rs|
    rs.declarations.each do |r|
      value = r[1][:value]
      if uri = /url\(['|"]([^']+)['|"]\)/i.match(value)
        uri = uri[1]
        if (uri = block.yield(uri))
          r[1][:value] = "url(\"#{uri}\")"
        end
      end
    end
  end
end

#each_import(&block) ⇒ Object

Call a block for each import Block call parameter will be the import’s uri if the block return something it will replace the uri



90
91
92
93
94
# File 'lib/webpage-archivist/stylesheet_document.rb', line 90

def each_import &block
  parser.imports.collect! do |uri|
    block.yield(uri) || uri
  end
end

#expand_if_neededObject



77
78
79
80
81
82
83
84
85
# File 'lib/webpage-archivist/stylesheet_document.rb', line 77

def expand_if_needed
  unless @expanded
    parser.each_rule_set do |rs|
      rs.expand_background_shorthand!
      rs.expand_list_style_shorthand!
    end
    @expanded = true
  end
end

#to_cssObject

Get the css content



115
116
117
# File 'lib/webpage-archivist/stylesheet_document.rb', line 115

def to_css
  parser.to_s
end