Skip to content

Commit

Permalink
Refactor rewriting URL's for non-markdown files
Browse files Browse the repository at this point in the history
  • Loading branch information
d0ugal committed Dec 19, 2014
1 parent 5cb1ee1 commit 0b914fe
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 5 deletions.
2 changes: 1 addition & 1 deletion mkdocs/relative_path_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def path_to_url(url, nav):
return url

if nav and not utils.is_markdown_file(path):
path = utils.create_media_urls(nav, [path])[0]
path = utils.create_relative_media_url(nav, path)
elif nav:
# If the site navigation has been provided, then validate
# the internal hyperlink, making sure the target actually exists.
Expand Down
34 changes: 30 additions & 4 deletions mkdocs/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,7 @@ def test_convert_internal_link_with_anchor(self):
self.assertEqual(html.strip(), expected.strip())

def test_convert_internal_media(self):
"""Test relative image URL's are the same for different base_urls"""
pages = [
('index.md',),
('internal.md',),
Expand All @@ -527,15 +528,40 @@ def test_convert_internal_media(self):
site_navigation = nav.SiteNavigation(pages)

expected_results = (
'<p><img alt="The initial MkDocs layout" src="./img/initial-layout.png" /></p>\n',
'<p><img alt="The initial MkDocs layout" src="../img/initial-layout.png" /></p>\n',
'<p><img alt="The initial MkDocs layout" src="../../img/initial-layout.png" /></p>\n',
'./img/initial-layout.png',
'../img/initial-layout.png',
'../img/initial-layout.png',
)

template = '<p><img alt="The initial MkDocs layout" src="%s" /></p>\n'

for (page, expected) in zip(site_navigation.walk_pages(), expected_results):
md_text = '![The initial MkDocs layout](img/initial-layout.png)'
html, _, _ = build.convert_markdown(md_text, site_navigation=site_navigation)
self.assertEqual(html, expected)
self.assertEqual(html, template % expected)

def test_convert_internal_asbolute_media(self):
"""Test absolute image URL's are correct for different base_urls"""
pages = [
('index.md',),
('internal.md',),
('sub/internal.md')
]

site_navigation = nav.SiteNavigation(pages)

expected_results = (
'./img/initial-layout.png',
'../img/initial-layout.png',
'../../img/initial-layout.png',
)

template = '<p><img alt="The initial MkDocs layout" src="%s" /></p>\n'

for (page, expected) in zip(site_navigation.walk_pages(), expected_results):
md_text = '![The initial MkDocs layout](/img/initial-layout.png)'
html, _, _ = build.convert_markdown(md_text, site_navigation=site_navigation)
self.assertEqual(html, template % expected)

def test_dont_convert_code_block_urls(self):
pages = [
Expand Down
38 changes: 38 additions & 0 deletions mkdocs/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,41 @@ def create_media_urls(nav, url_list):
relative_url = '%s/%s' % (nav.url_context.make_relative('/'), url)
final_urls.append(relative_url)
return final_urls


def create_relative_media_url(nav, url):
"""
For a current page, create a relative url based on the given URL.
On index.md (which becomes /index.html):
image.png -> ./image.png
/image.png -> ./image.png
on sub/page.md (which becomes /sub/page/index.html):
image.png -> ../image.png
/image.png -> ../../image.png
"""

# Allow links to fully qualified URL's
parsed = urlparse(url)
if parsed.netloc:
return url

# If the URL we are looking at starts with a /, then it should be
# considered as absolute and will be 'relative' to the root.
if url.startswith('/'):
base = '/'
url = url[1:]
else:
base = nav.url_context.base_path

relative_url = '%s/%s' % (nav.url_context.make_relative(base), url)

# TODO: Fix this, this is a hack. Relative urls are not being calculated
# correctly for images in the same directory as the markdown. I think this
# is due to us moving it into a directory with index.html, but I'm not sure
if nav.url_context.base_path is not '/' and relative_url.startswith("./"):
relative_url = ".%s" % relative_url

return relative_url

0 comments on commit 0b914fe

Please sign in to comment.