diff --git a/lib/middleman.rb b/lib/middleman.rb index a4c0ca1b..d3186cef 100755 --- a/lib/middleman.rb +++ b/lib/middleman.rb @@ -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 \ No newline at end of file diff --git a/lib/middleman/core_extensions/features.rb b/lib/middleman/core_extensions/features.rb index e4774e19..1dac0b15 100644 --- a/lib/middleman/core_extensions/features.rb +++ b/lib/middleman/core_extensions/features.rb @@ -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 diff --git a/lib/middleman/core_extensions/routing.rb b/lib/middleman/core_extensions/routing.rb index fb807457..74c7d2e6 100644 --- a/lib/middleman/core_extensions/routing.rb +++ b/lib/middleman/core_extensions/routing.rb @@ -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] diff --git a/lib/middleman/core_extensions/sitemap_tree.rb b/lib/middleman/core_extensions/sitemap_tree.rb new file mode 100644 index 00000000..8d6b99f9 --- /dev/null +++ b/lib/middleman/core_extensions/sitemap_tree.rb @@ -0,0 +1 @@ +# Represent the HTML files in the site as a tree \ No newline at end of file diff --git a/lib/middleman/guard.rb b/lib/middleman/guard.rb index be67a2e1..77c29ce5 100644 --- a/lib/middleman/guard.rb +++ b/lib/middleman/guard.rb @@ -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 \ No newline at end of file +end + +require "middleman/sitemap" \ No newline at end of file diff --git a/lib/middleman/sitemap.rb b/lib/middleman/sitemap.rb new file mode 100644 index 00000000..8593308b --- /dev/null +++ b/lib/middleman/sitemap.rb @@ -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 \ No newline at end of file diff --git a/middleman-x86-mingw32.gemspec b/middleman-x86-mingw32.gemspec index 90faa198..af442b24 100644 --- a/middleman-x86-mingw32.gemspec +++ b/middleman-x86-mingw32.gemspec @@ -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 =< 1.3.5"]) s.add_dependency("thin", ["~> 1.2.11"]) s.add_dependency("thor", ["~> 0.14.0"]) diff --git a/middleman.gemspec b/middleman.gemspec index 279c3c9f..ce49a3af 100644 --- a/middleman.gemspec +++ b/middleman.gemspec @@ -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 =< 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