##// END OF EJS Templates
some other fixes
some other fixes

File last commit:

r9014:1423a345
r9016:cebd3cda
Show More
template.py
102 lines | 3.2 KiB | text/x-python | PythonLexer
Matthias BUSSONNIER
starting templates
r8994 """Base classes for the notebook conversion pipeline.
This module defines Converter, from which all objects designed to implement
a conversion of IPython notebooks to some other format should inherit.
"""
#-----------------------------------------------------------------------------
# Copyright (c) 2012, the IPython Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
from __future__ import print_function, absolute_import
# Stdlib imports
import jinja2
import codecs
import io
import logging
import os
import pprint
import re
from types import FunctionType
from jinja2 import Environment, PackageLoader, FileSystemLoader
env = Environment(loader=FileSystemLoader('./templates/'))
# 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)
# Our own imports
from IPython.utils.text import indent
from .utils import remove_ansi
Matthias BUSSONNIER
null template
r8997 from markdown import markdown
from .utils import highlight
Matthias BUSSONNIER
starting templates
r8994 #-----------------------------------------------------------------------------
# Class declarations
#-----------------------------------------------------------------------------
def rm_fake(strng):
return strng.replace('/files/', '')
class ConversionException(Exception):
pass
def python_comment(string):
return '# '+'\n# '.join(string.split('\n'))
env.filters['pycomment'] = python_comment
env.filters['indent'] = indent
env.filters['rm_fake'] = rm_fake
env.filters['rm_ansi'] = remove_ansi
Matthias BUSSONNIER
null template
r8997 env.filters['markdown'] = markdown
env.filters['highlight'] = highlight
Matthias BUSSONNIER
starting templates
r8994
class ConverterTemplate(Configurable):
display_data_priority = ['pdf', 'svg', 'png', 'jpg', 'text']
#-------------------------------------------------------------------------
# Instance-level attributes that are set in the constructor for this
# class.
#-------------------------------------------------------------------------
infile = Any()
Matthias BUSSONNIER
deal with worksheets
r9010
Matthias BUSSONNIER
starting templates
r8994 infile_dir = Unicode()
Matthias BUSSONNIER
null template
r8997 def __init__(self, tplfile='fullhtml', config=None, **kw):
self.template = env.get_template(tplfile+'.tpl')
Matthias BUSSONNIER
starting templates
r8994 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
def process(self):
converted_cells = []
for worksheet in self.nb.worksheets:
for cell in worksheet.cells:
cell.type = cell.cell_type
Matthias BUSSONNIER
deal with worksheets
r9010 converted_cells.append(worksheet)
Matthias BUSSONNIER
starting templates
r8994
return converted_cells
def convert(self, cell_separator='\n'):
Matthias BUSSONNIER
deal with worksheets
r9010 return self.template.render(worksheets=self.process())
Matthias BUSSONNIER
starting templates
r8994
def read(self, filename):
"read and parse notebook into NotebookNode called self.nb"
Matthias BUSSONNIER
fix utf8
r9014 with io.open(filename) as f:
Matthias BUSSONNIER
starting templates
r8994 self.nb = nbformat.read(f, 'json')