rustdoc: Add anchors to section headers

This commit adds a appear-on-over link to all section headers to generated
documentation. Each header also receives an id now, even those generated through
markdown. The purpose of this is to provide easy to link to sections.

This modifies the default header markdown generation because the default id
added looks like "toc_NN" which is difficult to reconcile among all sections (by
default each section gets a "toc_0" id), and it's also not very descriptive of
where you're going.

This chooses to adopt the github-style anchors by taking the contents of the
title and hyphen-separating them (after lower casing).

Closes #12681
This commit is contained in:
Alex Crichton
2014-03-04 11:24:20 -08:00
parent 0a5138c752
commit 31e7e676e1
4 changed files with 105 additions and 24 deletions

View File

@@ -52,6 +52,7 @@ use doctree;
use fold::DocFolder;
use html::format::{VisSpace, Method, PuritySpace};
use html::layout;
use html::markdown;
use html::markdown::Markdown;
use html::highlight;
@@ -749,6 +750,8 @@ impl Context {
title: title,
};
markdown::reset_headers();
// We have a huge number of calls to write, so try to alleviate some
// of the pain by using a buffered writer instead of invoking the
// write sycall all the time.
@@ -1001,24 +1004,25 @@ fn item_module(w: &mut Writer, cx: &Context,
try!(write!(w, "</table>"));
}
curty = myty;
try!(write!(w, "<h2>{}</h2>\n<table>", match myitem.inner {
clean::ModuleItem(..) => "Modules",
clean::StructItem(..) => "Structs",
clean::EnumItem(..) => "Enums",
clean::FunctionItem(..) => "Functions",
clean::TypedefItem(..) => "Type Definitions",
clean::StaticItem(..) => "Statics",
clean::TraitItem(..) => "Traits",
clean::ImplItem(..) => "Implementations",
clean::ViewItemItem(..) => "Reexports",
clean::TyMethodItem(..) => "Type Methods",
clean::MethodItem(..) => "Methods",
clean::StructFieldItem(..) => "Struct Fields",
clean::VariantItem(..) => "Variants",
clean::ForeignFunctionItem(..) => "Foreign Functions",
clean::ForeignStaticItem(..) => "Foreign Statics",
clean::MacroItem(..) => "Macros",
}));
let (short, name) = match myitem.inner {
clean::ModuleItem(..) => ("modules", "Modules"),
clean::StructItem(..) => ("structs", "Structs"),
clean::EnumItem(..) => ("enums", "Enums"),
clean::FunctionItem(..) => ("functions", "Functions"),
clean::TypedefItem(..) => ("types", "Type Definitions"),
clean::StaticItem(..) => ("statics", "Statics"),
clean::TraitItem(..) => ("traits", "Traits"),
clean::ImplItem(..) => ("impls", "Implementations"),
clean::ViewItemItem(..) => ("reexports", "Reexports"),
clean::TyMethodItem(..) => ("tymethods", "Type Methods"),
clean::MethodItem(..) => ("methods", "Methods"),
clean::StructFieldItem(..) => ("fields", "Struct Fields"),
clean::VariantItem(..) => ("variants", "Variants"),
clean::ForeignFunctionItem(..) => ("ffi-fns", "Foreign Functions"),
clean::ForeignStaticItem(..) => ("ffi-statics", "Foreign Statics"),
clean::MacroItem(..) => ("macros", "Macros"),
};
try!(write!(w, "<h2 id='{}'>{}</h2>\n<table>", short, name));
}
match myitem.inner {