This does a little cleanup around the usage of filesystem functions: - Add `mdbook_core::utils::fs::read_to_string` as a wrapper around `std::fs::read_to_string` to provide better error messages. Use this wherever a file is read. - Add `mdbook_core::utils::fs::create_dir_all` as a wrapper around `std::fs::create_dir_all` to provide better error messages. Use this wherever a file is read. - Replace `mdbook_core::utils::fs::write_file` with `write` to mirror the `std::fs::write` API. - Remove `mdbook_core::utils::fs::create_file`. It was generally not used anymore. - Scrub the usage of `std::fs` to use the new wrappers. This doesn't remove it 100%, but it is now significantly reduced.
46 lines
1.2 KiB
Rust
46 lines
1.2 KiB
Rust
use anyhow::{Context, Result};
|
|
use mdbook_core::utils::fs;
|
|
use mdbook_renderer::{RenderContext, Renderer};
|
|
use tracing::trace;
|
|
|
|
/// A renderer to output the Markdown after the preprocessors have run. Mostly useful
|
|
/// when debugging preprocessors.
|
|
#[derive(Default)]
|
|
#[non_exhaustive]
|
|
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() {
|
|
fs::remove_dir_content(destination)
|
|
.with_context(|| "Unable to remove stale Markdown output")?;
|
|
}
|
|
|
|
trace!("markdown render");
|
|
for ch in book.chapters() {
|
|
let path = ctx
|
|
.destination
|
|
.join(ch.path.as_ref().expect("Checked path exists before"));
|
|
fs::write(path, &ch.content)?;
|
|
}
|
|
|
|
fs::create_dir_all(destination)
|
|
.with_context(|| "Unexpected error when constructing destination path")?;
|
|
|
|
Ok(())
|
|
}
|
|
}
|