Skip to content

Commit

Permalink
lint cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
waylan committed Feb 9, 2024
1 parent a8bb59e commit 4ef4ad6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
15 changes: 7 additions & 8 deletions markdown/extensions/toc.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import html
import unicodedata
from copy import deepcopy
from html import unescape as html_unescape
import xml.etree.ElementTree as etree
from typing import TYPE_CHECKING, Any, Iterator, MutableSet

Expand All @@ -39,7 +38,7 @@
def slugify(value: str, separator: str, unicode: bool = False) -> str:
""" Slugify a string, to make it URL friendly. """
# First convert HTML entities to Unicode characters
value = html_unescape(value)
value = html.unescape(value)
if not unicode:
# Replace Extended Latin characters with ASCII, i.e. `žlutý` => `zluty`
value = unicodedata.normalize('NFKD', value)
Expand Down Expand Up @@ -108,8 +107,8 @@ def run_postprocessors(text: str, md: Markdown) -> str:


def render_inner_html(el: etree.Element, md: Markdown) -> str:
""" Fully render inner html of an etree element as a string. """
# The UnescapeTreeprocessor runs after TOC so run here.
""" Fully render inner html of an `etree` element as a string. """
# The `UnescapeTreeprocessor` runs after `toc` extension so run here.
text = md_unescape(md.serializer(el))

# strip parent tag
Expand All @@ -121,7 +120,7 @@ def render_inner_html(el: etree.Element, md: Markdown) -> str:


def copy_element(el: etree.Element, exclude_fnrefs=True) -> etree.Element:
""" Return a deep copy of an etree element, optionally with footnote references removed. """
""" Return a deep copy of an `etree` element, optionally with footnote references removed. """
el = deepcopy(el)
# Remove footnote references, which look like this: `<sup id="fnref:1">...</sup>`.
if exclude_fnrefs:
Expand Down Expand Up @@ -345,8 +344,8 @@ def run(self, doc: etree.Element) -> None:
for el in doc.iter():
if isinstance(el.tag, str) and self.header_rgx.match(el.tag):
self.set_level(el)
html = render_inner_html(copy_element(el), self.md)
text = strip_tags(html)
innerhtml = render_inner_html(copy_element(el), self.md)
text = strip_tags(innerhtml)

# Do not override pre-existing ids
if "id" not in el.attrib:
Expand All @@ -365,7 +364,7 @@ def run(self, doc: etree.Element) -> None:
'level': int(el.tag[-1]),
'id': el.attrib["id"],
'name': text,
'html': html
'html': innerhtml
})

if self.use_anchors:
Expand Down
33 changes: 19 additions & 14 deletions tests/test_syntax/extensions/test_toc.py
Original file line number Diff line number Diff line change
Expand Up @@ -697,20 +697,25 @@ def testHeadingRemoveFootnoteRef(self):
[^1]: footnote
'''
),
self.dedent(
'''
<h1 id="header-1">Header 1<sup id="fnref:1"><a class="footnote-ref" href="#fn:1">1</a></sup></h1>
<h1 id="header-2">Header<sup id="fnref2:1"><a class="footnote-ref" href="#fn:1">1</a></sup> 2</h1>
<h1 id="header-subelement-3">Header <em>subelement</em><sup id="fnref3:1"><a class="footnote-ref" href="#fn:1">1</a></sup> 3</h1>
<div class="footnote">
<hr />
<ol>
<li id="fn:1">
<p>footnote&#160;<a class="footnote-backref" href="#fnref:1" title="Jump back to footnote 1 in the text">&#8617;</a><a class="footnote-backref" href="#fnref2:1" title="Jump back to footnote 1 in the text">&#8617;</a><a class="footnote-backref" href="#fnref3:1" title="Jump back to footnote 1 in the text">&#8617;</a></p>
</li>
</ol>
</div>
'''
(
'<h1 id="header-1">Header 1<sup id="fnref:1"><a class="footnote-ref" href="#fn:1">1</a></sup></h1>\n'
'<h1 id="header-2">Header<sup id="fnref2:1"><a class="footnote-ref" href="#fn:1">1</a></sup> 2</h1>\n'
'<h1 id="header-subelement-3">'
'Header <em>subelement</em><sup id="fnref3:1"><a class="footnote-ref" href="#fn:1">1</a></sup> 3'
'</h1>\n'
'<div class="footnote">\n'
'<hr />\n'
'<ol>\n'
'<li id="fn:1">\n'
'<p>'
'footnote&#160;'
'<a class="footnote-backref" href="#fnref:1" title="Jump back to footnote 1 in the text">&#8617;</a>'
'<a class="footnote-backref" href="#fnref2:1" title="Jump back to footnote 1 in the text">&#8617;</a>'
'<a class="footnote-backref" href="#fnref3:1" title="Jump back to footnote 1 in the text">&#8617;</a>'
'</p>\n'
'</li>\n'
'</ol>\n'
'</div>'
),
expected_attrs={
'toc': (
Expand Down

0 comments on commit 4ef4ad6

Please sign in to comment.