From 6813e9bd1b58ea240c9eae29ef8a97349ec4511b Mon Sep 17 00:00:00 2001 From: Pavel Hrdina Date: Tue, 23 Jan 2024 22:26:18 +0100 Subject: [PATCH] mkaestatic: add compact code to support mistune v3 Module mistune again changed the API. They changed signature of HTMLRenderer functions and dropped AstRenderer. The number of parameters is the same for link() and image() functions so it doesn't fails when defining our wrappers, instead it fails when they are called with the following error: TypeError: get_markdown_renderer..MyRenderer.link() got an unexpected keyword argument 'url' As we know that AstRenderer doesn't exist in v3 we can use it the same way to get AttributeError exception. Signed-off-by: Pavel Hrdina --- mkaestatic.py | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/mkaestatic.py b/mkaestatic.py index 0890208..27e09f7 100755 --- a/mkaestatic.py +++ b/mkaestatic.py @@ -199,16 +199,32 @@ def image(self, src, title, alt_text): return mistune.Markdown(renderer=MyRenderer(escape=False, use_xhtml=True)) except AttributeError: # Keep this to only support mistune 2 - class MyRenderer( mistune.HTMLRenderer ): - def link(self, link, text, title): - link = url_transform(link) - return super(MyRenderer, self).link(link, text, title) + try: + class Dummy( mistune.AstRenderer ): + pass - def image(self, src, alt_text, title): - src = url_transform(src) - return super(MyRenderer, self).image(src, alt_text, title) - - return mistune.create_markdown(renderer=MyRenderer(escape=False)) + class MyRenderer( mistune.HTMLRenderer ): + def link(self, link, text, title): + link = url_transform(link) + return super(MyRenderer, self).link(link, text, title) + + def image(self, src, alt_text, title): + src = url_transform(src) + return super(MyRenderer, self).image(src, alt_text, title) + + return mistune.create_markdown(renderer=MyRenderer(escape=False)) + + except AttributeError: + class MyRenderer( mistune.HTMLRenderer ): + def link(self, text, url, title=None): + url = url_transform(url) + return super(MyRenderer, self).link(text, url, title) + + def image(self, alt, url, title=None): + url = url_transform(url) + return super(MyRenderer, self).image(alt, url, title) + + return mistune.create_markdown(renderer=MyRenderer(escape=False)) def render( md_source, template_fn, site_cfg, config, cfg_tree, input_root ): ''' This function renders the markdown source to html code.