Module: Sinatra::Pages

Defined in:
lib/sinatra/pages.rb

Class Method Summary collapse

Class Method Details

.registered(app) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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/sinatra/pages.rb', line 7

def self.registered(app)
  app.configure do
    app.set :root, Dir.pwd
    app.enable :static
  end

  app.set :pages, Proc.new {app.views}
  app.set :styles, Proc.new {find_styles_directory app.public}
  app.set :html, :v5
  app.set :stylesheet, :scss
  app.set :cache, :write
  app.set :format, :tidy
  app.set :encoding, :utf8 if RUBY_VERSION.to_f > 1.8
  app.disable :escaping
  
  app.configure do
    app.set :haml, Proc.new {generate_setup :haml, app}
    app.set :sass, Proc.new {generate_setup :sass, app}
  end
  
  app.condition {app.stylesheet != :css}
  app.get '/*.css' do
    content_type :css, :charset => 'utf-8'

    sass File.basename(params[:splat].first).to_sym, :views => settings.styles
  end

  %w[/? /:page/? /*/:page/?].each do |route|
    app.get route do
      params[:page] = 'home' if params[:page].nil?

      page_to_render = settings.views != settings.pages ? "#{settings.pages.split('/').last}/" : ''
      page_to_render << "#{params[:splat].first}/" unless params[:splat].nil?
      page_to_render << params[:page]

      begin
        haml page_to_render.to_sym, :layout => !request.xhr?
      rescue Errno::ENOENT
        halt 404
      end
    end
  end
  
  app.not_found do
    params[:page] = 'not_found'

    haml :not_found, :layout => !request.xhr?
  end
end