From bc7ee29486ece84a7c6fe118428fe6b161ef6d1e Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Mon, 12 Sep 2011 10:20:34 -0700 Subject: [PATCH] make json a valid data format --- features/data.feature | 5 +++++ fixtures/test-app/config.rb | 1 + fixtures/test-app/data/test2.json | 4 ++++ fixtures/test-app/source/data3.html.erb | 1 + lib/middleman/core_extensions/data.rb | 14 +++++++++++++- 5 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 fixtures/test-app/data/test2.json create mode 100644 fixtures/test-app/source/data3.html.erb diff --git a/features/data.feature b/features/data.feature index d296e1ba..0977356f 100644 --- a/features/data.feature +++ b/features/data.feature @@ -6,6 +6,11 @@ Feature: Local Data API When I go to "/data.html" Then I should see "One:Two" + Scenario: Rendering json + Given the Server is running at "test-app" + When I go to "/data3.html" + Then I should see "One:Two" + Scenario: Rendering liquid Given the Server is running at "test-app" When I go to "/data2.html" diff --git a/fixtures/test-app/config.rb b/fixtures/test-app/config.rb index 1579c35a..04a6d333 100644 --- a/fixtures/test-app/config.rb +++ b/fixtures/test-app/config.rb @@ -20,6 +20,7 @@ with_layout false do page "/slim.html" page "/data.html" page "/data2.html" + page "/data3.html" page "/liquid_master.html" page "/page-classes.html" page "/sub1/page-classes.html" diff --git a/fixtures/test-app/data/test2.json b/fixtures/test-app/data/test2.json new file mode 100644 index 00000000..9f8b79c6 --- /dev/null +++ b/fixtures/test-app/data/test2.json @@ -0,0 +1,4 @@ +[ + { "title": "One" }, + { "title": "Two" } +] \ No newline at end of file diff --git a/fixtures/test-app/source/data3.html.erb b/fixtures/test-app/source/data3.html.erb new file mode 100644 index 00000000..d2ac7634 --- /dev/null +++ b/fixtures/test-app/source/data3.html.erb @@ -0,0 +1 @@ +<%= data.test2.map { |r| r.title }.join(":") %> \ No newline at end of file diff --git a/lib/middleman/core_extensions/data.rb b/lib/middleman/core_extensions/data.rb index 51bd24d4..44ebae10 100755 --- a/lib/middleman/core_extensions/data.rb +++ b/lib/middleman/core_extensions/data.rb @@ -1,4 +1,5 @@ require "yaml" +require "active_support/json" require "thor" module Middleman::CoreExtensions::Data @@ -38,7 +39,12 @@ module Middleman::CoreExtensions::Data response = YAML.load_file(file_path) else file_path = File.join(@app.root, @app.data_dir, "#{path}.yaml") - response = YAML.load_file(file_path) if File.exists? file_path + if File.exists? file_path + response = YAML.load_file(file_path) + else + file_path = File.join(@app.root, @app.data_dir, "#{path}.json") + response = ActiveSupport::JSON.decode(File.read(file_path)) if File.exists? file_path + end end end @@ -75,6 +81,12 @@ module Middleman::CoreExtensions::Data data[p] = data_for_path(p) end + json_path = File.join(@app.root, @app.data_dir, "*.json") + Dir[json_path].each do |f| + p = f.split("/").last.gsub(".json", "") + data[p] = data_for_path(p) + end + data end