|
|
@ -3,7 +3,7 @@ |
|
|
|
|
|
|
|
|
"""Functions related to HTML parsing/modification.""" |
|
|
"""Functions related to HTML parsing/modification.""" |
|
|
|
|
|
|
|
|
from bs4 import BeautifulSoup |
|
|
|
|
|
|
|
|
from bs4 import BeautifulSoup, Tag |
|
|
|
|
|
|
|
|
from tildes.lib.string import convert_to_url_slug |
|
|
from tildes.lib.string import convert_to_url_slug |
|
|
|
|
|
|
|
|
@ -15,6 +15,9 @@ def add_anchors_to_headings(html: str) -> str: |
|
|
headings = soup.find_all(["h1", "h2", "h3", "h4", "h5", "h6"]) |
|
|
headings = soup.find_all(["h1", "h2", "h3", "h4", "h5", "h6"]) |
|
|
|
|
|
|
|
|
for heading in headings: |
|
|
for heading in headings: |
|
|
|
|
|
if not isinstance(heading, Tag): |
|
|
|
|
|
continue |
|
|
|
|
|
|
|
|
# generate an anchor from the string contents of the heading |
|
|
# generate an anchor from the string contents of the heading |
|
|
anchor = convert_to_url_slug("".join(heading.strings)) |
|
|
anchor = convert_to_url_slug("".join(heading.strings)) |
|
|
|
|
|
|
|
|
@ -28,5 +31,8 @@ def add_anchors_to_headings(html: str) -> str: |
|
|
|
|
|
|
|
|
heading.replace_with(new_heading) |
|
|
heading.replace_with(new_heading) |
|
|
|
|
|
|
|
|
|
|
|
if soup.body is None: |
|
|
|
|
|
return "" |
|
|
|
|
|
|
|
|
# html5lib adds <html> and <body> tags around the fragment, strip them back out |
|
|
# html5lib adds <html> and <body> tags around the fragment, strip them back out |
|
|
return "".join([str(tag) for tag in soup.body.children]) |
|
|
return "".join([str(tag) for tag in soup.body.children]) |