Added parent attribute
Resolves #71.
Coverage increased (+0.07%) to 93.233% when pulling b13d7387f382922d1fa14be49039f8dc210a6a14 on Vallentin:parent into 846a419bcb83afab02f3f19d151ab0166fab68f6 on miyuchina:master.
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.
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.
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...
Replaced by #206. Thanks for the initial idea. :)