##// END OF EJS Templates
Introduce standard structure from coding guidelines in converters/.
David Warde-Farley -
Show More
@@ -1,3 +1,18 b''
1 """Notebook formatting converters.
2
3 This module provides several classes meant to deal with the conversion of
4 IPython notebooks to a variety of exported formats. All of these classes
5 inherit from `Converter`, which provides some general-purpose functionality
6 and defines the API relied upon by the `nbconvert` tool.
7 """
8 #-----------------------------------------------------------------------------
9 # Copyright (c) 2012, the IPython Development Team.
10 #
11 # Distributed under the terms of the Modified BSD License.
12 #
13 # The full license is in the file COPYING.txt, distributed with this software.
14 #-----------------------------------------------------------------------------
15
1 from .html import ConverterHTML
16 from .html import ConverterHTML
2 from .markdown import ConverterMarkdown
17 from .markdown import ConverterMarkdown
3 from .bloggerhtml import ConverterBloggerHTML
18 from .bloggerhtml import ConverterBloggerHTML
@@ -1,7 +1,23 b''
1 """Base classes for the notebook conversion pipeline.
2
3 This module defines Converter, from which all objects designed to implement
4 a conversion of IPython notebooks to some other format should inherit.
5 """
6 #-----------------------------------------------------------------------------
7 # Copyright (c) 2012, the IPython Development Team.
8 #
9 # Distributed under the terms of the Modified BSD License.
10 #
11 # The full license is in the file COPYING.txt, distributed with this software.
12 #-----------------------------------------------------------------------------
13
14 #-----------------------------------------------------------------------------
15 # Imports
16 #-----------------------------------------------------------------------------
17
1 from __future__ import print_function, absolute_import
18 from __future__ import print_function, absolute_import
2 from converters.utils import remove_fake_files_url
3
19
4 # Stdlib
20 # Stdlib imports
5 import codecs
21 import codecs
6 import io
22 import io
7 import logging
23 import logging
@@ -10,10 +26,16 b' import pprint'
10 import re
26 import re
11 from types import FunctionType
27 from types import FunctionType
12
28
13 # From IPython
29 # IPython imports
14 from IPython.nbformat import current as nbformat
30 from IPython.nbformat import current as nbformat
15
31
16 # local
32 # Our own imports
33 from converters.utils import remove_fake_files_url
34
35
36 #-----------------------------------------------------------------------------
37 # Local utilities
38 #-----------------------------------------------------------------------------
17
39
18 def clean_filename(filename):
40 def clean_filename(filename):
19 """
41 """
@@ -33,11 +55,11 b' def clean_filename(filename):'
33 filename = re.sub(r'[^a-zA-Z0-9_]', '_', filename)
55 filename = re.sub(r'[^a-zA-Z0-9_]', '_', filename)
34 return filename
56 return filename
35
57
58
36 #-----------------------------------------------------------------------------
59 #-----------------------------------------------------------------------------
37 # Class declarations
60 # Class declarations
38 #-----------------------------------------------------------------------------
61 #-----------------------------------------------------------------------------
39
62
40
41 class ConversionException(Exception):
63 class ConversionException(Exception):
42 pass
64 pass
43
65
@@ -1,6 +1,31 b''
1 from converters.html import ConverterHTML
1 """Notebook export in Blogger-aware HTML.
2
3 This file contains `ConverterBloggerHTML`, a subclass of `ConverterHTML` that
4 provides output suitable for easy pasting into a blog hosted on the Blogger
5 platform. See the class docstring for more information.
6 """
7 #-----------------------------------------------------------------------------
8 # Copyright (c) 2012, the IPython Development Team.
9 #
10 # Distributed under the terms of the Modified BSD License.
11 #
12 # The full license is in the file COPYING.txt, distributed with this software.
13 #-----------------------------------------------------------------------------
14
15 #-----------------------------------------------------------------------------
16 # Imports
17 #-----------------------------------------------------------------------------
18
19 # Stdlib imports
2 import io
20 import io
3
21
22 # Our own imports
23 from converters.html import ConverterHTML
24
25
26 #-----------------------------------------------------------------------------
27 # Classes and functions
28 #-----------------------------------------------------------------------------
4
29
5 class ConverterBloggerHTML(ConverterHTML):
30 class ConverterBloggerHTML(ConverterHTML):
6 """Convert a notebook to html suitable for easy pasting into Blogger.
31 """Convert a notebook to html suitable for easy pasting into Blogger.
@@ -1,15 +1,42 b''
1 """Implements conversion to ordinary HTML output.
2
3 This file implements a class that handles rendering IPython notebooks as
4 HTML, suitable for posting to the web.
5
6 Converters for more specific HTML generation needs (suitable for posting to
7 a particular web service) can usefully subclass `ConverterHTML` and override
8 certain methods. For output tuned to the Blogger blogging platform, see the
9 `ConverterBloggerHTML` class.
10 """
11 #-----------------------------------------------------------------------------
12 # Copyright (c) 2012, the IPython Development Team.
13 #
14 # Distributed under the terms of the Modified BSD License.
15 #
16 # The full license is in the file COPYING.txt, distributed with this software.
17 #-----------------------------------------------------------------------------
18
1 from __future__ import absolute_import
19 from __future__ import absolute_import
2
20
21 # Stdlib imports
22 import io
23 import os
24
25 # Third-party imports
26 from markdown import markdown
27
28 # IPython imports
29 from IPython.utils import path
30
31 # Our own imports
3 from converters.base import Converter
32 from converters.base import Converter
4 from converters.utils import text_cell, output_container
33 from converters.utils import text_cell, output_container
5 from converters.utils import highlight, coalesce_streams, ansi2html
34 from converters.utils import highlight, coalesce_streams, ansi2html
6
35
7 from IPython.utils import path
8 from markdown import markdown
9 import os
10 import io
11
12
36
37 #-----------------------------------------------------------------------------
38 # Classes and functions
39 #-----------------------------------------------------------------------------
13 class ConverterHTML(Converter):
40 class ConverterHTML(Converter):
14 extension = 'html'
41 extension = 'html'
15 blank_symbol = ' '
42 blank_symbol = ' '
@@ -1,9 +1,35 b''
1 from converters.base import Converter
1 """Notebook export to LaTeX.
2 from converters.utils import markdown2latex, remove_ansi
2
3 This file implements a converter class for rendering IPython notebooks as
4 LaTeX, suitable for rendering by pdflatex.
5 """
6 #-----------------------------------------------------------------------------
7 # Copyright (c) 2012, the IPython Development Team.
8 #
9 # Distributed under the terms of the Modified BSD License.
10 #
11 # The full license is in the file COPYING.txt, distributed with this software.
12 #-----------------------------------------------------------------------------
13
14 #-----------------------------------------------------------------------------
15 # Imports
16 #-----------------------------------------------------------------------------
17
18 # Stdlib imports
3 import os
19 import os
4 import subprocess
20 import subprocess
5 import sys
21 import sys
6
22
23 # Our own imports
24 from converters.base import Converter
25 from converters.utils import markdown2latex, remove_ansi
26
27
28 #-----------------------------------------------------------------------------
29 # Globals and constants
30 #-----------------------------------------------------------------------------
31
32 # XXX: This is a hack that needs to be addressed in a more principled fashion.
7 inkscape = 'inkscape'
33 inkscape = 'inkscape'
8 if sys.platform == 'darwin':
34 if sys.platform == 'darwin':
9 inkscape = '/Applications/Inkscape.app/Contents/Resources/bin/inkscape'
35 inkscape = '/Applications/Inkscape.app/Contents/Resources/bin/inkscape'
@@ -11,6 +37,10 b" if sys.platform == 'darwin':"
11 inkscape = None
37 inkscape = None
12
38
13
39
40 #-----------------------------------------------------------------------------
41 # Classes and functions
42 #-----------------------------------------------------------------------------
43
14 class ConverterLaTeX(Converter):
44 class ConverterLaTeX(Converter):
15 """Converts a notebook to a .tex file suitable for pdflatex.
45 """Converts a notebook to a .tex file suitable for pdflatex.
16
46
@@ -1,7 +1,21 b''
1 """A custom pygments lexer for IPython code cells.
2
3 Informs The pygments highlighting library of the quirks of IPython's superset
4 of Python -- magic commands, !shell commands, etc.
5 """
6 #-----------------------------------------------------------------------------
7 # Copyright (c) 2012, the IPython Development Team.
8 #
9 # Distributed under the terms of the Modified BSD License.
10 #
11 # The full license is in the file COPYING.txt, distributed with this software.
12 #-----------------------------------------------------------------------------
13
1 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
2 # Imports
15 # Imports
3 #-----------------------------------------------------------------------------
16 #-----------------------------------------------------------------------------
4
17
18 # Third-party imports
5 from pygments.lexers import PythonLexer, BashLexer
19 from pygments.lexers import PythonLexer, BashLexer
6 from pygments.lexer import bygroups, using
20 from pygments.lexer import bygroups, using
7 from pygments.token import Keyword, Operator, Text
21 from pygments.token import Keyword, Operator, Text
@@ -1,8 +1,29 b''
1 """Converter implementing Markdown export.
2
3 Implements a Converter that allows IPython notebooks to reasonably rendered
4 as a Markdown document.
5 """
6 #-----------------------------------------------------------------------------
7 # Copyright (c) 2012, the IPython Development Team.
8 #
9 # Distributed under the terms of the Modified BSD License.
10 #
11 # The full license is in the file COPYING.txt, distributed with this software.
12 #-----------------------------------------------------------------------------
13
14 #-----------------------------------------------------------------------------
15 # Imports
16 #-----------------------------------------------------------------------------
17
18 # Our own imports
1 from converters.base import Converter
19 from converters.base import Converter
2 from converters.utils import highlight, remove_ansi
20 from converters.utils import highlight, remove_ansi
3 from IPython.utils.text import indent
21 from IPython.utils.text import indent
4
22
5
23
24 #-----------------------------------------------------------------------------
25 # Classes and functions
26 #-----------------------------------------------------------------------------
6 class ConverterMarkdown(Converter):
27 class ConverterMarkdown(Converter):
7 extension = 'md'
28 extension = 'md'
8
29
@@ -1,10 +1,34 b''
1 """Base class for doing notebook-to-notebook transformations.
2
3 This implements a converter class that turns an IPython notebook into another
4 IPython notebook, mainly so that it can be subclassed to perform more useful
5 and sophisticated transformations.
6 """
7 #-----------------------------------------------------------------------------
8 # Copyright (c) 2011, the IPython Development Team.
9 #
10 # Distributed under the terms of the Modified BSD License.
11 #
12 # The full license is in the file COPYING.txt, distributed with this software.
13 #-----------------------------------------------------------------------------
14
15 #-----------------------------------------------------------------------------
16 # Imports
17 #-----------------------------------------------------------------------------
18
19 # Stdlib imports
20 import json
1 import os
21 import os
22 from shutil import rmtree
23
24 # Our own imports
2 from converters.base import Converter
25 from converters.base import Converter
3 from converters.utils import cell_to_lines
26 from converters.utils import cell_to_lines
4 from shutil import rmtree
5 import json
6
27
7
28
29 #-----------------------------------------------------------------------------
30 # Classes and functions
31 #-----------------------------------------------------------------------------
8 class ConverterNotebook(Converter):
32 class ConverterNotebook(Converter):
9 """
33 """
10 A converter that is essentially a null-op.
34 A converter that is essentially a null-op.
@@ -1,8 +1,32 b''
1 from converters.base import Converter
1 """Notebook export to .py source code files.
2
3 Since Python export is provided in the notebook itself (provided by classes
4 in `IPython.nbformat`), this class serves mainly as a base class for other
5 converters that may wish to implement cell-type-specific behaviors.
6 """
7 #-----------------------------------------------------------------------------
8 # Copyright (c) 2012, the IPython Development Team.
9 #
10 # Distributed under the terms of the Modified BSD License.
11 #
12 # The full license is in the file COPYING.txt, distributed with this software.
13 #-----------------------------------------------------------------------------
14
15 #-----------------------------------------------------------------------------
16 # Imports
17 #-----------------------------------------------------------------------------
18
19 # IPython imports
2 from IPython.utils.text import indent
20 from IPython.utils.text import indent
21
22 # Our own imports
23 from converters.base import Converter
3 from converters.utils import remove_ansi
24 from converters.utils import remove_ansi
4
25
5
26
27 #-----------------------------------------------------------------------------
28 # Classes and functions
29 #-----------------------------------------------------------------------------
6 class ConverterPy(Converter):
30 class ConverterPy(Converter):
7 """
31 """
8 A converter that takes a notebook and converts it to a .py file.
32 A converter that takes a notebook and converts it to a .py file.
@@ -1,8 +1,31 b''
1 """Notebook export to reStructuredText (rST).
2
3 This file provides a converter class for rendering IPython notebooks as valid
4 reStructuredText, suitable for inclusion in e.g. Sphinx documentation.
5 """
6 #-----------------------------------------------------------------------------
7 # Copyright (c) 2012, the IPython Development Team.
8 #
9 # Distributed under the terms of the Modified BSD License.
10 #
11 # The full license is in the file COPYING.txt, distributed with this software.
12 #-----------------------------------------------------------------------------
13
14 #-----------------------------------------------------------------------------
15 # Imports
16 #-----------------------------------------------------------------------------
17
18 # IPython imports
19 from IPython.utils.text import indent
20
21 # Our own imports
1 from converters.base import Converter
22 from converters.base import Converter
2 from converters.utils import markdown2rst, rst_directive, remove_ansi
23 from converters.utils import markdown2rst, rst_directive, remove_ansi
3 from IPython.utils.text import indent
4
24
5
25
26 #-----------------------------------------------------------------------------
27 # Classes and functions
28 #-----------------------------------------------------------------------------
6 class ConverterRST(Converter):
29 class ConverterRST(Converter):
7 extension = 'rst'
30 extension = 'rst'
8 heading_level = {1: '=', 2: '-', 3: '`', 4: '\'', 5: '.', 6: '~'}
31 heading_level = {1: '=', 2: '-', 3: '`', 4: '\'', 5: '.', 6: '~'}
@@ -1,6 +1,24 b''
1 """A one-line description.
2
3 A longer description that spans multiple lines. Explain the purpose of the
4 file and provide a short list of the key classes/functions it contains. This
5 is the docstring shown when some does 'import foo;foo?' in IPython, so it
6 should be reasonably useful and informative.
7 """
8 #-----------------------------------------------------------------------------
9 # Copyright (c) 2012, the IPython Development Team.
10 #
11 # Distributed under the terms of the Modified BSD License.
12 #
13 # The full license is in the file COPYING.txt, distributed with this software.
14 #-----------------------------------------------------------------------------
15
16 #-----------------------------------------------------------------------------
17 # Imports
18 #-----------------------------------------------------------------------------
1 from __future__ import print_function
19 from __future__ import print_function
2 from lexers import IPythonLexer
3
20
21 # Stdlib imports
4 import subprocess
22 import subprocess
5 import copy
23 import copy
6 import json
24 import json
@@ -8,10 +26,19 b' import re'
8 import os
26 import os
9 import sys
27 import sys
10
28
29 # IPython imports
11 from IPython.utils.text import indent
30 from IPython.utils.text import indent
12 from IPython.utils import py3compat
31 from IPython.utils import py3compat
13 from IPython.nbformat.v3.nbjson import BytesEncoder
32 from IPython.nbformat.v3.nbjson import BytesEncoder
14
33
34 # Our own imports
35 from lexers import IPythonLexer
36
37 #-----------------------------------------------------------------------------
38 # Globals and constants
39 #-----------------------------------------------------------------------------
40 _multiline_outputs = ['text', 'html', 'svg', 'latex', 'javascript', 'json']
41
15
42
16 #-----------------------------------------------------------------------------
43 #-----------------------------------------------------------------------------
17 # Utility functions
44 # Utility functions
@@ -308,9 +335,6 b' def writes_cell(cell, **kwargs):'
308 return py3compat.str_to_unicode(json.dumps(cell, **kwargs), 'utf-8')
335 return py3compat.str_to_unicode(json.dumps(cell, **kwargs), 'utf-8')
309
336
310
337
311 _multiline_outputs = ['text', 'html', 'svg', 'latex', 'javascript', 'json']
312
313
314 def split_lines_cell(cell):
338 def split_lines_cell(cell):
315 """
339 """
316 Split lines within a cell as in
340 Split lines within a cell as in
General Comments 0
You need to be logged in to leave comments. Login now