Refactor util/data.rb further
This commit is contained in:
parent
62f431b5ae
commit
91a06a1a35
|
@ -1,103 +1,82 @@
|
||||||
# Core Pathname library used for traversal
|
require 'yaml'
|
||||||
|
require 'json'
|
||||||
require 'pathname'
|
require 'pathname'
|
||||||
|
require 'middleman-core/util'
|
||||||
# DbC
|
|
||||||
require 'middleman-core/contracts'
|
require 'middleman-core/contracts'
|
||||||
|
|
||||||
# Shared util methods
|
module Middleman::Util::Data
|
||||||
require 'middleman-core/util'
|
include Contracts
|
||||||
|
|
||||||
# Parsing YAML data
|
module_function
|
||||||
require 'yaml'
|
|
||||||
|
|
||||||
# Parsing JSON data
|
# Get the frontmatter and plain content from a file
|
||||||
require 'json'
|
# @param [String] path
|
||||||
|
# @return [Array<Hash, String>]
|
||||||
|
Contract Pathname, Maybe[Symbol] => [Hash, Maybe[String]]
|
||||||
|
def parse(full_path, known_type=nil)
|
||||||
|
return [{}, nil] if Middleman::Util.binary?(full_path)
|
||||||
|
|
||||||
module Middleman
|
# Avoid weird race condition when a file is renamed
|
||||||
module Util
|
begin
|
||||||
module Data
|
content = File.read(full_path)
|
||||||
include Contracts
|
rescue EOFError, IOError, Errno::ENOENT
|
||||||
|
return [{}, nil]
|
||||||
|
end
|
||||||
|
|
||||||
module_function
|
case known_type
|
||||||
|
when :yaml
|
||||||
|
return [parse_yaml(content, full_path), nil]
|
||||||
|
when :json
|
||||||
|
return [parse_json(content, full_path), nil]
|
||||||
|
end
|
||||||
|
|
||||||
# Get the frontmatter and plain content from a file
|
/
|
||||||
# @param [String] path
|
(?<start>^[-;]{3})[ ]*\r?\n
|
||||||
# @return [Array<Hash, String>]
|
(?<frontmatter>.*?)[ ]*\r?\n
|
||||||
Contract Pathname, Maybe[Symbol] => [Hash, Maybe[String]]
|
(?<stop>^[-.;]{3})[ ]*\r?\n?
|
||||||
def parse(full_path, known_type=nil)
|
(?<additional_content>.*)
|
||||||
return [{}, nil] if Middleman::Util.binary?(full_path)
|
/mx =~ content
|
||||||
|
|
||||||
# Avoid weird race condition when a file is renamed
|
case [start, stop]
|
||||||
begin
|
when %w[--- ---], %w[--- ...]
|
||||||
content = File.read(full_path)
|
[parse_yaml(frontmatter, full_path), additional_content]
|
||||||
rescue EOFError, IOError, Errno::ENOENT
|
when %w[;;; ;;;]
|
||||||
return [{}, nil]
|
[parse_json(frontmatter, full_path), additional_content]
|
||||||
end
|
else
|
||||||
|
[{}, content]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
case known_type
|
# Parse YAML frontmatter out of a string
|
||||||
when :yaml
|
# @param [String] content
|
||||||
return [parse_yaml(content, full_path), nil]
|
# @return [Array<Hash, String>]
|
||||||
when :json
|
Contract String, Pathname, Bool => Hash
|
||||||
return [parse_json(content, full_path), nil]
|
def parse_yaml(content, full_path)
|
||||||
end
|
symbolize_recursive(YAML.load(content))
|
||||||
|
rescue StandardError, Psych::SyntaxError => error
|
||||||
|
warn "YAML Exception parsing #{full_path}: #{error.message}"
|
||||||
|
{}
|
||||||
|
end
|
||||||
|
|
||||||
/
|
# Parse JSON frontmatter out of a string
|
||||||
(?<start>^[-;]{3})[ ]*\r?\n
|
# @param [String] content
|
||||||
(?<frontmatter>.*?)[ ]*\r?\n
|
# @return [Array<Hash, String>]
|
||||||
(?<stop>^[-.;]{3})[ ]*\r?\n?
|
Contract String, Pathname => Hash
|
||||||
(?<additional_content>.*)
|
def parse_json(content, full_path)
|
||||||
/mx =~ content
|
symbolize_recursive(JSON.parse(content))
|
||||||
|
rescue StandardError => error
|
||||||
|
warn "JSON Exception parsing #{full_path}: #{error.message}"
|
||||||
|
{}
|
||||||
|
end
|
||||||
|
|
||||||
return [{}, content] unless frontmatter
|
def symbolize_recursive(value)
|
||||||
|
case value
|
||||||
case [start, stop]
|
when Hash
|
||||||
when %w[--- ---], %w[--- ...]
|
value.map { |k, v| [k.to_sym, symbolize_recursive(v)] }.to_h
|
||||||
[parse_yaml(frontmatter, full_path), additional_content]
|
when Array
|
||||||
when %w[;;; ;;;]
|
value.map { |v| symbolize_recursive(v) }
|
||||||
[parse_json(frontmatter, full_path), additional_content]
|
else
|
||||||
else
|
value
|
||||||
[{}, content]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Parse YAML frontmatter out of a string
|
|
||||||
# @param [String] content
|
|
||||||
# @return [Array<Hash, String>]
|
|
||||||
Contract String, Pathname, Bool => Hash
|
|
||||||
def parse_yaml(content, full_path)
|
|
||||||
map_value(YAML.load(content))
|
|
||||||
rescue StandardError, Psych::SyntaxError => error
|
|
||||||
warn "YAML Exception parsing #{full_path}: #{error.message}"
|
|
||||||
{}
|
|
||||||
end
|
|
||||||
|
|
||||||
# Parse JSON frontmatter out of a string
|
|
||||||
# @param [String] content
|
|
||||||
# @return [Array<Hash, String>]
|
|
||||||
Contract String, Pathname => Hash
|
|
||||||
def parse_json(content, full_path)
|
|
||||||
map_value(JSON.parse(content))
|
|
||||||
rescue StandardError => error
|
|
||||||
warn "JSON Exception parsing #{full_path}: #{error.message}"
|
|
||||||
{}
|
|
||||||
end
|
|
||||||
|
|
||||||
def symbolize_recursive(hash)
|
|
||||||
{}.tap do |h|
|
|
||||||
hash.each { |key, value| h[key.to_sym] = map_value(value) }
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def map_value(thing)
|
|
||||||
case thing
|
|
||||||
when Hash
|
|
||||||
symbolize_recursive(thing)
|
|
||||||
when Array
|
|
||||||
thing.map { |v| map_value(v) }
|
|
||||||
else
|
|
||||||
thing
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue