Support update of content inside lists.

This commit is contained in:
Sergio Martínez Portela 2021-08-04 00:06:18 +02:00
parent 7e944bcb3d
commit a164653cd0

View File

@ -111,15 +111,19 @@ class RangeInRaw:
self._end_id = id(end_token)
def update_range(self, new_contents):
contents = self._content
if isinstance(self._content, Text):
contents = self._content.contents
# Find start token
for start_idx, tok in enumerate(self._content.contents):
for start_idx, tok in enumerate(contents):
if id(tok) == self._start_id:
break
else:
raise Exception("Start token not found")
# Find end token
for offset, tok in enumerate(self._content.contents[start_idx:]):
for offset, tok in enumerate(contents[start_idx:]):
if id(tok) == self._end_id:
break
else:
@ -127,11 +131,11 @@ class RangeInRaw:
# Remove old contents
for i in range(1, offset):
self._content.contents.pop(start_idx + 1)
contents.pop(start_idx + 1)
# Add new ones
for i, element in enumerate(new_contents):
self._content.contents.insert(start_idx + i + 1, element)
contents.insert(start_idx + i + 1, element)
def get_links_from_content(content):
@ -884,8 +888,12 @@ class Text:
return "{{Text line: {}; content: {} }}".format(self.linenum, self.contents)
def get_raw(self):
return token_list_to_raw(self.contents)
def token_list_to_raw(tok_list):
contents = []
for chunk in self.contents:
for chunk in tok_list:
if isinstance(chunk, str):
contents.append(chunk)
elif isinstance(chunk, LinkToken):
@ -1197,7 +1205,14 @@ def dump_contents(raw):
return (raw.linenum, raw.line)
elif isinstance(raw, ListItem):
return (raw.linenum, raw.match.group(0))
bullet = raw.bullet if raw.bullet else raw.counter + raw.counter_sep
content = token_list_to_raw(raw.content)
checkbox = f"[{raw.checkbox_value}]" if raw.checkbox_value else ""
tag = f"{raw.tag_indentation}{raw.tag}::" if raw.tag else ""
return (
raw.linenum,
f"{raw.indentation}{bullet}{checkbox}{tag}{content}",
)
return (raw.linenum, raw.get_raw())