Initial spike for meta pages

This commit is contained in:
Ben Hollis 2012-10-13 13:12:47 -07:00 committed by Thomas Reynolds
parent ae743ac4db
commit 22fcc3c108
4 changed files with 108 additions and 1 deletions

View file

@ -0,0 +1,59 @@
require 'rack/builder'
require 'rack/static'
require 'tilt'
module Middleman
# Metadata pages to be served in preview, in order to present information about the Middleman
# application and its configuration. Analogous to Firefox/Chrome's "about:" pages.
#
# Built using a ghetto little Rack web framework cobbled together because I didn't want to depend
# on Sinatra or figure out how to host Middleman inside Middleman.
class MetaPages
def initialize(middleman)
# Hold a reference to the middleman application
@middleman = middleman
meta_pages = self
@rack_app = Rack::Builder.new do
# Serve assets from metadata/assets
use Rack::Static, :urls => ["/assets"], :root => File.join(File.dirname(__FILE__), 'meta_pages')
map '/' do
run meta_pages.method(:index)
end
map '/sitemap' do
run meta_pages.method(:sitemap)
end
end
end
def call(*args)
@rack_app.call(*args)
end
# The index page
def index(env)
template('index.html.erb')
end
# Inspect the sitemap
def sitemap(env)
template('sitemap.html.erb', :resources => @middleman.sitemap.resources(true))
end
private
# Render a template with the given name and locals
def template(template_name, locals={})
template_path = File.join(File.dirname(__FILE__), 'meta_pages', 'templates', template_name)
content = Tilt.new(template_path).render(nil, locals)
response(content)
end
# Respond to an HTML request
def response(content)
[ 200, {"Content-Type" => "text/html"}, Array(content) ]
end
end
end

View file

@ -0,0 +1,19 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Middleman Meta Pages</title>
</head>
<body>
<h1>Middleman Meta Pages</h1>
<p>Peer into the bowels of your Middleman application with these handy views!</p>
<ul>
<li><a href="sitemap/">Sitemap</a></li>
</ul>
</body>
</html>

View file

@ -0,0 +1,20 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Middleman Sitemap</title>
</head>
<body>
<h1>Middleman Sitemap</h1>
<a href="../">More meta pages</a>
<ul>
<% resources.each do |resource| %>
<li><%= resource.path %></li>
<% end %>
</ul>
</body>
</html>

View file

@ -1,4 +1,5 @@
require "webrick"
require 'middleman-core/meta_pages'
module Middleman
module PreviewServer
@ -156,8 +157,16 @@ module Middleman
@webrick ||= setup_webrick(@options[:debug] || false)
start_file_watcher
rack_app = app.class.to_rack_app
@webrick.mount "/", ::Rack::Handler::WEBrick, app.class.to_rack_app
# Add in the meta pages application
meta_app = Middleman::MetaPages.new(app.class.inst)
rack_app.map '/__middleman' do
run meta_app
end
@webrick.mount "/", ::Rack::Handler::WEBrick, rack_app
end
# Detach the current Middleman::Application instance