From 28d8b6914642e4f71dd23e3316db57a227bea6a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Mart=C3=ADnez=20Portela?= Date: Tue, 27 Sep 2022 23:55:07 +0200 Subject: [PATCH] Export table on `as_dom`. --- org_rw/dom.py | 18 ++++++++++++++++++ org_rw/org_rw.py | 25 ++++++++++++++++++++++--- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/org_rw/dom.py b/org_rw/dom.py index d34404d..dd9c7d7 100644 --- a/org_rw/dom.py +++ b/org_rw/dom.py @@ -50,6 +50,24 @@ class ListGroupNode: def __repr__(self): return "".format(len(self.children)) +class TableNode: + def __init__(self): + self.children = [] + + def append(self, child): + self.children.append(child) + + def __repr__(self): + return "".format(len(self.children)) + +class TableSeparatorRow: + def __init__(self, orig=None): + self.orig = orig + +class TableRow: + def __init__(self, cells, orig=None): + self.cells = cells + self.orig = orig class Text: def __init__(self, content): diff --git a/org_rw/org_rw.py b/org_rw/org_rw.py index 98b2d68..8a9c08d 100644 --- a/org_rw/org_rw.py +++ b/org_rw/org_rw.py @@ -332,6 +332,7 @@ class Headline: self.keywords + self.contents + self.list_items + + self.table_rows + self.properties + self.structural + self.delimiters @@ -377,20 +378,22 @@ class Headline: elif isinstance(current_node, dom.LogbookDrawerNode): current_node.append(dom.Text(line)) else: - if type(current_node) not in NON_FINISHED_GROUPS: + if isinstance(current_node, dom.TableNode): + pass # No problem here + elif type(current_node) not in NON_FINISHED_GROUPS: raise NotImplementedError('Not implemented node type: {}'.format(current_node)) current_node = None contents = [] tree.append(dom.Text(text_to_dom(line.contents, line))) elif isinstance(line, ListItem): - if current_node is None: + if current_node is None or isinstance(current_node, dom.TableNode): current_node = dom.ListGroupNode() tree.append(current_node) indentation_tree = [current_node] if not isinstance(current_node, dom.ListGroupNode): if not isinstance(current_node, dom.ListGroupNode): - logging.warning("Expected a {}, found: {} on line {}".format(dom.ListGroupNode, current_node, line.linenum)) + logging.warning("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 ( @@ -431,6 +434,22 @@ class Headline: node = dom.ListItem(text_to_dom(line.tag, line), text_to_dom(line.content, line), orig=line) current_node.append(node) + elif isinstance(line, TableRow): + if current_node is None: + current_node = dom.TableNode() + tree.append(current_node) + indentation_tree = [current_node] + if not isinstance(current_node, dom.TableNode): + if not isinstance(current_node, dom.TableNode): + logging.warning("Expected a {}, found: {} on line {}".format(dom.TableNode, current_node, line.linenum)) + # This can happen. Frequently inside a LogDrawer + + if len(line.cells) > 0 and len(line.cells[0]) > 0 and line.cells[0][0] == '-': + node = dom.TableSeparatorRow(orig=line) + else: + node = dom.TableRow(line.cells, orig=line) + current_node.append(node) + elif ( isinstance(line, DelimiterLine) and line.delimiter_type == DelimiterLineType.BEGIN_BLOCK