1
0
Fork 0
mirror of https://github.com/middleman/middleman.git synced 2022-11-09 12:20:27 -05:00
middleman--middleman/lib/middleman/core_extensions/sitemap.rb

232 lines
4.7 KiB
Ruby
Raw Normal View History

2011-11-07 20:58:07 -05:00
require 'find'
require 'hooks'
2011-11-07 20:58:07 -05:00
2011-11-08 01:34:02 -05:00
module Middleman::CoreExtensions::Sitemap
class << self
def registered(app)
sm = SitemapStore.new
app.set :sitemap, sm
app.initialized do |scope|
sm.setup(scope)
end
app.file_changed do |file|
sm.touch_file(file)
end
app.file_deleted do |file|
sm.remove_file(file)
end
2011-11-08 18:38:15 -05:00
app.extend ClassMethods
2011-11-07 20:58:07 -05:00
end
2011-11-08 01:34:02 -05:00
alias :included :registered
end
2011-11-08 18:38:15 -05:00
module ClassMethods
# Keep a path from building
def ignore(path)
action = Proc.new { ignore_path(path) }
if sitemap.setup?
action.call
else
sitemap.class.after_setup(&action)
end
end
def reroute(url, target)
action = Proc.new { set_path(url, target) }
if sitemap.setup?
action.call
else
sitemap.class.after_setup(&action)
end
2011-11-08 18:38:15 -05:00
end
end
2011-11-08 01:34:02 -05:00
class SitemapStore
include ::Hooks
define_hook :after_setup
def initialize
2011-11-07 20:58:07 -05:00
@map = {}
2011-11-08 01:34:02 -05:00
@ignored_paths = false
@generic_paths = false
@proxied_paths = false
@is_setup = false
2011-11-08 13:35:27 -05:00
end
def setup(app)
@app = app
2011-11-08 13:35:27 -05:00
@source = File.expand_path(@app.views, @app.root)
2011-11-08 13:35:27 -05:00
build_static_map
run_hook :after_setup
@is_setup = true
end
def setup?
@is_setup
2011-11-07 20:58:07 -05:00
end
# Check to see if we know about a specific path
def path_exists?(path)
2011-11-08 01:34:02 -05:00
path = path.sub(/^\//, "")
2011-11-07 20:58:07 -05:00
@map.has_key?(path)
end
def path_is_proxy?(path)
2011-11-08 01:34:02 -05:00
path = path.sub(/^\//, "")
2011-11-07 20:58:07 -05:00
return false if !path_exists?(path)
@map[path].is_a?(String)
end
def path_target(path)
2011-11-08 01:34:02 -05:00
path = path.sub(/^\//, "")
2011-11-07 20:58:07 -05:00
@map[path]
end
def set_path(path, target=true)
2011-11-08 01:34:02 -05:00
path = path.sub(/^\//, "")
target = target.sub(/^\//, "") if target.is_a?(String)
2011-11-07 20:58:07 -05:00
@map[path] = target
2011-11-08 01:34:02 -05:00
@ignored_paths = false if target === false
@generic_paths = false if target === true
@proxied_paths = false if target.is_a?(String)
2011-11-07 20:58:07 -05:00
end
def ignore_path(path)
2011-11-08 01:34:02 -05:00
set_path(path, false)
2011-11-07 20:58:07 -05:00
end
def each(&block)
@map.each do |k, v|
yield(k, v)
end
end
2011-11-08 01:34:02 -05:00
def all_paths
@map.keys
end
2011-11-08 02:44:34 -05:00
def all_values
@map.values
end
2011-11-08 01:34:02 -05:00
def ignored_path?(path)
path = path.sub(/^\//, "")
ignored_paths.include?(path)
end
2011-11-07 20:58:07 -05:00
def ignored_paths
@ignored_paths ||= begin
ignored = []
each do |k, v|
2011-11-08 01:34:02 -05:00
ignored << k if v === false
2011-11-07 20:58:07 -05:00
end
ignored
end
end
2011-11-08 01:34:02 -05:00
def generic_path?(path)
path = path.sub(/^\//, "")
generic_paths.include?(path)
end
2011-11-07 20:58:07 -05:00
def generic_paths
@generic_paths ||= begin
generic = []
each do |k, v|
2011-11-08 01:34:02 -05:00
generic << k if v === true
2011-11-07 20:58:07 -05:00
end
generic
end
end
2011-11-08 01:34:02 -05:00
def proxied_path?(path)
path = path.sub(/^\//, "")
proxied_paths.include?(path)
end
2011-11-07 20:58:07 -05:00
def proxied_paths
@proxied_paths ||= begin
proxied = []
each do |k, v|
2011-11-08 01:34:02 -05:00
proxied << k if v.is_a?(String)
2011-11-07 20:58:07 -05:00
end
proxied
end
end
def touch_file(file)
2011-11-08 13:35:27 -05:00
add_file(file)
2011-11-07 20:58:07 -05:00
end
def remove_file(file)
2011-11-08 13:35:27 -05:00
path = file_to_path(file)
remove_path(path) if path
2011-11-07 20:58:07 -05:00
end
def remove_path(path)
2011-11-08 01:34:02 -05:00
path = path.sub(/^\//, "")
@map.delete(path) if path_exists?(path)
2011-11-07 20:58:07 -05:00
end
protected
def build_static_map
Find.find(@source) do |file|
add_file(file)
end
end
def file_to_path(file)
2011-11-08 13:35:27 -05:00
@source ||= File.expand_path(@app.views, @app.root)
file = File.expand_path(file, @app.root)
prefix = @source + "/"
return false unless file.include?(prefix)
path = file.sub(prefix, "")
2011-11-07 20:58:07 -05:00
end_of_the_line = false
while !end_of_the_line
file_extension = File.extname(path)
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)
2011-11-08 13:35:27 -05:00
path = file_to_path(file)
add_path(path) if path && !path_exists?(path)
2011-11-07 20:58:07 -05:00
end
def add_path(path)
return false if path == "layout" ||
path.match(/^layouts/)
set_path(path)
true
end
end
end