mirror of
https://github.com/rust-lang/rust.git
synced 2025-01-03 11:25:18 +00:00
Use ranges returned by pulldown
Co-authored-by: Joshua Nelson <joshua@yottadb.com>
This commit is contained in:
parent
923e3d2400
commit
08f7b0a422
@ -414,11 +414,13 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for LinkReplacer<'a, I> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type SpannedEvent<'a> = (Event<'a>, Range<usize>);
|
||||||
|
|
||||||
/// Make headings links with anchor IDs and build up TOC.
|
/// Make headings links with anchor IDs and build up TOC.
|
||||||
struct HeadingLinks<'a, 'b, 'ids, I> {
|
struct HeadingLinks<'a, 'b, 'ids, I> {
|
||||||
inner: I,
|
inner: I,
|
||||||
toc: Option<&'b mut TocBuilder>,
|
toc: Option<&'b mut TocBuilder>,
|
||||||
buf: VecDeque<Event<'a>>,
|
buf: VecDeque<SpannedEvent<'a>>,
|
||||||
id_map: &'ids mut IdMap,
|
id_map: &'ids mut IdMap,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -428,8 +430,10 @@ impl<'a, 'b, 'ids, I> HeadingLinks<'a, 'b, 'ids, I> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'b, 'ids, I: Iterator<Item = Event<'a>>> Iterator for HeadingLinks<'a, 'b, 'ids, I> {
|
impl<'a, 'b, 'ids, I: Iterator<Item = SpannedEvent<'a>>> Iterator
|
||||||
type Item = Event<'a>;
|
for HeadingLinks<'a, 'b, 'ids, I>
|
||||||
|
{
|
||||||
|
type Item = SpannedEvent<'a>;
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
if let Some(e) = self.buf.pop_front() {
|
if let Some(e) = self.buf.pop_front() {
|
||||||
@ -437,31 +441,29 @@ impl<'a, 'b, 'ids, I: Iterator<Item = Event<'a>>> Iterator for HeadingLinks<'a,
|
|||||||
}
|
}
|
||||||
|
|
||||||
let event = self.inner.next();
|
let event = self.inner.next();
|
||||||
if let Some(Event::Start(Tag::Heading(level))) = event {
|
if let Some((Event::Start(Tag::Heading(level)), _)) = event {
|
||||||
let mut id = String::new();
|
let mut id = String::new();
|
||||||
for event in &mut self.inner {
|
for event in &mut self.inner {
|
||||||
match &event {
|
match &event.0 {
|
||||||
Event::End(Tag::Heading(..)) => break,
|
Event::End(Tag::Heading(..)) => break,
|
||||||
|
Event::Start(Tag::Link(_, _, _)) | Event::End(Tag::Link(..)) => {}
|
||||||
Event::Text(text) | Event::Code(text) => {
|
Event::Text(text) | Event::Code(text) => {
|
||||||
id.extend(text.chars().filter_map(slugify));
|
id.extend(text.chars().filter_map(slugify));
|
||||||
|
self.buf.push_back(event);
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => self.buf.push_back(event),
|
||||||
}
|
|
||||||
match event {
|
|
||||||
Event::Start(Tag::Link(_, _, _)) | Event::End(Tag::Link(..)) => {}
|
|
||||||
event => self.buf.push_back(event),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let id = self.id_map.derive(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();
|
||||||
html::push_html(&mut html_header, self.buf.iter().cloned());
|
html::push_html(&mut html_header, self.buf.iter().map(|(ev, _)| ev.clone()));
|
||||||
let sec = builder.push(level as u32, html_header, id.clone());
|
let sec = builder.push(level as u32, html_header, id.clone());
|
||||||
self.buf.push_front(Event::Html(format!("{} ", sec).into()));
|
self.buf.push_front((Event::Html(format!("{} ", sec).into()), 0..0));
|
||||||
}
|
}
|
||||||
|
|
||||||
self.buf.push_back(Event::Html(format!("</a></h{}>", level).into()));
|
self.buf.push_back((Event::Html(format!("</a></h{}>", level).into()), 0..0));
|
||||||
|
|
||||||
let start_tags = format!(
|
let start_tags = format!(
|
||||||
"<h{level} id=\"{id}\" class=\"section-header\">\
|
"<h{level} id=\"{id}\" class=\"section-header\">\
|
||||||
@ -469,7 +471,7 @@ impl<'a, 'b, 'ids, I: Iterator<Item = Event<'a>>> Iterator for HeadingLinks<'a,
|
|||||||
id = id,
|
id = id,
|
||||||
level = level
|
level = level
|
||||||
);
|
);
|
||||||
return Some(Event::Html(start_tags.into()));
|
return Some((Event::Html(start_tags.into()), 0..0));
|
||||||
}
|
}
|
||||||
event
|
event
|
||||||
}
|
}
|
||||||
@ -560,23 +562,23 @@ impl<'a, I> Footnotes<'a, I> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, I: Iterator<Item = Event<'a>>> Iterator for Footnotes<'a, I> {
|
impl<'a, I: Iterator<Item = SpannedEvent<'a>>> Iterator for Footnotes<'a, I> {
|
||||||
type Item = Event<'a>;
|
type Item = SpannedEvent<'a>;
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
loop {
|
loop {
|
||||||
match self.inner.next() {
|
match self.inner.next() {
|
||||||
Some(Event::FootnoteReference(ref reference)) => {
|
Some((Event::FootnoteReference(ref reference), range)) => {
|
||||||
let entry = self.get_entry(&reference);
|
let entry = self.get_entry(&reference);
|
||||||
let reference = format!(
|
let reference = format!(
|
||||||
"<sup id=\"fnref{0}\"><a href=\"#fn{0}\">{0}</a></sup>",
|
"<sup id=\"fnref{0}\"><a href=\"#fn{0}\">{0}</a></sup>",
|
||||||
(*entry).1
|
(*entry).1
|
||||||
);
|
);
|
||||||
return Some(Event::Html(reference.into()));
|
return Some((Event::Html(reference.into()), range));
|
||||||
}
|
}
|
||||||
Some(Event::Start(Tag::FootnoteDefinition(def))) => {
|
Some((Event::Start(Tag::FootnoteDefinition(def)), _)) => {
|
||||||
let mut content = Vec::new();
|
let mut content = Vec::new();
|
||||||
for event in &mut self.inner {
|
for (event, _) in &mut self.inner {
|
||||||
if let Event::End(Tag::FootnoteDefinition(..)) = event {
|
if let Event::End(Tag::FootnoteDefinition(..)) = event {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -607,7 +609,7 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for Footnotes<'a, I> {
|
|||||||
ret.push_str("</li>");
|
ret.push_str("</li>");
|
||||||
}
|
}
|
||||||
ret.push_str("</ol></div>");
|
ret.push_str("</ol></div>");
|
||||||
return Some(Event::Html(ret.into()));
|
return Some((Event::Html(ret.into()), 0..0));
|
||||||
} else {
|
} else {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
@ -917,13 +919,14 @@ impl Markdown<'_> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let p = Parser::new_with_broken_link_callback(md, opts(), Some(&mut replacer));
|
let p = Parser::new_with_broken_link_callback(md, opts(), Some(&mut replacer));
|
||||||
|
let p = p.into_offset_iter();
|
||||||
|
|
||||||
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);
|
let p = HeadingLinks::new(p, None, &mut ids);
|
||||||
let p = LinkReplacer::new(p, links);
|
|
||||||
let p = CodeBlocks::new(p, codes, edition, playground);
|
|
||||||
let p = Footnotes::new(p);
|
let p = Footnotes::new(p);
|
||||||
|
let p = LinkReplacer::new(p.map(|(ev, _)| ev), links);
|
||||||
|
let p = CodeBlocks::new(p, codes, edition, playground);
|
||||||
html::push_html(&mut s, p);
|
html::push_html(&mut s, p);
|
||||||
|
|
||||||
s
|
s
|
||||||
@ -934,7 +937,7 @@ impl MarkdownWithToc<'_> {
|
|||||||
crate fn into_string(self) -> String {
|
crate fn into_string(self) -> String {
|
||||||
let MarkdownWithToc(md, mut ids, codes, edition, playground) = self;
|
let MarkdownWithToc(md, mut ids, codes, edition, playground) = self;
|
||||||
|
|
||||||
let p = Parser::new_ext(md, opts());
|
let p = Parser::new_ext(md, opts()).into_offset_iter();
|
||||||
|
|
||||||
let mut s = String::with_capacity(md.len() * 3 / 2);
|
let mut s = String::with_capacity(md.len() * 3 / 2);
|
||||||
|
|
||||||
@ -942,8 +945,8 @@ impl MarkdownWithToc<'_> {
|
|||||||
|
|
||||||
{
|
{
|
||||||
let p = HeadingLinks::new(p, Some(&mut toc), &mut ids);
|
let p = HeadingLinks::new(p, Some(&mut toc), &mut ids);
|
||||||
let p = CodeBlocks::new(p, codes, edition, playground);
|
|
||||||
let p = Footnotes::new(p);
|
let p = Footnotes::new(p);
|
||||||
|
let p = CodeBlocks::new(p.map(|(ev, _)| ev), codes, edition, playground);
|
||||||
html::push_html(&mut s, p);
|
html::push_html(&mut s, p);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -959,19 +962,19 @@ impl MarkdownHtml<'_> {
|
|||||||
if md.is_empty() {
|
if md.is_empty() {
|
||||||
return String::new();
|
return String::new();
|
||||||
}
|
}
|
||||||
let p = Parser::new_ext(md, opts());
|
let p = Parser::new_ext(md, opts()).into_offset_iter();
|
||||||
|
|
||||||
// Treat inline HTML as plain text.
|
// Treat inline HTML as plain text.
|
||||||
let p = p.map(|event| match event {
|
let p = p.map(|event| match event.0 {
|
||||||
Event::Html(text) => Event::Text(text),
|
Event::Html(text) => (Event::Text(text), event.1),
|
||||||
_ => event,
|
_ => event,
|
||||||
});
|
});
|
||||||
|
|
||||||
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);
|
let p = HeadingLinks::new(p, None, &mut ids);
|
||||||
let p = CodeBlocks::new(p, codes, edition, playground);
|
|
||||||
let p = Footnotes::new(p);
|
let p = Footnotes::new(p);
|
||||||
|
let p = CodeBlocks::new(p.map(|(ev, _)| ev), codes, edition, playground);
|
||||||
html::push_html(&mut s, p);
|
html::push_html(&mut s, p);
|
||||||
|
|
||||||
s
|
s
|
||||||
@ -1153,7 +1156,8 @@ crate fn markdown_links(md: &str) -> Vec<(String, Option<Range<usize>>)> {
|
|||||||
shortcut_links.push((link.reference.to_owned(), locate(link.reference)));
|
shortcut_links.push((link.reference.to_owned(), locate(link.reference)));
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
let p = Parser::new_with_broken_link_callback(md, opts(), Some(&mut push));
|
let p =
|
||||||
|
Parser::new_with_broken_link_callback(md, opts(), Some(&mut push)).into_offset_iter();
|
||||||
|
|
||||||
// There's no need to thread an IdMap through to here because
|
// There's no need to thread an IdMap through to here because
|
||||||
// the IDs generated aren't going to be emitted anywhere.
|
// the IDs generated aren't going to be emitted anywhere.
|
||||||
@ -1161,7 +1165,7 @@ crate fn markdown_links(md: &str) -> Vec<(String, Option<Range<usize>>)> {
|
|||||||
let iter = Footnotes::new(HeadingLinks::new(p, None, &mut ids));
|
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.0 {
|
||||||
debug!("found link: {}", dest);
|
debug!("found link: {}", dest);
|
||||||
links.push(match dest {
|
links.push(match dest {
|
||||||
CowStr::Borrowed(s) => (s.to_owned(), locate(s)),
|
CowStr::Borrowed(s) => (s.to_owned(), locate(s)),
|
||||||
|
Loading…
Reference in New Issue
Block a user