diff --git a/IPython/nbconvert/filters/latex.py b/IPython/nbconvert/filters/latex.py index f366702..e03a945 100755 --- a/IPython/nbconvert/filters/latex.py +++ b/IPython/nbconvert/filters/latex.py @@ -60,4 +60,4 @@ def escape_latex(text): text = pattern.sub(replacement, text) return text - + diff --git a/IPython/nbconvert/filters/strings.py b/IPython/nbconvert/filters/strings.py index 97ef3a3..77253ca 100755 --- a/IPython/nbconvert/filters/strings.py +++ b/IPython/nbconvert/filters/strings.py @@ -122,19 +122,22 @@ def strip_dollars(text): return text.strip('$') -files_url_pattern = re.compile(r'(src|href)\=([\'"]?)files/') +files_url_pattern = re.compile(r'(src|href)\=([\'"]?)/?files/') +markdown_url_pattern = re.compile(r'(!?)\[(?P.*?)\]\(/?files/(?P.*?)\)') def strip_files_prefix(text): """ - Fix all fake URLs that start with `files/`, - stripping out the `files/` prefix. + Fix all fake URLs that start with `files/`, stripping out the `files/` prefix. + Applies to both urls (for html) and relative paths (for markdown paths). Parameters ---------- text : str Text in which to replace 'src="files/real...' with 'src="real...' """ - return files_url_pattern.sub(r"\1=\2", text) + cleaned_text = files_url_pattern.sub(r"\1=\2", text) + cleaned_text = markdown_url_pattern.sub(r'\1[\2](\3)', cleaned_text) + return cleaned_text def comment_lines(text, prefix='# '): diff --git a/IPython/nbconvert/filters/tests/test_latex.py b/IPython/nbconvert/filters/tests/test_latex.py index a66d0f5..cc324b1 100644 --- a/IPython/nbconvert/filters/tests/test_latex.py +++ b/IPython/nbconvert/filters/tests/test_latex.py @@ -41,5 +41,3 @@ class TestLatex(TestsBase): def _try_escape_latex(self, test, result): """Try to remove latex from string""" self.assertEqual(escape_latex(test), result) - - diff --git a/IPython/nbconvert/filters/tests/test_strings.py b/IPython/nbconvert/filters/tests/test_strings.py index 9fc6d63..f93749c 100644 --- a/IPython/nbconvert/filters/tests/test_strings.py +++ b/IPython/nbconvert/filters/tests/test_strings.py @@ -87,7 +87,20 @@ class TestStrings(TestsBase): ('/files', '/files'), ('test="/files"', 'test="/files"'), ('My files are in `files/`', 'My files are in `files/`'), - ('files/test.html', 'files/test.html')] + ('files/test.html', 'files/test.html'), + ('files/test.html', 'files/test.html'), + ("files/test.html", "files/test.html"), + ('', ''), + ('', ''), + ('hello![caption]', 'hello![caption]'), + ('hello![caption](/url/location.gif)', 'hello![caption](/url/location.gif)'), + ('hello![caption](url/location.gif)', 'hello![caption](url/location.gif)'), + ('hello![caption](url/location.gif)', 'hello![caption](url/location.gif)'), + ('hello![caption](files/url/location.gif)', 'hello![caption](url/location.gif)'), + ('hello![caption](/files/url/location.gif)', 'hello![caption](url/location.gif)'), + ('hello [text](/files/url/location.gif)', 'hello [text](url/location.gif)'), + ('hello [text space](files/url/location.gif)', 'hello [text space](url/location.gif)'), + ] for test in tests: self._try_files_prefix(test[0], test[1]) diff --git a/IPython/nbconvert/templates/latex/latex_base.tplx b/IPython/nbconvert/templates/latex/latex_base.tplx index 97a67a5..21bb5e7 100644 --- a/IPython/nbconvert/templates/latex/latex_base.tplx +++ b/IPython/nbconvert/templates/latex/latex_base.tplx @@ -202,7 +202,7 @@ This template does not define a docclass, the inheriting class must define this. ((* elif cell.level == 6 -*)) ((* block h6 -*))\\*\textit((* endblock h6 -*)) ((*- endif -*)) - {((( cell.source | replace('\n', ' ') | citation2latex | markdown2latex )))} + {((( cell.source | replace('\n', ' ') | citation2latex | strip_files_prefix | markdown2latex )))} ((* endblock headingcell *)) @@ -215,7 +215,7 @@ This template does not define a docclass, the inheriting class must define this. % Render markdown ((* block markdowncell scoped *)) - ((( cell.source | citation2latex | markdown2latex ))) + ((( cell.source | citation2latex | strip_files_prefix | markdown2latex ))) ((* endblock markdowncell *)) % Don't display unknown types