Class: Awestruct::Rack::GenerateOnAccess

Inherits:
Object
  • Object
show all
Defined in:
lib/awestruct/rack/generate.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ GenerateOnAccess

Returns a new instance of GenerateOnAccess



6
7
8
# File 'lib/awestruct/rack/generate.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
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/awestruct/rack/generate.rb', line 10

def call(env)
  engine = ::Awestruct::Engine.instance

  generate = false

  req_path = env['REQUEST_PATH']
  path = req_path
  path = req_path + 'index.html' if req_path.end_with? '/'

  page = engine.site.pages_by_output_path[path]
  if page.nil? and !req_path.end_with? '/'
    path = req_path + '/index.html'
    page = engine.site.pages_by_output_path[path]
  end

  unless page.nil?
    generate_path = File.join(engine.site.config.output_dir, page.output_path)

    generate = true if page.stale_output? generate_path
    generate = true if path.end_with? '.html'
  end

  if generate
    puts "Regenerate #{page.source_path}"

    engine.generate_page page, true
  end

  @app.call(env)
end