latex.py
96 lines
| 3.5 KiB
| text/x-python
|
PythonLexer
|
r13664 | """LaTeX Exporter class""" | ||
|
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 | ||||
#----------------------------------------------------------------------------- | ||||
|
r10585 | |||
|
r11203 | # Stdlib imports | ||
import os | ||||
|
r10585 | # IPython imports | ||
|
r13933 | from IPython.utils.traitlets import Unicode | ||
|
r10872 | from IPython.config import Config | ||
|
r10585 | |||
|
r18381 | from IPython.nbconvert.filters.highlight import Highlight2Latex | ||
|
r12505 | from .templateexporter import TemplateExporter | ||
|
r10677 | |||
|
r10479 | #----------------------------------------------------------------------------- | ||
# Classes and functions | ||||
#----------------------------------------------------------------------------- | ||||
|
r12505 | class LatexExporter(TemplateExporter): | ||
|
r10677 | """ | ||
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. | ||||
""" | ||||
|
r13925 | |||
def _file_extension_default(self): | ||||
return 'tex' | ||||
def _template_file_default(self): | ||||
return 'article' | ||||
|
r10479 | |||
|
r10690 | #Latex constants | ||
|
r14047 | def _default_template_path_default(self): | ||
return os.path.join("..", "templates", "latex") | ||||
|
r10690 | |||
|
r14050 | def _template_skeleton_path_default(self): | ||
return os.path.join("..", "templates", "latex", "skeleton") | ||||
|
r10690 | |||
#Special Jinja2 syntax that will not conflict when exporting latex. | ||||
jinja_comment_block_start = Unicode("((=", config=True) | ||||
jinja_comment_block_end = Unicode("=))", config=True) | ||||
jinja_variable_block_start = Unicode("(((", config=True) | ||||
jinja_variable_block_end = Unicode(")))", config=True) | ||||
jinja_logic_block_start = Unicode("((*", config=True) | ||||
jinja_logic_block_end = Unicode("*))", config=True) | ||||
#Extension that the template files use. | ||||
template_extension = Unicode(".tplx", config=True) | ||||
|
r10869 | |||
|
r13834 | output_mimetype = 'text/latex' | ||
|
r13664 | |||
|
r11733 | |||
|
r10963 | @property | ||
def default_config(self): | ||||
c = Config({ | ||||
|
r11419 | 'NbConvertBase': { | ||
|
r15346 | 'display_data_priority' : ['latex', 'application/pdf', 'png', 'jpg', 'svg', 'jpeg', 'text'] | ||
|
r10965 | }, | ||
|
r12219 | 'ExtractOutputPreprocessor': { | ||
|
r11367 | 'enabled':True | ||
|
r11383 | }, | ||
|
r12219 | 'SVG2PDFPreprocessor': { | ||
|
r11383 | 'enabled':True | ||
}, | ||||
|
r12219 | 'LatexPreprocessor': { | ||
|
r11383 | 'enabled':True | ||
|
r11736 | }, | ||
|
r12219 | 'SphinxPreprocessor': { | ||
|
r11733 | 'enabled':True | ||
|
r12573 | }, | ||
'HighlightMagicsPreprocessor': { | ||||
'enabled':True | ||||
|
r11733 | } | ||
|
r10963 | }) | ||
c.merge(super(LatexExporter,self).default_config) | ||||
return c | ||||
|
r18381 | |||
def from_notebook_node(self, nb, resources=None, **kw): | ||||
|
r18469 | langinfo = nb.metadata.get('language_info', {}) | ||
lexer = langinfo.get('pygments_lexer', langinfo.get('name', None)) | ||||
|
r18381 | self.register_filter('highlight_code', | ||
Highlight2Latex(pygments_lexer=lexer, parent=self)) | ||||
return super(LatexExporter, self).from_notebook_node(nb, resources, **kw) | ||||