##// END OF EJS Templates
Include empty lines condition in PromptStipper and cell_magic.
Tony Fast -
Show More
@@ -20,11 +20,11 b" _indent_re = re.compile(r'^[ \\t]+')"
20
20
21 def leading_indent(lines):
21 def leading_indent(lines):
22 """Remove leading indentation.
22 """Remove leading indentation.
23
23
24 If the first line starts with a spaces or tabs, the same whitespace will be
24 If the first line starts with a spaces or tabs, the same whitespace will be
25 removed from each following line in the cell.
25 removed from each following line in the cell.
26 """
26 """
27 if not lines:
27 if not lines:
28 return lines
28 return lines
29 m = _indent_re.match(lines[0])
29 m = _indent_re.match(lines[0])
30 if not m:
30 if not m:
@@ -36,7 +36,7 b' def leading_indent(lines):'
36
36
37 class PromptStripper:
37 class PromptStripper:
38 """Remove matching input prompts from a block of input.
38 """Remove matching input prompts from a block of input.
39
39
40 Parameters
40 Parameters
41 ----------
41 ----------
42 prompt_re : regular expression
42 prompt_re : regular expression
@@ -47,7 +47,7 b' class PromptStripper:'
47 If no initial expression is given, prompt_re will be used everywhere.
47 If no initial expression is given, prompt_re will be used everywhere.
48 Used mainly for plain Python prompts (``>>>``), where the continuation prompt
48 Used mainly for plain Python prompts (``>>>``), where the continuation prompt
49 ``...`` is a valid Python expression in Python 3, so shouldn't be stripped.
49 ``...`` is a valid Python expression in Python 3, so shouldn't be stripped.
50
50
51 If initial_re and prompt_re differ,
51 If initial_re and prompt_re differ,
52 only initial_re will be tested against the first line.
52 only initial_re will be tested against the first line.
53 If any prompt is found on the first two lines,
53 If any prompt is found on the first two lines,
@@ -61,6 +61,8 b' class PromptStripper:'
61 return [self.prompt_re.sub('', l, count=1) for l in lines]
61 return [self.prompt_re.sub('', l, count=1) for l in lines]
62
62
63 def __call__(self, lines):
63 def __call__(self, lines):
64 if not lines:
65 return lines
64 if self.initial_re.match(lines[0]) or \
66 if self.initial_re.match(lines[0]) or \
65 (len(lines) > 1 and self.prompt_re.match(lines[1])):
67 (len(lines) > 1 and self.prompt_re.match(lines[1])):
66 return self._strip(lines)
68 return self._strip(lines)
@@ -74,7 +76,7 b' classic_prompt = PromptStripper('
74 ipython_prompt = PromptStripper(re.compile(r'^(In \[\d+\]: |\s*\.{3,}: ?)'))
76 ipython_prompt = PromptStripper(re.compile(r'^(In \[\d+\]: |\s*\.{3,}: ?)'))
75
77
76 def cell_magic(lines):
78 def cell_magic(lines):
77 if not lines[0].startswith('%%'):
79 if not lines or not lines[0].startswith('%%'):
78 return lines
80 return lines
79 if re.match('%%\w+\?', lines[0]):
81 if re.match('%%\w+\?', lines[0]):
80 # This case will be handled by help_end
82 # This case will be handled by help_end
@@ -94,7 +96,7 b' def _find_assign_op(token_line):'
94 for i, ti in enumerate(token_line):
96 for i, ti in enumerate(token_line):
95 s = ti.string
97 s = ti.string
96 if s == '=' and paren_level == 0:
98 if s == '=' and paren_level == 0:
97 return i
99 return i
98 if s in '([{':
100 if s in '([{':
99 paren_level += 1
101 paren_level += 1
100 elif s in ')]}':
102 elif s in ')]}':
@@ -114,7 +116,7 b' def find_end_of_continued_line(lines, start_line: int):'
114 return end_line
116 return end_line
115
117
116 def assemble_continued_line(lines, start: Tuple[int, int], end_line: int):
118 def assemble_continued_line(lines, start: Tuple[int, int], end_line: int):
117 """Assemble a single line from multiple continued line pieces
119 """Assemble a single line from multiple continued line pieces
118
120
119 Continued lines are lines ending in ``\``, and the line following the last
121 Continued lines are lines ending in ``\``, and the line following the last
120 ``\`` in the block.
122 ``\`` in the block.
@@ -204,7 +206,7 b' class MagicAssign(TokenTransformBase):'
204 and (line[assign_ix+1].string == '%') \
206 and (line[assign_ix+1].string == '%') \
205 and (line[assign_ix+2].type == tokenize.NAME):
207 and (line[assign_ix+2].type == tokenize.NAME):
206 return cls(line[assign_ix+1].start)
208 return cls(line[assign_ix+1].start)
207
209
208 def transform(self, lines: List[str]):
210 def transform(self, lines: List[str]):
209 """Transform a magic assignment found by the ``find()`` classmethod.
211 """Transform a magic assignment found by the ``find()`` classmethod.
210 """
212 """
@@ -214,12 +216,12 b' class MagicAssign(TokenTransformBase):'
214 rhs = assemble_continued_line(lines, (start_line, start_col), end_line)
216 rhs = assemble_continued_line(lines, (start_line, start_col), end_line)
215 assert rhs.startswith('%'), rhs
217 assert rhs.startswith('%'), rhs
216 magic_name, _, args = rhs[1:].partition(' ')
218 magic_name, _, args = rhs[1:].partition(' ')
217
219
218 lines_before = lines[:start_line]
220 lines_before = lines[:start_line]
219 call = "get_ipython().run_line_magic({!r}, {!r})".format(magic_name, args)
221 call = "get_ipython().run_line_magic({!r}, {!r})".format(magic_name, args)
220 new_line = lhs + call + '\n'
222 new_line = lhs + call + '\n'
221 lines_after = lines[end_line+1:]
223 lines_after = lines[end_line+1:]
222
224
223 return lines_before + [new_line] + lines_after
225 return lines_before + [new_line] + lines_after
224
226
225
227
@@ -466,7 +468,7 b' def make_tokens_by_line(lines):'
466 pass
468 pass
467 if not tokens_by_line[-1]:
469 if not tokens_by_line[-1]:
468 tokens_by_line.pop()
470 tokens_by_line.pop()
469
471
470 return tokens_by_line
472 return tokens_by_line
471
473
472 def show_linewise_tokens(s: str):
474 def show_linewise_tokens(s: str):
@@ -503,12 +505,12 b' class TransformerManager:'
503 EscapedCommand,
505 EscapedCommand,
504 HelpEnd,
506 HelpEnd,
505 ]
507 ]
506
508
507 def do_one_token_transform(self, lines):
509 def do_one_token_transform(self, lines):
508 """Find and run the transform earliest in the code.
510 """Find and run the transform earliest in the code.
509
511
510 Returns (changed, lines).
512 Returns (changed, lines).
511
513
512 This method is called repeatedly until changed is False, indicating
514 This method is called repeatedly until changed is False, indicating
513 that all available transformations are complete.
515 that all available transformations are complete.
514
516
General Comments 0
You need to be logged in to leave comments. Login now