We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
nbconvert: Default encoding problem on OS X
Greetings.

I am using IPython 1.1.0 via MacPorts on OSX 10.7.5.  The following problem is reproducible on the master git branch (IPython 2.0.0-dev).

On any call to nbconvert, I get the following failure:

```
[NbConvertApp] Using existing profile dir: u'/Users/USERNAME_REDACTED/.ipython/profile_default'
[NbConvertApp] Converting notebook ticks.ipynb to html
[NbConvertApp] Support files will be in ticks_files/
Traceback (most recent call last):
  File "/opt/local/bin/ipython", line 6, in <module>
    start_ipython()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/__init__.py", line 118, in start_ipython
    return launch_new_instance(argv=argv, **kwargs)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/config/application.py", line 545, in launch_instance
    app.start()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/terminal/ipapp.py", line 358, in start
    return self.subapp.start()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/nbconvert/nbconvertapp.py", line 267, in start
    self.convert_notebooks()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/nbconvert/nbconvertapp.py", line 300, in convert_notebooks
    output, resources = exporter.from_filename(notebook_filename, resources=resources)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/nbconvert/exporters/exporter.py", line 288, in from_filename
    with io.open(filename) as f:
LookupError: unknown encoding: 

If you suspect this is an IPython bug, please report it at:
    https://github.com/ipython/ipython/issues
or send an email to the mailing list at ipython-dev@scipy.org

You can print a more detailed traceback right now with "%tb", or use "%debug"
to interactively debug it.

Extra-detailed tracebacks for bug-reporting purposes can be enabled via:
    c.Application.verbose_crash=True
```

This is an easy fix: I change the troublesome line such that it reads,

```
with io.open(filename, encoding='ascii') as f:
```

However, this ad hoc and likely a suboptimal solution.  I wanted to bring this to the developers' attention and inquire about a proper solution.  Thanks!

System info:

```
python -c "import IPython; print(IPython.sys_info())"
{'codename': 'An Afternoon Hack',
 'commit_hash': '7c2ea3a',
 'commit_source': 'installation',
 'default_encoding': 'US-ASCII',
 'ipython_path': '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython',
 'ipython_version': '1.1.0',
 'os_name': 'posix',
 'platform': 'Darwin-11.4.2-x86_64-i386-64bit',
 'sys_executable': '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python',
 'sys_platform': 'darwin',
 'sys_version': '2.7.6 (default, Nov 19 2013, 16:37:14) \n[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)]'}
```


