Change Markdown(...)
to Markdown { ... }
This commit is contained in:
parent
4a6aa6e406
commit
6518a0a8b9
6 changed files with 108 additions and 47 deletions
|
@ -39,14 +39,32 @@ impl ExternalHtml {
|
||||||
let bc = format!(
|
let bc = format!(
|
||||||
"{}{}",
|
"{}{}",
|
||||||
bc,
|
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 ac = load_external_files(after_content, diag)?;
|
||||||
let m_ac = load_external_files(md_after_content, diag)?;
|
let m_ac = load_external_files(md_after_content, diag)?;
|
||||||
let ac = format!(
|
let ac = format!(
|
||||||
"{}{}",
|
"{}{}",
|
||||||
ac,
|
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 })
|
Some(ExternalHtml { in_header: ih, before_content: bc, after_content: ac })
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,7 +12,15 @@
|
||||||
//!
|
//!
|
||||||
//! 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, &[], &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();
|
//! let html = md.into_string();
|
||||||
//! // ... something using html
|
//! // ... something using html
|
||||||
//! ```
|
//! ```
|
||||||
|
@ -69,19 +77,21 @@ pub(crate) fn summary_opts() -> Options {
|
||||||
|
|
||||||
/// When `to_string` is called, this struct will emit the HTML corresponding to
|
/// When `to_string` is called, this struct will emit the HTML corresponding to
|
||||||
/// the rendered version of the contained markdown string.
|
/// the rendered version of the contained markdown string.
|
||||||
pub struct Markdown<'a>(
|
pub struct Markdown<'a> {
|
||||||
pub &'a str,
|
pub content: &'a str,
|
||||||
/// A list of link replacements.
|
/// A list of link replacements.
|
||||||
pub &'a [RenderedLink],
|
pub links: &'a [RenderedLink],
|
||||||
/// The current list of used header IDs.
|
/// 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.
|
/// 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`).
|
/// Default edition to use when parsing doctests (to add a `fn main`).
|
||||||
pub Edition,
|
pub edition: Edition,
|
||||||
pub &'a Option<Playground>,
|
pub playground: &'a Option<Playground>,
|
||||||
pub u32,
|
/// 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.
|
/// A tuple struct like `Markdown` that renders the markdown with a table of contents.
|
||||||
crate struct MarkdownWithToc<'a>(
|
crate struct MarkdownWithToc<'a>(
|
||||||
crate &'a str,
|
crate &'a str,
|
||||||
|
@ -1010,7 +1020,15 @@ impl LangString {
|
||||||
|
|
||||||
impl Markdown<'_> {
|
impl Markdown<'_> {
|
||||||
pub fn into_string(self) -> String {
|
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
|
// This is actually common enough to special-case
|
||||||
if md.is_empty() {
|
if md.is_empty() {
|
||||||
|
@ -1031,7 +1049,7 @@ impl Markdown<'_> {
|
||||||
|
|
||||||
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, &mut ids, level);
|
let p = HeadingLinks::new(p, None, &mut ids, heading_level);
|
||||||
let p = Footnotes::new(p);
|
let p = Footnotes::new(p);
|
||||||
let p = LinkReplacer::new(p.map(|(ev, _)| ev), links);
|
let p = LinkReplacer::new(p.map(|(ev, _)| ev), links);
|
||||||
let p = TableWrapper::new(p);
|
let p = TableWrapper::new(p);
|
||||||
|
|
|
@ -147,8 +147,16 @@ fn test_lang_string_tokenizer() {
|
||||||
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, &[], &mut map, ErrorCodes::Yes, DEFAULT_EDITION, &None, 0)
|
let output = Markdown {
|
||||||
.into_string();
|
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);
|
assert_eq!(output, expect, "original: {}", input);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -181,8 +189,16 @@ 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 =
|
let output = Markdown {
|
||||||
Markdown(input, &[], map, ErrorCodes::Yes, DEFAULT_EDITION, &None, 0).into_string();
|
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);
|
assert_eq!(output, expect, "original: {}", input);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -498,21 +498,21 @@ fn render_markdown(
|
||||||
cx: &Context<'_>,
|
cx: &Context<'_>,
|
||||||
md_text: &str,
|
md_text: &str,
|
||||||
links: Vec<RenderedLink>,
|
links: Vec<RenderedLink>,
|
||||||
level: u32,
|
heading_level: u32,
|
||||||
) {
|
) {
|
||||||
let mut ids = cx.id_map.borrow_mut();
|
let mut ids = cx.id_map.borrow_mut();
|
||||||
write!(
|
write!(
|
||||||
w,
|
w,
|
||||||
"<div class=\"docblock\">{}</div>",
|
"<div class=\"docblock\">{}</div>",
|
||||||
Markdown(
|
Markdown {
|
||||||
md_text,
|
content: md_text,
|
||||||
&links,
|
links: &links,
|
||||||
&mut ids,
|
ids: &mut ids,
|
||||||
cx.shared.codes,
|
error_codes: cx.shared.codes,
|
||||||
cx.shared.edition(),
|
edition: cx.shared.edition(),
|
||||||
&cx.shared.playground,
|
playground: &cx.shared.playground,
|
||||||
level
|
heading_level,
|
||||||
)
|
}
|
||||||
.into_string()
|
.into_string()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -1596,15 +1596,15 @@ fn render_impl(
|
||||||
write!(
|
write!(
|
||||||
w,
|
w,
|
||||||
"<div class=\"docblock\">{}</div>",
|
"<div class=\"docblock\">{}</div>",
|
||||||
Markdown(
|
Markdown {
|
||||||
&*dox,
|
content: &*dox,
|
||||||
&i.impl_item.links(cx),
|
links: &i.impl_item.links(cx),
|
||||||
&mut ids,
|
ids: &mut ids,
|
||||||
cx.shared.codes,
|
error_codes: cx.shared.codes,
|
||||||
cx.shared.edition(),
|
edition: cx.shared.edition(),
|
||||||
&cx.shared.playground,
|
playground: &cx.shared.playground,
|
||||||
0
|
heading_level: 0
|
||||||
)
|
}
|
||||||
.into_string()
|
.into_string()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -70,7 +70,16 @@ crate fn render<P: AsRef<Path>>(
|
||||||
let text = if !options.markdown_no_toc {
|
let text = if !options.markdown_no_toc {
|
||||||
MarkdownWithToc(text, &mut ids, error_codes, edition, &playground).into_string()
|
MarkdownWithToc(text, &mut ids, error_codes, edition, &playground).into_string()
|
||||||
} else {
|
} 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!(
|
let err = write!(
|
||||||
|
|
|
@ -119,15 +119,15 @@ impl Formatter for HTMLFormatter {
|
||||||
write!(
|
write!(
|
||||||
output,
|
output,
|
||||||
"{}",
|
"{}",
|
||||||
Markdown(
|
Markdown {
|
||||||
desc,
|
content: desc,
|
||||||
&[],
|
links: &[],
|
||||||
&mut id_map,
|
ids: &mut id_map,
|
||||||
ErrorCodes::Yes,
|
error_codes: ErrorCodes::Yes,
|
||||||
DEFAULT_EDITION,
|
edition: DEFAULT_EDITION,
|
||||||
&Some(playground),
|
playground: &Some(playground),
|
||||||
0
|
heading_level: 0
|
||||||
)
|
}
|
||||||
.into_string()
|
.into_string()
|
||||||
)?
|
)?
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue