##// END OF EJS Templates
Expand a bit the documentation about transformers....
Matthias Bussonnier -
Show More
@@ -24,7 +24,6 b' import sys'
24 import tokenize
24 import tokenize
25 import warnings
25 import warnings
26
26
27 from IPython.utils.py3compat import cast_unicode
28 from IPython.core.inputtransformer import (leading_indent,
27 from IPython.core.inputtransformer import (leading_indent,
29 classic_prompt,
28 classic_prompt,
30 ipy_prompt,
29 ipy_prompt,
@@ -687,7 +686,6 b' class IPythonInputSplitter(InputSplitter):'
687 """
686 """
688
687
689 # We must ensure all input is pure unicode
688 # We must ensure all input is pure unicode
690 lines = cast_unicode(lines, self.encoding)
691 # ''.splitlines() --> [], but we need to push the empty line to transformers
689 # ''.splitlines() --> [], but we need to push the empty line to transformers
692 lines_list = lines.splitlines()
690 lines_list = lines.splitlines()
693 if not lines_list:
691 if not lines_list:
@@ -71,6 +71,32 b' The decorator returns a factory function which will produce instances of'
71 :class:`~IPython.core.inputtransformer.StatelessInputTransformer` using your
71 :class:`~IPython.core.inputtransformer.StatelessInputTransformer` using your
72 function.
72 function.
73
73
74 Transforming a full block
75 -------------------------
76
77 Transforming a full block of python code is possible by implementing a
78 :class:`~IPython.core.inputtransformer.Inputtransformer` and overwriting the
79 ``push`` and ``reset`` methods. The reset method should send the full block of
80 transformed text. As an example a transformer the reversed the lines from last
81 to first.
82
83 from IPython.core.inputtransformer import InputTransformer
84
85 class ReverseLineTransformer(InputTransformer):
86
87 def __init__(self):
88 self.acc = []
89
90 def push(self, line):
91 self.acc.append(line)
92 return None
93
94 def reset(self):
95 ret = '\n'.join(self.acc[::-1])
96 self.acc = []
97 return ret
98
99
74 Coroutine transformers
100 Coroutine transformers
75 ----------------------
101 ----------------------
76
102
@@ -79,9 +105,28 b' sent each line in turn, followed by ``None`` to reset it. It can yield lines, or'
79 ``None`` if it is accumulating text to yield at a later point. When reset, it
105 ``None`` if it is accumulating text to yield at a later point. When reset, it
80 should give up any code it has accumulated.
106 should give up any code it has accumulated.
81
107
108 You may use :meth:`CoroutineInputTransformer.wrap` to simplify the creation of
109 such a transformer.
110
111 Here is a simple :class:`CoroutineInputTransformer` that can be though of be
112 being the identity::
113
114 @CoroutineInputTransformer.wrap
115 def noop():
116 line = ''
117 while True:
118 line = (yield line)
119
120 ip = get_ipython()
121
122 ip.input_splitter.logical_line_transforms.append(noop())
123 ip.input_transformer_manager.logical_line_transforms.append(noop())
124
82 This code in IPython strips a constant amount of leading indentation from each
125 This code in IPython strips a constant amount of leading indentation from each
83 line in a cell::
126 line in a cell::
84
127
128 from IPython.core.inputtransformer import CoroutineInputTransformer
129
85 @CoroutineInputTransformer.wrap
130 @CoroutineInputTransformer.wrap
86 def leading_indent():
131 def leading_indent():
87 """Remove leading indentation.
132 """Remove leading indentation.
General Comments 0
You need to be logged in to leave comments. Login now