Class: Nanoc3::Extra::AutoCompiler

Nanoc3::Extra::AutoCompiler is a web server that will automatically compile items as they are requested. It also serves static files such as stylesheets and images.

Constructor Summary

public initialize(site)

Creates a new autocompiler for the given site.

[View source]


11
12
13
14
15
16
17
18
19
20
# File 'lib/nanoc3/extra/auto_compiler.rb', line 11

def initialize(site)
  require 'rack'
  require 'mime/types'

  # Set site
  @site = site

  # Create mutex to prevent parallel requests
  @mutex = Mutex.new
end

Public Visibility

Public Instance Method Summary

#call(env)

Public Instance Method Details

call

public call(env)
[View source]


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
56
57
58
59
60
61
62
63
# File 'lib/nanoc3/extra/auto_compiler.rb', line 22

def call(env)
  @mutex.synchronize do
    # Reload site data
    @site.load_data(true)

    # Find rep
    path = env['PATH_INFO']
    reps = @site.items.map { |i| i.reps }.flatten
    rep = reps.find { |r| r.path == path }

    if rep
      serve(rep)
    else
      # Get paths by appending index filenames
      if path =~ /\/$/
        possible_paths = @site.config[:index_filenames].map { |f| path + f }
      else
        possible_paths = [ path ]
      end

      # Find matching file
      modified_path = possible_paths.find { |f| File.file?(@site.config[:output_dir] + f) }
      modified_path ||= path

      # Serve using Rack::File
      file_server.call(env.merge('PATH_INFO' => modified_path))
    end
  end
rescue StandardError, ScriptError => e
  # Add compilation stack to env
  env['nanoc.stack'] = []
  @site.compiler.stack.reverse.each do |obj|
    if obj.is_a?(Nanoc3::ItemRep) # item rep
      env['nanoc.stack'] << "[item] #{obj.item.identifier} (rep #{obj.name})"
    else # layout
      env['nanoc.stack'] << "[layout] #{obj.identifier}"
    end
  end

  # Re-raise error
  raise e
end
Generated on Sunday, August 09 2009 at 01:43:14 PM by YARD 0.2.3.2 (ruby-1.8.7).