diff --git a/middleman-core/features/data.feature b/middleman-core/features/data.feature index 6d5b06c1..0c444f3c 100644 --- a/middleman-core/features/data.feature +++ b/middleman-core/features/data.feature @@ -5,18 +5,23 @@ Feature: Local Data API Given the Server is running at "basic-data-app" When I go to "/data.html" Then I should see "One:Two" - + Scenario: Rendering json Given the Server is running at "basic-data-app" When I go to "/data3.html" Then I should see "One:Two" - + Scenario: Using data in config.rb Given the Server is running at "data-app" When I go to "/test1.html" Then I should see "Welcome" - + Scenario: Using data2 in config.rb Given the Server is running at "data-app" When I go to "/test2.html" - Then I should see "Welcome" \ No newline at end of file + Then I should see "Welcome" + + Scenario: Using nested data + Given the Server is running at "nested-data-app" + When I go to "/test.html" + Then I should see "test:Hello" diff --git a/middleman-core/fixtures/nested-data-app/config.rb b/middleman-core/fixtures/nested-data-app/config.rb new file mode 100644 index 00000000..09ff69dd --- /dev/null +++ b/middleman-core/fixtures/nested-data-app/config.rb @@ -0,0 +1 @@ +set :layout, false diff --git a/middleman-core/fixtures/nested-data-app/data/examples/test.yml b/middleman-core/fixtures/nested-data-app/data/examples/test.yml new file mode 100644 index 00000000..d4b1eeeb --- /dev/null +++ b/middleman-core/fixtures/nested-data-app/data/examples/test.yml @@ -0,0 +1 @@ +title: "Hello" diff --git a/middleman-core/fixtures/nested-data-app/source/test.html.erb b/middleman-core/fixtures/nested-data-app/source/test.html.erb new file mode 100644 index 00000000..acd20c82 --- /dev/null +++ b/middleman-core/fixtures/nested-data-app/source/test.html.erb @@ -0,0 +1 @@ +<%= data.examples.map { |k, r| [k, r.title].join(":") }.join %> diff --git a/middleman-core/lib/middleman-core/core_extensions/data.rb b/middleman-core/lib/middleman-core/core_extensions/data.rb index c2167739..adf39216 100644 --- a/middleman-core/lib/middleman-core/core_extensions/data.rb +++ b/middleman-core/lib/middleman-core/core_extensions/data.rb @@ -92,19 +92,30 @@ module Middleman # @param [String] file The file to be re-parsed # @return [void] def touch_file(file) - file = File.expand_path(file, @app.root) + root = Pathname(@app.root) + full_path = root + file extension = File.extname(file) basename = File.basename(file, extension) + data_path = full_path.relative_path_from(root + @app.config[:data_dir]) + if %w(.yaml .yml).include?(extension) - data = YAML.load_file(file) + data = YAML.load_file(full_path) elsif extension == ".json" - data = ActiveSupport::JSON.decode(File.read(file)) + data = ActiveSupport::JSON.decode(full_path.read) else return end - @local_data[basename] = ::Middleman::Util.recursively_enhance(data) + data_branch = @local_data + + path = data_path.to_s.split(File::SEPARATOR)[0..-2] + path.each do |dir| + data_branch[dir] ||= ::Middleman::Util.recursively_enhance({}) + data_branch = data_branch[dir] + end + + data_branch[basename] = ::Middleman::Util.recursively_enhance(data) end # Remove a given file from the internal cache @@ -112,9 +123,21 @@ module Middleman # @param [String] file The file to be cleared # @return [void] def remove_file(file) + root = Pathname(@app.root) + full_path = root + file extension = File.extname(file) basename = File.basename(file, extension) - @local_data.delete(basename) if @local_data.has_key?(basename) + + data_path = full_path.relative_path_from(root + @app.config[:data_dir]) + + data_branch = @local_data + + path = data_path.to_s.split(File::SEPARATOR)[0..-2] + path.each do |dir| + data_branch = data_branch[dir] + end + + data_branch.delete(basename) if data_branch.has_key?(basename) end # Get a hash from either internal static data or a callback