--- 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/exporters/exporter.py`
Content:
```
1 """This module defines Exporter, a highly configurable converter
2 that uses Jinja2 to export notebook files into different formats.
3 """
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 
17 from __future__ import print_function, absolute_import
18 
19 # Stdlib imports
20 import io
21 import os
22 import copy
23 import collections
24 import datetime
25 
26 
27 # IPython imports
28 from IPython.config.configurable import LoggingConfigurable
29 from IPython.config import Config
30 from IPython.nbformat import current as nbformat
31 from IPython.utils.traitlets import MetaHasTraits, Unicode, List
32 from IPython.utils.importstring import import_item
33 from IPython.utils import text, py3compat
34 
35 #-----------------------------------------------------------------------------
36 # Class
37 #-----------------------------------------------------------------------------
38 
39 class ResourcesDict(collections.defaultdict):
40     def __missing__(self, key):
41         return ''
42 
43 
44 class Exporter(LoggingConfigurable):
45     """
46     Class containing methods that sequentially run a list of preprocessors on a 
47     NotebookNode object and then return the modified NotebookNode object and 
48     accompanying resources dict.
49     """
50 
51     file_extension = Unicode(
52         'txt', config=True,
53         help="Extension of the file that should be written to disk"
54         )
55 
56     #Configurability, allows the user to easily add filters and preprocessors.
57     preprocessors = List(config=True,
58         help="""List of preprocessors, by name or namespace, to enable.""")
59 
60     _preprocessors = None
61 
62     default_preprocessors = List(['IPython.nbconvert.preprocessors.coalesce_streams',
63                                   'IPython.nbconvert.preprocessors.SVG2PDFPreprocessor',
64                                   'IPython.nbconvert.preprocessors.ExtractOutputPreprocessor',
65                                   'IPython.nbconvert.preprocessors.CSSHTMLHeaderPreprocessor',
66                                   'IPython.nbconvert.preprocessors.RevealHelpPreprocessor',
67                                   'IPython.nbconvert.preprocessors.LatexPreprocessor',
68                                   'IPython.nbconvert.preprocessors.HighlightMagicsPreprocessor'],
69         config=True,
70         help="""List of preprocessors available by default, by name, namespace, 
71         instance, or type.""")
72 
73 
74     def __init__(self, config=None, **kw):
75         """
76         Public constructor
77 
78         Parameters
79         ----------
80         config : config
81             User configuration instance.
82         """
83         with_default_config = self.default_config
84         if config:
85             with_default_config.merge(config)
86         
87         super(Exporter, self).__init__(config=with_default_config, **kw)
88 
89         self._init_preprocessors()
90 
91 
92     @property
93     def default_config(self):
94         return Config()
95 
96     @nbformat.docstring_nbformat_mod
97     def from_notebook_node(self, nb, resources=None, **kw):
98         """
99         Convert a notebook from a notebook node instance.
100 
101         Parameters
102         ----------
103         nb : :class:`~{nbformat_mod}.nbbase.NotebookNode`
104           Notebook node
105         resources : dict
106           Additional resources that can be accessed read/write by
107           preprocessors and filters.
108         **kw
109           Ignored (?)
110         """
111         nb_copy = copy.deepcopy(nb)
112         resources = self._init_resources(resources)
113 
114         # Preprocess
115         nb_copy, resources = self._preprocess(nb_copy, resources)
116 
117         return nb_copy, resources
118 
119 
120     def from_filename(self, filename, resources=None, **kw):
121         """
122         Convert a notebook from a notebook file.
123 
124         Parameters
125         ----------
126         filename : str
127             Full filename of the notebook file to open and convert.
128         """
129 
130         # Pull the metadata from the filesystem.
131         if resources is None:
132             resources = ResourcesDict()
133         if not 'metadata' in resources or resources['metadata'] == '':
134             resources['metadata'] = ResourcesDict()
135         basename = os.path.basename(filename)
136         notebook_name = basename[:basename.rfind('.')]
137         resources['metadata']['name'] = notebook_name
138 
139         modified_date = datetime.datetime.fromtimestamp(os.path.getmtime(filename))
140         resources['metadata']['modified_date'] = modified_date.strftime(text.date_format)
141 
142         with io.open(filename) as f:
143             return self.from_notebook_node(nbformat.read(f, 'json'), resources=resources, **kw)
144 
145 
146     def from_file(self, file_stream, resources=None, **kw):
147         """
148         Convert a notebook from a notebook file.
149 
150         Parameters
151         ----------
152         file_stream : file-like object
153             Notebook file-like object to convert.
154         """
155         return self.from_notebook_node(nbformat.read(file_stream, 'json'), resources=resources, **kw)
156 
157 
158     def register_preprocessor(self, preprocessor, enabled=False):
159         """
160         Register a preprocessor.
161         Preprocessors are classes that act upon the notebook before it is
162         passed into the Jinja templating engine.  preprocessors are also
163         capable of passing additional information to the Jinja
164         templating engine.
165 
166         Parameters
167         ----------
168         preprocessor : preprocessor
169         """
170         if preprocessor is None:
171             raise TypeError('preprocessor')
172         isclass = isinstance(preprocessor, type)
173         constructed = not isclass
174 
175         # Handle preprocessor's registration based on it's type
176         if constructed and isinstance(preprocessor, py3compat.string_types):
177             # Preprocessor is a string, import the namespace and recursively call
178             # this register_preprocessor method
179             preprocessor_cls = import_item(preprocessor)
180             return self.register_preprocessor(preprocessor_cls, enabled)
181 
182         if constructed and hasattr(preprocessor, '__call__'):
183             # Preprocessor is a function, no need to construct it.
184             # Register and return the preprocessor.
185             if enabled:
186                 preprocessor.enabled = True
187             self._preprocessors.append(preprocessor)
188             return preprocessor
189 
190         elif isclass and isinstance(preprocessor, MetaHasTraits):
191             # Preprocessor is configurable.  Make sure to pass in new default for 
192             # the enabled flag if one was specified.
193             self.register_preprocessor(preprocessor(parent=self), enabled)
194 
195         elif isclass:
196             # Preprocessor is not configurable, construct it
197             self.register_preprocessor(preprocessor(), enabled)
198 
199         else:
200             # Preprocessor is an instance of something without a __call__ 
201             # attribute.  
202             raise TypeError('preprocessor')
203 
204 
205     def _init_preprocessors(self):
206         """
207         Register all of the preprocessors needed for this exporter, disabled
208         unless specified explicitly.
209         """
210         if self._preprocessors is None:
211             self._preprocessors = []
212 
213         #Load default preprocessors (not necessarly enabled by default).
214         if self.default_preprocessors:
215             for preprocessor in self.default_preprocessors:
216                 self.register_preprocessor(preprocessor)
217 
218         #Load user preprocessors.  Enable by default.
219         if self.preprocessors:
220             for preprocessor in self.preprocessors:
221                 self.register_preprocessor(preprocessor, enabled=True)
222 
223 
224     def _init_resources(self, resources):
225 
226         #Make sure the resources dict is of ResourcesDict type.
227         if resources is None:
228             resources = ResourcesDict()
229         if not isinstance(resources, ResourcesDict):
230             new_resources = ResourcesDict()
231             new_resources.update(resources)
232             resources = new_resources
233 
234         #Make sure the metadata extension exists in resources
235         if 'metadata' in resources:
236             if not isinstance(resources['metadata'], ResourcesDict):
237                 resources['metadata'] = ResourcesDict(resources['metadata'])
238         else:
239             resources['metadata'] = ResourcesDict()
240             if not resources['metadata']['name']:
241                 resources['metadata']['name'] = 'Notebook'
242 
243         #Set the output extension
244         resources['output_extension'] = self.file_extension
245         return resources
246 
247 
248     def _preprocess(self, nb, resources):
249         """
250         Preprocess the notebook before passing it into the Jinja engine.
251         To preprocess the notebook is to apply all of the
252 
253         Parameters
254         ----------
255         nb : notebook node
256             notebook that is being exported.
257         resources : a dict of additional resources that
258             can be accessed read/write by preprocessors
259         """
260 
261         # Do a copy.deepcopy first,
262         # we are never safe enough with what the preprocessors could do.
263         nbc =  copy.deepcopy(nb)
264         resc = copy.deepcopy(resources)
265 
266         #Run each preprocessor on the notebook.  Carry the output along
267         #to each preprocessor
268         for preprocessor in self._preprocessors:
269             nbc, resc = preprocessor(nbc, resc)
270         return nbc, resc
271
```
--- 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()

```

