Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor template system to be easily extensible and voila compatible #1056

Merged
merged 1 commit into from
Oct 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ install:
- pip install check-manifest
- python -m ipykernel.kernelspec --user
script:
- check-manifest
- check-manifest --ignore "share/**"
# cd so we test the install, not the repo
- cd `mktemp -d`
- py.test --cov nbconvert -v --pyargs nbconvert
Expand Down
4 changes: 2 additions & 2 deletions nbconvert/exporters/asciidoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class ASCIIDocExporter(TemplateExporter):
def _file_extension_default(self):
return '.asciidoc'

@default('template_file')
def _template_file_default(self):
@default('template_name')
def _template_name_default(self):
return 'asciidoc'

output_mimetype = 'text/asciidoc'
Expand Down
2 changes: 1 addition & 1 deletion nbconvert/exporters/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class Exporter(LoggingConfigurable):
accompanying resources dict.
"""

file_extension = FilenameExtension('.txt',
file_extension = FilenameExtension(
help="Extension of the file that should be written to disk"
).tag(config=True)

Expand Down
60 changes: 54 additions & 6 deletions nbconvert/exporters/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
# Distributed under the terms of the Modified BSD License.

import os
import mimetypes
import base64

from traitlets import default, Unicode
from traitlets.config import Config
from jupyter_core.paths import jupyter_path
from jinja2 import contextfilter
from jinja2.loaders import split_template_path
import jinja2

from nbconvert.filters.highlight import Highlight2HTML
from nbconvert.filters.markdown_mistune import IPythonRenderer, MarkdownWithMath
Expand All @@ -33,17 +37,18 @@ class HTMLExporter(TemplateExporter):
def _file_extension_default(self):
return '.html'

@default('default_template_path')
def _default_template_path_default(self):
return os.path.join("..", "templates", "html")
@default('template_name')
def _template_name_default(self):
return 'classic'

@default('template_data_paths')
def _template_data_paths_default(self):
return jupyter_path("nbconvert", "templates", "html")

@default('template_file')
def _template_file_default(self):
return 'full.tpl'

theme = Unicode('light',
help='Template specific theme(e.g. the JupyterLab CSS theme for the lab template)'
).tag(config=True)

output_mimetype = 'text/html'

Expand Down Expand Up @@ -93,3 +98,46 @@ def from_notebook_node(self, nb, resources=None, **kw):
highlight_code = self.filters.get('highlight_code', Highlight2HTML(pygments_lexer=lexer, parent=self))
self.register_filter('highlight_code', highlight_code)
return super(HTMLExporter, self).from_notebook_node(nb, resources, **kw)

def _init_resources(self, resources):
def resources_include_css(name):
env = self.environment
code = """<style type="text/css">\n%s</style>""" % (env.loader.get_source(env, name)[0])
return jinja2.Markup(code)

def resources_include_js(name):
env = self.environment
code = """<script>\n%s</script>""" % (env.loader.get_source(env, name)[0])
return jinja2.Markup(code)

def resources_include_url(name):
env = self.environment
mime_type, encoding = mimetypes.guess_type(name)
try:
# we try to load via the jinja loader, but that tries to load
# as (encoded) text
data = env.loader.get_source(env, name)[0].encode('utf8')
except UnicodeDecodeError:
# if that fails (for instance a binary file, png or ttf)
# we mimic jinja2
pieces = split_template_path(name)
searchpaths = self.get_template_paths()
for searchpath in searchpaths:
filename = os.path.join(searchpath, *pieces)
print(filename, os.path.exists(filename))
if os.path.exists(filename):
with open(filename, "rb") as f:
data = f.read()
break
else:
raise ValueError("No file %r found in %r" % (name, searchpaths))
data = base64.b64encode(data)
data = data.replace(b'\n', b'').decode('ascii')
src = 'data:{mime_type};base64,{data}'.format(mime_type=mime_type, data=data)
return jinja2.Markup(src)
resources = super(HTMLExporter, self)._init_resources(resources)
resources['theme'] = self.theme
resources['include_css'] = resources_include_css
resources['include_js'] = resources_include_js
resources['include_url'] = resources_include_url
return resources
19 changes: 4 additions & 15 deletions nbconvert/exporters/latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,14 @@ class LatexExporter(TemplateExporter):
def _file_extension_default(self):
return '.tex'

@default('template_file')
def _template_file_default(self):
return 'article.tplx'

# Latex constants
@default('default_template_path')
def _default_template_path_default(self):
return os.path.join("..", "templates", "latex")

@default('template_skeleton_path')
def _template_skeleton_path_default(self):
return os.path.join("..", "templates", "latex", "skeleton")

@default('template_data_paths')
def _template_data_paths_default(self):
return jupyter_path("nbconvert", "templates", "latex")

#Extension that the template files use.
template_extension = Unicode(".tplx").tag(config=True)

@default('template_name')
def _template_name_default(self):
return 'latex'

output_mimetype = 'text/latex'

Expand Down
6 changes: 3 additions & 3 deletions nbconvert/exporters/markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ class MarkdownExporter(TemplateExporter):
def _file_extension_default(self):
return '.md'

@default('template_file')
def _template_file_default(self):
return 'markdown.tpl'
@default('template_name')
def _template_name_default(self):
return 'markdown'

output_mimetype = 'text/markdown'

Expand Down
5 changes: 5 additions & 0 deletions nbconvert/exporters/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ class PDFExporter(LatexExporter):
def _file_extension_default(self):
return '.pdf'


@default('template_extension')
def _template_extension_default(self):
return '.tex.j2'

def run_command(self, command_list, filename, count, log_function, raise_on_failure=None):
"""Run command_list count times.

