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

Returns a new instance of Middleware.

Parameters:

  • opts (Hash) (defaults to: {})

    Middlewar options

Options Hash (opts):

  • :stylesheets_path (String) — default: env['DOCUMENT_ROOT']

    Stylesheets root path or app’s public directory where the stylesheets are to be found

  • :paths (Regexp, Array, String)

    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 (Boolean) — default: false

    If set to true will inline style for pseudo classes according to the W3C specification: www.w3.org/TR/css-style-attr. Should probably be left as false because browsers don’t seem to comply with the specification for pseudo class style in the style attribute.



16
17
18
19
20
# File 'lib/inline-style/rack-middleware.rb', line 16

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

Instance Method Details

#call(env) ⇒ Object



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

def call env
  response = @app.call env
  return response unless @paths === env['PATH_INFO']
  
  status, headers, body = response
  
  body = InlineStyle.process(body.first, {:stylesheets_path => env['DOCUMENT_ROOT']}.merge(@opts))
  [status, headers, [body]]
end