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:
parent
dcdbf3e110
commit
b5b6349220
|
@ -6,26 +6,47 @@ Feature: YAML Front Matter
|
||||||
When I go to "/front-matter-auto.html"
|
When I go to "/front-matter-auto.html"
|
||||||
Then I should see "<h1>This is the title</h1>"
|
Then I should see "<h1>This is the title</h1>"
|
||||||
Then I should not see "---"
|
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"
|
When I go to "/front-matter-2.php"
|
||||||
Then I should see "<h1>This is the title</h1>"
|
Then I should see "<h1>This is the title</h1>"
|
||||||
Then I should see "<?php"
|
Then I should see "<?php"
|
||||||
Then I should not see "---"
|
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)
|
Scenario: Rendering html (json)
|
||||||
Given the Server is running at "frontmatter-app"
|
Given the Server is running at "frontmatter-app"
|
||||||
When I go to "/json-front-matter-auto.html"
|
When I go to "/json-front-matter-auto.html"
|
||||||
Then I should see "<h1>This is the title</h1>"
|
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"
|
When I go to "/json-front-matter.html"
|
||||||
Then I should see "<h1>This is the title</h1>"
|
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"
|
When I go to "/json-front-matter-2.php"
|
||||||
Then I should see "<h1>This is the title</h1>"
|
Then I should see "<h1>This is the title</h1>"
|
||||||
Then I should see "<?php"
|
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
|
Scenario: A template changes frontmatter during preview
|
||||||
Given the Server is running at "frontmatter-app"
|
Given the Server is running at "frontmatter-app"
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
# encoding: UTF-8
|
||||||
|
---
|
||||||
|
layout: false
|
||||||
|
title: This is the title
|
||||||
|
---
|
||||||
|
|
||||||
|
<h1><%= data.page.title %></h1>
|
|
@ -1,5 +1,4 @@
|
||||||
<h2> Test</h2>
|
<h2> Test</h2>
|
||||||
|
|
||||||
---
|
---
|
||||||
layout: false
|
layout: false
|
||||||
title: This is the title
|
title: This is the title
|
|
@ -0,0 +1,7 @@
|
||||||
|
# encoding: UTF-8
|
||||||
|
;;;
|
||||||
|
"layout": false,
|
||||||
|
"title": "This is the title"
|
||||||
|
;;;
|
||||||
|
|
||||||
|
<h1><%= data.page.title %></h1>
|
|
@ -0,0 +1,7 @@
|
||||||
|
<h2> Test</h2>
|
||||||
|
;;;
|
||||||
|
layout: false,
|
||||||
|
title: "This is the title"
|
||||||
|
;;;
|
||||||
|
|
||||||
|
<h1><%= data.page.title %></h1>
|
|
@ -77,7 +77,7 @@ module Middleman::CoreExtensions
|
||||||
# @param [String] content
|
# @param [String] content
|
||||||
# @return [Array<Hash, String>]
|
# @return [Array<Hash, String>]
|
||||||
def parse_yaml_front_matter(content)
|
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
|
if content =~ yaml_regex
|
||||||
content = content.sub(yaml_regex, "")
|
content = content.sub(yaml_regex, "")
|
||||||
|
|
||||||
|
@ -98,7 +98,7 @@ module Middleman::CoreExtensions
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse_json_front_matter(content)
|
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
|
if content =~ json_regex
|
||||||
content = content.sub(json_regex, "")
|
content = content.sub(json_regex, "")
|
||||||
|
@ -125,14 +125,24 @@ module Middleman::CoreExtensions
|
||||||
# @return [Array<Thor::CoreExt::HashWithIndifferentAccess, String>]
|
# @return [Array<Thor::CoreExt::HashWithIndifferentAccess, String>]
|
||||||
def frontmatter_and_content(path)
|
def frontmatter_and_content(path)
|
||||||
full_path = File.expand_path(path, @app.source_dir)
|
full_path = File.expand_path(path, @app.source_dir)
|
||||||
|
|
||||||
content = File.read(full_path)
|
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)
|
if result = parse_yaml_front_matter(content)
|
||||||
data, content = result
|
data, content = result
|
||||||
elsif result = parse_json_front_matter(content)
|
elsif result = parse_json_front_matter(content)
|
||||||
data, content = result
|
data, content = result
|
||||||
else
|
end
|
||||||
data = {}
|
rescue => e
|
||||||
|
# Probably a binary file, move on
|
||||||
end
|
end
|
||||||
|
|
||||||
[::Middleman::Util.recursively_enhance(data).freeze, content]
|
[::Middleman::Util.recursively_enhance(data).freeze, content]
|
||||||
|
|
Loading…
Reference in a new issue