Move ID generator to a more suited location
This commit is contained in:
parent
d5321f2abe
commit
538689ddc7
3 changed files with 46 additions and 41 deletions
|
@ -14,7 +14,7 @@
|
||||||
//! (bundled into the rust runtime). This module self-contains the C bindings
|
//! (bundled into the rust runtime). This module self-contains the C bindings
|
||||||
//! and necessary legwork to render markdown, and exposes all of the
|
//! and necessary legwork to render markdown, and exposes all of the
|
||||||
//! functionality through a unit-struct, `Markdown`, which has an implementation
|
//! functionality through a unit-struct, `Markdown`, which has an implementation
|
||||||
//! of `fmt::String`. Example usage:
|
//! of `fmt::Display`. Example usage:
|
||||||
//!
|
//!
|
||||||
//! ```rust,ignore
|
//! ```rust,ignore
|
||||||
//! use rustdoc::html::markdown::Markdown;
|
//! use rustdoc::html::markdown::Markdown;
|
||||||
|
@ -29,19 +29,19 @@
|
||||||
use libc;
|
use libc;
|
||||||
use std::ascii::AsciiExt;
|
use std::ascii::AsciiExt;
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::collections::HashMap;
|
|
||||||
use std::default::Default;
|
use std::default::Default;
|
||||||
use std::ffi::CString;
|
use std::ffi::CString;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::slice;
|
use std::slice;
|
||||||
use std::str;
|
use std::str;
|
||||||
|
|
||||||
|
use html::render::{with_unique_id, reset_ids};
|
||||||
use html::toc::TocBuilder;
|
use html::toc::TocBuilder;
|
||||||
use html::highlight;
|
use html::highlight;
|
||||||
use html::escape::Escape;
|
use html::escape::Escape;
|
||||||
use test;
|
use test;
|
||||||
|
|
||||||
/// A unit struct which has the `fmt::String` trait implemented. When
|
/// A unit struct which has the `fmt::Display` trait implemented. When
|
||||||
/// formatted, this struct will emit the HTML corresponding to the rendered
|
/// formatted, this struct will emit the HTML corresponding to the rendered
|
||||||
/// version of the contained markdown string.
|
/// version of the contained markdown string.
|
||||||
pub struct Markdown<'a>(pub &'a str);
|
pub struct Markdown<'a>(pub &'a str);
|
||||||
|
@ -210,10 +210,6 @@ fn collapse_whitespace(s: &str) -> String {
|
||||||
s.split_whitespace().collect::<Vec<_>>().join(" ")
|
s.split_whitespace().collect::<Vec<_>>().join(" ")
|
||||||
}
|
}
|
||||||
|
|
||||||
thread_local!(static USED_HEADER_MAP: RefCell<HashMap<String, usize>> = {
|
|
||||||
RefCell::new(HashMap::new())
|
|
||||||
});
|
|
||||||
|
|
||||||
thread_local!(pub static PLAYGROUND_KRATE: RefCell<Option<Option<String>>> = {
|
thread_local!(pub static PLAYGROUND_KRATE: RefCell<Option<Option<String>>> = {
|
||||||
RefCell::new(None)
|
RefCell::new(None)
|
||||||
});
|
});
|
||||||
|
@ -311,31 +307,22 @@ 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) };
|
||||||
|
|
||||||
// Make sure our hyphenated ID is unique for this page
|
let text = with_unique_id(id, |id| {
|
||||||
let id = USED_HEADER_MAP.with(|map| {
|
let sec = opaque.toc_builder.as_mut().map_or("".to_owned(), |builder| {
|
||||||
let id = match map.borrow_mut().get_mut(&id) {
|
format!("{} ", builder.push(level as u32, s.clone(), id.to_owned()))
|
||||||
None => id,
|
});
|
||||||
Some(a) => { *a += 1; format!("{}-{}", id, *a - 1) }
|
|
||||||
};
|
// Render the HTML
|
||||||
map.borrow_mut().insert(id.clone(), 1);
|
format!("<h{lvl} id='{id}' class='section-header'>\
|
||||||
id
|
<a href='#{id}'>{sec}{}</a></h{lvl}>",
|
||||||
|
s, lvl = level, id = id, sec = sec)
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
let sec = opaque.toc_builder.as_mut().map_or("".to_owned(), |builder| {
|
|
||||||
format!("{} ", builder.push(level as u32, s.clone(), id.clone()))
|
|
||||||
});
|
|
||||||
|
|
||||||
// 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()) }
|
||||||
}
|
}
|
||||||
|
|
||||||
reset_headers();
|
reset_ids();
|
||||||
|
|
||||||
extern fn codespan(
|
extern fn codespan(
|
||||||
ob: *mut hoedown_buffer,
|
ob: *mut hoedown_buffer,
|
||||||
|
@ -500,18 +487,6 @@ impl LangString {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// By default this markdown renderer generates anchors for each header in the
|
|
||||||
/// rendered document. The anchor name is the contents of the header separated
|
|
||||||
/// by hyphens, and a thread-local map is used to disambiguate among duplicate
|
|
||||||
/// headers (numbers are appended).
|
|
||||||
///
|
|
||||||
/// This method will reset the local table for these headers. This is typically
|
|
||||||
/// used at the beginning of rendering an entire HTML page to reset from the
|
|
||||||
/// previous state (if any).
|
|
||||||
pub fn reset_headers() {
|
|
||||||
USED_HEADER_MAP.with(|s| s.borrow_mut().clear());
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> fmt::Display for Markdown<'a> {
|
impl<'a> fmt::Display for Markdown<'a> {
|
||||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||||
let Markdown(md) = *self;
|
let Markdown(md) = *self;
|
||||||
|
|
|
@ -342,6 +342,35 @@ impl fmt::Display for IndexItemFunctionType {
|
||||||
thread_local!(static CACHE_KEY: RefCell<Arc<Cache>> = Default::default());
|
thread_local!(static CACHE_KEY: RefCell<Arc<Cache>> = Default::default());
|
||||||
thread_local!(pub static CURRENT_LOCATION_KEY: RefCell<Vec<String>> =
|
thread_local!(pub static CURRENT_LOCATION_KEY: RefCell<Vec<String>> =
|
||||||
RefCell::new(Vec::new()));
|
RefCell::new(Vec::new()));
|
||||||
|
thread_local!(static USED_ID_MAP: RefCell<HashMap<String, usize>> =
|
||||||
|
RefCell::new(HashMap::new()));
|
||||||
|
|
||||||
|
/// This method resets the local table of used ID attributes. This is typically
|
||||||
|
/// used at the beginning of rendering an entire HTML page to reset from the
|
||||||
|
/// previous state (if any).
|
||||||
|
pub fn reset_ids() {
|
||||||
|
USED_ID_MAP.with(|s| s.borrow_mut().clear());
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_unique_id<T, F: FnOnce(&str) -> T>(candidate: String, f: F) -> T {
|
||||||
|
USED_ID_MAP.with(|map| {
|
||||||
|
let (id, ret) = match map.borrow_mut().get_mut(&candidate) {
|
||||||
|
None => {
|
||||||
|
let ret = f(&candidate);
|
||||||
|
(candidate, ret)
|
||||||
|
},
|
||||||
|
Some(a) => {
|
||||||
|
let id = format!("{}-{}", candidate, *a);
|
||||||
|
let ret = f(&id);
|
||||||
|
*a += 1;
|
||||||
|
(id, ret)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
map.borrow_mut().insert(id, 1);
|
||||||
|
ret
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
/// Generates the documentation for `crate` into the directory `dst`
|
/// Generates the documentation for `crate` into the directory `dst`
|
||||||
pub fn run(mut krate: clean::Crate,
|
pub fn run(mut krate: clean::Crate,
|
||||||
|
@ -1274,7 +1303,7 @@ impl Context {
|
||||||
keywords: &keywords,
|
keywords: &keywords,
|
||||||
};
|
};
|
||||||
|
|
||||||
markdown::reset_headers();
|
reset_ids();
|
||||||
|
|
||||||
// We have a huge number of calls to write, so try to alleviate some
|
// 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
|
// of the pain by using a buffered writer instead of invoking the
|
||||||
|
|
|
@ -21,9 +21,10 @@ use rustc::session::search_paths::SearchPaths;
|
||||||
|
|
||||||
use externalfiles::ExternalHtml;
|
use externalfiles::ExternalHtml;
|
||||||
|
|
||||||
|
use html::render::reset_ids;
|
||||||
use html::escape::Escape;
|
use html::escape::Escape;
|
||||||
use html::markdown;
|
use html::markdown;
|
||||||
use html::markdown::{Markdown, MarkdownWithToc, find_testable_code, reset_headers};
|
use html::markdown::{Markdown, MarkdownWithToc, find_testable_code};
|
||||||
use test::{TestOptions, Collector};
|
use test::{TestOptions, Collector};
|
||||||
|
|
||||||
/// Separate any lines at the start of the file that begin with `%`.
|
/// 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,
|
||||||
}
|
}
|
||||||
let title = metadata[0];
|
let title = metadata[0];
|
||||||
|
|
||||||
reset_headers();
|
reset_ids();
|
||||||
|
|
||||||
let rendered = if include_toc {
|
let rendered = if include_toc {
|
||||||
format!("{}", MarkdownWithToc(text))
|
format!("{}", MarkdownWithToc(text))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue