make json a valid data format

This commit is contained in:
Thomas Reynolds 2011-09-12 10:20:34 -07:00
parent dbaae67d1a
commit bc7ee29486
5 changed files with 24 additions and 1 deletions

View file

@ -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"

View file

@ -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"

View file

@ -0,0 +1,4 @@
[
{ "title": "One" },
{ "title": "Two" }
]

View file

@ -0,0 +1 @@
<%= data.test2.map { |r| r.title }.join(":") %>

View file

@ -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