support IPython 2.0 in notebook liquid tag

The only relevant changes seem to be a few renamed classes, and changed signatures.
This commit is contained in:
MinRK
2014-03-09 11:00:14 -07:00
parent dc96679d03
commit db4683c515

View File

@@ -55,7 +55,12 @@ if not LooseVersion(IPython.__version__) >= '1.0':
from IPython import nbconvert
from IPython.nbconvert.filters.highlight import _pygment_highlight
try:
from IPython.nbconvert.filters.highlight import _pygments_highlight
except ImportError:
# IPython < 2.0
from IPython.nbconvert.filters.highlight import _pygment_highlight as _pygments_highlight
from pygments.formatters import HtmlFormatter
from IPython.nbconvert.exporters import HTMLExporter
@@ -64,9 +69,10 @@ from IPython.config import Config
from IPython.nbformat import current as nbformat
try:
from IPython.nbconvert.transformers import Transformer
from IPython.nbconvert.preprocessors import Preprocessor
except ImportError:
raise ValueError("IPython version 2.0 is not yet supported")
# IPython < 2.0
from IPython.nbconvert.transformers import Transformer as Preprocessor
from IPython.utils.traitlets import Integer
from copy import deepcopy
@@ -144,7 +150,7 @@ CSS_WRAPPER = """
#----------------------------------------------------------------------
# Create a custom transformer
# Create a custom preprocessor
class SliceIndex(Integer):
"""An integer trait that accepts None"""
default_value = None
@@ -156,20 +162,22 @@ class SliceIndex(Integer):
return super(SliceIndex, self).validate(obj, value)
class SubCell(Transformer):
class SubCell(Preprocessor):
"""A transformer to select a slice of the cells of a notebook"""
start = SliceIndex(0, config=True,
help="first cell of notebook to be converted")
end = SliceIndex(None, config=True,
help="last cell of notebook to be converted")
def call(self, nb, resources):
def preprocess(self, nb, resources):
nbc = deepcopy(nb)
for worksheet in nbc.worksheets :
for worksheet in nbc.worksheets:
cells = worksheet.cells[:]
worksheet.cells = cells[self.start:self.end]
return nbc, resources
call = preprocess # IPython < 2.0
#----------------------------------------------------------------------
# Customize the html template:
@@ -205,9 +213,11 @@ pelican_loader = DictLoader({'pelicanhtml.tpl':
#----------------------------------------------------------------------
# Custom highlighter:
# instead of using class='highlight', use class='highlight-ipynb'
def custom_highlighter(source, language='ipython'):
def custom_highlighter(source, language='ipython', metadata=None):
formatter = HtmlFormatter(cssclass='highlight-ipynb')
output = _pygment_highlight(source, formatter, language)
if not language:
language = 'ipython'
output = _pygments_highlight(source, formatter, language)
return output.replace('<pre>', '<pre class="ipynb">')
@@ -252,12 +262,17 @@ def notebook(preprocessor, tag, markup):
{'enabled':True, 'highlight_class':'.highlight-ipynb'},
'SubCell':
{'enabled':True, 'start':start, 'end':end}})
if LooseVersion(IPython.__version__) >= '2.0':
subcell_kwarg = dict(preprocessors=[SubCell])
else:
subcell_kwarg = dict(transformers=[SubCell])
exporter = HTMLExporter(config=c,
template_file='basic',
filters={'highlight2html': custom_highlighter},
transformers=[SubCell],
extra_loaders=[pelican_loader])
extra_loaders=[pelican_loader],
**subcell_kwarg)
# read and parse the notebook
with open(nb_path) as f: