pub use self::cmd::CmdPostprocessor;
use crate::book::{Book, Chapter};
use crate::renderer::RenderContext;
use crate::Config;
use serde::{Deserialize, Serialize};
use std::cell::RefCell;
use std::collections::HashMap;
use std::path::PathBuf;
mod cmd;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PostprocessorContext {
pub root: PathBuf,
pub config: Config,
pub renderer: String,
pub mdbook_version: String,
pub render_context: RenderContext,
#[serde(skip)]
pub(crate) chapter_titles: RefCell<HashMap<PathBuf, String>>,
#[serde(skip)]
__non_exhaustive: (),
}
impl PostprocessorContext {
pub fn new(
root: PathBuf,
config: Config,
renderer: String,
render_context: RenderContext,
) -> Self {
Self {
root,
config,
renderer,
render_context,
mdbook_version: crate::MDBOOK_VERSION.to_string(),
chapter_titles: RefCell::new(HashMap::new()),
__non_exhaustive: (),
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Rendered {
Book(RenderedBook),
Document(RenderedDocumentOfBook),
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct RenderedDocumentOfBook {
pub book: Book,
pub page: RenderedDocument,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct RenderedDocument {
pub document: Document,
pub renderer: String,
pub rendered_content: String,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Document {
Chapter(Chapter),
Index,
Separator,
DocTitle(String),
Page(String),
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct RenderedBook {
pub book: Book,
pub rendered_documents: Vec<RenderedDocument>,
}
pub trait Postprocessor
where
Self: Sync + Send,
{
fn name(&self) -> &str;
fn postprocess_document(
&self,
context: &PostprocessorContext,
rendered_document: RenderedDocumentOfBook,
) -> std::result::Result<RenderedDocumentOfBook, anyhow::Error>;
fn postprocess_book(
&self,
context: &PostprocessorContext,
rendered_book: RenderedBook,
) -> std::result::Result<RenderedBook, anyhow::Error>;
fn supports_renderer(&self, renderer: &str) -> bool;
fn clone_dyn(&self) -> Box<dyn Postprocessor>;
fn to_cmd_postprocessor(&self) -> Option<CmdPostprocessor>;
}
impl Clone for Box<dyn Postprocessor> {
fn clone(&self) -> Self {
self.clone_dyn()
}
}