Skip to content

Commit

Permalink
markdown2html: Remove unneeded methods
Browse files Browse the repository at this point in the history
  • Loading branch information
TiagodePAlves committed Oct 4, 2022
1 parent 86243c4 commit ff1d6c1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 38 deletions.
3 changes: 2 additions & 1 deletion nbconvert/exporters/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ def markdown2html(self, context, source):
anchor_link_text=self.anchor_link_text,
exclude_anchor_links=self.exclude_anchor_links,
)
return MarkdownWithMath(renderer=renderer).render(source)
render = MarkdownWithMath(renderer=renderer)
return render(source)

def default_filters(self):
yield from super().default_filters()
Expand Down
65 changes: 28 additions & 37 deletions nbconvert/filters/markdown_mistune.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,36 +128,6 @@ def parse_latex_environment(self, m, state):
return m.end()


class MarkdownWithMath(Markdown):
def __init__(self, renderer, block=None, inline=None, plugins=None):
if block is None:
block = MathBlockParser()
if inline is None:
inline = MathInlineParser(renderer, hard_wrap=False)
if plugins is None:
plugins = [
# "abbr",
# 'footnotes',
"strikethrough",
"table",
"url",
"task_lists",
"def_list",
]
_plugins = []
for p in plugins:
if isinstance(p, str):
_plugins.append(import_plugin(p))
else:
_plugins.append(p)
plugins = _plugins
super().__init__(renderer, block, inline, plugins)

def render(self, s):
"""Compatibility method with `mistune==0.8.4`."""
return self(s)


class IPythonRenderer(HTMLRenderer):
def __init__(
self,
Expand Down Expand Up @@ -214,18 +184,15 @@ def heading(self, text, level, **attrs):
return html
return add_anchor(html, anchor_link_text=self.anchor_link_text)

def escape_html(self, text):
return html_escape(text)

def block_math(self, body):
return f"$${self.escape_html(body)}$$"
return f"$${html_escape(body)}$$"

def latex_environment(self, body, name):
name, body = self.escape_html(name), self.escape_html(body)
name, body = html_escape(name), html_escape(body)
return f"\\begin{{{name}}}{body}\\end{{{name}}}"

def inline_math(self, body):
return f"${self.escape_html(body)}$"
return f"${html_escape(body)}$"

def image(self, alt, url, title=None):
"""Rendering a image with title and text.
Expand Down Expand Up @@ -298,6 +265,30 @@ def _html_embed_images(self, html):
return str(parsed_html)


class MarkdownWithMath(Markdown):
DEFAULT_PLUGINS = (
# "abbr",
# 'footnotes',
"strikethrough",
"table",
"url",
"task_lists",
"def_list",
)

def __init__(self, renderer=None, block=None, inline=None, plugins=None):
if renderer is None:
renderer = IPythonRenderer(escape=False)
if block is None:
block = MathBlockParser()
if inline is None:
inline = MathInlineParser(renderer, hard_wrap=False)
if plugins is None:
plugins = [import_plugin(p) for p in self.DEFAULT_PLUGINS]
super().__init__(renderer, block, inline, plugins)


def markdown2html_mistune(source):
"""Convert a markdown string to HTML using mistune"""
return MarkdownWithMath(renderer=IPythonRenderer(escape=False)).render(source)
render = MarkdownWithMath()
return render(source)

0 comments on commit ff1d6c1

Please sign in to comment.