Skip to content

Commit

Permalink
replace 'transformer' with 'preprocessor'
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanov committed Aug 16, 2013
1 parent 19e43d9 commit 60e0680
Show file tree
Hide file tree
Showing 29 changed files with 271 additions and 271 deletions.
2 changes: 1 addition & 1 deletion IPython/nbconvert/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

from .exporters import *
import filters
import transformers
import preprocessors
import post_processors
import writers
124 changes: 62 additions & 62 deletions IPython/nbconvert/exporters/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
from IPython.utils.text import indent
from IPython.utils import py3compat

from IPython.nbconvert import transformers as nbtransformers
from IPython.nbconvert import preprocessors as nbpreprocessors
from IPython.nbconvert import filters

#-----------------------------------------------------------------------------
Expand Down Expand Up @@ -83,8 +83,8 @@ class Exporter(LoggingConfigurable):
"""
Exports notebooks into other file formats. Uses Jinja 2 templating engine
to output new formats. Inherit from this class if you are creating a new
template type along with new filters/transformers. If the filters/
transformers provided by default suffice, there is no need to inherit from
template type along with new filters/preprocessors. If the filters/
preprocessors provided by default suffice, there is no need to inherit from
this class. Instead, override the template_file and file_extension
traits via a config file.
Expand Down Expand Up @@ -138,23 +138,23 @@ def _template_path_changed(self, name, old, new):
#Extension that the template files use.
template_extension = Unicode(".tpl", config=True)

#Configurability, allows the user to easily add filters and transformers.
transformers = List(config=True,
help="""List of transformers, by name or namespace, to enable.""")
#Configurability, allows the user to easily add filters and preprocessors.
preprocessors = List(config=True,
help="""List of preprocessors, by name or namespace, to enable.""")

filters = Dict(config=True,
help="""Dictionary of filters, by name and namespace, to add to the Jinja
environment.""")

default_transformers = List([nbtransformers.coalesce_streams,
nbtransformers.SVG2PDFTransformer,
nbtransformers.ExtractOutputTransformer,
nbtransformers.CSSHTMLHeaderTransformer,
nbtransformers.RevealHelpTransformer,
nbtransformers.LatexTransformer,
nbtransformers.SphinxTransformer],
default_preprocessors = List([nbpreprocessors.coalesce_streams,
nbpreprocessors.SVG2PDFPreprocessor,
nbpreprocessors.ExtractOutputPreprocessor,
nbpreprocessors.CSSHTMLHeaderPreprocessor,
nbpreprocessors.RevealHelpPreprocessor,
nbpreprocessors.LatexPreprocessor,
nbpreprocessors.SphinxPreprocessor],
config=True,
help="""List of transformers available by default, by name, namespace,
help="""List of preprocessors available by default, by name, namespace,
instance, or type.""")


Expand All @@ -180,7 +180,7 @@ def __init__(self, config=None, extra_loaders=None, **kw):
#Init
self._init_template()
self._init_environment(extra_loaders=extra_loaders)
self._init_transformers()
self._init_preprocessors()
self._init_filters()


