Hierarchical sitemap

This commit is contained in:
Ben Hollis 2012-10-13 14:02:38 -07:00 committed by Thomas Reynolds
parent 22fcc3c108
commit b8eb932a73
4 changed files with 81 additions and 51 deletions

View file

@ -3,12 +3,13 @@ require 'rack/static'
require 'tilt' require 'tilt'
module Middleman module Middleman
module MetaPages
# Metadata pages to be served in preview, in order to present information about the 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. # 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 # 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. # on Sinatra or figure out how to host Middleman inside Middleman.
class MetaPages class Application
def initialize(middleman) def initialize(middleman)
# Hold a reference to the middleman application # Hold a reference to the middleman application
@middleman = middleman @middleman = middleman
@ -39,7 +40,20 @@ module Middleman
# Inspect the sitemap # Inspect the sitemap
def sitemap(env) def sitemap(env)
template('sitemap.html.erb', :resources => @middleman.sitemap.resources(true)) 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
template('sitemap.html.erb', :resource_tree => resource_tree)
end end
private private
@ -47,7 +61,7 @@ module Middleman
# Render a template with the given name and locals # Render a template with the given name and locals
def template(template_name, locals={}) def template(template_name, locals={})
template_path = File.join(File.dirname(__FILE__), 'meta_pages', 'templates', template_name) template_path = File.join(File.dirname(__FILE__), 'meta_pages', 'templates', template_name)
content = Tilt.new(template_path).render(nil, locals) content = Tilt.new(template_path).render(RenderHelper.new, locals)
response(content) response(content)
end end
@ -55,5 +69,13 @@ module Middleman
def response(content) def response(content)
[ 200, {"Content-Type" => "text/html"}, Array(content) ] [ 200, {"Content-Type" => "text/html"}, Array(content) ]
end 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
end end
end end

View file

@ -0,0 +1,12 @@
<ul>
<% tree.each do |path_part, subtree| %>
<li>
<%= path_part %>
<% if subtree.respond_to? :path %>
Path!
<% else %>
<%= partial 'sitemap_tree.html.erb', :tree => subtree %>
<% end %>
</li>
<% end %>
</ul>

View file

@ -10,11 +10,7 @@
<h1>Middleman Sitemap</h1> <h1>Middleman Sitemap</h1>
<a href="../">More meta pages</a> <a href="../">More meta pages</a>
<ul> <%= partial 'sitemap_tree.html.erb', :tree => resource_tree %>
<% resources.each do |resource| %>
<li><%= resource.path %></li>
<% end %>
</ul>
</body> </body>
</html> </html>

View file

@ -161,7 +161,7 @@ module Middleman
rack_app = app.class.to_rack_app rack_app = app.class.to_rack_app
# Add in the meta pages application # 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 rack_app.map '/__middleman' do
run meta_app run meta_app
end end