Skip to content

Commit fa0269d

Browse files
committed
Generate unique IDs for each rustdoc HTML page
1 parent 538689d commit fa0269d

File tree

2 files changed

+36
-40
lines changed

2 files changed

+36
-40
lines changed

src/librustdoc/html/markdown.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use std::fmt;
3535
use std::slice;
3636
use std::str;
3737

38-
use html::render::{with_unique_id, reset_ids};
38+
use html::render::with_unique_id;
3939
use html::toc::TocBuilder;
4040
use html::highlight;
4141
use html::escape::Escape;
@@ -322,8 +322,6 @@ pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result {
322322
unsafe { hoedown_buffer_puts(ob, text.as_ptr()) }
323323
}
324324

325-
reset_ids();
326-
327325
extern fn codespan(
328326
ob: *mut hoedown_buffer,
329327
text: *const hoedown_buffer,
@@ -554,6 +552,7 @@ pub fn plain_summary_line(md: &str) -> String {
554552
mod tests {
555553
use super::{LangString, Markdown};
556554
use super::plain_summary_line;
555+
use html::render::reset_ids;
557556

558557
#[test]
559558
fn test_lang_string_parse() {
@@ -593,6 +592,7 @@ mod tests {
593592
fn t(input: &str, expect: &str) {
594593
let output = format!("{}", Markdown(input));
595594
assert_eq!(output, expect);
595+
reset_ids();
596596
}
597597

598598
t("# Foo bar", "\n<h1 id='foo-bar' class='section-header'>\

src/librustdoc/html/render.rs

+33-37
Original file line numberDiff line numberDiff line change
@@ -1725,10 +1725,10 @@ fn item_module(w: &mut fmt::Formatter, cx: &Context,
17251725
ItemType::AssociatedType => ("associated-types", "Associated Types"),
17261726
ItemType::AssociatedConst => ("associated-consts", "Associated Constants"),
17271727
};
1728-
try!(write!(w,
1729-
"<h2 id='{id}' class='section-header'>\
1730-
<a href=\"#{id}\">{name}</a></h2>\n<table>",
1731-
id = short, name = name));
1728+
try!(with_unique_id(short.to_owned(), |id|
1729+
write!(w, "<h2 id='{id}' class='section-header'>\
1730+
<a href=\"#{id}\">{name}</a></h2>\n<table>",
1731+
id = id, name = name)));
17321732
}
17331733

17341734
match myitem.inner {
@@ -1949,10 +1949,11 @@ fn item_trait(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
19491949

19501950
fn trait_item(w: &mut fmt::Formatter, cx: &Context, m: &clean::Item)
19511951
-> fmt::Result {
1952-
try!(write!(w, "<h3 id='{ty}.{name}' class='method stab {stab}'><code>",
1953-
ty = shortty(m),
1954-
name = *m.name.as_ref().unwrap(),
1955-
stab = m.stability_class()));
1952+
let name = m.name.as_ref().unwrap();
1953+
try!(with_unique_id(format!("{}.{}", shortty(m), name), |id|
1954+
write!(w, "<h3 id='{id}' class='method stab {stab}'><code>",
1955+
id = id,
1956+
stab = m.stability_class())));
19561957
try!(render_assoc_item(w, m, AssocItemLink::Anchor));
19571958
try!(write!(w, "</code></h3>"));
19581959
try!(document(w, cx, m));
@@ -2141,11 +2142,12 @@ fn item_struct(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
21412142
if fields.peek().is_some() {
21422143
try!(write!(w, "<h2 class='fields'>Fields</h2>\n<table>"));
21432144
for field in fields {
2144-
try!(write!(w, "<tr class='stab {stab}'>
2145-
<td id='structfield.{name}'>\
2146-
<code>{name}</code></td><td>",
2147-
stab = field.stability_class(),
2148-
name = field.name.as_ref().unwrap()));
2145+
let name = field.name.as_ref().unwrap();
2146+
try!(with_unique_id(format!("structfield.{}", name), |id|
2147+
write!(w, "<tr class='stab {}'><td id='{}'><code>{}</code></td><td>",
2148+
field.stability_class(),
2149+
id,
2150+
name)));
21492151
try!(document(w, cx, field));
21502152
try!(write!(w, "</td></tr>"));
21512153
}
@@ -2212,8 +2214,9 @@ fn item_enum(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
22122214
if !e.variants.is_empty() {
22132215
try!(write!(w, "<h2 class='variants'>Variants</h2>\n<table>"));
22142216
for variant in &e.variants {
2215-
try!(write!(w, "<tr><td id='variant.{name}'><code>{name}</code></td><td>",
2216-
name = variant.name.as_ref().unwrap()));
2217+
let name = variant.name.as_ref().unwrap();
2218+
try!(with_unique_id(format!("variant.{}", name), |id|
2219+
write!(w, "<tr><td id='{}'><code>{}</code></td><td>", id, name)));
22172220
try!(document(w, cx, variant));
22182221
match variant.inner {
22192222
clean::VariantItem(ref var) => {
@@ -2231,11 +2234,10 @@ fn item_enum(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
22312234
try!(write!(w, "<h3 class='fields'>Fields</h3>\n
22322235
<table>"));
22332236
for field in fields {
2234-
try!(write!(w, "<tr><td \
2235-
id='variant.{v}.field.{f}'>\
2236-
<code>{f}</code></td><td>",
2237-
v = variant.name.as_ref().unwrap(),
2238-
f = field.name.as_ref().unwrap()));
2237+
let v = variant.name.as_ref().unwrap();
2238+
let f = field.name.as_ref().unwrap();
2239+
try!(with_unique_id(format!("variant.{}.field.{}", v, f), |id|
2240+
write!(w, "<tr><td id='{}'><code>{}</code></td><td>", id, f)));
22392241
try!(document(w, cx, field));
22402242
try!(write!(w, "</td></tr>"));
22412243
}
@@ -2447,44 +2449,38 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
24472449

24482450
fn doctraititem(w: &mut fmt::Formatter, cx: &Context, item: &clean::Item,
24492451
link: AssocItemLink, render_static: bool) -> fmt::Result {
2452+
let name = item.name.as_ref().unwrap();
24502453
match item.inner {
24512454
clean::MethodItem(..) | clean::TyMethodItem(..) => {
24522455
// Only render when the method is not static or we allow static methods
24532456
if !is_static_method(item) || render_static {
2454-
try!(write!(w, "<h4 id='method.{}' class='{}'><code>",
2455-
*item.name.as_ref().unwrap(),
2456-
shortty(item)));
2457+
try!(with_unique_id(format!("method.{}", name), |id|
2458+
write!(w, "<h4 id='{}' class='{}'><code>", id, shortty(item))));
24572459
try!(render_assoc_item(w, item, link));
24582460
try!(write!(w, "</code></h4>\n"));
24592461
}
24602462
}
24612463
clean::TypedefItem(ref tydef, _) => {
2462-
let name = item.name.as_ref().unwrap();
2463-
try!(write!(w, "<h4 id='assoc_type.{}' class='{}'><code>",
2464-
*name,
2465-
shortty(item)));
2464+
try!(with_unique_id(format!("assoc_type.{}", name), |id|
2465+
write!(w, "<h4 id='{}' class='{}'><code>", id, shortty(item))));
24662466
try!(write!(w, "type {} = {}", name, tydef.type_));
24672467
try!(write!(w, "</code></h4>\n"));
24682468
}
24692469
clean::AssociatedConstItem(ref ty, ref default) => {
2470-
let name = item.name.as_ref().unwrap();
2471-
try!(write!(w, "<h4 id='assoc_const.{}' class='{}'><code>",
2472-
*name, shortty(item)));
2470+
try!(with_unique_id(format!("assoc_const.{}", name), |id|
2471+
write!(w, "<h4 id='{}' class='{}'><code>", id, shortty(item))));
24732472
try!(assoc_const(w, item, ty, default.as_ref()));
24742473
try!(write!(w, "</code></h4>\n"));
24752474
}
24762475
clean::ConstantItem(ref c) => {
2477-
let name = item.name.as_ref().unwrap();
2478-
try!(write!(w, "<h4 id='assoc_const.{}' class='{}'><code>",
2479-
*name, shortty(item)));
2476+
try!(with_unique_id(format!("assoc_const.{}", name), |id|
2477+
write!(w, "<h4 id='{}' class='{}'><code>", id, shortty(item))));
24802478
try!(assoc_const(w, item, &c.type_, Some(&c.expr)));
24812479
try!(write!(w, "</code></h4>\n"));
24822480
}
24832481
clean::AssociatedTypeItem(ref bounds, ref default) => {
2484-
let name = item.name.as_ref().unwrap();
2485-
try!(write!(w, "<h4 id='assoc_type.{}' class='{}'><code>",
2486-
*name,
2487-
shortty(item)));
2482+
try!(with_unique_id(format!("assoc_type.{}", name), |id|
2483+
write!(w, "<h4 id='{}' class='{}'><code>", id, shortty(item))));
24882484
try!(assoc_type(w, item, bounds, default));
24892485
try!(write!(w, "</code></h4>\n"));
24902486
}

0 commit comments

Comments
 (0)