middleman/lib/middleman/core_extensions/sitemap.rb

188 lines
3.9 KiB
Ruby
Raw Normal View History

2011-11-08 02:58:07 +01:00
require 'find'
2011-11-08 07:34:02 +01:00
module Middleman::CoreExtensions::Sitemap
class << self
def registered(app)
app.set :sitemap, SitemapStore.new(app)
2011-11-08 08:31:43 +01:00
app.before_configuration do
app.sitemap.setup
end
2011-11-08 02:58:07 +01:00
end
2011-11-08 07:34:02 +01:00
alias :included :registered
end
class SitemapStore
2011-11-08 02:58:07 +01:00
def initialize(app)
@app = app
@map = {}
2011-11-08 07:34:02 +01:00
@ignored_paths = false
@generic_paths = false
@proxied_paths = false
2011-11-08 08:31:43 +01:00
end
2011-11-08 07:34:02 +01:00
2011-11-08 08:31:43 +01:00
def setup
@source = File.expand_path(@app.views, @app.root)
2011-11-08 02:58:07 +01:00
build_static_map
2011-11-08 08:31:43 +01:00
2011-11-08 07:34:02 +01:00
@app.on_file_change do |file|
touch_file(file)
end
2011-11-08 08:31:43 +01:00
2011-11-08 07:34:02 +01:00
@app.on_file_delete do |file|
remove_file(file)
2011-11-08 02:58:07 +01:00
end
end
# Check to see if we know about a specific path
def path_exists?(path)
2011-11-08 07:34:02 +01:00
path = path.sub(/^\//, "")
2011-11-08 02:58:07 +01:00
@map.has_key?(path)
end
def path_is_proxy?(path)
2011-11-08 07:34:02 +01:00
path = path.sub(/^\//, "")
2011-11-08 02:58:07 +01:00
return false if !path_exists?(path)
@map[path].is_a?(String)
end
def path_target(path)
2011-11-08 07:34:02 +01:00
path = path.sub(/^\//, "")
2011-11-08 02:58:07 +01:00
@map[path]
end
def set_path(path, target=true)
2011-11-08 07:34:02 +01:00
path = path.sub(/^\//, "")
target = target.sub(/^\//, "") if target.is_a?(String)
2011-11-08 02:58:07 +01:00
@map[path] = target
2011-11-08 07:34:02 +01:00
@ignored_paths = false if target === false
@generic_paths = false if target === true
@proxied_paths = false if target.is_a?(String)
2011-11-08 02:58:07 +01:00
end
def ignore_path(path)
2011-11-08 07:34:02 +01:00
set_path(path, false)
2011-11-08 02:58:07 +01:00
end
def each(&block)
@map.each do |k, v|
yield(k, v)
end
end
2011-11-08 07:34:02 +01:00
def all_paths
@map.keys
end
2011-11-08 08:44:34 +01:00
def all_values
@map.values
end
2011-11-08 07:34:02 +01:00
def ignored_path?(path)
path = path.sub(/^\//, "")
ignored_paths.include?(path)
end
2011-11-08 02:58:07 +01:00
def ignored_paths
@ignored_paths ||= begin
ignored = []
each do |k, v|
2011-11-08 07:34:02 +01:00
ignored << k if v === false
2011-11-08 02:58:07 +01:00
end
ignored
end
end
2011-11-08 07:34:02 +01:00
def generic_path?(path)
path = path.sub(/^\//, "")
generic_paths.include?(path)
end
2011-11-08 02:58:07 +01:00
def generic_paths
@generic_paths ||= begin
generic = []
each do |k, v|
2011-11-08 07:34:02 +01:00
generic << k if v === true
2011-11-08 02:58:07 +01:00
end
generic
end
end
2011-11-08 07:34:02 +01:00
def proxied_path?(path)
path = path.sub(/^\//, "")
proxied_paths.include?(path)
end
2011-11-08 02:58:07 +01:00
def proxied_paths
@proxied_paths ||= begin
proxied = []
each do |k, v|
2011-11-08 07:34:02 +01:00
proxied << k if v.is_a?(String)
2011-11-08 02:58:07 +01:00
end
proxied
end
end
def touch_file(file)
touch_path(file_to_path(file))
end
def touch_path(path)
set_path(path) unless path_exists?(path)
end
def remove_file(file)
remove_path(file_to_path(file))
end
def remove_path(path)
2011-11-08 07:34:02 +01:00
path = path.sub(/^\//, "")
@map.delete(path) if path_exists?(path)
2011-11-08 02:58:07 +01:00
end
protected
def build_static_map
Find.find(@source) do |file|
add_file(file)
end
end
def file_to_path(file)
path = file.sub(@source + "/", "")
end_of_the_line = false
while !end_of_the_line
file_extension = File.extname(path)
# TODO: Loop and continue popping Tilt-aware extensions
if ::Tilt.mappings.has_key?(file_extension.gsub(/^\./, ""))
path = path.sub(file_extension, "")
else
end_of_the_line = true
end
end
path
end
def add_file(file)
return false if file == @source ||
file.match(/\/\./) ||
(file.match(/\/_/) && !file.match(/\/__/)) ||
File.directory?(file)
add_path(file_to_path(file))
end
def add_path(path)
return false if path == "layout" ||
path.match(/^layouts/)
set_path(path)
true
end
end
end