2011-11-21 02:05:29 +01:00
|
|
|
module Middleman::Sitemap
|
|
|
|
class Store
|
|
|
|
attr_accessor :app
|
|
|
|
|
|
|
|
def initialize(app)
|
|
|
|
@app = app
|
2011-11-24 06:59:53 +01:00
|
|
|
@source = File.expand_path(@app.source, @app.root)
|
2011-11-21 02:05:29 +01:00
|
|
|
@pages = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
# Check to see if we know about a specific path
|
|
|
|
def exists?(path)
|
|
|
|
@pages.has_key?(path.sub(/^\//, ""))
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_context(path, opts={}, blk=nil)
|
|
|
|
page(path) do
|
|
|
|
template.options = opts
|
|
|
|
template.blocks = [blk]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def ignore(path)
|
|
|
|
page(path) { ignore }
|
2011-11-27 05:09:14 +01:00
|
|
|
app.cache.remove(:ignored_paths)
|
2011-11-21 02:05:29 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def proxy(path, target)
|
|
|
|
page(path) { proxy_to(target.sub(%r{^/}, "")) }
|
2011-11-27 05:09:14 +01:00
|
|
|
app.cache.remove(:proxied_paths)
|
2011-11-21 02:05:29 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def page(path, &block)
|
|
|
|
path = path.sub(/^\//, "").gsub("%20", " ")
|
|
|
|
@pages[path] = ::Middleman::Sitemap::Page.new(self, path) unless @pages.has_key?(path)
|
|
|
|
@pages[path].instance_exec(&block) if block_given?
|
|
|
|
@pages[path]
|
|
|
|
end
|
|
|
|
|
2011-11-25 00:04:00 +01:00
|
|
|
def each(&block)
|
|
|
|
@pages.each do |k, v|
|
|
|
|
yield k, v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-11-21 02:05:29 +01:00
|
|
|
def all_paths
|
|
|
|
@pages.keys
|
|
|
|
end
|
|
|
|
|
|
|
|
def ignored?(path)
|
|
|
|
ignored_paths.include?(path.sub(/^\//, ""))
|
|
|
|
end
|
|
|
|
|
|
|
|
def ignored_paths
|
2011-11-27 05:09:14 +01:00
|
|
|
app.cache.fetch :ignored_paths do
|
2011-11-21 02:05:29 +01:00
|
|
|
@pages.values.select(&:ignored?).map(&:path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def generic?(path)
|
|
|
|
generic_paths.include?(path.sub(/^\//, ""))
|
|
|
|
end
|
|
|
|
|
|
|
|
def generic_paths
|
2011-11-27 05:09:14 +01:00
|
|
|
app.cache.fetch :generic_paths do
|
2011-11-21 02:05:29 +01:00
|
|
|
@pages.values.select(&:generic?).map(&:path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def proxied?(path)
|
|
|
|
proxied_paths.include?(path.sub(/^\//, ""))
|
|
|
|
end
|
|
|
|
|
|
|
|
def proxied_paths
|
2011-11-27 05:09:14 +01:00
|
|
|
app.cache.fetch :proxied_paths do
|
2011-11-21 02:05:29 +01:00
|
|
|
@pages.values.select(&:proxy?).map(&:path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_file(file)
|
|
|
|
path = file_to_path(file)
|
|
|
|
return false unless path
|
|
|
|
|
|
|
|
path = path.sub(/^\//, "")
|
|
|
|
@pages.delete(path) if @pages.has_key?(path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def file_to_path(file)
|
|
|
|
file = File.expand_path(file, @app.root)
|
|
|
|
|
|
|
|
prefix = @source + "/"
|
|
|
|
return false unless file.include?(prefix)
|
|
|
|
|
|
|
|
path = file.sub(prefix, "")
|
|
|
|
path = extensionless_path(path)
|
|
|
|
|
|
|
|
path
|
|
|
|
end
|
|
|
|
|
|
|
|
def touch_file(file)
|
|
|
|
return false if file == @source ||
|
|
|
|
file.match(/^\./) ||
|
2011-11-28 05:22:51 +01:00
|
|
|
(file.match(/\/\./) && !file.match(/\/\.htaccess/)) ||
|
2011-11-21 02:05:29 +01:00
|
|
|
(file.match(/\/_/) && !file.match(/\/__/)) ||
|
|
|
|
File.directory?(file)
|
|
|
|
|
|
|
|
path = file_to_path(file)
|
|
|
|
|
|
|
|
return false unless path
|
|
|
|
|
|
|
|
return false if path.match(%r{^layout}) ||
|
|
|
|
path.match(%r{^layouts/})
|
|
|
|
|
|
|
|
# @app.logger.debug :sitemap_update, Time.now, path if @app.logging?
|
|
|
|
|
|
|
|
# Add generic path
|
2011-12-08 06:00:27 +01:00
|
|
|
p = page(path)
|
|
|
|
p.source_file = File.expand_path(file, @app.root)
|
|
|
|
p.touch
|
2011-11-21 02:05:29 +01:00
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
def extensionless_path(file)
|
2011-11-27 05:09:14 +01:00
|
|
|
app.cache.fetch(:extensionless_path, file) do
|
2011-11-21 02:05:29 +01:00
|
|
|
path = file.dup
|
|
|
|
|
|
|
|
end_of_the_line = false
|
|
|
|
while !end_of_the_line
|
|
|
|
if !::Tilt[path].nil?
|
|
|
|
path = path.sub(File.extname(path), "")
|
|
|
|
else
|
|
|
|
end_of_the_line = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-12-27 06:52:16 +01:00
|
|
|
end
|