file watchers can match regex against path
This commit is contained in:
parent
34d3522d93
commit
ffb7611acc
|
@ -9,16 +9,12 @@ module Middleman::CoreExtensions::Data
|
|||
app.extend ClassMethods
|
||||
app.helpers Helpers
|
||||
|
||||
app.file_changed do |file|
|
||||
if file.match(%r{^#{settings.data_dir}\/[\w-]+\.(yml|yaml|json)})
|
||||
data.touch_file(file)
|
||||
end
|
||||
app.file_changed DataStore.matcher do |file|
|
||||
data.touch_file(file) if file.match(%r{^#{settings.data_dir}\/})
|
||||
end
|
||||
|
||||
app.file_deleted do |file|
|
||||
if file.match(%r{^#{settings.data_dir}\/[\w-]+\.(yml|yaml|json)})
|
||||
data.remove_file(file)
|
||||
end
|
||||
app.file_deleted DataStore.matcher do |file|
|
||||
data.remove_file(file) if file.match(%r{^#{settings.data_dir}\/})
|
||||
end
|
||||
end
|
||||
alias :included :registered
|
||||
|
@ -26,11 +22,15 @@ module Middleman::CoreExtensions::Data
|
|||
|
||||
module Helpers
|
||||
def data
|
||||
self.class.data
|
||||
settings.data
|
||||
end
|
||||
end
|
||||
|
||||
class DataObject
|
||||
class DataStore
|
||||
def self.matcher
|
||||
%r{[\w-]+\.(yml|yaml|json)$}
|
||||
end
|
||||
|
||||
def initialize(app)
|
||||
@app = app
|
||||
@local_data = {}
|
||||
|
@ -144,17 +144,17 @@ module Middleman::CoreExtensions::Data
|
|||
|
||||
module ClassMethods
|
||||
def data
|
||||
@data ||= DataObject.new(self)
|
||||
@data ||= DataStore.new(self)
|
||||
end
|
||||
|
||||
# Makes a hash available on the data var with a given name
|
||||
def data_content(name, content)
|
||||
DataObject.data_content(name, content)
|
||||
DataStore.data_content(name, content)
|
||||
end
|
||||
|
||||
# Makes a hash available on the data var with a given name
|
||||
def data_callback(name, &block)
|
||||
DataObject.data_callback(name, block)
|
||||
DataStore.data_callback(name, block)
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,21 +1,39 @@
|
|||
module Middleman::CoreExtensions::FileWatcher
|
||||
class << self
|
||||
def registered(app)
|
||||
app.define_hook :file_changed
|
||||
app.define_hook :file_deleted
|
||||
|
||||
app.extend ClassMethods
|
||||
app.send :include, InstanceMethods
|
||||
end
|
||||
alias :included :registered
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
def file_changed(matcher=nil, &block)
|
||||
@_file_changed ||= []
|
||||
@_file_changed << [block, matcher] if block_given?
|
||||
@_file_changed
|
||||
end
|
||||
|
||||
def file_deleted(matcher=nil, &block)
|
||||
@_file_deleted ||= []
|
||||
@_file_deleted << [block, matcher] if block_given?
|
||||
@_file_deleted
|
||||
end
|
||||
end
|
||||
|
||||
module InstanceMethods
|
||||
def file_did_change(path)
|
||||
run_hook :file_changed, path
|
||||
settings.file_changed.each do |callback, matcher|
|
||||
next unless matcher.nil? || path.match(matcher)
|
||||
settings.instance_exec(path, &callback)
|
||||
end
|
||||
end
|
||||
|
||||
def file_did_delete(path)
|
||||
run_hook :file_deleted, path
|
||||
settings.file_deleted.each do |callback, matcher|
|
||||
next unless matcher.nil? || path.match(matcher)
|
||||
settings.instance_exec(path, &callback)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -7,7 +7,7 @@ module Middleman::CoreExtensions::FrontMatter
|
|||
app.extend ClassMethods
|
||||
app.send :include, InstanceMethods
|
||||
|
||||
app.file_changed do |file|
|
||||
app.file_changed FrontMatter.matcher do |file|
|
||||
frontmatter.touch_file(file)
|
||||
end
|
||||
|
||||
|
@ -47,7 +47,7 @@ module Middleman::CoreExtensions::FrontMatter
|
|||
|
||||
module ClassMethods
|
||||
def frontmatter
|
||||
@frontmatter ||= FrontmatterData.new(self)
|
||||
@frontmatter ||= FrontMatter.new(self)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -57,21 +57,22 @@ module Middleman::CoreExtensions::FrontMatter
|
|||
end
|
||||
end
|
||||
|
||||
class FrontmatterData
|
||||
class FrontMatter
|
||||
def self.matcher
|
||||
%r{source/.*\.html}
|
||||
end
|
||||
|
||||
def initialize(app)
|
||||
@app = app
|
||||
@source ||= File.expand_path(@app.views, @app.root)
|
||||
@local_data = {}
|
||||
|
||||
views_dir = @app.views
|
||||
views_dir = File.join(@app.root, @app.views) unless views_dir.include?(@app.root)
|
||||
|
||||
Dir[File.join(views_dir, "**/*")].each do |file|
|
||||
Dir[File.join(@source, "**/*")].each do |file|
|
||||
next if file.match(/\/\./) ||
|
||||
(file.match(/\/_/) && !file.match(/\/__/)) ||
|
||||
File.directory?(file)
|
||||
!file.match(self.class.matcher)
|
||||
|
||||
touch_file(file)
|
||||
touch_file(file.sub(@app.root, "").sub(/^\//, ""))
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -83,6 +84,7 @@ module Middleman::CoreExtensions::FrontMatter
|
|||
extension = File.extname(file).sub(/\./, "")
|
||||
return unless ::Tilt.mappings.has_key?(extension)
|
||||
|
||||
file = File.expand_path(file, @app.root)
|
||||
content = File.read(file)
|
||||
file = file.sub(@source, "")
|
||||
|
||||
|
@ -97,6 +99,7 @@ module Middleman::CoreExtensions::FrontMatter
|
|||
end
|
||||
|
||||
def remove_file(file)
|
||||
file = File.expand_path(file, @app.root)
|
||||
file = file.sub(@source, "")
|
||||
@app.logger.debug :frontmatter_remove, Time.now, file if @app.settings.logging?
|
||||
|
||||
|
|
Loading…
Reference in a new issue