Skip to content

Commit

Permalink
Refactor template system to be easily extensible and voila compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
maartenbreddels committed Oct 14, 2019
1 parent 6a02aa8 commit 621eb9d
Show file tree
Hide file tree
Showing 54 changed files with 756 additions and 205 deletions.
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
43 changes: 37 additions & 6 deletions nbconvert/exporters/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
# 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
import jinja2

from nbconvert.filters.highlight import Highlight2HTML
from nbconvert.filters.markdown_mistune import IPythonRenderer, MarkdownWithMath
Expand All @@ -33,17 +36,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 +97,30 @@ 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)
data = env.loader.get_source(env, name)[0]
data = base64.b64encode(data.encode('utf8'))
data = data.replace(b'\n', b'').decode('ascii')
print(type(data), data)
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
24 changes: 10 additions & 14 deletions nbconvert/exporters/latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,21 @@ 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")


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

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

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

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
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'

output_mimetype = 'text/html'

Expand Down
Loading

0 comments on commit 621eb9d

Please sign in to comment.