1
Fork 0

Generate unique IDs for each rustdoc HTML page

This commit is contained in:
mitaa 2015-12-02 23:49:18 +01:00
parent 538689ddc7
commit fa0269dd35
2 changed files with 36 additions and 40 deletions

View file

@ -35,7 +35,7 @@ use std::fmt;
use std::slice; use std::slice;
use std::str; use std::str;
use html::render::{with_unique_id, reset_ids}; use html::render::with_unique_id;
use html::toc::TocBuilder; use html::toc::TocBuilder;
use html::highlight; use html::highlight;
use html::escape::Escape; use html::escape::Escape;
@ -322,8 +322,6 @@ pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result {
unsafe { hoedown_buffer_puts(ob, text.as_ptr()) } unsafe { hoedown_buffer_puts(ob, text.as_ptr()) }
} }
reset_ids();
extern fn codespan( extern fn codespan(
ob: *mut hoedown_buffer, ob: *mut hoedown_buffer,
text: *const hoedown_buffer, text: *const hoedown_buffer,
@ -554,6 +552,7 @@ pub fn plain_summary_line(md: &str) -> String {
mod tests { mod tests {
use super::{LangString, Markdown}; use super::{LangString, Markdown};
use super::plain_summary_line; use super::plain_summary_line;
use html::render::reset_ids;
#[test] #[test]
fn test_lang_string_parse() { fn test_lang_string_parse() {
@ -593,6 +592,7 @@ mod tests {
fn t(input: &str, expect: &str) { fn t(input: &str, expect: &str) {
let output = format!("{}", Markdown(input)); let output = format!("{}", Markdown(input));
assert_eq!(output, expect); assert_eq!(output, expect);
reset_ids();
} }
t("# Foo bar", "\n<h1 id='foo-bar' class='section-header'>\ t("# Foo bar", "\n<h1 id='foo-bar' class='section-header'>\

View file

@ -1725,10 +1725,10 @@ fn item_module(w: &mut fmt::Formatter, cx: &Context,
ItemType::AssociatedType => ("associated-types", "Associated Types"), ItemType::AssociatedType => ("associated-types", "Associated Types"),
ItemType::AssociatedConst => ("associated-consts", "Associated Constants"), ItemType::AssociatedConst => ("associated-consts", "Associated Constants"),
}; };
try!(write!(w, try!(with_unique_id(short.to_owned(), |id|
"<h2 id='{id}' class='section-header'>\ write!(w, "<h2 id='{id}' class='section-header'>\
<a href=\"#{id}\">{name}</a></h2>\n<table>", <a href=\"#{id}\">{name}</a></h2>\n<table>",
id = short, name = name)); id = id, name = name)));
} }
match myitem.inner { match myitem.inner {
@ -1949,10 +1949,11 @@ fn item_trait(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
fn trait_item(w: &mut fmt::Formatter, cx: &Context, m: &clean::Item) fn trait_item(w: &mut fmt::Formatter, cx: &Context, m: &clean::Item)
-> fmt::Result { -> fmt::Result {
try!(write!(w, "<h3 id='{ty}.{name}' class='method stab {stab}'><code>", let name = m.name.as_ref().unwrap();
ty = shortty(m), try!(with_unique_id(format!("{}.{}", shortty(m), name), |id|
name = *m.name.as_ref().unwrap(), write!(w, "<h3 id='{id}' class='method stab {stab}'><code>",
stab = m.stability_class())); id = id,
stab = m.stability_class())));
try!(render_assoc_item(w, m, AssocItemLink::Anchor)); try!(render_assoc_item(w, m, AssocItemLink::Anchor));
try!(write!(w, "</code></h3>")); try!(write!(w, "</code></h3>"));
try!(document(w, cx, m)); try!(document(w, cx, m));
@ -2141,11 +2142,12 @@ fn item_struct(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
if fields.peek().is_some() { if fields.peek().is_some() {
try!(write!(w, "<h2 class='fields'>Fields</h2>\n<table>")); try!(write!(w, "<h2 class='fields'>Fields</h2>\n<table>"));
for field in fields { for field in fields {
try!(write!(w, "<tr class='stab {stab}'> let name = field.name.as_ref().unwrap();
<td id='structfield.{name}'>\ try!(with_unique_id(format!("structfield.{}", name), |id|
<code>{name}</code></td><td>", write!(w, "<tr class='stab {}'><td id='{}'><code>{}</code></td><td>",
stab = field.stability_class(), field.stability_class(),
name = field.name.as_ref().unwrap())); id,
name)));
try!(document(w, cx, field)); try!(document(w, cx, field));
try!(write!(w, "</td></tr>")); try!(write!(w, "</td></tr>"));
} }
@ -2212,8 +2214,9 @@ fn item_enum(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
if !e.variants.is_empty() { if !e.variants.is_empty() {
try!(write!(w, "<h2 class='variants'>Variants</h2>\n<table>")); try!(write!(w, "<h2 class='variants'>Variants</h2>\n<table>"));
for variant in &e.variants { for variant in &e.variants {
try!(write!(w, "<tr><td id='variant.{name}'><code>{name}</code></td><td>", let name = variant.name.as_ref().unwrap();
name = variant.name.as_ref().unwrap())); try!(with_unique_id(format!("variant.{}", name), |id|
write!(w, "<tr><td id='{}'><code>{}</code></td><td>", id, name)));
try!(document(w, cx, variant)); try!(document(w, cx, variant));
match variant.inner { match variant.inner {
clean::VariantItem(ref var) => { clean::VariantItem(ref var) => {
@ -2231,11 +2234,10 @@ fn item_enum(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
try!(write!(w, "<h3 class='fields'>Fields</h3>\n try!(write!(w, "<h3 class='fields'>Fields</h3>\n
<table>")); <table>"));
for field in fields { for field in fields {
try!(write!(w, "<tr><td \ let v = variant.name.as_ref().unwrap();
id='variant.{v}.field.{f}'>\ let f = field.name.as_ref().unwrap();
<code>{f}</code></td><td>", try!(with_unique_id(format!("variant.{}.field.{}", v, f), |id|
v = variant.name.as_ref().unwrap(), write!(w, "<tr><td id='{}'><code>{}</code></td><td>", id, f)));
f = field.name.as_ref().unwrap()));
try!(document(w, cx, field)); try!(document(w, cx, field));
try!(write!(w, "</td></tr>")); try!(write!(w, "</td></tr>"));
} }
@ -2447,44 +2449,38 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
fn doctraititem(w: &mut fmt::Formatter, cx: &Context, item: &clean::Item, fn doctraititem(w: &mut fmt::Formatter, cx: &Context, item: &clean::Item,
link: AssocItemLink, render_static: bool) -> fmt::Result { link: AssocItemLink, render_static: bool) -> fmt::Result {
let name = item.name.as_ref().unwrap();
match item.inner { match item.inner {
clean::MethodItem(..) | clean::TyMethodItem(..) => { clean::MethodItem(..) | clean::TyMethodItem(..) => {
// Only render when the method is not static or we allow static methods // Only render when the method is not static or we allow static methods
if !is_static_method(item) || render_static { if !is_static_method(item) || render_static {
try!(write!(w, "<h4 id='method.{}' class='{}'><code>", try!(with_unique_id(format!("method.{}", name), |id|
*item.name.as_ref().unwrap(), write!(w, "<h4 id='{}' class='{}'><code>", id, shortty(item))));
shortty(item)));
try!(render_assoc_item(w, item, link)); try!(render_assoc_item(w, item, link));
try!(write!(w, "</code></h4>\n")); try!(write!(w, "</code></h4>\n"));
} }
} }
clean::TypedefItem(ref tydef, _) => { clean::TypedefItem(ref tydef, _) => {
let name = item.name.as_ref().unwrap(); try!(with_unique_id(format!("assoc_type.{}", name), |id|
try!(write!(w, "<h4 id='assoc_type.{}' class='{}'><code>", write!(w, "<h4 id='{}' class='{}'><code>", id, shortty(item))));
*name,
shortty(item)));
try!(write!(w, "type {} = {}", name, tydef.type_)); try!(write!(w, "type {} = {}", name, tydef.type_));
try!(write!(w, "</code></h4>\n")); try!(write!(w, "</code></h4>\n"));
} }
clean::AssociatedConstItem(ref ty, ref default) => { clean::AssociatedConstItem(ref ty, ref default) => {
let name = item.name.as_ref().unwrap(); try!(with_unique_id(format!("assoc_const.{}", name), |id|
try!(write!(w, "<h4 id='assoc_const.{}' class='{}'><code>", write!(w, "<h4 id='{}' class='{}'><code>", id, shortty(item))));
*name, shortty(item)));
try!(assoc_const(w, item, ty, default.as_ref())); try!(assoc_const(w, item, ty, default.as_ref()));
try!(write!(w, "</code></h4>\n")); try!(write!(w, "</code></h4>\n"));
} }
clean::ConstantItem(ref c) => { clean::ConstantItem(ref c) => {
let name = item.name.as_ref().unwrap(); try!(with_unique_id(format!("assoc_const.{}", name), |id|
try!(write!(w, "<h4 id='assoc_const.{}' class='{}'><code>", write!(w, "<h4 id='{}' class='{}'><code>", id, shortty(item))));
*name, shortty(item)));
try!(assoc_const(w, item, &c.type_, Some(&c.expr))); try!(assoc_const(w, item, &c.type_, Some(&c.expr)));
try!(write!(w, "</code></h4>\n")); try!(write!(w, "</code></h4>\n"));
} }
clean::AssociatedTypeItem(ref bounds, ref default) => { clean::AssociatedTypeItem(ref bounds, ref default) => {
let name = item.name.as_ref().unwrap(); try!(with_unique_id(format!("assoc_type.{}", name), |id|
try!(write!(w, "<h4 id='assoc_type.{}' class='{}'><code>", write!(w, "<h4 id='{}' class='{}'><code>", id, shortty(item))));
*name,
shortty(item)));
try!(assoc_type(w, item, bounds, default)); try!(assoc_type(w, item, bounds, default));
try!(write!(w, "</code></h4>\n")); try!(write!(w, "</code></h4>\n"));
} }