From 8b1fc4b842929961f2f6a09d7c44ec010150ed77 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 24 Nov 2017 23:12:09 +0100 Subject: [PATCH 1/2] Generate difference warnings for markdown files as well --- src/librustdoc/clean/mod.rs | 2 +- src/librustdoc/html/render.rs | 7 +++-- src/librustdoc/markdown.rs | 53 +++++++++++++++++++++++++++++++---- 3 files changed, 53 insertions(+), 9 deletions(-) diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index be7bd3d5510ef..fbff6e83fb911 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -2521,7 +2521,7 @@ pub struct Span { } impl Span { - fn empty() -> Span { + pub fn empty() -> Span { Span { filename: "".to_string(), loline: 0, locol: 0, diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index 9dc01bb0916f5..8478fd6c5fdc7 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -421,7 +421,7 @@ impl ToJson for IndexItemFunctionType { thread_local!(static CACHE_KEY: RefCell> = Default::default()); thread_local!(pub static CURRENT_LOCATION_KEY: RefCell> = RefCell::new(Vec::new())); -thread_local!(static USED_ID_MAP: RefCell> = +thread_local!(pub static USED_ID_MAP: RefCell> = RefCell::new(init_ids())); fn init_ids() -> FxHashMap { @@ -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: `{}`", diff --git a/src/librustdoc/markdown.rs b/src/librustdoc/markdown.rs index fe6bd985bb618..1fdfa17a8dd34 100644 --- a/src/librustdoc/markdown.rs +++ b/src/librustdoc/markdown.rs @@ -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#" @@ -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, } } From eb84f4243fa7e18f97ee39f6c0b751f4c329a4ee Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 24 Nov 2017 23:12:35 +0100 Subject: [PATCH 2/2] fix markdown file differences --- src/doc/not_found.md | 14 +++++++------- src/librustdoc/html/render.rs | 17 +++++++++++------ src/librustdoc/markdown.rs | 16 ++++------------ 3 files changed, 22 insertions(+), 25 deletions(-) diff --git a/src/doc/not_found.md b/src/doc/not_found.md index ebe7c59313fa5..f404aa046c1ae 100644 --- a/src/doc/not_found.md +++ b/src/doc/not_found.md @@ -13,20 +13,20 @@ Some things that might be helpful to you though: # Search -*
+ - -
-* Rust doc search: + + +Rust doc search: # Reference -* [The Rust official site](https://www.rust-lang.org) -* [The Rust reference](https://doc.rust-lang.org/reference/index.html) + * [The Rust official site](https://www.rust-lang.org) + * [The Rust reference](https://doc.rust-lang.org/reference/index.html) # Docs -* [The standard library](https://doc.rust-lang.org/std/) +[The standard library](https://doc.rust-lang.org/std/)