Class: WebpageArchivist::StylesheetDocument
- Inherits:
-
Object
- Object
- WebpageArchivist::StylesheetDocument
- 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
-
#charset ⇒ Object
readonly
Returns the value of attribute charset.
-
#parser ⇒ Object
readonly
Returns the value of attribute parser.
Instance Method Summary collapse
-
#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.
-
#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.
- #expand_if_needed ⇒ Object
-
#initialize(content, base_uri, charset = nil) ⇒ StylesheetDocument
constructor
A new instance of StylesheetDocument.
-
#to_css ⇒ Object
Get the css content.
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
#charset ⇒ Object (readonly)
Returns the value of attribute charset.
61 62 63 |
# File 'lib/webpage-archivist/stylesheet_document.rb', line 61 def charset @charset end |
#parser ⇒ Object (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 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_needed ⇒ Object
77 78 79 80 81 82 83 84 85 |
# File 'lib/webpage-archivist/stylesheet_document.rb', line 77 def unless @expanded parser.each_rule_set do |rs| rs. rs. end @expanded = true end end |
#to_css ⇒ Object
Get the css content
115 116 117 |
# File 'lib/webpage-archivist/stylesheet_document.rb', line 115 def to_css parser.to_s end |