diff --git a/src/__init__.py b/src/__init__.py index 8f7ff6f..3215cef 100644 --- a/src/__init__.py +++ b/src/__init__.py @@ -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 ################################################### diff --git a/src/files.py b/src/files.py index 145cf55..a7a6aad 100644 --- a/src/files.py +++ b/src/files.py @@ -1,4 +1,5 @@ import os +import posixpath from typing import Self @@ -13,35 +14,36 @@ 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") - # First pass, get the whole folder structure - for dir in dirs: - dir_path = os.path.join(path, dir) + _, dirs, files = next(os.walk(internal_path)) - ret.append( - DocumentTree(dir, get_tree(dir_path)) - ) + for dir in dirs: + 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: What if there's a file obscuring a folder? - # Check if the superposing is a .md file, warn the user otherwise - for file in files: - if file.endswith('.md'): - file = file[:-3] + # 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: What if there's a file obscuring a folder? + # Check if the superposing is a .md file, warn the user otherwise + for file in files: + 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) diff --git a/templates/main.html b/templates/main.html index 6d08967..7a965e0 100644 --- a/templates/main.html +++ b/templates/main.html @@ -16,7 +16,7 @@ {% block body %}