impl: Update DocumentTree to have get_node()

This commit is contained in:
Dendy 2024-02-24 10:52:36 +01:00
parent a5b93eee90
commit a9de529c2b
3 changed files with 19 additions and 10 deletions

View File

@ -35,8 +35,8 @@ def index(path):
data_path = config.get('data_path') data_path = config.get('data_path')
internal_path = os.path.join(data_path, path + '.md') internal_path = os.path.join(data_path, path + '.md')
document_tree = files.DocumentTree.get_tree(data_path) document_tree = files.DocumentTree(data_path)
document_tree.print_tree() document_tree.root.print_tree()
# Checks ################################################### # Checks ###################################################

View File

@ -3,8 +3,8 @@ import posixpath
from typing import Self from typing import Self
class DocumentTree(): class DocumentNode():
def __init__(self, name: str, children: list[Self]): def __init__(self, name: str, children: list):
self.name = name self.name = name
self.children = children self.children = children
@ -34,7 +34,7 @@ class DocumentTree():
return None return None
@staticmethod @staticmethod
def get_tree(base: str, path: str = '') -> "DocumentTree": def get_tree(base: str, path: str) -> "DocumentNode":
children = list() children = list()
# Split and remerge for Windows # Split and remerge for Windows
@ -47,7 +47,7 @@ class DocumentTree():
for dir in dirs: for dir in dirs:
subpath = posixpath.join(path, dir) 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, # Second pass, add the files. If it's a .md file, strip the extension,
# checking for conflicts with folders # checking for conflicts with folders
@ -62,6 +62,15 @@ class DocumentTree():
file = file[:-3] file = file[:-3]
if file not in children: 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)

View File

@ -17,7 +17,7 @@
{% block body %} {% block body %}
<main> <main>
<nav> <nav>
{{ render_tree(document_tree.children) }} {{ render_tree(document_tree.root.children) }}
</nav> </nav>
<article> <article>
@ -60,7 +60,7 @@
<hr/> <hr/>
{% for child in document_tree.get_child(path).children %} {% for child in document_tree.get_node(path).children %}
{% if loop.first %} {% if loop.first %}
<h1>Children</h1> <h1>Children</h1>