Merge pull request #1608 from middleman/frontmatter

Fix frontmatter parsing changes from #1608
This commit is contained in:
Thomas Reynolds 2015-09-23 15:23:05 -07:00
commit 5afc138eb8

View file

@ -23,19 +23,22 @@ module Middleman::Util::Data
return [{}, nil] return [{}, nil]
end end
/
\A(.*?coding:.*?\n)?
(?<start>[-;]{3})[ ]*\r?\n
(?<frontmatter>.*?)[ ]*\r?\n?
(?<stop>[-.;]{3})[ ]*\r?\n?
(?<additional_content>.*)
/mx =~ content
unless frontmatter
case known_type case known_type
when :yaml when :yaml
return [parse_yaml(content, full_path), nil] return [parse_yaml(content, full_path), nil]
when :json when :json
return [parse_json(content, full_path), nil] return [parse_json(content, full_path), nil]
end end
end
/
(?<start>^[-;]{3})[ ]*\r?\n
(?<frontmatter>.*?)[ ]*\r?\n
(?<stop>^[-.;]{3})[ ]*\r?\n?
(?<additional_content>.*)
/mx =~ content
case [start, stop] case [start, stop]
when %w[--- ---], %w[--- ...] when %w[--- ---], %w[--- ...]
@ -49,10 +52,10 @@ module Middleman::Util::Data
# 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 [Hash]
Contract String, Pathname, Bool => Hash Contract String, Pathname, Bool => Hash
def parse_yaml(content, full_path) def parse_yaml(content, full_path)
symbolize_recursive(YAML.load(content)) symbolize_recursive(YAML.load(content) || {})
rescue StandardError, Psych::SyntaxError => error rescue StandardError, Psych::SyntaxError => error
warn "YAML Exception parsing #{full_path}: #{error.message}" warn "YAML Exception parsing #{full_path}: #{error.message}"
{} {}
@ -60,10 +63,10 @@ module Middleman::Util::Data
# Parse JSON frontmatter out of a string # Parse JSON frontmatter out of a string
# @param [String] content # @param [String] content
# @return [Array<Hash, String>] # @return [Hash]
Contract String, Pathname => Hash Contract String, Pathname => Hash
def parse_json(content, full_path) def parse_json(content, full_path)
symbolize_recursive(JSON.parse(content)) symbolize_recursive(JSON.parse(content) || {})
rescue StandardError => error rescue StandardError => error
warn "JSON Exception parsing #{full_path}: #{error.message}" warn "JSON Exception parsing #{full_path}: #{error.message}"
{} {}