Skip to content

Commit

Permalink
Merge pull request #2080 from t2y/fix-copy-fonts-message
Browse files Browse the repository at this point in the history
Fix handling of copy-fonts=true when fonts.css is overridden
  • Loading branch information
ehuss authored May 13, 2023
2 parents 71ea92b + 0104784 commit f4507ae
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
14 changes: 4 additions & 10 deletions src/renderer/html_handlebars/hbs_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,8 @@ impl HtmlHandlebars {
"FontAwesome/fonts/FontAwesome.ttf",
theme::FONT_AWESOME_TTF,
)?;
if html_config.copy_fonts {
// Don't copy the stock fonts if the user has specified their own fonts to use.
if html_config.copy_fonts && theme.fonts_css.is_none() {
write_file(destination, "fonts/fonts.css", theme::fonts::CSS)?;
for (file_name, contents) in theme::fonts::LICENSES.iter() {
write_file(destination, file_name, contents)?;
Expand All @@ -291,20 +292,13 @@ impl HtmlHandlebars {
}
if let Some(fonts_css) = &theme.fonts_css {
if !fonts_css.is_empty() {
if html_config.copy_fonts {
warn!(
"output.html.copy_fonts is deprecated.\n\
Set copy_fonts=false and ensure the fonts you want are in \
the `theme/fonts/` directory."
);
}
write_file(destination, "fonts/fonts.css", &fonts_css)?;
}
}
if !html_config.copy_fonts && theme.fonts_css.is_none() {
warn!(
"output.html.copy_fonts is deprecated.\n\
This book appears to have copy_fonts=false without a fonts.css file.\n\
"output.html.copy-fonts is deprecated.\n\
This book appears to have copy-fonts=false in book.toml without a fonts.css file.\n\
Add an empty `theme/fonts/fonts.css` file to squelch this warning."
);
}
Expand Down
12 changes: 6 additions & 6 deletions tests/rendered_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -891,19 +891,19 @@ fn custom_fonts() {
assert_eq!(actual_files(&p.join("book/fonts")), &builtin_fonts);
assert!(has_fonts_css(p));

// Mixed with copy_fonts=true
// This should generate a deprecation warning.
// Mixed with copy-fonts=true
// Should ignore the copy-fonts setting since the user has provided their own fonts.css.
let temp = TempFileBuilder::new().prefix("mdbook").tempdir().unwrap();
let p = temp.path();
MDBook::init(p).build().unwrap();
write_file(&p.join("theme/fonts"), "fonts.css", b"/*custom*/").unwrap();
write_file(&p.join("theme/fonts"), "myfont.woff", b"").unwrap();
MDBook::load(p).unwrap().build().unwrap();
assert!(has_fonts_css(p));
let mut expected = Vec::from(builtin_fonts);
expected.push("myfont.woff");
expected.sort();
assert_eq!(actual_files(&p.join("book/fonts")), expected.as_slice());
assert_eq!(
actual_files(&p.join("book/fonts")),
["fonts.css", "myfont.woff"]
);

// copy-fonts=false, no theme
// This should generate a deprecation warning.
Expand Down

0 comments on commit f4507ae

Please sign in to comment.