Expand Down Expand Up @@ -245,13 +245,13 @@ def from_notebook_node(self, nb, resources=None, **kw):
nb : Notebook node
resources : dict (**kw)
of additional resources that can be accessed read/write by
transformers and filters.
preprocessors and filters.
"""
nb_copy = copy.deepcopy(nb)
resources = self._init_resources(resources)

# Preprocess
nb_copy, resources = self._transform(nb_copy, resources)
nb_copy, resources = self._preprocess(nb_copy, resources)

self._load_template()

Expand Down Expand Up @@ -300,51 +300,51 @@ def from_file(self, file_stream, resources=None, **kw):
return self.from_notebook_node(nbformat.read(file_stream, 'json'), resources=resources, **kw)


def register_transformer(self, transformer, enabled=False):
def register_preprocessor(self, preprocessor, enabled=False):
"""
Register a transformer.
Transformers are classes that act upon the notebook before it is
passed into the Jinja templating engine. Transformers are also
Register a preprocessor.
Preprocessors are classes that act upon the notebook before it is
passed into the Jinja templating engine. Preprocessors are also
capable of passing additional information to the Jinja
templating engine.
Parameters
----------
transformer : transformer
preprocessor : preprocessor
"""
if transformer is None:
raise TypeError('transformer')
isclass = isinstance(transformer, type)
if preprocessor is None:
raise TypeError('preprocessor')
isclass = isinstance(preprocessor, type)
constructed = not isclass

#Handle transformer's registration based on it's type
if constructed and isinstance(transformer, py3compat.string_types):
#Transformer is a string, import the namespace and recursively call
#this register_transformer method
transformer_cls = import_item(transformer)
return self.register_transformer(transformer_cls, enabled)
#Handle preprocessor's registration based on it's type
if constructed and isinstance(preprocessor, py3compat.string_types):
#Preprocessor is a string, import the namespace and recursively call
#this register_preprocessor method
preprocessor_cls = import_item(preprocessor)
return self.register_preprocessor(preprocessor_cls, enabled)

if constructed and hasattr(transformer, '__call__'):
#Transformer is a function, no need to construct it.
#Register and return the transformer.
if constructed and hasattr(preprocessor, '__call__'):
#Preprocessor is a function, no need to construct it.
#Register and return the preprocessor.
if enabled:
transformer.enabled = True
self._transformers.append(transformer)
return transformer
preprocessor.enabled = True
self._preprocessors.append(preprocessor)
return preprocessor

elif isclass and isinstance(transformer, MetaHasTraits):
#Transformer is configurable. Make sure to pass in new default for
elif isclass and isinstance(preprocessor, MetaHasTraits):
#Preprocessor is configurable. Make sure to pass in new default for
#the enabled flag if one was specified.
self.register_transformer(transformer(parent=self), enabled)
self.register_preprocessor(preprocessor(parent=self), enabled)

elif isclass:
#Transformer is not configurable, construct it
self.register_transformer(transformer(), enabled)
#Preprocessor is not configurable, construct it
self.register_preprocessor(preprocessor(), enabled)

else:
#Transformer is an instance of something without a __call__
#Preprocessor is an instance of something without a __call__
#attribute.
raise TypeError('transformer')
raise TypeError('preprocessor')


def register_filter(self, name, jinja_filter):
Expand Down Expand Up @@ -435,22 +435,22 @@ def _init_environment(self, extra_loaders=None):
self.environment.comment_end_string = self.jinja_comment_block_end


def _init_transformers(self):
def _init_preprocessors(self):
"""
Register all of the transformers needed for this exporter, disabled
Register all of the preprocessors needed for this exporter, disabled
unless specified explicitly.
"""
self._transformers = []
self._preprocessors = []

#Load default transformers (not necessarly enabled by default).
if self.default_transformers:
for transformer in self.default_transformers:
self.register_transformer(transformer)
#Load default preprocessors (not necessarly enabled by default).
if self.default_preprocessors:
for preprocessor in self.default_preprocessors:
self.register_preprocessor(preprocessor)

#Load user transformers. Enable by default.
if self.transformers:
for transformer in self.transformers:
self.register_transformer(transformer, enabled=True)
#Load user preprocessors. Enable by default.
if self.preprocessors:
for preprocessor in self.preprocessors:
self.register_preprocessor(preprocessor, enabled=True)


def _init_filters(self):
Expand Down Expand Up @@ -492,7 +492,7 @@ def _init_resources(self, resources):
return resources


def _transform(self, nb, resources):
def _preprocess(self, nb, resources):
"""
Preprocess the notebook before passing it into the Jinja engine.
To preprocess the notebook is to apply all of the
Expand All @@ -502,17 +502,17 @@ def _transform(self, nb, resources):
nb : notebook node
notebook that is being exported.
resources : a dict of additional resources that
can be accessed read/write by transformers
can be accessed read/write by preprocessors
and filters.
"""

# Do a copy.deepcopy first,
# we are never safe enough with what the transformers could do.
# we are never safe enough with what the preprocessors could do.
nbc = copy.deepcopy(nb)
resc = copy.deepcopy(resources)

#Run each transformer on the notebook. Carry the output along
#to each transformer
for transformer in self._transformers:
nbc, resc = transformer(nbc, resc)
#Run each preprocessor on the notebook. Carry the output along
#to each preprocessor
for preprocessor in self._preprocessors:
nbc, resc = preprocessor(nbc, resc)
return nbc, resc
6 changes: 3 additions & 3 deletions IPython/nbconvert/exporters/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

from IPython.utils.traitlets import Unicode, List

from IPython.nbconvert import transformers
from IPython.nbconvert import preprocessors
from IPython.config import Config

from .exporter import Exporter
Expand All @@ -29,7 +29,7 @@ class HTMLExporter(Exporter):
"""
Exports a basic HTML document. This exporter assists with the export of
HTML. Inherit from it if you are writing your own HTML template and need
custom transformers/filters. If you don't need custom transformers/
custom preprocessors/filters. If you don't need custom preprocessors/
filters, just change the 'template_file' config option.
"""

Expand All @@ -44,7 +44,7 @@ class HTMLExporter(Exporter):
@property
def default_config(self):
c = Config({
'CSSHTMLHeaderTransformer':{
'CSSHTMLHeaderPreprocessor':{
'enabled':True
}
})
Expand Down
10 changes: 5 additions & 5 deletions IPython/nbconvert/exporters/latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from IPython.utils.traitlets import Unicode, List
from IPython.config import Config

from IPython.nbconvert import filters, transformers
from IPython.nbconvert import filters, preprocessors
from .exporter import Exporter

#-----------------------------------------------------------------------------
Expand Down Expand Up @@ -74,16 +74,16 @@ def default_config(self):
'NbConvertBase': {
'display_data_priority' : ['latex', 'pdf', 'png', 'jpg', 'svg', 'jpeg', 'text']
},
'ExtractOutputTransformer': {
'ExtractOutputPreprocessor': {
'enabled':True
},
'SVG2PDFTransformer': {
'SVG2PDFPreprocessor': {
'enabled':True
},
'LatexTransformer': {
'LatexPreprocessor': {
'enabled':True
},
'SphinxTransformer': {
'SphinxPreprocessor': {
'enabled':True
}
})
Expand Down
2 changes: 1 addition & 1 deletion IPython/nbconvert/exporters/rst.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ class RSTExporter(Exporter):

@property
def default_config(self):
c = Config({'ExtractOutputTransformer':{'enabled':True}})
c = Config({'ExtractOutputPreprocessor':{'enabled':True}})
c.merge(super(RSTExporter,self).default_config)
return c
6 changes: 3 additions & 3 deletions IPython/nbconvert/exporters/slides.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

from IPython.utils.traitlets import Unicode

from IPython.nbconvert import transformers
from IPython.nbconvert import preprocessors
from IPython.config import Config

from .exporter import Exporter
Expand All @@ -41,10 +41,10 @@ class SlidesExporter(Exporter):
@property
def default_config(self):
c = Config({
'CSSHTMLHeaderTransformer':{
'CSSHTMLHeaderPreprocessor':{
'enabled':True
},
'RevealHelpTransformer':{
'RevealHelpPreprocessor':{
'enabled':True,
},
})
Expand Down
14 changes: 7 additions & 7 deletions IPython/nbconvert/exporters/tests/cheese.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
Contains CheeseTransformer
Contains CheesePreprocessor
"""
#-----------------------------------------------------------------------------
# Copyright (c) 2013, the IPython Development Team.
Expand All @@ -13,13 +13,13 @@
# Imports
#-----------------------------------------------------------------------------

from ...transformers.base import Transformer
from ...preprocessors.base import Preprocessor

#-----------------------------------------------------------------------------
# Classes
#-----------------------------------------------------------------------------

class CheeseTransformer(Transformer):
class CheesePreprocessor(Preprocessor):
"""
Adds a cheese tag to the resources object
"""
Expand All @@ -29,20 +29,20 @@ def __init__(self, **kw):
"""
Public constructor
"""
super(CheeseTransformer, self).__init__(**kw)
super(CheesePreprocessor, self).__init__(**kw)


def transform(self, nb, resources):
def preprocess(self, nb, resources):
"""
Sphinx transformation to apply on each notebook.
Sphinx preprocessing to apply on each notebook.
Parameters
----------
nb : NotebookNode
Notebook being converted
resources : dictionary
Additional resources used in the conversion process. Allows
transformers to pass variables into the Jinja engine.
preprocessors to pass variables into the Jinja engine.
"""
resources['cheese'] = 'real'
return nb, resources
Loading

0 comments on commit 60e0680

Please sign in to comment.