You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm writing a custom renderer, and I would like to modify my generated img tags to customize its url with a prefix.
That prefix depends on some extra data that depends on the current execution, but I'm unable to find a way to insert a context object in my custom render methods.
I know I could add an external prepopulated state object, but following the code, I can't find a way for that state to reach the rendering methods. The method's signature doesn't include a state. I've reviewed renderers/html.pyrender_token method, and it seems that the state is never used in the render functions.
How could I achieve that? The options that come to my mind are:
Add a property to my custom renderer class that changes before each invocation, but this could cause problems in case of multithreaded use and feels kinda ugly. I colud recreate the mistune object and/or the renderer in each invocation with my custom data, but I was looking for a better option.
Override the renderer core methods and call the render functions with the state included, but it seems like a lot of work and prone to errors.
Any ideas?
The text was updated successfully, but these errors were encountered:
I finally solved it like this: My Renderer class overrides the render_token method, and there, I get the state object and put my supplied information in the attributes dict.
This attributes dict is used in the parent render_token to dynamically call the specific render methods with the attributes unpacked.
It's a little hacky but works ok, and it doesn't affect anything else.
class WikiRenderer(mistune.HTMLRenderer):
def render_token(self, token, state) -> str:
attrs = token.get('attrs')
if attrs and token['type'] in ['wikilink', 'link']:
attrs['doc'] = state.env["doc"]
return mistune.HTMLRenderer.render_token(self, token, state)
def wikilink(self, text, title, doc):
return f'<a ...'
def link(self, text, url, title=None, doc=None):
...
I call the markdown rendering like this:
def markdown_view(space, doc):
state = markdown.block.state_cls()
state.env["doc"] =doc
text, _ = markdown.parse(space.fs.read_text(doc.path), state)
return text
I'm writing a custom renderer, and I would like to modify my generated
img
tags to customize its url with a prefix.That prefix depends on some extra data that depends on the current execution, but I'm unable to find a way to insert a context object in my custom render methods.
I know I could add an external prepopulated
state
object, but following the code, I can't find a way for that state to reach the rendering methods. The method's signature doesn't include a state. I've reviewedrenderers/html.py
render_token
method, and it seems that thestate
is never used in the render functions.How could I achieve that? The options that come to my mind are:
state
included, but it seems like a lot of work and prone to errors.Any ideas?
The text was updated successfully, but these errors were encountered: