From d9e50170a417601bfd4f804bf6b142c696aeaa9f Mon Sep 17 00:00:00 2001 From: Dougal Matthews Date: Wed, 8 Oct 2014 12:18:08 +0100 Subject: [PATCH] Fix image paths in non-index markdown files Fixes #138 --- mkdocs/build.py | 24 +++++++++++++++++------- mkdocs/compat.py | 5 +++++ mkdocs/test.py | 22 +++++++++++++++++++++- 3 files changed, 43 insertions(+), 8 deletions(-) diff --git a/mkdocs/build.py b/mkdocs/build.py index 7c0f259a..d02dd182 100644 --- a/mkdocs/build.py +++ b/mkdocs/build.py @@ -10,18 +10,21 @@ import re class PathToURL(object): - def __init__(self, nav=None): + def __init__(self, template, nav=None): + self.template = template self.nav = nav def __call__(self, match): url = match.groups()[0] scheme, netloc, path, query, query, fragment = urlparse(url) - if (scheme or netloc or not utils.is_markdown_file(path)): + if scheme or netloc: # Ignore URLs unless they are a relative link to a markdown file. - return 'a href="%s"' % url + return self.template % url - if self.nav: + if self.nav and not utils.is_markdown_file(path): + path = utils.create_media_urls(self.nav, [path])[0] + elif self.nav: # If the site navigation has been provided, then validate # the internal hyperlink, making sure the target actually exists. target_file = self.nav.file_context.make_absolute(path) @@ -39,7 +42,7 @@ class PathToURL(object): # Convert the .md hyperlink to a relative hyperlink to the HTML page. url = urlunparse((scheme, netloc, path, query, query, fragment)) - return 'a href="%s"' % url + return self.template % url def convert_markdown(markdown_source, extensions=()): @@ -72,8 +75,15 @@ def convert_markdown(markdown_source, extensions=()): def post_process_html(html_content, nav=None): - html_content = re.sub(r'a href="([^"]*)"', PathToURL(nav), html_content) - html_content = re.sub('
', '
', html_content)
+
+    anchor_sub = PathToURL('a href="%s"', nav)
+    html_content = re.sub(r'a href="([^"]*)"', anchor_sub, html_content)
+
+    img_sub = PathToURL('src="%s"', nav)
+    html_content = re.sub(r'src="([^"]*)"', img_sub, html_content)
+
+    html_content = html_content.replace('
', '
')
+
     return html_content
 
 
diff --git a/mkdocs/compat.py b/mkdocs/compat.py
index bb04bc1e..49bd396a 100644
--- a/mkdocs/compat.py
+++ b/mkdocs/compat.py
@@ -14,6 +14,9 @@ if PY2:
     import SocketServer
     socketserver = SocketServer
 
+    import itertools
+    zip = itertools.izip
+
     text_type = unicode
     binary_type = str
     string_types = (str, unicode)
@@ -28,6 +31,8 @@ else:  # PY3
     import socketserver
     socketserver = socketserver
 
+    zip = zip
+
     text_type = str
     binary_type = bytes
     string_types = (str,)
diff --git a/mkdocs/test.py b/mkdocs/test.py
index f05891a8..5d1f6c63 100755
--- a/mkdocs/test.py
+++ b/mkdocs/test.py
@@ -3,7 +3,7 @@
 
 
 from mkdocs import build, nav, toc, utils, config
-from mkdocs.compat import PY2
+from mkdocs.compat import PY2, zip
 import markdown
 import os
 import shutil
@@ -431,6 +431,26 @@ class BuildTests(unittest.TestCase):
         html = build.post_process_html(html)
         self.assertEqual(html.strip(), expected.strip())
 
+    def test_convert_internal_media(self):
+        pages = [
+            ('index.md',),
+            ('internal.md',),
+            ('sub/internal.md')
+        ]
+
+        site_navigation = nav.SiteNavigation(pages)
+
+        expected_results = (
+            '

The initial MkDocs layout

', + '

The initial MkDocs layout

', + '

The initial MkDocs layout

', + ) + + for (page, expected) in zip(site_navigation.walk_pages(), expected_results): + html = '

The initial MkDocs layout

' + html = build.post_process_html(html, site_navigation) + self.assertEqual(html, expected) + def test_ignore_external_link(self): md_text = 'An [external link](http://example.com/external.md).' expected = '

An external link.

'