Make indifferent access easy to use anywhere. Change frontmatter.data to plain old

This commit is contained in:
Thomas Reynolds 2011-12-26 11:24:33 -08:00
parent b9c5c89e40
commit 6ef32652c7
4 changed files with 41 additions and 39 deletions

View file

@ -1,5 +1,8 @@
require "rbconfig" require "rbconfig"
# Using Thor's indifferent hash access
require "thor"
# Setup our load paths # Setup our load paths
libdir = File.expand_path(File.dirname(__FILE__)) libdir = File.expand_path(File.dirname(__FILE__))
$LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir) $LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir)
@ -163,6 +166,28 @@ module Middleman
class << self class << self
# Recursively convert a normal Hash into a HashWithIndifferentAccess
#
# @private
# @param [Hash] data Normal hash
# @return [Thor::CoreExt::HashWithIndifferentAccess]
def recursively_enhance(data)
if data.is_a? Hash
data = ::Thor::CoreExt::HashWithIndifferentAccess.new(data)
data.each do |key, val|
data[key] = recursively_enhance(val)
end
data
elsif data.is_a? Array
data.each_with_index do |val, i|
data[i] = recursively_enhance(val)
end
data
else
data
end
end
# Automatically load extensions from available RubyGems # Automatically load extensions from available RubyGems
# which contain the EXTENSION_FILE # which contain the EXTENSION_FILE
# #

View file

@ -2,9 +2,6 @@
require "yaml" require "yaml"
require "active_support/json" require "active_support/json"
# Using Thor's indifferent hash access
require "thor"
# 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 Middleman::CoreExtensions::Data module Middleman::CoreExtensions::Data
@ -119,7 +116,7 @@ module Middleman::CoreExtensions::Data
return return
end end
@local_data[basename] = recursively_enhance(data) @local_data[basename] = ::Middleman.recursively_enhance(data)
end end
# Remove a given file from the internal cache # Remove a given file from the internal cache
@ -162,7 +159,7 @@ module Middleman::CoreExtensions::Data
result = data_for_path(path) result = data_for_path(path)
if result if result
return recursively_enhance(result) return ::Middleman.recursively_enhance(result)
end end
end end
@ -192,28 +189,5 @@ module Middleman::CoreExtensions::Data
data data
end end
private
# Recursively convert a normal Hash into a HashWithIndifferentAccess
#
# @private
# @param [Hash] data Normal hash
# @return [Thor::CoreExt::HashWithIndifferentAccess]
def recursively_enhance(data)
if data.is_a? Hash
data = Thor::CoreExt::HashWithIndifferentAccess.new(data)
data.each do |key, val|
data[key] = recursively_enhance(val)
end
data
elsif data.is_a? Array
data.each_with_index do |val, i|
data[i] = recursively_enhance(val)
end
data
else
data
end
end
end end
end end

View file

@ -28,18 +28,18 @@ module Middleman::CoreExtensions::FrontMatter
matcher = %r{source/.*(#{exts})} matcher = %r{source/.*(#{exts})}
file_changed matcher do |file| file_changed matcher do |file|
frontmatter.touch_file(file) frontmatter_extension.touch_file(file)
end end
file_deleted matcher do |file| file_deleted matcher do |file|
frontmatter.remove_file(file) frontmatter_extension.remove_file(file)
end end
provides_metadata matcher do |path| provides_metadata matcher do |path|
relative_path = path.sub(source_dir, "") relative_path = path.sub(source_dir, "")
fmdata = if frontmatter.has_data?(relative_path) fmdata = if frontmatter_extension.has_data?(relative_path)
frontmatter.data(relative_path)[0] frontmatter(relative_path)[0]
else else
{} {}
end end
@ -61,8 +61,12 @@ module Middleman::CoreExtensions::FrontMatter
end end
end end
def frontmatter def frontmatter_extension
@frontmatter ||= FrontMatter.new(self) @_frontmatter_extension ||= FrontMatter.new(self)
end
def frontmatter(*args)
frontmatter_extension.data(*args)
end end
end end
@ -88,8 +92,10 @@ module Middleman::CoreExtensions::FrontMatter
result = parse_front_matter(content) result = parse_front_matter(content)
if result if result
data, content = result
data = ::Middleman.recursively_enhance(data)
file = file.sub(@app.source_dir, "") file = file.sub(@app.source_dir, "")
@local_data[file] = result @local_data[file] = [data, content]
path = File.join(@app.source_dir, file) path = File.join(@app.source_dir, file)
@app.cache.set([:raw_template, path], result[1]) @app.cache.set([:raw_template, path], result[1])
@app.frontmatter_did_change(path) @app.frontmatter_did_change(path)

View file

@ -118,13 +118,10 @@ module Middleman::Sitemap
end end
def data def data
data, content = app.frontmatter.data(relative_path) data, content = app.frontmatter(relative_path)
data || nil data || nil
end end
def parent def parent
parts = path.split("/") parts = path.split("/")
if path.include?(app.index_file) if path.include?(app.index_file)