Skip to content

Commit

Permalink
cherrytree: don't create empty notes or notebooks
Browse files Browse the repository at this point in the history
  • Loading branch information
marph91 committed Sep 28, 2024
1 parent ab742da commit 26bfb0e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/formats/cherrytree.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand Down
3 changes: 3 additions & 0 deletions src/intermediate_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 26bfb0e

Please sign in to comment.