|
14 | 14 | //! (bundled into the rust runtime). This module self-contains the C bindings
|
15 | 15 | //! and necessary legwork to render markdown, and exposes all of the
|
16 | 16 | //! functionality through a unit-struct, `Markdown`, which has an implementation
|
17 |
| -//! of `fmt::String`. Example usage: |
| 17 | +//! of `fmt::Display`. Example usage: |
18 | 18 | //!
|
19 | 19 | //! ```rust,ignore
|
20 | 20 | //! use rustdoc::html::markdown::Markdown;
|
|
29 | 29 | use libc;
|
30 | 30 | use std::ascii::AsciiExt;
|
31 | 31 | use std::cell::RefCell;
|
32 |
| -use std::collections::HashMap; |
33 | 32 | use std::default::Default;
|
34 | 33 | use std::ffi::CString;
|
35 | 34 | use std::fmt;
|
36 | 35 | use std::slice;
|
37 | 36 | use std::str;
|
38 | 37 |
|
| 38 | +use html::render::{with_unique_id, reset_ids}; |
39 | 39 | use html::toc::TocBuilder;
|
40 | 40 | use html::highlight;
|
41 | 41 | use html::escape::Escape;
|
42 | 42 | use test;
|
43 | 43 |
|
44 |
| -/// A unit struct which has the `fmt::String` trait implemented. When |
| 44 | +/// A unit struct which has the `fmt::Display` trait implemented. When |
45 | 45 | /// formatted, this struct will emit the HTML corresponding to the rendered
|
46 | 46 | /// version of the contained markdown string.
|
47 | 47 | pub struct Markdown<'a>(pub &'a str);
|
@@ -210,10 +210,6 @@ fn collapse_whitespace(s: &str) -> String {
|
210 | 210 | s.split_whitespace().collect::<Vec<_>>().join(" ")
|
211 | 211 | }
|
212 | 212 |
|
213 |
| -thread_local!(static USED_HEADER_MAP: RefCell<HashMap<String, usize>> = { |
214 |
| - RefCell::new(HashMap::new()) |
215 |
| -}); |
216 |
| - |
217 | 213 | thread_local!(pub static PLAYGROUND_KRATE: RefCell<Option<Option<String>>> = {
|
218 | 214 | RefCell::new(None)
|
219 | 215 | });
|
@@ -311,31 +307,22 @@ pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result {
|
311 | 307 | let opaque = unsafe { (*data).opaque as *mut hoedown_html_renderer_state };
|
312 | 308 | let opaque = unsafe { &mut *((*opaque).opaque as *mut MyOpaque) };
|
313 | 309 |
|
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 | + }); |
324 | 314 |
|
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) |
327 | 319 | });
|
328 | 320 |
|
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 |
| - |
334 | 321 | let text = CString::new(text).unwrap();
|
335 | 322 | unsafe { hoedown_buffer_puts(ob, text.as_ptr()) }
|
336 | 323 | }
|
337 | 324 |
|
338 |
| - reset_headers(); |
| 325 | + reset_ids(); |
339 | 326 |
|
340 | 327 | extern fn codespan(
|
341 | 328 | ob: *mut hoedown_buffer,
|
@@ -500,18 +487,6 @@ impl LangString {
|
500 | 487 | }
|
501 | 488 | }
|
502 | 489 |
|
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 |
| - |
515 | 490 | impl<'a> fmt::Display for Markdown<'a> {
|
516 | 491 | fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
517 | 492 | let Markdown(md) = *self;
|
|
0 commit comments