From 1cacef025de3bf9e3d6620d68a4d60ac37b61f33 Mon Sep 17 00:00:00 2001 From: josh rotenberg Date: Tue, 28 Dec 2021 21:00:06 -0800 Subject: [PATCH 1/5] check for the index.html file first --- src/cmd/build.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/cmd/build.rs b/src/cmd/build.rs index d1c66302..e9112f9b 100644 --- a/src/cmd/build.rs +++ b/src/cmd/build.rs @@ -2,6 +2,7 @@ use crate::{get_book_dir, open}; use clap::{App, ArgMatches, SubCommand}; use mdbook::errors::Result; use mdbook::MDBook; +use std::path::Path; // Create clap subcommand arguments pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> { @@ -32,6 +33,12 @@ 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? + let path = book.build_dir_for("html").join("index.html"); + if !Path::new(&path).exists() { + error!("Need a more descriptive error here: {:?}", path); + std::process::exit(1); + } + open(book.build_dir_for("html").join("index.html")); } From 5d6596744856db513f4115ce712a34cf8c4971be Mon Sep 17 00:00:00 2001 From: josh rotenberg Date: Tue, 10 May 2022 11:16:22 -0700 Subject: [PATCH 2/5] add first_chapter function --- src/main.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/main.rs b/src/main.rs index 35562e64..f993d475 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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; @@ -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>(path: P) { info!("Opening web browser"); if let Err(e) = opener::open(path) { From 29c729fd2323d9ef6022fea881f6ac4d9223f2ef Mon Sep 17 00:00:00 2001 From: josh rotenberg Date: Tue, 10 May 2022 11:17:20 -0700 Subject: [PATCH 3/5] call first_chapter --- src/cmd/build.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/cmd/build.rs b/src/cmd/build.rs index dacf45cf..c0a28ec0 100644 --- a/src/cmd/build.rs +++ b/src/cmd/build.rs @@ -1,4 +1,4 @@ -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; @@ -39,13 +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? - let path = book.build_dir_for("html").join("index.html"); - if !Path::new(&path).exists() { - error!("Need a more descriptive error here: {:?}", path); - std::process::exit(1); + 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) + } } - - open(book.build_dir_for("html").join("index.html")); } Ok(()) From 8b49600673765a56b5d6414cf580d4ac63778eb3 Mon Sep 17 00:00:00 2001 From: josh rotenberg Date: Tue, 10 May 2022 11:19:23 -0700 Subject: [PATCH 4/5] call first_chapter --- src/cmd/watch.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/cmd/watch.rs b/src/cmd/watch.rs index 78ae1968..2effd29f 100644 --- a/src/cmd/watch.rs +++ b/src/cmd/watch.rs @@ -1,3 +1,4 @@ +use crate::first_chapter; use crate::{get_book_dir, open}; use clap::{arg, App, Arg, ArgMatches}; use mdbook::errors::Result; @@ -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| { From c74c6829391502eeff4ae90f7dae2024780bd071 Mon Sep 17 00:00:00 2001 From: josh rotenberg Date: Wed, 11 May 2022 13:14:38 -0700 Subject: [PATCH 5/5] call find_chapter when opening browser --- src/cmd/serve.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/cmd/serve.rs b/src/cmd/serve.rs index bafbfd52..04ee556c 100644 --- a/src/cmd/serve.rs +++ b/src/cmd/serve.rs @@ -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; @@ -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); }