##// END OF EJS Templates
Cleanup and refactor of API, almost complete....
Cleanup and refactor of API, almost complete. Still need to target exporter.py and convert.py Then one last run-trough.

File last commit:

r10677:e8436603
r10677:e8436603
Show More
latex.py
116 lines | 4.5 KiB | text/x-python | PythonLexer
Jonathan Frederic
Cleanup and refactor of API, almost complete....
r10677 """
Exporter that allows Latex Jinja templates to work. Contains logic to
appropriately prepare IPYNB files for export to LaTeX. Including but
not limited to escaping LaTeX, fixing math region tags, using special
tags to circumvent Jinja/Latex syntax conflicts.
"""
Jonathan Frederic
Split exporter into base and latex.
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
Added imports
r10585
# Stdlib imports
import os
# IPython imports
Jonathan Frederic
Fixed all broken references, refactored some stuff here and there,...
r10624 from IPython.utils.traitlets import Unicode
Jonathan Frederic
Added imports
r10585
# other libs/dependencies
from jinja2 import Environment, FileSystemLoader
Jonathan Frederic
Got RST exporting to work... Sort of.
r10626 import nbconvert.filters.latex
Jonathan Frederic
Almost have nbconvert working again...
r10630 import nbconvert.filters.highlight
Jonathan Frederic
Got RST exporting to work... Sort of.
r10626 from nbconvert.transformers.latex import LatexTransformer
Jonathan Frederic
Cleanup and refactor of API, almost complete....
r10677
# local import
import exporter
Jonathan Frederic
Split exporter into base and latex.
r10479 #-----------------------------------------------------------------------------
# Globals and constants
#-----------------------------------------------------------------------------
#Latex Jinja2 constants
Jonathan Frederic
Almost have nbconvert working again...
r10630 LATEX_TEMPLATE_PATH = "/../templates/latex/"
LATEX_TEMPLATE_SKELETON_PATH = "/../templates/latex/skeleton/"
Jonathan Frederic
Split exporter into base and latex.
r10479
#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
Cleanup and refactor of API, almost complete....
r10677 class LatexExporter(exporter.Exporter):
"""
Exports to a Latex template. Inherit from this class if your template is
LaTeX based and you need custom tranformers/filters. Inherit from it if
you are writing your own HTML template and need custom tranformers/filters.
If you don't need custom tranformers/filters, just change the
'template_file' config option. Place your template in the special "/latex"
subfolder of the "../templates" folder.
"""
Jonathan Frederic
Fixed all broken references, refactored some stuff here and there,...
r10624 #Extension that the template files use.
template_extension = ".tplx"
file_extension = Unicode(
'tex', config=True,
help="Extension of the file that should be written to disk")
Jonathan Frederic
Split exporter into base and latex.
r10479
Jonathan Frederic
Fixed all broken references, refactored some stuff here and there,...
r10624 template_file = Unicode(
Jonathan Frederic
Almost have nbconvert working again...
r10630 'base', config=True,
Jonathan Frederic
Fixed all broken references, refactored some stuff here and there,...
r10624 help="Name of the template file to use")
Jonathan Frederic
Cleanup and refactor of API, almost complete....
r10677 def __init__(self, transformers=None, filters=None, config=None, **kw):
Jonathan Frederic
Finished a rough draft of the exporters.
r10588
#Call base class constructor.
Jonathan Frederic
Cleanup and refactor of API, almost complete....
r10677 super(LatexExporter, self).__init__(transformers, filters, config, **kw)
Jonathan Frederic
Fixed all broken references, refactored some stuff here and there,...
r10624
self.extract_figure_transformer.display_data_priority = ['latex', 'svg', 'png', 'jpg', 'jpeg' , 'text']
Jonathan Frederic
Finished a rough draft of the exporters.
r10588 self.extract_figure_transformer.extra_ext_map={'svg':'pdf'}
Jonathan Frederic
More clean-up. Nomenclature changes
r10578 def _init_environment(self):
Jonathan Frederic
Added latexexporter
r10587 self.environment = Environment(
Jonathan Frederic
More clean-up. Nomenclature changes
r10578 loader=FileSystemLoader([
os.path.dirname(os.path.realpath(__file__)) + LATEX_TEMPLATE_PATH,
os.path.dirname(os.path.realpath(__file__)) + LATEX_TEMPLATE_SKELETON_PATH,
]),
Jonathan Frederic
Added latexexporter
r10587 extensions=exporter.JINJA_EXTENSIONS
Jonathan Frederic
More clean-up. Nomenclature changes
r10578 )
#Set special Jinja2 syntax that will not conflict with latex.
Jonathan Frederic
Added latexexporter
r10587 self.environment.block_start_string = LATEX_JINJA_LOGIC_BLOCK[0]
self.environment.block_end_string = LATEX_JINJA_LOGIC_BLOCK[1]
self.environment.variable_start_string = LATEX_JINJA_VARIABLE_BLOCK[0]
self.environment.variable_end_string = LATEX_JINJA_VARIABLE_BLOCK[1]
self.environment.comment_start_string = LATEX_JINJA_COMMENT_BLOCK[0]
self.environment.comment_end_string = LATEX_JINJA_COMMENT_BLOCK[1]
def _register_filters(self):
#Register the filters of the base class.
Jonathan Frederic
Almost have nbconvert working again...
r10630 super(LatexExporter, self)._register_filters()
Jonathan Frederic
Added latexexporter
r10587
#Add latex filters to the Jinja2 environment
Jonathan Frederic
Cleanup and refactor of API, almost complete....
r10677 self.register_filter('escape_tex', nbconvert.filters.latex.escape_latex)
Jonathan Frederic
Almost have nbconvert working again...
r10630 self.register_filter('highlight', nbconvert.filters.highlight.highlight2latex)
Jonathan Frederic
Added latexexporter
r10587
Jonathan Frederic
Cleanup and refactor of API, almost complete....
r10677
Jonathan Frederic
Added latexexporter
r10587 def _register_transformers(self):
#Register the transformers of the base class.
Jonathan Frederic
Almost have nbconvert working again...
r10630 super(LatexExporter, self)._register_transformers()
Jonathan Frederic
Added latexexporter
r10587
#Register latex transformer
self.register_transformer(LatexTransformer)