Better document retrieval, separate into module
Added a class to represent the structure
This commit is contained in:
parent
4a525457a6
commit
1707fac972
|
@ -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('/<path:path>', 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,
|
||||
|
|
|
@ -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
|
|
@ -4,11 +4,10 @@
|
|||
|
||||
{% macro render_tree(tree, root='/') %}
|
||||
<ul>
|
||||
{% for key, value in tree.items() %}
|
||||
<li><a href="{{ root }}{{ key }}">{{ key }}</a>
|
||||
{% if value is mapping %}
|
||||
{{ render_tree(value, root + key + '/') }}
|
||||
{% endif %}
|
||||
{% for entry in tree %}
|
||||
<li>
|
||||
<a href="{{ root }}{{ entry.name }}">{{ entry.name }}</a>
|
||||
{{ render_tree(entry.children, root + entry.name + '/') }}
|
||||
</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
|
|
Loading…
Reference in New Issue