Skip to content

Commit 0afdaf2

Browse files
Merge pull request #672 from Crozzers/fix-664
Fix nested footnote references (#664)
2 parents d18e2fc + 5699d7b commit 0afdaf2

File tree

5 files changed

+36
-7
lines changed

5 files changed

+36
-7
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- [pull #644] Fix a number of em/strong issues (#641, #642, #643)
88
- [pull #659] Fix a number of safemode issues (#647)
99
- [pull #665] Rewrite emphasis and strong processing to be more GFM compliant
10+
- [pull #672] Fix nested footnote references (#664)
1011

1112

1213
## python-markdown2 2.5.4

lib/markdown2.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -514,12 +514,7 @@ def convert(self, text: str) -> 'UnicodeWithAttrs':
514514
text = self._run_block_gamut(text)
515515

516516
if "footnotes" in self.extras:
517-
def footnote_sub(match):
518-
normed_id = match.group(1)
519-
self.footnote_ids.append(normed_id)
520-
return str(len(self.footnote_ids))
521-
522-
text = re.sub(r'%s-(.*?)(?=</a></sup>)' % self._footnote_marker, footnote_sub, text)
517+
text = self._do_footnote_marker(text)
523518
text = self._add_footnotes(text)
524519

525520
text = self.postprocess(text)
@@ -572,6 +567,15 @@ def toc_sort(entry):
572567
rv.metadata = self.metadata
573568
return rv
574569

570+
def _do_footnote_marker(self, text):
571+
def footnote_sub(match):
572+
normed_id = match.group(1)
573+
if normed_id not in self.footnote_ids:
574+
self.footnote_ids.append(normed_id)
575+
return str(len(self.footnote_ids))
576+
577+
return re.sub(r'%s-(.*?)(?=</a></sup>)' % self._footnote_marker, footnote_sub, text)
578+
575579
@mark_stage(Stage.POSTPROCESS)
576580
def postprocess(self, text: str) -> str:
577581
"""A hook for subclasses to do some postprocessing of the html, if
@@ -2170,7 +2174,12 @@ def _add_footnotes(self, text: str) -> str:
21702174
if i != 0:
21712175
footer.append('')
21722176
footer.append('<li id="fn-%s">' % id)
2173-
footer.append(self._run_block_gamut(self.footnotes[id]))
2177+
footer.append(
2178+
# handle any nested footnote markers
2179+
self._do_footnote_marker(
2180+
self._run_block_gamut(self.footnotes[id])
2181+
)
2182+
)
21742183
try:
21752184
backlink = ('<a href="#fnref-%s" ' +
21762185
'class="footnoteBackLink" ' +
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<p>foo<sup class="footnote-ref" id="fnref-n1"><a href="#fn-n1">1</a></sup></p>
2+
3+
<div class="footnotes">
4+
<hr />
5+
<ol>
6+
<li id="fn-n1">
7+
<p>Note 1<sup class="footnote-ref" id="fnref-n2"><a href="#fn-n2">2</a></sup>&#160;<a href="#fnref-n1" class="footnoteBackLink" title="Jump back to footnote 1 in the text.">&#8617;</a></p>
8+
</li>
9+
10+
<li id="fn-n2">
11+
<p>Note 2&#160;<a href="#fnref-n2" class="footnoteBackLink" title="Jump back to footnote 2 in the text.">&#8617;</a></p>
12+
</li>
13+
</ol>
14+
</div>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"extras": ["footnotes"]}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
foo[^n1]
2+
3+
[^n1]: Note 1[^n2]
4+
[^n2]: Note 2

0 commit comments

Comments
 (0)