From 7479e9119f4fecf680299adb127e49ca9784a11a Mon Sep 17 00:00:00 2001 From: Petr Bodnar Date: Tue, 6 Jun 2023 22:25:33 +0200 Subject: [PATCH] feat: make the HTMLRenderer escaping options available to subclasses (#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 --- mistletoe/base_renderer.py | 2 +- mistletoe/contrib/github_wiki.py | 9 +++++++-- mistletoe/contrib/mathjax.py | 12 ++++++++---- mistletoe/contrib/toc_renderer.py | 21 ++++++++++++--------- mistletoe/html_renderer.py | 6 ++++-- mistletoe/latex_renderer.py | 6 ++++-- 6 files changed, 36 insertions(+), 20 deletions(-) diff --git a/mistletoe/base_renderer.py b/mistletoe/base_renderer.py index 4a07cce9..9c20113b 100644 --- a/mistletoe/base_renderer.py +++ b/mistletoe/base_renderer.py @@ -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, diff --git a/mistletoe/contrib/github_wiki.py b/mistletoe/contrib/github_wiki.py index 7c1804b6..0cb3420c 100644 --- a/mistletoe/contrib/github_wiki.py +++ b/mistletoe/contrib/github_wiki.py @@ -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 = '{inner}' diff --git a/mistletoe/contrib/mathjax.py b/mistletoe/contrib/mathjax.py index c9766a93..53606b07 100644 --- a/mistletoe/contrib/mathjax.py +++ b/mistletoe/contrib/mathjax.py @@ -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 = '\n' def render_math(self, token): diff --git a/mistletoe/contrib/toc_renderer.py b/mistletoe/contrib/toc_renderer.py index 975195bd..7861fe90 100644 --- a/mistletoe/contrib/toc_renderer.py +++ b/mistletoe/contrib/toc_renderer.py @@ -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 diff --git a/mistletoe/html_renderer.py b/mistletoe/html_renderer.py index 45ee9d43..048cf09c 100644 --- a/mistletoe/html_renderer.py +++ b/mistletoe/html_renderer.py @@ -18,7 +18,7 @@ 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. @@ -26,9 +26,11 @@ def __init__(self, *extras, html_escape_double_quotes=True, html_escape_single_q 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 diff --git a/mistletoe/latex_renderer.py b/mistletoe/latex_renderer.py index 26acdd03..9ff52b34 100644 --- a/mistletoe/latex_renderer.py +++ b/mistletoe/latex_renderer.py @@ -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))