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 Aug 13, 2022
1 parent 6257bc3 commit bc04451
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 21 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
36 changes: 16 additions & 20 deletions nbconvert/filters/markdown_mistune.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,19 +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)
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 @@ -197,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 @@ -281,6 +265,18 @@ def _html_embed_images(self, html):
return str(parsed_html)


class MarkdownWithMath(Markdown):
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)
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 bc04451

Please sign in to comment.