sitemap tree traversal. closes #207

This commit is contained in:
Thomas Reynolds 2011-12-23 12:04:38 -08:00
parent 82cd984a29
commit 488fa579f9
17 changed files with 133 additions and 47 deletions

View file

@ -0,0 +1,56 @@
Feature: Step through sitemap as a tree
Scenario: Root
Given the Server is running at "traversal-app"
When I go to "/index.html"
Then I should not see "Parent: index.html"
Then I should see "Child: sub/index.html"
Then I should see "Child: root.html"
Then I should not see "Child: proxied.html"
Scenario: Directories have children and a parent
Given the Server is running at "traversal-app"
When I go to "/sub/index.html"
Then I should see "Parent: index.html"
Then I should see "Child: sub/fake.html"
Then I should see "Child: sub/fake2.html"
Then I should see "Child: sub/sibling.html"
Then I should see "Child: sub/sibling2.html"
Then I should see "Child: sub/sub2/index.html"
Then I should see "Sibling: root.html"
Scenario: Page has siblings
Given the Server is running at "traversal-app"
When I go to "/sub/sibling.html"
Then I should see "Sibling: sub/fake.html"
Then I should see "Sibling: sub/fake2.html"
Then I should see "Sibling: sub/sibling2.html"
Then I should see "Sibling: sub/sub2/index.html"
Scenario: Proxied page has siblings
Given the Server is running at "traversal-app"
When I go to "/sub/fake.html"
Then I should see "Sibling: sub/fake2.html"
Then I should see "Sibling: sub/sibling.html"
Then I should see "Sibling: sub/sibling2.html"
Then I should see "Sibling: sub/sub2/index.html"
Scenario: Page has parent
Given the Server is running at "traversal-app"
When I go to "/sub/sibling.html"
Then I should see "Parent: sub/index.html"
Scenario: Proxied page has parent
Given the Server is running at "traversal-app"
When I go to "/sub/fake.html"
Then I should see "Parent: sub/index.html"
Scenario: Page has source_file
Given the Server is running at "traversal-app"
When I go to "/sub/sibling.html"
Then I should see "Source: source/sub/sibling.html.erb"
Scenario: Proxied page has source_file
Given the Server is running at "traversal-app"
When I go to "/sub/fake.html"
Then I should see "Source: source/proxied.html.erb"

View file

@ -0,0 +1,2 @@
page "/sub/fake.html", :proxy => "/proxied.html", :ignore => true
page "/sub/fake2.html", :proxy => "/proxied.html", :ignore => true

View file

@ -0,0 +1,13 @@
Source: <%= current_page.source_file.sub(root + "/", "") %>
<% if current_page.parent %>
Parent: <%= current_page.parent.path %>
<% end %>
<% current_page.children.each do |p| %>
Child: <%= p.path %>
<% end %>
<% current_page.siblings.each do |p| %>
Sibling: <%= p.path %>
<% end %>

View file

@ -111,9 +111,6 @@ module Middleman
# Automatically convert filename.html files into filename/index.html
autoload :DirectoryIndexes, "middleman/extensions/directory_indexes"
# Organize the sitemap as a tree
autoload :SitemapTree, "middleman/extensions/sitemap_tree"
class << self
def registered
@_registered ||= {}

View file

@ -192,8 +192,7 @@ class Middleman::Base
# Automatically loaded extensions
# @return [Array<Symbol>]
set :default_extensions, [
:lorem,
# :sitemap_tree
:lorem
]
# Default layout name
@ -256,8 +255,6 @@ class Middleman::Base
Middleman::Extensions::MinifyJavascript }
Middleman::Extensions.register(:relative_assets) {
Middleman::Extensions::RelativeAssets }
Middleman::Extensions.register(:sitemap_tree) {
Middleman::Extensions::SitemapTree }
# Backwards-compatibility with old request.path signature
attr :request

View file

@ -26,6 +26,10 @@ module Middleman::CoreExtensions::Sitemap
@sitemap ||= ::Middleman::Sitemap::Store.new(self)
end
def current_page
sitemap.page(current_path)
end
# Keep a path from building
def ignore(path)
sitemap.ignore(path)

View file

@ -1,38 +0,0 @@
module Middleman::Extensions
module SitemapTree
class << self
def registered(app)
app.helpers Helpers
end
alias :included :registered
end
module Helpers
def sitemap_tree(regex=nil)
@sitemap_tree_cache = {}
key = regex.nil? ? "all" : regex
if !@sitemap_tree_cache.has_key?(key)
auto_hash = Hash.new{ |h,k| h[k] = Hash.new &h.default_proc }
sitemap.all_paths.each do |path|
next if !regex.nil? && !path.match(regex)
sub = auto_hash
path.split( "/" ).each{ |dir| sub[dir]; sub = sub[dir] }
end
@sitemap_tree_cache[key] = auto_hash
end
@sitemap_tree_cache[key]
end
def html_sitemap
sitemap_tree(/\.html$/)
end
end
end
register :sitemap_tree, SitemapTree
end

View file

@ -75,7 +75,7 @@ module Guard
puts "== The Middleman is shutting down"
if ::Middleman::JRUBY
else
Process.kill(self.class.kill_command, @server_job)
Process.kill(::Middleman::WINDOWS ? :KILL : :TERM, @server_job)
Process.wait @server_job
@server_job = nil
end
@ -133,5 +133,5 @@ end
# Trap the interupt signal and shut down Guard (and thus the server) smoothly
trap(::Guard::Middleman.kill_command) do
::Guard.stop
# exit!(0)
exit!(0)
end

View file

@ -99,6 +99,61 @@ module Middleman::Sitemap
end
end
def directory_index?
path.include?(app.index_file) || path =~ /\/$/
end
def parent
parts = path.split("/")
if path.include?(app.index_file)
parts.pop
else
end
return nil if parts.length < 1
parts.pop
parts.push(app.index_file)
parent_path = "/" + parts.join("/")
if store.exists?(parent_path)
store.page(parent_path)
else
nil
end
end
def children
return [] unless directory_index?
base_path = path.sub("#{app.index_file}", "")
prefix = /^#{base_path.sub("/", "\\/")}/
store.all_paths.select do |sub_path|
sub_path =~ prefix
end.select do |sub_path|
path != sub_path
end.select do |sub_path|
relative_path = sub_path.sub(prefix, "")
parts = relative_path.split("/")
if parts.length == 1
true
elsif parts.length == 2
parts.last == app.index_file
else
false
end
end.map do |p|
store.page(p)
end.reject { |p| p.ignored? }
end
def siblings
return [] unless parent
parent.children.reject { |p| p == self }
end
protected
def app
@store.app