forked from rust-lang/mdBook
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement a markdown renderer (rust-lang#1018)
Use case: when trying to `mdbook test` a file that has many `include` directives, and a test fails, the line numbers in the `rustdoc` output don't match the line numbers in the original markdown file. Turning on the markdown renderer implemented here lets you see what is being passed to `rustdoc` by saving the markdown after the preprocessors have run. This renderer could be helpful for debugging many preprocessors, but it's probably not useful in the general case, so it's turned off by default.
- Loading branch information
1 parent
653db8d
commit 9552ad5
Showing
5 changed files
with
74 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
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) | ||
.chain_err(|| "Unable to remove stale Markdown output")?; | ||
} | ||
|
||
trace!("markdown render"); | ||
for item in book.iter() { | ||
if let BookItem::Chapter(ref ch) = *item { | ||
utils::fs::write_file(&ctx.destination, &ch.path, ch.content.as_bytes())?; | ||
} | ||
} | ||
|
||
fs::create_dir_all(&destination) | ||
.chain_err(|| "Unexpected error when constructing destination path")?; | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters