Skip to content

Commit

Permalink
Fix switching content tabs, closes #45
Browse files Browse the repository at this point in the history
  • Loading branch information
timvink committed May 10, 2021
1 parent ff0d41d commit 40491f6
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 97 deletions.
55 changes: 54 additions & 1 deletion mkdocs_print_site_plugin/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,16 @@ def update_anchor_ids(page_html, page_key):
"""
# Regex demo / tests: https://regex101.com/r/mlAPNH/1
href_regex = re.compile(
r"\<([h1|h2|h3|h4|h5|h6|sup|li]+).+id=\"([aA-zZ|0-9|\-|\_|\.|\:]+)\"",
r"<([^\s]+).*?id=\"([^\"]*?)\".*?>(.*?)<\/\1>",
flags=re.IGNORECASE,
)
matches = re.finditer(href_regex, page_html)

change_tags = ["h1", "h2", "h3", "h4", "h5", "h6", "sup", "li"]
for m in matches:
tag_text = m.group(1)
if tag_text not in change_tags:
continue
id_text = m.group(2)
match_text = m.group()
new_text = match_text.replace(id_text, page_key + "-" + id_text)
Expand All @@ -151,6 +155,54 @@ def update_anchor_ids(page_html, page_key):
return page_html


def fix_tabbed_content(page_html, page_key):
"""
Tabbed content have <input> and <label> that are linked.
When combining multiple pages into one, name duplicates occur.
So in the example:
<input checked="checked" id="__tabbed_1_1" name="__tabbed_1" type="radio">
<label for="__tabbed_1_1">C</label>
we should change <input> id, and <label> for attribute, to contain the pagekey.
"""
# Replace <input>
href_regex = re.compile(
r"<input.*?id=\"([^\"]*?)\".*?name=\"([^\"]*?)\".*?>",
flags=re.IGNORECASE,
)
matches = re.finditer(href_regex, page_html)
for m in matches:
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)

name_text = m.group(2)
match_text = m.group()
new_text = match_text.replace(name_text, page_key + "_" + name_text)
page_html = page_html.replace(match_text, new_text)

# Replace <label>
href_regex = re.compile(
r"<([^\s]+).*?for=\"([^\"]*?)\".*?>(.*?)<\/\1>",
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)
match_text = m.group()
new_text = match_text.replace(id_text, page_key + "_" + id_text)
page_html = page_html.replace(match_text, new_text)

return page_html


def fix_image_src(page_html, page_url, directory_urls):
"""
Update img src path for images displayed in print page.
Expand Down Expand Up @@ -219,6 +271,7 @@ def fix_internal_links(page_html, page_url, directory_urls):

page_html = fix_href_links(page_html, page_key, page_url, directory_urls)
page_html = update_anchor_ids(page_html, page_key)
page_html = fix_tabbed_content(page_html, page_key)
page_html = fix_image_src(page_html, page_url, directory_urls)

# Finally, wrap the entire page in a section with an anchor ID
Expand Down
17 changes: 17 additions & 0 deletions tests/fixtures/projects/with_markdown_ext/docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,25 @@ Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent rutrum erat qu

Maecenas semper urna ac orci lacinia vestibulum. Donec sed tincidunt quam, in pulvinar velit. Suspendisse tristique lorem augue, non congue eros porta faucibus. Phasellus orci sapien, facilisis vel bibendum ut, ultrices ut erat. Nam in nibh a neque elementum condimentum et non nibh. Phasellus ex dui, pretium vel posuere a, viverra ac diam. Vivamus ipsum felis, placerat id lobortis gravida, mattis ac libero. Integer luctus enim id nibh sollicitudin, quis mattis purus convallis. Ut aliquet sollicitudin mollis. Phasellus quam turpis, sodales sed pulvinar sed, accumsan at felis. Fusce pharetra felis sed condimentum dictum. Quisque sit amet augue vitae felis elementum auctor sit amet eget turpis. Duis dictum, sapien nec semper luctus, orci libero convallis lacus, id ultricies ipsum est sit amet elit. Integer rhoncus erat at ultrices sagittis.

??? optional-class "Summary"
Here's some content.

??? multiple optional-class "Summary"
Here's some content.

??? success
Content.

??? warning classes
Content.

Phasellus commodo volutpat varius. Nulla volutpat id nisi non vulputate. Ut vitae dapibus nulla, nec maximus felis. Vivamus sem leo, mattis vel consequat eget, mollis sit amet justo. Aenean eget laoreet sem, quis vulputate risus. Nunc ac commodo odio, ullamcorper tristique quam. Quisque eros ante, rutrum quis dui vitae, pretium consectetur est. Quisque in urna gravida, molestie quam eget, elementum metus. In leo sapien, posuere eget ante id, bibendum laoreet tellus. Cras tellus eros, congue rhoncus porttitor id, tincidunt id nibh. Nullam eget porta tellus, et gravida orci. In quis aliquet sapien. Aenean rhoncus nisi non magna volutpat egestas. Integer quis ipsum ultrices, feugiat nulla sed, vulputate magna. In sit amet hendrerit metus, et laoreet nulla.

Ut neque erat, finibus vitae metus ut, facilisis accumsan risus. Vivamus et rutrum turpis. Quisque ac molestie erat, ut fringilla tortor. Fusce congue gravida sapien, venenatis vulputate purus dictum id. Suspendisse odio lorem, rhoncus id diam eu, euismod fermentum nisi. Mauris eget pretium nunc. Donec at mauris leo. Mauris porta sed purus nec interdum. Donec pretium sit amet turpis eget dignissim. Quisque malesuada orci a purus consequat, vel consectetur massa placerat.

???+ note "Open styled details"

??? danger "Nested details!"
And more content again.

Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Phasellus eget justo ut neque iaculis interdum. Donec varius, lectus eu pharetra vulputate, diam sem luctus dui, quis tristique diam neque ac turpis. Fusce porttitor euismod massa, quis accumsan odio tincidunt ac. Sed dictum lorem at magna dignissim congue. Donec ultricies sagittis neque, a blandit quam rutrum eu. In a ligula fermentum, tincidunt nibh vehicula, rutrum erat. Suspendisse blandit malesuada tortor facilisis convallis. Sed placerat rhoncus imperdiet. Cras porta purus vel nulla fermentum, in tristique arcu facilisis. Morbi tristique vitae eros vel tempor. Proin in enim semper risus posuere posuere. Phasellus imperdiet commodo eros sit amet luctus. Aliquam lectus eros, malesuada id vestibulum eget, condimentum eget turpis. Maecenas sollicitudin velit eget elit eleifend mollis.
166 changes: 70 additions & 96 deletions tests/test_urls.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,39 @@
from mkdocs_print_site_plugin.urls import fix_href_links, update_anchor_ids, fix_image_src, get_page_key, is_external, is_attachment
import pytest

from mkdocs_print_site_plugin.urls import (
fix_href_links,
update_anchor_ids,
fix_image_src,
get_page_key,
is_external,
is_attachment,
)

def test_get_page_key():

assert get_page_key('index.html') == 'index'
assert get_page_key('/') =='index'
assert get_page_key('abc/') == 'abc'
assert get_page_key('abc.html') == 'abc'
assert get_page_key('/folder/subfolder/index.html') == "folder-subfolder-index"
def test_get_page_key():
"""
Test page key.
"""
assert get_page_key("index.html") == "index"
assert get_page_key("/") == "index"
assert get_page_key("abc/") == "abc"
assert get_page_key("abc.html") == "abc"
assert get_page_key("/folder/subfolder/index.html") == "folder-subfolder-index"


def test_is_external():
"""
Test.
"""
assert is_external("https://www.google.com")
assert not is_external("/index.html")
assert not is_external("index.html")


def test_is_attachment():
"""
Test.
"""
assert is_attachment("/file.py")
assert is_attachment("../files/file.xlsx")
assert not is_attachment("https://www.google.com")
Expand All @@ -24,7 +42,9 @@ def test_is_attachment():


def test_fix_href_links():

"""
Test.
"""
html = '<h1><a href="page.html#anchor-link">the link</a></h1>'
result = '<h1><a href="#page-anchor-link">the link</a></h1>'
assert fix_href_links(html, "this_page", "/") == result
Expand Down Expand Up @@ -61,39 +81,56 @@ def test_fix_href_links():
result = '<li><a class = "bla" href="#chapter1-section1-reference">page z</a></li>'
assert fix_href_links(html, "this_page", "/Chapter1/Section2/") == result

html = "<td>Wraps the hero teaser (if available)</td>\n</tr>\n<tr>\n<td><code>htmltitle</code></td>\n<td>Wraps the <code><title></code> tag</td>\n</tr>\n<tr>\n<td><code>libs</code></td>\n<td>Wraps"
html = "<td>Wraps the hero teaser (if available)</td>\n</tr>\n<tr>\n<td><code>htmltitle</code></td>\n<td>Wraps the <code><title></code> tag</td>\n</tr>\n<tr>\n<td><code>libs</code></td>\n<td>Wraps" # noqa
result = fix_href_links(html, "this_page", "/")
assert result == html


def test_update_anchor_ids():

def test_update_anchor_ids_noupdate():
"""
Test.
"""
# Make sure no changes are made
htmls = [
'<h1><a href="page.html#anchor-link">the link</a></h1>',
'<a href="test"',
'<li><a href="a/">page a</a></li><li><a href="z/">page z</a></li>',
'<li><a class = "bla" href="z/">page z</a></li>',
"<td>Wraps the hero teaser (if available)</td>\n</tr>\n<tr>\n<td><code>htmltitle</code></td>\n<td>Wraps the <code><title></code> tag</td>\n</tr>\n<tr>\n<td><code>libs</code></td>\n<td>Wraps", # noqa
'<h1>no "id" here</h1>',
'<input id="hello">blabla</input>',
'<input class="something" id="hello">blabla</input>',
]

for html in htmls:
assert update_anchor_ids(html, "this_page") == html


@pytest.mark.parametrize("html_element", ["h1", "h2", "h3", "h4", "h5", "h6", "li", "sup"])
def test_update_anchor_ids(html_element):
"""
Test changing ids.
"""
html = '<%s id="a-section-on-something">A Section on something</%s>' % (html_element, 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

html = '<h1><a href="page.html#anchor-link">the link</a></h1>'
assert update_anchor_ids(html, "this_page") == html

html = '<a href="test"'
assert update_anchor_ids(html, "this_page") == html

html = '<li><a href="a/">page a</a></li><li><a href="z/">page z</a></li>'
assert update_anchor_ids(html, "this_page") == html

html = '<li><a class = "bla" href="z/">page z</a></li>'
assert update_anchor_ids(html, "this_page") == html

html = "<td>Wraps the hero teaser (if available)</td>\n</tr>\n<tr>\n<td><code>htmltitle</code></td>\n<td>Wraps the <code><title></code> tag</td>\n</tr>\n<tr>\n<td><code>libs</code></td>\n<td>Wraps"
assert update_anchor_ids(html, "this_page") == html

# Make sure changes are made

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


def test_fix_image_src():

"""
Test fixing image source.
"""
# Make sure no changes are made

html = '<h1><a href="page.html#anchor-link">the link</a></h1>'
Expand All @@ -108,80 +145,17 @@ def test_fix_image_src():
html = '<li><a class = "bla" href="z/">page z</a></li>'
assert fix_image_src(html, "this_page", True) == html

html = "<td>Wraps the hero teaser (if available)</td>\n</tr>\n<tr>\n<td><code>htmltitle</code></td>\n<td>Wraps the <code><title></code> tag</td>\n</tr>\n<tr>\n<td><code>libs</code></td>\n<td>Wraps"
html = "<td>Wraps the hero teaser (if available)</td>\n</tr>\n<tr>\n<td><code>htmltitle</code></td>\n<td>Wraps the <code><title></code> tag</td>\n</tr>\n<tr>\n<td><code>libs</code></td>\n<td>Wraps" # noqa
assert fix_image_src(html, "this_page", True) == html

# Make sure absolute urls stay intct
html = '<img src="/img.png">'
assert fix_image_src(html, "this_page", False) == html

# Make sure changes are made
html = '<img src="../appendix/table.png">'
result = '<img src="../appendix/table.png">'
assert fix_image_src(html, "this_page", False) == result

result = '<img src="../../appendix/table.png">'
assert fix_image_src(html, "this_page", True) == result

# def test_url_to_anchor():
# assert url_to_anchor("") == "#"
# assert url_to_anchor("/") == "#"
# assert url_to_anchor("a.html") == "#a"
# assert url_to_anchor("a/") == "#a"
# assert url_to_anchor("section/a/") == "#section-a"
# assert url_to_anchor("section/a.html") == "#section-a"
# assert url_to_anchor("section/a.html#anchor") == "#section-a-anchor"


# def test_fix_internal_links():

# page_url = "customization/"
# directory_urls = True

# html = "<td>Wraps the hero teaser (if available)</td>\n</tr>\n<tr>\n<td><code>htmltitle</code></td>\n<td>Wraps the <code><title></code> tag</td>\n</tr>\n<tr>\n<td><code>libs</code></td>\n<td>Wraps"
# result = fix_internal_links(html, page_url, directory_urls)
# assert result == html

# def test_fix_internal_links():
# html = """
# <div style="border:2px; border-style:solid; border-color:#000000; padding: 1em; margin-bottom: 1em">
# <h3>Print Site Page</h3>
# <p>First example with text surrounded by a red border. This example also has multiple lines.</p>
# </div>
# <ul>
# <li><a href="a/">page a</a></li>
# <li><a href="z/">page z</a></li>
# <li><a class="bla" style="color: #132" href="page.html#anchor">anchor on page</a></li>
# </ul>
# <p>text</p>
# <h1 id="z">Z</h1>
# <p>text</p>
# <h1 id="a">A</h1>
# <p>text</p>
# <h2 id="sub-one">sub one</h2>
# <p>text</p>
# <h2 id="sub-two">sub two</h2>
# """

# assert '<a href="#a">page a</a>' in fix_internal_links(html, page_url="a/")

# def sample_html():
# return """
# <div style="border:2px; border-style:solid; border-color:#000000; padding: 1em; margin-bottom: 1em">
# <h3>Print Site Page</h3>
# <p>First example with text surrounded by a red border. This example also has multiple lines.</p>
# </div>
# <ul>
# <li><a href="a/">page a</a></li>
# <li><a href="z/">page z</a></li>
# <li><a class="bla" style="color: #132" href="page.html#anchor">anchor on page</a></li>
# </ul>
# <p>text</p>
# <h1 id="z">Z</h1>
# <p>text</p>
# <h1 id="a">A</h1>
# <p>text</p>
# <h2 id="sub-one">sub one</h2>
# <p>text</p>
# <h2 id="sub-two">sub two</h2>
# """

0 comments on commit 40491f6

Please sign in to comment.