add first_chapter function

This commit is contained in:
josh rotenberg 2022-05-10 11:16:22 -07:00
parent 8357811d96
commit 5d65967448
1 changed files with 14 additions and 0 deletions

View File

@ -9,7 +9,10 @@ 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;
@ -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) { 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) {