diff --git a/converters/template.py b/converters/template.py index 023c3f4..3a87bbb 100755 --- a/converters/template.py +++ b/converters/template.py @@ -18,17 +18,11 @@ a conversion of IPython notebooks to some other format should inherit. from __future__ import print_function, absolute_import # Stdlib imports -import jinja2 -import codecs import io -import logging import os from IPython.utils import path -import pprint -import re -from types import FunctionType -from jinja2 import Environment, PackageLoader, FileSystemLoader +from jinja2 import Environment, FileSystemLoader env = Environment( loader=FileSystemLoader('./templates/'), extensions=['jinja2.ext.loopcontrols'] @@ -36,15 +30,14 @@ env = Environment( # IPython imports from IPython.nbformat import current as nbformat -from IPython.config.configurable import Configurable, SingletonConfigurable -from IPython.utils.traitlets import (List, Unicode, Type, Bool, Dict, CaselessStrEnum, - Any) +from IPython.config.configurable import Configurable +from IPython.utils.traitlets import ( Unicode, Any) # Our own imports from IPython.utils.text import indent from .utils import remove_ansi from markdown import markdown -from .utils import highlight,ansi2html +from .utils import highlight, ansi2html #----------------------------------------------------------------------------- # Class declarations #----------------------------------------------------------------------------- @@ -61,43 +54,43 @@ def python_comment(string): def header_body(): - """Return the body of the header as a list of strings.""" - - from pygments.formatters import HtmlFormatter - - header = [] - static = os.path.join(path.get_ipython_package_dir(), - 'frontend', 'html', 'notebook', 'static', - ) - here = os.path.split(os.path.realpath(__file__))[0] - css = os.path.join(static, 'css') - for sheet in [ - # do we need jquery and prettify? - # os.path.join(static, 'jquery', 'css', 'themes', 'base', - # 'jquery-ui.min.css'), - # os.path.join(static, 'prettify', 'prettify.css'), - os.path.join(css, 'boilerplate.css'), - os.path.join(css, 'fbm.css'), - os.path.join(css, 'notebook.css'), - os.path.join(css, 'renderedhtml.css'), - # our overrides: - os.path.join(here, '..', 'css', 'static_html.css'), - ]: - - with io.open(sheet, encoding='utf-8') as f: - s = f.read() - header.append(s) - - pygments_css = HtmlFormatter().get_style_defs('.highlight') - header.append(pygments_css) - return header - -inlining= {} + """Return the body of the header as a list of strings.""" + + from pygments.formatters import HtmlFormatter + + header = [] + static = os.path.join(path.get_ipython_package_dir(), + 'frontend', 'html', 'notebook', 'static', + ) + here = os.path.split(os.path.realpath(__file__))[0] + css = os.path.join(static, 'css') + for sheet in [ + # do we need jquery and prettify? + # os.path.join(static, 'jquery', 'css', 'themes', 'base', + # 'jquery-ui.min.css'), + # os.path.join(static, 'prettify', 'prettify.css'), + os.path.join(css, 'boilerplate.css'), + os.path.join(css, 'fbm.css'), + os.path.join(css, 'notebook.css'), + os.path.join(css, 'renderedhtml.css'), + # our overrides: + os.path.join(here, '..', 'css', 'static_html.css'), + ]: + + with io.open(sheet, encoding='utf-8') as f: + s = f.read() + header.append(s) + + pygments_css = HtmlFormatter().get_style_defs('.highlight') + header.append(pygments_css) + return header + +inlining = {} inlining['css'] = header_body() def filter_data_type(output): - for fmt in ['html', 'pdf', 'svg', 'latex','png', 'jpg','jpeg' , 'text']: + for fmt in ['html', 'pdf', 'svg', 'latex', 'png', 'jpg', 'jpeg' , 'text']: if fmt in output: return [fmt] @@ -125,12 +118,8 @@ class ConverterTemplate(Configurable): def __init__(self, tplfile='fullhtml', config=None, **kw): self.template = env.get_template(tplfile+'.tpl') - super(ConverterTemplate,self).__init__(config=config) - - def _get_prompt_number(self, cell): - return cell.prompt_number if hasattr(cell, 'prompt_number') \ - else self.blank_symbol - + self.nb = None + super(ConverterTemplate, self).__init__(config=config, **kw) def process(self): converted_cells = [] @@ -138,7 +127,7 @@ class ConverterTemplate(Configurable): for cell in worksheet.cells: cell.type = cell.cell_type cell.haspyout = False - for out in cell.get('outputs',[]): + for out in cell.get('outputs', []): if out.output_type == 'pyout': cell.haspyout = True break @@ -146,13 +135,13 @@ class ConverterTemplate(Configurable): return converted_cells - def convert(self, cell_separator='\n'): + def convert(self): """ convert the ipynb file return both the converted ipynb file and a dict containing potential other resources """ - return self.template.render(worksheets=self.process(), inlining=inlining),{} + return self.template.render(worksheets=self.process(), inlining=inlining), {} def read(self, filename): diff --git a/templates/python.tpl b/templates/python.tpl deleted file mode 100644 index 49eaa43..0000000 --- a/templates/python.tpl +++ /dev/null @@ -1,56 +0,0 @@ -{%- extends 'null.tpl' -%} - -{% block in_prompt %} -# In[{{cell.prompt_number if cell.prompt_number else ' '}}]: -{% endblock in_prompt %} - -{% block output_prompt %} -# Out[{{cell.prompt_number}}]:{% endblock output_prompt %} - -{% block input %}{{ cell.input }} -{% endblock input %} - - -{# Those Two are for error displaying -even if the first one seem to do nothing, -it introduces a new line - -#} -{% block pyerr %}{{ super() }} -{% endblock pyerr %} - -{% block traceback_line %} -{{ line |indent| rm_ansi }}{% endblock traceback_line %} -{# .... #} - - -{% block pyout %} -{{ output.text| indent | pycomment}} -{% endblock pyout %} - -{% block stream %} -{{ output.text| indent | pycomment}} -{% endblock stream %} - - - - -{% block display_data scoped %} -# image file: -{% endblock display_data %} - -{% block markdowncell scoped %} -{{ cell.source | pycomment }} -{% endblock markdowncell %} - -{% block headingcell scoped %} -{{ '#' * cell.level }}{{ cell.source | pycomment}} -{% endblock headingcell %} - -{% block rawcell scoped %} -{{ cell.source | pycomment }} -{% endblock rawcell %} - -{% block unknowncell scoped %} -unknown type {{cell.type}} -{% endblock unknowncell %}