#![doc = include_str!("../README.md")]
mod angular;
pub(crate) mod codeblock;
pub(crate) mod config;
mod js;
mod markdown;
mod utils;
pub const MDBOOK_ANGULAR_VERSION: &str = env!("CARGO_PKG_VERSION");
pub const EXPECTED_MDBOOK_VERSION: &str = mdbook::MDBOOK_VERSION;
pub use angular::stop_background_process;
pub use config::Config;
use angular::build;
use log::debug;
use markdown::process_markdown;
use markdown::ChapterWithCodeBlocks;
use mdbook::{
renderer::{HtmlHandlebars, RenderContext},
BookItem, Renderer,
};
fn validate_version(ctx: &RenderContext) -> Result<()> {
let req = semver::VersionReq::parse(EXPECTED_MDBOOK_VERSION).unwrap();
if semver::Version::parse(&ctx.version).map_or(false, |version| req.matches(&version)) {
Ok(())
} else {
bail!("Invalid mdbook version {}, expected {}", &ctx.version, req);
}
}
pub(crate) use anyhow::{bail, Context, Error, Result};
pub struct AngularRenderer {}
impl Renderer for AngularRenderer {
fn name(&self) -> &str {
"angular"
}
#[inline]
fn render(&self, ctx: &RenderContext) -> Result<()> {
self.render_mut(&mut ctx.clone())
}
}
impl AngularRenderer {
pub fn new() -> Self {
Self {}
}
#[allow(clippy::missing_errors_doc)]
pub fn render_mut(&self, ctx: &mut RenderContext) -> Result<()> {
validate_version(ctx)?;
let config = Config::new(ctx)?;
let mut chapters_with_codeblocks = Vec::new();
let mut result: Result<()> = Ok(());
ctx.book.for_each_mut(|item| {
if result.is_err() {
return;
}
if let BookItem::Chapter(chapter) = item {
debug!("Processing chapter {}", &chapter.name);
match process_markdown(&config, chapter) {
Ok(processed) => {
debug!("Processed chapter {}", &chapter.name);
if let Some(processed) = processed {
chapters_with_codeblocks.push(processed);
}
}
Err(error) => result = Err(error),
};
}
});
debug!("Processed chapters");
HtmlHandlebars::new().render(ctx)?;
debug!("Finished rendering");
if !chapters_with_codeblocks.is_empty() {
build(ctx, &config, chapters_with_codeblocks)?;
}
debug!("Finished");
Ok(())
}
}
impl Default for AngularRenderer {
fn default() -> Self {
Self::new()
}
}