mirror of
https://github.com/rust-lang/rust.git
synced 2025-06-05 03:38:29 +00:00
rustdoc: Changed section headers anchor rendering
This commit is contained in:
parent
fd38a75077
commit
b8cfa59be0
@ -283,19 +283,37 @@ pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result {
|
|||||||
str::from_utf8(s).unwrap().to_string()
|
str::from_utf8(s).unwrap().to_string()
|
||||||
};
|
};
|
||||||
|
|
||||||
// Transform the contents of the header into a hyphenated string
|
// Discard '<em>', '<code>' tags and some escaped characters,
|
||||||
let id = s.split_whitespace().map(|s| s.to_ascii_lowercase())
|
// transform the contents of the header into a hyphenated string
|
||||||
.collect::<Vec<String>>().join("-");
|
// without non-alphanumeric characters other than '-' and '_'.
|
||||||
|
//
|
||||||
// This is a terrible hack working around how hoedown gives us rendered
|
// This is a terrible hack working around how hoedown gives us rendered
|
||||||
// html for text rather than the raw text.
|
// html for text rather than the raw text.
|
||||||
|
let mut id = s.clone();
|
||||||
|
let repl_sub = vec!["<em>", "</em>", "<code>", "</code>",
|
||||||
|
"<", ">", "&", "'", """];
|
||||||
|
for sub in repl_sub {
|
||||||
|
id = id.replace(sub, "");
|
||||||
|
}
|
||||||
|
let id = id.chars().filter_map(|c| {
|
||||||
|
if c.is_alphanumeric() || c == '-' || c == '_' {
|
||||||
|
if c.is_ascii() {
|
||||||
|
Some(c.to_ascii_lowercase())
|
||||||
|
} else {
|
||||||
|
Some(c)
|
||||||
|
}
|
||||||
|
} else if c.is_whitespace() && c.is_ascii() {
|
||||||
|
Some('-')
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}).collect::<String>();
|
||||||
|
|
||||||
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) };
|
||||||
|
|
||||||
// Make sure our hyphenated ID is unique for this page
|
// Make sure our hyphenated ID is unique for this page
|
||||||
let id = USED_HEADER_MAP.with(|map| {
|
let id = USED_HEADER_MAP.with(|map| {
|
||||||
let id = id.replace("<code>", "").replace("</code>", "").to_string();
|
|
||||||
let id = match map.borrow_mut().get_mut(&id) {
|
let id = match map.borrow_mut().get_mut(&id) {
|
||||||
None => id,
|
None => id,
|
||||||
Some(a) => { *a += 1; format!("{}-{}", id, *a - 1) }
|
Some(a) => { *a += 1; format!("{}-{}", id, *a - 1) }
|
||||||
|
Loading…
Reference in New Issue
Block a user