impl: Make get_tree() a static method & return DocumentTree

This commit is contained in:
Dendy 2024-02-24 09:37:56 +01:00
parent 1707fac972
commit 6e4ae37bea
3 changed files with 30 additions and 29 deletions

View File

@ -30,9 +30,8 @@ def index(path):
internal_path = os.path.join(data_path, path + '.md')
path = '/' + path
document_tree = files.get_tree(data_path)
for entry in document_tree:
entry.print_tree()
document_tree = files.DocumentTree.get_tree(data_path)
document_tree.print_tree()
# Checks ###################################################

View File

@ -1,4 +1,5 @@
import os
import posixpath
from typing import Self
@ -13,27 +14,28 @@ class DocumentTree():
for child in self.children:
child.print_tree(level + 1)
@staticmethod
def get_tree(base: str, path: str = '') -> "DocumentTree":
print(base, path)
children = list()
# TODO: Make this a static method or something, so we can make some sort of
# interface that calls this predefined class
def get_tree(path: str) -> list[DocumentTree]:
ret = list()
# Split and remerge for Windows
internal_path = os.path.join(base, *path.split('/'))
_, dirs, files = next(os.walk(path))
if not os.path.exists(internal_path):
raise Exception("Tried to open a folder that doesn't exist")
_, dirs, files = next(os.walk(internal_path))
# First pass, get the whole folder structure
for dir in dirs:
dir_path = os.path.join(path, dir)
ret.append(
DocumentTree(dir, get_tree(dir_path))
)
subpath = posixpath.join(path, dir)
children.append(DocumentTree.get_tree(base, subpath))
# Second pass, add the files. If it's a .md file, strip the extension,
# checking for conflicts with folders
#
# TODO: Maybe this isn't the best way to define what the text of a folder
# should be.
# TODO: Maybe this isn't the best way to define what the text of a
# folder should be.
#
# TODO: What if there's a file obscuring a folder?
# Check if the superposing is a .md file, warn the user otherwise
@ -41,7 +43,7 @@ def get_tree(path: str) -> list[DocumentTree]:
if file.endswith('.md'):
file = file[:-3]
if file not in ret:
ret.append(DocumentTree(file, []))
if file not in children:
children.append(DocumentTree(file, []))
return ret
return DocumentTree(path.split('/')[-1], children)

View File

@ -16,7 +16,7 @@
{% block body %}
<main>
<nav>
{{ render_tree(item_list) }}
{{ render_tree(item_list.children) }}
</nav>
<article>