little cleanups

This commit is contained in:
Thomas Reynolds 2011-11-08 10:35:27 -08:00
parent e8d50ad204
commit 19e566f6a5
4 changed files with 35 additions and 43 deletions

View file

@ -79,7 +79,7 @@ module Middleman::Base
app.set :default_features, [ app.set :default_features, [
:lorem, :lorem,
:sitemap_tree #:sitemap_tree
] ]
# Default layout name # Default layout name

View file

@ -63,16 +63,6 @@ module Middleman::CoreExtensions::Features
register feature register feature
end end
# Add a block/proc to be run after features have been setup
def before_configuration(&block)
@run_before_features ||= []
@run_before_features << block
end
def run_before_features
@run_before_features || []
end
# Add a block/proc to be run after features have been setup # Add a block/proc to be run after features have been setup
def after_configuration(&block) def after_configuration(&block)
@run_after_features ||= [] @run_after_features ||= []
@ -85,8 +75,6 @@ module Middleman::CoreExtensions::Features
# Load features before starting server # Load features before starting server
def new def new
run_before_features.each { |block| class_eval(&block) }
# Check for and evaluate local configuration # Check for and evaluate local configuration
local_config = File.join(self.root, "config.rb") local_config = File.join(self.root, "config.rb")
if File.exists? local_config if File.exists? local_config

View file

@ -1,6 +1,9 @@
module Middleman::CoreExtensions::FileWatcher module Middleman::CoreExtensions::FileWatcher
class << self class << self
def registered(app) def registered(app)
app.set :run_after_file_change, []
app.set :run_after_file_delete, []
app.extend ClassMethods app.extend ClassMethods
end end
alias :included :registered alias :included :registered
@ -8,23 +11,19 @@ module Middleman::CoreExtensions::FileWatcher
module ClassMethods module ClassMethods
def file_did_change(path) def file_did_change(path)
@run_after_file_change ||= [] settings.run_after_file_change.each { |block| block.call(path) }
@run_after_file_change.each { |block| block.call(path) }
end end
def on_file_change(&block) def on_file_change(&block)
@run_after_file_change ||= [] settings.run_after_file_change << block
@run_after_file_change << block
end end
def file_did_delete(path) def file_did_delete(path)
@run_after_file_delete ||= [] settings.run_after_file_delete.each { |block| block.call(path) }
@run_after_file_delete.each { |block| block.call(path) }
end end
def on_file_delete(&block) def on_file_delete(&block)
@run_after_file_delete ||= [] settings.run_after_file_delete << block
@run_after_file_delete << block
end end
end end
end end

View file

@ -4,10 +4,6 @@ module Middleman::CoreExtensions::Sitemap
class << self class << self
def registered(app) def registered(app)
app.set :sitemap, SitemapStore.new(app) app.set :sitemap, SitemapStore.new(app)
app.before_configuration do
app.sitemap.setup
end
end end
alias :included :registered alias :included :registered
end end
@ -19,19 +15,24 @@ module Middleman::CoreExtensions::Sitemap
@ignored_paths = false @ignored_paths = false
@generic_paths = false @generic_paths = false
@proxied_paths = false @proxied_paths = false
@app.on_file_change do |file|
touch_file(file)
end
@app.on_file_delete do |file|
remove_file(file)
end
setup
# @app.after_configuration do
# sitemap.setup
# end
end end
def setup def setup
@source = File.expand_path(@app.views, @app.root) @source = File.expand_path(@app.views, @app.root)
build_static_map build_static_map
@app.on_file_change do |file|
touch_file(file)
end
@app.on_file_delete do |file|
remove_file(file)
end
end end
# Check to see if we know about a specific path # Check to see if we know about a specific path
@ -126,15 +127,12 @@ module Middleman::CoreExtensions::Sitemap
end end
def touch_file(file) def touch_file(file)
touch_path(file_to_path(file)) add_file(file)
end
def touch_path(path)
set_path(path) unless path_exists?(path)
end end
def remove_file(file) def remove_file(file)
remove_path(file_to_path(file)) path = file_to_path(file)
remove_path(path) if path
end end
def remove_path(path) def remove_path(path)
@ -150,13 +148,18 @@ module Middleman::CoreExtensions::Sitemap
end end
def file_to_path(file) def file_to_path(file)
path = file.sub(@source + "/", "") @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, "")
end_of_the_line = false end_of_the_line = false
while !end_of_the_line while !end_of_the_line
file_extension = File.extname(path) file_extension = File.extname(path)
# TODO: Loop and continue popping Tilt-aware extensions
if ::Tilt.mappings.has_key?(file_extension.gsub(/^\./, "")) if ::Tilt.mappings.has_key?(file_extension.gsub(/^\./, ""))
path = path.sub(file_extension, "") path = path.sub(file_extension, "")
else else
@ -172,8 +175,10 @@ module Middleman::CoreExtensions::Sitemap
file.match(/\/\./) || file.match(/\/\./) ||
(file.match(/\/_/) && !file.match(/\/__/)) || (file.match(/\/_/) && !file.match(/\/__/)) ||
File.directory?(file) File.directory?(file)
add_path(file_to_path(file)) path = file_to_path(file)
add_path(path) if path && !path_exists?(path)
end end
def add_path(path) def add_path(path)