From 302689a622eb55ad6dc04b408cee2eb544d99f54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Mart=C3=ADnez=20Portela?= Date: Sun, 23 Apr 2023 20:48:57 +0200 Subject: [PATCH] Fix dom generated of table nested into list. --- org_rw/org_rw.py | 12 ++++++--- tests/10-tables.org | 15 +++++++++++ tests/test_org.py | 66 ++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 89 insertions(+), 4 deletions(-) diff --git a/org_rw/org_rw.py b/org_rw/org_rw.py index 822367b..734851f 100644 --- a/org_rw/org_rw.py +++ b/org_rw/org_rw.py @@ -488,9 +488,15 @@ class Headline: 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): - logging.warning("Expected a {}, found: {} on line {}".format(dom.TableNode, current_node, line.linenum)) + elif not isinstance(current_node, dom.TableNode): + if isinstance(current_node, dom.ListGroupNode): + # As an item inside a list + list_node = current_node + current_node = dom.TableNode() + list_node.append(current_node) + indentation_tree.append(current_node) + else: + logging.debug("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] == '-': diff --git a/tests/10-tables.org b/tests/10-tables.org index d9d404b..a473bed 100644 --- a/tests/10-tables.org +++ b/tests/10-tables.org @@ -16,3 +16,18 @@ | Content2-1 | Content2-2 | Content2-3 | Content after the table. +** Indented table +:PROPERTIES: +:ID: 10-table-test-id-02-indented +:CREATED: [2020-01-01 Wed 01:01] +:END: + +- This table is indented inside a list item. + - Item before in list + + | Header1 | Header2 | Header3 | + |------------+------------+------------| + | Content1-1 | Content1-2 | Content1-3 (last cell unclosed) + | Content2-1 | Content2-2 | Content2-3 | + - Item after in list +- This item happens after the indented table. diff --git a/tests/test_org.py b/tests/test_org.py index 8afd6a0..1255067 100644 --- a/tests/test_org.py +++ b/tests/test_org.py @@ -4,7 +4,8 @@ import unittest from datetime import date from datetime import datetime as DT -from org_rw import MarkerToken, MarkerType, Timestamp, dumps, load, loads +from org_rw import MarkerToken, MarkerType, Timestamp, dumps, load, loads, dom +import org_rw from utils.assertions import (BOLD, CODE, HL, ITALIC, SPAN, STRIKE, UNDERLINED, VERBATIM, WEB_LINK, Doc, Tokens) @@ -681,3 +682,66 @@ class TestSerde(unittest.TestCase): self.assertEqual(first_table[0].cells[1].strip(), 'Header2') self.assertEqual(first_table[0].cells[2].strip(), 'Header3') + hl = hl.children[0] + + tables = hl.get_tables() + first_table = tables[0] + self.assertEqual(len(first_table), 4) + + print(first_table[0]) + self.assertEqual(len(first_table[0].cells), 3) + self.assertEqual(first_table[0].cells[0].strip(), 'Header1') + self.assertEqual(first_table[0].cells[1].strip(), 'Header2') + self.assertEqual(first_table[0].cells[2].strip(), 'Header3') + + def test_tables_html_file_10(self): + with open(os.path.join(DIR, "10-tables.org")) as f: + doc = load(f) + + hl = doc.getTopHeadlines()[0] + + tree = hl.as_dom() + non_props = [ + item + for item in tree + if not isinstance(item, dom.PropertyDrawerNode) + ] + self.assertTrue(isinstance(non_props[0], dom.Text) + and isinstance(non_props[1], dom.TableNode) + and isinstance(non_props[2], dom.Text), + 'Expected ') + + + hl = hl.children[0] + tree = hl.as_dom() + non_props = [ + item + for item in tree + if not (isinstance(item, dom.PropertyDrawerNode) + or isinstance(item, dom.Text)) + ] + print_tree(non_props) + self.assertTrue(len(non_props) == 1, + 'Expected , with only (1) element') + + +def print_tree(tree, indentation=0, headline=None): + for element in tree: + print(" " * indentation * 2, "EL:", element) + if "children" in dir(element): + if len(element.children) > 0: + print_element(element.children, indentation + 1, headline) + print() + + elif "content" in dir(element): + for content in element.content: + print_element(content, indentation + 1, headline) + + +def print_element(element, indentation, headline): + if isinstance(element, org_rw.Link): + print(" " * indentation * 2, "Link:", element.get_raw()) + elif isinstance(element, str): + print(" " * indentation * 2, "Str[" + element.replace('\n', '') + "]", type(element)) + else: + print_tree(element, indentation, headline)