Better document retrieval, separate into module

Added a class to represent the structure
This commit is contained in:
Dendy 2024-02-24 08:33:32 +01:00
parent 4a525457a6
commit 1707fac972
3 changed files with 109 additions and 85 deletions

View File

@ -10,6 +10,7 @@ import markdown
from src import ( from src import (
utils, utils,
config, config,
files,
) )
# TODO: Put the creation of the Flask application into a function # 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('/', defaults={'path': ''})
@app.route('/<path:path>', methods=['POST', 'GET']) @app.route('/<path:path>', methods=['POST', 'GET'])
def index(path): 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 path = '/' + path
document_tree = files.get_tree(data_path)
for entry in document_tree:
entry.print_tree()
# Checks ################################################### # Checks ###################################################
if '..' in path: if '..' in path:
@ -68,7 +46,7 @@ def index(path):
render_template( render_template(
"main.html", "main.html",
path=path, path=path,
item_list=get_tree(), item_list=document_tree,
markdown=markdown.markdown, markdown=markdown.markdown,
content='Welcome', content='Welcome',
edit=False, edit=False,
@ -106,7 +84,7 @@ def index(path):
render_template( render_template(
"main.html", "main.html",
path=path, path=path,
item_list=get_tree(), item_list=document_tree,
markdown=markdown.markdown, markdown=markdown.markdown,
content=raw_markdown, content=raw_markdown,
edit=is_edit, edit=is_edit,

47
src/files.py Normal file
View File

@ -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

View File

@ -4,11 +4,10 @@
{% macro render_tree(tree, root='/') %} {% macro render_tree(tree, root='/') %}
<ul> <ul>
{% for key, value in tree.items() %} {% for entry in tree %}
<li><a href="{{ root }}{{ key }}">{{ key }}</a> <li>
{% if value is mapping %} <a href="{{ root }}{{ entry.name }}">{{ entry.name }}</a>
{{ render_tree(value, root + key + '/') }} {{ render_tree(entry.children, root + entry.name + '/') }}
{% endif %}
</li> </li>
{% endfor %} {% endfor %}
</ul> </ul>