From aad54c9a8792ccf4b606cf0d2d4309ee902c65de Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Tue, 26 Jan 2016 17:08:54 -0800 Subject: [PATCH] Add Utils.merged_file_read_opts to unify reading & strip the BOM --- lib/jekyll/convertible.rb | 8 +------- lib/jekyll/document.rb | 12 +----------- lib/jekyll/utils.rb | 10 ++++++++++ test/test_utils.rb | 17 +++++++++++++++++ 4 files changed, 29 insertions(+), 18 deletions(-) diff --git a/lib/jekyll/convertible.rb b/lib/jekyll/convertible.rb index 383bb024..f58796f0 100644 --- a/lib/jekyll/convertible.rb +++ b/lib/jekyll/convertible.rb @@ -28,12 +28,6 @@ module Jekyll !(data.key?('published') && data['published'] == false) end - # Returns merged option hash for File.read of self.site (if exists) - # and a given param - def merged_file_read_opts(opts) - (site ? site.file_read_opts : {}).merge(opts) - end - # Read the YAML frontmatter. # # base - The String path to the dir containing the file. @@ -46,7 +40,7 @@ module Jekyll begin self.content = File.read(site.in_source_dir(base, name), - merged_file_read_opts(opts)) + Utils.merged_file_read_opts(site, opts)) if content =~ /\A(---\s*\n.*?\n?)^((---|\.\.\.)\s*$\n?)/m self.content = $POSTMATCH self.data = SafeYAML.load(Regexp.last_match(1)) diff --git a/lib/jekyll/document.rb b/lib/jekyll/document.rb index 2840ba51..fc3633d8 100644 --- a/lib/jekyll/document.rb +++ b/lib/jekyll/document.rb @@ -237,16 +237,6 @@ module Jekyll trigger_hooks(:post_write) end - # Returns merged option hash for File.read of self.site (if exists) - # and a given param - # - # opts - override options - # - # Return the file read options hash. - def merged_file_read_opts(opts) - site ? site.file_read_opts.merge(opts) : opts - end - # Whether the file is published or not, as indicated in YAML front-matter # # Returns true if the 'published' key is specified in the YAML front-matter and not `false`. @@ -269,7 +259,7 @@ module Jekyll defaults = @site.frontmatter_defaults.all(url, collection.label.to_sym) merge_data!(defaults, source: "front matter defaults") unless defaults.empty? - self.content = File.read(path, merged_file_read_opts(opts)) + self.content = File.read(path, Utils.merged_file_read_opts(site, opts)) if content =~ YAML_FRONT_MATTER_REGEXP self.content = $POSTMATCH data_file = SafeYAML.load(Regexp.last_match(1)) diff --git a/lib/jekyll/utils.rb b/lib/jekyll/utils.rb index 6dd2b117..e5d45c6d 100644 --- a/lib/jekyll/utils.rb +++ b/lib/jekyll/utils.rb @@ -273,5 +273,15 @@ module Jekyll end end + # Returns merged option hash for File.read of self.site (if exists) + # and a given param + def merged_file_read_opts(site, opts) + merged = (site ? site.file_read_opts : {}).merge(opts) + if merged["encoding"] && !merged["encoding"].start_with?("bom|") + merged["encoding"].insert(0, "bom|") + end + merged + end + end end diff --git a/test/test_utils.rb b/test/test_utils.rb index d3720127..a2335391 100644 --- a/test/test_utils.rb +++ b/test/test_utils.rb @@ -276,4 +276,21 @@ class TestUtils < JekyllUnitTest refute Utils.has_yaml_header?(file) end end + + context "The \`Utils.merged_file_read_opts\` method" do + should "ignore encoding if it's not there" do + opts = Utils.merged_file_read_opts(nil, {}) + assert_nil opts["encoding"] + end + + should "add bom to encoding" do + opts = Utils.merged_file_read_opts(nil, { "encoding" => "utf-8" }) + assert_equal "bom|utf-8", opts["encoding"] + end + + should "preserve bom in encoding" do + opts = Utils.merged_file_read_opts(nil, { "encoding" => "bom|utf-8" }) + assert_equal "bom|utf-8", opts["encoding"] + end + end end