Change `init --theme` to place theme in root. (#1432)

This commit is contained in:
Eric Huss 2021-01-06 16:29:38 -08:00 committed by GitHub
parent 76cd39e5e2
commit 77e7cfd22b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 13 deletions

View File

@ -110,10 +110,7 @@ impl BookBuilder {
debug!("Copying theme"); debug!("Copying theme");
let html_config = self.config.html_config().unwrap_or_default(); let html_config = self.config.html_config().unwrap_or_default();
let themedir = html_config let themedir = html_config.theme_dir(&self.root);
.theme
.unwrap_or_else(|| self.config.book.src.join("theme"));
let themedir = self.root.join(themedir);
if !themedir.exists() { if !themedir.exists() {
debug!( debug!(
@ -127,7 +124,9 @@ impl BookBuilder {
index.write_all(theme::INDEX)?; index.write_all(theme::INDEX)?;
let cssdir = themedir.join("css"); let cssdir = themedir.join("css");
if !cssdir.exists() {
fs::create_dir(&cssdir)?; fs::create_dir(&cssdir)?;
}
let mut general_css = File::create(cssdir.join("general.css"))?; let mut general_css = File::create(cssdir.join("general.css"))?;
general_css.write_all(theme::GENERAL_CSS)?; general_css.write_all(theme::GENERAL_CSS)?;

View File

@ -28,15 +28,11 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
// If flag `--theme` is present, copy theme to src // If flag `--theme` is present, copy theme to src
if args.is_present("theme") { if args.is_present("theme") {
config.set("output.html.theme", "src/theme")?; let theme_dir = book_dir.join("theme");
// Skip this if `--force` is present
if !args.is_present("force") {
// Print warning
println!(); println!();
println!( println!("Copying the default theme to {}", theme_dir.display());
"Copying the default theme to {}", // Skip this if `--force` is present
builder.config().book.src.display() if !args.is_present("force") && theme_dir.exists() {
);
println!("This could potentially overwrite files already present in that directory."); println!("This could potentially overwrite files already present in that directory.");
print!("\nAre you sure you want to continue? (y/n) "); print!("\nAre you sure you want to continue? (y/n) ");

View File

@ -108,3 +108,37 @@ fn book_toml_isnt_required() {
md.build().unwrap(); md.build().unwrap();
} }
#[test]
fn copy_theme() {
let temp = TempFileBuilder::new().prefix("mdbook").tempdir().unwrap();
MDBook::init(temp.path()).copy_theme(true).build().unwrap();
let expected = vec![
"book.js",
"css/chrome.css",
"css/general.css",
"css/print.css",
"css/variables.css",
"favicon.png",
"favicon.svg",
"highlight.css",
"highlight.js",
"index.hbs",
];
let theme_dir = temp.path().join("theme");
let mut actual: Vec<_> = walkdir::WalkDir::new(&theme_dir)
.into_iter()
.filter_map(|e| e.ok())
.filter(|e| !e.file_type().is_dir())
.map(|e| {
e.path()
.strip_prefix(&theme_dir)
.unwrap()
.to_str()
.unwrap()
.replace('\\', "/")
})
.collect();
actual.sort();
assert_eq!(actual, expected);
}