Convert external data to a real extension
This commit is contained in:
parent
e649bc2809
commit
fed95f9c5e
3 changed files with 36 additions and 47 deletions
|
@ -142,9 +142,6 @@ module Middleman
|
||||||
# Handle exceptions
|
# Handle exceptions
|
||||||
include Middleman::CoreExtensions::ShowExceptions
|
include Middleman::CoreExtensions::ShowExceptions
|
||||||
|
|
||||||
# Activate Data package
|
|
||||||
include Middleman::CoreExtensions::Data
|
|
||||||
|
|
||||||
# Setup custom rendering
|
# Setup custom rendering
|
||||||
include Middleman::CoreExtensions::Rendering
|
include Middleman::CoreExtensions::Rendering
|
||||||
|
|
||||||
|
@ -188,6 +185,7 @@ module Middleman
|
||||||
# Parse YAML from templates. Must be before sitemap so sitemap
|
# Parse YAML from templates. Must be before sitemap so sitemap
|
||||||
# extensions see updated frontmatter!
|
# extensions see updated frontmatter!
|
||||||
activate :front_matter
|
activate :front_matter
|
||||||
|
activate :data
|
||||||
activate :file_watcher
|
activate :file_watcher
|
||||||
|
|
||||||
# Initialize the Sitemap
|
# Initialize the Sitemap
|
||||||
|
|
|
@ -6,7 +6,10 @@ end
|
||||||
|
|
||||||
# Data looks at the data/ folder for YAML files and makes them available
|
# Data looks at the data/ folder for YAML files and makes them available
|
||||||
# to dynamic requests.
|
# to dynamic requests.
|
||||||
require 'middleman-core/core_extensions/data'
|
Middleman::Extensions.register :data do
|
||||||
|
require 'middleman-core/core_extensions/data'
|
||||||
|
Middleman::CoreExtensions::Data
|
||||||
|
end
|
||||||
|
|
||||||
# Parse YAML from templates
|
# Parse YAML from templates
|
||||||
Middleman::Extensions.register :front_matter do
|
Middleman::Extensions.register :front_matter do
|
||||||
|
|
|
@ -1,37 +1,35 @@
|
||||||
|
require 'yaml'
|
||||||
|
require 'active_support/json'
|
||||||
|
|
||||||
module Middleman
|
module Middleman
|
||||||
module CoreExtensions
|
module CoreExtensions
|
||||||
# The data extension parses YAML and JSON files in the data/ directory
|
# The data extension parses YAML and JSON files in the `data/` directory
|
||||||
# and makes them available to config.rb, templates and extensions
|
# and makes them available to `config.rb`, templates and extensions
|
||||||
module Data
|
class Data < Extension
|
||||||
# Extension registered
|
# The regex which tells Middleman which files are for data
|
||||||
class << self
|
MATCHER = /[\w-]+\.(yml|yaml|json)$/
|
||||||
# @private
|
|
||||||
def included(app)
|
|
||||||
# Data formats
|
|
||||||
require 'yaml'
|
|
||||||
require 'active_support/json'
|
|
||||||
|
|
||||||
app.config.define_setting :data_dir, 'data', 'The directory data files are stored in'
|
def initialize(app, options_hash={}, &block)
|
||||||
app.send :include, InstanceMethods
|
super
|
||||||
|
app.config.define_setting :data_dir, 'data', 'The directory data files are stored in'
|
||||||
|
|
||||||
|
# Directly include the #data method instead of using helpers so that this is available immediately
|
||||||
|
app.send :include, InstanceMethods
|
||||||
|
end
|
||||||
|
|
||||||
|
def before_configuration
|
||||||
|
# Setup data files before anything else so they are available when
|
||||||
|
# parsing config.rb
|
||||||
|
app.files.changed MATCHER do |file|
|
||||||
|
data.touch_file(file) if file.start_with?("#{config[:data_dir]}/")
|
||||||
|
end
|
||||||
|
|
||||||
|
app.files.deleted MATCHER do |file|
|
||||||
|
data.remove_file(file) if file.start_with?("#{config[:data_dir]}/")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Instance methods
|
|
||||||
module InstanceMethods
|
module InstanceMethods
|
||||||
# Setup data files before anything else so they are available when
|
|
||||||
# parsing config.rb
|
|
||||||
def initialize
|
|
||||||
files.changed DataStore.matcher do |file|
|
|
||||||
data.touch_file(file) if file.start_with?("#{config[:data_dir]}/")
|
|
||||||
end
|
|
||||||
|
|
||||||
files.deleted DataStore.matcher do |file|
|
|
||||||
data.remove_file(file) if file.start_with?("#{config[:data_dir]}/")
|
|
||||||
end
|
|
||||||
|
|
||||||
super
|
|
||||||
end
|
|
||||||
|
|
||||||
# The data object
|
# The data object
|
||||||
#
|
#
|
||||||
# @return [DataStore]
|
# @return [DataStore]
|
||||||
|
@ -42,25 +40,14 @@ module Middleman
|
||||||
|
|
||||||
# The core logic behind the data extension.
|
# The core logic behind the data extension.
|
||||||
class DataStore
|
class DataStore
|
||||||
# Static methods
|
|
||||||
class << self
|
|
||||||
# The regex which tells Middleman which files are for data
|
|
||||||
#
|
|
||||||
# @return [Regexp]
|
|
||||||
def matcher
|
|
||||||
%r{[\w-]+\.(yml|yaml|json)$}
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Store static data hash
|
# Store static data hash
|
||||||
#
|
#
|
||||||
# @param [Symbol] name Name of the data, used for namespacing
|
# @param [Symbol] name Name of the data, used for namespacing
|
||||||
# @param [Hash] content The content for this data
|
# @param [Hash] content The content for this data
|
||||||
# @return [Hash]
|
# @return [Hash]
|
||||||
def store(name=nil, content=nil)
|
def store(name=nil, content=nil)
|
||||||
@_local_sources ||= {}
|
@local_sources[name.to_s] = content unless name.nil? || content.nil?
|
||||||
@_local_sources[name.to_s] = content unless name.nil? || content.nil?
|
@local_sources
|
||||||
@_local_sources
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Store callback-based data
|
# Store callback-based data
|
||||||
|
@ -69,9 +56,8 @@ module Middleman
|
||||||
# @param [Proc] proc The callback which will return data
|
# @param [Proc] proc The callback which will return data
|
||||||
# @return [Hash]
|
# @return [Hash]
|
||||||
def callbacks(name=nil, proc=nil)
|
def callbacks(name=nil, proc=nil)
|
||||||
@_callback_sources ||= {}
|
@callback_sources[name.to_s] = proc unless name.nil? || proc.nil?
|
||||||
@_callback_sources[name.to_s] = proc unless name.nil? || proc.nil?
|
@callback_sources
|
||||||
@_callback_sources
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Setup data store
|
# Setup data store
|
||||||
|
@ -80,6 +66,8 @@ module Middleman
|
||||||
def initialize(app)
|
def initialize(app)
|
||||||
@app = app
|
@app = app
|
||||||
@local_data = {}
|
@local_data = {}
|
||||||
|
@local_sources = {}
|
||||||
|
@callback_sources = {}
|
||||||
end
|
end
|
||||||
|
|
||||||
# Update the internal cache for a given file path
|
# Update the internal cache for a given file path
|
||||||
|
|
Loading…
Add table
Reference in a new issue