-
Notifications
You must be signed in to change notification settings - Fork 566
/
html.py
175 lines (147 loc) · 6.67 KB
/
html.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# -*- coding: utf-8 -*-
"""HTML Exporter class"""
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
import os
import mimetypes
import base64
from traitlets import default, Unicode, Bool
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
from .templateexporter import TemplateExporter
class HTMLExporter(TemplateExporter):
"""
Exports a basic HTML document. This exporter assists with the export of
HTML. Inherit from it if you are writing your own HTML template and need
custom preprocessors/filters. If you don't need custom preprocessors/
filters, just change the 'template_file' config option.
"""
export_from_notebook = "HTML"
anchor_link_text = Unicode(u'¶',
help="The text used as the text for anchor links.").tag(config=True)
exclude_anchor_links = Bool(False,
help="If anchor links should be included or not.").tag(config=True)
require_js_url = Unicode(
"https://cdnjs.cloudflare.com/ajax/libs/require.js/2.1.10/require.min.js",
help="""
URL to load require.js from.
Defaults to loading from cdnjs.
"""
).tag(config=True)
jquery_url = Unicode(
"https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js",
help="""
URL to load jQuery from.
Defaults to loading from cdnjs.
"""
).tag(config=True)
jupyter_widgets_base_url = Unicode(
"https://unpkg.com/",
help="URL base for Jupyter widgets"
).tag(config=True)
html_manager_semver_range = Unicode(
"*",
help="Semver range for Jupyter widgets HTML manager"
).tag(config=True)
@default('file_extension')
def _file_extension_default(self):
return '.html'
@default('template_name')
def _template_name_default(self):
return 'lab'
@default('template_data_paths')
def _template_data_paths_default(self):
return jupyter_path("nbconvert", "templates", "html")
theme = Unicode('light',
help='Template specific theme(e.g. the JupyterLab CSS theme for the lab template)'
).tag(config=True)
output_mimetype = 'text/html'
@property
def default_config(self):
c = Config({
'NbConvertBase': {
'display_data_priority' : ['application/vnd.jupyter.widget-state+json',
'application/vnd.jupyter.widget-view+json',
'application/javascript',
'text/html',
'text/markdown',
'image/svg+xml',
'text/latex',
'image/png',
'image/jpeg',
'text/plain'
]
},
'HighlightMagicsPreprocessor': {
'enabled':True
}
})
c.merge(super().default_config)
return c
@contextfilter
def markdown2html(self, context, source):
"""Markdown to HTML filter respecting the anchor_link_text setting"""
cell = context.get('cell', {})
attachments = cell.get('attachments', {})
renderer = IPythonRenderer(escape=False, attachments=attachments,
anchor_link_text=self.anchor_link_text,
exclude_anchor_links=self.exclude_anchor_links)
return MarkdownWithMath(renderer=renderer).render(source)
def default_filters(self):
for pair in super().default_filters():
yield pair
yield ('markdown2html', self.markdown2html)
def from_notebook_node(self, nb, resources=None, **kw):
langinfo = nb.metadata.get('language_info', {})
lexer = langinfo.get('pygments_lexer', langinfo.get('name', None))
highlight_code = self.filters.get('highlight_code', Highlight2HTML(pygments_lexer=lexer, parent=self))
self.register_filter('highlight_code', highlight_code)
return super().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)
for searchpath in self.template_paths:
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()._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
resources['require_js_url'] = self.require_js_url
resources['jquery_url'] = self.jquery_url
resources['jupyter_widgets_base_url'] = self.jupyter_widgets_base_url
resources['html_manager_semver_range'] = self.html_manager_semver_range
return resources