Class: Rack::Coderay::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/coderay/parser.rb

Overview

This class is the interface between Rack and the CodeRay gem

Constant Summary collapse

DEFAULT_CODERAY_OPTS =

Defaults for the Coderay gem config

{
  :css => :class
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, markup_trigger, options = {}) ⇒ Parser

Returns a new instance of Parser.


17
18
19
20
21
22
# File 'lib/rack/coderay/parser.rb', line 17

def initialize(app, markup_trigger, options = {})
  @app = app
  @markup_trigger = markup_trigger
  self.coderay_options = DEFAULT_CODERAY_OPTS.merge(options)
  raise "Markup trigger must include a 'lang' attribute" if !markup_trigger.include?('lang')
end

Instance Attribute Details

#coderay_optionsObject

Coderay gem options for HTML encoder, see coderay.rubychan.de/doc/classes/CodeRay/Encoders/HTML.html


15
16
17
# File 'lib/rack/coderay/parser.rb', line 15

def coderay_options
  @coderay_options
end

Instance Method Details

#call(env) ⇒ Object

method required by Rack interface


25
26
27
# File 'lib/rack/coderay/parser.rb', line 25

def call(env)
  call! env
end

#call!(env) ⇒ Object

thread safe version using shallow copy of env


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rack/coderay/parser.rb', line 30

def call!(env)
  @env = env.dup
  status, headers, response = @app.call(@env)
  if headers["Content-Type"] && headers["Content-Type"].include?("text/html")
    headers.delete('Content-Length')
    response = Rack::Response.new(
      response = parse_and_replace(response.respond_to?(:body) ? response.body : response),
      status,
      headers
    )
    response.finish
    response.to_a
  else
    [status, headers, response]
  end
end