##// END OF EJS Templates
Quick fix for failing tests. This will be cleaned up soon....
Maximilian Albert -
Show More
@@ -1,99 +1,104
1 from converters.base import Converter
1 from converters.base import Converter
2 from IPython.utils.text import indent
2 from IPython.utils.text import indent
3 from converters.utils import remove_ansi
3 from converters.utils import remove_ansi
4
4
5 class ConverterPy(Converter):
5 class ConverterPy(Converter):
6 """
6 """
7 A converter that takes a notebook and converts it to a .py file.
7 A converter that takes a notebook and converts it to a .py file.
8
8
9 What distinguishes this from PyWriter and PyReader in IPython.nbformat is
9 What distinguishes this from PyWriter and PyReader in IPython.nbformat is
10 that subclasses can specify what to do with each type of cell.
10 that subclasses can specify what to do with each type of cell.
11 Additionally, unlike PyWriter, this does not preserve the '# <markdown>'
11 Additionally, unlike PyWriter, this does not preserve the '# <markdown>'
12 opening and closing comments style comments in favor of a cleaner looking
12 opening and closing comments style comments in favor of a cleaner looking
13 python program.
13 python program.
14
14
15 Note:
15 Note:
16 Even though this produces a .py file, it is not guaranteed to be valid
16 Even though this produces a .py file, it is not guaranteed to be valid
17 python file, since the notebook may be using magics and even cell
17 python file, since the notebook may be using magics and even cell
18 magics.
18 magics.
19 """
19 """
20 extension = 'py'
20 extension = 'py'
21
21
22 def __init__(self, infile, show_prompts=True, show_output=True):
22 def __init__(self, infile, show_prompts=True, show_output=True):
23 super(ConverterPy, self).__init__(infile)
23 super(ConverterPy, self).__init__(infile)
24 self.show_prompts = show_prompts
24 self.show_prompts = show_prompts
25 self.show_output = show_output
25 self.show_output = show_output
26
26
27 @staticmethod
27 @staticmethod
28 def comment(input):
28 def comment(input):
29 "returns every line in input as commented out"
29 "returns every line in input as commented out"
30 return "# "+input.replace("\n", "\n# ")
30 return "# "+input.replace("\n", "\n# ")
31
31
32 def render_heading(self, cell):
32 def render_heading(self, cell):
33 return ['#{0} {1}'.format('#'*cell.level, cell.source), '']
33 return ['#{0} {1}'.format('#'*cell.level, cell.source), '']
34
34
35 def render_code(self, cell):
35 def render_code(self, cell):
36 try:
37 prompt_number = cell.prompt_number
38 except AttributeError:
39 prompt_number = " "
40
36 if not cell.input:
41 if not cell.input:
37 return []
42 return []
38 lines = []
43 lines = []
39 if self.show_prompts:
44 if self.show_prompts:
40 lines.extend(['# In[%s]:' % cell.prompt_number])
45 lines.extend(['# In[%s]:' % prompt_number])
41 src = cell.input
46 src = cell.input
42 lines.extend([src, ''])
47 lines.extend([src, ''])
43 if self.show_output:
48 if self.show_output:
44 if cell.outputs :
49 if cell.outputs :
45 lines.extend(['# Out[%s]:' % cell.prompt_number])
50 lines.extend(['# Out[%s]:' % prompt_number])
46 for output in cell.outputs:
51 for output in cell.outputs:
47 conv_fn = self.dispatch(output.output_type)
52 conv_fn = self.dispatch(output.output_type)
48 lines.extend(conv_fn(output))
53 lines.extend(conv_fn(output))
49 return lines
54 return lines
50
55
51 def render_markdown(self, cell):
56 def render_markdown(self, cell):
52 return [self.comment(cell.source), '']
57 return [self.comment(cell.source), '']
53
58
54 def render_raw(self, cell):
59 def render_raw(self, cell):
55 if self.raw_as_verbatim:
60 if self.raw_as_verbatim:
56 return [self.comment(indent(cell.source)), '']
61 return [self.comment(indent(cell.source)), '']
57 else:
62 else:
58 return [self.comment(cell.source), '']
63 return [self.comment(cell.source), '']
59
64
60 def render_pyout(self, output):
65 def render_pyout(self, output):
61 lines = []
66 lines = []
62
67
63 ## if 'text' in output:
68 ## if 'text' in output:
64 ## lines.extend(['*Out[%s]:*' % output.prompt_number, ''])
69 ## lines.extend(['*Out[%s]:*' % output.prompt_number, ''])
65
70
66 # output is a dictionary like object with type as a key
71 # output is a dictionary like object with type as a key
67 if 'latex' in output:
72 if 'latex' in output:
68 pass
73 pass
69
74
70 if 'text' in output:
75 if 'text' in output:
71 lines.extend([self.comment(indent(output.text)), ''])
76 lines.extend([self.comment(indent(output.text)), ''])
72
77
73 lines.append('')
78 lines.append('')
74 return lines
79 return lines
75
80
76 def render_pyerr(self, output):
81 def render_pyerr(self, output):
77 # Note: a traceback is a *list* of frames.
82 # Note: a traceback is a *list* of frames.
78 return [indent(remove_ansi('\n'.join(output.traceback))), '']
83 return [indent(remove_ansi('\n'.join(output.traceback))), '']
79
84
80 def _img_lines(self, img_file):
85 def _img_lines(self, img_file):
81 return [ self.comment('image file: %s' % img_file), '']
86 return [ self.comment('image file: %s' % img_file), '']
82
87
83 def render_display_format_text(self, output):
88 def render_display_format_text(self, output):
84 return [self.comment(indent(output.text))]
89 return [self.comment(indent(output.text))]
85
90
86 def _unknown_lines(self, data):
91 def _unknown_lines(self, data):
87 return [self.comment('Warning: Unknown cell'+ str(data))]
92 return [self.comment('Warning: Unknown cell'+ str(data))]
88
93
89 def render_display_format_html(self, output):
94 def render_display_format_html(self, output):
90 return [self.comment(output.html)]
95 return [self.comment(output.html)]
91
96
92 def render_display_format_latex(self, output):
97 def render_display_format_latex(self, output):
93 return []
98 return []
94
99
95 def render_display_format_json(self, output):
100 def render_display_format_json(self, output):
96 return []
101 return []
97
102
98 def render_display_format_javascript(self, output):
103 def render_display_format_javascript(self, output):
99 return []
104 return []
@@ -1,73 +1,79
1 from converters.base import Converter
1 from converters.base import Converter
2 from converters.utils import markdown2rst, rst_directive, remove_ansi
2 from converters.utils import markdown2rst, rst_directive, remove_ansi
3 from IPython.utils.text import indent
3 from IPython.utils.text import indent
4
4
5 class ConverterRST(Converter):
5 class ConverterRST(Converter):
6 extension = 'rst'
6 extension = 'rst'
7 heading_level = {1: '=', 2: '-', 3: '`', 4: '\'', 5: '.', 6: '~'}
7 heading_level = {1: '=', 2: '-', 3: '`', 4: '\'', 5: '.', 6: '~'}
8
8
9 def render_heading(self, cell):
9 def render_heading(self, cell):
10 marker = self.heading_level[cell.level]
10 marker = self.heading_level[cell.level]
11 return ['{0}\n{1}\n'.format(cell.source, marker * len(cell.source))]
11 return ['{0}\n{1}\n'.format(cell.source, marker * len(cell.source))]
12
12
13 def render_code(self, cell):
13 def render_code(self, cell):
14 # Note: cell has type 'IPython.nbformat.v3.nbbase.NotebookNode'
14 if not cell.input:
15 if not cell.input:
15 return []
16 return []
16
17
17 lines = ['In[%s]:' % cell.prompt_number, '']
18 try:
19 prompt_number = cell.prompt_number
20 except AttributeError:
21 prompt_number = " "
22
23 lines = ['In[%s]:' % prompt_number, '']
18 lines.extend(rst_directive('.. code:: python', cell.input))
24 lines.extend(rst_directive('.. code:: python', cell.input))
19
25
20 for output in cell.outputs:
26 for output in cell.outputs:
21 conv_fn = self.dispatch(output.output_type)
27 conv_fn = self.dispatch(output.output_type)
22 lines.extend(conv_fn(output))
28 lines.extend(conv_fn(output))
23
29
24 return lines
30 return lines
25
31
26 def render_markdown(self, cell):
32 def render_markdown(self, cell):
27 #return [cell.source]
33 #return [cell.source]
28 return [markdown2rst(cell.source)]
34 return [markdown2rst(cell.source)]
29
35
30 def render_raw(self, cell):
36 def render_raw(self, cell):
31 if self.raw_as_verbatim:
37 if self.raw_as_verbatim:
32 return ['::', '', indent(cell.source), '']
38 return ['::', '', indent(cell.source), '']
33 else:
39 else:
34 return [cell.source]
40 return [cell.source]
35
41
36 def render_pyout(self, output):
42 def render_pyout(self, output):
37 lines = ['Out[%s]:' % output.prompt_number, '']
43 lines = ['Out[%s]:' % output.prompt_number, '']
38
44
39 # output is a dictionary like object with type as a key
45 # output is a dictionary like object with type as a key
40 if 'latex' in output:
46 if 'latex' in output:
41 lines.extend(rst_directive('.. math::', output.latex))
47 lines.extend(rst_directive('.. math::', output.latex))
42
48
43 if 'text' in output:
49 if 'text' in output:
44 lines.extend(rst_directive('.. parsed-literal::', output.text))
50 lines.extend(rst_directive('.. parsed-literal::', output.text))
45
51
46 return lines
52 return lines
47
53
48 def render_pyerr(self, output):
54 def render_pyerr(self, output):
49 # Note: a traceback is a *list* of frames.
55 # Note: a traceback is a *list* of frames.
50 return ['::', '', indent(remove_ansi('\n'.join(output.traceback))), '']
56 return ['::', '', indent(remove_ansi('\n'.join(output.traceback))), '']
51
57
52 def _img_lines(self, img_file):
58 def _img_lines(self, img_file):
53 return ['.. image:: %s' % img_file, '']
59 return ['.. image:: %s' % img_file, '']
54
60
55 def render_display_format_text(self, output):
61 def render_display_format_text(self, output):
56 return rst_directive('.. parsed-literal::', output.text)
62 return rst_directive('.. parsed-literal::', output.text)
57
63
58 def _unknown_lines(self, data):
64 def _unknown_lines(self, data):
59 return rst_directive('.. warning:: Unknown cell') + [data]
65 return rst_directive('.. warning:: Unknown cell') + [data]
60
66
61 def render_display_format_html(self, output):
67 def render_display_format_html(self, output):
62 return rst_directive('.. raw:: html', output.html)
68 return rst_directive('.. raw:: html', output.html)
63
69
64 def render_display_format_latex(self, output):
70 def render_display_format_latex(self, output):
65 return rst_directive('.. math::', output.latex)
71 return rst_directive('.. math::', output.latex)
66
72
67 def render_display_format_json(self, output):
73 def render_display_format_json(self, output):
68 return rst_directive('.. raw:: json', output.json)
74 return rst_directive('.. raw:: json', output.json)
69
75
70
76
71 def render_display_format_javascript(self, output):
77 def render_display_format_javascript(self, output):
72 return rst_directive('.. raw:: javascript', output.javascript)
78 return rst_directive('.. raw:: javascript', output.javascript)
73
79
General Comments 0
You need to be logged in to leave comments. Login now