Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get parent token #71

Closed
vallentin opened this issue Nov 15, 2018 · 0 comments · Fixed by #206
Closed

Get parent token #71

vallentin opened this issue Nov 15, 2018 · 0 comments · Fixed by #206
Assignees
Labels

Comments

@vallentin
Copy link
Contributor

I've implemented a custom HTMLRenderer, where I want to know if render_image is the only element on a paragraph.

Essentially be able to differentiate the output between these:

Foo ![](foobar.png) Bar

![](foobar.png)

This could be possible if token has a parent attribute.

class TestRenderer(HTMLRenderer):
    def render_image(self, token):
        has_more = len(token.parent.children) > 1
        if has_more:
            ...
        else:
            ...

Currently I have a _set_parent function, that is applied to Document in render.
However it would be nice, if this was part of mistletoe and done as children are added to the token.

from mistletoe import HTMLRenderer
from mistletoe.block_token import Document

def _set_parent(parent):
    if hasattr(parent, "children"):
        for child in parent.children:
            setattr(child, "parent", parent)
            _set_parent(child)

class TestRenderer(HTMLRenderer):
    def render(self, token):
        if isinstance(token, Document):
            _set_parent(token)
        return super().render(token)

    def render_image(self, token):
        has_more = len(token.parent.children) > 1
        ...
vallentin added a commit to vallentin/mistletoe that referenced this issue Nov 15, 2018
@pbodnar pbodnar added this to the 1.2.0 milestone Jun 17, 2023
@pbodnar pbodnar self-assigned this Jun 17, 2023
@pbodnar pbodnar modified the milestones: 1.2.0, 1.3.0 Aug 21, 2023
pbodnar added a commit that referenced this issue Dec 16, 2023
pbodnar added a commit that referenced this issue Jan 20, 2024
pbodnar added a commit that referenced this issue Jan 20, 2024
At the same time, we also make the `children`
attribute a _property_. This makes it easier for us
to set the `parent` to every child token, without
having to change much of the existing code.
@pbodnar pbodnar removed this from the 1.3.0 milestone Jan 21, 2024
pbodnar added a commit that referenced this issue Jan 27, 2024
At the same time, we also make the `children`
attribute a _property_. This makes it easier for us
to set the `parent` to every child token, without
having to change much of the existing code.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants