Skip to content

Commit

Permalink
Change Markdown(...) to Markdown { ... }
Browse files Browse the repository at this point in the history
  • Loading branch information
Mukund Lakshman committed Oct 5, 2021
1 parent 4a6aa6e commit 6518a0a
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 47 deletions.
22 changes: 20 additions & 2 deletions src/librustdoc/externalfiles.rs
Expand Up @@ -39,14 +39,32 @@ impl ExternalHtml {
let bc = format!(
"{}{}",
bc,
Markdown(&m_bc, &[], id_map, codes, edition, playground, 0).into_string()
Markdown {
content: &m_bc,
links: &[],
ids: id_map,
error_codes: codes,
edition,
playground,
heading_level: 0
}
.into_string()
);
let ac = load_external_files(after_content, diag)?;
let m_ac = load_external_files(md_after_content, diag)?;
let ac = format!(
"{}{}",
ac,
Markdown(&m_ac, &[], id_map, codes, edition, playground, 0).into_string()
Markdown {
content: &m_ac,
links: &[],
ids: id_map,
error_codes: codes,
edition,
playground,
heading_level: 0
}
.into_string()
);
Some(ExternalHtml { in_header: ih, before_content: bc, after_content: ac })
}
Expand Down
42 changes: 30 additions & 12 deletions src/librustdoc/html/markdown.rs
Expand Up @@ -12,7 +12,15 @@
//!
//! let s = "My *markdown* _text_";
//! let mut id_map = IdMap::new();
//! let md = Markdown(s, &[], &mut id_map, ErrorCodes::Yes, Edition::Edition2015, &None, 0);
//! let md = Markdown {
//! content: s,
//! links: &[],
//! ids: &mut id_map,
//! error_codes: ErrorCodes::Yes,
//! edition: Edition::Edition2015,
//! playground: &None,
//! heading_level: 0
//! };
//! let html = md.into_string();
//! // ... something using html
//! ```
Expand Down Expand Up @@ -69,19 +77,21 @@ pub(crate) fn summary_opts() -> Options {

/// When `to_string` is called, this struct will emit the HTML corresponding to
/// the rendered version of the contained markdown string.
pub struct Markdown<'a>(
pub &'a str,
pub struct Markdown<'a> {
pub content: &'a str,
/// A list of link replacements.
pub &'a [RenderedLink],
pub links: &'a [RenderedLink],
/// The current list of used header IDs.
pub &'a mut IdMap,
pub ids: &'a mut IdMap,
/// Whether to allow the use of explicit error codes in doctest lang strings.
pub ErrorCodes,
pub error_codes: ErrorCodes,
/// Default edition to use when parsing doctests (to add a `fn main`).
pub Edition,
pub &'a Option<Playground>,
pub u32,
);
pub edition: Edition,
pub playground: &'a Option<Playground>,
/// Offset at which we render headings.
/// E.g. if `heading_level: 1`, then `# something` renders an `<h2>` instead of `<h1>`
pub heading_level: u32,
}
/// A tuple struct like `Markdown` that renders the markdown with a table of contents.
crate struct MarkdownWithToc<'a>(
crate &'a str,
Expand Down Expand Up @@ -1010,7 +1020,15 @@ impl LangString {

impl Markdown<'_> {
pub fn into_string(self) -> String {
let Markdown(md, links, mut ids, codes, edition, playground, level) = self;
let Markdown {
content: md,
links,
mut ids,
error_codes: codes,
edition,
playground,
heading_level,
} = self;

// This is actually common enough to special-case
if md.is_empty() {
Expand All @@ -1031,7 +1049,7 @@ impl Markdown<'_> {

let mut s = String::with_capacity(md.len() * 3 / 2);

let p = HeadingLinks::new(p, None, &mut ids, level);
let p = HeadingLinks::new(p, None, &mut ids, heading_level);
let p = Footnotes::new(p);
let p = LinkReplacer::new(p.map(|(ev, _)| ev), links);
let p = TableWrapper::new(p);
Expand Down
24 changes: 20 additions & 4 deletions src/librustdoc/html/markdown/tests.rs
Expand Up @@ -147,8 +147,16 @@ fn test_lang_string_tokenizer() {
fn test_header() {
fn t(input: &str, expect: &str) {
let mut map = IdMap::new();
let output = Markdown(input, &[], &mut map, ErrorCodes::Yes, DEFAULT_EDITION, &None, 0)
.into_string();
let output = Markdown {
content: input,
links: &[],
ids: &mut map,
error_codes: ErrorCodes::Yes,
edition: DEFAULT_EDITION,
playground: &None,
heading_level: 0,
}
.into_string();
assert_eq!(output, expect, "original: {}", input);
}

Expand Down Expand Up @@ -181,8 +189,16 @@ fn test_header() {
fn test_header_ids_multiple_blocks() {
let mut map = IdMap::new();
fn t(map: &mut IdMap, input: &str, expect: &str) {
let output =
Markdown(input, &[], map, ErrorCodes::Yes, DEFAULT_EDITION, &None, 0).into_string();
let output = Markdown {
content: input,
links: &[],
ids: map,
error_codes: ErrorCodes::Yes,
edition: DEFAULT_EDITION,
playground: &None,
heading_level: 0,
}
.into_string();
assert_eq!(output, expect, "original: {}", input);
}

Expand Down
38 changes: 19 additions & 19 deletions src/librustdoc/html/render/mod.rs
Expand Up @@ -498,21 +498,21 @@ fn render_markdown(
cx: &Context<'_>,
md_text: &str,
links: Vec<RenderedLink>,
level: u32,
heading_level: u32,
) {
let mut ids = cx.id_map.borrow_mut();
write!(
w,
"<div class=\"docblock\">{}</div>",
Markdown(
md_text,
&links,
&mut ids,
cx.shared.codes,
cx.shared.edition(),
&cx.shared.playground,
level
)
Markdown {
content: md_text,
links: &links,
ids: &mut ids,
error_codes: cx.shared.codes,
edition: cx.shared.edition(),
playground: &cx.shared.playground,
heading_level,
}
.into_string()
)
}
Expand Down Expand Up @@ -1596,15 +1596,15 @@ fn render_impl(
write!(
w,
"<div class=\"docblock\">{}</div>",
Markdown(
&*dox,
&i.impl_item.links(cx),
&mut ids,
cx.shared.codes,
cx.shared.edition(),
&cx.shared.playground,
0
)
Markdown {
content: &*dox,
links: &i.impl_item.links(cx),
ids: &mut ids,
error_codes: cx.shared.codes,
edition: cx.shared.edition(),
playground: &cx.shared.playground,
heading_level: 0
}
.into_string()
);
}
Expand Down
11 changes: 10 additions & 1 deletion src/librustdoc/markdown.rs
Expand Up @@ -70,7 +70,16 @@ crate fn render<P: AsRef<Path>>(
let text = if !options.markdown_no_toc {
MarkdownWithToc(text, &mut ids, error_codes, edition, &playground).into_string()
} else {
Markdown(text, &[], &mut ids, error_codes, edition, &playground, 0).into_string()
Markdown {
content: text,
links: &[],
ids: &mut ids,
error_codes,
edition,
playground: &playground,
heading_level: 0,
}
.into_string()
};

let err = write!(
Expand Down
18 changes: 9 additions & 9 deletions src/tools/error_index_generator/main.rs
Expand Up @@ -119,15 +119,15 @@ impl Formatter for HTMLFormatter {
write!(
output,
"{}",
Markdown(
desc,
&[],
&mut id_map,
ErrorCodes::Yes,
DEFAULT_EDITION,
&Some(playground),
0
)
Markdown {
content: desc,
links: &[],
ids: &mut id_map,
error_codes: ErrorCodes::Yes,
edition: DEFAULT_EDITION,
playground: &Some(playground),
heading_level: 0
}
.into_string()
)?
}
Expand Down

0 comments on commit 6518a0a

Please sign in to comment.