Skip to content

Commit

Permalink
Don't initialize id-map when rendering md files
Browse files Browse the repository at this point in the history
Adding these "known" values to the table of used ids is only required
when embedding markdown into a rustdoc  html page and may yield
unexpected results when rendering a standalone `*.md` file.
  • Loading branch information
mitaa committed Mar 26, 2016
1 parent 6a76872 commit 8779e7b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/librustdoc/html/markdown.rs
Expand Up @@ -607,15 +607,15 @@ mod tests {
fn issue_17736() {
let markdown = "# title";
format!("{}", Markdown(markdown));
reset_ids();
reset_ids(true);
}

#[test]
fn test_header() {
fn t(input: &str, expect: &str) {
let output = format!("{}", Markdown(input));
assert_eq!(output, expect);
reset_ids();
reset_ids(true);
}

t("# Foo bar", "\n<h1 id='foo-bar' class='section-header'>\
Expand Down Expand Up @@ -654,7 +654,7 @@ mod tests {
<a href='#panics-1'>Panics</a></h1>");
};
test();
reset_ids();
reset_ids(true);
test();
}

Expand Down
14 changes: 10 additions & 4 deletions src/librustdoc/html/render.rs
Expand Up @@ -378,8 +378,14 @@ fn init_ids() -> HashMap<String, usize> {
/// 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() = init_ids());
pub fn reset_ids(embedded: bool) {
USED_ID_MAP.with(|s| {
*s.borrow_mut() = if embedded {
init_ids()
} else {
HashMap::new()
};
});
}

pub fn derive_id(candidate: String) -> String {
Expand Down Expand Up @@ -1280,7 +1286,7 @@ impl Context {
keywords: &keywords,
};

reset_ids();
reset_ids(true);

// 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
Expand Down Expand Up @@ -2748,6 +2754,6 @@ fn test_unique_id() {
assert_eq!(&actual[..], expected);
};
test();
reset_ids();
reset_ids(true);
test();
}
2 changes: 1 addition & 1 deletion src/librustdoc/markdown.rs
Expand Up @@ -83,7 +83,7 @@ pub fn render(input: &str, mut output: PathBuf, matches: &getopts::Matches,
}
let title = metadata[0];

reset_ids();
reset_ids(false);

let rendered = if include_toc {
format!("{}", MarkdownWithToc(text))
Expand Down

0 comments on commit 8779e7b

Please sign in to comment.