prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Converting notebooks with spaces in their names to RST gives broken images
  I am using `ipython nbconvert --to rst example1.ipynb` to convert my example notebooks into reStructuredText, for incorporation into my package's Sphinx documentation. This works quite well, unless the filename has a space in it. In this case, any image files from my notebooks are lost when I run Sphinx's `make html`. 
  
  My guess is that the problem seems is in the generated rst file, where the `.. image` command may need to be escaped or quoted somehow to work with spaces in the filename.
  
  I note that a similar issue was reported and resolved for latex output in issue #3774, however the solution was specific to latex.
  
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `IPython/nbconvert/filters/markdown.py`
  Content:
  ```
  1 """Markdown filters
  2 This file contains a collection of utility filters for dealing with 
  3 markdown within Jinja templates.
  4 """
  5 #-----------------------------------------------------------------------------
  6 # Copyright (c) 2013, the IPython Development Team.
  7 #
  8 # Distributed under the terms of the Modified BSD License.
  9 #
  10 # The full license is in the file COPYING.txt, distributed with this software.
  11 #-----------------------------------------------------------------------------
  12 
  13 #-----------------------------------------------------------------------------
  14 # Imports
  15 #-----------------------------------------------------------------------------
  16 from __future__ import print_function
  17 
  18 # Stdlib imports
  19 import os
  20 import subprocess
  21 import warnings
  22 from io import TextIOWrapper, BytesIO
  23 
  24 # IPython imports
  25 from IPython.nbconvert.utils.pandoc import pandoc
  26 from IPython.nbconvert.utils.exceptions import ConversionException
  27 from IPython.utils.process import get_output_error_code
  28 from IPython.utils.py3compat import cast_bytes
  29 from IPython.utils.version import check_version
  30 
  31 #-----------------------------------------------------------------------------
  32 # Functions
  33 #-----------------------------------------------------------------------------
  34 marked = os.path.join(os.path.dirname(__file__), "marked.js")
  35 _node = None
  36 
  37 __all__ = [
  38     'markdown2html',
  39     'markdown2html_pandoc',
  40     'markdown2html_marked',
  41     'markdown2latex',
  42     'markdown2rst',
  43 ]
  44 
  45 class NodeJSMissing(ConversionException):
  46     """Exception raised when node.js is missing."""
  47     pass
  48 
  49 def markdown2latex(source):
  50     """Convert a markdown string to LaTeX via pandoc.
  51 
  52     This function will raise an error if pandoc is not installed.
  53     Any error messages generated by pandoc are printed to stderr.
  54 
  55     Parameters
  56     ----------
  57     source : string
  58       Input string, assumed to be valid markdown.
  59 
  60     Returns
  61     -------
  62     out : string
  63       Output as returned by pandoc.
  64     """
  65     return pandoc(source, 'markdown', 'latex')
  66 
  67 def markdown2html(source):
  68     """Convert a markdown string to HTML"""
  69     global _node
  70     if _node is None:
  71         # prefer md2html via marked if node.js >= 0.9.12 is available
  72         # node is called nodejs on debian, so try that first
  73         _node = 'nodejs'
  74         if not _verify_node(_node):
  75             _node = 'node'
  76             if not _verify_node(_node):
  77                 warnings.warn(  "Node.js 0.9.12 or later wasn't found.\n" +
  78                                 "Nbconvert will try to use Pandoc instead.")
  79                 _node = False
  80     if _node:
  81         return markdown2html_marked(source)
  82     else:
  83         return markdown2html_pandoc(source)
  84 
  85 def markdown2html_pandoc(source):
  86     """Convert a markdown string to HTML via pandoc"""
  87     return pandoc(source, 'markdown', 'html', extra_args=['--mathjax'])
  88 
  89 def markdown2html_marked(source, encoding='utf-8'):
  90     """Convert a markdown string to HTML via marked"""
  91     command = [_node, marked]
  92     try:
  93         p = subprocess.Popen(command,
  94                              stdin=subprocess.PIPE, stdout=subprocess.PIPE
  95         )
  96     except OSError as e:
  97         raise NodeJSMissing(
  98             "The command '%s' returned an error: %s.\n" % (" ".join(command), e) +
  99             "Please check that Node.js is installed."
  100         )
  101     out, _ = p.communicate(cast_bytes(source, encoding))
  102     out = TextIOWrapper(BytesIO(out), encoding, 'replace').read()
  103     return out.rstrip('\n')
  104 
  105 def markdown2rst(source):
  106     """Convert a markdown string to LaTeX via pandoc.
  107 
  108     This function will raise an error if pandoc is not installed.
  109     Any error messages generated by pandoc are printed to stderr.
  110 
  111     Parameters
  112     ----------
  113     source : string
  114       Input string, assumed to be valid markdown.
  115 
  116     Returns
  117     -------
  118     out : string
  119       Output as returned by pandoc.
  120     """
  121     return pandoc(source, 'markdown', 'rst')
  122 
  123 def _verify_node(cmd):
  124     """Verify that the node command exists and is at least the minimum supported
  125     version of node.
  126 
  127     Parameters
  128     ----------
  129     cmd : string
  130         Node command to verify (i.e 'node')."""
  131     try:
  132         out, err, return_code = get_output_error_code([cmd, '--version'])
  133     except OSError:
  134         # Command not found
  135         return False
  136     if return_code:
  137         # Command error
  138         return False
  139     return check_version(out.lstrip('v'), '0.9.12')
  140
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/IPython/nbconvert/filters/markdown.py b/IPython/nbconvert/filters/markdown.py
  --- a/IPython/nbconvert/filters/markdown.py
  +++ b/IPython/nbconvert/filters/markdown.py
  @@ -103,7 +103,7 @@
       return out.rstrip('\n')
   
   def markdown2rst(source):
  -    """Convert a markdown string to LaTeX via pandoc.
  +    """Convert a markdown string to ReST via pandoc.
   
       This function will raise an error if pandoc is not installed.
       Any error messages generated by pandoc are printed to stderr.
  
verification_info: {"golden_diff": "diff --git a/IPython/nbconvert/filters/markdown.py b/IPython/nbconvert/filters/markdown.py\n--- a/IPython/nbconvert/filters/markdown.py\n+++ b/IPython/nbconvert/filters/markdown.py\n@@ -103,7 +103,7 @@\n     return out.rstrip('\\n')\n \n def markdown2rst(source):\n-    \"\"\"Convert a markdown string to LaTeX via pandoc.\n+    \"\"\"Convert a markdown string to ReST via pandoc.\n \n     This function will raise an error if pandoc is not installed.\n     Any error messages generated by pandoc are printed to stderr.\n", "issue": "Converting notebooks with spaces in their names to RST gives broken images\nI am using `ipython nbconvert --to rst example1.ipynb` to convert my example notebooks into reStructuredText, for incorporation into my package's Sphinx documentation. This works quite well, unless the filename has a space in it. In this case, any image files from my notebooks are lost when I run Sphinx's `make html`. \n\nMy guess is that the problem seems is in the generated rst file, where the `.. image` command may need to be escaped or quoted somehow to work with spaces in the filename.\n\nI note that a similar issue was reported and resolved for latex output in issue #3774, however the solution was specific to latex.\n\n", "before_files": [{"content": "\"\"\"Markdown filters\nThis file contains a collection of utility filters for dealing with \nmarkdown within Jinja templates.\n\"\"\"\n#-----------------------------------------------------------------------------\n# Copyright (c) 2013, the IPython Development Team.\n#\n# Distributed under the terms of the Modified BSD License.\n#\n# The full license is in the file COPYING.txt, distributed with this software.\n#-----------------------------------------------------------------------------\n\n#-----------------------------------------------------------------------------\n# Imports\n#-----------------------------------------------------------------------------\nfrom __future__ import print_function\n\n# Stdlib imports\nimport os\nimport subprocess\nimport warnings\nfrom io import TextIOWrapper, BytesIO\n\n# IPython imports\nfrom IPython.nbconvert.utils.pandoc import pandoc\nfrom IPython.nbconvert.utils.exceptions import ConversionException\nfrom IPython.utils.process import get_output_error_code\nfrom IPython.utils.py3compat import cast_bytes\nfrom IPython.utils.version import check_version\n\n#-----------------------------------------------------------------------------\n# Functions\n#-----------------------------------------------------------------------------\nmarked = os.path.join(os.path.dirname(__file__), \"marked.js\")\n_node = None\n\n__all__ = [\n    'markdown2html',\n    'markdown2html_pandoc',\n    'markdown2html_marked',\n    'markdown2latex',\n    'markdown2rst',\n]\n\nclass NodeJSMissing(ConversionException):\n    \"\"\"Exception raised when node.js is missing.\"\"\"\n    pass\n\ndef markdown2latex(source):\n    \"\"\"Convert a markdown string to LaTeX via pandoc.\n\n    This function will raise an error if pandoc is not installed.\n    Any error messages generated by pandoc are printed to stderr.\n\n    Parameters\n    ----------\n    source : string\n      Input string, assumed to be valid markdown.\n\n    Returns\n    -------\n    out : string\n      Output as returned by pandoc.\n    \"\"\"\n    return pandoc(source, 'markdown', 'latex')\n\ndef markdown2html(source):\n    \"\"\"Convert a markdown string to HTML\"\"\"\n    global _node\n    if _node is None:\n        # prefer md2html via marked if node.js >= 0.9.12 is available\n        # node is called nodejs on debian, so try that first\n        _node = 'nodejs'\n        if not _verify_node(_node):\n            _node = 'node'\n            if not _verify_node(_node):\n                warnings.warn(  \"Node.js 0.9.12 or later wasn't found.\\n\" +\n                                \"Nbconvert will try to use Pandoc instead.\")\n                _node = False\n    if _node:\n        return markdown2html_marked(source)\n    else:\n        return markdown2html_pandoc(source)\n\ndef markdown2html_pandoc(source):\n    \"\"\"Convert a markdown string to HTML via pandoc\"\"\"\n    return pandoc(source, 'markdown', 'html', extra_args=['--mathjax'])\n\ndef markdown2html_marked(source, encoding='utf-8'):\n    \"\"\"Convert a markdown string to HTML via marked\"\"\"\n    command = [_node, marked]\n    try:\n        p = subprocess.Popen(command,\n                             stdin=subprocess.PIPE, stdout=subprocess.PIPE\n        )\n    except OSError as e:\n        raise NodeJSMissing(\n            \"The command '%s' returned an error: %s.\\n\" % (\" \".join(command), e) +\n            \"Please check that Node.js is installed.\"\n        )\n    out, _ = p.communicate(cast_bytes(source, encoding))\n    out = TextIOWrapper(BytesIO(out), encoding, 'replace').read()\n    return out.rstrip('\\n')\n\ndef markdown2rst(source):\n    \"\"\"Convert a markdown string to LaTeX via pandoc.\n\n    This function will raise an error if pandoc is not installed.\n    Any error messages generated by pandoc are printed to stderr.\n\n    Parameters\n    ----------\n    source : string\n      Input string, assumed to be valid markdown.\n\n    Returns\n    -------\n    out : string\n      Output as returned by pandoc.\n    \"\"\"\n    return pandoc(source, 'markdown', 'rst')\n\ndef _verify_node(cmd):\n    \"\"\"Verify that the node command exists and is at least the minimum supported\n    version of node.\n\n    Parameters\n    ----------\n    cmd : string\n        Node command to verify (i.e 'node').\"\"\"\n    try:\n        out, err, return_code = get_output_error_code([cmd, '--version'])\n    except OSError:\n        # Command not found\n        return False\n    if return_code:\n        # Command error\n        return False\n    return check_version(out.lstrip('v'), '0.9.12')\n", "path": "IPython/nbconvert/filters/markdown.py"}], "after_files": [{"content": "\"\"\"Markdown filters\nThis file contains a collection of utility filters for dealing with \nmarkdown within Jinja templates.\n\"\"\"\n#-----------------------------------------------------------------------------\n# Copyright (c) 2013, the IPython Development Team.\n#\n# Distributed under the terms of the Modified BSD License.\n#\n# The full license is in the file COPYING.txt, distributed with this software.\n#-----------------------------------------------------------------------------\n\n#-----------------------------------------------------------------------------\n# Imports\n#-----------------------------------------------------------------------------\nfrom __future__ import print_function\n\n# Stdlib imports\nimport os\nimport subprocess\nimport warnings\nfrom io import TextIOWrapper, BytesIO\n\n# IPython imports\nfrom IPython.nbconvert.utils.pandoc import pandoc\nfrom IPython.nbconvert.utils.exceptions import ConversionException\nfrom IPython.utils.process import get_output_error_code\nfrom IPython.utils.py3compat import cast_bytes\nfrom IPython.utils.version import check_version\n\n#-----------------------------------------------------------------------------\n# Functions\n#-----------------------------------------------------------------------------\nmarked = os.path.join(os.path.dirname(__file__), \"marked.js\")\n_node = None\n\n__all__ = [\n    'markdown2html',\n    'markdown2html_pandoc',\n    'markdown2html_marked',\n    'markdown2latex',\n    'markdown2rst',\n]\n\nclass NodeJSMissing(ConversionException):\n    \"\"\"Exception raised when node.js is missing.\"\"\"\n    pass\n\ndef markdown2latex(source):\n    \"\"\"Convert a markdown string to LaTeX via pandoc.\n\n    This function will raise an error if pandoc is not installed.\n    Any error messages generated by pandoc are printed to stderr.\n\n    Parameters\n    ----------\n    source : string\n      Input string, assumed to be valid markdown.\n\n    Returns\n    -------\n    out : string\n      Output as returned by pandoc.\n    \"\"\"\n    return pandoc(source, 'markdown', 'latex')\n\ndef markdown2html(source):\n    \"\"\"Convert a markdown string to HTML\"\"\"\n    global _node\n    if _node is None:\n        # prefer md2html via marked if node.js >= 0.9.12 is available\n        # node is called nodejs on debian, so try that first\n        _node = 'nodejs'\n        if not _verify_node(_node):\n            _node = 'node'\n            if not _verify_node(_node):\n                warnings.warn(  \"Node.js 0.9.12 or later wasn't found.\\n\" +\n                                \"Nbconvert will try to use Pandoc instead.\")\n                _node = False\n    if _node:\n        return markdown2html_marked(source)\n    else:\n        return markdown2html_pandoc(source)\n\ndef markdown2html_pandoc(source):\n    \"\"\"Convert a markdown string to HTML via pandoc\"\"\"\n    return pandoc(source, 'markdown', 'html', extra_args=['--mathjax'])\n\ndef markdown2html_marked(source, encoding='utf-8'):\n    \"\"\"Convert a markdown string to HTML via marked\"\"\"\n    command = [_node, marked]\n    try:\n        p = subprocess.Popen(command,\n                             stdin=subprocess.PIPE, stdout=subprocess.PIPE\n        )\n    except OSError as e:\n        raise NodeJSMissing(\n            \"The command '%s' returned an error: %s.\\n\" % (\" \".join(command), e) +\n            \"Please check that Node.js is installed.\"\n        )\n    out, _ = p.communicate(cast_bytes(source, encoding))\n    out = TextIOWrapper(BytesIO(out), encoding, 'replace').read()\n    return out.rstrip('\\n')\n\ndef markdown2rst(source):\n    \"\"\"Convert a markdown string to ReST via pandoc.\n\n    This function will raise an error if pandoc is not installed.\n    Any error messages generated by pandoc are printed to stderr.\n\n    Parameters\n    ----------\n    source : string\n      Input string, assumed to be valid markdown.\n\n    Returns\n    -------\n    out : string\n      Output as returned by pandoc.\n    \"\"\"\n    return pandoc(source, 'markdown', 'rst')\n\ndef _verify_node(cmd):\n    \"\"\"Verify that the node command exists and is at least the minimum supported\n    version of node.\n\n    Parameters\n    ----------\n    cmd : string\n        Node command to verify (i.e 'node').\"\"\"\n    try:\n        out, err, return_code = get_output_error_code([cmd, '--version'])\n    except OSError:\n        # Command not found\n        return False\n    if return_code:\n        # Command error\n        return False\n    return check_version(out.lstrip('v'), '0.9.12')\n", "path": "IPython/nbconvert/filters/markdown.py"}]}