Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change init --theme to place theme in root. #1432

Merged
merged 1 commit into from
Jan 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/book/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,7 @@ impl BookBuilder {
debug!("Copying theme");

let html_config = self.config.html_config().unwrap_or_default();
let themedir = html_config
.theme
.unwrap_or_else(|| self.config.book.src.join("theme"));
let themedir = self.root.join(themedir);
let themedir = html_config.theme_dir(&self.root);

if !themedir.exists() {
debug!(
Expand All @@ -127,7 +124,9 @@ impl BookBuilder {
index.write_all(theme::INDEX)?;

let cssdir = themedir.join("css");
fs::create_dir(&cssdir)?;
if !cssdir.exists() {
fs::create_dir(&cssdir)?;
}

let mut general_css = File::create(cssdir.join("general.css"))?;
general_css.write_all(theme::GENERAL_CSS)?;
Expand Down
12 changes: 4 additions & 8 deletions src/cmd/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,11 @@ pub fn execute(args: &ArgMatches) -> Result<()> {

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

Expand Down
34 changes: 34 additions & 0 deletions tests/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,37 @@ fn book_toml_isnt_required() {

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);
}