Skip to content

Commit 1d2f911

Browse files
Fix handling of footnote reference in footnote definition
1 parent a4cedec commit 1d2f911

1 file changed

Lines changed: 34 additions & 26 deletions

File tree

‎src/librustdoc/html/markdown/footnotes.rs‎

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Markdown footnote handling.
22
use std::fmt::Writeas _;
33

4-
use pulldown_cmark::{Event,Tag,TagEnd, html};
4+
use pulldown_cmark::{CowStr,Event,Tag,TagEnd, html};
55
use rustc_data_structures::fx::FxIndexMap;
66

77
usesuper::SpannedEvent;
@@ -21,7 +21,7 @@ struct FootnoteDef<'a> {
2121
id:usize,
2222
}
2323

24-
impl<'a,'b,I>Footnotes<'a,'b,I>{
24+
impl<'a,'b,I:Iterator<Item = SpannedEvent<'a>>>Footnotes<'a,'b,I>{
2525
pub(super)fnnew(iter:I,existing_footnotes:&'bmutusize) -> Self{
2626
Footnotes{inner: iter,footnotes:FxIndexMap::default(), existing_footnotes }
2727
}
@@ -34,31 +34,50 @@ impl<'a, 'b, I> Footnotes<'a, 'b, I> {
3434
// Don't allow changing the ID of existing entrys, but allow changing the contents.
3535
(content,*id)
3636
}
37+
38+
fnhandle_footnote_reference(&mutself,reference:&CowStr<'a>) -> Event<'a>{
39+
// When we see a reference (to a footnote we may not know) the definition of,
40+
// reserve a number for it, and emit a link to that number.
41+
let(_, id) = self.get_entry(reference);
42+
let reference = format!(
43+
"<sup id=\"fnref{0}\"><a href=\"#fn{0}\">{1}</a></sup>",
44+
id,
45+
// Although the ID count is for the whole page, the footnote reference
46+
// are local to the item so we make this ID "local" when displayed.
47+
id - *self.existing_footnotes
48+
);
49+
Event::Html(reference.into())
50+
}
51+
52+
fncollect_footnote_def(&mutself) -> Vec<Event<'a>>{
53+
letmut content = Vec::new();
54+
whileletSome((event, _)) = self.inner.next(){
55+
match event {
56+
Event::End(TagEnd::FootnoteDefinition) => break,
57+
Event::FootnoteReference(ref reference) => {
58+
content.push(self.handle_footnote_reference(reference));
59+
}
60+
event => content.push(event),
61+
}
62+
}
63+
content
64+
}
3765
}
3866

3967
impl<'a,'b,I:Iterator<Item = SpannedEvent<'a>>>IteratorforFootnotes<'a,'b,I>{
4068
typeItem = SpannedEvent<'a>;
4169

4270
fnnext(&mutself) -> Option<Self::Item>{
4371
loop{
44-
matchself.inner.next(){
72+
let next = self.inner.next();
73+
match next {
4574
Some((Event::FootnoteReference(ref reference), range)) => {
46-
// When we see a reference (to a footnote we may not know) the definition of,
47-
// reserve a number for it, and emit a link to that number.
48-
let(_, id) = self.get_entry(reference);
49-
let reference = format!(
50-
"<sup id=\"fnref{0}\"><a href=\"#fn{0}\">{1}</a></sup>",
51-
id,
52-
// Although the ID count is for the whole page, the footnote reference
53-
// are local to the item so we make this ID "local" when displayed.
54-
id - *self.existing_footnotes
55-
);
56-
returnSome((Event::Html(reference.into()), range));
75+
returnSome((self.handle_footnote_reference(reference), range));
5776
}
5877
Some((Event::Start(Tag::FootnoteDefinition(def)), _)) => {
5978
// When we see a footnote definition, collect the assocated content, and store
6079
// that for rendering later.
61-
let content = collect_footnote_def(&mutself.inner);
80+
let content = self.collect_footnote_def();
6281
let(entry_content, _) = self.get_entry(&def);
6382
*entry_content = content;
6483
}
@@ -80,17 +99,6 @@ impl<'a, 'b, I: Iterator<Item = SpannedEvent<'a>>> Iterator for Footnotes<'a, 'b
8099
}
81100
}
82101

83-
fncollect_footnote_def<'a>(events:implIterator<Item = SpannedEvent<'a>>) -> Vec<Event<'a>>{
84-
letmut content = Vec::new();
85-
for(event, _)in events {
86-
ifletEvent::End(TagEnd::FootnoteDefinition) = event {
87-
break;
88-
}
89-
content.push(event);
90-
}
91-
content
92-
}
93-
94102
fnrender_footnotes_defs(mutfootnotes:Vec<FootnoteDef<'_>>) -> String{
95103
letmut ret = String::from("<div class=\"footnotes\"><hr><ol>");
96104

0 commit comments

Comments
 (0)