Skip to content

Commit 42129c6

Browse files
committed
Always open index page with serve --open
1 parent a10a57e commit 42129c6

File tree

4 files changed

+13
-35
lines changed

4 files changed

+13
-35
lines changed

src/cmd/build.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
use crate::{first_chapter, get_book_dir, open};
1+
use crate::{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;
65

76
// Create clap subcommand arguments
87
pub fn make_subcommand<'help>() -> App<'help> {
@@ -39,15 +38,12 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
3938

4039
if args.is_present("open") {
4140
// FIXME: What's the right behaviour if we don't use the HTML renderer?
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-
}
41+
let path = book.build_dir_for("html").join("index.html");
42+
if !path.exists() {
43+
error!("No chapter available to open");
44+
std::process::exit(1)
5045
}
46+
open(path);
5147
}
5248

5349
Ok(())

src/cmd/serve.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#[cfg(feature = "watch")]
22
use super::watch;
3-
use crate::{first_chapter, get_book_dir, open};
3+
use crate::{get_book_dir, open};
44
use clap::{arg, App, Arg, ArgMatches};
55
use futures_util::sink::SinkExt;
66
use futures_util::StreamExt;
@@ -103,10 +103,7 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
103103
});
104104

105105
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-
};
106+
let serving_url = format!("http://{}", address);
110107
info!("Serving on: {}", serving_url);
111108
open(serving_url);
112109
}

src/cmd/watch.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use crate::first_chapter;
21
use crate::{get_book_dir, open};
32
use clap::{arg, App, Arg, ArgMatches};
43
use mdbook::errors::Result;
@@ -46,12 +45,12 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
4645

4746
if args.is_present("open") {
4847
book.build()?;
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"),
48+
let path = book.build_dir_for("html").join("index.html");
49+
if !path.exists() {
50+
error!("No chapter available to open");
51+
std::process::exit(1)
5452
}
53+
open(path);
5554
}
5655

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

src/main.rs

-14
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ 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;
1312
use mdbook::utils;
14-
use mdbook::BookItem;
15-
use mdbook::MDBook;
1613
use std::env;
1714
use std::ffi::OsStr;
1815
use std::io::Write;
@@ -140,17 +137,6 @@ fn get_book_dir(args: &ArgMatches) -> PathBuf {
140137
}
141138
}
142139

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-
154140
fn open<P: AsRef<OsStr>>(path: P) {
155141
info!("Opening web browser");
156142
if let Err(e) = opener::open(path) {

0 commit comments

Comments
 (0)