require'yaml'require'json'require'toml'require'pathname'require'hashie'require'memoist'require'middleman-core/util/binary'require'middleman-core/contracts'moduleMiddlemanmoduleUtilincludeContractsmodule_functionclassEnhancedHash<::Hashie::Mash# include ::Hashie::Extensions::MergeInitializer# include ::Hashie::Extensions::MethodReader# include ::Hashie::Extensions::IndifferentAccessend# Recursively convert a normal Hash into a EnhancedHash## @private# @param [Hash] data Normal hash# @return [Hash]ContractAny=>Maybe[Or[Array,EnhancedHash]]defrecursively_enhance(obj)ifobj.is_a?::Arrayobj.map{|e|recursively_enhance(e)}elsifobj.is_a?::HashEnhancedHash.new(obj)elseobjendendmoduleDataextendMemoistincludeContractsmodule_function# Get the frontmatter and plain content from a file# @param [String] path# @return [Array<Hash, String>]ContractIsA['Middleman::SourceFile'],Maybe[Symbol]=>[Hash,Maybe[String]]defparse(file,frontmatter_delims,known_type=nil)full_path=file[:full_path]return[{},nil]if::Middleman::Util.binary?(full_path)||file[:types].include?(:binary)# Avoid weird race condition when a file is renamedbegincontent=file.readrescueEOFError,IOError,::Errno::ENOENTreturn[{},nil]endmatch=build_regex(frontmatter_delims).match(content)||{}unlessmatch[:frontmatter]caseknown_typewhen:yamlreturn[parse_yaml(content,full_path),nil]when:jsonreturn[parse_json(content,full_path),nil]when:tomlreturn[parse_toml(content,full_path),nil]endendcase[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]]when*frontmatter_delims[:toml][parse_toml(match[:frontmatter],full_path),match[:additional_content]]else[{},content]endenddefbuild_regex(frontmatter_delims)start_delims,stop_delims=frontmatter_delims.values.flatten(1).transpose.map(&::Regexp.method(:union))/
\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>.*)
/mxendmemoize:build_regex# Parse YAML frontmatter out of a string# @param [String] content# @return [Hash]ContractString,Pathname=>Hashdefparse_yaml(content,full_path)permitted_classes=[Date,Time,DateTime,Symbol,Regexp]c=begin::Middleman::Util.instrument'parse.yaml'doallowed_parameters=::YAML.method(:safe_load).parametersifallowed_parameters.include?[:key,:permitted_classes]::YAML.safe_load(content,permitted_classes: permitted_classes,aliases: true)elsifallowed_parameters.include?[:key,:whitelist_classes]::YAML.safe_load(content,whitelist_classes: permitted_classes,aliases: true)else::YAML.safe_load(content,permitted_classes,[],true)endendrescueStandardError,::Psych::SyntaxError=>errorwarn"YAML Exception parsing #{full_path}: #{error.message}"{}endc?symbolize_recursive(c):{}endmemoize:parse_yaml# Parse TOML frontmatter out of a string# @param [String] content# @return [Hash]ContractString,Pathname=>Hashdefparse_toml(content,full_path)c=begin::Middleman::Util.instrument'parse.toml'do::TOML.load(content)endrescueStandardError# TOML parser swallows useful error, so we can't warn about it.# https://github.com/jm/toml/issues/47warn"TOML Exception parsing #{full_path}"{}endc?symbolize_recursive(c):{}endmemoize:parse_yaml# Parse JSON frontmatter out of a string# @param [String] content# @return [Hash]ContractString,Pathname=>Hashdefparse_json(content,full_path)c=begin::Middleman::Util.instrument'parse.json'do::JSON.parse(content)endrescueStandardError=>errorwarn"JSON Exception parsing #{full_path}: #{error.message}"{}endc?symbolize_recursive(c):{}endmemoize:parse_jsondefsymbolize_recursive(value)casevaluewhenHashvalue.mapdo|k,v|key=k.is_a?(String)?k.to_sym:k[key,symbolize_recursive(v)]end.to_hwhenArrayvalue.map{|v|symbolize_recursive(v)}elsevalueendendendendend