diff --git a/liquid_tags/Readme.md b/liquid_tags/Readme.md index 2fff718..8ddca8e 100644 --- a/liquid_tags/Readme.md +++ b/liquid_tags/Readme.md @@ -107,4 +107,16 @@ are a few extra steps required for this plugin: this will insert the proper css formatting into your document. +### Collapsible Code in IPython Notebooks + +The plugin also enables collapsible code input boxes. For this to work +you first need to copy the file ``pelicanhtml_1.tpl`` (for IPython +1.x) ``pelicanhtml_2.tpl`` (for IPython 2.x) to the top level of your +Pelican blog. Notebook input cells containing the comment line ``# +`` will be collapsed when the html page is +loaded and can be expanded by clicking on them. Cells containing the +comment line ``# `` will be open on load but +can be collapsed by clicking on their header. Cells without collapse +comments are rendered as standard code input cells. + [1] http://ipython.org/ diff --git a/liquid_tags/notebook.py b/liquid_tags/notebook.py index 8c896a2..1208f1e 100644 --- a/liquid_tags/notebook.py +++ b/liquid_tags/notebook.py @@ -129,6 +129,14 @@ div.text_cell_render { } img.anim_icon{padding:0; border:0; vertical-align:middle; -webkit-box-shadow:none; -box-shadow:none} + +div.collapseheader { + width=100%; + background-color:#d3d3d3; + padding: 2px; + cursor: pointer; + font-family:"Helvetica Neue",Helvetica,Arial,sans-serif; +} @@ -151,6 +159,23 @@ init_mathjax = function() { } init_mathjax(); + + + + """ CSS_WRAPPER = """ @@ -190,36 +215,6 @@ class SubCell(Preprocessor): call = preprocess # IPython < 2.0 -#---------------------------------------------------------------------- -# Customize the html template: -# This changes the
 tags in basic_html.tpl to 
-
{{output.text |ansi2html}}
- -{%- endblock stream_stdout %} - -{% block stream_stderr -%} -
-
{{output.text |ansi2html}}
-
-{%- endblock stream_stderr %} - -{% block pyerr -%} -
-
{{super()}}
-
-{%- endblock pyerr %} - -{%- block data_text %} -
{{output.text | ansi2html}}
-{%- endblock -%} -"""}) - #---------------------------------------------------------------------- # Custom highlighter: @@ -273,16 +268,23 @@ def notebook(preprocessor, tag, markup): {'enabled':True, 'highlight_class':'.highlight-ipynb'}, 'SubCell': {'enabled':True, 'start':start, 'end':end}}) - + + template_file = 'basic' + if LooseVersion(IPython.__version__) >= '2.0': + if os.path.exists('pelicanhtml_2.tpl'): + template_file = 'pelicanhtml_2' + else: + if os.path.exists('pelicanhtml_1.tpl'): + template_file = 'pelicanhtml_1' + if LooseVersion(IPython.__version__) >= '2.0': subcell_kwarg = dict(preprocessors=[SubCell]) else: subcell_kwarg = dict(transformers=[SubCell]) exporter = HTMLExporter(config=c, - template_file='basic', + template_file=template_file, filters={'highlight2html': custom_highlighter}, - extra_loaders=[pelican_loader], **subcell_kwarg) # read and parse the notebook diff --git a/liquid_tags/pelicanhtml_1.tpl b/liquid_tags/pelicanhtml_1.tpl new file mode 100644 index 0000000..e0094f3 --- /dev/null +++ b/liquid_tags/pelicanhtml_1.tpl @@ -0,0 +1,44 @@ +{%- extends 'html_basic.tpl' -%} + +{% block stream_stdout -%} +
+
{{output.text |ansi2html}}
+
+{%- endblock stream_stdout %} + +{% block stream_stderr -%} +
+
{{output.text |ansi2html}}
+
+{%- endblock stream_stderr %} + +{% block pyerr -%} +
+
{{super()}}
+
+{%- endblock pyerr %} + +{%- block data_text %} +
{{output.text | ansi2html}}
+{%- endblock -%} + +{% block input %} +{% if "# " in cell.input %} +
Expand Code + +
+{% elif "# " in cell.input %} +
Collapse Code +
+{{ cell.input.replace("# \n", "") | highlight2html(metadata=cell.metadata) }} +
+
+{% else %} +
+{{ cell.input | highlight2html(metadata=cell.metadata) }} +
+{% endif %} +{%- endblock input %} + diff --git a/liquid_tags/pelicanhtml_2.tpl b/liquid_tags/pelicanhtml_2.tpl new file mode 100644 index 0000000..937c383 --- /dev/null +++ b/liquid_tags/pelicanhtml_2.tpl @@ -0,0 +1,44 @@ +{%- extends 'basic.tpl' -%} + +{% block stream_stdout -%} +
+
{{output.text |ansi2html}}
+
+{%- endblock stream_stdout %} + +{% block stream_stderr -%} +
+
{{output.text |ansi2html}}
+
+{%- endblock stream_stderr %} + +{% block pyerr -%} +
+
{{super()}}
+
+{%- endblock pyerr %} + +{%- block data_text %} +
{{output.text | ansi2html}}
+{%- endblock -%} + +{% block input %} +{% if "# " in cell.input %} +
Expand Code + +
+{% elif "# " in cell.input %} +
Collapse Code +
+{{ cell.input.replace("# \n", "") | highlight2html(metadata=cell.metadata) }} +
+
+{% else %} +
+{{ cell.input | highlight2html(metadata=cell.metadata) }} +
+{% endif %} +{%- endblock input %} +