diff --git a/middleman-core/features/front-matter.feature b/middleman-core/features/front-matter.feature index f7fd0042..0b7103e3 100644 --- a/middleman-core/features/front-matter.feature +++ b/middleman-core/features/front-matter.feature @@ -1,8 +1,11 @@ Feature: YAML Front Matter In order to specific options and data inline - Scenario: Rendering html + Scenario: Rendering html (yaml) Given the Server is running at "frontmatter-app" + When I go to "/front-matter-auto.html" + Then I should see "

This is the title

" + Then I should not see "---" When I go to "/front-matter.html" Then I should see "

This is the title

" Then I should not see "---" @@ -10,6 +13,19 @@ Feature: YAML Front Matter Then I should see "

This is the title

" Then I should see "This is the title" + Then I should not see "{" + When I go to "/json-front-matter.html" + Then I should see "

This is the title

" + Then I should not see "{" + When I go to "/json-front-matter-2.php" + Then I should see "

This is the title

" + Then I should see "<%= data.page.title %> \ No newline at end of file diff --git a/middleman-core/fixtures/frontmatter-app/source/front-matter.html.erb b/middleman-core/fixtures/frontmatter-app/source/front-matter.html.erb index 73d30e72..2bb8e884 100644 --- a/middleman-core/fixtures/frontmatter-app/source/front-matter.html.erb +++ b/middleman-core/fixtures/frontmatter-app/source/front-matter.html.erb @@ -1,3 +1,5 @@ +

Test

+ --- layout: false title: This is the title diff --git a/middleman-core/fixtures/frontmatter-app/source/json-front-matter-2.php.erb b/middleman-core/fixtures/frontmatter-app/source/json-front-matter-2.php.erb new file mode 100644 index 00000000..a47095af --- /dev/null +++ b/middleman-core/fixtures/frontmatter-app/source/json-front-matter-2.php.erb @@ -0,0 +1,7 @@ +{{{ + "layout": false, + "title": "This is the title" +}}} + +

<%= data.page.title %>

+ \ No newline at end of file diff --git a/middleman-core/fixtures/frontmatter-app/source/json-front-matter-auto.erb b/middleman-core/fixtures/frontmatter-app/source/json-front-matter-auto.erb new file mode 100644 index 00000000..c86ebd84 --- /dev/null +++ b/middleman-core/fixtures/frontmatter-app/source/json-front-matter-auto.erb @@ -0,0 +1,6 @@ +{{{ + "layout": false, + "title": "This is the title" +}}} + +

<%= data.page.title %>

\ No newline at end of file diff --git a/middleman-core/fixtures/frontmatter-app/source/json-front-matter.html.erb b/middleman-core/fixtures/frontmatter-app/source/json-front-matter.html.erb new file mode 100644 index 00000000..c86ebd84 --- /dev/null +++ b/middleman-core/fixtures/frontmatter-app/source/json-front-matter.html.erb @@ -0,0 +1,6 @@ +{{{ + "layout": false, + "title": "This is the title" +}}} + +

<%= data.page.title %>

\ No newline at end of file diff --git a/middleman-core/lib/middleman-core/core_extensions/front_matter.rb b/middleman-core/lib/middleman-core/core_extensions/front_matter.rb index 76713060..b4c2d99f 100644 --- a/middleman-core/lib/middleman-core/core_extensions/front_matter.rb +++ b/middleman-core/lib/middleman-core/core_extensions/front_matter.rb @@ -11,6 +11,9 @@ module Middleman::CoreExtensions def registered(app) # Parsing YAML frontmatter require "yaml" + + # Parsing JSON frontmatter + require "active_support/json" app.after_configuration do ::Middleman::Sitemap::Resource.send :include, ResourceInstanceMethods @@ -56,13 +59,13 @@ module Middleman::CoreExtensions @cache.delete(p) end - # Parse frontmatter out of a string + # Parse YAML frontmatter out of a string # @param [String] content # @return [Array] - def parse_front_matter(content) + def parse_yaml_front_matter(content) yaml_regex = /^(---\s*\n.*?\n?)^(---\s*$\n?)/m if content =~ yaml_regex - content = content[($1.size + $2.size)..-1] + content = content.sub(yaml_regex, "") begin data = YAML.load($1) @@ -80,6 +83,29 @@ module Middleman::CoreExtensions [{}, content] end + def parse_json_front_matter(content) + json_regex = /^(\{\{\{\s*\n.*?\n?)^(\}\}\}\s*$\n?)/m + + if content =~ json_regex + content = content.sub(json_regex, "") + + begin + json = ($1+$2).sub("{{{", "{").sub("}}}", "}") + data = ActiveSupport::JSON.decode(json) + rescue => e + puts "JSON Exception: #{e.message}" + return false + end + + else + return false + end + + [data, content] + rescue + [{}, content] + end + # Get the frontmatter and plain content from a file # @param [String] path # @return [Array] @@ -87,9 +113,10 @@ module Middleman::CoreExtensions full_path = File.expand_path(path, @app.source_dir) content = File.read(full_path) - result = parse_front_matter(content) - - if result + if result = parse_yaml_front_matter(content) + data, content = result + data = ::Middleman::Util.recursively_enhance(data).freeze + elsif result = parse_json_front_matter(content) data, content = result data = ::Middleman::Util.recursively_enhance(data).freeze else