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

214 lines
4.3 KiB
Ruby
Raw Normal View History

2011-11-07 20:58:07 -05:00
require 'find'
2011-11-08 01:34:02 -05:00
module Middleman::CoreExtensions::Sitemap
class << self
def registered(app)
2011-11-10 00:19:11 -05:00
app.extend ClassMethods
app.helpers Helpers
app.file_changed do |file|
2011-11-10 00:19:11 -05:00
sitemap.touch_file(file)
end
app.file_deleted do |file|
2011-11-10 00:19:11 -05:00
sitemap.remove_file(file)
end
2011-11-07 20:58:07 -05:00
end
2011-11-08 01:34:02 -05:00
alias :included :registered
end
2011-11-10 00:19:11 -05:00
module Helpers
def sitemap
self.class.sitemap
end
end
2011-11-08 18:38:15 -05:00
module ClassMethods
2011-11-10 00:19:11 -05:00
def sitemap
@sitemap ||= SitemapStore.new(self)
end
2011-11-08 18:38:15 -05:00
# Keep a path from building
def ignore(path)
2011-11-10 00:19:11 -05:00
sitemap.ignore_path(path)
end
def reroute(url, target)
2011-11-10 00:19:11 -05:00
sitemap.set_path(url, target)
2011-11-08 18:38:15 -05:00
end
end
2011-11-08 01:34:02 -05:00
class SitemapStore
2011-11-10 00:19:11 -05:00
def initialize(app)
@app = app
@source = File.expand_path(@app.views, @app.root)
2011-11-07 20:58:07 -05:00
@map = {}
2011-11-11 15:27:09 -05:00
@source_map = {}
2011-11-08 01:34:02 -05:00
@ignored_paths = false
@generic_paths = false
@proxied_paths = false
2011-11-08 13:35:27 -05:00
build_static_map
end
2011-11-07 20:58:07 -05:00
# 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
2011-11-11 15:27:09 -05:00
def source_map
@source_map
end
2011-11-07 20:58:07 -05:00
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
2011-11-11 15:27:09 -05:00
@source_map[path] = file
2011-11-07 20:58:07 -05:00
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