Compare commits

...

3 Commits

Author SHA1 Message Date
Sergio Martínez Portela
790ef57598 Add extra check for TimeRange validity. 2023-04-23 20:50:06 +02:00
Sergio Martínez Portela
302689a622 Fix dom generated of table nested into list. 2023-04-23 20:50:06 +02:00
Sergio Martínez Portela
7008124509 Use more robust list de-indentation handler. 2023-04-13 23:56:58 +02:00
3 changed files with 105 additions and 15 deletions

View File

@ -457,17 +457,20 @@ class Headline:
current_node = sublist current_node = sublist
indentation_tree.append(current_node) indentation_tree.append(current_node)
while len(indentation_tree) > 0 and ( while len(indentation_tree) > 0:
(len(indentation_tree[-1].children) > 0) list_children = [
and len( c
[ for c in indentation_tree[-1].children
c if isinstance(c, dom.ListItem)
for c in indentation_tree[-1].children ]
if isinstance(c, dom.ListItem)
][-1].orig.indentation if ((len(list_children) > 0)
) and (len(list_children[-1].orig.indentation)
> len(line.indentation) <= len(line.indentation))):
): # No more breaking out of lists, it's indentation
# is less than ours
break
rem = indentation_tree.pop(-1) rem = indentation_tree.pop(-1)
if len(indentation_tree) == 0: if len(indentation_tree) == 0:
indentation_tree.append(rem) indentation_tree.append(rem)
@ -485,9 +488,15 @@ class Headline:
tree.append(current_node) tree.append(current_node)
# TODO: Allow indentation of this element inside others # TODO: Allow indentation of this element inside others
indentation_tree = [current_node] indentation_tree = [current_node]
if not isinstance(current_node, dom.TableNode): elif not isinstance(current_node, dom.TableNode):
if not isinstance(current_node, dom.TableNode): if isinstance(current_node, dom.ListGroupNode):
logging.warning("Expected a {}, found: {} on line {}".format(dom.TableNode, current_node, line.linenum)) # 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 # This can happen. Frequently inside a LogDrawer
if len(line.cells) > 0 and len(line.cells[0]) > 0 and line.cells[0][0] == '-': if len(line.cells) > 0 and len(line.cells[0]) > 0 and line.cells[0][0] == '-':
@ -1058,6 +1067,8 @@ def token_from_type(tok_type):
class TimeRange: class TimeRange:
def __init__(self, start_time: OrgTime, end_time: OrgTime): def __init__(self, start_time: OrgTime, end_time: OrgTime):
assert start_time is not None
assert end_time is not None
self.start_time = start_time self.start_time = start_time
self.end_time = end_time self.end_time = end_time

View File

@ -16,3 +16,18 @@
| Content2-1 | Content2-2 | Content2-3 | | Content2-1 | Content2-2 | Content2-3 |
Content after the table. 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.

View File

@ -4,7 +4,8 @@ import unittest
from datetime import date from datetime import date
from datetime import datetime as DT 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, from utils.assertions import (BOLD, CODE, HL, ITALIC, SPAN, STRIKE, UNDERLINED,
VERBATIM, WEB_LINK, Doc, Tokens) 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[1].strip(), 'Header2')
self.assertEqual(first_table[0].cells[2].strip(), 'Header3') 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 <Text><Table><Text>')
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 <List>, 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', '<NL>') + "]", type(element))
else:
print_tree(element, indentation, headline)