Always open index page with `serve --open`
This commit is contained in:
parent
a10a57e67d
commit
42129c6181
|
@ -1,8 +1,7 @@
|
||||||
use crate::{first_chapter, get_book_dir, open};
|
use crate::{get_book_dir, open};
|
||||||
use clap::{arg, App, Arg, ArgMatches};
|
use clap::{arg, App, Arg, ArgMatches};
|
||||||
use mdbook::errors::Result;
|
use mdbook::errors::Result;
|
||||||
use mdbook::MDBook;
|
use mdbook::MDBook;
|
||||||
use std::path::Path;
|
|
||||||
|
|
||||||
// Create clap subcommand arguments
|
// Create clap subcommand arguments
|
||||||
pub fn make_subcommand<'help>() -> App<'help> {
|
pub fn make_subcommand<'help>() -> App<'help> {
|
||||||
|
@ -39,15 +38,12 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
|
||||||
|
|
||||||
if args.is_present("open") {
|
if args.is_present("open") {
|
||||||
// FIXME: What's the right behaviour if we don't use the HTML renderer?
|
// FIXME: What's the right behaviour if we don't use the HTML renderer?
|
||||||
match first_chapter(&book)
|
let path = book.build_dir_for("html").join("index.html");
|
||||||
.map(|path| book.build_dir_for("html").join(path).with_extension("html"))
|
if !path.exists() {
|
||||||
{
|
|
||||||
Some(path) if Path::new(&path).exists() => open(path),
|
|
||||||
_ => {
|
|
||||||
error!("No chapter available to open");
|
error!("No chapter available to open");
|
||||||
std::process::exit(1)
|
std::process::exit(1)
|
||||||
}
|
}
|
||||||
}
|
open(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
#[cfg(feature = "watch")]
|
#[cfg(feature = "watch")]
|
||||||
use super::watch;
|
use super::watch;
|
||||||
use crate::{first_chapter, get_book_dir, open};
|
use crate::{get_book_dir, open};
|
||||||
use clap::{arg, App, Arg, ArgMatches};
|
use clap::{arg, App, Arg, ArgMatches};
|
||||||
use futures_util::sink::SinkExt;
|
use futures_util::sink::SinkExt;
|
||||||
use futures_util::StreamExt;
|
use futures_util::StreamExt;
|
||||||
|
@ -103,10 +103,7 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
|
||||||
});
|
});
|
||||||
|
|
||||||
if open_browser {
|
if open_browser {
|
||||||
let serving_url = match first_chapter(&book).map(|path| path.with_extension("html")) {
|
let serving_url = format!("http://{}", address);
|
||||||
Some(path) => format!("http://{}/{}", address, path.display()),
|
|
||||||
_ => format!("http://{}", address),
|
|
||||||
};
|
|
||||||
info!("Serving on: {}", serving_url);
|
info!("Serving on: {}", serving_url);
|
||||||
open(serving_url);
|
open(serving_url);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
use crate::first_chapter;
|
|
||||||
use crate::{get_book_dir, open};
|
use crate::{get_book_dir, open};
|
||||||
use clap::{arg, App, Arg, ArgMatches};
|
use clap::{arg, App, Arg, ArgMatches};
|
||||||
use mdbook::errors::Result;
|
use mdbook::errors::Result;
|
||||||
|
@ -46,12 +45,12 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
|
||||||
|
|
||||||
if args.is_present("open") {
|
if args.is_present("open") {
|
||||||
book.build()?;
|
book.build()?;
|
||||||
match first_chapter(&book)
|
let path = book.build_dir_for("html").join("index.html");
|
||||||
.map(|path| book.build_dir_for("html").join(path).with_extension("html"))
|
if !path.exists() {
|
||||||
{
|
error!("No chapter available to open");
|
||||||
Some(path) if Path::new(&path).exists() => open(path),
|
std::process::exit(1)
|
||||||
_ => warn!("No chapter available to open"),
|
|
||||||
}
|
}
|
||||||
|
open(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
trigger_on_change(&book, |paths, book_dir| {
|
trigger_on_change(&book, |paths, book_dir| {
|
||||||
|
|
14
src/main.rs
14
src/main.rs
|
@ -9,10 +9,7 @@ use clap::{App, AppSettings, Arg, ArgMatches};
|
||||||
use clap_complete::Shell;
|
use clap_complete::Shell;
|
||||||
use env_logger::Builder;
|
use env_logger::Builder;
|
||||||
use log::LevelFilter;
|
use log::LevelFilter;
|
||||||
use mdbook::book::Chapter;
|
|
||||||
use mdbook::utils;
|
use mdbook::utils;
|
||||||
use mdbook::BookItem;
|
|
||||||
use mdbook::MDBook;
|
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::ffi::OsStr;
|
use std::ffi::OsStr;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
@ -140,17 +137,6 @@ 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) {
|
fn open<P: AsRef<OsStr>>(path: P) {
|
||||||
info!("Opening web browser");
|
info!("Opening web browser");
|
||||||
if let Err(e) = opener::open(path) {
|
if let Err(e) = opener::open(path) {
|
||||||
|
|
Loading…
Reference in New Issue