middleman/middleman-core/lib/middleman-core/util/data.rb

107 lines
3.0 KiB
Ruby
Raw Normal View History

2015-06-17 00:30:37 +02:00
require 'yaml'
2015-09-17 18:41:17 +02:00
require 'json'
2015-09-20 14:23:47 +02:00
require 'pathname'
require 'middleman-core/util'
require 'middleman-core/contracts'
2015-10-01 19:25:53 +02:00
require 'backports/2.1.0/array/to_h'
2015-06-17 00:30:37 +02:00
2016-01-13 01:03:23 +01:00
module Middleman
module Util
module Data
include Contracts
2015-06-17 00:30:37 +02:00
2016-01-13 01:03:23 +01:00
module_function
2015-06-17 00:30:37 +02:00
2016-01-13 01:03:23 +01:00
# Get the frontmatter and plain content from a file
# @param [String] path
# @return [Array<Hash, String>]
Contract Pathname, Maybe[Symbol] => [Hash, Maybe[String]]
def parse(full_path, frontmatter_delims, known_type=nil)
2016-01-14 02:16:36 +01:00
return [{}, nil] if ::Middleman::Util.binary?(full_path)
2015-06-17 00:30:37 +02:00
2016-01-13 01:03:23 +01:00
# Avoid weird race condition when a file is renamed
begin
content = File.read(full_path)
rescue EOFError, IOError, Errno::ENOENT
return [{}, nil]
end
2015-06-17 00:30:37 +02:00
2016-01-13 01:03:23 +01:00
start_delims, stop_delims = frontmatter_delims
.values
.flatten(1)
.transpose
.map(&Regexp.method(:union))
2015-10-23 11:08:09 +02:00
2016-01-13 01:03:23 +01:00
match = /
\A(?:[^\r\n]*coding:[^\r\n]*\r?\n)?
(?<start>#{start_delims})[ ]*\r?\n
(?<frontmatter>.*?)[ ]*\r?\n?
^(?<stop>#{stop_delims})[ ]*\r?\n?
\r?\n?
(?<additional_content>.*)
/mx.match(content) || {}
2015-09-20 14:23:47 +02:00
2016-01-13 01:03:23 +01:00
unless match[:frontmatter]
case known_type
when :yaml
return [parse_yaml(content, full_path), nil]
when :json
return [parse_json(content, full_path), nil]
end
end
2016-01-13 01:03:23 +01:00
case [match[:start], match[:stop]]
when *frontmatter_delims[:yaml]
[
parse_yaml(match[:frontmatter], full_path),
match[:additional_content]
]
when *frontmatter_delims[:json]
[
parse_json("{#{match[:frontmatter]}}", full_path),
match[:additional_content]
]
else
[
{},
content
]
end
end
2015-06-17 00:30:37 +02:00
2016-01-13 01:03:23 +01:00
# Parse YAML frontmatter out of a string
# @param [String] content
# @return [Hash]
Contract String, Pathname, Bool => Hash
def parse_yaml(content, full_path)
2016-01-14 02:16:36 +01:00
symbolize_recursive(::YAML.load(content) || {})
rescue StandardError, ::Psych::SyntaxError => error
2016-01-13 01:03:23 +01:00
warn "YAML Exception parsing #{full_path}: #{error.message}"
{}
end
2015-06-17 00:30:37 +02:00
2016-01-13 01:03:23 +01:00
# Parse JSON frontmatter out of a string
# @param [String] content
# @return [Hash]
Contract String, Pathname => Hash
def parse_json(content, full_path)
2016-01-14 02:16:36 +01:00
symbolize_recursive(::JSON.parse(content) || {})
2016-01-13 01:03:23 +01:00
rescue StandardError => error
warn "JSON Exception parsing #{full_path}: #{error.message}"
{}
end
2015-06-17 00:30:37 +02:00
2016-01-13 01:03:23 +01:00
def symbolize_recursive(value)
case value
when Hash
value.map { |k, v| [k.to_sym, symbolize_recursive(v)] }.to_h
when Array
value.map { |v| symbolize_recursive(v) }
else
value
end
end
2015-06-17 00:30:37 +02:00
end
end
end