Improve support for list management.

This commit is contained in:
Sergio Martínez Portela 2021-04-03 00:59:23 +02:00
parent 2d7e7f23ed
commit e4b3d42319
2 changed files with 46 additions and 7 deletions

View File

@ -1251,7 +1251,7 @@ def parse_headline(hl, doc, parent) -> Headline:
class OrgDoc: class OrgDoc:
def __init__(self, headlines, keywords, contents): def __init__(self, headlines, keywords, contents, list_items):
self.todo_keywords = DEFAULT_TODO_KEYWORDS self.todo_keywords = DEFAULT_TODO_KEYWORDS
self.done_keywords = DEFAULT_DONE_KEYWORDS self.done_keywords = DEFAULT_DONE_KEYWORDS
@ -1264,6 +1264,7 @@ class OrgDoc:
self.keywords: List[Property] = keywords self.keywords: List[Property] = keywords
self.contents: List[RawLine] = contents self.contents: List[RawLine] = contents
self.list_items: List[ListItem] = list_items
self._path = None self._path = None
self.headlines: List[Headline] = list( self.headlines: List[Headline] = list(
map(lambda hl: parse_headline(hl, self, self), headlines) map(lambda hl: parse_headline(hl, self, self), headlines)
@ -1317,9 +1318,15 @@ class OrgDoc:
) )
def dump_property(self, prop: Property): def dump_property(self, prop: Property):
plus = prop.match.group("plus") plus = ""
if plus is None: indentation = ""
plus = "" spacing = " "
if prop.match is not None:
plus = prop.match.group("plus")
if plus is None:
plus = ""
indentation = prop.match.group("indentation")
spacing = prop.match.group("spacing")
if isinstance(prop.value, TimeRange): if isinstance(prop.value, TimeRange):
value = timerange_to_string(prop.value) value = timerange_to_string(prop.value)
@ -1331,10 +1338,10 @@ class OrgDoc:
return ( return (
prop.linenum, prop.linenum,
"{indentation}:{key}{plus}:{spacing}{value}".format( "{indentation}:{key}{plus}:{spacing}{value}".format(
indentation=prop.match.group("indentation"), indentation=indentation,
key=prop.key, key=prop.key,
plus=plus, plus=plus,
spacing=prop.match.group("spacing"), spacing=spacing,
value=value, value=value,
), ),
) )
@ -1435,6 +1442,9 @@ class OrgDoc:
for line in self.contents: for line in self.contents:
lines.append(dump_contents(line)) lines.append(dump_contents(line))
for li in self.list_items:
lines.append(dump_contents(li))
yield from map(lambda x: x[1], sorted(lines, key=lambda x: x[0])) yield from map(lambda x: x[1], sorted(lines, key=lambda x: x[0]))
for headline in self.headlines: for headline in self.headlines:
@ -1451,7 +1461,7 @@ class OrgDocReader:
self.list_items: List[ListItem] = [] self.list_items: List[ListItem] = []
def finalize(self): def finalize(self):
return OrgDoc(self.headlines, self.keywords, self.contents) return OrgDoc(self.headlines, self.keywords, self.contents, self.list_items)
## Construction ## Construction
def add_headline(self, linenum: int, match: re.Match) -> int: def add_headline(self, linenum: int, match: re.Match) -> int:

View File

@ -459,3 +459,32 @@ class TestSerde(unittest.TestCase):
# ... # ...
lists = hl.getLists() lists = hl.getLists()
self.assertEqual(len(lists), 3) self.assertEqual(len(lists), 3)
self.assertEqual(lists[0][0].content, " This is a simple list.")
self.assertEqual(lists[0][0].bullet, "-")
self.assertEqual(
lists[0][1].content, " This list has multiple elements, with _markup_."
)
self.assertEqual(lists[1][0].content, " This is a simple list.")
self.assertEqual(lists[1][0].bullet, "+")
hl2 = doc.getTopHeadlines()[1]
# ...
lists2 = hl2.getLists()
self.assertEqual(len(lists2), 2)
self.assertEqual(lists2[0][0].content, " First element")
self.assertEqual(lists2[0][0].counter, "1")
self.assertEqual(lists2[0][0].counter_sep, ".")
self.assertEqual(lists2[0][1].content, " Second element")
self.assertEqual(lists2[0][1].counter, "2")
self.assertEqual(lists2[0][1].counter_sep, ".")
self.assertEqual(lists2[1][0].content, " First element")
self.assertEqual(lists2[1][0].counter, "1")
self.assertEqual(lists2[1][0].counter_sep, ")")
self.assertEqual(lists2[1][1].content, " Second element")
self.assertEqual(lists2[1][1].counter, "2")
self.assertEqual(lists2[1][1].counter_sep, ")")