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

Added parent attribute #72

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions mistletoe/ast_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,6 @@ def get_ast(token):
node['header'] = get_ast(node['header'])
if 'children' in node:
node['children'] = [get_ast(child) for child in node['children']]
if 'parent' in node:
node['parent'] = node['parent'].__class__.__name__
return node
19 changes: 19 additions & 0 deletions mistletoe/block_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ class BlockToken(object):
"""
def __init__(self, lines, tokenize_func):
self.children = tokenize_func(lines)
for child in self.children:
child.parent = self

def __contains__(self, text):
return any(text in child for child in self.children)
Expand Down Expand Up @@ -148,6 +150,8 @@ def __init__(self, lines):
_root_node = self
span_token._root_node = self
self.children = tokenize(lines)
for child in self.children:
child.parent = self
span_token._root_node = None
_root_node = None

Expand Down Expand Up @@ -211,6 +215,8 @@ class Quote(BlockToken):
def __init__(self, parse_buffer):
# span-level tokenizing happens here.
self.children = tokenizer.make_tokens(parse_buffer)
for child in self.children:
child.parent = self

@staticmethod
def start(line):
Expand Down Expand Up @@ -362,6 +368,8 @@ class BlockCode(BlockToken):
def __init__(self, lines):
self.language = ''
self.children = (span_token.RawText(''.join(lines).strip('\n')+'\n'),)
for child in self.children:
child.parent = self

@staticmethod
def start(line):
Expand Down Expand Up @@ -410,6 +418,8 @@ def __init__(self, match):
lines, open_info = match
self.language = span_token.EscapeSequence.strip(open_info[2])
self.children = (span_token.RawText(''.join(lines)),)
for child in self.children:
child.parent = self

@classmethod
def start(cls, line):
Expand Down Expand Up @@ -451,6 +461,8 @@ class List(BlockToken):
pattern = re.compile(r' {0,3}(?:\d{0,9}[.)]|[+\-*])(?:[ \t]*$|[ \t]+)')
def __init__(self, matches):
self.children = [ListItem(*match) for match in matches]
for child in self.children:
child.parent = self
self.loose = any(item.loose for item in self.children)
leader = self.children[0].leader
self.start = None
Expand Down Expand Up @@ -497,6 +509,8 @@ def __init__(self, parse_buffer, prepend, leader):
self.leader = leader
self.prepend = prepend
self.children = tokenizer.make_tokens(parse_buffer)
for child in self.children:
child.parent = self
self.loose = parse_buffer.loose

@staticmethod
Expand Down Expand Up @@ -619,9 +633,12 @@ def __init__(self, lines):
for column in self.split_delimiter(lines[1])]
self.header = TableRow(lines[0], self.column_align)
self.children = [TableRow(line, self.column_align) for line in lines[2:]]
self.header.parent = self
else:
self.column_align = [None]
self.children = [TableRow(line) for line in lines]
for child in self.children:
child.parent = self

@staticmethod
def split_delimiter(delimiter):
Expand Down Expand Up @@ -675,6 +692,8 @@ def __init__(self, line, row_align=None):
cells = filter(None, line.strip().split('|'))
self.children = [TableCell(cell.strip(), align)
for cell, align in zip_longest(cells, self.row_align)]
for child in self.children:
child.parent = self


class TableCell(BlockToken):
Expand Down
6 changes: 6 additions & 0 deletions mistletoe/span_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ class InlineCode(SpanToken):
def __init__(self, match):
content = match.group(self.parse_group)
self.children = (RawText(' '.join(re.split('[ \n]+', content.strip()))),)
for child in self.children:
child.parent = self

@classmethod
def find(cls, string):
Expand Down Expand Up @@ -169,6 +171,8 @@ class AutoLink(SpanToken):
def __init__(self, match):
content = match.group(self.parse_group)
self.children = (RawText(content),)
for child in self.children:
child.parent = self
self.target = content
self.mailto = '@' in self.target and 'mailto' not in self.target.casefold()

Expand All @@ -186,6 +190,8 @@ class EscapeSequence(SpanToken):

def __init__(self, match):
self.children = (RawText(match.group(self.parse_group)),)
for child in self.children:
child.parent = self

@classmethod
def strip(cls, string):
Expand Down
2 changes: 2 additions & 0 deletions mistletoe/span_tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ def make(self):
children = make_tokens(self.children, self.parse_start, self.parse_end, self.string, self.fallback_token)
token = self.cls(self.match)
token.children = children
for child in token.children:
child.parent = token
return token

def __lt__(self, other):
Expand Down
27 changes: 18 additions & 9 deletions test/test_ast_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,27 @@ def test(self):
'level': 1,
'children': [{
'type': 'RawText',
'content': 'heading 1'
}]
'content': 'heading 1',
'parent': 'Heading'
}],
'parent': 'Document'
}, {
'type': 'Paragraph',
'children': [{
'type': 'RawText',
'content': 'hello'
'content': 'hello',
'parent': 'Paragraph'
}, {
'type': 'LineBreak',
'soft': True,
'content': ''
'content': '',
'parent': 'Paragraph'
}, {
'type': 'RawText',
'content': 'world'
}]
'content': 'world',
'parent': 'Paragraph'
}],
'parent': 'Document'
}]}
self.assertEqual(output, target)

Expand All @@ -46,9 +52,12 @@ def test_footnotes(self):
'title': '',
'children': [{
'type': 'RawText',
'content': 'bar'
}]
}]
'content': 'bar',
'parent': 'Link'
}],
'parent': 'Paragraph'
}],
'parent': 'Document'
}]}
output = ast_renderer.get_ast(d)
self.assertEqual(output, target)