Skip to content

Commit

Permalink
Special case media being in the same directory as the markdown files
Browse files Browse the repository at this point in the history
Closes mkdocs#495
Closes mkdocs#494
  • Loading branch information
d0ugal committed May 18, 2015
1 parent 7180e07 commit 8818012
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
31 changes: 30 additions & 1 deletion mkdocs/tests/utils_tests.py
Expand Up @@ -55,20 +55,49 @@ def test_is_html_file(self):
def test_create_media_urls(self):
pages = [
{'Home': 'index.md'},
{'About': 'about.md'}
{'About': 'about.md'},
{'Sub': [
{'Sub Home': 'index.md'},
{'Sub About': 'about.md'},

]}
]
expected_results = {
'https://media.cdn.org/jq.js': 'https://media.cdn.org/jq.js',
'http://media.cdn.org/jquery.js': 'http://media.cdn.org/jquery.js',
'//media.cdn.org/jquery.js': '//media.cdn.org/jquery.js',
'media.cdn.org/jquery.js': './media.cdn.org/jquery.js',
'local/file/jquery.js': './local/file/jquery.js',
'image.png': './image.png',
}
site_navigation = nav.SiteNavigation(pages)
for path, expected_result in expected_results.items():
urls = utils.create_media_urls(site_navigation, [path])
self.assertEqual(urls[0], expected_result)

def test_create_relative_media_url_sub_index(self):
'''
test special case where there's a sub/index.md page
'''

site_navigation = nav.SiteNavigation([
{'Home': 'index.md'},
{'Sub': [
{'Sub Home': '/subpage/index.md'},

]}
])
site_navigation.url_context.set_current_url('/subpage/')
site_navigation.file_context.current_file = "subpage/index.md"

def assertPathGenerated(declared, expected):
url = utils.create_relative_media_url(site_navigation, declared)
self.assertEqual(url, expected)

assertPathGenerated("img.png", "./img.png")
assertPathGenerated("./img.png", "./img.png")
assertPathGenerated("/img.png", "../img.png")

def test_reduce_list(self):
self.assertEqual(
utils.reduce_list([1, 2, 3, 4, 5, 5, 2, 4, 6, 7, 8]),
Expand Down
16 changes: 13 additions & 3 deletions mkdocs/utils.py
Expand Up @@ -210,10 +210,14 @@ def create_relative_media_url(nav, url):
image.png -> ./image.png
/image.png -> ./image.png
on sub/page.md (which becomes /sub/page/index.html):
On sub/page.md (which becomes /sub/page/index.html):
image.png -> ../image.png
/image.png -> ../../image.png
On sub/index.md (which becomes /sub/index.html):
image.png -> ./image.png
/image.png -> ./image.png
"""

# Allow links to fully qualified URL's
Expand All @@ -229,12 +233,18 @@ def create_relative_media_url(nav, url):
else:
base = nav.url_context.base_path

relative_url = '%s/%s' % (nav.url_context.make_relative(base), url)
relative_base = nav.url_context.make_relative(base)
if relative_base == "." and url.startswith("./"):
relative_url = url
else:
relative_url = '%s/%s' % (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("./"):
if (not nav.file_context.current_file.endswith("/index.md")
and nav.url_context.base_path is not '/'
and relative_url.startswith("./")):
relative_url = ".%s" % relative_url

return relative_url
Expand Down

0 comments on commit 8818012

Please sign in to comment.