latex.py
126 lines
| 5.4 KiB
| text/x-python
|
PythonLexer
Jonathan Frederic
|
r10485 | """Latex exporter for the notebook conversion pipeline. | |
Jonathan Frederic
|
r10479 | ||
This module defines Exporter, a highly configurable converter | |||
that uses Jinja2 to export notebook files into different format. | |||
You can register both pre-transformers that will act on the notebook format | |||
Jonathan Frederic
|
r10578 | before conversion and jinja filter that would then be available in the templates | |
Jonathan Frederic
|
r10479 | """ | |
#----------------------------------------------------------------------------- | |||
# Copyright (c) 2013, 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 | |||
#----------------------------------------------------------------------------- | |||
Jonathan Frederic
|
r10485 | import base.Exporter as Exporter | |
Jonathan Frederic
|
r10479 | ||
#Try to import the Sphinx exporter. If the user doesn't have Sphinx isntalled | |||
#on his/her machine, fail silently. | |||
try: | |||
from .sphinx_transformer import (SphinxTransformer) #TODO | |||
except ImportError: | |||
SphinxTransformer = None | |||
#----------------------------------------------------------------------------- | |||
# Globals and constants | |||
#----------------------------------------------------------------------------- | |||
#Latex Jinja2 constants | |||
LATEX_TEMPLATE_PATH = "/../templates/tex/" | |||
LATEX_TEMPLATE_SKELETON_PATH = "/../templates/tex/skeleton/" | |||
LATEX_TEMPLATE_EXTENSION = ".tplx" | |||
#Special Jinja2 syntax that will not conflict when exporting latex. | |||
LATEX_JINJA_COMMENT_BLOCK = ["((=", "=))"] | |||
LATEX_JINJA_VARIABLE_BLOCK = ["(((", ")))"] | |||
LATEX_JINJA_LOGIC_BLOCK = ["((*", "*))"] | |||
#----------------------------------------------------------------------------- | |||
# Classes and functions | |||
#----------------------------------------------------------------------------- | |||
Jonathan Frederic
|
r10485 | class LatexExporter(Exporter): | |
Jonathan Frederic
|
r10479 | """ A Jinja2 base converter templates | |
Preprocess the ipynb files, feed it throug jinja templates, | |||
and spit an converted files and a data object with other data | |||
should be mostly configurable | |||
""" | |||
#Processors that process the input data prior to the export, set in the | |||
#constructor for this class. | |||
preprocessors = [] | |||
def __init__(self, preprocessors={}, jinja_filters={}, config=None, export_format, **kw): | |||
""" Init a new converter. | |||
config: the Configurable config object to pass around. | |||
Jonathan Frederic
|
r10578 | preprocessors: dict of **available** key/value function to run on | |
Jonathan Frederic
|
r10479 | ipynb json data before conversion to extract/inline file. | |
See `transformer.py` and `ConfigurableTransformers` | |||
set the order in which the transformers should apply | |||
with the `pre_transformer_order` trait of this class | |||
transformers registerd by this key will take precedence on | |||
default one. | |||
jinja_filters: dict of supplementary jinja filter that should be made | |||
Jonathan Frederic
|
r10578 | available in template. If those are of Configurable Class type, | |
Jonathan Frederic
|
r10479 | they will be instanciated with the config object as argument. | |
Jonathan Frederic
|
r10578 | user defined filter will overwrite the one available by default. | |
Jonathan Frederic
|
r10479 | """ | |
#Call the base class constructor | |||
super(Exporter, self).__init__(config=config, **kw) | |||
#For compatibility, TODO: remove later. | |||
self.preprocessors.append(trans.coalesce_streams) | |||
self.preprocessors.append(trans.ExtractFigureTransformer(config=config)) | |||
self.preprocessors.append(trans.RevealHelpTransformer(config=config)) | |||
self.preprocessors.append(trans.CSSHtmlHeaderTransformer(config=config)) | |||
self.preprocessors.append(LatexTransformer(config=config)) | |||
#Only load the sphinx transformer if the file reference worked | |||
#(Sphinx dependencies exist on the user's machine.) | |||
if SphinxTransformer: | |||
self.preprocessors.append(SphinxTransformer(config=config)) | |||
#Add filters to the Jinja2 environment | |||
Jonathan Frederic
|
r10485 | self.env.filters['escape_tex'] = filters.latex.escape_tex | |
self.env.filters['highlight'] = filters.pygments.highlight2latex | |||
Jonathan Frederic
|
r10479 | #Load user filters. Overwrite existing filters if need be. | |
for key, user_filter in jinja_filters.iteritems(): | |||
if isinstance(user_filter, MetaHasTraits): | |||
self.env.filters[key] = user_filter(config=config) | |||
else: | |||
self.env.filters[key] = user_filter | |||
#Load the template file. | |||
self.template = self.env.get_template(self.template_file+self.ext) | |||
Jonathan Frederic
|
r10578 | ||
def _init_environment(self): | |||
self.ext = LATEX_TEMPLATE_EXTENSION | |||
self.env = Environment( | |||
loader=FileSystemLoader([ | |||
os.path.dirname(os.path.realpath(__file__)) + LATEX_TEMPLATE_PATH, | |||
os.path.dirname(os.path.realpath(__file__)) + LATEX_TEMPLATE_SKELETON_PATH, | |||
]), | |||
extensions=JINJA_EXTENSIONS | |||
) | |||
#Set special Jinja2 syntax that will not conflict with latex. | |||
self.env.block_start_string = LATEX_JINJA_LOGIC_BLOCK[0] | |||
self.env.block_end_string = LATEX_JINJA_LOGIC_BLOCK[1] | |||
self.env.variable_start_string = LATEX_JINJA_VARIABLE_BLOCK[0] | |||
self.env.variable_end_string = LATEX_JINJA_VARIABLE_BLOCK[1] | |||
self.env.comment_start_string = LATEX_JINJA_COMMENT_BLOCK[0] | |||
self.env.comment_end_string = LATEX_JINJA_COMMENT_BLOCK[1] |