Added support for replacing line magics
@@ -70,11 +70,29 @@ def strip_magics(source):
return '\n'.join(filtered)
+def replace_line_magic(source, magic, template='{line}'):
+ """
+ Given a cell's source, replace line magics using a formatting
+ template, where {line} is the string that follows the magic.
+ """
+ filtered = []
+ for line in source.splitlines():
+ if line.strip().startswith(magic):
+ substitution = template.format(line=line.replace(magic, ''))
+ filtered.append(substitution)
+ else:
+ filtered.append(line)
+ return '\n'.join(filtered)
+
+
+
class MagicConversion(Preprocessor):
def preprocess_cell(self, cell, resources, index):
if cell['cell_type'] == 'code':
source, opts_lines = filter_magic(cell['source'], '%%opts')
+ source = replace_line_magic(source, '%output',
+ template='hv.util.output({line!r})')
source = strip_magics(source)
if opts_lines:
source = wrap_cell_expression(source,
