Initial spike for meta pages
This commit is contained in:
parent
ae743ac4db
commit
22fcc3c108
59
middleman-core/lib/middleman-core/meta_pages.rb
Normal file
59
middleman-core/lib/middleman-core/meta_pages.rb
Normal 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
|
|
@ -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>
|
||||||
|
|
|
@ -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>
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
require "webrick"
|
require "webrick"
|
||||||
|
require 'middleman-core/meta_pages'
|
||||||
|
|
||||||
module Middleman
|
module Middleman
|
||||||
module PreviewServer
|
module PreviewServer
|
||||||
|
@ -157,7 +158,15 @@ module Middleman
|
||||||
|
|
||||||
start_file_watcher
|
start_file_watcher
|
||||||
|
|
||||||
@webrick.mount "/", ::Rack::Handler::WEBrick, app.class.to_rack_app
|
rack_app = 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
|
end
|
||||||
|
|
||||||
# Detach the current Middleman::Application instance
|
# Detach the current Middleman::Application instance
|
||||||
|
|
Loading…
Reference in a new issue