Class: Mascut::Hamlth

Inherits:
Object
  • Object
show all
Defined in:
lib/mascut/hamlth.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Hamlth

Returns a new instance of Hamlth.



6
7
8
# File 'lib/mascut/hamlth.rb', line 6

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/mascut/hamlth.rb', line 10

def call(env)
  case path = env['PATH_INFO'][1..-1] # remove / and path is now relative
  when /\.(html|haml)$/
    data = ( File.exist?(path) or File.exist?(path.sub!(/html$/, 'haml')) ) ? hamlize(path) : raise
    [ 200, { 'Content-Type' => 'text/html', 'Content-Length' => Rack::Utils.bytesize(data).to_s }, [ data ] ]
  when /\.(css|sass)$/
    data = ( File.exist?(path) or File.exist?(path.sub!(/css$/, 'sass')) ) ? sassize(path) : raise
    [ 200, { 'Content- type' => 'text/css', 'Content-Length' => Rack::Utils.bytesize(data).to_s  }, [ data ] ]
  else
    raise # all raise jumps to app.call
  end
rescue => e
  @app.call(env)
end

#hamlize(path) ⇒ Object



25
26
27
# File 'lib/mascut/hamlth.rb', line 25

def hamlize(path)
  Haml::Engine.new(File.read(path)).to_html
end

#sassize(path) ⇒ Object



29
30
31
# File 'lib/mascut/hamlth.rb', line 29

def sassize(path)
  Sass::Engine.new(File.read(path)).to_css
end