Class: Rack::Highlighter

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/rack/highlighter.rb,
lib/rack/highlighter/version.rb

Constant Summary collapse

DEFAULT_LANG =
'text'
DEFAULT_OPTS =
{
  elements:  ['pre', 'code'],
  attribute: 'class',
  pattern:   /(?<lang>\w+)/,
  misc:      {}
}
VERSION =
'0.2.1'

Instance Method Summary collapse

Constructor Details

#initialize(app, highlighter, opts = {}) ⇒ Highlighter

Returns a new instance of Highlighter.



20
21
22
23
24
25
# File 'lib/rack/highlighter.rb', line 20

def initialize(app, highlighter, opts = {})
  @app = app
  @highlighter = highlighter
  @coder = ::HTMLEntities.new
  @opts = DEFAULT_OPTS.merge(opts)
end

Instance Method Details

#call(env) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rack/highlighter.rb', line 27

def call(env)
  status, headers, response = @app.call(env)
  headers = HeaderHash.new(headers)

  if should_highlight?(status, headers)
    content = extract_content(response)

    doc = ::Nokogiri::HTML(content)
    search_for_nodes(doc).each { |node| highlight_node(node) }
    content = doc.to_html

    headers['content-length'] = bytesize(content).to_s
    response = [content]
  end

  [status, headers, response]
end