Repository: rust-lang/mdBook (Rust)

Issue:
Existing SUMMARY.md not taken into account
I'm trying to [bootstrap a book project](https://github.com/basbossink/bisd) however when running:
```sh
mdbook init 
```
in the root directory of the clone of the above mentioned repository the `SUMMARY.md` is overwritten and no additional files appart from the empty `chapter_1.md` are created in the `src` directory. While the documentation states: 

> Tip: Generate chapters from SUMMARY.md
> When a SUMMARY.md file already exists, the init command will first parse it and generate the missing files according to the paths used in the SUMMARY.md. This allows you to think and create the whole  structure of your book and then let mdBook generate it for you.

I've tried this on both Windows and the Windows subsystem for Linux. For the later the issue is also confirmed on a debug build of `master` with `005dfc55b` as the `HEAD`. 


Discussion:
Trying to move this issue along I was investigating by diving into the source but I don't see where an existing `SUMMARY.md` file is taken into account. Shouldn't `book::init::create_stub_files` check to see if an existing `SUMMARY.md` is present before creating a boilerplate one? A proposed fix is present in #841. Please provide some feedback if this analysis is somehow misguided or incomplete.
I have run into this too.

---

I'm using `mdbook` and read <https://rust-lang-nursery.github.io/mdBook/cli/init.html#tip-generate-chapters-from-summarymd> which says:

```
When a SUMMARY.md file already exists, the init command will first parse it and generate the missing files according to the paths used in the SUMMARY.md. This allows you to think and create the whole structure of your book and then let mdBook generate it for you.
```

I have the following directory structure:

```
$ tree
.
└── SUMMARY.md

0 directories, 1 file
```

As you can see it is empty except for a `SUMMARY.md` file which has the following content:

```md
- [Terminology](terminology.md)
- [1. Playing](sect-playing/README.md)
    - [1.1 Installing OpenRA](sect-playing/ch01-installing.md)
- [2. Modding](sect-modding/README.md)
    - [2.1. Getting Started](sect-modding/ch01-getting-started/README.md)
        - [2.1.1. Clone The Source Code](sect-modding/ch01-getting-started/cloning.md)
        - [2.1.2. Run The _Red Alert_ Mod](sect-modding/ch01-getting-started/run-ra.md)
    - [2.2. MiniYaml](sect-modding/ch02-miniyaml/README.md)
        - [2.1. Syntax Basics](sect-modding/ch02-miniyaml/syntax.md)
        - [2.2. A Real-World Example](sect-modding/ch02-miniyaml/example.md)
    - [2.?. Map Making](sect-modding/chXX-map-making/README.md)
        - [2.?.1. Map Editor 101](sect-modding/chXX-map-making/editor.md)
        - [2.?.2. Map Scripting 101](sect-modding/chXX-map-making/scripting.md)
    - [2.?. Campaign Creation](sect-modding/chXX-campaigns/README.md)
        - [2.?.1. What Isn't Yet Possible](sect-modding/chXX-campaigns/what-isnt-yet-possible.md)
    - [2.?. Utility](sect-modding/chXX-utility/README.md)
        - [2.?.1 Wiki](https://github.com/OpenRA/OpenRA/wiki/Utility)
        - [2.?.? Mod & Map Linting](sect-modding/chXX-utility/linting.md)
- [3. Developing](sect-developing/README.md)
```

After running `mdbook init` I have the following directory structure:

```
$ tree
.
├── SUMMARY.md
├── book
├── book.toml
└── src
    ├── SUMMARY.md
    └── chapter_1.md

2 directories, 4 files
```

Which _does not_ have the structure I expect given the mdbook docs I quoted and the existing `SUMMARY.md` file.



Patch:
diff --git a/src/book/init.rs b/src/book/init.rs
index 4726d570a1..458928d3d4 100644
--- a/src/book/init.rs
+++ b/src/book/init.rs
@@ -176,15 +176,19 @@ impl BookBuilder {
         let src_dir = self.root.join(&self.config.book.src);
 
         let summary = src_dir.join("SUMMARY.md");
-        let mut f = File::create(&summary).chain_err(|| "Unable to create SUMMARY.md")?;
-        writeln!(f, "# Summary")?;
-        writeln!(f)?;
-        writeln!(f, "- [Chapter 1](./chapter_1.md)")?;
-
-        let chapter_1 = src_dir.join("chapter_1.md");
-        let mut f = File::create(&chapter_1).chain_err(|| "Unable to create chapter_1.md")?;
-        writeln!(f, "# Chapter 1")?;
-
+        if !summary.exists() {
+            trace!("No summary found creating stub summary and chapter_1.md.");
+            let mut f = File::create(&summary).chain_err(|| "Unable to create SUMMARY.md")?;
+            writeln!(f, "# Summary")?;
+            writeln!(f)?;
+            writeln!(f, "- [Chapter 1](./chapter_1.md)")?;
+
+            let chapter_1 = src_dir.join("chapter_1.md");
+            let mut f = File::create(&chapter_1).chain_err(|| "Unable to create chapter_1.md")?;
+            writeln!(f, "# Chapter 1")?;
+        } else {
+            trace!("Existing summary found, no need to create stub files.");
+        }
         Ok(())
     }
 


Test patch:
diff --git a/tests/init.rs b/tests/init.rs
index f48848c86d..ed9aed7e2a 100644
--- a/tests/init.rs
+++ b/tests/init.rs
@@ -4,6 +4,8 @@ extern crate tempfile;
 use mdbook::config::Config;
 use mdbook::MDBook;
 use std::fs;
+use std::fs::File;
+use std::io::prelude::*;
 use std::path::PathBuf;
 use tempfile::Builder as TempFileBuilder;
 
@@ -27,6 +29,36 @@ fn base_mdbook_init_should_create_default_content() {
     }
 }
 
+/// Run `mdbook init` in a directory containing a SUMMARY.md should create the
+/// files listed in the summary.
+#[test]
+fn run_mdbook_init_should_create_content_from_summary() {
+    let created_files = vec!["intro.md", "first.md", "outro.md"];
+
+    let temp = TempFileBuilder::new().prefix("mdbook").tempdir().unwrap();
+    let src_dir = temp.path().join("src");
+    fs::create_dir_all(src_dir.clone()).unwrap();
+    static SUMMARY: &'static str = r#"# Summary
+
+[intro](intro.md)
+
+- [First chapter](first.md)
+
+[outro](outro.md)
+
+"#;
+
+    let mut summary = File::create(src_dir.join("SUMMARY.md")).unwrap();
+    summary.write_all(SUMMARY.as_bytes()).unwrap();
+    MDBook::init(temp.path()).build().unwrap();
+
+    for file in &created_files {
+        let target = src_dir.join(file);
+        println!("{}", target.display());
+        assert!(target.exists(), "{} doesn't exist", file);
+    }
+}
+
 /// Set some custom arguments for where to place the source and destination
 /// files, then call `mdbook init`.
 #[test]