From db4683c515898f7b6978c454e68c601f41a91f3a Mon Sep 17 00:00:00 2001 From: MinRK Date: Sun, 9 Mar 2014 11:00:14 -0700 Subject: [PATCH 1/2] support IPython 2.0 in notebook liquid tag The only relevant changes seem to be a few renamed classes, and changed signatures. --- liquid_tags/notebook.py | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/liquid_tags/notebook.py b/liquid_tags/notebook.py index 8b45d5d..f803bdf 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 @@ -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('
', '
')
 
 
@@ -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:

From 657505cf3538724f1fe5fcfda3044a822fcb4095 Mon Sep 17 00:00:00 2001
From: MinRK 
Date: Sun, 9 Mar 2014 16:51:31 -0700
Subject: [PATCH 2/2] adjust embedded notebook CSS for IPython 2.0

remove text-cell prompt and padding, so it aligns with outer body text
---
 liquid_tags/notebook.py | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/liquid_tags/notebook.py b/liquid_tags/notebook.py
index f803bdf..8c896a2 100644
--- a/liquid_tags/notebook.py
+++ b/liquid_tags/notebook.py
@@ -117,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}