Class: InlineStyle::Rack::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/inline-style/rack/middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, opts = {}) ⇒ Middleware

Options:

+document_root+
  File system path for app's public directory where the stylesheets are to be found, defaults to
  env['DOCUMENT_ROOT']

+paths+
  Limit processing to the passed absolute paths
  Can be an array of strings or regular expressions, a single string or regular expression
  If not passed will process output for every path.
  Regexps and strings must comence with '/'

+pseudo+
  If set to true will inline style for pseudo classes according to the W3C specification:
  http://www.w3.org/TR/css-style-attr.
  Defaults to false and should probably be left like that because at least Safari and Firefox don't seem to 
  comply with the specification for pseudo class style in the style attribute.


22
23
24
25
26
# File 'lib/inline-style/rack/middleware.rb', line 22

def initialize app, opts = {}
  @app   = app
  @opts  = {:document_root => env['DOCUMENT_ROOT']}.merge(opts)
  @paths = /^(?:#{ [*opts[:paths]].join('|') })/
end

Instance Method Details

#call(env) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/inline-style/rack/middleware.rb', line 28

def call env
  response = @app.call env
  return response unless @paths === env['PATH_INFO']
  
  status, headers, content = response
  response = ::Rack::Response.new '', status, headers
  body     = content.respond_to?(:body) ? content.body : content
  
  response.write InlineStyle.process(body, @opts)
  response.finish
end