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