Skip to content

Commit 8b1fc4b

Browse files
Generate difference warnings for markdown files as well
1 parent 9c49f40 commit 8b1fc4b

File tree

3 files changed

+53
-9
lines changed

3 files changed

+53
-9
lines changed

src/librustdoc/clean/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2521,7 +2521,7 @@ pub struct Span {
25212521
}
25222522

25232523
impl Span {
2524-
fn empty() -> Span {
2524+
pub fn empty() -> Span {
25252525
Span {
25262526
filename: "".to_string(),
25272527
loline: 0, locol: 0,

src/librustdoc/html/render.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ impl ToJson for IndexItemFunctionType {
421421
thread_local!(static CACHE_KEY: RefCell<Arc<Cache>> = Default::default());
422422
thread_local!(pub static CURRENT_LOCATION_KEY: RefCell<Vec<String>> =
423423
RefCell::new(Vec::new()));
424-
thread_local!(static USED_ID_MAP: RefCell<FxHashMap<String, usize>> =
424+
thread_local!(pub static USED_ID_MAP: RefCell<FxHashMap<String, usize>> =
425425
RefCell::new(init_ids()));
426426

427427
fn init_ids() -> FxHashMap<String, usize> {
@@ -699,7 +699,10 @@ fn print_message(msg: &str, intro_msg: &mut bool, span: &Span, text: &str) {
699699
println!("{}", msg);
700700
}
701701

702-
fn render_difference(diff: &html_diff::Difference, intro_msg: &mut bool, span: &Span, text: &str) {
702+
pub fn render_difference(diff: &html_diff::Difference,
703+
intro_msg: &mut bool,
704+
span: &Span,
705+
text: &str) {
703706
match *diff {
704707
html_diff::Difference::NodeType { ref elem, ref opposite_elem } => {
705708
print_message(&format!(" {} Types differ: expected: `{}`, found: `{}`",

src/librustdoc/markdown.rs

+47-6
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,15 @@ use rustc::session::search_paths::SearchPaths;
1919
use rustc::session::config::Externs;
2020
use syntax::codemap::DUMMY_SP;
2121

22+
use clean::Span;
23+
2224
use externalfiles::{ExternalHtml, LoadStringError, load_string};
2325

26+
use html_diff;
27+
2428
use html::render::reset_ids;
2529
use html::escape::Escape;
30+
use html::render::{USED_ID_MAP, render_difference};
2631
use html::markdown;
2732
use html::markdown::{Markdown, MarkdownWithToc, find_testable_code, old_find_testable_code};
2833
use html::markdown::RenderType;
@@ -52,6 +57,10 @@ fn extract_leading_metadata<'a>(s: &'a str) -> (Vec<&'a str>, &'a str) {
5257
pub fn render(input: &str, mut output: PathBuf, matches: &getopts::Matches,
5358
external_html: &ExternalHtml, include_toc: bool,
5459
render_type: RenderType) -> isize {
60+
// Span used for markdown hoedown/pulldown differences.
61+
let mut span = Span::empty();
62+
span.filename = input.to_owned();
63+
5564
let input_p = Path::new(input);
5665
output.push(input_p.file_stem().unwrap());
5766
output.set_extension("html");
@@ -89,12 +98,44 @@ pub fn render(input: &str, mut output: PathBuf, matches: &getopts::Matches,
8998

9099
reset_ids(false);
91100

92-
let rendered = if include_toc {
93-
format!("{}", MarkdownWithToc(text, render_type))
101+
let (hoedown_output, pulldown_output) = if include_toc {
102+
// Save the state of USED_ID_MAP so it only gets updated once even
103+
// though we're rendering twice.
104+
let orig_used_id_map = USED_ID_MAP.with(|map| map.borrow().clone());
105+
let hoedown_output = format!("{}", MarkdownWithToc(text, RenderType::Hoedown));
106+
USED_ID_MAP.with(|map| *map.borrow_mut() = orig_used_id_map);
107+
let pulldown_output = format!("{}", MarkdownWithToc(text, RenderType::Pulldown));
108+
(hoedown_output, pulldown_output)
94109
} else {
95-
format!("{}", Markdown(text, render_type))
110+
// Save the state of USED_ID_MAP so it only gets updated once even
111+
// though we're rendering twice.
112+
let orig_used_id_map = USED_ID_MAP.with(|map| map.borrow().clone());
113+
let hoedown_output = format!("{}", Markdown(text, RenderType::Hoedown));
114+
USED_ID_MAP.with(|map| *map.borrow_mut() = orig_used_id_map);
115+
let pulldown_output = format!("{}", Markdown(text, RenderType::Pulldown));
116+
(hoedown_output, pulldown_output)
96117
};
97118

119+
let mut differences = html_diff::get_differences(&pulldown_output, &hoedown_output);
120+
differences.retain(|s| {
121+
match *s {
122+
html_diff::Difference::NodeText { ref elem_text,
123+
ref opposite_elem_text,
124+
.. }
125+
if elem_text.split_whitespace().eq(opposite_elem_text.split_whitespace()) => {
126+
false
127+
}
128+
_ => true,
129+
}
130+
});
131+
132+
if !differences.is_empty() {
133+
let mut intro_msg = false;
134+
for diff in differences {
135+
render_difference(&diff, &mut intro_msg, &span, text);
136+
}
137+
}
138+
98139
let err = write!(
99140
&mut out,
100141
r#"<!DOCTYPE html>
@@ -126,16 +167,16 @@ pub fn render(input: &str, mut output: PathBuf, matches: &getopts::Matches,
126167
css = css,
127168
in_header = external_html.in_header,
128169
before_content = external_html.before_content,
129-
text = rendered,
170+
text = if render_type == RenderType::Pulldown { pulldown_output } else { hoedown_output },
130171
after_content = external_html.after_content,
131-
);
172+
);
132173

133174
match err {
134175
Err(e) => {
135176
eprintln!("rustdoc: cannot write to `{}`: {}", output.display(), e);
136177
6
137178
}
138-
Ok(_) => 0
179+
Ok(_) => 0,
139180
}
140181
}
141182

0 commit comments

Comments
 (0)