Skip to content

Commit 538689d

Browse files
committed
Move ID generator to a more suited location
1 parent d5321f2 commit 538689d

File tree

3 files changed

+45
-40
lines changed

3 files changed

+45
-40
lines changed

src/librustdoc/html/markdown.rs

+12-37
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
//! (bundled into the rust runtime). This module self-contains the C bindings
1515
//! and necessary legwork to render markdown, and exposes all of the
1616
//! functionality through a unit-struct, `Markdown`, which has an implementation
17-
//! of `fmt::String`. Example usage:
17+
//! of `fmt::Display`. Example usage:
1818
//!
1919
//! ```rust,ignore
2020
//! use rustdoc::html::markdown::Markdown;
@@ -29,19 +29,19 @@
2929
use libc;
3030
use std::ascii::AsciiExt;
3131
use std::cell::RefCell;
32-
use std::collections::HashMap;
3332
use std::default::Default;
3433
use std::ffi::CString;
3534
use std::fmt;
3635
use std::slice;
3736
use std::str;
3837

38+
use html::render::{with_unique_id, reset_ids};
3939
use html::toc::TocBuilder;
4040
use html::highlight;
4141
use html::escape::Escape;
4242
use test;
4343

44-
/// A unit struct which has the `fmt::String` trait implemented. When
44+
/// A unit struct which has the `fmt::Display` trait implemented. When
4545
/// formatted, this struct will emit the HTML corresponding to the rendered
4646
/// version of the contained markdown string.
4747
pub struct Markdown<'a>(pub &'a str);
@@ -210,10 +210,6 @@ fn collapse_whitespace(s: &str) -> String {
210210
s.split_whitespace().collect::<Vec<_>>().join(" ")
211211
}
212212

213-
thread_local!(static USED_HEADER_MAP: RefCell<HashMap<String, usize>> = {
214-
RefCell::new(HashMap::new())
215-
});
216-
217213
thread_local!(pub static PLAYGROUND_KRATE: RefCell<Option<Option<String>>> = {
218214
RefCell::new(None)
219215
});
@@ -311,31 +307,22 @@ pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result {
311307
let opaque = unsafe { (*data).opaque as *mut hoedown_html_renderer_state };
312308
let opaque = unsafe { &mut *((*opaque).opaque as *mut MyOpaque) };
313309

314-
// Make sure our hyphenated ID is unique for this page
315-
let id = USED_HEADER_MAP.with(|map| {
316-
let id = match map.borrow_mut().get_mut(&id) {
317-
None => id,
318-
Some(a) => { *a += 1; format!("{}-{}", id, *a - 1) }
319-
};
320-
map.borrow_mut().insert(id.clone(), 1);
321-
id
322-
});
323-
310+
let text = with_unique_id(id, |id| {
311+
let sec = opaque.toc_builder.as_mut().map_or("".to_owned(), |builder| {
312+
format!("{} ", builder.push(level as u32, s.clone(), id.to_owned()))
313+
});
324314

325-
let sec = opaque.toc_builder.as_mut().map_or("".to_owned(), |builder| {
326-
format!("{} ", builder.push(level as u32, s.clone(), id.clone()))
315+
// Render the HTML
316+
format!("<h{lvl} id='{id}' class='section-header'>\
317+
<a href='#{id}'>{sec}{}</a></h{lvl}>",
318+
s, lvl = level, id = id, sec = sec)
327319
});
328320

329-
// Render the HTML
330-
let text = format!("<h{lvl} id='{id}' class='section-header'>\
331-
<a href='#{id}'>{sec}{}</a></h{lvl}>",
332-
s, lvl = level, id = id, sec = sec);
333-
334321
let text = CString::new(text).unwrap();
335322
unsafe { hoedown_buffer_puts(ob, text.as_ptr()) }
336323
}
337324

338-
reset_headers();
325+
reset_ids();
339326

340327
extern fn codespan(
341328
ob: *mut hoedown_buffer,
@@ -500,18 +487,6 @@ impl LangString {
500487
}
501488
}
502489

503-
/// By default this markdown renderer generates anchors for each header in the
504-
/// rendered document. The anchor name is the contents of the header separated
505-
/// by hyphens, and a thread-local map is used to disambiguate among duplicate
506-
/// headers (numbers are appended).
507-
///
508-
/// This method will reset the local table for these headers. This is typically
509-
/// used at the beginning of rendering an entire HTML page to reset from the
510-
/// previous state (if any).
511-
pub fn reset_headers() {
512-
USED_HEADER_MAP.with(|s| s.borrow_mut().clear());
513-
}
514-
515490
impl<'a> fmt::Display for Markdown<'a> {
516491
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
517492
let Markdown(md) = *self;

src/librustdoc/html/render.rs

+30-1
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,35 @@ impl fmt::Display for IndexItemFunctionType {
342342
thread_local!(static CACHE_KEY: RefCell<Arc<Cache>> = Default::default());
343343
thread_local!(pub static CURRENT_LOCATION_KEY: RefCell<Vec<String>> =
344344
RefCell::new(Vec::new()));
345+
thread_local!(static USED_ID_MAP: RefCell<HashMap<String, usize>> =
346+
RefCell::new(HashMap::new()));
347+
348+
/// This method resets the local table of used ID attributes. This is typically
349+
/// used at the beginning of rendering an entire HTML page to reset from the
350+
/// previous state (if any).
351+
pub fn reset_ids() {
352+
USED_ID_MAP.with(|s| s.borrow_mut().clear());
353+
}
354+
355+
pub fn with_unique_id<T, F: FnOnce(&str) -> T>(candidate: String, f: F) -> T {
356+
USED_ID_MAP.with(|map| {
357+
let (id, ret) = match map.borrow_mut().get_mut(&candidate) {
358+
None => {
359+
let ret = f(&candidate);
360+
(candidate, ret)
361+
},
362+
Some(a) => {
363+
let id = format!("{}-{}", candidate, *a);
364+
let ret = f(&id);
365+
*a += 1;
366+
(id, ret)
367+
}
368+
};
369+
370+
map.borrow_mut().insert(id, 1);
371+
ret
372+
})
373+
}
345374

346375
/// Generates the documentation for `crate` into the directory `dst`
347376
pub fn run(mut krate: clean::Crate,
@@ -1274,7 +1303,7 @@ impl Context {
12741303
keywords: &keywords,
12751304
};
12761305

1277-
markdown::reset_headers();
1306+
reset_ids();
12781307

12791308
// We have a huge number of calls to write, so try to alleviate some
12801309
// of the pain by using a buffered writer instead of invoking the

src/librustdoc/markdown.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ use rustc::session::search_paths::SearchPaths;
2121

2222
use externalfiles::ExternalHtml;
2323

24+
use html::render::reset_ids;
2425
use html::escape::Escape;
2526
use html::markdown;
26-
use html::markdown::{Markdown, MarkdownWithToc, find_testable_code, reset_headers};
27+
use html::markdown::{Markdown, MarkdownWithToc, find_testable_code};
2728
use test::{TestOptions, Collector};
2829

2930
/// Separate any lines at the start of the file that begin with `%`.
@@ -82,7 +83,7 @@ pub fn render(input: &str, mut output: PathBuf, matches: &getopts::Matches,
8283
}
8384
let title = metadata[0];
8485

85-
reset_headers();
86+
reset_ids();
8687

8788
let rendered = if include_toc {
8889
format!("{}", MarkdownWithToc(text))

0 commit comments

Comments
 (0)