diff --git a/IPython/lib/display.py b/IPython/lib/display.py index c315c50..98102ef 100644 --- a/IPython/lib/display.py +++ b/IPython/lib/display.py @@ -5,7 +5,7 @@ Authors : MinRK, gregcaporaso, dannystaple import urllib from os.path import exists, isfile, splitext, abspath, join, isdir -from os import walk +from os import walk, sep class YouTubeVideo(object): @@ -192,7 +192,8 @@ class FileLinks(FileLink): def _get_display_formatter(self, dirname_output_format, fname_output_format, - fp_format): + fp_format, + fp_cleaner=None): """ generate built-in formatter function this is used to define both the notebook and terminal built-in @@ -232,6 +233,8 @@ class FileLinks(FileLink): result.append(dirname_output_line) for fname in display_fnames: fp = fp_format % (dirname,fname) + if fp_cleaner != None: + fp = fp_cleaner(fp) try: # output can include both a filepath and a filename... fname_output_line = fname_output_format % (fp, fname) @@ -251,10 +254,21 @@ class FileLinks(FileLink): fname_output_format = \ self.result_html_prefix + spacer + self.html_link_str + self.result_html_suffix fp_format = self.url_prefix + '%s/%s' + if sep == "\\": + # Working on a platform where the path separator is "\", so + # must convert these to "/" for generating a URI + def fp_cleaner(fp): + # Replace all occurences of backslash ("\") with a forward + # slash ("/") - this is necessary on windows when a path is + # provided as input, but we must link to a URI + return fp.replace('\\','/') + else: + fp_cleaner = None return self._get_display_formatter(dirname_output_format, fname_output_format, - fp_format) + fp_format, + fp_cleaner) def _get_terminal_display_formatter(self, spacer=" "): diff --git a/IPython/lib/tests/test_display.py b/IPython/lib/tests/test_display.py index 0e89afb..dee073b 100644 --- a/IPython/lib/tests/test_display.py +++ b/IPython/lib/tests/test_display.py @@ -15,6 +15,7 @@ from __future__ import print_function from tempfile import NamedTemporaryFile, mkdtemp from os.path import split +from os import sep # Third-party imports import nose.tools as nt @@ -89,9 +90,13 @@ def test_existing_path_FileLinks(): actual = fl._repr_html_() actual = actual.split('\n') actual.sort() + # the links should always have forward slashes, even on windows, so replace + # backslashes with forward slashes here expected = ["%s/
" % td, - "  %s
" % (tf2.name,split(tf2.name)[1]), - "  %s
" % (tf1.name,split(tf1.name)[1])] + "  %s
" %\ + (tf2.name.replace("\\","/"),split(tf2.name)[1]), + "  %s
" %\ + (tf1.name.replace("\\","/"),split(tf1.name)[1])] expected.sort() # We compare the sorted list of links here as that's more reliable nt.assert_equal(actual,expected)