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

Check for the index.html file before trying to opener::open it #1714

Merged
merged 8 commits into from
May 16, 2022
13 changes: 11 additions & 2 deletions src/cmd/build.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::{get_book_dir, open};
use crate::{first_chapter, get_book_dir, open};
use clap::{arg, App, Arg, ArgMatches};
use mdbook::errors::Result;
use mdbook::MDBook;
use std::path::Path;

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

if args.is_present("open") {
// FIXME: What's the right behaviour if we don't use the HTML renderer?
open(book.build_dir_for("html").join("index.html"));
match first_chapter(&book)
.map(|path| book.build_dir_for("html").join(path).with_extension("html"))
{
Some(path) if Path::new(&path).exists() => open(path),
_ => {
error!("No chapter available to open");
std::process::exit(1)
}
}
}

Ok(())
Expand Down
10 changes: 6 additions & 4 deletions src/cmd/serve.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[cfg(feature = "watch")]
use super::watch;
use crate::{get_book_dir, open};
use crate::{first_chapter, get_book_dir, open};
use clap::{arg, App, Arg, ArgMatches};
use futures_util::sink::SinkExt;
use futures_util::StreamExt;
Expand Down Expand Up @@ -102,10 +102,12 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
serve(build_dir, sockaddr, reload_tx, &file_404);
});

let serving_url = format!("http://{}", address);
info!("Serving on: {}", serving_url);

if open_browser {
let serving_url = match first_chapter(&book).map(|path| path.with_extension("html")) {
Some(path) => format!("http://{}/{}", address, path.display()),
_ => format!("http://{}", address),
};
info!("Serving on: {}", serving_url);
open(serving_url);
}

Expand Down
8 changes: 7 additions & 1 deletion src/cmd/watch.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::first_chapter;
use crate::{get_book_dir, open};
use clap::{arg, App, Arg, ArgMatches};
use mdbook::errors::Result;
Expand Down Expand Up @@ -45,7 +46,12 @@ pub fn execute(args: &ArgMatches) -> Result<()> {

if args.is_present("open") {
book.build()?;
open(book.build_dir_for("html").join("index.html"));
match first_chapter(&book)
.map(|path| book.build_dir_for("html").join(path).with_extension("html"))
{
Some(path) if Path::new(&path).exists() => open(path),
_ => warn!("No chapter available to open"),
}
}

trigger_on_change(&book, |paths, book_dir| {
Expand Down
14 changes: 14 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ use clap::{App, AppSettings, Arg, ArgMatches};
use clap_complete::Shell;
use env_logger::Builder;
use log::LevelFilter;
use mdbook::book::Chapter;
use mdbook::utils;
use mdbook::BookItem;
use mdbook::MDBook;
use std::env;
use std::ffi::OsStr;
use std::io::Write;
Expand Down Expand Up @@ -137,6 +140,17 @@ fn get_book_dir(args: &ArgMatches) -> PathBuf {
}
}

// Return the first displayable chapter of the given book, or None if no displayable
// chapter is found (i.e. only drafts).
fn first_chapter(book: &MDBook) -> Option<&PathBuf> {
book.iter().find_map(|item| match item {
BookItem::Chapter(Chapter {
path: Some(path), ..
}) => Some(path),
_ => None,
})
}

fn open<P: AsRef<OsStr>>(path: P) {
info!("Opening web browser");
if let Err(e) = opener::open(path) {
Expand Down