Generate difference warnings for markdown files as well

This commit is contained in:
Guillaume Gomez 2017-11-24 23:12:09 +01:00
parent 9c49f401fe
commit 8b1fc4b842
3 changed files with 53 additions and 9 deletions

View File

@ -2521,7 +2521,7 @@ pub struct Span {
}
impl Span {
fn empty() -> Span {
pub fn empty() -> Span {
Span {
filename: "".to_string(),
loline: 0, locol: 0,

View File

@ -421,7 +421,7 @@ impl ToJson for IndexItemFunctionType {
thread_local!(static CACHE_KEY: RefCell<Arc<Cache>> = Default::default());
thread_local!(pub static CURRENT_LOCATION_KEY: RefCell<Vec<String>> =
RefCell::new(Vec::new()));
thread_local!(static USED_ID_MAP: RefCell<FxHashMap<String, usize>> =
thread_local!(pub static USED_ID_MAP: RefCell<FxHashMap<String, usize>> =
RefCell::new(init_ids()));
fn init_ids() -> FxHashMap<String, usize> {
@ -699,7 +699,10 @@ fn print_message(msg: &str, intro_msg: &mut bool, span: &Span, text: &str) {
println!("{}", msg);
}
fn render_difference(diff: &html_diff::Difference, intro_msg: &mut bool, span: &Span, text: &str) {
pub fn render_difference(diff: &html_diff::Difference,
intro_msg: &mut bool,
span: &Span,
text: &str) {
match *diff {
html_diff::Difference::NodeType { ref elem, ref opposite_elem } => {
print_message(&format!(" {} Types differ: expected: `{}`, found: `{}`",

View File

@ -19,10 +19,15 @@ use rustc::session::search_paths::SearchPaths;
use rustc::session::config::Externs;
use syntax::codemap::DUMMY_SP;
use clean::Span;
use externalfiles::{ExternalHtml, LoadStringError, load_string};
use html_diff;
use html::render::reset_ids;
use html::escape::Escape;
use html::render::{USED_ID_MAP, render_difference};
use html::markdown;
use html::markdown::{Markdown, MarkdownWithToc, find_testable_code, old_find_testable_code};
use html::markdown::RenderType;
@ -52,6 +57,10 @@ fn extract_leading_metadata<'a>(s: &'a str) -> (Vec<&'a str>, &'a str) {
pub fn render(input: &str, mut output: PathBuf, matches: &getopts::Matches,
external_html: &ExternalHtml, include_toc: bool,
render_type: RenderType) -> isize {
// Span used for markdown hoedown/pulldown differences.
let mut span = Span::empty();
span.filename = input.to_owned();
let input_p = Path::new(input);
output.push(input_p.file_stem().unwrap());
output.set_extension("html");
@ -89,12 +98,44 @@ pub fn render(input: &str, mut output: PathBuf, matches: &getopts::Matches,
reset_ids(false);
let rendered = if include_toc {
format!("{}", MarkdownWithToc(text, render_type))
let (hoedown_output, pulldown_output) = if include_toc {
// Save the state of USED_ID_MAP so it only gets updated once even
// though we're rendering twice.
let orig_used_id_map = USED_ID_MAP.with(|map| map.borrow().clone());
let hoedown_output = format!("{}", MarkdownWithToc(text, RenderType::Hoedown));
USED_ID_MAP.with(|map| *map.borrow_mut() = orig_used_id_map);
let pulldown_output = format!("{}", MarkdownWithToc(text, RenderType::Pulldown));
(hoedown_output, pulldown_output)
} else {
format!("{}", Markdown(text, render_type))
// Save the state of USED_ID_MAP so it only gets updated once even
// though we're rendering twice.
let orig_used_id_map = USED_ID_MAP.with(|map| map.borrow().clone());
let hoedown_output = format!("{}", Markdown(text, RenderType::Hoedown));
USED_ID_MAP.with(|map| *map.borrow_mut() = orig_used_id_map);
let pulldown_output = format!("{}", Markdown(text, RenderType::Pulldown));
(hoedown_output, pulldown_output)
};
let mut differences = html_diff::get_differences(&pulldown_output, &hoedown_output);
differences.retain(|s| {
match *s {
html_diff::Difference::NodeText { ref elem_text,
ref opposite_elem_text,
.. }
if elem_text.split_whitespace().eq(opposite_elem_text.split_whitespace()) => {
false
}
_ => true,
}
});
if !differences.is_empty() {
let mut intro_msg = false;
for diff in differences {
render_difference(&diff, &mut intro_msg, &span, text);
}
}
let err = write!(
&mut out,
r#"<!DOCTYPE html>
@ -126,16 +167,16 @@ pub fn render(input: &str, mut output: PathBuf, matches: &getopts::Matches,
css = css,
in_header = external_html.in_header,
before_content = external_html.before_content,
text = rendered,
text = if render_type == RenderType::Pulldown { pulldown_output } else { hoedown_output },
after_content = external_html.after_content,
);
);
match err {
Err(e) => {
eprintln!("rustdoc: cannot write to `{}`: {}", output.display(), e);
6
}
Ok(_) => 0
Ok(_) => 0,
}
}