1
Fork 0

Address review comments

This commit is contained in:
mitaa 2015-12-04 00:48:59 +01:00
parent 5c01cf485f
commit 72d5675fef
2 changed files with 43 additions and 49 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; use html::render::derive_id;
use html::toc::TocBuilder; use html::toc::TocBuilder;
use html::highlight; use html::highlight;
use html::escape::Escape; use html::escape::Escape;
@ -307,17 +307,17 @@ pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result {
let opaque = unsafe { (*data).opaque as *mut hoedown_html_renderer_state }; let opaque = unsafe { (*data).opaque as *mut hoedown_html_renderer_state };
let opaque = unsafe { &mut *((*opaque).opaque as *mut MyOpaque) }; let opaque = unsafe { &mut *((*opaque).opaque as *mut MyOpaque) };
let text = with_unique_id(id, |id| { let id = derive_id(id);
let sec = opaque.toc_builder.as_mut().map_or("".to_owned(), |builder| {
format!("{} ", builder.push(level as u32, s.clone(), id.to_owned()))
});
// Render the HTML let sec = opaque.toc_builder.as_mut().map_or("".to_owned(), |builder| {
format!("<h{lvl} id='{id}' class='section-header'>\ format!("{} ", builder.push(level as u32, s.clone(), id.clone()))
<a href='#{id}'>{sec}{}</a></h{lvl}>",
s, lvl = level, id = id, sec = sec)
}); });
// Render the HTML
let text = format!("<h{lvl} id='{id}' class='section-header'>\
<a href='#{id}'>{sec}{}</a></h{lvl}>",
s, lvl = level, id = id, sec = sec);
let text = CString::new(text).unwrap(); let text = CString::new(text).unwrap();
unsafe { hoedown_buffer_puts(ob, text.as_ptr()) } unsafe { hoedown_buffer_puts(ob, text.as_ptr()) }
} }

View file

@ -372,23 +372,19 @@ pub fn reset_ids() {
USED_ID_MAP.with(|s| *s.borrow_mut() = init_ids()); USED_ID_MAP.with(|s| *s.borrow_mut() = init_ids());
} }
pub fn with_unique_id<T, F: FnOnce(&str) -> T>(candidate: String, f: F) -> T { pub fn derive_id(candidate: String) -> String {
USED_ID_MAP.with(|map| { USED_ID_MAP.with(|map| {
let (id, ret) = match map.borrow_mut().get_mut(&candidate) { let id = match map.borrow_mut().get_mut(&candidate) {
None => { None => candidate,
let ret = f(&candidate);
(candidate, ret)
},
Some(a) => { Some(a) => {
let id = format!("{}-{}", candidate, *a); let id = format!("{}-{}", candidate, *a);
let ret = f(&id);
*a += 1; *a += 1;
(id, ret) id
} }
}; };
map.borrow_mut().insert(id, 1); map.borrow_mut().insert(id.clone(), 1);
ret id
}) })
} }
@ -1745,10 +1741,9 @@ 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!(with_unique_id(short.to_owned(), |id| try!(write!(w, "<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 = derive_id(short.to_owned()), name = name));
id = id, name = name)));
} }
match myitem.inner { match myitem.inner {
@ -1970,10 +1965,10 @@ 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 {
let name = m.name.as_ref().unwrap(); let name = m.name.as_ref().unwrap();
try!(with_unique_id(format!("{}.{}", shortty(m), name), |id| let id = derive_id(format!("{}.{}", shortty(m), name));
write!(w, "<h3 id='{id}' class='method stab {stab}'><code>", try!(write!(w, "<h3 id='{id}' class='method stab {stab}'><code>",
id = id, id = id,
stab = m.stability_class()))); 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));
@ -2162,12 +2157,11 @@ 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 {
let name = field.name.as_ref().unwrap(); try!(write!(w, "<tr class='stab {stab}'>
try!(with_unique_id(format!("structfield.{}", name), |id| <td id='structfield.{name}'>\
write!(w, "<tr class='stab {}'><td id='{}'><code>{}</code></td><td>", <code>{name}</code></td><td>",
field.stability_class(), stab = field.stability_class(),
id, name = field.name.as_ref().unwrap()));
name)));
try!(document(w, cx, field)); try!(document(w, cx, field));
try!(write!(w, "</td></tr>")); try!(write!(w, "</td></tr>"));
} }
@ -2234,9 +2228,8 @@ 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 {
let name = variant.name.as_ref().unwrap(); try!(write!(w, "<tr><td id='variant.{name}'><code>{name}</code></td><td>",
try!(with_unique_id(format!("variant.{}", name), |id| name = variant.name.as_ref().unwrap()));
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) => {
@ -2254,10 +2247,11 @@ 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 {
let v = variant.name.as_ref().unwrap(); try!(write!(w, "<tr><td \
let f = field.name.as_ref().unwrap(); id='variant.{v}.field.{f}'>\
try!(with_unique_id(format!("variant.{}.field.{}", v, f), |id| <code>{f}</code></td><td>",
write!(w, "<tr><td id='{}'><code>{}</code></td><td>", id, f))); v = variant.name.as_ref().unwrap(),
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>"));
} }
@ -2474,33 +2468,33 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
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!(with_unique_id(format!("method.{}", name), |id| let id = derive_id(format!("method.{}", name));
write!(w, "<h4 id='{}' class='{}'><code>", id, shortty(item)))); try!(write!(w, "<h4 id='{}' class='{}'><code>", id, 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, _) => {
try!(with_unique_id(format!("assoc_type.{}", name), |id| let id = derive_id(format!("assoc_type.{}", name));
write!(w, "<h4 id='{}' class='{}'><code>", id, shortty(item)))); try!(write!(w, "<h4 id='{}' class='{}'><code>", id, 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) => {
try!(with_unique_id(format!("assoc_const.{}", name), |id| let id = derive_id(format!("assoc_const.{}", name));
write!(w, "<h4 id='{}' class='{}'><code>", id, shortty(item)))); try!(write!(w, "<h4 id='{}' class='{}'><code>", id, 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) => {
try!(with_unique_id(format!("assoc_const.{}", name), |id| let id = derive_id(format!("assoc_const.{}", name));
write!(w, "<h4 id='{}' class='{}'><code>", id, shortty(item)))); try!(write!(w, "<h4 id='{}' class='{}'><code>", id, 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) => {
try!(with_unique_id(format!("assoc_type.{}", name), |id| let id = derive_id(format!("assoc_type.{}", name));
write!(w, "<h4 id='{}' class='{}'><code>", id, shortty(item)))); try!(write!(w, "<h4 id='{}' class='{}'><code>", id, 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"));
} }