Class: Serve::DynamicHandler::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/serve/handlers/dynamic_handler.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, template_path) ⇒ Parser

Returns a new instance of Parser



26
27
28
29
30
# File 'lib/serve/handlers/dynamic_handler.rb', line 26

def initialize(context, template_path)
  @context = context
  @context.parser = self
  @template_path = template_path
end

Instance Attribute Details

#contextObject

Returns the value of attribute context



24
25
26
# File 'lib/serve/handlers/dynamic_handler.rb', line 24

def context
  @context
end

#engineObject

Returns the value of attribute engine



24
25
26
# File 'lib/serve/handlers/dynamic_handler.rb', line 24

def engine
  @engine
end

#script_extensionObject

Returns the value of attribute script_extension



24
25
26
# File 'lib/serve/handlers/dynamic_handler.rb', line 24

def script_extension
  @script_extension
end

#template_pathObject

Returns the value of attribute template_path



24
25
26
# File 'lib/serve/handlers/dynamic_handler.rb', line 24

def template_path
  @template_path
end

Instance Method Details

#parse(input, ext, locals = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/serve/handlers/dynamic_handler.rb', line 32

def parse(input, ext, locals={})
  old_script_extension, old_engine = @script_extension, @engine

  if ext == 'slim' # Ugly, but works
    if Thread.list.size > 1
      warn "WARN: serve autoloading 'slim' in a non thread-safe way; " +
           "explicit require 'slim' suggested."
    end
    require 'slim'
  end

  @script_extension = ext

  @engine = Tilt[ext].new(nil, nil, {outvar: '@_out_buf'}.merge(FileTypeHandler.options_for(ext))){input}

  raise "#{ext} extension not supported" if @engine.nil?

  @engine.render(context, locals) do |*args|
    context.get_content_for(*args)
  end
ensure
  @script_extension = old_script_extension
  @engine = old_engine
end