templateexporter.py
323 lines
| 11.9 KiB
| text/x-python
|
PythonLexer
MinRK
|
r13664 | """This module defines TemplateExporter, a highly configurable converter | ||
Jonathan Frederic
|
r10690 | that uses Jinja2 to export notebook files into different formats. | ||
Matthias BUSSONNIER
|
r9578 | """ | ||
Matthias BUSSONNIER
|
r9819 | |||
Matthias BUSSONNIER
|
r9578 | #----------------------------------------------------------------------------- | ||
Matthias BUSSONNIER
|
r9665 | # Copyright (c) 2013, the IPython Development Team. | ||
Matthias BUSSONNIER
|
r9578 | # | ||
# 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
|
r10677 | |||
Jonathan Frederic
|
r10430 | from __future__ import print_function, absolute_import | ||
Matthias BUSSONNIER
|
r9665 | |||
# Stdlib imports | ||||
Matthias BUSSONNIER
|
r9819 | import os | ||
Matthias BUSSONNIER
|
r9665 | |||
MinRK
|
r14043 | # other libs/dependencies are imported at runtime | ||
# to move ImportErrors to runtime when the requirement is actually needed | ||||
Brian E. Granger
|
r11089 | |||
Matthias BUSSONNIER
|
r9665 | # IPython imports | ||
Jonathan Frederic
|
r12505 | from IPython.utils.traitlets import MetaHasTraits, Unicode, List, Dict, Any | ||
Jonathan Frederic
|
r11367 | from IPython.utils.importstring import import_item | ||
Jonathan Frederic
|
r12505 | from IPython.utils import py3compat, text | ||
Matthias BUSSONNIER
|
r9701 | |||
Thomas Kluyver
|
r13627 | from IPython.nbformat.current import docstring_nbformat_mod | ||
Brian E. Granger
|
r11089 | from IPython.nbconvert import filters | ||
Jonathan Frederic
|
r12505 | from .exporter import Exporter | ||
Jonathan Frederic
|
r10430 | |||
Jonathan Frederic
|
r10431 | #----------------------------------------------------------------------------- | ||
# Globals and constants | ||||
#----------------------------------------------------------------------------- | ||||
Matthias BUSSONNIER
|
r9665 | |||
Jonathan Frederic
|
r10431 | #Jinja2 extensions to load. | ||
JINJA_EXTENSIONS = ['jinja2.ext.loopcontrols'] | ||||
Matthias BUSSONNIER
|
r10838 | default_filters = { | ||
Jonathan Frederic
|
r12402 | 'indent': text.indent, | ||
Jonathan Frederic
|
r11685 | 'markdown2html': filters.markdown2html, | ||
Brian E. Granger
|
r11089 | 'ansi2html': filters.ansi2html, | ||
'filter_data_type': filters.DataTypeFilter, | ||||
'get_lines': filters.get_lines, | ||||
Matthias BUSSONNIER
|
r13128 | 'highlight2html': filters.Highlight2Html, | ||
Matthias BUSSONNIER
|
r13505 | 'highlight2latex': filters.Highlight2Latex, | ||
MinRK
|
r11711 | 'ipython2python': filters.ipython2python, | ||
MinRK
|
r11972 | 'posix_path': filters.posix_path, | ||
Brian E. Granger
|
r11089 | 'markdown2latex': filters.markdown2latex, | ||
'markdown2rst': filters.markdown2rst, | ||||
Jonathan Frederic
|
r11685 | 'comment_lines': filters.comment_lines, | ||
'strip_ansi': filters.strip_ansi, | ||||
'strip_dollars': filters.strip_dollars, | ||||
'strip_files_prefix': filters.strip_files_prefix, | ||||
'html2text' : filters.html2text, | ||||
MinRK
|
r11302 | 'add_anchor': filters.add_anchor, | ||
Brian E. Granger
|
r11089 | 'ansi2latex': filters.ansi2latex, | ||
Jonathan Frederic
|
r11685 | 'wrap_text': filters.wrap_text, | ||
MinRK
|
r11972 | 'escape_latex': filters.escape_latex, | ||
MinRK
|
r12451 | 'citation2latex': filters.citation2latex, | ||
'path2url': filters.path2url, | ||||
Jonathan Frederic
|
r12752 | 'add_prompts': filters.add_prompts, | ||
MinRK
|
r15434 | 'ascii_only': filters.ascii_only, | ||
Matthias BUSSONNIER
|
r10838 | } | ||
Jonathan Frederic
|
r10431 | #----------------------------------------------------------------------------- | ||
Jonathan Frederic
|
r10677 | # Class | ||
Matthias BUSSONNIER
|
r9665 | #----------------------------------------------------------------------------- | ||
Jonathan Frederic
|
r10677 | |||
Jonathan Frederic
|
r12505 | class TemplateExporter(Exporter): | ||
Jonathan Frederic
|
r10690 | """ | ||
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 | ||||
Paul Ivanov
|
r12219 | template type along with new filters/preprocessors. If the filters/ | ||
preprocessors provided by default suffice, there is no need to inherit from | ||||
Jonathan Frederic
|
r10690 | this class. Instead, override the template_file and file_extension | ||
traits via a config file. | ||||
Jonathan Frederic
|
r11750 | |||
Matthias BUSSONNIER
|
r10838 | {filters} | ||
""" | ||||
Matthias BUSSONNIER
|
r10874 | |||
# finish the docstring | ||||
Matthias BUSSONNIER
|
r10838 | __doc__ = __doc__.format(filters = '- '+'\n - '.join(default_filters.keys())) | ||
Jonathan Frederic
|
r11745 | template_file = Unicode(u'default', | ||
Jonathan Frederic
|
r11733 | config=True, | ||
Jonathan Frederic
|
r10431 | help="Name of the template file to use") | ||
Jonathan Frederic
|
r11745 | def _template_file_changed(self, name, old, new): | ||
Jonathan Frederic
|
r12505 | if new == 'default': | ||
Jonathan Frederic
|
r11745 | self.template_file = self.default_template | ||
else: | ||||
self.template_file = new | ||||
MinRK
|
r11860 | self.template = None | ||
MinRK
|
r11844 | self._load_template() | ||
Jonathan Frederic
|
r11745 | default_template = Unicode(u'') | ||
MinRK
|
r11844 | template = Any() | ||
environment = Any() | ||||
Jonathan Frederic
|
r9768 | |||
Jonathan Frederic
|
r11655 | template_path = List(['.'], config=True) | ||
MinRK
|
r11844 | def _template_path_changed(self, name, old, new): | ||
self._load_template() | ||||
Jonathan Frederic
|
r11648 | |||
default_template_path = Unicode( | ||||
os.path.join("..", "templates"), | ||||
Jonathan Frederic
|
r10690 | help="Path where the template files are located.") | ||
template_skeleton_path = Unicode( | ||||
Jonathan Frederic
|
r11648 | os.path.join("..", "templates", "skeleton"), | ||
Jonathan Frederic
|
r10690 | help="Path where the template skeleton files are located.") | ||
#Jinja block definitions | ||||
Jonathan Frederic
|
r10693 | 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) | ||||
Jonathan Frederic
|
r10690 | |||
Jonathan Frederic
|
r10624 | #Extension that the template files use. | ||
Jonathan Frederic
|
r10690 | template_extension = Unicode(".tpl", config=True) | ||
Jonathan Frederic
|
r10624 | |||
Jonathan Frederic
|
r11367 | filters = Dict(config=True, | ||
help="""Dictionary of filters, by name and namespace, to add to the Jinja | ||||
environment.""") | ||||
Jonathan Frederic
|
r11383 | |||
MinRK
|
r13678 | raw_mimetypes = List(config=True, | ||
MinRK
|
r13664 | help="""formats of raw cells to be included in this Exporter's output.""" | ||
) | ||||
MinRK
|
r13678 | def _raw_mimetypes_default(self): | ||
Thomas Kluyver
|
r13832 | return [self.output_mimetype, ''] | ||
MinRK
|
r13664 | |||
Jonathan Frederic
|
r11429 | |||
Jonathan Frederic
|
r11367 | def __init__(self, config=None, extra_loaders=None, **kw): | ||
Jonathan Frederic
|
r10690 | """ | ||
Public constructor | ||||
Jonathan Frederic
|
r10485 | |||
Jonathan Frederic
|
r10690 | Parameters | ||
---------- | ||||
config : config | ||||
User configuration instance. | ||||
Matthias BUSSONNIER
|
r11279 | extra_loaders : list[of Jinja Loaders] | ||
MinRK
|
r11845 | ordered list of Jinja loader to find templates. Will be tried in order | ||
before the default FileSystem ones. | ||||
Jonathan Frederic
|
r11745 | template : str (optional, kw arg) | ||
Template to use when exporting. | ||||
Jonathan Frederic
|
r10690 | """ | ||
MinRK
|
r13473 | super(TemplateExporter, self).__init__(config=config, **kw) | ||
Matthias BUSSONNIER
|
r9665 | |||
Jonathan Frederic
|
r11383 | #Init | ||
MinRK
|
r11852 | self._init_template() | ||
Matthias BUSSONNIER
|
r11279 | self._init_environment(extra_loaders=extra_loaders) | ||
Paul Ivanov
|
r12219 | self._init_preprocessors() | ||
Jonathan Frederic
|
r11383 | self._init_filters() | ||
Matthias BUSSONNIER
|
r9624 | |||
Matthias BUSSONNIER
|
r10862 | |||
MinRK
|
r11844 | def _load_template(self): | ||
MinRK
|
r11848 | """Load the Jinja template object from the template file | ||
This is a no-op if the template attribute is already defined, | ||||
or the Jinja environment is not setup yet. | ||||
This is triggered by various trait changes that would change the template. | ||||
""" | ||||
MinRK
|
r14043 | from jinja2 import TemplateNotFound | ||
MinRK
|
r11844 | if self.template is not None: | ||
return | ||||
# called too early, do nothing | ||||
if self.environment is None: | ||||
return | ||||
# Try different template names during conversion. First try to load the | ||||
# template by name with extension added, then try loading the template | ||||
# as if the name is explicitly specified, then try the name as a | ||||
# 'flavor', and lastly just try to load the template by module name. | ||||
MinRK
|
r11952 | try_names = [] | ||
if self.template_file: | ||||
try_names.extend([ | ||||
self.template_file + self.template_extension, | ||||
self.template_file, | ||||
]) | ||||
MinRK
|
r11844 | for try_name in try_names: | ||
self.log.debug("Attempting to load template %s", try_name) | ||||
try: | ||||
self.template = self.environment.get_template(try_name) | ||||
MinRK
|
r11951 | except (TemplateNotFound, IOError): | ||
MinRK
|
r11844 | pass | ||
MinRK
|
r11951 | except Exception as e: | ||
self.log.warn("Unexpected exception loading template: %s", try_name, exc_info=True) | ||||
MinRK
|
r11844 | else: | ||
self.log.info("Loaded template %s", try_name) | ||||
break | ||||
Thomas Kluyver
|
r13627 | |||
@docstring_nbformat_mod | ||||
Jonathan Frederic
|
r11376 | def from_notebook_node(self, nb, resources=None, **kw): | ||
Jonathan Frederic
|
r10690 | """ | ||
Convert a notebook from a notebook node instance. | ||||
Parameters | ||||
---------- | ||||
Thomas Kluyver
|
r13627 | nb : :class:`~{nbformat_mod}.nbbase.NotebookNode` | ||
Thomas Kluyver
|
r13598 | Notebook node | ||
resources : dict | ||||
Additional resources that can be accessed read/write by | ||||
preprocessors and filters. | ||||
Jonathan Frederic
|
r10690 | """ | ||
Jonathan Frederic
|
r12505 | nb_copy, resources = super(TemplateExporter, self).from_notebook_node(nb, resources, **kw) | ||
MinRK
|
r13678 | resources.setdefault('raw_mimetypes', self.raw_mimetypes) | ||
Matthias BUSSONNIER
|
r9578 | |||
MinRK
|
r11844 | self._load_template() | ||
Jonathan Frederic
|
r11745 | |||
MinRK
|
r11844 | if self.template is not None: | ||
Jonathan Frederic
|
r11745 | output = self.template.render(nb=nb_copy, resources=resources) | ||
else: | ||||
raise IOError('template file "%s" could not be found' % self.template_file) | ||||
Jonathan Frederic
|
r11367 | return output, resources | ||
Matthias BUSSONNIER
|
r9578 | |||
Jonathan Frederic
|
r11367 | |||
Jonathan Frederic
|
r11547 | def register_filter(self, name, jinja_filter): | ||
Jonathan Frederic
|
r10690 | """ | ||
Register a filter. | ||||
A filter is a function that accepts and acts on one string. | ||||
The filters are accesible within the Jinja templating engine. | ||||
Parameters | ||||
---------- | ||||
name : str | ||||
name to give the filter in the Jinja engine | ||||
filter : filter | ||||
""" | ||||
Jonathan Frederic
|
r11547 | if jinja_filter is None: | ||
Jonathan Frederic
|
r11428 | raise TypeError('filter') | ||
Jonathan Frederic
|
r11547 | isclass = isinstance(jinja_filter, type) | ||
Jonathan Frederic
|
r11428 | constructed = not isclass | ||
#Handle filter's registration based on it's type | ||||
Jonathan Frederic
|
r11547 | if constructed and isinstance(jinja_filter, py3compat.string_types): | ||
Jonathan Frederic
|
r11428 | #filter is a string, import the namespace and recursively call | ||
#this register_filter method | ||||
Jonathan Frederic
|
r11547 | filter_cls = import_item(jinja_filter) | ||
Jonathan Frederic
|
r11428 | return self.register_filter(name, filter_cls) | ||
Jonathan Frederic
|
r11547 | if constructed and hasattr(jinja_filter, '__call__'): | ||
Jonathan Frederic
|
r11428 | #filter is a function, no need to construct it. | ||
Jonathan Frederic
|
r11547 | self.environment.filters[name] = jinja_filter | ||
return jinja_filter | ||||
Jonathan Frederic
|
r11428 | |||
Jonathan Frederic
|
r11547 | elif isclass and isinstance(jinja_filter, MetaHasTraits): | ||
Jonathan Frederic
|
r11428 | #filter is configurable. Make sure to pass in new default for | ||
#the enabled flag if one was specified. | ||||
Jonathan Frederic
|
r11547 | filter_instance = jinja_filter(parent=self) | ||
self.register_filter(name, filter_instance ) | ||||
Jonathan Frederic
|
r11428 | |||
elif isclass: | ||||
#filter is not configurable, construct it | ||||
Jonathan Frederic
|
r11547 | filter_instance = jinja_filter() | ||
self.register_filter(name, filter_instance) | ||||
Jonathan Frederic
|
r11428 | |||
Jonathan Frederic
|
r10578 | else: | ||
Jonathan Frederic
|
r11428 | #filter is an instance of something without a __call__ | ||
#attribute. | ||||
raise TypeError('filter') | ||||
Jonathan Frederic
|
r10578 | |||
Jonathan Frederic
|
r10588 | |||
MinRK
|
r11852 | def _init_template(self): | ||
Jonathan Frederic
|
r11733 | """ | ||
Make sure a template name is specified. If one isn't specified, try to | ||||
build one from the information we know. | ||||
""" | ||||
Jonathan Frederic
|
r11745 | self._template_file_changed('template_file', self.template_file, self.template_file) | ||
Jonathan Frederic
|
r11733 | |||
Matthias BUSSONNIER
|
r11279 | def _init_environment(self, extra_loaders=None): | ||
Jonathan Frederic
|
r10690 | """ | ||
Create the Jinja templating environment. | ||||
""" | ||||
MinRK
|
r14043 | from jinja2 import Environment, ChoiceLoader, FileSystemLoader | ||
MinRK
|
r11197 | here = os.path.dirname(os.path.realpath(__file__)) | ||
Matthias BUSSONNIER
|
r11279 | loaders = [] | ||
if extra_loaders: | ||||
loaders.extend(extra_loaders) | ||||
Jonathan Frederic
|
r11655 | paths = self.template_path | ||
Jonathan Frederic
|
r11648 | paths.extend([os.path.join(here, self.default_template_path), | ||
os.path.join(here, self.template_skeleton_path)]) | ||||
loaders.append(FileSystemLoader(paths)) | ||||
Matthias BUSSONNIER
|
r11279 | |||
self.environment = Environment( | ||||
loader= ChoiceLoader(loaders), | ||||
Jonathan Frederic
|
r10485 | extensions=JINJA_EXTENSIONS | ||
) | ||||
Jonathan Frederic
|
r10690 | |||
#Set special Jinja2 syntax that will not conflict with latex. | ||||
Jonathan Frederic
|
r10693 | if self.jinja_logic_block_start: | ||
Jonathan Frederic
|
r10690 | self.environment.block_start_string = self.jinja_logic_block_start | ||
Jonathan Frederic
|
r10693 | if self.jinja_logic_block_end: | ||
Jonathan Frederic
|
r10690 | self.environment.block_end_string = self.jinja_logic_block_end | ||
Jonathan Frederic
|
r10693 | if self.jinja_variable_block_start: | ||
Jonathan Frederic
|
r10690 | self.environment.variable_start_string = self.jinja_variable_block_start | ||
Jonathan Frederic
|
r10693 | if self.jinja_variable_block_end: | ||
Jonathan Frederic
|
r10690 | self.environment.variable_end_string = self.jinja_variable_block_end | ||
Jonathan Frederic
|
r10693 | if self.jinja_comment_block_start: | ||
Jonathan Frederic
|
r10690 | self.environment.comment_start_string = self.jinja_comment_block_start | ||
Jonathan Frederic
|
r10693 | if self.jinja_comment_block_end: | ||
Jonathan Frederic
|
r10690 | self.environment.comment_end_string = self.jinja_comment_block_end | ||
Jonathan Frederic
|
r10485 | |||
Jonathan Frederic
|
r11383 | |||
def _init_filters(self): | ||||
""" | ||||
Register all of the filters required for the exporter. | ||||
""" | ||||
#Add default filters to the Jinja2 environment | ||||
Jonathan Frederic
|
r11547 | for key, value in default_filters.items(): | ||
Jonathan Frederic
|
r11383 | self.register_filter(key, value) | ||
#Load user filters. Overwrite existing filters if need be. | ||||
if self.filters: | ||||
Jonathan Frederic
|
r11547 | for key, user_filter in self.filters.items(): | ||
Jonathan Frederic
|
r11383 | self.register_filter(key, user_filter) | ||