Only allow frontmatter on first line, unless we have a ruby 1.9 encoding comment, then allow it on the second line. Fixes #446

This commit is contained in:
Thomas Reynolds 2012-05-25 13:17:22 -07:00
parent dcdbf3e110
commit b5b6349220
6 changed files with 66 additions and 15 deletions

View file

@ -6,26 +6,47 @@ Feature: YAML Front Matter
When I go to "/front-matter-auto.html"
Then I should see "<h1>This is the title</h1>"
Then I should not see "---"
When I go to "/front-matter.html"
Then I should see "<h1>This is the title</h1>"
Then I should not see "---"
When I go to "/front-matter-2.php"
Then I should see "<h1>This is the title</h1>"
Then I should see "<?php"
Then I should not see "---"
Scenario: YAML not on first line, no encoding
Given the Server is running at "frontmatter-app"
When I go to "/front-matter-line-2.html"
Then I should see "<h1></h1>"
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 "<h1>This is the title</h1>"
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 "<h1>This is the title</h1>"
Then I should not see "{"
Then I should not see ";;;"
When I go to "/json-front-matter.html"
Then I should see "<h1>This is the title</h1>"
Then I should not see "{"
Then I should not see ";;;"
When I go to "/json-front-matter-2.php"
Then I should see "<h1>This is the title</h1>"
Then I should see "<?php"
Then I should not see "{"
Then I should not see ";;;"
Scenario: JSON not on first line, no encoding
Given the Server is running at "frontmatter-app"
When I go to "/json-front-matter-line-2.html"
Then I should see "<h1></h1>"
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 "<h1>This is the title</h1>"
Then I should not see ";;;"
Scenario: A template changes frontmatter during preview
Given the Server is running at "frontmatter-app"

View file

@ -0,0 +1,7 @@
# encoding: UTF-8
---
layout: false
title: This is the title
---
<h1><%= data.page.title %></h1>

View file

@ -0,0 +1,7 @@
# encoding: UTF-8
;;;
"layout": false,
"title": "This is the title"
;;;
<h1><%= data.page.title %></h1>

View file

@ -0,0 +1,7 @@
<h2> Test</h2>
;;;
layout: false,
title: "This is the title"
;;;
<h1><%= data.page.title %></h1>

View file

@ -77,7 +77,7 @@ module Middleman::CoreExtensions
# @param [String] content
# @return [Array<Hash, String>]
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<Thor::CoreExt::HashWithIndifferentAccess, String>]
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]