diff --git a/src/__init__.py b/src/__init__.py index 14aa9e6..8f7ff6f 100644 --- a/src/__init__.py +++ b/src/__init__.py @@ -10,6 +10,7 @@ import markdown from src import ( utils, config, + files, ) # TODO: Put the creation of the Flask application into a function @@ -22,40 +23,17 @@ app = Flask( ) -def get_tree() -> dict: - data_path = config.get('data_path') - ret = {} - - for root, _, files in os.walk(data_path): - current = ret - - relpath = os.path.relpath(root, data_path) - breadcrumbs = relpath.split(os.path.sep) - if breadcrumbs == ['.']: - breadcrumbs = [] - - # Move the current to that folder and create - # the intermediate steps if they don't exist - for folder in breadcrumbs: - if folder not in current or current[folder] is None: - current[folder] = {} - - current = current[folder] - - print(breadcrumbs) - for file in files: - filename, _ = os.path.splitext(file) - current[filename] = None - - return ret - - @app.route('/', defaults={'path': ''}) @app.route('/', methods=['POST', 'GET']) def index(path): - internal_path = os.path.join(config.get('data_path'), path + '.md') + data_path = config.get('data_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() + # Checks ################################################### if '..' in path: @@ -68,7 +46,7 @@ def index(path): render_template( "main.html", path=path, - item_list=get_tree(), + item_list=document_tree, markdown=markdown.markdown, content='Welcome', edit=False, @@ -106,7 +84,7 @@ def index(path): render_template( "main.html", path=path, - item_list=get_tree(), + item_list=document_tree, markdown=markdown.markdown, content=raw_markdown, edit=is_edit, diff --git a/src/files.py b/src/files.py new file mode 100644 index 0000000..145cf55 --- /dev/null +++ b/src/files.py @@ -0,0 +1,47 @@ +import os +from typing import Self + + +class DocumentTree(): + def __init__(self, name: str, children: list[Self]): + self.name = name + self.children = children + + def print_tree(self, level=0) -> None: + print('-' * level + self.name) + + for child in self.children: + child.print_tree(level + 1) + + +# 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() + + _, dirs, files = next(os.walk(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)) + ) + + # 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, [])) + + return ret diff --git a/templates/main.html b/templates/main.html index 86eddd4..6d08967 100644 --- a/templates/main.html +++ b/templates/main.html @@ -3,66 +3,65 @@ {% block title %}{{ path }}{% endblock %} {% macro render_tree(tree, root='/') %} - + {% endmacro %} {% block body %} -
- +
+ -
- {% if not edit %} - {% autoescape false %} - {{ markdown(content, extensions=['extra']) }} - {% endautoescape %} +
+ {% if not edit %} + {% autoescape false %} + {{ markdown(content, extensions=['extra']) }} + {% endautoescape %} - {% if path != '/' %} - Edit - {% endif %} - {% else %} -
- - - - + {% if path != '/' %} + Edit {% endif %} -
-
+ {% else %} + + + + + + {% endif %} + +
- {% set footer = config.get('foote') %} - {% if footer not in [None, False, ""] %} - - {% endif %} +{% set footer = config.get('foote') %} +{% if footer not in [None, False, ""] %} + +{% endif %} {% endblock %}