use super::config::SUPPORTED_MDBOOK_VERSION;
use crate::fatal;
use crate::preprocessor::config::PreprocessorConfig;
use log::{error, warn};
use mdbook_preprocessor::{
PreprocessorContext,
book::{Book, BookItem, Chapter},
};
use rayon::prelude::*;
use std::iter::Iterator;
pub struct BookData {
ctx: PreprocessorContext,
book: Book,
config: PreprocessorConfig,
}
impl BookData {
fn new(ctx: PreprocessorContext, book: Book, config: PreprocessorConfig) -> Self {
Self { ctx, book, config }
}
pub fn version(&self) -> &str {
&self.ctx.mdbook_version
}
pub fn is_compatible_version(&self) -> bool {
self.version() == SUPPORTED_MDBOOK_VERSION
}
pub fn emit_compatibility_warning(&self) {
if !self.is_compatible_version() {
warn!(
"This preprocessor was developed for mdbook v{}, but found v{}. May not work as expected.",
SUPPORTED_MDBOOK_VERSION,
self.version()
);
}
}
pub fn get_config(&self) -> PreprocessorConfig {
self.config.clone()
}
pub fn get_book_path(&self) -> std::path::PathBuf {
self.ctx.root.clone()
}
#[allow(dead_code)]
pub fn chapter_iter_mut(&mut self) -> impl Iterator<Item = &mut Chapter> {
self.book.items.iter_mut().filter_map(|item| {
if let BookItem::Chapter(chapter) = item {
Some(chapter)
} else {
None
}
})
}
#[cfg(feature = "sync")]
pub fn chapter_par_iter(&mut self) -> impl ParallelIterator<Item = &mut Chapter> {
self.book.items.par_iter_mut().filter_map(|item| {
if let BookItem::Chapter(chapter) = item {
Some(chapter)
} else {
None
}
})
}
pub fn into_book(self) -> Book {
self.book
}
}
pub fn get_book_data() -> BookData {
let (ctx, book) = match mdbook_preprocessor::parse_input(std::io::stdin()) {
Ok(parsed) => parsed,
Err(e) => fatal!("Input parsing failed.\nInterError: {:#?}", e),
};
let config = match ctx.config.get::<PreprocessorConfig>("preprocessor.plotly") {
Ok(Some(cfg)) => cfg,
Ok(None) => {
warn!("Custom config not found; using default configuration.");
PreprocessorConfig::default()
}
Err(e) => {
error!(
"Illegal config format for 'preprocessor.mdbook-plotly': {}",
e.root_cause()
);
PreprocessorConfig::default()
}
};
BookData::new(ctx, book, config)
}