From 5d6596744856db513f4115ce712a34cf8c4971be Mon Sep 17 00:00:00 2001 From: josh rotenberg Date: Tue, 10 May 2022 11:16:22 -0700 Subject: [PATCH] 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) {