Skip to content

Commit

Permalink
Fix update_anchor_ids
Browse files Browse the repository at this point in the history
  • Loading branch information
timvink committed May 10, 2021
1 parent 0c9be93 commit 9a4bc78
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 11 deletions.
15 changes: 6 additions & 9 deletions mkdocs_print_site_plugin/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@ def update_anchor_ids(page_html, page_key):
It does this only for the h1-h6 tags.
"""
# Regex demo / tests: https://regex101.com/r/mlAPNH/1
# Regex demo / tests: https://regex101.com/r/mlAPNH/3
href_regex = re.compile(
r"<([^\s]+).*?id=\"([^\"]*?)\".*?>(.*?)<\/\1>",
r"<([^\s]+).*?id=\"([^\"]*?)\".*?>",
flags=re.IGNORECASE,
)
matches = re.finditer(href_regex, page_html)
Expand Down Expand Up @@ -169,7 +169,7 @@ def fix_tabbed_content(page_html, page_key):
we should change <input> id, and <label> for attribute, to contain the pagekey.
"""
# Replace <input>
# Replace <input> name and id
href_regex = re.compile(
r"<input.*?id=\"([^\"]*?)\".*?name=\"([^\"]*?)\".*?>",
flags=re.IGNORECASE,
Expand All @@ -186,17 +186,14 @@ def fix_tabbed_content(page_html, page_key):
new_text = match_text.replace(name_text, page_key + "_" + name_text)
page_html = page_html.replace(match_text, new_text)

# Replace <label>
# Replace <label> for
href_regex = re.compile(
r"<([^\s]+).*?for=\"([^\"]*?)\".*?>(.*?)<\/\1>",
r"<label.*?for=\"([^\"]*?)\".*?>",
flags=re.IGNORECASE,
)
matches = re.finditer(href_regex, page_html)
for m in matches:
tag_text = m.group(1)
if tag_text != "label":
continue
id_text = m.group(2)
id_text = m.group(1)
match_text = m.group()
new_text = match_text.replace(id_text, page_key + "_" + id_text)
page_html = page_html.replace(match_text, new_text)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

setup(
name="mkdocs-print-site-plugin",
version="1.2.0",
version="1.2.1",
description="MkDocs plugin that adds a page with all site pages, enabling printing to PDF for users.",
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down
2 changes: 1 addition & 1 deletion tests/test_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def test_update_anchor_ids(html_element):
result = '<%s id="this_page-a-section-on-something">A Section on something</%s>' % (html_element, html_element)
assert update_anchor_ids(html, "this_page") == result

# Make sure changes are made, h6 with a class in front
# Make sure changes are made
html = '<%s class="something" id="a-section-on-something">A Section on something</%s>' % (
html_element,
html_element,
Expand Down

0 comments on commit 9a4bc78

Please sign in to comment.