]> git.proxmox.com Git - rustc.git/blob - vendor/mdbook/src/renderer/markdown_renderer.rs
New upstream version 1.46.0~beta.2+dfsg1
[rustc.git] / vendor / mdbook / src / renderer / markdown_renderer.rs
1 use crate::book::BookItem;
2 use crate::errors::*;
3 use crate::renderer::{RenderContext, Renderer};
4 use crate::utils;
5
6 use std::fs;
7
8 #[derive(Default)]
9 /// A renderer to output the Markdown after the preprocessors have run. Mostly useful
10 /// when debugging preprocessors.
11 pub struct MarkdownRenderer;
12
13 impl MarkdownRenderer {
14 /// Create a new `MarkdownRenderer` instance.
15 pub fn new() -> Self {
16 MarkdownRenderer
17 }
18 }
19
20 impl 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 }