middleman/middleman-core/lib/middleman-core/core_extensions/data.rb

226 lines
7.0 KiB
Ruby
Raw Normal View History

2014-07-03 04:04:34 +02:00
require 'middleman-core/contracts'
2015-06-17 00:30:37 +02:00
require 'middleman-core/util/data'
module Middleman
module CoreExtensions
# The data extension parses YAML and JSON files in the `data/` directory
# and makes them available to `config.rb`, templates and extensions
class Data < Extension
attr_reader :data_store
define_setting :data_dir, ENV['MM_DATA_DIR'] || 'data', 'The directory data files are stored in'
2016-01-13 01:03:23 +01:00
# Make the internal `data_store` method available as `app.data`
expose_to_application data: :data_store
2015-05-02 23:23:16 +02:00
# Exposes `data` to templates
expose_to_template data: :data_store
2014-07-24 07:03:54 +02:00
# The regex which tells Middleman which files are for data
DATA_FILE_MATCHER = /^(.*?)[\w-]+\.(yml|yaml|json)$/
2014-07-16 03:01:45 +02:00
def initialize(app, config={}, &block)
super
2015-04-26 22:22:58 +02:00
2014-07-24 07:03:54 +02:00
@data_store = DataStore.new(app, DATA_FILE_MATCHER)
2014-07-06 01:44:04 +02:00
2014-07-16 03:01:45 +02:00
start_watching(app.config[:data_dir])
end
def start_watching(dir)
@original_data_dir = dir
# Tell the file watcher to observe the :data_dir
@watcher = app.files.watch :data,
path: File.join(app.root, dir),
2015-02-27 02:08:40 +01:00
only: DATA_FILE_MATCHER
2014-07-06 01:44:04 +02:00
# Setup data files before anything else so they are available when
# parsing config.rb
2014-12-23 23:54:21 +01:00
app.files.on_change(:data, &@data_store.method(:update_files))
2014-07-16 03:01:45 +02:00
end
2014-07-16 03:01:45 +02:00
def after_configuration
return unless @original_data_dir != app.config[:data_dir]
2014-03-26 00:54:16 +01:00
2014-07-16 03:01:45 +02:00
@watcher.update_path(app.config[:data_dir])
end
# The core logic behind the data extension.
class DataStore
2014-07-03 04:04:34 +02:00
include Contracts
# Setup data store
#
# @param [Middleman::Application] app The current instance of Middleman
2014-07-16 03:01:45 +02:00
def initialize(app, data_file_matcher)
2014-07-03 04:04:34 +02:00
@app = app
2014-07-16 03:01:45 +02:00
@data_file_matcher = data_file_matcher
2014-07-03 04:04:34 +02:00
@local_data = {}
2016-01-22 23:25:02 +01:00
@local_data_enhanced = nil
2014-07-03 04:04:34 +02:00
@local_sources = {}
@callback_sources = {}
end
# Store static data hash
#
# @param [Symbol] name Name of the data, used for namespacing
# @param [Hash] content The content for this data
2013-03-22 10:14:10 +01:00
# @return [Hash]
Contract Symbol, Or[Hash, Array] => Hash
def store(name=nil, content=nil)
@local_sources[name.to_s] = content unless name.nil? || content.nil?
@local_sources
end
2011-11-21 06:21:19 +01:00
# Store callback-based data
#
# @param [Symbol] name Name of the data, used for namespacing
# @param [Proc] proc The callback which will return data
2013-03-22 10:14:10 +01:00
# @return [Hash]
2015-02-24 20:06:28 +01:00
Contract Maybe[Symbol], Maybe[Proc] => Hash
def callbacks(name=nil, proc=nil)
@callback_sources[name.to_s] = proc unless name.nil? || proc.nil?
@callback_sources
end
2014-07-16 03:01:45 +02:00
Contract ArrayOf[IsA['Middleman::SourceFile']], ArrayOf[IsA['Middleman::SourceFile']] => Any
def update_files(updated_files, removed_files)
updated_files.each(&method(:touch_file))
removed_files.each(&method(:remove_file))
@app.sitemap.rebuild_resource_list!(:touched_data_file)
2014-07-16 03:01:45 +02:00
end
# Update the internal cache for a given file path
#
# @param [String] file The file to be re-parsed
# @return [void]
2014-07-16 03:01:45 +02:00
Contract IsA['Middleman::SourceFile'] => Any
def touch_file(file)
2015-11-29 04:32:45 +01:00
data_path = file[:relative_path]
2014-07-16 03:01:45 +02:00
extension = File.extname(data_path)
basename = File.basename(data_path, extension)
2013-03-23 09:13:05 +01:00
return unless %w(.yaml .yml .json).include?(extension)
if %w(.yaml .yml).include?(extension)
2016-01-20 20:50:25 +01:00
data, postscript = ::Middleman::Util::Data.parse(file, @app.config[:frontmatter_delims], :yaml)
2015-06-17 00:30:37 +02:00
data[:postscript] = postscript if !postscript.nil? && data.is_a?(Hash)
elsif extension == '.json'
2016-01-20 20:50:25 +01:00
data, _postscript = ::Middleman::Util::Data.parse(file, @app.config[:frontmatter_delims], :json)
end
2011-11-10 06:19:11 +01:00
2013-03-23 09:13:05 +01:00
data_branch = @local_data
path = data_path.to_s.split(File::SEPARATOR)[0..-2]
path.each do |dir|
2014-07-14 22:19:34 +02:00
data_branch[dir] ||= {}
2013-03-23 09:13:05 +01:00
data_branch = data_branch[dir]
end
2014-07-14 22:19:34 +02:00
data_branch[basename] = data
2016-01-22 23:25:02 +01:00
@local_data_enhanced = nil
end
# Remove a given file from the internal cache
#
# @param [String] file The file to be cleared
# @return [void]
2014-07-16 03:01:45 +02:00
Contract IsA['Middleman::SourceFile'] => Any
def remove_file(file)
2015-11-29 04:32:45 +01:00
data_path = file[:relative_path]
2014-07-16 03:01:45 +02:00
extension = File.extname(data_path)
basename = File.basename(data_path, extension)
2013-03-23 09:13:05 +01:00
data_branch = @local_data
path = data_path.to_s.split(File::SEPARATOR)[0..-2]
path.each do |dir|
data_branch = data_branch[dir]
end
2014-04-29 19:50:21 +02:00
data_branch.delete(basename) if data_branch.key?(basename)
2016-01-22 23:25:02 +01:00
@local_data_enhanced = nil
end
2013-03-22 10:14:10 +01:00
# Get a hash from either internal static data or a callback
#
# @param [String, Symbol] path The name of the data namespace
# @return [Hash, nil]
Contract Or[String, Symbol] => Maybe[Or[Array, IsA['Middleman::Util::EnhancedHash']]]
def data_for_path(path)
2014-07-14 22:19:34 +02:00
response = if store.key?(path.to_s)
store[path.to_s]
2014-04-29 19:50:21 +02:00
elsif callbacks.key?(path.to_s)
2014-07-14 22:19:34 +02:00
callbacks[path.to_s].call
end
2016-01-22 23:25:02 +01:00
::Middleman::Util.recursively_enhance(response)
end
# "Magically" find namespaces of data if they exist
#
# @param [String] path The namespace to search for
# @return [Hash, nil]
def method_missing(path)
2014-04-29 19:50:21 +02:00
if @local_data.key?(path.to_s)
2015-03-20 21:58:23 +01:00
# Any way to cache this?
2016-01-22 23:25:02 +01:00
@local_data_enhanced ||= ::Middleman::Util.recursively_enhance(@local_data)
return @local_data_enhanced[path.to_s]
else
result = data_for_path(path)
2014-07-14 22:19:34 +02:00
return result if result
end
super
end
# Needed so that method_missing makes sense
2014-04-29 19:50:21 +02:00
def respond_to?(method, include_private=false)
super || key?(method)
end
2013-12-28 19:14:15 +01:00
# Make DataStore act like a hash. Return requested data, or
# nil if data does not exist
#
# @param [String, Symbol] key The name of the data namespace
# @return [Hash, nil]
def [](key)
2014-04-29 19:50:21 +02:00
__send__(key) if key?(key)
end
2014-04-29 19:44:24 +02:00
def key?(key)
2015-02-07 22:38:29 +01:00
(@local_data.keys + @local_sources.keys + @callback_sources.keys).include?(key.to_s)
end
2016-01-14 20:21:42 +01:00
alias has_key? key?
2014-04-29 01:02:18 +02:00
# Convert all the data into a static hash
#
# @return [Hash]
2015-04-24 19:26:42 +02:00
Contract Hash
def to_h
data = {}
2016-01-23 00:57:07 +01:00
store.each_key do |k|
data[k] = data_for_path(k)
end
2016-01-23 00:57:07 +01:00
callbacks.each_key do |k|
data[k] = data_for_path(k)
end
(@local_data || {}).each do |k, v|
data[k] = v
end
data
end
end
end
2011-04-15 00:35:41 +02:00
end
end