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

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