diff --git a/org_rw/org_rw.py b/org_rw/org_rw.py index f074f8a..c8836e1 100644 --- a/org_rw/org_rw.py +++ b/org_rw/org_rw.py @@ -820,6 +820,17 @@ class Headline: raise NotImplementedError() def update_raw_contents(self, new_contents): + # Clear elements + self.keywords = [] + self.contents = [] + self.list_items = [] + self.table_rows = [] + self.properties = [] + self.structural = [] + self.delimiters = [] + self.scheduled = None + self.deadline = None + self.closed = None reader = OrgDocReader(environment=self.doc.environment) reader.read(new_contents) @@ -829,19 +840,20 @@ class Headline: # Probably can be done by just adding the headlines to this one's children raise NotImplementedError('new headlines on raw contents not supported yet. This probably should be simple, see comment on code.') - # Clear elements - self.keywords = reader.keywords - self.contents = reader.contents - self.list_items = reader.list_items - self.table_rows = reader.table_rows - self.properties = reader.properties - self.structural = reader.structural - self.delimiters = reader.delimiters + for kw in reader.keywords: + self.keywords.append(offset_linenum(self.start_line + 1, kw)) - # TODO: Support update of scheduled/deadline/closed line - self.scheduled = None - self.deadline = None - self.closed = None + for content in reader.contents: + self.contents.append(offset_linenum(self.start_line + 1, content)) + + for list_item in reader.list_items: + self.list_items.append(offset_linenum(self.start_line + 1, list_item)) + + for struct_item in reader.structural: + self.structural.append(offset_linenum(self.start_line + 1, struct_item)) + + for prop in reader.properties: + self.properties.append(offset_linenum(self.start_line + 1, prop)) # Environment is not used, as it's known @@ -1014,6 +1026,9 @@ Keyword = collections.namedtuple( Property = collections.namedtuple( "Property", ("linenum", "match", "key", "value", "options") ) +Structural = collections.namedtuple( + "Structural", ("linenum", "line") +) class ListItem: @@ -1062,6 +1077,17 @@ TableRow = collections.namedtuple( ), ) +ItemWithLineNum = Union[Keyword, RawLine, Property, ListItem, Structural] +def offset_linenum(offset: int, item: ItemWithLineNum) -> ItemWithLineNum: + if isinstance(item, ListItem): + item.linenum += offset + return item + + assert isinstance(item, (Keyword, RawLine, Property, Structural)), \ + "Expected (Keyword|RawLine|Property|Structural), found {}".format(item) + return item._replace(linenum=item.linenum + offset) + + # @TODO How are [YYYY-MM-DD HH:mm--HH:mm] and ([... HH:mm]--[... HH:mm]) differentiated ? # @TODO Consider recurrence annotations class Timestamp: @@ -2439,8 +2465,8 @@ class OrgDocReader: self.delimiters: List[DelimiterLine] = [] self.list_items: List[ListItem] = [] self.table_rows: List[TableRow] = [] - self.structural: List = [] - self.properties: List = [] + self.structural: List[Structural] = [] + self.properties: List[Property] = [] self.current_drawer: Optional[List] = None self.environment = environment @@ -2622,7 +2648,7 @@ class OrgDocReader: def add_property_drawer_line(self, linenum: int, line: str, match: re.Match): if len(self.headline_hierarchy) == 0: self.current_drawer = self.properties - self.structural.append((linenum, line)) + self.structural.append(Structural(linenum, line)) else: assert self.headline_hierarchy[-1] is not None self.current_drawer = self.headline_hierarchy[-1]["properties"] @@ -2641,7 +2667,7 @@ class OrgDocReader: def add_drawer_end_line(self, linenum: int, line: str, match: re.Match): self.current_drawer = None if len(self.headline_hierarchy) == 0: - self.structural.append((linenum, line)) + self.structural.append(Structural(linenum, line)) else: assert self.headline_hierarchy[-1] is not None self.headline_hierarchy[-1]["structural"].append((linenum, line))