From a9de529c2bc9034ec7282ab42865053a89cd80d0 Mon Sep 17 00:00:00 2001 From: Dendy Faist Date: Sat, 24 Feb 2024 10:52:36 +0100 Subject: [PATCH] impl: Update DocumentTree to have get_node() --- src/__init__.py | 4 ++-- src/files.py | 21 +++++++++++++++------ templates/main.html | 4 ++-- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/__init__.py b/src/__init__.py index 73362a8..51f948e 100644 --- a/src/__init__.py +++ b/src/__init__.py @@ -35,8 +35,8 @@ def index(path): data_path = config.get('data_path') internal_path = os.path.join(data_path, path + '.md') - document_tree = files.DocumentTree.get_tree(data_path) - document_tree.print_tree() + document_tree = files.DocumentTree(data_path) + document_tree.root.print_tree() # Checks ################################################### diff --git a/src/files.py b/src/files.py index 00cc8b6..1388897 100644 --- a/src/files.py +++ b/src/files.py @@ -3,8 +3,8 @@ import posixpath from typing import Self -class DocumentTree(): - def __init__(self, name: str, children: list[Self]): +class DocumentNode(): + def __init__(self, name: str, children: list): self.name = name self.children = children @@ -34,7 +34,7 @@ class DocumentTree(): return None @staticmethod - def get_tree(base: str, path: str = '') -> "DocumentTree": + def get_tree(base: str, path: str) -> "DocumentNode": children = list() # Split and remerge for Windows @@ -47,7 +47,7 @@ class DocumentTree(): for dir in dirs: subpath = posixpath.join(path, dir) - children.append(DocumentTree.get_tree(base, subpath)) + children.append(DocumentNode.get_tree(base, subpath)) # Second pass, add the files. If it's a .md file, strip the extension, # checking for conflicts with folders @@ -62,6 +62,15 @@ class DocumentTree(): file = file[:-3] if file not in children: - children.append(DocumentTree(file, [])) + children.append(DocumentNode(file, [])) - return DocumentTree(path.split('/')[-1], children) + return DocumentNode(path.split('/')[-1], children) + + +class DocumentTree(): + def __init__(self, path: str): + self.path = path + self.root = DocumentNode.get_tree(path, '') + + def get_node(self, path: str) -> DocumentNode | None: + return self.root.get_child(path) diff --git a/templates/main.html b/templates/main.html index e49840e..434d22f 100644 --- a/templates/main.html +++ b/templates/main.html @@ -17,7 +17,7 @@ {% block body %}
@@ -60,7 +60,7 @@
- {% for child in document_tree.get_child(path).children %} + {% for child in document_tree.get_node(path).children %} {% if loop.first %}

Children