2025-07-21 11:37:46 -07:00
|
|
|
use anyhow::{Context, Result};
|
2025-09-20 17:05:33 -07:00
|
|
|
use mdbook_core::utils::fs;
|
2025-07-21 21:12:43 -07:00
|
|
|
use mdbook_renderer::{RenderContext, Renderer};
|
2025-09-12 06:13:45 -07:00
|
|
|
use tracing::trace;
|
2019-08-30 06:20:53 -04:00
|
|
|
|
|
|
|
|
/// A renderer to output the Markdown after the preprocessors have run. Mostly useful
|
|
|
|
|
/// when debugging preprocessors.
|
2025-08-09 16:38:22 -07:00
|
|
|
#[derive(Default)]
|
|
|
|
|
#[non_exhaustive]
|
2019-08-30 06:20:53 -04:00
|
|
|
pub struct MarkdownRenderer;
|
|
|
|
|
|
|
|
|
|
impl MarkdownRenderer {
|
|
|
|
|
/// Create a new `MarkdownRenderer` instance.
|
|
|
|
|
pub fn new() -> Self {
|
|
|
|
|
MarkdownRenderer
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Renderer for MarkdownRenderer {
|
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
|
"markdown"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn render(&self, ctx: &RenderContext) -> Result<()> {
|
|
|
|
|
let destination = &ctx.destination;
|
|
|
|
|
let book = &ctx.book;
|
|
|
|
|
|
|
|
|
|
if destination.exists() {
|
2025-09-20 17:05:33 -07:00
|
|
|
fs::remove_dir_content(destination)
|
2020-05-20 14:32:00 -07:00
|
|
|
.with_context(|| "Unable to remove stale Markdown output")?;
|
2019-08-30 06:20:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
trace!("markdown render");
|
2025-09-15 07:11:19 -07:00
|
|
|
for ch in book.chapters() {
|
2025-09-20 17:05:33 -07:00
|
|
|
let path = ctx
|
|
|
|
|
.destination
|
|
|
|
|
.join(ch.path.as_ref().expect("Checked path exists before"));
|
|
|
|
|
fs::write(path, &ch.content)?;
|
2019-08-30 06:20:53 -04:00
|
|
|
}
|
|
|
|
|
|
2023-05-13 09:44:11 -07:00
|
|
|
fs::create_dir_all(destination)
|
2020-05-20 14:32:00 -07:00
|
|
|
.with_context(|| "Unexpected error when constructing destination path")?;
|
2019-08-30 06:20:53 -04:00
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|