Skip to content

Commit 9ab20a3

Browse files
committed
rustdoc: collect rendering warnings and print them in one place
1 parent a5f50a9 commit 9ab20a3

File tree

1 file changed

+34
-16
lines changed

1 file changed

+34
-16
lines changed

src/librustdoc/html/render.rs

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ pub struct SharedContext {
124124
/// The given user css file which allow to customize the generated
125125
/// documentation theme.
126126
pub css_file_extension: Option<PathBuf>,
127+
/// Warnings for the user if rendering would differ using different markdown
128+
/// parsers.
129+
pub markdown_warnings: RefCell<Vec<(String, Vec<String>)>>,
127130
}
128131

129132
/// Indicates where an external crate can be found.
@@ -457,6 +460,7 @@ pub fn run(mut krate: clean::Crate,
457460
krate: krate.name.clone(),
458461
},
459462
css_file_extension: css_file_extension.clone(),
463+
markdown_warnings: RefCell::new(vec![]),
460464
};
461465

462466
// If user passed in `--playground-url` arg, we fill in crate name here
@@ -579,8 +583,19 @@ pub fn run(mut krate: clean::Crate,
579583

580584
write_shared(&cx, &krate, &*cache, index)?;
581585

586+
let scx = cx.shared.clone();
587+
582588
// And finally render the whole crate's documentation
583-
cx.krate(krate)
589+
let result = cx.krate(krate);
590+
591+
let markdown_warnings = scx.markdown_warnings.borrow();
592+
for &(ref text, ref diffs) in &*markdown_warnings {
593+
println!("Differences spotted in {:?}:\n{}",
594+
text,
595+
diffs.join("\n"));
596+
}
597+
598+
result
584599
}
585600

586601
/// Build the search index from the collected metadata
@@ -1641,12 +1656,18 @@ fn plain_summary_line(s: Option<&str>) -> String {
16411656
fn document(w: &mut fmt::Formatter, cx: &Context, item: &clean::Item) -> fmt::Result {
16421657
document_stability(w, cx, item)?;
16431658
let prefix = render_assoc_const_value(item);
1644-
document_full(w, item, cx.render_type, &prefix)?;
1659+
document_full(w, item, cx, &prefix)?;
16451660
Ok(())
16461661
}
16471662

1648-
fn render_markdown(w: &mut fmt::Formatter, md_text: &str, render_type: RenderType,
1649-
prefix: &str) -> fmt::Result {
1663+
/// Render md_text as markdown. Warns the user if there are difference in
1664+
/// rendering between Pulldown and Hoedown.
1665+
fn render_markdown(w: &mut fmt::Formatter,
1666+
md_text: &str,
1667+
render_type: RenderType,
1668+
prefix: &str,
1669+
scx: &SharedContext)
1670+
-> fmt::Result {
16501671
let hoedown_output = format!("{}", Markdown(md_text, RenderType::Hoedown));
16511672
// We only emit warnings if the user has opted-in to Pulldown rendering.
16521673
let output = if render_type == RenderType::Pulldown {
@@ -1665,10 +1686,7 @@ fn render_markdown(w: &mut fmt::Formatter, md_text: &str, render_type: RenderTyp
16651686
.collect::<Vec<String>>();
16661687

16671688
if !differences.is_empty() {
1668-
// Emit warnings if there are differences.
1669-
println!("Differences spotted in {:?}:\n{}",
1670-
md_text,
1671-
differences.join("\n"));
1689+
scx.markdown_warnings.borrow_mut().push((md_text.to_owned(), differences));
16721690
}
16731691

16741692
pulldown_output
@@ -1680,15 +1698,15 @@ fn render_markdown(w: &mut fmt::Formatter, md_text: &str, render_type: RenderTyp
16801698
}
16811699

16821700
fn document_short(w: &mut fmt::Formatter, item: &clean::Item, link: AssocItemLink,
1683-
render_type: RenderType, prefix: &str) -> fmt::Result {
1701+
cx: &Context, prefix: &str) -> fmt::Result {
16841702
if let Some(s) = item.doc_value() {
16851703
let markdown = if s.contains('\n') {
16861704
format!("{} [Read more]({})",
16871705
&plain_summary_line(Some(s)), naive_assoc_href(item, link))
16881706
} else {
16891707
format!("{}", &plain_summary_line(Some(s)))
16901708
};
1691-
render_markdown(w, &markdown, render_type, prefix)?;
1709+
render_markdown(w, &markdown, cx.render_type, prefix, &cx.shared)?;
16921710
} else if !prefix.is_empty() {
16931711
write!(w, "<div class='docblock'>{}</div>", prefix)?;
16941712
}
@@ -1710,9 +1728,9 @@ fn render_assoc_const_value(item: &clean::Item) -> String {
17101728
}
17111729

17121730
fn document_full(w: &mut fmt::Formatter, item: &clean::Item,
1713-
render_type: RenderType, prefix: &str) -> fmt::Result {
1731+
cx: &Context, prefix: &str) -> fmt::Result {
17141732
if let Some(s) = item.doc_value() {
1715-
render_markdown(w, s, render_type, prefix)?;
1733+
render_markdown(w, s, cx.render_type, prefix, &cx.shared)?;
17161734
} else if !prefix.is_empty() {
17171735
write!(w, "<div class='docblock'>{}</div>", prefix)?;
17181736
}
@@ -3111,20 +3129,20 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
31113129
// because impls can't have a stability.
31123130
document_stability(w, cx, it)?;
31133131
if item.doc_value().is_some() {
3114-
document_full(w, item, cx.render_type, &prefix)?;
3132+
document_full(w, item, cx, &prefix)?;
31153133
} else {
31163134
// In case the item isn't documented,
31173135
// provide short documentation from the trait.
3118-
document_short(w, it, link, cx.render_type, &prefix)?;
3136+
document_short(w, it, link, cx, &prefix)?;
31193137
}
31203138
}
31213139
} else {
31223140
document_stability(w, cx, item)?;
3123-
document_full(w, item, cx.render_type, &prefix)?;
3141+
document_full(w, item, cx, &prefix)?;
31243142
}
31253143
} else {
31263144
document_stability(w, cx, item)?;
3127-
document_short(w, item, link, cx.render_type, &prefix)?;
3145+
document_short(w, item, link, cx, &prefix)?;
31283146
}
31293147
}
31303148
Ok(())

0 commit comments

Comments
 (0)