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

begin core sitemap restructure

This commit is contained in:
Thomas Reynolds 2011-11-07 17:58:07 -08:00
parent f8ed2d60b6
commit 41a5d9bbee
8 changed files with 209 additions and 42 deletions

View file

@ -62,6 +62,7 @@ require "sinatra/base"
module Middleman
# Auto-load modules on-demand
autoload :Base, "middleman/base"
autoload :Sitemap, "middleman/sitemap"
autoload :Builder, "middleman/builder"
autoload :CLI, "middleman/cli"
autoload :Templates, "middleman/templates"
@ -143,9 +144,6 @@ module Middleman
# words, paragraphs, fake images, names and email addresses.
autoload :Lorem, "middleman/features/lorem"
# guard-livereload
autoload :LiveReload, "middleman/features/live_reload"
# Automatically convert filename.html files into filename/index.html
autoload :DirectoryIndexes, "middleman/features/directory_indexes"
end
@ -200,4 +198,4 @@ module Middleman
end
require "middleman/version"
Middleman.load_extensions_in_path
Middleman.load_extensions_in_path

View file

@ -75,6 +75,8 @@ module Middleman::CoreExtensions::Features
# Load features before starting server
def new
set :sitemap, ::Middleman::Sitemap.new(self)
# Check for and evaluate local configuration
local_config = File.join(self.root, "config.rb")
if File.exists? local_config

View file

@ -48,6 +48,9 @@ module Middleman::CoreExtensions::Routing
# Keep a path from building
def ignore(path)
# New sitemap based ignore
settings.sitemap.ignore_path(path)
settings.excluded_paths << paths_for_url(path)
settings.excluded_paths.flatten!
settings.excluded_paths.uniq!
@ -61,6 +64,9 @@ module Middleman::CoreExtensions::Routing
options[:layout] = settings.layout if options[:layout].nil?
if options.has_key?(:proxy)
# New sitemap based proxy
settings.sitemap.set_path(url, options[:proxy])
settings.proxied_paths[url] = options[:proxy]
if options.has_key?(:ignore) && options[:ignore]
settings.ignore(options[:proxy])
@ -73,6 +79,12 @@ module Middleman::CoreExtensions::Routing
paths_for_url(url).each do |p|
get(p) do
# New sitemap based rerouting
if settings.sitemap.path_is_proxy?(url)
request["is_proxy"] = true
request.path_info = settings.sitemap.path_target(url)
end
if settings.proxied_paths.has_key?(url)
request["is_proxy"] = true
request.path_info = settings.proxied_paths[url]

View file

@ -0,0 +1 @@
# Represent the HTML files in the site as a tree

View file

@ -20,7 +20,7 @@ module Middleman
end
guardfile_contents = %Q{
guard 'middleman'#{options_hash} do
guard 'middlemanconfig'#{options_hash} do
watch("config.rb")
watch(%r{^lib/^[^\.](.*)\.rb$})
end
@ -30,14 +30,14 @@ module Middleman
result = block.call(options, livereload)
guardfile_contents << result unless result.nil?
end
::Guard.start({ :guardfile_contents => guardfile_contents })
end
end
end
module Guard
class Middleman < Guard
class MiddlemanConfig < Guard
def initialize(watchers = [], options = {})
super
@options = options
@ -67,4 +67,6 @@ module Guard
# @server_options[:app] = nil
end
end
end
end
require "middleman/sitemap"

185
lib/middleman/sitemap.rb Normal file
View file

@ -0,0 +1,185 @@
require 'find'
module Middleman
class Sitemap
def self.singleton
@@singleton || nil
end
def initialize(app)
@app = app
@map = {}
@ignored_paths = nil
@generic_paths = nil
@proxied_paths = nil
@source = File.expand_path(@app.views, @app.root)
build_static_map
each do |request, destination|
$stderr.puts request
end
@@singleton = self
end
# Check to see if we know about a specific path
def path_exists?(path)
@map.has_key?(path)
end
def path_is_proxy?(path)
return false if !path_exists?(path)
@map[path].is_a?(String)
end
def path_target(path)
@map[path]
end
def set_path(path, target=true)
@map[path] = target
@ignored_paths = nil if target.nil?
@generic_paths = nil if target === true
@proxied_paths = nil if target.is_a?(String)
end
def ignore_path(path)
set_path(path, nil)
end
def each(&block)
@map.each do |k, v|
next if v.nil?
yield(k, v)
end
end
def ignored_paths
@ignored_paths ||= begin
ignored = []
each do |k, v|
ignored << k unless v.nil?
end
ignored
end
end
def generic_paths
@generic_paths ||= begin
generic = []
each do |k, v|
generic << k unless v === true
end
generic
end
end
def proxied_paths
@proxied_paths ||= begin
proxied = []
each do |k, v|
proxied << k unless target.is_a?(String)
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)
if @map.has_key?(path)
@map.delete(path)
end
end
protected
def build_static_map
# found_template = resolve_template(request_path, :raise_exceptions => false)
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
module Guard
class MiddlemanSitemap < Guard
def initialize(watchers = [], options = {})
super
@options = options
end
def run_on_change(files)
files.each do |file|
::Middleman::Sitemap.singleton.touch_file(file)
end
end
def run_on_deletion(files)
files.each do |file|
::Middleman::Sitemap.singleton.remove_file(file)
end
end
end
end
# Add Sitemap guard
Middleman::Guard.add_guard do
%Q{
guard 'middlemansitemap' do
watch(%r{^source/(.*)})
end
}
end

View file

@ -20,23 +20,6 @@ Gem::Specification.new do |s|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]
# TODO remove for 2.1.x
s.post_install_message =<<eos
********************************************************************************
Welcome to Middleman 2.0
In addition to many new features, some backwards-incompatible
changes have been made to the structure of Middleman sites.
Before running you old, v1.x project on Middleman 2.0,
review the Migration guide:
http://middlemanapp.com/guides/migrating
********************************************************************************
eos
s.add_dependency("rack", ["~> 1.3.5"])
s.add_dependency("thin", ["~> 1.2.11"])
s.add_dependency("thor", ["~> 0.14.0"])

View file

@ -20,23 +20,6 @@ Gem::Specification.new do |s|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]
# TODO remove for 2.1.x
s.post_install_message =<<eos
********************************************************************************
Welcome to Middleman 2.0
In addition to many new features, some backwards-incompatible
changes have been made to the structure of Middleman sites.
Before running you old, v1.x project on Middleman 2.0,
review the Migration guide:
http://middlemanapp.com/guides/migrating
********************************************************************************
eos
s.add_dependency("rack", ["~> 1.3.5"])
s.add_dependency("thin", ["~> 1.2.11"])
s.add_dependency("thor", ["~> 0.14.0"])
@ -58,6 +41,7 @@ eos
s.add_dependency("padrino-helpers", ["~> 0.10.5"])
s.add_dependency("guard", ["~> 0.8.8"])
s.add_dependency("eventmachine", ["1.0.0.beta.4"])
# s.add_dependency("middleman-livereload", ["~> 0.2.0"])
# Development and test