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

- (Parser) initialize(context)

Returns a new instance of Parser



62
63
64
65
# File 'lib/serve/handlers/dynamic_handler.rb', line 62

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

Instance Attribute Details

- (Object) context

Returns the value of attribute context



60
61
62
# File 'lib/serve/handlers/dynamic_handler.rb', line 60

def context
  @context
end

- (Object) engine

Returns the value of attribute engine



60
61
62
# File 'lib/serve/handlers/dynamic_handler.rb', line 60

def engine
  @engine
end

- (Object) script_extension

Returns the value of attribute script_extension



60
61
62
# File 'lib/serve/handlers/dynamic_handler.rb', line 60

def script_extension
  @script_extension
end

- (Object) script_filename

Returns the value of attribute script_filename



60
61
62
# File 'lib/serve/handlers/dynamic_handler.rb', line 60

def script_filename
  @script_filename
end

Instance Method Details

- (Object) parse_file(filename)



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/serve/handlers/dynamic_handler.rb', line 67

def parse_file(filename)
  old_script_filename, old_script_extension, old_engine = @script_filename, @script_extension, @engine
  
  @script_filename = filename
  
  ext = File.extname(filename).sub(/^\.html\.|^\./, '').downcase
  
  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(filename, nil, :outvar => '@_out_buf')
  
  raise "#{ext} extension not supported" if @engine.nil?
  
  @engine.render(context) do |*args|
    context.get_content_for(*args)
  end
ensure
  @script_filename = old_script_filename
  @script_extension = old_script_extension
  @engine = old_engine
end