Expand Down
6 changes: 3 additions & 3 deletions nbconvert/exporters/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class PythonExporter(TemplateExporter):
def _file_extension_default(self):
return '.py'

@default('template_file')
def _template_file_default(self):
return 'python.tpl'
@default('template_name')
def _template_name_default(self):
return 'python'

output_mimetype = 'text/x-python'
6 changes: 3 additions & 3 deletions nbconvert/exporters/rst.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ class RSTExporter(TemplateExporter):
def _file_extension_default(self):
return '.rst'

@default('template_file')
def _template_file_default(self):
return 'rst.tpl'
@default('template_name')
def _template_name_default(self):
return 'rst'

output_mimetype = 'text/restructuredtext'
export_from_notebook = "reST"
Expand Down
6 changes: 5 additions & 1 deletion nbconvert/exporters/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ class ScriptExporter(TemplateExporter):

@default('template_file')
def _template_file_default(self):
return 'script.tpl'
return 'script.j2'

@default('template_name')
def _template_name_default(self):
return 'script'

def _get_language_exporter(self, lang_name):
"""Find an exporter for the language name from notebook metadata.
Expand Down
14 changes: 11 additions & 3 deletions nbconvert/exporters/slides.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ class SlidesExporter(HTMLExporter):

export_from_notebook = "Reveal.js slides"

@default('template_name')
def _template_name_default(self):
return 'reveal'

template_name = Unicode('reveal',
help="Name of the template to use"
).tag(config=True, affects_template=True)

reveal_url_prefix = Unicode(
help="""The URL prefix for reveal.js (version 3.x).
This defaults to the reveal CDN, but can be any url pointing to a copy
Expand Down Expand Up @@ -160,9 +168,9 @@ def _reveal_url_prefix_default(self):
def _file_extension_default(self):
return '.slides.html'

@default('template_file')
def _template_file_default(self):
return 'slides_reveal.tpl'
@default('template_extension')
def _template_extension_default(self):
return '.html.j2'

output_mimetype = 'text/html'

Expand Down
Loading