diff --git a/liquid_tags/notebook.py b/liquid_tags/notebook.py index 8b45d5d..8c896a2 100644 --- a/liquid_tags/notebook.py +++ b/liquid_tags/notebook.py @@ -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 @@ -111,6 +117,17 @@ pre.ipynb { font-size: 13px; } +/* remove the prompt div from text cells */ +div.text_cell .prompt { + display: none; +} + +/* remove horizontal padding from text cells, */ +/* so it aligns with outer body text */ +div.text_cell_render { + padding: 0.5em 0em; +} + img.anim_icon{padding:0; border:0; vertical-align:middle; -webkit-box-shadow:none; -box-shadow:none} @@ -144,7 +161,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 +173,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 +224,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('
', '
')
 
 
@@ -252,12 +273,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: