Print file where YAML/JSON errors occur. Fixes #954.

This commit is contained in:
Ben Hollis 2013-07-08 22:16:03 -07:00
parent 8b970f9d92
commit 8979fe9f18

View file

@ -119,7 +119,7 @@ module Middleman::CoreExtensions
# Parse YAML frontmatter out of a string # Parse YAML frontmatter out of a string
# @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, full_path)
yaml_regex = /\A(---\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, "")
@ -128,7 +128,7 @@ module Middleman::CoreExtensions
data = YAML.load($1) || {} data = YAML.load($1) || {}
data = data.symbolize_keys data = data.symbolize_keys
rescue *YAML_ERRORS => e rescue *YAML_ERRORS => e
app.logger.error "YAML Exception: #{e.message}" app.logger.error "YAML Exception parsing #{full_path}: #{e.message}"
return false return false
end end
else else
@ -140,7 +140,7 @@ module Middleman::CoreExtensions
[{}, content] [{}, content]
end end
def parse_json_front_matter(content) def parse_json_front_matter(content, full_path)
json_regex = /\A(;;;\s*\n.*?\n?)^(;;;\s*$\n?)/m json_regex = /\A(;;;\s*\n.*?\n?)^(;;;\s*$\n?)/m
if content =~ json_regex if content =~ json_regex
@ -150,7 +150,7 @@ module Middleman::CoreExtensions
json = ($1+$2).sub(";;;", "{").sub(";;;", "}") json = ($1+$2).sub(";;;", "{").sub(";;;", "}")
data = ActiveSupport::JSON.decode(json).symbolize_keys data = ActiveSupport::JSON.decode(json).symbolize_keys
rescue => e rescue => e
app.logger.error "JSON Exception: #{e.message}" app.logger.error "JSON Exception parsing #{full_path}: #{e.message}"
return false return false
end end
@ -186,7 +186,7 @@ module Middleman::CoreExtensions
content = lines.join("\n") content = lines.join("\n")
end end
result = parse_yaml_front_matter(content) || parse_json_front_matter(content) result = parse_yaml_front_matter(content, full_path) || parse_json_front_matter(content, full_path)
return result if result return result if result
rescue rescue
# Probably a binary file, move on # Probably a binary file, move on