impl: Make get_tree() a static method & return DocumentTree
This commit is contained in:
parent
1707fac972
commit
6e4ae37bea
|
@ -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 ###################################################
|
||||
|
||||
|
|
52
src/files.py
52
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)
|
||||
|
|
|
@ -16,7 +16,7 @@
|
|||
{% block body %}
|
||||
<main>
|
||||
<nav>
|
||||
{{ render_tree(item_list) }}
|
||||
{{ render_tree(item_list.children) }}
|
||||
</nav>
|
||||
|
||||
<article>
|
||||
|
|
Loading…
Reference in New Issue