From 26bfb0e6112b3a252ef1e77632d1b0ba6d3832c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20D=C3=B6rfelt?= Date: Sat, 28 Sep 2024 19:52:55 +0200 Subject: [PATCH] cherrytree: don't create empty notes or notebooks --- src/formats/cherrytree.py | 9 ++++++++- src/intermediate_format.py | 3 +++ test/data | 2 +- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/formats/cherrytree.py b/src/formats/cherrytree.py index 5e7c21d..3ca3343 100644 --- a/src/formats/cherrytree.py +++ b/src/formats/cherrytree.py @@ -191,6 +191,9 @@ def convert_to_markdown(self, node, root_notebook): f"parent: {root_notebook.title}" ) self.convert_to_markdown(child, new_root_notebook) + if new_root_notebook.is_empty(): + # Delete the notebook if it's empty. + del root_notebook.child_notebooks[-1] case "codebox": language = child.attrib.get("syntax_highlighting", "") note_body += f"\n```{language}\n{child.text}\n```\n" @@ -238,7 +241,11 @@ def convert_to_markdown(self, node, root_notebook): note_imf.created = dt.datetime.utcfromtimestamp(int(created_time)) if (updated_time := node.attrib.get("ts_lastsave")) is not None: note_imf.updated = dt.datetime.utcfromtimestamp(int(updated_time)) - root_notebook.child_notes.append(note_imf) + + # If the cherrytree node is only used to contain children (i. e. a folder), + # don't create a superfluous empty note. + if not note_imf.is_empty(): + root_notebook.child_notes.append(note_imf) def convert(self, file_or_folder: Path): self.root_path = common.get_temp_folder() diff --git a/src/intermediate_format.py b/src/intermediate_format.py index 7155eae..2e8297a 100644 --- a/src/intermediate_format.py +++ b/src/intermediate_format.py @@ -96,6 +96,9 @@ def time_from_file(self, file_: Path): self.created = dt.datetime.utcfromtimestamp(file_.stat().st_ctime) self.updated = dt.datetime.utcfromtimestamp(file_.stat().st_mtime) + def is_empty(self) -> bool: + return not self.body.strip() and not self.tags and not self.resources + @dataclass class Notebook: diff --git a/test/data b/test/data index 48681e6..ccdade5 160000 --- a/test/data +++ b/test/data @@ -1 +1 @@ -Subproject commit 48681e6c085be929ce18097520d4094966f67a0c +Subproject commit ccdade5bcce071f08cfd3abc54b52f63a0f3824d