2019-08-30 18:20:53 +08:00
|
|
|
use crate::book::BookItem;
|
|
|
|
use crate::errors::*;
|
|
|
|
use crate::renderer::{RenderContext, Renderer};
|
|
|
|
use crate::utils;
|
|
|
|
|
|
|
|
use std::fs;
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
/// A renderer to output the Markdown after the preprocessors have run. Mostly useful
|
|
|
|
/// when debugging preprocessors.
|
|
|
|
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() {
|
|
|
|
utils::fs::remove_dir_content(destination)
|
2020-05-21 05:32:00 +08:00
|
|
|
.with_context(|| "Unable to remove stale Markdown output")?;
|
2019-08-30 18:20:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
trace!("markdown render");
|
|
|
|
for item in book.iter() {
|
|
|
|
if let BookItem::Chapter(ref ch) = *item {
|
2020-03-01 00:55:45 +08:00
|
|
|
if !ch.is_draft_chapter() {
|
|
|
|
utils::fs::write_file(
|
|
|
|
&ctx.destination,
|
|
|
|
&ch.path.as_ref().expect("Checked path exists before"),
|
|
|
|
ch.content.as_bytes(),
|
|
|
|
)?;
|
|
|
|
}
|
2019-08-30 18:20:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fs::create_dir_all(&destination)
|
2020-05-21 05:32:00 +08:00
|
|
|
.with_context(|| "Unexpected error when constructing destination path")?;
|
2019-08-30 18:20:53 +08:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|