Class: Nanoc2::Extra::AutoCompiler

Inherits:
Object
  • Object
show all
Defined in:
lib/nanoc2/extra/auto_compiler.rb

Overview

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

Defined Under Namespace

Classes: UnknownHandlerError

Constant Summary collapse

HANDLER_NAMES =
[ :thin, :mongrel, :webrick, :ebb, :cgi, :fastcgi, :lsws, :scgi ]
ERROR_404 =
<<END
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<title>404 File Not Found</title>
		<style type="text/css">
			body { padding: 10px; border: 10px solid #f00; margin: 10px; font-family: Helvetica, Arial, sans-serif; }
		</style>
	</head>
	<body>
		<h1>404 File Not Found</h1>
		<p>The file you requested, <i><%=h path %></i>, was not found on this server.</p>
	</body>
</html>
END
ERROR_500 =
<<END
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<title>500 Server Error</title>
		<style type="text/css">
			body { padding: 10px; border: 10px solid #f00; margin: 10px; font-family: Helvetica, Arial, sans-serif; }
		</style>
	</head>
	<body>
		<h1>500 Server Error</h1>
		<p>An error occurred while compiling the page you requested, <i><%=h path %></i>.</p>
		<p>If you think this is a bug in nanoc, please do <a href="http://nanoc.stoneship.org/trac/newticket">report it</a>&mdash;thanks!</p>
		<p>Message:</p>
		<blockquote><p><%=h message %></p></blockquote>
		<p>Page compilation stack:</p>
		<ol>
<% @site.compiler.stack.reverse.each do |item| %>
<%   if item.is_a?(Nanoc2::PageRep) # page rep %>
			<li><strong>Page</strong> <%= item.page.path %> (rep <%= item.name %>)</li>
<% else # layout %>
			<li><strong>Layout</strong> <%= item.path %></li>
<% end %>
<% end %>
		</ol>
		<p>Backtrace:</p>
		<ol>
<% exception.backtrace.each do |line| %>
			<li><%= line %></li>
<% end %>
		</ol>
	</body>
</html>
END

Instance Method Summary collapse

Constructor Details

#initialize(site, include_outdated = false) ⇒ AutoCompiler

Creates a new autocompiler for the given site.



68
69
70
71
72
73
74
75
76
77
# File 'lib/nanoc2/extra/auto_compiler.rb', line 68

def initialize(site, include_outdated=false)
  # Set site
  @site = site

  # Set options
  @include_outdated = include_outdated

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

Instance Method Details

#start(port, handler_name) ⇒ Object

Starts the server on the given port.

port

The port the autocompiler web server should be started on. Can be nil; in this case the server will be started on port 3000.

handler_name

A symbol containing the name of the handler to use. See HANDLER_NAMES for a list of supported handlers. Can be set to nil; in this case the best handler will be picked.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/nanoc2/extra/auto_compiler.rb', line 88

def start(port, handler_name)
  require 'mime/types'
  require 'rack'

  # Determine handler
  if handler_name.nil?
    handler = preferred_handler
  else
    handler = handler_named(handler_name.to_sym)
    raise UnknownHandlerError.new(handler_name) if handler.nil?
  end

  # Build Rack app
  app = lambda do |env|
    begin
      handle_request(env['PATH_INFO'])
    rescue Exception => exception
      return serve_500(nil, exception)
    end
  end

  # Run Rack app
  port ||= 3000
  handler.run(app, :Port => port, :port => port) do |server|
    trap(:INT) { server.stop }
  end
end