Remove global derive_id and reset_ids functions
Previously these functions relied on TLS but we can instead thread the relevant state through explicitly.
This commit is contained in:
parent
2216db9de7
commit
7bea518d3a
|
@ -13,7 +13,8 @@ use std::path::Path;
|
||||||
use std::str;
|
use std::str;
|
||||||
use errors;
|
use errors;
|
||||||
use syntax::feature_gate::UnstableFeatures;
|
use syntax::feature_gate::UnstableFeatures;
|
||||||
use html::markdown::{ErrorCodes, Markdown};
|
use html::markdown::{IdMap, ErrorCodes, Markdown};
|
||||||
|
use std::cell::RefCell;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct ExternalHtml {
|
pub struct ExternalHtml {
|
||||||
|
@ -30,7 +31,8 @@ pub struct ExternalHtml {
|
||||||
|
|
||||||
impl ExternalHtml {
|
impl ExternalHtml {
|
||||||
pub fn load(in_header: &[String], before_content: &[String], after_content: &[String],
|
pub fn load(in_header: &[String], before_content: &[String], after_content: &[String],
|
||||||
md_before_content: &[String], md_after_content: &[String], diag: &errors::Handler)
|
md_before_content: &[String], md_after_content: &[String], diag: &errors::Handler,
|
||||||
|
id_map: &mut IdMap)
|
||||||
-> Option<ExternalHtml> {
|
-> Option<ExternalHtml> {
|
||||||
let codes = ErrorCodes::from(UnstableFeatures::from_environment().is_nightly_build());
|
let codes = ErrorCodes::from(UnstableFeatures::from_environment().is_nightly_build());
|
||||||
load_external_files(in_header, diag)
|
load_external_files(in_header, diag)
|
||||||
|
@ -40,7 +42,8 @@ impl ExternalHtml {
|
||||||
)
|
)
|
||||||
.and_then(|(ih, bc)|
|
.and_then(|(ih, bc)|
|
||||||
load_external_files(md_before_content, diag)
|
load_external_files(md_before_content, diag)
|
||||||
.map(|m_bc| (ih, format!("{}{}", bc, Markdown(&m_bc, &[], codes))))
|
.map(|m_bc| (ih,
|
||||||
|
format!("{}{}", bc, Markdown(&m_bc, &[], RefCell::new(id_map), codes))))
|
||||||
)
|
)
|
||||||
.and_then(|(ih, bc)|
|
.and_then(|(ih, bc)|
|
||||||
load_external_files(after_content, diag)
|
load_external_files(after_content, diag)
|
||||||
|
@ -48,7 +51,8 @@ impl ExternalHtml {
|
||||||
)
|
)
|
||||||
.and_then(|(ih, bc, ac)|
|
.and_then(|(ih, bc, ac)|
|
||||||
load_external_files(md_after_content, diag)
|
load_external_files(md_after_content, diag)
|
||||||
.map(|m_ac| (ih, bc, format!("{}{}", ac, Markdown(&m_ac, &[], codes))))
|
.map(|m_ac| (ih, bc,
|
||||||
|
format!("{}{}", ac, Markdown(&m_ac, &[], RefCell::new(id_map), codes))))
|
||||||
)
|
)
|
||||||
.map(|(ih, bc, ac)|
|
.map(|(ih, bc, ac)|
|
||||||
ExternalHtml {
|
ExternalHtml {
|
||||||
|
|
|
@ -18,10 +18,12 @@
|
||||||
//! ```
|
//! ```
|
||||||
//! #![feature(rustc_private)]
|
//! #![feature(rustc_private)]
|
||||||
//!
|
//!
|
||||||
//! use rustdoc::html::markdown::{Markdown, ErrorCodes};
|
//! use rustdoc::html::markdown::{IdMap, Markdown, ErrorCodes};
|
||||||
|
//! use std::cell::RefCell;
|
||||||
//!
|
//!
|
||||||
//! let s = "My *markdown* _text_";
|
//! let s = "My *markdown* _text_";
|
||||||
//! let html = format!("{}", Markdown(s, &[], ErrorCodes::Yes));
|
//! let mut id_map = IdMap::new();
|
||||||
|
//! let html = format!("{}", Markdown(s, &[], RefCell::new(&mut id_map), ErrorCodes::Yes));
|
||||||
//! // ... something using html
|
//! // ... something using html
|
||||||
//! ```
|
//! ```
|
||||||
|
|
||||||
|
@ -35,7 +37,6 @@ use std::borrow::Cow;
|
||||||
use std::ops::Range;
|
use std::ops::Range;
|
||||||
use std::str;
|
use std::str;
|
||||||
|
|
||||||
use html::render::derive_id;
|
|
||||||
use html::toc::TocBuilder;
|
use html::toc::TocBuilder;
|
||||||
use html::highlight;
|
use html::highlight;
|
||||||
use test;
|
use test;
|
||||||
|
@ -47,12 +48,13 @@ use pulldown_cmark::{Options, OPTION_ENABLE_FOOTNOTES, OPTION_ENABLE_TABLES};
|
||||||
/// 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.
|
||||||
/// The second parameter is a list of link replacements
|
/// The second parameter is a list of link replacements
|
||||||
pub struct Markdown<'a>(pub &'a str, pub &'a [(String, String)], pub ErrorCodes);
|
pub struct Markdown<'a>(
|
||||||
|
pub &'a str, pub &'a [(String, String)], pub RefCell<&'a mut IdMap>, pub ErrorCodes);
|
||||||
/// A unit struct like `Markdown`, that renders the markdown with a
|
/// A unit struct like `Markdown`, that renders the markdown with a
|
||||||
/// table of contents.
|
/// table of contents.
|
||||||
pub struct MarkdownWithToc<'a>(pub &'a str, pub ErrorCodes);
|
pub struct MarkdownWithToc<'a>(pub &'a str, pub RefCell<&'a mut IdMap>, pub ErrorCodes);
|
||||||
/// A unit struct like `Markdown`, that renders the markdown escaping HTML tags.
|
/// A unit struct like `Markdown`, that renders the markdown escaping HTML tags.
|
||||||
pub struct MarkdownHtml<'a>(pub &'a str, pub ErrorCodes);
|
pub struct MarkdownHtml<'a>(pub &'a str, pub RefCell<&'a mut IdMap>, pub ErrorCodes);
|
||||||
/// A unit struct like `Markdown`, that renders only the first paragraph.
|
/// A unit struct like `Markdown`, that renders only the first paragraph.
|
||||||
pub struct MarkdownSummaryLine<'a>(pub &'a str, pub &'a [(String, String)]);
|
pub struct MarkdownSummaryLine<'a>(pub &'a str, pub &'a [(String, String)]);
|
||||||
|
|
||||||
|
@ -287,23 +289,25 @@ impl<'a, 'b, I: Iterator<Item = Event<'a>>> Iterator for LinkReplacer<'a, 'b, I>
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Make headings links with anchor ids and build up TOC.
|
/// Make headings links with anchor ids and build up TOC.
|
||||||
struct HeadingLinks<'a, 'b, I: Iterator<Item = Event<'a>>> {
|
struct HeadingLinks<'a, 'b, 'ids, I: Iterator<Item = Event<'a>>> {
|
||||||
inner: I,
|
inner: I,
|
||||||
toc: Option<&'b mut TocBuilder>,
|
toc: Option<&'b mut TocBuilder>,
|
||||||
buf: VecDeque<Event<'a>>,
|
buf: VecDeque<Event<'a>>,
|
||||||
|
id_map: &'ids mut IdMap,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'b, I: Iterator<Item = Event<'a>>> HeadingLinks<'a, 'b, I> {
|
impl<'a, 'b, 'ids, I: Iterator<Item = Event<'a>>> HeadingLinks<'a, 'b, 'ids, I> {
|
||||||
fn new(iter: I, toc: Option<&'b mut TocBuilder>) -> Self {
|
fn new(iter: I, toc: Option<&'b mut TocBuilder>, ids: &'ids mut IdMap) -> Self {
|
||||||
HeadingLinks {
|
HeadingLinks {
|
||||||
inner: iter,
|
inner: iter,
|
||||||
toc,
|
toc,
|
||||||
buf: VecDeque::new(),
|
buf: VecDeque::new(),
|
||||||
|
id_map: ids,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'b, I: Iterator<Item = Event<'a>>> Iterator for HeadingLinks<'a, 'b, I> {
|
impl<'a, 'b, 'ids, I: Iterator<Item = Event<'a>>> Iterator for HeadingLinks<'a, 'b, 'ids, I> {
|
||||||
type Item = Event<'a>;
|
type Item = Event<'a>;
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
|
@ -322,7 +326,7 @@ impl<'a, 'b, I: Iterator<Item = Event<'a>>> Iterator for HeadingLinks<'a, 'b, I>
|
||||||
}
|
}
|
||||||
self.buf.push_back(event);
|
self.buf.push_back(event);
|
||||||
}
|
}
|
||||||
let id = derive_id(id);
|
let id = self.id_map.derive(id);
|
||||||
|
|
||||||
if let Some(ref mut builder) = self.toc {
|
if let Some(ref mut builder) = self.toc {
|
||||||
let mut html_header = String::new();
|
let mut html_header = String::new();
|
||||||
|
@ -641,7 +645,8 @@ impl LangString {
|
||||||
|
|
||||||
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, links, codes) = *self;
|
let Markdown(md, links, ref ids, codes) = *self;
|
||||||
|
let mut ids = ids.borrow_mut();
|
||||||
|
|
||||||
// This is actually common enough to special-case
|
// This is actually common enough to special-case
|
||||||
if md.is_empty() { return Ok(()) }
|
if md.is_empty() { return Ok(()) }
|
||||||
|
@ -661,7 +666,7 @@ impl<'a> fmt::Display for Markdown<'a> {
|
||||||
|
|
||||||
let mut s = String::with_capacity(md.len() * 3 / 2);
|
let mut s = String::with_capacity(md.len() * 3 / 2);
|
||||||
|
|
||||||
let p = HeadingLinks::new(p, None);
|
let p = HeadingLinks::new(p, None, &mut ids);
|
||||||
let p = LinkReplacer::new(p, links);
|
let p = LinkReplacer::new(p, links);
|
||||||
let p = CodeBlocks::new(p, codes);
|
let p = CodeBlocks::new(p, codes);
|
||||||
let p = Footnotes::new(p);
|
let p = Footnotes::new(p);
|
||||||
|
@ -673,7 +678,8 @@ impl<'a> fmt::Display for Markdown<'a> {
|
||||||
|
|
||||||
impl<'a> fmt::Display for MarkdownWithToc<'a> {
|
impl<'a> fmt::Display for MarkdownWithToc<'a> {
|
||||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||||
let MarkdownWithToc(md, codes) = *self;
|
let MarkdownWithToc(md, ref ids, codes) = *self;
|
||||||
|
let mut ids = ids.borrow_mut();
|
||||||
|
|
||||||
let mut opts = Options::empty();
|
let mut opts = Options::empty();
|
||||||
opts.insert(OPTION_ENABLE_TABLES);
|
opts.insert(OPTION_ENABLE_TABLES);
|
||||||
|
@ -686,7 +692,7 @@ impl<'a> fmt::Display for MarkdownWithToc<'a> {
|
||||||
let mut toc = TocBuilder::new();
|
let mut toc = TocBuilder::new();
|
||||||
|
|
||||||
{
|
{
|
||||||
let p = HeadingLinks::new(p, Some(&mut toc));
|
let p = HeadingLinks::new(p, Some(&mut toc), &mut ids);
|
||||||
let p = CodeBlocks::new(p, codes);
|
let p = CodeBlocks::new(p, codes);
|
||||||
let p = Footnotes::new(p);
|
let p = Footnotes::new(p);
|
||||||
html::push_html(&mut s, p);
|
html::push_html(&mut s, p);
|
||||||
|
@ -700,7 +706,8 @@ impl<'a> fmt::Display for MarkdownWithToc<'a> {
|
||||||
|
|
||||||
impl<'a> fmt::Display for MarkdownHtml<'a> {
|
impl<'a> fmt::Display for MarkdownHtml<'a> {
|
||||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||||
let MarkdownHtml(md, codes) = *self;
|
let MarkdownHtml(md, ref ids, codes) = *self;
|
||||||
|
let mut ids = ids.borrow_mut();
|
||||||
|
|
||||||
// This is actually common enough to special-case
|
// This is actually common enough to special-case
|
||||||
if md.is_empty() { return Ok(()) }
|
if md.is_empty() { return Ok(()) }
|
||||||
|
@ -718,7 +725,7 @@ impl<'a> fmt::Display for MarkdownHtml<'a> {
|
||||||
|
|
||||||
let mut s = String::with_capacity(md.len() * 3 / 2);
|
let mut s = String::with_capacity(md.len() * 3 / 2);
|
||||||
|
|
||||||
let p = HeadingLinks::new(p, None);
|
let p = HeadingLinks::new(p, None, &mut ids);
|
||||||
let p = CodeBlocks::new(p, codes);
|
let p = CodeBlocks::new(p, codes);
|
||||||
let p = Footnotes::new(p);
|
let p = Footnotes::new(p);
|
||||||
html::push_html(&mut s, p);
|
html::push_html(&mut s, p);
|
||||||
|
@ -835,7 +842,10 @@ pub fn markdown_links(md: &str) -> Vec<(String, Option<Range<usize>>)> {
|
||||||
let p = Parser::new_with_broken_link_callback(md, opts,
|
let p = Parser::new_with_broken_link_callback(md, opts,
|
||||||
Some(&push));
|
Some(&push));
|
||||||
|
|
||||||
let iter = Footnotes::new(HeadingLinks::new(p, None));
|
// There's no need to thread an IdMap through to here because
|
||||||
|
// the IDs generated aren't going to be emitted anywhere.
|
||||||
|
let mut ids = IdMap::new();
|
||||||
|
let iter = Footnotes::new(HeadingLinks::new(p, None, &mut ids));
|
||||||
|
|
||||||
for ev in iter {
|
for ev in iter {
|
||||||
if let Event::Start(Tag::Link(dest, _)) = ev {
|
if let Event::Start(Tag::Link(dest, _)) = ev {
|
||||||
|
@ -854,11 +864,67 @@ pub fn markdown_links(md: &str) -> Vec<(String, Option<Range<usize>>)> {
|
||||||
links
|
links
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct IdMap {
|
||||||
|
map: HashMap<String, usize>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl IdMap {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
IdMap::default()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn populate<I: IntoIterator<Item=String>>(&mut self, ids: I) {
|
||||||
|
for id in ids {
|
||||||
|
let _ = self.derive(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn reset(&mut self) {
|
||||||
|
self.map = HashMap::new();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn derive(&mut self, candidate: String) -> String {
|
||||||
|
let id = match self.map.get_mut(&candidate) {
|
||||||
|
None => candidate,
|
||||||
|
Some(a) => {
|
||||||
|
let id = format!("{}-{}", candidate, *a);
|
||||||
|
*a += 1;
|
||||||
|
id
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
self.map.insert(id.clone(), 1);
|
||||||
|
id
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
#[test]
|
||||||
|
fn test_unique_id() {
|
||||||
|
let input = ["foo", "examples", "examples", "method.into_iter","examples",
|
||||||
|
"method.into_iter", "foo", "main", "search", "methods",
|
||||||
|
"examples", "method.into_iter", "assoc_type.Item", "assoc_type.Item"];
|
||||||
|
let expected = ["foo", "examples", "examples-1", "method.into_iter", "examples-2",
|
||||||
|
"method.into_iter-1", "foo-1", "main", "search", "methods",
|
||||||
|
"examples-3", "method.into_iter-2", "assoc_type.Item", "assoc_type.Item-1"];
|
||||||
|
|
||||||
|
let map = RefCell::new(IdMap::new());
|
||||||
|
let test = || {
|
||||||
|
let mut map = map.borrow_mut();
|
||||||
|
let actual: Vec<String> = input.iter().map(|s| map.derive(s.to_string())).collect();
|
||||||
|
assert_eq!(&actual[..], expected);
|
||||||
|
};
|
||||||
|
test();
|
||||||
|
map.borrow_mut().reset();
|
||||||
|
test();
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::{ErrorCodes, LangString, Markdown, MarkdownHtml};
|
use super::{ErrorCodes, LangString, Markdown, MarkdownHtml, IdMap};
|
||||||
use super::plain_summary_line;
|
use super::plain_summary_line;
|
||||||
use html::render::reset_ids;
|
use std::cell::RefCell;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_lang_string_parse() {
|
fn test_lang_string_parse() {
|
||||||
|
@ -901,19 +967,12 @@ mod tests {
|
||||||
t("text,no_run", false, true, false, false, false, false, false, v());
|
t("text,no_run", false, true, false, false, false, false, false, v());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn issue_17736() {
|
|
||||||
let markdown = "# title";
|
|
||||||
Markdown(markdown, &[], ErrorCodes::Yes).to_string();
|
|
||||||
reset_ids(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_header() {
|
fn test_header() {
|
||||||
fn t(input: &str, expect: &str) {
|
fn t(input: &str, expect: &str) {
|
||||||
let output = Markdown(input, &[], ErrorCodes::Yes).to_string();
|
let mut map = IdMap::new();
|
||||||
|
let output = Markdown(input, &[], RefCell::new(&mut map), ErrorCodes::Yes).to_string();
|
||||||
assert_eq!(output, expect, "original: {}", input);
|
assert_eq!(output, expect, "original: {}", input);
|
||||||
reset_ids(true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
t("# Foo bar", "<h1 id=\"foo-bar\" class=\"section-header\">\
|
t("# Foo bar", "<h1 id=\"foo-bar\" class=\"section-header\">\
|
||||||
|
@ -932,28 +991,24 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_header_ids_multiple_blocks() {
|
fn test_header_ids_multiple_blocks() {
|
||||||
fn t(input: &str, expect: &str) {
|
let mut map = IdMap::new();
|
||||||
let output = Markdown(input, &[], ErrorCodes::Yes).to_string();
|
fn t(map: &mut IdMap, input: &str, expect: &str) {
|
||||||
|
let output = Markdown(input, &[], RefCell::new(map), ErrorCodes::Yes).to_string();
|
||||||
assert_eq!(output, expect, "original: {}", input);
|
assert_eq!(output, expect, "original: {}", input);
|
||||||
}
|
}
|
||||||
|
|
||||||
let test = || {
|
t(&mut map, "# Example", "<h1 id=\"example\" class=\"section-header\">\
|
||||||
t("# Example", "<h1 id=\"example\" class=\"section-header\">\
|
|
||||||
<a href=\"#example\">Example</a></h1>");
|
<a href=\"#example\">Example</a></h1>");
|
||||||
t("# Panics", "<h1 id=\"panics\" class=\"section-header\">\
|
t(&mut map, "# Panics", "<h1 id=\"panics\" class=\"section-header\">\
|
||||||
<a href=\"#panics\">Panics</a></h1>");
|
<a href=\"#panics\">Panics</a></h1>");
|
||||||
t("# Example", "<h1 id=\"example-1\" class=\"section-header\">\
|
t(&mut map, "# Example", "<h1 id=\"example-1\" class=\"section-header\">\
|
||||||
<a href=\"#example-1\">Example</a></h1>");
|
<a href=\"#example-1\">Example</a></h1>");
|
||||||
t("# Main", "<h1 id=\"main-1\" class=\"section-header\">\
|
t(&mut map, "# Main", "<h1 id=\"main\" class=\"section-header\">\
|
||||||
<a href=\"#main-1\">Main</a></h1>");
|
<a href=\"#main\">Main</a></h1>");
|
||||||
t("# Example", "<h1 id=\"example-2\" class=\"section-header\">\
|
t(&mut map, "# Example", "<h1 id=\"example-2\" class=\"section-header\">\
|
||||||
<a href=\"#example-2\">Example</a></h1>");
|
<a href=\"#example-2\">Example</a></h1>");
|
||||||
t("# Panics", "<h1 id=\"panics-1\" class=\"section-header\">\
|
t(&mut map, "# Panics", "<h1 id=\"panics-1\" class=\"section-header\">\
|
||||||
<a href=\"#panics-1\">Panics</a></h1>");
|
<a href=\"#panics-1\">Panics</a></h1>");
|
||||||
};
|
|
||||||
test();
|
|
||||||
reset_ids(true);
|
|
||||||
test();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -974,7 +1029,8 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_markdown_html_escape() {
|
fn test_markdown_html_escape() {
|
||||||
fn t(input: &str, expect: &str) {
|
fn t(input: &str, expect: &str) {
|
||||||
let output = MarkdownHtml(input, ErrorCodes::Yes).to_string();
|
let mut idmap = IdMap::new();
|
||||||
|
let output = MarkdownHtml(input, RefCell::new(&mut idmap), ErrorCodes::Yes).to_string();
|
||||||
assert_eq!(output, expect, "original: {}", input);
|
assert_eq!(output, expect, "original: {}", input);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -50,6 +50,7 @@ use std::mem;
|
||||||
use std::path::{PathBuf, Path, Component};
|
use std::path::{PathBuf, Path, Component};
|
||||||
use std::str;
|
use std::str;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
use std::rc::Rc;
|
||||||
|
|
||||||
use externalfiles::ExternalHtml;
|
use externalfiles::ExternalHtml;
|
||||||
|
|
||||||
|
@ -73,7 +74,7 @@ use html::format::{GenericBounds, WhereClause, href, AbiSpace};
|
||||||
use html::format::{VisSpace, Method, UnsafetySpace, MutableSpace};
|
use html::format::{VisSpace, Method, UnsafetySpace, MutableSpace};
|
||||||
use html::format::fmt_impl_for_trait_page;
|
use html::format::fmt_impl_for_trait_page;
|
||||||
use html::item_type::ItemType;
|
use html::item_type::ItemType;
|
||||||
use html::markdown::{self, Markdown, MarkdownHtml, MarkdownSummaryLine, ErrorCodes};
|
use html::markdown::{self, Markdown, MarkdownHtml, MarkdownSummaryLine, ErrorCodes, IdMap};
|
||||||
use html::{highlight, layout};
|
use html::{highlight, layout};
|
||||||
|
|
||||||
use minifier;
|
use minifier;
|
||||||
|
@ -89,7 +90,7 @@ pub type NameDoc = (String, Option<String>);
|
||||||
/// easily cloned because it is cloned per work-job (about once per item in the
|
/// easily cloned because it is cloned per work-job (about once per item in the
|
||||||
/// rustdoc tree).
|
/// rustdoc tree).
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Context {
|
struct Context {
|
||||||
/// Current hierarchy of components leading down to what's currently being
|
/// Current hierarchy of components leading down to what's currently being
|
||||||
/// rendered
|
/// rendered
|
||||||
pub current: Vec<String>,
|
pub current: Vec<String>,
|
||||||
|
@ -101,10 +102,12 @@ pub struct Context {
|
||||||
/// publicly reused items to redirect to the right location.
|
/// publicly reused items to redirect to the right location.
|
||||||
pub render_redirect_pages: bool,
|
pub render_redirect_pages: bool,
|
||||||
pub codes: ErrorCodes,
|
pub codes: ErrorCodes,
|
||||||
|
/// The map used to ensure all generated 'id=' attributes are unique.
|
||||||
|
id_map: Rc<RefCell<IdMap>>,
|
||||||
pub shared: Arc<SharedContext>,
|
pub shared: Arc<SharedContext>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct SharedContext {
|
struct SharedContext {
|
||||||
/// The path to the crate root source minus the file name.
|
/// The path to the crate root source minus the file name.
|
||||||
/// Used for simplifying paths to the highlighted source code files.
|
/// Used for simplifying paths to the highlighted source code files.
|
||||||
pub src_root: PathBuf,
|
pub src_root: PathBuf,
|
||||||
|
@ -452,9 +455,8 @@ impl ToJson 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>> = RefCell::new(Vec::new()));
|
thread_local!(pub static CURRENT_LOCATION_KEY: RefCell<Vec<String>> = RefCell::new(Vec::new()));
|
||||||
thread_local!(pub static USED_ID_MAP: RefCell<FxHashMap<String, usize>> = RefCell::new(init_ids()));
|
|
||||||
|
|
||||||
fn init_ids() -> FxHashMap<String, usize> {
|
pub fn initial_ids() -> Vec<String> {
|
||||||
[
|
[
|
||||||
"main",
|
"main",
|
||||||
"search",
|
"search",
|
||||||
|
@ -472,36 +474,7 @@ fn init_ids() -> FxHashMap<String, usize> {
|
||||||
"methods",
|
"methods",
|
||||||
"deref-methods",
|
"deref-methods",
|
||||||
"implementations",
|
"implementations",
|
||||||
].into_iter().map(|id| (String::from(*id), 1)).collect()
|
].into_iter().map(|id| (String::from(*id))).collect()
|
||||||
}
|
|
||||||
|
|
||||||
/// 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(embedded: bool) {
|
|
||||||
USED_ID_MAP.with(|s| {
|
|
||||||
*s.borrow_mut() = if embedded {
|
|
||||||
init_ids()
|
|
||||||
} else {
|
|
||||||
FxHashMap()
|
|
||||||
};
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn derive_id(candidate: String) -> String {
|
|
||||||
USED_ID_MAP.with(|map| {
|
|
||||||
let id = match map.borrow_mut().get_mut(&candidate) {
|
|
||||||
None => candidate,
|
|
||||||
Some(a) => {
|
|
||||||
let id = format!("{}-{}", candidate, *a);
|
|
||||||
*a += 1;
|
|
||||||
id
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
map.borrow_mut().insert(id.clone(), 1);
|
|
||||||
id
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Generates the documentation for `crate` into the directory `dst`
|
/// Generates the documentation for `crate` into the directory `dst`
|
||||||
|
@ -515,7 +488,8 @@ pub fn run(mut krate: clean::Crate,
|
||||||
renderinfo: RenderInfo,
|
renderinfo: RenderInfo,
|
||||||
sort_modules_alphabetically: bool,
|
sort_modules_alphabetically: bool,
|
||||||
themes: Vec<PathBuf>,
|
themes: Vec<PathBuf>,
|
||||||
enable_minification: bool) -> Result<(), Error> {
|
enable_minification: bool,
|
||||||
|
id_map: IdMap) -> Result<(), Error> {
|
||||||
let src_root = match krate.src {
|
let src_root = match krate.src {
|
||||||
FileName::Real(ref p) => match p.parent() {
|
FileName::Real(ref p) => match p.parent() {
|
||||||
Some(p) => p.to_path_buf(),
|
Some(p) => p.to_path_buf(),
|
||||||
|
@ -584,6 +558,7 @@ pub fn run(mut krate: clean::Crate,
|
||||||
dst,
|
dst,
|
||||||
render_redirect_pages: false,
|
render_redirect_pages: false,
|
||||||
codes: ErrorCodes::from(UnstableFeatures::from_environment().is_nightly_build()),
|
codes: ErrorCodes::from(UnstableFeatures::from_environment().is_nightly_build()),
|
||||||
|
id_map: Rc::new(RefCell::new(id_map)),
|
||||||
shared: Arc::new(scx),
|
shared: Arc::new(scx),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -1711,6 +1686,11 @@ impl<'a> fmt::Display for Settings<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Context {
|
impl Context {
|
||||||
|
fn derive_id(&self, id: String) -> String {
|
||||||
|
let mut map = self.id_map.borrow_mut();
|
||||||
|
map.derive(id)
|
||||||
|
}
|
||||||
|
|
||||||
/// String representation of how to get back to the root path of the 'doc/'
|
/// String representation of how to get back to the root path of the 'doc/'
|
||||||
/// folder in terms of a relative URL.
|
/// folder in terms of a relative URL.
|
||||||
fn root_path(&self) -> String {
|
fn root_path(&self) -> String {
|
||||||
|
@ -1865,7 +1845,10 @@ impl Context {
|
||||||
resource_suffix: &self.shared.resource_suffix,
|
resource_suffix: &self.shared.resource_suffix,
|
||||||
};
|
};
|
||||||
|
|
||||||
reset_ids(true);
|
{
|
||||||
|
self.id_map.borrow_mut().reset();
|
||||||
|
self.id_map.borrow_mut().populate(initial_ids());
|
||||||
|
}
|
||||||
|
|
||||||
if !self.render_redirect_pages {
|
if !self.render_redirect_pages {
|
||||||
layout::render(writer, &self.shared.layout, &page,
|
layout::render(writer, &self.shared.layout, &page,
|
||||||
|
@ -2222,16 +2205,18 @@ fn document(w: &mut fmt::Formatter, cx: &Context, item: &clean::Item) -> fmt::Re
|
||||||
|
|
||||||
/// Render md_text as markdown.
|
/// Render md_text as markdown.
|
||||||
fn render_markdown(w: &mut fmt::Formatter,
|
fn render_markdown(w: &mut fmt::Formatter,
|
||||||
|
cx: &Context,
|
||||||
md_text: &str,
|
md_text: &str,
|
||||||
links: Vec<(String, String)>,
|
links: Vec<(String, String)>,
|
||||||
prefix: &str,
|
prefix: &str)
|
||||||
codes: ErrorCodes)
|
|
||||||
-> fmt::Result {
|
-> fmt::Result {
|
||||||
write!(w, "<div class='docblock'>{}{}</div>", prefix, Markdown(md_text, &links, codes))
|
let mut ids = cx.id_map.borrow_mut();
|
||||||
|
write!(w, "<div class='docblock'>{}{}</div>",
|
||||||
|
prefix, Markdown(md_text, &links, RefCell::new(&mut ids), cx.codes))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn document_short(w: &mut fmt::Formatter, item: &clean::Item, link: AssocItemLink,
|
fn document_short(w: &mut fmt::Formatter, cx: &Context, item: &clean::Item, link: AssocItemLink,
|
||||||
prefix: &str, codes: ErrorCodes) -> fmt::Result {
|
prefix: &str) -> fmt::Result {
|
||||||
if let Some(s) = item.doc_value() {
|
if let Some(s) = item.doc_value() {
|
||||||
let markdown = if s.contains('\n') {
|
let markdown = if s.contains('\n') {
|
||||||
format!("{} [Read more]({})",
|
format!("{} [Read more]({})",
|
||||||
|
@ -2239,7 +2224,7 @@ fn document_short(w: &mut fmt::Formatter, item: &clean::Item, link: AssocItemLin
|
||||||
} else {
|
} else {
|
||||||
plain_summary_line(Some(s)).to_string()
|
plain_summary_line(Some(s)).to_string()
|
||||||
};
|
};
|
||||||
render_markdown(w, &markdown, item.links(), prefix, codes)?;
|
render_markdown(w, cx, &markdown, item.links(), prefix)?;
|
||||||
} else if !prefix.is_empty() {
|
} else if !prefix.is_empty() {
|
||||||
write!(w, "<div class='docblock'>{}</div>", prefix)?;
|
write!(w, "<div class='docblock'>{}</div>", prefix)?;
|
||||||
}
|
}
|
||||||
|
@ -2265,7 +2250,7 @@ fn document_full(w: &mut fmt::Formatter, item: &clean::Item,
|
||||||
cx: &Context, prefix: &str) -> fmt::Result {
|
cx: &Context, prefix: &str) -> fmt::Result {
|
||||||
if let Some(s) = cx.shared.maybe_collapsed_doc_value(item) {
|
if let Some(s) = cx.shared.maybe_collapsed_doc_value(item) {
|
||||||
debug!("Doc block: =====\n{}\n=====", s);
|
debug!("Doc block: =====\n{}\n=====", s);
|
||||||
render_markdown(w, &*s, item.links(), prefix, cx.codes)?;
|
render_markdown(w, cx, &*s, item.links(), prefix)?;
|
||||||
} else if !prefix.is_empty() {
|
} else if !prefix.is_empty() {
|
||||||
write!(w, "<div class='docblock'>{}</div>", prefix)?;
|
write!(w, "<div class='docblock'>{}</div>", prefix)?;
|
||||||
}
|
}
|
||||||
|
@ -2431,7 +2416,7 @@ fn item_module(w: &mut fmt::Formatter, cx: &Context,
|
||||||
let (short, name) = item_ty_to_strs(&myty.unwrap());
|
let (short, name) = item_ty_to_strs(&myty.unwrap());
|
||||||
write!(w, "<h2 id='{id}' class='section-header'>\
|
write!(w, "<h2 id='{id}' class='section-header'>\
|
||||||
<a href=\"#{id}\">{name}</a></h2>\n<table>",
|
<a href=\"#{id}\">{name}</a></h2>\n<table>",
|
||||||
id = derive_id(short.to_owned()), name = name)?;
|
id = cx.derive_id(short.to_owned()), name = name)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
match myitem.inner {
|
match myitem.inner {
|
||||||
|
@ -2526,7 +2511,8 @@ fn short_stability(item: &clean::Item, cx: &Context, show_reason: bool) -> Vec<S
|
||||||
} else {
|
} else {
|
||||||
String::new()
|
String::new()
|
||||||
};
|
};
|
||||||
let html = MarkdownHtml(&deprecated_reason, error_codes);
|
let mut ids = cx.id_map.borrow_mut();
|
||||||
|
let html = MarkdownHtml(&deprecated_reason, RefCell::new(&mut ids), error_codes);
|
||||||
let text = if stability::deprecation_in_effect(&stab.deprecated_since) {
|
let text = if stability::deprecation_in_effect(&stab.deprecated_since) {
|
||||||
format!("Deprecated{}{}", since, html)
|
format!("Deprecated{}{}", since, html)
|
||||||
} else {
|
} else {
|
||||||
|
@ -2557,12 +2543,14 @@ fn short_stability(item: &clean::Item, cx: &Context, show_reason: bool) -> Vec<S
|
||||||
</div>",
|
</div>",
|
||||||
unstable_extra));
|
unstable_extra));
|
||||||
} else {
|
} else {
|
||||||
|
let mut ids = cx.id_map.borrow_mut();
|
||||||
let text = format!("<summary><span class=microscope>🔬</span> \
|
let text = format!("<summary><span class=microscope>🔬</span> \
|
||||||
This is a nightly-only experimental API. {}\
|
This is a nightly-only experimental API. {}\
|
||||||
</summary>{}",
|
</summary>{}",
|
||||||
unstable_extra,
|
unstable_extra,
|
||||||
MarkdownHtml(
|
MarkdownHtml(
|
||||||
&stab.unstable_reason,
|
&stab.unstable_reason,
|
||||||
|
RefCell::new(&mut ids),
|
||||||
error_codes));
|
error_codes));
|
||||||
stability.push(format!("<div class='stab unstable'><details>{}</details></div>",
|
stability.push(format!("<div class='stab unstable'><details>{}</details></div>",
|
||||||
text));
|
text));
|
||||||
|
@ -2583,14 +2571,15 @@ fn short_stability(item: &clean::Item, cx: &Context, show_reason: bool) -> Vec<S
|
||||||
String::new()
|
String::new()
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let mut ids = cx.id_map.borrow_mut();
|
||||||
let text = if stability::deprecation_in_effect(&depr.since) {
|
let text = if stability::deprecation_in_effect(&depr.since) {
|
||||||
format!("Deprecated{}{}",
|
format!("Deprecated{}{}",
|
||||||
since,
|
since,
|
||||||
MarkdownHtml(¬e, error_codes))
|
MarkdownHtml(¬e, RefCell::new(&mut ids), error_codes))
|
||||||
} else {
|
} else {
|
||||||
format!("Deprecating in {}{}",
|
format!("Deprecating in {}{}",
|
||||||
Escape(&depr.since),
|
Escape(&depr.since),
|
||||||
MarkdownHtml(¬e, error_codes))
|
MarkdownHtml(¬e, RefCell::new(&mut ids), error_codes))
|
||||||
};
|
};
|
||||||
stability.push(format!("<div class='stab deprecated'>{}</div>", text))
|
stability.push(format!("<div class='stab deprecated'>{}</div>", text))
|
||||||
}
|
}
|
||||||
|
@ -2831,8 +2820,8 @@ fn item_trait(
|
||||||
-> fmt::Result {
|
-> fmt::Result {
|
||||||
let name = m.name.as_ref().unwrap();
|
let name = m.name.as_ref().unwrap();
|
||||||
let item_type = m.type_();
|
let item_type = m.type_();
|
||||||
let id = derive_id(format!("{}.{}", item_type, name));
|
let id = cx.derive_id(format!("{}.{}", item_type, name));
|
||||||
let ns_id = derive_id(format!("{}.{}", name, item_type.name_space()));
|
let ns_id = cx.derive_id(format!("{}.{}", name, item_type.name_space()));
|
||||||
write!(w, "{extra}<h3 id='{id}' class='method'>\
|
write!(w, "{extra}<h3 id='{id}' class='method'>\
|
||||||
<span id='{ns_id}' class='invisible'><code>",
|
<span id='{ns_id}' class='invisible'><code>",
|
||||||
extra = render_spotlight_traits(m)?,
|
extra = render_spotlight_traits(m)?,
|
||||||
|
@ -3187,10 +3176,10 @@ fn item_struct(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
|
||||||
document_non_exhaustive_header(it))?;
|
document_non_exhaustive_header(it))?;
|
||||||
document_non_exhaustive(w, it)?;
|
document_non_exhaustive(w, it)?;
|
||||||
for (field, ty) in fields {
|
for (field, ty) in fields {
|
||||||
let id = derive_id(format!("{}.{}",
|
let id = cx.derive_id(format!("{}.{}",
|
||||||
ItemType::StructField,
|
ItemType::StructField,
|
||||||
field.name.as_ref().unwrap()));
|
field.name.as_ref().unwrap()));
|
||||||
let ns_id = derive_id(format!("{}.{}",
|
let ns_id = cx.derive_id(format!("{}.{}",
|
||||||
field.name.as_ref().unwrap(),
|
field.name.as_ref().unwrap(),
|
||||||
ItemType::StructField.name_space()));
|
ItemType::StructField.name_space()));
|
||||||
write!(w, "<span id=\"{id}\" class=\"{item_type} small-section-header\">
|
write!(w, "<span id=\"{id}\" class=\"{item_type} small-section-header\">
|
||||||
|
@ -3321,10 +3310,10 @@ fn item_enum(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
|
||||||
document_non_exhaustive_header(it))?;
|
document_non_exhaustive_header(it))?;
|
||||||
document_non_exhaustive(w, it)?;
|
document_non_exhaustive(w, it)?;
|
||||||
for variant in &e.variants {
|
for variant in &e.variants {
|
||||||
let id = derive_id(format!("{}.{}",
|
let id = cx.derive_id(format!("{}.{}",
|
||||||
ItemType::Variant,
|
ItemType::Variant,
|
||||||
variant.name.as_ref().unwrap()));
|
variant.name.as_ref().unwrap()));
|
||||||
let ns_id = derive_id(format!("{}.{}",
|
let ns_id = cx.derive_id(format!("{}.{}",
|
||||||
variant.name.as_ref().unwrap(),
|
variant.name.as_ref().unwrap(),
|
||||||
ItemType::Variant.name_space()));
|
ItemType::Variant.name_space()));
|
||||||
write!(w, "<span id=\"{id}\" class=\"variant small-section-header\">\
|
write!(w, "<span id=\"{id}\" class=\"variant small-section-header\">\
|
||||||
|
@ -3352,7 +3341,7 @@ fn item_enum(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
|
||||||
if let clean::VariantItem(Variant {
|
if let clean::VariantItem(Variant {
|
||||||
kind: VariantKind::Struct(ref s)
|
kind: VariantKind::Struct(ref s)
|
||||||
}) = variant.inner {
|
}) = variant.inner {
|
||||||
let variant_id = derive_id(format!("{}.{}.fields",
|
let variant_id = cx.derive_id(format!("{}.{}.fields",
|
||||||
ItemType::Variant,
|
ItemType::Variant,
|
||||||
variant.name.as_ref().unwrap()));
|
variant.name.as_ref().unwrap()));
|
||||||
write!(w, "<span class='docblock autohide sub-variant' id='{id}'>",
|
write!(w, "<span class='docblock autohide sub-variant' id='{id}'>",
|
||||||
|
@ -3362,10 +3351,10 @@ fn item_enum(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
|
||||||
for field in &s.fields {
|
for field in &s.fields {
|
||||||
use clean::StructFieldItem;
|
use clean::StructFieldItem;
|
||||||
if let StructFieldItem(ref ty) = field.inner {
|
if let StructFieldItem(ref ty) = field.inner {
|
||||||
let id = derive_id(format!("variant.{}.field.{}",
|
let id = cx.derive_id(format!("variant.{}.field.{}",
|
||||||
variant.name.as_ref().unwrap(),
|
variant.name.as_ref().unwrap(),
|
||||||
field.name.as_ref().unwrap()));
|
field.name.as_ref().unwrap()));
|
||||||
let ns_id = derive_id(format!("{}.{}.{}.{}",
|
let ns_id = cx.derive_id(format!("{}.{}.{}.{}",
|
||||||
variant.name.as_ref().unwrap(),
|
variant.name.as_ref().unwrap(),
|
||||||
ItemType::Variant.name_space(),
|
ItemType::Variant.name_space(),
|
||||||
field.name.as_ref().unwrap(),
|
field.name.as_ref().unwrap(),
|
||||||
|
@ -3794,7 +3783,7 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
|
||||||
render_mode: RenderMode, outer_version: Option<&str>,
|
render_mode: RenderMode, outer_version: Option<&str>,
|
||||||
show_def_docs: bool) -> fmt::Result {
|
show_def_docs: bool) -> fmt::Result {
|
||||||
if render_mode == RenderMode::Normal {
|
if render_mode == RenderMode::Normal {
|
||||||
let id = derive_id(match i.inner_impl().trait_ {
|
let id = cx.derive_id(match i.inner_impl().trait_ {
|
||||||
Some(ref t) => format!("impl-{}", small_url_encode(&format!("{:#}", t))),
|
Some(ref t) => format!("impl-{}", small_url_encode(&format!("{:#}", t))),
|
||||||
None => "impl".to_string(),
|
None => "impl".to_string(),
|
||||||
});
|
});
|
||||||
|
@ -3814,8 +3803,9 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
|
||||||
}
|
}
|
||||||
write!(w, "</span></td></tr></tbody></table></h3>")?;
|
write!(w, "</span></td></tr></tbody></table></h3>")?;
|
||||||
if let Some(ref dox) = cx.shared.maybe_collapsed_doc_value(&i.impl_item) {
|
if let Some(ref dox) = cx.shared.maybe_collapsed_doc_value(&i.impl_item) {
|
||||||
|
let mut ids = cx.id_map.borrow_mut();
|
||||||
write!(w, "<div class='docblock'>{}</div>",
|
write!(w, "<div class='docblock'>{}</div>",
|
||||||
Markdown(&*dox, &i.impl_item.links(), cx.codes))?;
|
Markdown(&*dox, &i.impl_item.links(), RefCell::new(&mut ids), cx.codes))?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3836,8 +3826,8 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
|
||||||
clean::TyMethodItem(clean::TyMethod{ ref decl, .. }) => {
|
clean::TyMethodItem(clean::TyMethod{ ref decl, .. }) => {
|
||||||
// Only render when the method is not static or we allow static methods
|
// Only render when the method is not static or we allow static methods
|
||||||
if render_method_item {
|
if render_method_item {
|
||||||
let id = derive_id(format!("{}.{}", item_type, name));
|
let id = cx.derive_id(format!("{}.{}", item_type, name));
|
||||||
let ns_id = derive_id(format!("{}.{}", name, item_type.name_space()));
|
let ns_id = cx.derive_id(format!("{}.{}", name, item_type.name_space()));
|
||||||
write!(w, "<h4 id='{}' class=\"{}\">", id, item_type)?;
|
write!(w, "<h4 id='{}' class=\"{}\">", id, item_type)?;
|
||||||
write!(w, "{}", spotlight_decl(decl)?)?;
|
write!(w, "{}", spotlight_decl(decl)?)?;
|
||||||
write!(w, "<span id='{}' class='invisible'>", ns_id)?;
|
write!(w, "<span id='{}' class='invisible'>", ns_id)?;
|
||||||
|
@ -3858,24 +3848,24 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
clean::TypedefItem(ref tydef, _) => {
|
clean::TypedefItem(ref tydef, _) => {
|
||||||
let id = derive_id(format!("{}.{}", ItemType::AssociatedType, name));
|
let id = cx.derive_id(format!("{}.{}", ItemType::AssociatedType, name));
|
||||||
let ns_id = derive_id(format!("{}.{}", name, item_type.name_space()));
|
let ns_id = cx.derive_id(format!("{}.{}", name, item_type.name_space()));
|
||||||
write!(w, "<h4 id='{}' class=\"{}\">", id, item_type)?;
|
write!(w, "<h4 id='{}' class=\"{}\">", id, item_type)?;
|
||||||
write!(w, "<span id='{}' class='invisible'><code>", ns_id)?;
|
write!(w, "<span id='{}' class='invisible'><code>", ns_id)?;
|
||||||
assoc_type(w, item, &Vec::new(), Some(&tydef.type_), link.anchor(&id))?;
|
assoc_type(w, item, &Vec::new(), Some(&tydef.type_), link.anchor(&id))?;
|
||||||
write!(w, "</code></span></h4>\n")?;
|
write!(w, "</code></span></h4>\n")?;
|
||||||
}
|
}
|
||||||
clean::AssociatedConstItem(ref ty, ref default) => {
|
clean::AssociatedConstItem(ref ty, ref default) => {
|
||||||
let id = derive_id(format!("{}.{}", item_type, name));
|
let id = cx.derive_id(format!("{}.{}", item_type, name));
|
||||||
let ns_id = derive_id(format!("{}.{}", name, item_type.name_space()));
|
let ns_id = cx.derive_id(format!("{}.{}", name, item_type.name_space()));
|
||||||
write!(w, "<h4 id='{}' class=\"{}\">", id, item_type)?;
|
write!(w, "<h4 id='{}' class=\"{}\">", id, item_type)?;
|
||||||
write!(w, "<span id='{}' class='invisible'><code>", ns_id)?;
|
write!(w, "<span id='{}' class='invisible'><code>", ns_id)?;
|
||||||
assoc_const(w, item, ty, default.as_ref(), link.anchor(&id))?;
|
assoc_const(w, item, ty, default.as_ref(), link.anchor(&id))?;
|
||||||
write!(w, "</code></span></h4>\n")?;
|
write!(w, "</code></span></h4>\n")?;
|
||||||
}
|
}
|
||||||
clean::AssociatedTypeItem(ref bounds, ref default) => {
|
clean::AssociatedTypeItem(ref bounds, ref default) => {
|
||||||
let id = derive_id(format!("{}.{}", item_type, name));
|
let id = cx.derive_id(format!("{}.{}", item_type, name));
|
||||||
let ns_id = derive_id(format!("{}.{}", name, item_type.name_space()));
|
let ns_id = cx.derive_id(format!("{}.{}", name, item_type.name_space()));
|
||||||
write!(w, "<h4 id='{}' class=\"{}\">", id, item_type)?;
|
write!(w, "<h4 id='{}' class=\"{}\">", id, item_type)?;
|
||||||
write!(w, "<span id='{}' class='invisible'><code>", ns_id)?;
|
write!(w, "<span id='{}' class='invisible'><code>", ns_id)?;
|
||||||
assoc_type(w, item, bounds, default.as_ref(), link.anchor(&id))?;
|
assoc_type(w, item, bounds, default.as_ref(), link.anchor(&id))?;
|
||||||
|
@ -3901,7 +3891,7 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
|
||||||
} else if show_def_docs {
|
} else if show_def_docs {
|
||||||
// In case the item isn't documented,
|
// In case the item isn't documented,
|
||||||
// provide short documentation from the trait.
|
// provide short documentation from the trait.
|
||||||
document_short(w, it, link, &prefix, cx.codes)?;
|
document_short(w, cx, it, link, &prefix)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -3913,7 +3903,7 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
|
||||||
} else {
|
} else {
|
||||||
document_stability(w, cx, item)?;
|
document_stability(w, cx, item)?;
|
||||||
if show_def_docs {
|
if show_def_docs {
|
||||||
document_short(w, item, link, &prefix, cx.codes)?;
|
document_short(w, cx, item, link, &prefix)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4719,25 +4709,6 @@ pub fn cache() -> Arc<Cache> {
|
||||||
CACHE_KEY.with(|c| c.borrow().clone())
|
CACHE_KEY.with(|c| c.borrow().clone())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
#[test]
|
|
||||||
fn test_unique_id() {
|
|
||||||
let input = ["foo", "examples", "examples", "method.into_iter","examples",
|
|
||||||
"method.into_iter", "foo", "main", "search", "methods",
|
|
||||||
"examples", "method.into_iter", "assoc_type.Item", "assoc_type.Item"];
|
|
||||||
let expected = ["foo", "examples", "examples-1", "method.into_iter", "examples-2",
|
|
||||||
"method.into_iter-1", "foo-1", "main-1", "search-1", "methods-1",
|
|
||||||
"examples-3", "method.into_iter-2", "assoc_type.Item", "assoc_type.Item-1"];
|
|
||||||
|
|
||||||
let test = || {
|
|
||||||
let actual: Vec<String> = input.iter().map(|s| derive_id(s.to_string())).collect();
|
|
||||||
assert_eq!(&actual[..], expected);
|
|
||||||
};
|
|
||||||
test();
|
|
||||||
reset_ids(true);
|
|
||||||
test();
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_name_key() {
|
fn test_name_key() {
|
||||||
|
|
|
@ -500,12 +500,14 @@ fn main_args(args: &[String]) -> isize {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let mut id_map = html::markdown::IdMap::new();
|
||||||
|
id_map.populate(html::render::initial_ids());
|
||||||
let external_html = match ExternalHtml::load(
|
let external_html = match ExternalHtml::load(
|
||||||
&matches.opt_strs("html-in-header"),
|
&matches.opt_strs("html-in-header"),
|
||||||
&matches.opt_strs("html-before-content"),
|
&matches.opt_strs("html-before-content"),
|
||||||
&matches.opt_strs("html-after-content"),
|
&matches.opt_strs("html-after-content"),
|
||||||
&matches.opt_strs("markdown-before-content"),
|
&matches.opt_strs("markdown-before-content"),
|
||||||
&matches.opt_strs("markdown-after-content"), &diag) {
|
&matches.opt_strs("markdown-after-content"), &diag, &mut id_map) {
|
||||||
Some(eh) => eh,
|
Some(eh) => eh,
|
||||||
None => return 3,
|
None => return 3,
|
||||||
};
|
};
|
||||||
|
@ -562,7 +564,7 @@ fn main_args(args: &[String]) -> isize {
|
||||||
renderinfo,
|
renderinfo,
|
||||||
sort_modules_alphabetically,
|
sort_modules_alphabetically,
|
||||||
themes,
|
themes,
|
||||||
enable_minification)
|
enable_minification, id_map)
|
||||||
.expect("failed to generate documentation");
|
.expect("failed to generate documentation");
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,6 +12,7 @@ use std::default::Default;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::prelude::*;
|
use std::io::prelude::*;
|
||||||
use std::path::{PathBuf, Path};
|
use std::path::{PathBuf, Path};
|
||||||
|
use std::cell::RefCell;
|
||||||
|
|
||||||
use errors;
|
use errors;
|
||||||
use getopts;
|
use getopts;
|
||||||
|
@ -24,10 +25,9 @@ use syntax::edition::Edition;
|
||||||
|
|
||||||
use externalfiles::{ExternalHtml, LoadStringError, load_string};
|
use externalfiles::{ExternalHtml, LoadStringError, load_string};
|
||||||
|
|
||||||
use html::render::reset_ids;
|
|
||||||
use html::escape::Escape;
|
use html::escape::Escape;
|
||||||
use html::markdown;
|
use html::markdown;
|
||||||
use html::markdown::{ErrorCodes, Markdown, MarkdownWithToc, find_testable_code};
|
use html::markdown::{ErrorCodes, IdMap, 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 `# ` or `%`.
|
/// Separate any lines at the start of the file that begin with `# ` or `%`.
|
||||||
|
@ -87,13 +87,12 @@ pub fn render(input: &Path, mut output: PathBuf, matches: &getopts::Matches,
|
||||||
}
|
}
|
||||||
let title = metadata[0];
|
let title = metadata[0];
|
||||||
|
|
||||||
reset_ids(false);
|
let mut ids = IdMap::new();
|
||||||
|
|
||||||
let error_codes = ErrorCodes::from(UnstableFeatures::from_environment().is_nightly_build());
|
let error_codes = ErrorCodes::from(UnstableFeatures::from_environment().is_nightly_build());
|
||||||
let text = if include_toc {
|
let text = if include_toc {
|
||||||
MarkdownWithToc(text, error_codes).to_string()
|
MarkdownWithToc(text, RefCell::new(&mut ids), error_codes).to_string()
|
||||||
} else {
|
} else {
|
||||||
Markdown(text, &[], error_codes).to_string()
|
Markdown(text, &[], RefCell::new(&mut ids), error_codes).to_string()
|
||||||
};
|
};
|
||||||
|
|
||||||
let err = write!(
|
let err = write!(
|
||||||
|
|
|
@ -21,10 +21,11 @@ use std::fs::{read_dir, File};
|
||||||
use std::io::{Read, Write};
|
use std::io::{Read, Write};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
use std::cell::RefCell;
|
||||||
|
|
||||||
use syntax::diagnostics::metadata::{get_metadata_dir, ErrorMetadataMap, ErrorMetadata};
|
use syntax::diagnostics::metadata::{get_metadata_dir, ErrorMetadataMap, ErrorMetadata};
|
||||||
|
|
||||||
use rustdoc::html::markdown::{Markdown, ErrorCodes, PLAYGROUND};
|
use rustdoc::html::markdown::{Markdown, IdMap, ErrorCodes, PLAYGROUND};
|
||||||
use rustc_serialize::json;
|
use rustc_serialize::json;
|
||||||
|
|
||||||
enum OutputFormat {
|
enum OutputFormat {
|
||||||
|
@ -36,7 +37,7 @@ enum OutputFormat {
|
||||||
impl OutputFormat {
|
impl OutputFormat {
|
||||||
fn from(format: &str) -> OutputFormat {
|
fn from(format: &str) -> OutputFormat {
|
||||||
match &*format.to_lowercase() {
|
match &*format.to_lowercase() {
|
||||||
"html" => OutputFormat::HTML(HTMLFormatter),
|
"html" => OutputFormat::HTML(HTMLFormatter(RefCell::new(IdMap::new()))),
|
||||||
"markdown" => OutputFormat::Markdown(MarkdownFormatter),
|
"markdown" => OutputFormat::Markdown(MarkdownFormatter),
|
||||||
s => OutputFormat::Unknown(s.to_owned()),
|
s => OutputFormat::Unknown(s.to_owned()),
|
||||||
}
|
}
|
||||||
|
@ -51,7 +52,7 @@ trait Formatter {
|
||||||
fn footer(&self, output: &mut dyn Write) -> Result<(), Box<dyn Error>>;
|
fn footer(&self, output: &mut dyn Write) -> Result<(), Box<dyn Error>>;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct HTMLFormatter;
|
struct HTMLFormatter(RefCell<IdMap>);
|
||||||
struct MarkdownFormatter;
|
struct MarkdownFormatter;
|
||||||
|
|
||||||
impl Formatter for HTMLFormatter {
|
impl Formatter for HTMLFormatter {
|
||||||
|
@ -100,7 +101,11 @@ impl Formatter for HTMLFormatter {
|
||||||
|
|
||||||
// Description rendered as markdown.
|
// Description rendered as markdown.
|
||||||
match info.description {
|
match info.description {
|
||||||
Some(ref desc) => write!(output, "{}", Markdown(desc, &[], ErrorCodes::Yes))?,
|
Some(ref desc) => {
|
||||||
|
let mut id_map = self.0.borrow_mut();
|
||||||
|
write!(output, "{}",
|
||||||
|
Markdown(desc, &[], RefCell::new(&mut id_map), ErrorCodes::Yes))?
|
||||||
|
},
|
||||||
None => write!(output, "<p>No description.</p>\n")?,
|
None => write!(output, "<p>No description.</p>\n")?,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue