mistletoe icon indicating copy to clipboard operation
mistletoe copied to clipboard

Added parent attribute

Open vallentin opened this issue 7 years ago • 4 comments

Resolves #71.

vallentin avatar Nov 15 '18 13:11 vallentin

Coverage Status

Coverage increased (+0.07%) to 93.233% when pulling b13d7387f382922d1fa14be49039f8dc210a6a14 on Vallentin:parent into 846a419bcb83afab02f3f19d151ab0166fab68f6 on miyuchina:master.

coveralls avatar Nov 15 '18 14:11 coveralls

Here's a suggestion.

Instead of all the for child in children, it might be beneficial to change __init__ of SpanToken and BlockToken. To allow and instead pass all children via super().

Something like:

class SpanToken:
    ...

    def __init__(self, match, children=None):
        if not self.parse_inner:
            self.content = match.group(self.parse_group)
        if children is not None:
            self.children = children
            for child in self.children:
                child.parent = self

    def __contains__(self, text):
        if hasattr(self, 'children'):
            return any(text in child for child in self.children)
        return text in self.content

What do you think @miyuchina? Is the parent attribute even something you want in the first place?

If you're open to this feature, then I'll happily refactor mistletoe.

vallentin avatar Nov 15 '18 14:11 vallentin

This looks promising. I totally agree with some refactoring though, so that the logic of setting parent into children is kept just at one place.

pbodnar avatar Sep 17 '21 18:09 pbodnar

Hi there, I would like to finally finish this feature after all the years. :) I think now, the simplest thing to do is to make the children attribute a @property with a setter function which would take care of setting the parent to all the children. Now that we've already got the common Token ancestor class, it would mean extending just this class. I will check this out and possibly create a fresh PR...

pbodnar avatar Dec 14 '23 21:12 pbodnar

Replaced by #206. Thanks for the initial idea. :)

pbodnar avatar Mar 20 '24 07:03 pbodnar