Skip to content

Commit

Permalink
Import Markup and escape directly from MarkupsSafe (#1737)
Browse files Browse the repository at this point in the history
* Use markupsafe escape instead of jinja

* Add markupsafe to requirements

* use markupsafe import in exporters/html.py

* Setup jinja2 upper bound

* remove jinja upper bound
  • Loading branch information
rwedge committed Mar 28, 2022
1 parent 1211cb2 commit c91a038
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
9 changes: 5 additions & 4 deletions nbconvert/exporters/html.py
Expand Up @@ -14,6 +14,7 @@
from traitlets.config import Config
from jupyter_core.paths import jupyter_path
import jinja2
import markupsafe

if tuple(int(x) for x in jinja2.__version__.split(".")[:3]) < (3, 0, 0):
from jinja2 import contextfilter
Expand Down Expand Up @@ -213,7 +214,7 @@ 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)
return markupsafe.Markup(code)

def resources_include_lab_theme(name):
# Try to find the theme with the given name, looking through the labextensions
Expand All @@ -237,12 +238,12 @@ def resources_include_lab_theme(name):
data = data.replace(local_url, 'url(data:{};base64,{})'.format(mime_type, base64_data))

code = """<style type="text/css">\n%s</style>""" % data
return jinja2.Markup(code)
return markupsafe.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)
return markupsafe.Markup(code)

def resources_include_url(name):
env = self.environment
Expand All @@ -266,7 +267,7 @@ def resources_include_url(name):
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)
return markupsafe.Markup(src)

resources = super()._init_resources(resources)
resources['theme'] = self.theme
Expand Down
4 changes: 2 additions & 2 deletions nbconvert/filters/ansi.py
Expand Up @@ -4,7 +4,7 @@
# Distributed under the terms of the Modified BSD License.

import re
import jinja2
import markupsafe

__all__ = [
'strip_ansi',
Expand Down Expand Up @@ -57,7 +57,7 @@ def ansi2html(text):
Text containing ANSI colors to convert to HTML
"""
text = jinja2.utils.escape(text)
text = markupsafe.escape(text)
return _ansi2anything(text, _htmlconverter)


Expand Down
3 changes: 2 additions & 1 deletion setup.py
Expand Up @@ -225,7 +225,8 @@ def get_data_files():
'testpath',
'defusedxml',
'beautifulsoup4',
'nbclient>=0.5.0,<0.6.0'
'nbclient>=0.5.0,<0.6.0',
'MarkupSafe>=2.0'
]

pyppeteer_req = 'pyppeteer>=1,<1.1'
Expand Down

0 comments on commit c91a038

Please sign in to comment.