Skip to content

Commit

Permalink
feat: make the HTMLRenderer escaping options available to subclasses (#…
Browse files Browse the repository at this point in the history
…176)

This is also de-facto a preparation for the possibility to pass options to renderers from command line.

* fix `TOCRenderer` arguments order at the same time (`*extras` cannot be passed after `**kwargs`)
* `MathJaxRenderer` has 2 parents, but unless ancestors' constructors consume the same params, this doesn't seem to be a problem in Python
  • Loading branch information
pbodnar committed Jun 10, 2023
1 parent 04bb2e6 commit 7479e91
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 20 deletions.
2 changes: 1 addition & 1 deletion mistletoe/base_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class BaseRenderer(object):
"""
_parse_name = re.compile(r"([A-Z][a-z]+|[A-Z]+(?![a-z]))")

def __init__(self, *extras):
def __init__(self, *extras, **kwargs):
self.render_map = {
'Strong': self.render_strong,
'Emphasis': self.render_emphasis,
Expand Down
9 changes: 7 additions & 2 deletions mistletoe/contrib/github_wiki.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ def __init__(self, match):


class GithubWikiRenderer(HTMLRenderer):
def __init__(self):
super().__init__(GithubWiki)
def __init__(self, **kwargs):
"""
Args:
**kwargs: additional parameters to be passed to the ancestor's
constructor.
"""
super().__init__(GithubWiki, **kwargs)

def render_github_wiki(self, token):
template = '<a href="{target}">{inner}</a>'
Expand Down
12 changes: 8 additions & 4 deletions mistletoe/contrib/mathjax.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
from mistletoe.latex_renderer import LaTeXRenderer

class MathJaxRenderer(HTMLRenderer, LaTeXRenderer):
"""
MRO will first look for render functions under HTMLRenderer,
then LaTeXRenderer.
"""
def __init__(self, **kwargs):
"""
Args:
**kwargs: additional parameters to be passed to the ancestors'
constructors.
"""
super().__init__(**kwargs)

mathjax_src = '<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-MML-AM_CHTML"></script>\n'

def render_math(self, token):
Expand Down
21 changes: 12 additions & 9 deletions mistletoe/contrib/toc_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@
class TOCRenderer(HTMLRenderer):
"""
Extends HTMLRenderer class for table of contents support.
Args:
depth (int): the maximum level of heading to be included in TOC;
omit_title (bool): whether to ignore tokens where token.level == 1;
filter_conds (list): when any of these functions evaluate to true,
current heading will not be included;
extras (list): allows subclasses to add even more custom tokens.
"""
def __init__(self, depth=5, omit_title=True, filter_conds=[], *extras):
super().__init__(*extras)
def __init__(self, *extras, depth=5, omit_title=True, filter_conds=[], **kwargs):
"""
Args:
extras (list): allows subclasses to add even more custom tokens.
depth (int): the maximum level of heading to be included in TOC.
omit_title (bool): whether to ignore tokens where token.level == 1.
filter_conds (list): when any of these functions evaluate to true,
current heading will not be included.
**kwargs: additional parameters to be passed to the ancestor's
constructor.
"""
super().__init__(*extras, **kwargs)
self._headings = []
self.depth = depth
self.omit_title = omit_title
Expand Down
6 changes: 4 additions & 2 deletions mistletoe/html_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,19 @@ class HTMLRenderer(BaseRenderer):
See mistletoe.base_renderer module for more info.
"""
def __init__(self, *extras, html_escape_double_quotes=True, html_escape_single_quotes=False):
def __init__(self, *extras, html_escape_double_quotes=True, html_escape_single_quotes=False, **kwargs):
"""
Args:
extras (list): allows subclasses to add even more custom tokens.
html_escape_double_quotes (bool): whether to also escape double
quotes when HTML-escaping rendered text.
html_escape_single_quotes (bool): whether to also escape single
quotes when HTML-escaping rendered text.
**kwargs: additional parameters to be passed to the ancestor's
constructor.
"""
self._suppress_ptag_stack = [False]
super().__init__(*chain((HTMLBlock, HTMLSpan), extras))
super().__init__(*chain((HTMLBlock, HTMLSpan), extras), **kwargs)
self.html_escape_double_quotes = html_escape_double_quotes
self.html_escape_single_quotes = html_escape_single_quotes

Expand Down
6 changes: 4 additions & 2 deletions mistletoe/latex_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@


class LaTeXRenderer(BaseRenderer):
def __init__(self, *extras):
def __init__(self, *extras, **kwargs):
"""
Args:
extras (list): allows subclasses to add even more custom tokens.
**kwargs: additional parameters to be passed to the ancestor's
constructor.
"""
tokens = self._tokens_from_module(latex_token)
self.packages = {}
self.verb_delimiters = verb_delimiters
super().__init__(*chain(tokens, extras))
super().__init__(*chain(tokens, extras), **kwargs)

def render_strong(self, token):
return '\\textbf{{{}}}'.format(self.render_inner(token))
Expand Down

0 comments on commit 7479e91

Please sign in to comment.