diff --git a/middleman-core/lib/middleman-core/meta_pages.rb b/middleman-core/lib/middleman-core/meta_pages.rb index 7e38ba9f..340ee626 100644 --- a/middleman-core/lib/middleman-core/meta_pages.rb +++ b/middleman-core/lib/middleman-core/meta_pages.rb @@ -3,57 +3,79 @@ 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 + module MetaPages + # 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 Application + 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') + 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) + 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) + resources = @middleman.sitemap.resources(true) + + resource_tree = {} + resources.each do |resource| + branch = resource_tree + path_parts = resource.path.split('/') + path_parts[0...-1].each do |path_part| + branch[path_part] ||= {} + branch = branch[path_part] + end + branch[path_parts.last] = resource end - map '/sitemap' do - run meta_pages.method(:sitemap) + template('sitemap.html.erb', :resource_tree => resource_tree) + 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(RenderHelper.new, locals) + response(content) + end + + # Respond to an HTML request + def response(content) + [ 200, {"Content-Type" => "text/html"}, Array(content) ] + end + + class RenderHelper + def partial(partial_name, locals={}) + partial_path = File.join(File.dirname(__FILE__), 'meta_pages', 'templates', 'partials', partial_name) + content = Tilt.new(partial_path).render(RenderHelper.new, locals) 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/partials/sitemap_tree.html.erb b/middleman-core/lib/middleman-core/meta_pages/templates/partials/sitemap_tree.html.erb new file mode 100644 index 00000000..8d31ed2d --- /dev/null +++ b/middleman-core/lib/middleman-core/meta_pages/templates/partials/sitemap_tree.html.erb @@ -0,0 +1,12 @@ + 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 index c8c3eb8b..88a879ba 100644 --- a/middleman-core/lib/middleman-core/meta_pages/templates/sitemap.html.erb +++ b/middleman-core/lib/middleman-core/meta_pages/templates/sitemap.html.erb @@ -10,11 +10,7 @@

Middleman Sitemap

More meta pages - + <%= partial 'sitemap_tree.html.erb', :tree => resource_tree %> diff --git a/middleman-core/lib/middleman-core/preview_server.rb b/middleman-core/lib/middleman-core/preview_server.rb index 6e2f19ec..87e3b290 100644 --- a/middleman-core/lib/middleman-core/preview_server.rb +++ b/middleman-core/lib/middleman-core/preview_server.rb @@ -161,7 +161,7 @@ module Middleman rack_app = app.class.to_rack_app # Add in the meta pages application - meta_app = Middleman::MetaPages.new(app.class.inst) + meta_app = Middleman::MetaPages::Application.new(app.class.inst) rack_app.map '/__middleman' do run meta_app end