Switch to using view classes isntead of partials
This commit is contained in:
parent
b8eb932a73
commit
142abe027e
6 changed files with 71 additions and 30 deletions
|
@ -1,6 +1,7 @@
|
|||
require 'rack/builder'
|
||||
require 'rack/static'
|
||||
require 'tilt'
|
||||
require 'middleman-core/meta_pages/sitemap_tree'
|
||||
|
||||
module Middleman
|
||||
module MetaPages
|
||||
|
@ -42,18 +43,13 @@ module Middleman
|
|||
def sitemap(env)
|
||||
resources = @middleman.sitemap.resources(true)
|
||||
|
||||
resource_tree = {}
|
||||
sitemap_tree = SitemapTree.new
|
||||
|
||||
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
|
||||
sitemap_tree.add_resource resource
|
||||
end
|
||||
|
||||
template('sitemap.html.erb', :resource_tree => resource_tree)
|
||||
template('sitemap.html.erb', :sitemap_tree => sitemap_tree)
|
||||
end
|
||||
|
||||
private
|
||||
|
@ -61,7 +57,7 @@ module Middleman
|
|||
# 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)
|
||||
content = Tilt.new(template_path).render(nil, locals)
|
||||
response(content)
|
||||
end
|
||||
|
||||
|
@ -69,13 +65,6 @@ module Middleman
|
|||
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
|
||||
end
|
||||
end
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
alert("foo");
|
|
@ -0,0 +1,14 @@
|
|||
module Middleman
|
||||
module MetaPages
|
||||
# View class for a sitemap resource
|
||||
class SitemapResource
|
||||
def initialize(resource)
|
||||
@resource = resource
|
||||
end
|
||||
|
||||
def render
|
||||
"<p>#{@resource.destination_path}</p>"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
48
middleman-core/lib/middleman-core/meta_pages/sitemap_tree.rb
Normal file
48
middleman-core/lib/middleman-core/meta_pages/sitemap_tree.rb
Normal file
|
@ -0,0 +1,48 @@
|
|||
require 'middleman-core/meta_pages/sitemap_resource'
|
||||
|
||||
module Middleman
|
||||
module MetaPages
|
||||
# View class for a sitemap tree
|
||||
class SitemapTree
|
||||
def initialize
|
||||
@children = {}
|
||||
end
|
||||
|
||||
def add_resource(resource)
|
||||
add_path(resource.path.split('/'), resource)
|
||||
end
|
||||
|
||||
def render
|
||||
content = ""
|
||||
@children.keys.sort_by(&:downcase).each do |path_part|
|
||||
content << "<details>"
|
||||
content << "<summary>"
|
||||
content << path_part
|
||||
content << "</summary>"
|
||||
|
||||
subtree = @children[path_part]
|
||||
content << subtree.render
|
||||
content << "</details>"
|
||||
end
|
||||
content
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def add_path(path_parts, resource)
|
||||
first_part = path_parts.first
|
||||
|
||||
if path_parts.size == 1
|
||||
@children[first_part] = SitemapResource.new(resource)
|
||||
else
|
||||
@children[first_part] ||= SitemapTree.new
|
||||
@children[first_part].add_path(path_parts[1..-1], resource)
|
||||
end
|
||||
end
|
||||
|
||||
def to_s
|
||||
"Sitemap Tree"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,12 +0,0 @@
|
|||
<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>
|
|
@ -4,13 +4,14 @@
|
|||
<meta charset="utf-8">
|
||||
|
||||
<title>Middleman Sitemap</title>
|
||||
<script src="../assets/sitemap.js"></script>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>Middleman Sitemap</h1>
|
||||
<a href="../">More meta pages</a>
|
||||
|
||||
<%= partial 'sitemap_tree.html.erb', :tree => resource_tree %>
|
||||
<%= sitemap_tree.render %>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
|
|
Loading…
Reference in a new issue