From 22fcc3c10862eb65f22be6d3052dfb7118c4c03e Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Sat, 13 Oct 2012 13:12:47 -0700 Subject: [PATCH] Initial spike for meta pages --- .../lib/middleman-core/meta_pages.rb | 59 +++++++++++++++++++ .../meta_pages/templates/index.html.erb | 19 ++++++ .../meta_pages/templates/sitemap.html.erb | 20 +++++++ .../lib/middleman-core/preview_server.rb | 11 +++- 4 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 middleman-core/lib/middleman-core/meta_pages.rb create mode 100644 middleman-core/lib/middleman-core/meta_pages/templates/index.html.erb create mode 100644 middleman-core/lib/middleman-core/meta_pages/templates/sitemap.html.erb diff --git a/middleman-core/lib/middleman-core/meta_pages.rb b/middleman-core/lib/middleman-core/meta_pages.rb new file mode 100644 index 00000000..7e38ba9f --- /dev/null +++ b/middleman-core/lib/middleman-core/meta_pages.rb @@ -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 diff --git a/middleman-core/lib/middleman-core/meta_pages/templates/index.html.erb b/middleman-core/lib/middleman-core/meta_pages/templates/index.html.erb new file mode 100644 index 00000000..2de502a7 --- /dev/null +++ b/middleman-core/lib/middleman-core/meta_pages/templates/index.html.erb @@ -0,0 +1,19 @@ + + + + + + Middleman Meta Pages + + + +

Middleman Meta Pages

+ +

Peer into the bowels of your Middleman application with these handy views!

+ + + + + diff --git a/middleman-core/lib/middleman-core/meta_pages/templates/sitemap.html.erb b/middleman-core/lib/middleman-core/meta_pages/templates/sitemap.html.erb new file mode 100644 index 00000000..c8c3eb8b --- /dev/null +++ b/middleman-core/lib/middleman-core/meta_pages/templates/sitemap.html.erb @@ -0,0 +1,20 @@ + + + + + + Middleman Sitemap + + + +

Middleman Sitemap

+ More meta pages + + + + + diff --git a/middleman-core/lib/middleman-core/preview_server.rb b/middleman-core/lib/middleman-core/preview_server.rb index eddd6637..6e2f19ec 100644 --- a/middleman-core/lib/middleman-core/preview_server.rb +++ b/middleman-core/lib/middleman-core/preview_server.rb @@ -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