1
Fork 0

Drop RefCell from IdMap in markdown rendering

This commit is contained in:
Mark Rousskov 2019-08-10 18:36:04 -04:00
parent c250b5fd03
commit 1aa0964b54
6 changed files with 22 additions and 31 deletions

View file

@ -6,8 +6,6 @@ use crate::syntax::feature_gate::UnstableFeatures;
use crate::syntax::edition::Edition; use crate::syntax::edition::Edition;
use crate::html::markdown::{IdMap, ErrorCodes, Markdown, Playground}; use crate::html::markdown::{IdMap, ErrorCodes, Markdown, Playground};
use std::cell::RefCell;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct ExternalHtml { pub struct ExternalHtml {
/// Content that will be included inline in the <head> section of a /// Content that will be included inline in the <head> section of a
@ -35,7 +33,7 @@ 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, .map(|m_bc| (ih,
format!("{}{}", bc, Markdown(&m_bc, &[], RefCell::new(id_map), format!("{}{}", bc, Markdown(&m_bc, &[], id_map,
codes, edition, playground).to_string()))) codes, edition, playground).to_string())))
) )
.and_then(|(ih, bc)| .and_then(|(ih, bc)|
@ -45,7 +43,7 @@ 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, .map(|m_ac| (ih, bc,
format!("{}{}", ac, Markdown(&m_ac, &[], RefCell::new(id_map), format!("{}{}", ac, Markdown(&m_ac, &[], id_map,
codes, edition, playground).to_string()))) codes, edition, playground).to_string())))
) )
.map(|(ih, bc, ac)| .map(|(ih, bc, ac)|

View file

@ -9,12 +9,10 @@
//! //!
//! use syntax::edition::Edition; //! use syntax::edition::Edition;
//! use rustdoc::html::markdown::{IdMap, Markdown, ErrorCodes}; //! use rustdoc::html::markdown::{IdMap, Markdown, ErrorCodes};
//! use std::cell::RefCell;
//! //!
//! let s = "My *markdown* _text_"; //! let s = "My *markdown* _text_";
//! let mut id_map = IdMap::new(); //! let mut id_map = IdMap::new();
//! let md = Markdown(s, &[], RefCell::new(&mut id_map), //! let md = Markdown(s, &[], &mut id_map, ErrorCodes::Yes, Edition::Edition2015, &None);
//! ErrorCodes::Yes, Edition::Edition2015, None);
//! let html = md.to_string(); //! let html = md.to_string();
//! // ... something using html //! // ... something using html
//! ``` //! ```
@ -51,7 +49,7 @@ pub struct Markdown<'a>(
/// A list of link replacements. /// A list of link replacements.
pub &'a [(String, String)], pub &'a [(String, String)],
/// The current list of used header IDs. /// The current list of used header IDs.
pub RefCell<&'a mut IdMap>, pub &'a mut IdMap,
/// Whether to allow the use of explicit error codes in doctest lang strings. /// Whether to allow the use of explicit error codes in doctest lang strings.
pub ErrorCodes, pub ErrorCodes,
/// Default edition to use when parsing doctests (to add a `fn main`). /// Default edition to use when parsing doctests (to add a `fn main`).
@ -61,7 +59,7 @@ pub struct Markdown<'a>(
/// A tuple struct like `Markdown` that renders the markdown with a table of contents. /// A tuple struct like `Markdown` that renders the markdown with a table of contents.
pub struct MarkdownWithToc<'a>( pub struct MarkdownWithToc<'a>(
pub &'a str, pub &'a str,
pub RefCell<&'a mut IdMap>, pub &'a mut IdMap,
pub ErrorCodes, pub ErrorCodes,
pub Edition, pub Edition,
pub &'a Option<Playground>, pub &'a Option<Playground>,
@ -69,7 +67,7 @@ pub struct MarkdownWithToc<'a>(
/// A tuple struct like `Markdown` that renders the markdown escaping HTML tags. /// A tuple struct like `Markdown` that renders the markdown escaping HTML tags.
pub struct MarkdownHtml<'a>( pub struct MarkdownHtml<'a>(
pub &'a str, pub &'a str,
pub RefCell<&'a mut IdMap>, pub &'a mut IdMap,
pub ErrorCodes, pub ErrorCodes,
pub Edition, pub Edition,
pub &'a Option<Playground>, pub &'a Option<Playground>,
@ -690,8 +688,7 @@ impl LangString {
impl Markdown<'_> { impl Markdown<'_> {
pub fn to_string(self) -> String { pub fn to_string(self) -> String {
let Markdown(md, links, ids, codes, edition, playground) = self; let Markdown(md, links, mut ids, codes, edition, playground) = 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 String::new(); } if md.is_empty() { return String::new(); }
@ -719,8 +716,7 @@ impl Markdown<'_> {
impl MarkdownWithToc<'_> { impl MarkdownWithToc<'_> {
pub fn to_string(self) -> String { pub fn to_string(self) -> String {
let MarkdownWithToc(md, ref ids, codes, edition, playground) = self; let MarkdownWithToc(md, mut ids, codes, edition, playground) = self;
let mut ids = ids.borrow_mut();
let p = Parser::new_ext(md, opts()); let p = Parser::new_ext(md, opts());
@ -741,8 +737,7 @@ impl MarkdownWithToc<'_> {
impl MarkdownHtml<'_> { impl MarkdownHtml<'_> {
pub fn to_string(self) -> String { pub fn to_string(self) -> String {
let MarkdownHtml(md, ref ids, codes, edition, playground) = self; let MarkdownHtml(md, mut ids, codes, edition, playground) = 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 String::new(); } if md.is_empty() { return String::new(); }

View file

@ -73,8 +73,8 @@ fn test_lang_string_parse() {
fn test_header() { fn test_header() {
fn t(input: &str, expect: &str) { fn t(input: &str, expect: &str) {
let mut map = IdMap::new(); let mut map = IdMap::new();
let output = Markdown(input, &[], RefCell::new(&mut map), let output = Markdown(
ErrorCodes::Yes, DEFAULT_EDITION).to_string(); input, &[], &mut map, ErrorCodes::Yes, DEFAULT_EDITION, &None).to_string();
assert_eq!(output, expect, "original: {}", input); assert_eq!(output, expect, "original: {}", input);
} }
@ -96,8 +96,8 @@ fn test_header() {
fn test_header_ids_multiple_blocks() { fn test_header_ids_multiple_blocks() {
let mut map = IdMap::new(); let mut map = IdMap::new();
fn t(map: &mut IdMap, input: &str, expect: &str) { fn t(map: &mut IdMap, input: &str, expect: &str) {
let output = Markdown(input, &[], RefCell::new(map), let output = Markdown(input, &[], map,
ErrorCodes::Yes, DEFAULT_EDITION).to_string(); ErrorCodes::Yes, DEFAULT_EDITION, &None).to_string();
assert_eq!(output, expect, "original: {}", input); assert_eq!(output, expect, "original: {}", input);
} }
@ -134,8 +134,8 @@ fn test_plain_summary_line() {
fn test_markdown_html_escape() { fn test_markdown_html_escape() {
fn t(input: &str, expect: &str) { fn t(input: &str, expect: &str) {
let mut idmap = IdMap::new(); let mut idmap = IdMap::new();
let output = MarkdownHtml(input, RefCell::new(&mut idmap), let output = MarkdownHtml(input, &mut idmap,
ErrorCodes::Yes, DEFAULT_EDITION).to_string(); ErrorCodes::Yes, DEFAULT_EDITION, &None).to_string();
assert_eq!(output, expect, "original: {}", input); assert_eq!(output, expect, "original: {}", input);
} }

View file

@ -2595,7 +2595,7 @@ fn render_markdown(w: &mut fmt::Formatter<'_>,
write!(w, "<div class='docblock{}'>{}{}</div>", write!(w, "<div class='docblock{}'>{}{}</div>",
if is_hidden { " hidden" } else { "" }, if is_hidden { " hidden" } else { "" },
prefix, prefix,
Markdown(md_text, &links, RefCell::new(&mut ids), Markdown(md_text, &links, &mut ids,
cx.codes, cx.edition, &cx.playground).to_string()) cx.codes, cx.edition, &cx.playground).to_string())
} }
@ -2961,8 +2961,7 @@ fn short_stability(item: &clean::Item, cx: &Context) -> Vec<String> {
if let Some(note) = note { if let Some(note) = note {
let mut ids = cx.id_map.borrow_mut(); let mut ids = cx.id_map.borrow_mut();
let html = MarkdownHtml( let html = MarkdownHtml(&note, &mut ids, error_codes, cx.edition, &cx.playground);
&note, RefCell::new(&mut ids), error_codes, cx.edition, &cx.playground);
message.push_str(&format!(": {}", html.to_string())); message.push_str(&format!(": {}", html.to_string()));
} }
stability.push(format!("<div class='stab deprecated'>{}</div>", message)); stability.push(format!("<div class='stab deprecated'>{}</div>", message));
@ -3013,7 +3012,7 @@ fn short_stability(item: &clean::Item, cx: &Context) -> Vec<String> {
message, message,
MarkdownHtml( MarkdownHtml(
&unstable_reason, &unstable_reason,
RefCell::new(&mut ids), &mut ids,
error_codes, error_codes,
cx.edition, cx.edition,
&cx.playground, &cx.playground,
@ -4247,7 +4246,7 @@ fn render_impl(w: &mut fmt::Formatter<'_>, cx: &Context, i: &Impl, link: AssocIt
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(); 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(), RefCell::new(&mut ids), Markdown(&*dox, &i.impl_item.links(), &mut ids,
cx.codes, cx.edition, &cx.playground).to_string())?; cx.codes, cx.edition, &cx.playground).to_string())?;
} }
} }

View file

@ -1,7 +1,6 @@
use std::fs::File; use std::fs::File;
use std::io::prelude::*; use std::io::prelude::*;
use std::path::PathBuf; use std::path::PathBuf;
use std::cell::RefCell;
use errors; use errors;
use testing; use testing;
@ -83,9 +82,9 @@ pub fn render(
let mut ids = IdMap::new(); 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 !options.markdown_no_toc { let text = if !options.markdown_no_toc {
MarkdownWithToc(text, RefCell::new(&mut ids), error_codes, edition, &playground).to_string() MarkdownWithToc(text, &mut ids, error_codes, edition, &playground).to_string()
} else { } else {
Markdown(text, &[], RefCell::new(&mut ids), error_codes, edition, &playground).to_string() Markdown(text, &[], &mut ids, error_codes, edition, &playground).to_string()
}; };
let err = write!( let err = write!(

View file

@ -100,7 +100,7 @@ impl Formatter for HTMLFormatter {
url: String::from("https://play.rust-lang.org/"), url: String::from("https://play.rust-lang.org/"),
}; };
write!(output, "{}", write!(output, "{}",
Markdown(desc, &[], RefCell::new(&mut id_map), Markdown(desc, &[], &mut id_map,
ErrorCodes::Yes, DEFAULT_EDITION, &Some(playground)).to_string())? ErrorCodes::Yes, DEFAULT_EDITION, &Some(playground)).to_string())?
}, },
None => write!(output, "<p>No description.</p>\n")?, None => write!(output, "<p>No description.</p>\n")?,