Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- [pull #644] Fix a number of em/strong issues (#641, #642, #643)
- [pull #659] Fix a number of safemode issues (#647)
- [pull #665] Rewrite emphasis and strong processing to be more GFM compliant
- [pull #672] Fix nested footnote references (#664)


## python-markdown2 2.5.4
Expand Down
23 changes: 16 additions & 7 deletions lib/markdown2.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,12 +514,7 @@ def convert(self, text: str) -> 'UnicodeWithAttrs':
text = self._run_block_gamut(text)

if "footnotes" in self.extras:
def footnote_sub(match):
normed_id = match.group(1)
self.footnote_ids.append(normed_id)
return str(len(self.footnote_ids))

text = re.sub(r'%s-(.*?)(?=</a></sup>)' % self._footnote_marker, footnote_sub, text)
text = self._do_footnote_marker(text)
text = self._add_footnotes(text)

text = self.postprocess(text)
Expand Down Expand Up @@ -572,6 +567,15 @@ def toc_sort(entry):
rv.metadata = self.metadata
return rv

def _do_footnote_marker(self, text):
def footnote_sub(match):
normed_id = match.group(1)
if normed_id not in self.footnote_ids:
self.footnote_ids.append(normed_id)
return str(len(self.footnote_ids))

return re.sub(r'%s-(.*?)(?=</a></sup>)' % self._footnote_marker, footnote_sub, text)

@mark_stage(Stage.POSTPROCESS)
def postprocess(self, text: str) -> str:
"""A hook for subclasses to do some postprocessing of the html, if
Expand Down Expand Up @@ -2170,7 +2174,12 @@ def _add_footnotes(self, text: str) -> str:
if i != 0:
footer.append('')
footer.append('<li id="fn-%s">' % id)
footer.append(self._run_block_gamut(self.footnotes[id]))
footer.append(
# handle any nested footnote markers
self._do_footnote_marker(
self._run_block_gamut(self.footnotes[id])
)
)
try:
backlink = ('<a href="#fnref-%s" ' +
'class="footnoteBackLink" ' +
Expand Down
14 changes: 14 additions & 0 deletions test/tm-cases/nested_footnotes_issue664.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<p>foo<sup class="footnote-ref" id="fnref-n1"><a href="#fn-n1">1</a></sup></p>

<div class="footnotes">
<hr />
<ol>
<li id="fn-n1">
<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>
</li>

<li id="fn-n2">
<p>Note 2&#160;<a href="#fnref-n2" class="footnoteBackLink" title="Jump back to footnote 2 in the text.">&#8617;</a></p>
</li>
</ol>
</div>
1 change: 1 addition & 0 deletions test/tm-cases/nested_footnotes_issue664.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"extras": ["footnotes"]}
4 changes: 4 additions & 0 deletions test/tm-cases/nested_footnotes_issue664.text
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
foo[^n1]

[^n1]: Note 1[^n2]
[^n2]: Note 2