From d1d5de89b7c808f9d6c1cac6526b7a201d4bd0b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Mart=C3=ADnez=20Portela?= Date: Tue, 1 Nov 2022 19:13:29 +0100 Subject: [PATCH 1/3] Revert "Fix parsing of lists inside drawers." This reverts commit 5e4a9f8ff2df3deac85b6fd4d08cadf4be6361e3. The tree should not be modified, as it's the returned value. We need an additional tree for parent-child element connections. --- org_rw/org_rw.py | 29 +++++++++-------------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/org_rw/org_rw.py b/org_rw/org_rw.py index c3d9f57..01cae02 100644 --- a/org_rw/org_rw.py +++ b/org_rw/org_rw.py @@ -489,26 +489,15 @@ class Headline: elif content.strip().upper() == ":END:": if current_node is None: logging.warning('Finished node (:END:) with no known starter') - else: - tree_up = list(tree) - while len(tree_up) > 0: - node = tree_up[-1] - if (isinstance( - node, dom.PropertyDrawerNode - ) or isinstance( - node, dom.LogbookDrawerNode - ) or isinstance( - node, dom.ResultsDrawerNode - )): - tree = tree_up - current_node = node - break - else: - tree_up.pop(-1) - else: - raise Exception('Unexpected node ({}) on headline (id={}), line {}'.format(current_node, self.id, linenum)) - current_node = None - tree.pop() + elif not (isinstance( + current_node, dom.PropertyDrawerNode + ) or isinstance( + current_node, dom.LogbookDrawerNode + ) or isinstance( + current_node, dom.ResultsDrawerNode + )): + raise Exception('Unexpected node: {}'.format(current_node)) + current_node = None elif content.strip().upper() == ":RESULTS:": assert current_node is None current_node = dom.ResultsDrawerNode() From fc9ce664aee4a1f2875a13b474d0003cc91781b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Mart=C3=ADnez=20Portela?= Date: Tue, 1 Nov 2022 19:57:50 +0100 Subject: [PATCH 2/3] Unify drawer node classes under a common parent. --- org_rw/dom.py | 27 +++++++++++---------------- org_rw/org_rw.py | 2 +- 2 files changed, 12 insertions(+), 17 deletions(-) diff --git a/org_rw/dom.py b/org_rw/dom.py index dd9c7d7..bc24272 100644 --- a/org_rw/dom.py +++ b/org_rw/dom.py @@ -1,32 +1,23 @@ -class PropertyDrawerNode: +class DrawerNode: def __init__(self): self.children = [] def append(self, child): self.children.append(child) + +class PropertyDrawerNode(DrawerNode): + def __repr__(self): return "".format(len(self.children)) -class LogbookDrawerNode: - def __init__(self): - self.children = [] - - def append(self, child): - self.children.append(child) - +class LogbookDrawerNode(DrawerNode): def __repr__(self): return "".format(len(self.children)) -class ResultsDrawerNode: - def __init__(self): - self.children = [] - - def append(self, child): - self.children.append(child) - +class ResultsDrawerNode(DrawerNode): def __repr__(self): return "".format(len(self.children)) @@ -82,12 +73,16 @@ class ListItem: class BlockNode: + def __init__(self): + self.children = [] + def append(self, child): - raise NotImplementedError() + self.children.append(child) class CodeBlock(BlockNode): def __init__(self, header, subtype): + super().__init__() self.header = header self.lines = None self.subtype = subtype diff --git a/org_rw/org_rw.py b/org_rw/org_rw.py index 01cae02..c6713b9 100644 --- a/org_rw/org_rw.py +++ b/org_rw/org_rw.py @@ -378,7 +378,7 @@ class Headline: elif isinstance(line, Text): if isinstance(current_node, dom.BlockNode): current_node.append(dom.Text(line)) - elif isinstance(current_node, dom.LogbookDrawerNode): + elif isinstance(current_node, dom.DrawerNode): current_node.append(dom.Text(line)) else: if isinstance(current_node, dom.TableNode): From 440f0bd4b79b02b9b3706df13d79f7dd4d3b52e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Mart=C3=ADnez=20Portela?= Date: Tue, 1 Nov 2022 19:58:17 +0100 Subject: [PATCH 3/3] Properly fix parsing of lists inside drawers. --- org_rw/org_rw.py | 50 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/org_rw/org_rw.py b/org_rw/org_rw.py index c6713b9..6cbd04b 100644 --- a/org_rw/org_rw.py +++ b/org_rw/org_rw.py @@ -390,13 +390,18 @@ class Headline: tree.append(dom.Text(text_to_dom(line.contents, line))) 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() - tree.append(current_node) - indentation_tree = [current_node] + if current_node is None: + tree.append(current_node) + indentation_tree.append(current_node) 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 if len(indentation_tree) > 0 and ( @@ -441,6 +446,7 @@ class Headline: if current_node is None: current_node = dom.TableNode() tree.append(current_node) + # TODO: Allow indentation of this element inside others indentation_tree = [current_node] 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 current_node = dom.PropertyDrawerNode() tree.append(current_node) + # TODO: Check if this can be nested + indentation_tree = [current_node] elif content.strip().upper() == ":LOGBOOK:": assert current_node is None current_node = dom.LogbookDrawerNode() tree.append(current_node) + # TODO: Check if this can be nested + indentation_tree = [current_node] elif content.strip().upper() == ":END:": - if current_node is None: - logging.warning('Finished node (:END:) with no known starter') - elif not (isinstance( - current_node, dom.PropertyDrawerNode - ) or isinstance( - current_node, dom.LogbookDrawerNode - ) or isinstance( - current_node, dom.ResultsDrawerNode - )): - raise Exception('Unexpected node: {}'.format(current_node)) - current_node = None + if current_node is None and len(indentation_tree) == 0: + logging.error('Finished node (:END:) with no known starter') + else: + tree_up = list(indentation_tree) + while len(tree_up) > 0: + node = tree_up[-1] + if isinstance(node, dom.DrawerNode): + indentation_tree = tree_up + current_node = 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 elif content.strip().upper() == ":RESULTS:": assert current_node is None current_node = dom.ResultsDrawerNode() + + # TODO: Allow indentation of these blocks inside others + indentation_tree = [current_node] tree.append(current_node) else: raise Exception("Unknown structural line: {}".format(line))