Properly fix parsing of lists inside drawers.

This commit is contained in:
Sergio Martínez Portela 2022-11-01 19:58:17 +01:00
parent fc9ce664ae
commit 440f0bd4b7

View File

@ -390,13 +390,18 @@ class Headline:
tree.append(dom.Text(text_to_dom(line.contents, line))) tree.append(dom.Text(text_to_dom(line.contents, line)))
elif isinstance(line, ListItem): elif isinstance(line, ListItem):
if current_node is None or isinstance(current_node, dom.TableNode): if (current_node is None
or isinstance(current_node, dom.TableNode)
or isinstance(current_node, dom.BlockNode)
or isinstance(current_node, dom.DrawerNode)
):
current_node = dom.ListGroupNode() current_node = dom.ListGroupNode()
if current_node is None:
tree.append(current_node) tree.append(current_node)
indentation_tree = [current_node] indentation_tree.append(current_node)
if not isinstance(current_node, dom.ListGroupNode): if not isinstance(current_node, dom.ListGroupNode):
if not isinstance(current_node, dom.ListGroupNode): if not isinstance(current_node, dom.ListGroupNode):
logging.warning("Expected a {}, found: {} on line {} on {}".format(dom.ListGroupNode, current_node, line.linenum, self.doc.path)) raise Exception("Expected a {}, found: {} on line {} on {}".format(dom.ListGroupNode, current_node, line.linenum, self.doc.path))
# This can happen. Frequently inside a LogDrawer # This can happen. Frequently inside a LogDrawer
if len(indentation_tree) > 0 and ( if len(indentation_tree) > 0 and (
@ -441,6 +446,7 @@ class Headline:
if current_node is None: if current_node is None:
current_node = dom.TableNode() current_node = dom.TableNode()
tree.append(current_node) tree.append(current_node)
# TODO: Allow indentation of this element inside others
indentation_tree = [current_node] indentation_tree = [current_node]
if not isinstance(current_node, dom.TableNode): if not isinstance(current_node, dom.TableNode):
if not isinstance(current_node, dom.TableNode): if not isinstance(current_node, dom.TableNode):
@ -482,25 +488,39 @@ class Headline:
assert current_node is None assert current_node is None
current_node = dom.PropertyDrawerNode() current_node = dom.PropertyDrawerNode()
tree.append(current_node) tree.append(current_node)
# TODO: Check if this can be nested
indentation_tree = [current_node]
elif content.strip().upper() == ":LOGBOOK:": elif content.strip().upper() == ":LOGBOOK:":
assert current_node is None assert current_node is None
current_node = dom.LogbookDrawerNode() current_node = dom.LogbookDrawerNode()
tree.append(current_node) tree.append(current_node)
# TODO: Check if this can be nested
indentation_tree = [current_node]
elif content.strip().upper() == ":END:": elif content.strip().upper() == ":END:":
if current_node is None: if current_node is None and len(indentation_tree) == 0:
logging.warning('Finished node (:END:) with no known starter') logging.error('Finished node (:END:) with no known starter')
elif not (isinstance( else:
current_node, dom.PropertyDrawerNode tree_up = list(indentation_tree)
) or isinstance( while len(tree_up) > 0:
current_node, dom.LogbookDrawerNode node = tree_up[-1]
) or isinstance( if isinstance(node, dom.DrawerNode):
current_node, dom.ResultsDrawerNode indentation_tree = tree_up
)): current_node = node
raise Exception('Unexpected node: {}'.format(current_node)) tree_up.pop(-1)
break
else:
tree_up.pop(-1)
else:
raise Exception('Unexpected node ({}) on headline (id={}), line {}'.format(current_node, self.id, linenum))
if self.id == 'd07fcf27-d6fc-41e3-a9d0-b2e2902aec23':
print("Found node:", current_node)
current_node = None current_node = None
elif content.strip().upper() == ":RESULTS:": elif content.strip().upper() == ":RESULTS:":
assert current_node is None assert current_node is None
current_node = dom.ResultsDrawerNode() current_node = dom.ResultsDrawerNode()
# TODO: Allow indentation of these blocks inside others
indentation_tree = [current_node]
tree.append(current_node) tree.append(current_node)
else: else:
raise Exception("Unknown structural line: {}".format(line)) raise Exception("Unknown structural line: {}".format(line))