Skip to content

Commit 1377cf2

Browse files
authored
Backport utf8-bom from #6322 to 3.6-stable (#6451)
1 parent de4007c commit 1377cf2

6 files changed

Lines changed: 53 additions & 4 deletions

File tree

lib/jekyll/site.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,7 @@ def configure_include_paths
444444
def configure_file_read_opts
445445
self.file_read_opts = {}
446446
self.file_read_opts[:encoding] = config["encoding"] if config["encoding"]
447+
self.file_read_opts = Jekyll::Utils.merged_file_read_opts(self, {})
447448
end
448449

449450
private

lib/jekyll/utils.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,9 @@ def safe_glob(dir, patterns, flags = 0)
301301
# and a given param
302302
def merged_file_read_opts(site, opts)
303303
merged = (site ? site.file_read_opts : {}).merge(opts)
304+
if merged[:encoding] && !merged[:encoding].start_with?("bom|")
305+
merged[:encoding] = "bom|#{merged[:encoding]}"
306+
end
304307
if merged["encoding"] && !merged["encoding"].start_with?("bom|")
305308
merged["encoding"] = "bom|#{merged["encoding"]}"
306309
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
layout: post
3+
title: "UTF8CRLFandBOM"
4+
date: 2017-04-05 16:16:01 -0800
5+
categories: bom
6+
---
7+
This file was created with CR/LFs, and encoded as UTF8 with a BOM
8+
9+
You’ll find this post in your `_posts` directory. Go ahead and edit it and re-build the site to see your changes. You can rebuild the site in many different ways, but the most common way is to run `bundle exec jekyll serve`, which launches a web server and auto-regenerates your site when a file is updated.
10+
11+
To add new posts, simply add a file in the `_posts` directory that follows the convention `YYYY-MM-DD-name-of-post.ext` and includes the necessary front matter. Take a look at the source for this post to get an idea about how it works.
1.52 KB

layout: post title: "Unicode16CRLFandBOM" date: 2017-04-05 16:08:01 -0800 categories: bom

This file was created with CR/LFs, and encoded as Unicode in Notepad, which is Unicode 16, Little Endian and adds a BOM.

You’ll find this post in your _posts directory. Go ahead and edit it and re-build the site to see your changes. You can rebuild the site in many different ways, but the most common way is to run bundle exec jekyll serve, which launches a web server and auto-regenerates your site when a file is updated.

To add new posts, simply add a file in the _posts directory that follows the convention YYYY-MM-DD-name-of-post.ext and includes the necessary front matter. Take a look at the source for this post to get an idea about how it works.

test/test_document.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ def assert_equal_value(key, one, other)
77
assert_equal(one[key], other[key])
88
end
99

10+
def setup_encoded_document(filename)
11+
site = fixture_site("collections" => ["encodings"])
12+
site.process
13+
Document.new(site.in_source_dir(File.join("_encodings", filename)), {
14+
:site => site,
15+
:collection => site.collections["encodings"],
16+
}).tap(&:read)
17+
end
18+
1019
context "a document in a collection" do
1120
setup do
1221
@site = fixture_site({
@@ -529,4 +538,24 @@ def assert_equal_value(key, one, other)
529538
assert_equal true, File.file?(@dest_file)
530539
end
531540
end
541+
542+
context "a document with UTF-8 CLRF" do
543+
setup do
544+
@document = setup_encoded_document "UTF8CRLFandBOM.md"
545+
end
546+
547+
should "not throw an error" do
548+
Jekyll::Renderer.new(@document.site, @document).render_document
549+
end
550+
end
551+
552+
context "a document with UTF-16LE CLRF" do
553+
setup do
554+
@document = setup_encoded_document "Unicode16LECRLFandBOM.md"
555+
end
556+
557+
should "not throw an error" do
558+
Jekyll::Renderer.new(@document.site, @document).render_document
559+
end
560+
end
532561
end

test/test_utils.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -386,16 +386,21 @@ class TestUtils < JekyllUnitTest
386386
should "ignore encoding if it's not there" do
387387
opts = Utils.merged_file_read_opts(nil, {})
388388
assert_nil opts["encoding"]
389+
assert_nil opts[:encoding]
389390
end
390391

391392
should "add bom to encoding" do
392-
opts = Utils.merged_file_read_opts(nil, { "encoding" => "utf-8" })
393-
assert_equal "bom|utf-8", opts["encoding"]
393+
opts = { "encoding" => "utf-8", :encoding => "utf-8" }
394+
merged = Utils.merged_file_read_opts(nil, opts)
395+
assert_equal "bom|utf-8", merged["encoding"]
396+
assert_equal "bom|utf-8", merged[:encoding]
394397
end
395398

396399
should "preserve bom in encoding" do
397-
opts = Utils.merged_file_read_opts(nil, { "encoding" => "bom|utf-8" })
398-
assert_equal "bom|utf-8", opts["encoding"]
400+
opts = { "encoding" => "bom|another", :encoding => "bom|another" }
401+
merged = Utils.merged_file_read_opts(nil, opts)
402+
assert_equal "bom|another", merged["encoding"]
403+
assert_equal "bom|another", merged[:encoding]
399404
end
400405
end
401406
end

0 commit comments

Comments
 (0)