diff --git a/IPython/nbconvert/exporters/exporter.py b/IPython/nbconvert/exporters/exporter.py
--- a/IPython/nbconvert/exporters/exporter.py
+++ b/IPython/nbconvert/exporters/exporter.py
@@ -139,7 +139,7 @@
         modified_date = datetime.datetime.fromtimestamp(os.path.getmtime(filename))
         resources['metadata']['modified_date'] = modified_date.strftime(text.date_format)
 
-        with io.open(filename) as f:
+        with io.open(filename, encoding='utf-8') as f:
             return self.from_notebook_node(nbformat.read(f, 'json'), resources=resources, **kw)

{"golden_diff": "diff --git a/IPython/nbconvert/exporters/exporter.py b/IPython/nbconvert/exporters/exporter.py\n--- a/IPython/nbconvert/exporters/exporter.py\n+++ b/IPython/nbconvert/exporters/exporter.py\n@@ -139,7 +139,7 @@\n         modified_date = datetime.datetime.fromtimestamp(os.path.getmtime(filename))\n         resources['metadata']['modified_date'] = modified_date.strftime(text.date_format)\n \n-        with io.open(filename) as f:\n+        with io.open(filename, encoding='utf-8') as f:\n             return self.from_notebook_node(nbformat.read(f, 'json'), resources=resources, **kw)\n", "issue": "nbconvert: Default encoding problem on OS X\nGreetings.\n\nI am using IPython 1.1.0 via MacPorts on OSX 10.7.5.  The following problem is reproducible on the master git branch (IPython 2.0.0-dev).\n\nOn any call to nbconvert, I get the following failure:\n\n```\n[NbConvertApp] Using existing profile dir: u'/Users/USERNAME_REDACTED/.ipython/profile_default'\n[NbConvertApp] Converting notebook ticks.ipynb to html\n[NbConvertApp] Support files will be in ticks_files/\nTraceback (most recent call last):\n  File \"/opt/local/bin/ipython\", line 6, in <module>\n    start_ipython()\n  File \"/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/__init__.py\", line 118, in start_ipython\n    return launch_new_instance(argv=argv, **kwargs)\n  File \"/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/config/application.py\", line 545, in launch_instance\n    app.start()\n  File \"/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/terminal/ipapp.py\", line 358, in start\n    return self.subapp.start()\n  File \"/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/nbconvert/nbconvertapp.py\", line 267, in start\n    self.convert_notebooks()\n  File \"/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/nbconvert/nbconvertapp.py\", line 300, in convert_notebooks\n    output, resources = exporter.from_filename(notebook_filename, resources=resources)\n  File \"/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/nbconvert/exporters/exporter.py\", line 288, in from_filename\n    with io.open(filename) as f:\nLookupError: unknown encoding: \n\nIf you suspect this is an IPython bug, please report it at:\n    https://github.com/ipython/ipython/issues\nor send an email to the mailing list at ipython-dev@scipy.org\n\nYou can print a more detailed traceback right now with \"%tb\", or use \"%debug\"\nto interactively debug it.\n\nExtra-detailed tracebacks for bug-reporting purposes can be enabled via:\n    c.Application.verbose_crash=True\n```\n\nThis is an easy fix: I change the troublesome line such that it reads,\n\n```\nwith io.open(filename, encoding='ascii') as f:\n```\n\nHowever, this ad hoc and likely a suboptimal solution.  I wanted to bring this to the developers' attention and inquire about a proper solution.  Thanks!\n\nSystem info:\n\n```\npython -c \"import IPython; print(IPython.sys_info())\"\n{'codename': 'An Afternoon Hack',\n 'commit_hash': '7c2ea3a',\n 'commit_source': 'installation',\n 'default_encoding': 'US-ASCII',\n 'ipython_path': '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython',\n 'ipython_version': '1.1.0',\n 'os_name': 'posix',\n 'platform': 'Darwin-11.4.2-x86_64-i386-64bit',\n 'sys_executable': '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python',\n 'sys_platform': 'darwin',\n 'sys_version': '2.7.6 (default, Nov 19 2013, 16:37:14) \\n[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)]'}\n```\n\n", "before_files": [{"content": "\"\"\"This module defines Exporter, a highly configurable converter\nthat uses Jinja2 to export notebook files into different formats.\n\"\"\"\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#-----------------------------------------------------------------------------\n\nfrom __future__ import print_function, absolute_import\n\n# Stdlib imports\nimport io\nimport os\nimport copy\nimport collections\nimport datetime\n\n\n# IPython imports\nfrom IPython.config.configurable import LoggingConfigurable\nfrom IPython.config import Config\nfrom IPython.nbformat import current as nbformat\nfrom IPython.utils.traitlets import MetaHasTraits, Unicode, List\nfrom IPython.utils.importstring import import_item\nfrom IPython.utils import text, py3compat\n\n#-----------------------------------------------------------------------------\n# Class\n#-----------------------------------------------------------------------------\n\nclass ResourcesDict(collections.defaultdict):\n    def __missing__(self, key):\n        return ''\n\n\nclass Exporter(LoggingConfigurable):\n    \"\"\"\n    Class containing methods that sequentially run a list of preprocessors on a \n    NotebookNode object and then return the modified NotebookNode object and \n    accompanying resources dict.\n    \"\"\"\n\n    file_extension = Unicode(\n        'txt', config=True,\n        help=\"Extension of the file that should be written to disk\"\n        )\n\n    #Configurability, allows the user to easily add filters and preprocessors.\n    preprocessors = List(config=True,\n        help=\"\"\"List of preprocessors, by name or namespace, to enable.\"\"\")\n\n    _preprocessors = None\n\n    default_preprocessors = List(['IPython.nbconvert.preprocessors.coalesce_streams',\n                                  'IPython.nbconvert.preprocessors.SVG2PDFPreprocessor',\n                                  'IPython.nbconvert.preprocessors.ExtractOutputPreprocessor',\n                                  'IPython.nbconvert.preprocessors.CSSHTMLHeaderPreprocessor',\n                                  'IPython.nbconvert.preprocessors.RevealHelpPreprocessor',\n                                  'IPython.nbconvert.preprocessors.LatexPreprocessor',\n                                  'IPython.nbconvert.preprocessors.HighlightMagicsPreprocessor'],\n        config=True,\n        help=\"\"\"List of preprocessors available by default, by name, namespace, \n        instance, or type.\"\"\")\n\n\n    def __init__(self, config=None, **kw):\n        \"\"\"\n        Public constructor\n\n        Parameters\n        ----------\n        config : config\n            User configuration instance.\n        \"\"\"\n        with_default_config = self.default_config\n        if config:\n            with_default_config.merge(config)\n        \n        super(Exporter, self).__init__(config=with_default_config, **kw)\n\n        self._init_preprocessors()\n\n\n    @property\n    def default_config(self):\n        return Config()\n\n    @nbformat.docstring_nbformat_mod\n    def from_notebook_node(self, nb, resources=None, **kw):\n        \"\"\"\n        Convert a notebook from a notebook node instance.\n\n        Parameters\n        ----------\n        nb : :class:`~{nbformat_mod}.nbbase.NotebookNode`\n          Notebook node\n        resources : dict\n          Additional resources that can be accessed read/write by\n          preprocessors and filters.\n        **kw\n          Ignored (?)\n        \"\"\"\n        nb_copy = copy.deepcopy(nb)\n        resources = self._init_resources(resources)\n\n        # Preprocess\n        nb_copy, resources = self._preprocess(nb_copy, resources)\n\n        return nb_copy, resources\n\n\n    def from_filename(self, filename, resources=None, **kw):\n        \"\"\"\n        Convert a notebook from a notebook file.\n\n        Parameters\n        ----------\n        filename : str\n            Full filename of the notebook file to open and convert.\n        \"\"\"\n\n        # Pull the metadata from the filesystem.\n        if resources is None:\n            resources = ResourcesDict()\n        if not 'metadata' in resources or resources['metadata'] == '':\n            resources['metadata'] = ResourcesDict()\n        basename = os.path.basename(filename)\n        notebook_name = basename[:basename.rfind('.')]\n        resources['metadata']['name'] = notebook_name\n\n        modified_date = datetime.datetime.fromtimestamp(os.path.getmtime(filename))\n        resources['metadata']['modified_date'] = modified_date.strftime(text.date_format)\n\n        with io.open(filename) as f:\n            return self.from_notebook_node(nbformat.read(f, 'json'), resources=resources, **kw)\n\n\n    def from_file(self, file_stream, resources=None, **kw):\n        \"\"\"\n        Convert a notebook from a notebook file.\n\n        Parameters\n        ----------\n        file_stream : file-like object\n            Notebook file-like object to convert.\n        \"\"\"\n        return self.from_notebook_node(nbformat.read(file_stream, 'json'), resources=resources, **kw)\n\n\n    def register_preprocessor(self, preprocessor, enabled=False):\n        \"\"\"\n        Register a preprocessor.\n        Preprocessors are classes that act upon the notebook before it is\n        passed into the Jinja templating engine.  preprocessors are also\n        capable of passing additional information to the Jinja\n        templating engine.\n\n        Parameters\n        ----------\n        preprocessor : preprocessor\n        \"\"\"\n        if preprocessor is None:\n            raise TypeError('preprocessor')\n        isclass = isinstance(preprocessor, type)\n        constructed = not isclass\n\n        # Handle preprocessor's registration based on it's type\n        if constructed and isinstance(preprocessor, py3compat.string_types):\n            # Preprocessor is a string, import the namespace and recursively call\n            # this register_preprocessor method\n            preprocessor_cls = import_item(preprocessor)\n            return self.register_preprocessor(preprocessor_cls, enabled)\n\n        if constructed and hasattr(preprocessor, '__call__'):\n            # Preprocessor is a function, no need to construct it.\n            # Register and return the preprocessor.\n            if enabled:\n                preprocessor.enabled = True\n            self._preprocessors.append(preprocessor)\n            return preprocessor\n\n        elif isclass and isinstance(preprocessor, MetaHasTraits):\n            # Preprocessor is configurable.  Make sure to pass in new default for \n            # the enabled flag if one was specified.\n            self.register_preprocessor(preprocessor(parent=self), enabled)\n\n        elif isclass:\n            # Preprocessor is not configurable, construct it\n            self.register_preprocessor(preprocessor(), enabled)\n\n        else:\n            # Preprocessor is an instance of something without a __call__ \n            # attribute.  \n            raise TypeError('preprocessor')\n\n\n    def _init_preprocessors(self):\n        \"\"\"\n        Register all of the preprocessors needed for this exporter, disabled\n        unless specified explicitly.\n        \"\"\"\n        if self._preprocessors is None:\n            self._preprocessors = []\n\n        #Load default preprocessors (not necessarly enabled by default).\n        if self.default_preprocessors:\n            for preprocessor in self.default_preprocessors:\n                self.register_preprocessor(preprocessor)\n\n        #Load user preprocessors.  Enable by default.\n        if self.preprocessors:\n            for preprocessor in self.preprocessors:\n                self.register_preprocessor(preprocessor, enabled=True)\n\n\n    def _init_resources(self, resources):\n\n        #Make sure the resources dict is of ResourcesDict type.\n        if resources is None:\n            resources = ResourcesDict()\n        if not isinstance(resources, ResourcesDict):\n            new_resources = ResourcesDict()\n            new_resources.update(resources)\n            resources = new_resources\n\n        #Make sure the metadata extension exists in resources\n        if 'metadata' in resources:\n            if not isinstance(resources['metadata'], ResourcesDict):\n                resources['metadata'] = ResourcesDict(resources['metadata'])\n        else:\n            resources['metadata'] = ResourcesDict()\n            if not resources['metadata']['name']:\n                resources['metadata']['name'] = 'Notebook'\n\n        #Set the output extension\n        resources['output_extension'] = self.file_extension\n        return resources\n\n\n    def _preprocess(self, nb, resources):\n        \"\"\"\n        Preprocess the notebook before passing it into the Jinja engine.\n        To preprocess the notebook is to apply all of the\n\n        Parameters\n        ----------\n        nb : notebook node\n            notebook that is being exported.\n        resources : a dict of additional resources that\n            can be accessed read/write by preprocessors\n        \"\"\"\n\n        # Do a copy.deepcopy first,\n        # we are never safe enough with what the preprocessors could do.\n        nbc =  copy.deepcopy(nb)\n        resc = copy.deepcopy(resources)\n\n        #Run each preprocessor on the notebook.  Carry the output along\n        #to each preprocessor\n        for preprocessor in self._preprocessors:\n            nbc, resc = preprocessor(nbc, resc)\n        return nbc, resc\n", "path": "IPython/nbconvert/exporters/exporter.py"}], "after_files": [{"content": "\"\"\"This module defines Exporter, a highly configurable converter\nthat uses Jinja2 to export notebook files into different formats.\n\"\"\"\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#-----------------------------------------------------------------------------\n\nfrom __future__ import print_function, absolute_import\n\n# Stdlib imports\nimport io\nimport os\nimport copy\nimport collections\nimport datetime\n\n\n# IPython imports\nfrom IPython.config.configurable import LoggingConfigurable\nfrom IPython.config import Config\nfrom IPython.nbformat import current as nbformat\nfrom IPython.utils.traitlets import MetaHasTraits, Unicode, List\nfrom IPython.utils.importstring import import_item\nfrom IPython.utils import text, py3compat\n\n#-----------------------------------------------------------------------------\n# Class\n#-----------------------------------------------------------------------------\n\nclass ResourcesDict(collections.defaultdict):\n    def __missing__(self, key):\n        return ''\n\n\nclass Exporter(LoggingConfigurable):\n    \"\"\"\n    Class containing methods that sequentially run a list of preprocessors on a \n    NotebookNode object and then return the modified NotebookNode object and \n    accompanying resources dict.\n    \"\"\"\n\n    file_extension = Unicode(\n        'txt', config=True,\n        help=\"Extension of the file that should be written to disk\"\n        )\n\n    #Configurability, allows the user to easily add filters and preprocessors.\n    preprocessors = List(config=True,\n        help=\"\"\"List of preprocessors, by name or namespace, to enable.\"\"\")\n\n    _preprocessors = None\n\n    default_preprocessors = List(['IPython.nbconvert.preprocessors.coalesce_streams',\n                                  'IPython.nbconvert.preprocessors.SVG2PDFPreprocessor',\n                                  'IPython.nbconvert.preprocessors.ExtractOutputPreprocessor',\n                                  'IPython.nbconvert.preprocessors.CSSHTMLHeaderPreprocessor',\n                                  'IPython.nbconvert.preprocessors.RevealHelpPreprocessor',\n                                  'IPython.nbconvert.preprocessors.LatexPreprocessor',\n                                  'IPython.nbconvert.preprocessors.HighlightMagicsPreprocessor'],\n        config=True,\n        help=\"\"\"List of preprocessors available by default, by name, namespace, \n        instance, or type.\"\"\")\n\n\n    def __init__(self, config=None, **kw):\n        \"\"\"\n        Public constructor\n\n        Parameters\n        ----------\n        config : config\n            User configuration instance.\n        \"\"\"\n        with_default_config = self.default_config\n        if config:\n            with_default_config.merge(config)\n        \n        super(Exporter, self).__init__(config=with_default_config, **kw)\n\n        self._init_preprocessors()\n\n\n    @property\n    def default_config(self):\n        return Config()\n\n    @nbformat.docstring_nbformat_mod\n    def from_notebook_node(self, nb, resources=None, **kw):\n        \"\"\"\n        Convert a notebook from a notebook node instance.\n\n        Parameters\n        ----------\n        nb : :class:`~{nbformat_mod}.nbbase.NotebookNode`\n          Notebook node\n        resources : dict\n          Additional resources that can be accessed read/write by\n          preprocessors and filters.\n        **kw\n          Ignored (?)\n        \"\"\"\n        nb_copy = copy.deepcopy(nb)\n        resources = self._init_resources(resources)\n\n        # Preprocess\n        nb_copy, resources = self._preprocess(nb_copy, resources)\n\n        return nb_copy, resources\n\n\n    def from_filename(self, filename, resources=None, **kw):\n        \"\"\"\n        Convert a notebook from a notebook file.\n\n        Parameters\n        ----------\n        filename : str\n            Full filename of the notebook file to open and convert.\n        \"\"\"\n\n        # Pull the metadata from the filesystem.\n        if resources is None:\n            resources = ResourcesDict()\n        if not 'metadata' in resources or resources['metadata'] == '':\n            resources['metadata'] = ResourcesDict()\n        basename = os.path.basename(filename)\n        notebook_name = basename[:basename.rfind('.')]\n        resources['metadata']['name'] = notebook_name\n\n        modified_date = datetime.datetime.fromtimestamp(os.path.getmtime(filename))\n        resources['metadata']['modified_date'] = modified_date.strftime(text.date_format)\n\n        with io.open(filename, encoding='utf-8') as f:\n            return self.from_notebook_node(nbformat.read(f, 'json'), resources=resources, **kw)\n\n\n    def from_file(self, file_stream, resources=None, **kw):\n        \"\"\"\n        Convert a notebook from a notebook file.\n\n        Parameters\n        ----------\n        file_stream : file-like object\n            Notebook file-like object to convert.\n        \"\"\"\n        return self.from_notebook_node(nbformat.read(file_stream, 'json'), resources=resources, **kw)\n\n\n    def register_preprocessor(self, preprocessor, enabled=False):\n        \"\"\"\n        Register a preprocessor.\n        Preprocessors are classes that act upon the notebook before it is\n        passed into the Jinja templating engine.  preprocessors are also\n        capable of passing additional information to the Jinja\n        templating engine.\n\n        Parameters\n        ----------\n        preprocessor : preprocessor\n        \"\"\"\n        if preprocessor is None:\n            raise TypeError('preprocessor')\n        isclass = isinstance(preprocessor, type)\n        constructed = not isclass\n\n        # Handle preprocessor's registration based on it's type\n        if constructed and isinstance(preprocessor, py3compat.string_types):\n            # Preprocessor is a string, import the namespace and recursively call\n            # this register_preprocessor method\n            preprocessor_cls = import_item(preprocessor)\n            return self.register_preprocessor(preprocessor_cls, enabled)\n\n        if constructed and hasattr(preprocessor, '__call__'):\n            # Preprocessor is a function, no need to construct it.\n            # Register and return the preprocessor.\n            if enabled:\n                preprocessor.enabled = True\n            self._preprocessors.append(preprocessor)\n            return preprocessor\n\n        elif isclass and isinstance(preprocessor, MetaHasTraits):\n            # Preprocessor is configurable.  Make sure to pass in new default for \n            # the enabled flag if one was specified.\n            self.register_preprocessor(preprocessor(parent=self), enabled)\n\n        elif isclass:\n            # Preprocessor is not configurable, construct it\n            self.register_preprocessor(preprocessor(), enabled)\n\n        else:\n            # Preprocessor is an instance of something without a __call__ \n            # attribute.  \n            raise TypeError('preprocessor')\n\n\n    def _init_preprocessors(self):\n        \"\"\"\n        Register all of the preprocessors needed for this exporter, disabled\n        unless specified explicitly.\n        \"\"\"\n        if self._preprocessors is None:\n            self._preprocessors = []\n\n        #Load default preprocessors (not necessarly enabled by default).\n        if self.default_preprocessors:\n            for preprocessor in self.default_preprocessors:\n                self.register_preprocessor(preprocessor)\n\n        #Load user preprocessors.  Enable by default.\n        if self.preprocessors:\n            for preprocessor in self.preprocessors:\n                self.register_preprocessor(preprocessor, enabled=True)\n\n\n    def _init_resources(self, resources):\n\n        #Make sure the resources dict is of ResourcesDict type.\n        if resources is None:\n            resources = ResourcesDict()\n        if not isinstance(resources, ResourcesDict):\n            new_resources = ResourcesDict()\n            new_resources.update(resources)\n            resources = new_resources\n\n        #Make sure the metadata extension exists in resources\n        if 'metadata' in resources:\n            if not isinstance(resources['metadata'], ResourcesDict):\n                resources['metadata'] = ResourcesDict(resources['metadata'])\n        else:\n            resources['metadata'] = ResourcesDict()\n            if not resources['metadata']['name']:\n                resources['metadata']['name'] = 'Notebook'\n\n        #Set the output extension\n        resources['output_extension'] = self.file_extension\n        return resources\n\n\n    def _preprocess(self, nb, resources):\n        \"\"\"\n        Preprocess the notebook before passing it into the Jinja engine.\n        To preprocess the notebook is to apply all of the\n\n        Parameters\n        ----------\n        nb : notebook node\n            notebook that is being exported.\n        resources : a dict of additional resources that\n            can be accessed read/write by preprocessors\n        \"\"\"\n\n        # Do a copy.deepcopy first,\n        # we are never safe enough with what the preprocessors could do.\n        nbc =  copy.deepcopy(nb)\n        resc = copy.deepcopy(resources)\n\n        #Run each preprocessor on the notebook.  Carry the output along\n        #to each preprocessor\n        for preprocessor in self._preprocessors:\n            nbc, resc = preprocessor(nbc, resc)\n        return nbc, resc\n", "path": "IPython/nbconvert/exporters/exporter.py"}]}