diff --git a/src/__init__.py b/src/__init__.py index 3215cef..73362a8 100644 --- a/src/__init__.py +++ b/src/__init__.py @@ -2,6 +2,7 @@ import traceback import os +import posixpath from flask import Flask, render_template, request from pygments import formatters, highlight, lexers @@ -22,13 +23,17 @@ app = Flask( static_folder="../static", ) +app.jinja_env.globals.update( + config=config, + path_join=posixpath.join, +) + @app.route('/', defaults={'path': ''}) @app.route('/', methods=['POST', 'GET']) def index(path): data_path = config.get('data_path') internal_path = os.path.join(data_path, path + '.md') - path = '/' + path document_tree = files.DocumentTree.get_tree(data_path) document_tree.print_tree() @@ -40,12 +45,12 @@ def index(path): # If the user sent input, save it ########################## - if path == '/': + if path == '': return ( render_template( "main.html", path=path, - item_list=document_tree, + document_tree=document_tree, markdown=markdown.markdown, content='Welcome', edit=False, @@ -60,7 +65,7 @@ def index(path): # Get the document contents ################################ - is_edit = 'edit' in request.args and path != '/' + is_edit = 'edit' in request.args and path != '' if os.path.exists(internal_path): with open(internal_path) as file: @@ -83,7 +88,7 @@ def index(path): render_template( "main.html", path=path, - item_list=document_tree, + document_tree=document_tree, markdown=markdown.markdown, content=raw_markdown, edit=is_edit, diff --git a/src/files.py b/src/files.py index a7a6aad..00cc8b6 100644 --- a/src/files.py +++ b/src/files.py @@ -14,9 +14,27 @@ class DocumentTree(): for child in self.children: child.print_tree(level + 1) + def get_child(self, path: str) -> Self | None: + # TODO: Handle paths like "a//b" + # TODO: Handle paths like "/" + + # Stop condition, we found it + if path == '': + return self + + # Go recursively through each link in the chain + components = path.split('/') + for child in self.children: + if child.name == components[0]: + # If there's just one left '' will be sent + subpath = '/'.join(components[1:]) + return child.get_child(subpath) + + # A link in the chain doesn't exist + return None + @staticmethod def get_tree(base: str, path: str = '') -> "DocumentTree": - print(base, path) children = list() # Split and remerge for Windows diff --git a/templates/main.html b/templates/main.html index 7a965e0..e49840e 100644 --- a/templates/main.html +++ b/templates/main.html @@ -6,8 +6,9 @@ @@ -16,18 +17,18 @@ {% block body %}
{% if not edit %} + {% if path != '' %} + Edit + {% endif %} + {% autoescape false %} {{ markdown(content, extensions=['extra']) }} {% endautoescape %} - - {% if path != '/' %} - Edit - {% endif %} {% else %}
@@ -56,6 +57,18 @@ }); {% endif %} + +
+ + {% for child in document_tree.get_child(path).children %} + + {% if loop.first %} +

Children

+ {% endif %} + + {{child.name}}{% if not loop.last %},{% endif %} + {% endfor %} +