Refactor frontmatter parsing

Closes #1603
This commit is contained in:
Eliott Appleford 2015-09-19 14:53:12 +00:00
parent 2907761556
commit 62f431b5ae

View file

@ -20,116 +20,66 @@ module Middleman
module_function module_function
YAML_ERRORS = [StandardError]
# https://github.com/tenderlove/psych/issues/23
if defined?(Psych) && defined?(Psych::SyntaxError)
YAML_ERRORS << Psych::SyntaxError
end
# Get the frontmatter and plain content from a file # Get the frontmatter and plain content from a file
# @param [String] path # @param [String] path
# @return [Array<Hash, String>] # @return [Array<Hash, String>]
Contract Pathname, Maybe[Symbol] => [Hash, Maybe[String]] Contract Pathname, Maybe[Symbol] => [Hash, Maybe[String]]
def parse(full_path, known_type=nil) def parse(full_path, known_type=nil)
data = {} return [{}, nil] if Middleman::Util.binary?(full_path)
return [data, nil] if ::Middleman::Util.binary?(full_path)
# Avoid weird race condition when a file is renamed.
content = begin
File.read(full_path)
rescue ::EOFError
rescue ::IOError
rescue ::Errno::ENOENT
''
end
# Avoid weird race condition when a file is renamed
begin begin
if content =~ /\A.*coding:/ content = File.read(full_path)
lines = content.split(/\n/) rescue EOFError, IOError, Errno::ENOENT
lines.shift return [{}, nil]
content = lines.join("\n")
end end
if known_type case known_type
if known_type == :yaml when :yaml
result = parse_yaml(content, full_path, true) return [parse_yaml(content, full_path), nil]
elsif known_type == :json when :json
result = parse_json(content, full_path) return [parse_json(content, full_path), nil]
end end
/
(?<start>^[-;]{3})[ ]*\r?\n
(?<frontmatter>.*?)[ ]*\r?\n
(?<stop>^[-.;]{3})[ ]*\r?\n?
(?<additional_content>.*)
/mx =~ content
return [{}, content] unless frontmatter
case [start, stop]
when %w[--- ---], %w[--- ...]
[parse_yaml(frontmatter, full_path), additional_content]
when %w[;;; ;;;]
[parse_json(frontmatter, full_path), additional_content]
else else
result = parse_yaml(content, full_path, false) [{}, content]
end end
return result if result
rescue
# Probably a binary file, move on
end
[data, content]
end end
# 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>]
Contract String, Pathname, Bool => Maybe[[Hash, String]] Contract String, Pathname, Bool => Hash
def parse_yaml(content, full_path, require_yaml=false) def parse_yaml(content, full_path)
total_delims = content.scan(/^(?:---|\.\.\.)\s*(?:\n|$)/).length map_value(YAML.load(content))
has_first_line_delim = !content.match(/\A(---\s*(?:\n|$))/).nil? rescue StandardError, Psych::SyntaxError => error
# has_closing_delim = (total_delims > 1 && has_first_line_delim) || (!has_first_line_delim && total_delims == 1) warn "YAML Exception parsing #{full_path}: #{error.message}"
{}
parts = content.split(/^(?:---|\.\.\.)\s*(?:\n|$)/)
parts.shift if parts[0].empty?
yaml_string = nil
additional_content = nil
if require_yaml
yaml_string = parts[0]
additional_content = parts[1]
else
if total_delims > 1
if has_first_line_delim
yaml_string = parts[0]
additional_content = parts[1]
else
additional_content = content
end
else
additional_content = parts[0]
end
end
return [{}, additional_content] if yaml_string.nil?
begin
data = map_value(::YAML.load(yaml_string) || {})
rescue *YAML_ERRORS => e
$stderr.puts "YAML Exception parsing #{full_path}: #{e.message}"
return nil
end
[data, additional_content]
rescue
[{}, additional_content]
end end
# 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 [Array<Hash, String>]
Contract String, Pathname => Maybe[[Hash, String]] Contract String, Pathname => Hash
def parse_json(content, full_path) def parse_json(content, full_path)
begin map_value(JSON.parse(content))
data = map_value(::JSON.parse(content)) rescue StandardError => error
rescue => e warn "JSON Exception parsing #{full_path}: #{error.message}"
$stderr.puts "JSON Exception parsing #{full_path}: #{e.message}" {}
return nil
end
[data, nil]
rescue
[{}, nil]
end end
def symbolize_recursive(hash) def symbolize_recursive(hash)