Skip to content

Commit f878266

Browse files
authored
Merge pull request #1714 from joshrotenberg/draft-no-index
Check for the index.html file before trying to opener::open it
2 parents bf258ee + c74c682 commit f878266

File tree

4 files changed

+38
-7
lines changed

4 files changed

+38
-7
lines changed

src/cmd/build.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
use crate::{get_book_dir, open};
1+
use crate::{first_chapter, get_book_dir, open};
22
use clap::{arg, App, Arg, ArgMatches};
33
use mdbook::errors::Result;
44
use mdbook::MDBook;
5+
use std::path::Path;
56

67
// Create clap subcommand arguments
78
pub fn make_subcommand<'help>() -> App<'help> {
@@ -38,7 +39,15 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
3839

3940
if args.is_present("open") {
4041
// FIXME: What's the right behaviour if we don't use the HTML renderer?
41-
open(book.build_dir_for("html").join("index.html"));
42+
match first_chapter(&book)
43+
.map(|path| book.build_dir_for("html").join(path).with_extension("html"))
44+
{
45+
Some(path) if Path::new(&path).exists() => open(path),
46+
_ => {
47+
error!("No chapter available to open");
48+
std::process::exit(1)
49+
}
50+
}
4251
}
4352

4453
Ok(())

src/cmd/serve.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#[cfg(feature = "watch")]
22
use super::watch;
3-
use crate::{get_book_dir, open};
3+
use crate::{first_chapter, get_book_dir, open};
44
use clap::{arg, App, Arg, ArgMatches};
55
use futures_util::sink::SinkExt;
66
use futures_util::StreamExt;
@@ -102,10 +102,12 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
102102
serve(build_dir, sockaddr, reload_tx, &file_404);
103103
});
104104

105-
let serving_url = format!("http://{}", address);
106-
info!("Serving on: {}", serving_url);
107-
108105
if open_browser {
106+
let serving_url = match first_chapter(&book).map(|path| path.with_extension("html")) {
107+
Some(path) => format!("http://{}/{}", address, path.display()),
108+
_ => format!("http://{}", address),
109+
};
110+
info!("Serving on: {}", serving_url);
109111
open(serving_url);
110112
}
111113

src/cmd/watch.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::first_chapter;
12
use crate::{get_book_dir, open};
23
use clap::{arg, App, Arg, ArgMatches};
34
use mdbook::errors::Result;
@@ -45,7 +46,12 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
4546

4647
if args.is_present("open") {
4748
book.build()?;
48-
open(book.build_dir_for("html").join("index.html"));
49+
match first_chapter(&book)
50+
.map(|path| book.build_dir_for("html").join(path).with_extension("html"))
51+
{
52+
Some(path) if Path::new(&path).exists() => open(path),
53+
_ => warn!("No chapter available to open"),
54+
}
4955
}
5056

5157
trigger_on_change(&book, |paths, book_dir| {

src/main.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ use clap::{App, AppSettings, Arg, ArgMatches};
99
use clap_complete::Shell;
1010
use env_logger::Builder;
1111
use log::LevelFilter;
12+
use mdbook::book::Chapter;
1213
use mdbook::utils;
14+
use mdbook::BookItem;
15+
use mdbook::MDBook;
1316
use std::env;
1417
use std::ffi::OsStr;
1518
use std::io::Write;
@@ -137,6 +140,17 @@ fn get_book_dir(args: &ArgMatches) -> PathBuf {
137140
}
138141
}
139142

143+
// Return the first displayable chapter of the given book, or None if no displayable
144+
// chapter is found (i.e. only drafts).
145+
fn first_chapter(book: &MDBook) -> Option<&PathBuf> {
146+
book.iter().find_map(|item| match item {
147+
BookItem::Chapter(Chapter {
148+
path: Some(path), ..
149+
}) => Some(path),
150+
_ => None,
151+
})
152+
}
153+
140154
fn open<P: AsRef<OsStr>>(path: P) {
141155
info!("Opening web browser");
142156
if let Err(e) = opener::open(path) {

0 commit comments

Comments
 (0)