Class: Markitup::Middleware

Inherits:
Struct
  • Object
show all
Defined in:
lib/markitup/middleware.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#appObject

Returns the value of attribute app

Returns:

  • (Object)

    the current value of app



4
5
6
# File 'lib/markitup/middleware.rb', line 4

def app
  @app
end

#optionsObject

Returns the value of attribute options

Returns:

  • (Object)

    the current value of options



4
5
6
# File 'lib/markitup/middleware.rb', line 4

def options
  @options
end

Instance Method Details

#call(env) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/markitup/middleware.rb', line 6

def call(env)
  return app.call(env) unless config['enabled']

  # if env["REQUEST_METHOD"] == "POST"
  #   return render(:markdown, env) if env["PATH_INFO"] =~ /^\/markitup\/markdown/
  # end

  status, headers, response = app.call(env)
  return [ status, headers, response ] unless 
    headers["Content-Type"] =~ /text\/html|application\/xhtml\+xml/

  body = ""
  response.each { |part| body << part }
  return [ status, headers, response ] unless
    body =~ /#{config['keyword']}/

  index = body.rindex "</head>"
  if index
    body.insert index, styles + scripts
    headers["Content-Length"] = body.length.to_s
    response = [ body ]
  end
  [ status, headers, response ]
end