sitemap tree traversal. closes #207
This commit is contained in:
parent
82cd984a29
commit
488fa579f9
56
features/sitemap_traversal.features
Normal file
56
features/sitemap_traversal.features
Normal 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"
|
2
fixtures/traversal-app/config.rb
Normal file
2
fixtures/traversal-app/config.rb
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
page "/sub/fake.html", :proxy => "/proxied.html", :ignore => true
|
||||||
|
page "/sub/fake2.html", :proxy => "/proxied.html", :ignore => true
|
0
fixtures/traversal-app/source/index.html.erb
Normal file
0
fixtures/traversal-app/source/index.html.erb
Normal file
13
fixtures/traversal-app/source/layout.erb
Normal file
13
fixtures/traversal-app/source/layout.erb
Normal 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 %>
|
0
fixtures/traversal-app/source/proxied.html.erb
Normal file
0
fixtures/traversal-app/source/proxied.html.erb
Normal file
0
fixtures/traversal-app/source/root.html.erb
Normal file
0
fixtures/traversal-app/source/root.html.erb
Normal file
0
fixtures/traversal-app/source/sub/index.html.erb
Normal file
0
fixtures/traversal-app/source/sub/index.html.erb
Normal file
0
fixtures/traversal-app/source/sub/sibling.html.erb
Normal file
0
fixtures/traversal-app/source/sub/sibling.html.erb
Normal file
0
fixtures/traversal-app/source/sub/sibling2.html.erb
Normal file
0
fixtures/traversal-app/source/sub/sibling2.html.erb
Normal file
|
@ -111,9 +111,6 @@ module Middleman
|
||||||
# Automatically convert filename.html files into filename/index.html
|
# Automatically convert filename.html files into filename/index.html
|
||||||
autoload :DirectoryIndexes, "middleman/extensions/directory_indexes"
|
autoload :DirectoryIndexes, "middleman/extensions/directory_indexes"
|
||||||
|
|
||||||
# Organize the sitemap as a tree
|
|
||||||
autoload :SitemapTree, "middleman/extensions/sitemap_tree"
|
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
def registered
|
def registered
|
||||||
@_registered ||= {}
|
@_registered ||= {}
|
||||||
|
|
|
@ -192,8 +192,7 @@ class Middleman::Base
|
||||||
# Automatically loaded extensions
|
# Automatically loaded extensions
|
||||||
# @return [Array<Symbol>]
|
# @return [Array<Symbol>]
|
||||||
set :default_extensions, [
|
set :default_extensions, [
|
||||||
:lorem,
|
:lorem
|
||||||
# :sitemap_tree
|
|
||||||
]
|
]
|
||||||
|
|
||||||
# Default layout name
|
# Default layout name
|
||||||
|
@ -256,8 +255,6 @@ class Middleman::Base
|
||||||
Middleman::Extensions::MinifyJavascript }
|
Middleman::Extensions::MinifyJavascript }
|
||||||
Middleman::Extensions.register(:relative_assets) {
|
Middleman::Extensions.register(:relative_assets) {
|
||||||
Middleman::Extensions::RelativeAssets }
|
Middleman::Extensions::RelativeAssets }
|
||||||
Middleman::Extensions.register(:sitemap_tree) {
|
|
||||||
Middleman::Extensions::SitemapTree }
|
|
||||||
|
|
||||||
# Backwards-compatibility with old request.path signature
|
# Backwards-compatibility with old request.path signature
|
||||||
attr :request
|
attr :request
|
||||||
|
|
|
@ -26,6 +26,10 @@ module Middleman::CoreExtensions::Sitemap
|
||||||
@sitemap ||= ::Middleman::Sitemap::Store.new(self)
|
@sitemap ||= ::Middleman::Sitemap::Store.new(self)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def current_page
|
||||||
|
sitemap.page(current_path)
|
||||||
|
end
|
||||||
|
|
||||||
# Keep a path from building
|
# Keep a path from building
|
||||||
def ignore(path)
|
def ignore(path)
|
||||||
sitemap.ignore(path)
|
sitemap.ignore(path)
|
||||||
|
|
|
@ -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
|
|
|
@ -75,7 +75,7 @@ module Guard
|
||||||
puts "== The Middleman is shutting down"
|
puts "== The Middleman is shutting down"
|
||||||
if ::Middleman::JRUBY
|
if ::Middleman::JRUBY
|
||||||
else
|
else
|
||||||
Process.kill(self.class.kill_command, @server_job)
|
Process.kill(::Middleman::WINDOWS ? :KILL : :TERM, @server_job)
|
||||||
Process.wait @server_job
|
Process.wait @server_job
|
||||||
@server_job = nil
|
@server_job = nil
|
||||||
end
|
end
|
||||||
|
@ -133,5 +133,5 @@ end
|
||||||
# Trap the interupt signal and shut down Guard (and thus the server) smoothly
|
# Trap the interupt signal and shut down Guard (and thus the server) smoothly
|
||||||
trap(::Guard::Middleman.kill_command) do
|
trap(::Guard::Middleman.kill_command) do
|
||||||
::Guard.stop
|
::Guard.stop
|
||||||
# exit!(0)
|
exit!(0)
|
||||||
end
|
end
|
|
@ -99,6 +99,61 @@ module Middleman::Sitemap
|
||||||
end
|
end
|
||||||
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
|
protected
|
||||||
def app
|
def app
|
||||||
@store.app
|
@store.app
|
||||||
|
|
Loading…
Reference in a new issue