From 65d9eb6f7e01e298ed52db0c2fb2bd7a8b48ecbd Mon Sep 17 00:00:00 2001 From: FrankHB Date: Sat, 25 Jul 2020 13:02:44 +0800 Subject: Handled UTF-8 BOM Fixed #1155 . --- src/book/book.rs | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/book') diff --git a/src/book/book.rs b/src/book/book.rs index 071d1589..369c6a07 100644 --- a/src/book/book.rs +++ b/src/book/book.rs @@ -264,6 +264,10 @@ fn load_chapter>( format!("Unable to read \"{}\" ({})", link.name, location.display()) })?; + if content.as_bytes().starts_with(b"\xef\xbb\xbf") { + content = content[3..].to_string() + } + let stripped = location .strip_prefix(&src_dir) .expect("Chapters are always inside a book"); -- cgit v1.2.3 From 780fb979a0462feca19ba3bb6d0e3c1ccc7fe9dc Mon Sep 17 00:00:00 2001 From: FrankHB Date: Tue, 29 Sep 2020 18:01:06 +0800 Subject: Avoided the redundant allocation. Signed-off-by: FrankHB --- src/book/book.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/book') diff --git a/src/book/book.rs b/src/book/book.rs index 369c6a07..09b51888 100644 --- a/src/book/book.rs +++ b/src/book/book.rs @@ -265,7 +265,7 @@ fn load_chapter>( })?; if content.as_bytes().starts_with(b"\xef\xbb\xbf") { - content = content[3..].to_string() + content.replace_range(..3, ""); } let stripped = location -- cgit v1.2.3 From 9e9cf49c503a9d7a47ec57457d0d5875f100b987 Mon Sep 17 00:00:00 2001 From: FrankHB Date: Wed, 7 Oct 2020 22:50:25 +0800 Subject: Added a test. Signed-off-by: FrankHB --- src/book/book.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'src/book') diff --git a/src/book/book.rs b/src/book/book.rs index 09b51888..35f66438 100644 --- a/src/book/book.rs +++ b/src/book/book.rs @@ -397,6 +397,29 @@ And here is some \ assert_eq!(got, should_be); } + #[test] + fn load_a_single_chapter_with_utf8_bom_from_disk() { + let temp_dir = TempFileBuilder::new().prefix("book").tempdir().unwrap(); + + let chapter_path = temp_dir.path().join("chapter_1.md"); + File::create(&chapter_path) + .unwrap() + .write_all(("\u{feff}".to_owned() + DUMMY_SRC).as_bytes()) + .unwrap(); + + let link = Link::new("Chapter 1", chapter_path); + + let should_be = Chapter::new( + "Chapter 1", + DUMMY_SRC.to_string(), + "chapter_1.md", + Vec::new(), + ); + + let got = load_chapter(&link, temp_dir.path(), Vec::new()).unwrap(); + assert_eq!(got, should_be); + } + #[test] fn cant_load_a_nonexistent_chapter() { let link = Link::new("Chapter 1", "/foo/bar/baz.md"); -- cgit v1.2.3