##// END OF EJS Templates
Replace ad-hoc/broken code with safe method to extract the prompt_number of a cell....
Maximilian Albert -
Show More
@@ -63,6 +63,7 b' class Converter(object):'
63 user_preamble = None
63 user_preamble = None
64 output = unicode()
64 output = unicode()
65 raw_as_verbatim = False
65 raw_as_verbatim = False
66 blank_symbol = " "
66
67
67 def __init__(self, infile):
68 def __init__(self, infile):
68 self.infile = infile
69 self.infile = infile
@@ -79,6 +80,10 b' class Converter(object):'
79 if os.path.isdir(self.files_dir) and not os.listdir(self.files_dir):
80 if os.path.isdir(self.files_dir) and not os.listdir(self.files_dir):
80 os.rmdir(self.files_dir)
81 os.rmdir(self.files_dir)
81
82
83 def _get_prompt_number(self, cell):
84 return cell.prompt_number if hasattr(cell, 'prompt_number') \
85 else self.blank_symbol
86
82 def dispatch(self, cell_type):
87 def dispatch(self, cell_type):
83 """return cell_type dependent render method, for example render_code
88 """return cell_type dependent render method, for example render_code
84 """
89 """
@@ -11,6 +11,7 b' import io'
11
11
12 class ConverterHTML(Converter):
12 class ConverterHTML(Converter):
13 extension = 'html'
13 extension = 'html'
14 blank_symbol = ' '
14
15
15 def in_tag(self, tag, src, attrs=None):
16 def in_tag(self, tag, src, attrs=None):
16 """Return a list of elements bracketed by the given tag"""
17 """Return a list of elements bracketed by the given tag"""
@@ -29,8 +30,7 b' class ConverterHTML(Converter):'
29
30
30 def _out_prompt(self, output):
31 def _out_prompt(self, output):
31 if output.output_type == 'pyout':
32 if output.output_type == 'pyout':
32 n = output.prompt_number if output.prompt_number is not None else ' '
33 content = 'Out[%s]:' % self._get_prompt_number(output)
33 content = 'Out[%s]:' % n
34 else:
34 else:
35 content = ''
35 content = ''
36 return ['<div class="prompt output_prompt">%s</div>' % content]
36 return ['<div class="prompt output_prompt">%s</div>' % content]
@@ -93,7 +93,7 b' class ConverterHTML(Converter):'
93 lines = ['<div class="cell border-box-sizing code_cell vbox">']
93 lines = ['<div class="cell border-box-sizing code_cell vbox">']
94
94
95 lines.append('<div class="input hbox">')
95 lines.append('<div class="input hbox">')
96 n = cell.prompt_number if getattr(cell, 'prompt_number', None) is not None else '&nbsp;'
96 n = self._get_prompt_number(cell)
97 lines.append('<div class="prompt input_prompt">In&nbsp;[%s]:</div>' % n)
97 lines.append('<div class="prompt input_prompt">In&nbsp;[%s]:</div>' % n)
98 lines.append('<div class="input_area box-flex1">')
98 lines.append('<div class="input_area box-flex1">')
99 lines.append(highlight(cell.input))
99 lines.append(highlight(cell.input))
@@ -19,10 +19,11 b' class ConverterMarkdown(Converter):'
19 if not cell.input:
19 if not cell.input:
20 return []
20 return []
21 lines = []
21 lines = []
22 n = self._get_prompt_number(cell)
22 if self.show_prompts and not self.inline_prompt:
23 if self.show_prompts and not self.inline_prompt:
23 lines.extend(['*In[%s]:*' % cell.prompt_number, ''])
24 lines.extend(['*In[%s]:*' % n, ''])
24 if self.show_prompts and self.inline_prompt:
25 if self.show_prompts and self.inline_prompt:
25 prompt = 'In[%s]: ' % cell.prompt_number
26 prompt = 'In[%s]: ' % n
26 input_lines = cell.input.split('\n')
27 input_lines = cell.input.split('\n')
27 src = prompt + input_lines[0] + '\n' + indent('\n'.join(input_lines[1:]), nspaces=len(prompt))
28 src = prompt + input_lines[0] + '\n' + indent('\n'.join(input_lines[1:]), nspaces=len(prompt))
28 else:
29 else:
@@ -30,7 +31,7 b' class ConverterMarkdown(Converter):'
30 src = highlight(src) if self.highlight_source else indent(src)
31 src = highlight(src) if self.highlight_source else indent(src)
31 lines.extend([src, ''])
32 lines.extend([src, ''])
32 if cell.outputs and self.show_prompts and not self.inline_prompt:
33 if cell.outputs and self.show_prompts and not self.inline_prompt:
33 lines.extend(['*Out[%s]:*' % cell.prompt_number, ''])
34 lines.extend(['*Out[%s]:*' % n, ''])
34 for output in cell.outputs:
35 for output in cell.outputs:
35 conv_fn = self.dispatch(output.output_type)
36 conv_fn = self.dispatch(output.output_type)
36 lines.extend(conv_fn(output))
37 lines.extend(conv_fn(output))
@@ -52,7 +53,7 b' class ConverterMarkdown(Converter):'
52 lines = []
53 lines = []
53
54
54 ## if 'text' in output:
55 ## if 'text' in output:
55 ## lines.extend(['*Out[%s]:*' % output.prompt_number, ''])
56 ## lines.extend(['*Out[%s]:*' % self._get_prompt_number(cell), ''])
56
57
57 # output is a dictionary like object with type as a key
58 # output is a dictionary like object with type as a key
58 if 'latex' in output:
59 if 'latex' in output:
@@ -33,21 +33,17 b' class ConverterPy(Converter):'
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:
36 n = self._get_prompt_number(cell)
37 prompt_number = cell.prompt_number
38 except AttributeError:
39 prompt_number = " "
40
41 if not cell.input:
37 if not cell.input:
42 return []
38 return []
43 lines = []
39 lines = []
44 if self.show_prompts:
40 if self.show_prompts:
45 lines.extend(['# In[%s]:' % prompt_number])
41 lines.extend(['# In[%s]:' % n])
46 src = cell.input
42 src = cell.input
47 lines.extend([src, ''])
43 lines.extend([src, ''])
48 if self.show_output:
44 if self.show_output:
49 if cell.outputs :
45 if cell.outputs :
50 lines.extend(['# Out[%s]:' % prompt_number])
46 lines.extend(['# Out[%s]:' % n])
51 for output in cell.outputs:
47 for output in cell.outputs:
52 conv_fn = self.dispatch(output.output_type)
48 conv_fn = self.dispatch(output.output_type)
53 lines.extend(conv_fn(output))
49 lines.extend(conv_fn(output))
@@ -66,7 +62,7 b' class ConverterPy(Converter):'
66 lines = []
62 lines = []
67
63
68 ## if 'text' in output:
64 ## if 'text' in output:
69 ## lines.extend(['*Out[%s]:*' % output.prompt_number, ''])
65 ## lines.extend(['*Out[%s]:*' % self._get_prompt_number(cell), ''])
70
66
71 # output is a dictionary like object with type as a key
67 # output is a dictionary like object with type as a key
72 if 'latex' in output:
68 if 'latex' in output:
@@ -15,12 +15,7 b' class ConverterRST(Converter):'
15 if not cell.input:
15 if not cell.input:
16 return []
16 return []
17
17
18 try:
18 lines = ['In[%s]:' % self._get_prompt_number(cell), '']
19 prompt_number = cell.prompt_number
20 except AttributeError:
21 prompt_number = " "
22
23 lines = ['In[%s]:' % prompt_number, '']
24 lines.extend(rst_directive('.. code:: python', cell.input))
19 lines.extend(rst_directive('.. code:: python', cell.input))
25
20
26 for output in cell.outputs:
21 for output in cell.outputs:
@@ -40,7 +35,7 b' class ConverterRST(Converter):'
40 return [cell.source]
35 return [cell.source]
41
36
42 def render_pyout(self, output):
37 def render_pyout(self, output):
43 lines = ['Out[%s]:' % output.prompt_number, '']
38 lines = ['Out[%s]:' % self._get_prompt_number(output), '']
44
39
45 # output is a dictionary like object with type as a key
40 # output is a dictionary like object with type as a key
46 if 'latex' in output:
41 if 'latex' in output:
General Comments 0
You need to be logged in to leave comments. Login now