diff --git a/middleman-core/lib/middleman-core/core_extensions/front_matter.rb b/middleman-core/lib/middleman-core/core_extensions/front_matter.rb index 3b3e3935..7460dca9 100644 --- a/middleman-core/lib/middleman-core/core_extensions/front_matter.rb +++ b/middleman-core/lib/middleman-core/core_extensions/front_matter.rb @@ -130,23 +130,27 @@ module Middleman::CoreExtensions # @return [Array] def frontmatter_and_content(path) full_path = File.expand_path(File.join(@app.source_dir, path)) - content = File.read(full_path) data = {} + content = nil - begin - if content =~ /\A.*coding:/ - lines = content.split(/\n/) - lines.shift - content = lines.join("\n") - end + if !::Middleman::Util.binary?(full_path) + content = File.read(full_path) + + begin + if content =~ /\A.*coding:/ + lines = content.split(/\n/) + lines.shift + content = lines.join("\n") + end - if result = parse_yaml_front_matter(content) - data, content = result - elsif result = parse_json_front_matter(content) - data, content = result + if result = parse_yaml_front_matter(content) + data, content = result + elsif result = parse_json_front_matter(content) + data, content = result + end + rescue => e + # Probably a binary file, move on end - rescue => e - # Probably a binary file, move on end [data, content] diff --git a/middleman-core/lib/middleman-core/sitemap/resource.rb b/middleman-core/lib/middleman-core/sitemap/resource.rb index 6c14fca5..87bb610e 100644 --- a/middleman-core/lib/middleman-core/sitemap/resource.rb +++ b/middleman-core/lib/middleman-core/sitemap/resource.rb @@ -151,8 +151,7 @@ module Middleman # # @retrun [Boolean] def binary? - s = (File.read(source_file, File.stat(source_file).blksize) || "").split(//) - ((s.size - s.grep(" ".."~").size) / s.size.to_f) > 0.30 + ::Middleman::Util.binary?(source_file) end end end diff --git a/middleman-core/lib/middleman-core/util.rb b/middleman-core/lib/middleman-core/util.rb index 6702c73d..b4b543fe 100644 --- a/middleman-core/lib/middleman-core/util.rb +++ b/middleman-core/lib/middleman-core/util.rb @@ -10,10 +10,21 @@ require "thor" # Core Pathname library used for traversal require "pathname" +require 'win32/file' if File::ALT_SEPARATOR + module Middleman module Util + # Whether the source file is binary. + # + # @param [String] filename The file to check. + # @return [Boolean] + def self.binary?(filename) + s = (File.read(filename, File.stat(filename).blksize) || "").split(//) + ((s.size - s.grep(" ".."~").size) / s.size.to_f) > 0.30 + end + # The logger # # @return [Middleman::Logger] The logger