sitemap_tree helper

This commit is contained in:
Thomas Reynolds 2011-11-08 00:00:33 -08:00
parent ce841ccd18
commit e8d50ad204
3 changed files with 39 additions and 2 deletions

View file

@ -150,6 +150,9 @@ module Middleman
# Automatically convert filename.html files into filename/index.html
autoload :DirectoryIndexes, "middleman/features/directory_indexes"
# Organize the sitemap as a tree
autoload :SitemapTree, "middleman/features/sitemap_tree"
end
EXTENSION_FILE = File.join("lib", "middleman_init.rb")

View file

@ -78,7 +78,8 @@ module Middleman::Base
app.register Middleman::CoreExtensions::FrontMatter
app.set :default_features, [
:lorem
:lorem,
:sitemap_tree
]
# Default layout name

View file

@ -1 +1,34 @@
# Represent the HTML files in the site as a tree
module Middleman::Features::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 }
app.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