Skip to content

Commit

Permalink
Merge pull request #1285 from FrankHB/patch-1
Browse files Browse the repository at this point in the history
Handled UTF-8 BOM
  • Loading branch information
ehuss committed Nov 10, 2020
2 parents eaa6914 + 9e9cf49 commit 643d5ec
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/book/book.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,10 @@ fn load_chapter<P: AsRef<Path>>(
format!("Unable to read \"{}\" ({})", link.name, location.display())
})?;

if content.as_bytes().starts_with(b"\xef\xbb\xbf") {
content.replace_range(..3, "");
}

let stripped = location
.strip_prefix(&src_dir)
.expect("Chapters are always inside a book");
Expand Down Expand Up @@ -393,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");
Expand Down

0 comments on commit 643d5ec

Please sign in to comment.