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