1use crate::book::BookItem;
2use crate::errors::*;
3use crate::renderer::{RenderContext, Renderer};
4use crate::utils;
5use log::trace;
6use std::fs;
7
8#[derive(Default)]
9/// A renderer to output the Markdown after the preprocessors have run. Mostly useful
10/// when debugging preprocessors.
11pub struct MarkdownRenderer;
12
13impl MarkdownRenderer {
14 /// Create a new `MarkdownRenderer` instance.
15 pub fn new() -> Self {
16 MarkdownRenderer
17 }
18}
19
20impl Renderer for MarkdownRenderer {
21 fn name(&self) -> &str {
22 "markdown"
23 }
24
25 fn render(&self, ctx: &RenderContext) -> Result<()> {
26 let destination = &ctx.destination;
27 let book = &ctx.book;
28
29 if destination.exists() {
30 utils::fs::remove_dir_content(destination)
31 .with_context(|| "Unable to remove stale Markdown output")?;
32 }
33
34 trace!("markdown render");
35 for item in book.iter() {
36 if let BookItem::Chapter(ref ch) = *item {
37 if !ch.is_draft_chapter() {
38 utils::fs::write_file(
39 &ctx.destination,
40 ch.path.as_ref().expect("Checked path exists before"),
41 ch.content.as_bytes(),
42 )?;
43 }
44 }
45 }
46
47 fs::create_dir_all(destination)
48 .with_context(|| "Unexpected error when constructing destination path")?;
49
50 Ok(())
51 }
52}
53