Class: Nanoc::Extra::AutoCompiler
- Inherits:
-
Object
- Object
- Nanoc::Extra::AutoCompiler
- Defined in:
- lib/nanoc/extra/auto_compiler.rb
Overview
A web server that will automatically compile items as they are requested. It also serves static files such as stylesheets and images.
Instance Attribute Summary (collapse)
-
- (Nanoc::Site) site
readonly
The site this autocompiler belongs to.
Instance Method Summary (collapse)
-
- (Array) call(env)
Calls the autocompiler.
-
- (AutoCompiler) initialize(site_path)
constructor
Creates a new autocompiler for the given site.
Constructor Details
- (AutoCompiler) initialize(site_path)
Creates a new autocompiler for the given site.
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/nanoc/extra/auto_compiler.rb', line 13 def initialize(site_path) require 'rack' require 'mime/types' # Set site @site_path = site_path # Create mutex to prevent parallel requests require 'thread' @mutex = Mutex.new end |
Instance Attribute Details
- (Nanoc::Site) site (readonly)
Returns The site this autocompiler belongs to
8 9 10 |
# File 'lib/nanoc/extra/auto_compiler.rb', line 8 def site @site end |
Instance Method Details
- (Array) call(env)
Calls the autocompiler. The behaviour of this method is defined by the Rack specification.
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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/nanoc/extra/auto_compiler.rb', line 32 def call(env) @mutex.synchronize do # Start with a new site build_site # Find rep path = Rack::Utils.unescape(env['PATH_INFO']) reps = site.items.map(&:reps).flatten rep = reps.find do |r| r.path == path || r.raw_path == site.config[:output_dir] + path end # Recompile site.compile if rep # 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 puts "*** serving file #{modified_path}" res = file_server.call(env.merge('PATH_INFO' => modified_path)) puts "*** done serving file #{modified_path}" res end rescue StandardError, ScriptError => e # Add compilation stack to env env['nanoc.stack'] = [] stack.reverse_each do |obj| if obj.is_a?(Nanoc::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 |