From b5b63492202ecf14adbad327440d509b6a6a149f Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Fri, 25 May 2012 13:17:22 -0700 Subject: [PATCH] Only allow frontmatter on first line, unless we have a ruby 1.9 encoding comment, then allow it on the second line. Fixes #446 --- middleman-core/features/front-matter.feature | 33 +++++++++++++++---- .../source/front-matter-encoding.html.erb | 7 ++++ ....html.erb => front-matter-line-2.html.erb} | 1 - .../json-front-matter-encoding.html.erb | 7 ++++ .../source/json-front-matter-line-2.html.erb | 7 ++++ .../core_extensions/front_matter.rb | 26 ++++++++++----- 6 files changed, 66 insertions(+), 15 deletions(-) create mode 100644 middleman-core/fixtures/frontmatter-app/source/front-matter-encoding.html.erb rename middleman-core/fixtures/frontmatter-app/source/{front-matter.html.erb => front-matter-line-2.html.erb} (66%) create mode 100644 middleman-core/fixtures/frontmatter-app/source/json-front-matter-encoding.html.erb create mode 100644 middleman-core/fixtures/frontmatter-app/source/json-front-matter-line-2.html.erb diff --git a/middleman-core/features/front-matter.feature b/middleman-core/features/front-matter.feature index 0b7103e3..5e57de30 100644 --- a/middleman-core/features/front-matter.feature +++ b/middleman-core/features/front-matter.feature @@ -6,26 +6,47 @@ Feature: YAML Front Matter 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 "---" When I go to "/front-matter-2.php" Then I should see "

This is the title

" Then I should see "" + Then I should see "---" + + Scenario: YAML not on first line, with encoding + Given the Server is running at "frontmatter-app" + When I go to "/front-matter-encoding.html" + Then I should see "

This is the title

" + Then I should not see "---" Scenario: Rendering html (json) Given the Server is running at "frontmatter-app" When I go to "/json-front-matter-auto.html" Then I should see "

This is the title

" - Then I should not see "{" + 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 "{" + 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 "" + Then I should see ";;;" + + Scenario: JSON not on first line, with encoding + Given the Server is running at "frontmatter-app" + When I go to "/json-front-matter-encoding.html" + Then I should see "

This is the title

" + Then I should not see ";;;" Scenario: A template changes frontmatter during preview Given the Server is running at "frontmatter-app" diff --git a/middleman-core/fixtures/frontmatter-app/source/front-matter-encoding.html.erb b/middleman-core/fixtures/frontmatter-app/source/front-matter-encoding.html.erb new file mode 100644 index 00000000..c44d9633 --- /dev/null +++ b/middleman-core/fixtures/frontmatter-app/source/front-matter-encoding.html.erb @@ -0,0 +1,7 @@ +# encoding: UTF-8 +--- +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/front-matter.html.erb b/middleman-core/fixtures/frontmatter-app/source/front-matter-line-2.html.erb similarity index 66% rename from middleman-core/fixtures/frontmatter-app/source/front-matter.html.erb rename to middleman-core/fixtures/frontmatter-app/source/front-matter-line-2.html.erb index 2bb8e884..a3a6dacf 100644 --- a/middleman-core/fixtures/frontmatter-app/source/front-matter.html.erb +++ b/middleman-core/fixtures/frontmatter-app/source/front-matter-line-2.html.erb @@ -1,5 +1,4 @@

Test

- --- layout: false title: This is the title diff --git a/middleman-core/fixtures/frontmatter-app/source/json-front-matter-encoding.html.erb b/middleman-core/fixtures/frontmatter-app/source/json-front-matter-encoding.html.erb new file mode 100644 index 00000000..d34321a4 --- /dev/null +++ b/middleman-core/fixtures/frontmatter-app/source/json-front-matter-encoding.html.erb @@ -0,0 +1,7 @@ +# encoding: UTF-8 +;;; +"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-line-2.html.erb b/middleman-core/fixtures/frontmatter-app/source/json-front-matter-line-2.html.erb new file mode 100644 index 00000000..b6428aa9 --- /dev/null +++ b/middleman-core/fixtures/frontmatter-app/source/json-front-matter-line-2.html.erb @@ -0,0 +1,7 @@ +

Test

+;;; +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 8c47f84b..d3e8d35a 100644 --- a/middleman-core/lib/middleman-core/core_extensions/front_matter.rb +++ b/middleman-core/lib/middleman-core/core_extensions/front_matter.rb @@ -77,7 +77,7 @@ module Middleman::CoreExtensions # @param [String] content # @return [Array] def parse_yaml_front_matter(content) - yaml_regex = /^(---\s*\n.*?\n?)^(---\s*$\n?)/m + yaml_regex = /\A(---\s*\n.*?\n?)^(---\s*$\n?)/m if content =~ yaml_regex content = content.sub(yaml_regex, "") @@ -98,7 +98,7 @@ module Middleman::CoreExtensions end def parse_json_front_matter(content) - json_regex = /^(\{\{\{\s*\n.*?\n?)^(\}\}\}\s*$\n?)/m + json_regex = /\A(;;;\s*\n.*?\n?)^(;;;\s*$\n?)/m if content =~ json_regex content = content.sub(json_regex, "") @@ -125,14 +125,24 @@ module Middleman::CoreExtensions # @return [Array] def frontmatter_and_content(path) full_path = File.expand_path(path, @app.source_dir) + content = File.read(full_path) + data = {} + + begin + if content =~ /\A.*coding:/ + lines = content.split(/\n/) + lines.shift + content = lines.join("\n") + end - if result = parse_yaml_front_matter(content) - data, content = result - elsif result = parse_json_front_matter(content) - data, content = result - else - data = {} + if result = parse_yaml_front_matter(content) + data, content = result + elsif result = parse_json_front_matter(content) + data, content = result + end + rescue => e + # Probably a binary file, move on end [::Middleman::Util.recursively_enhance(data).freeze, content]