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 (
|
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,
|
||||||
|
|
|
@ -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
|
|
@ -3,66 +3,65 @@
|
||||||
{% block title %}{{ path }}{% endblock %}
|
{% block title %}{{ path }}{% endblock %}
|
||||||
|
|
||||||
{% 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>
|
|
||||||
{% endmacro %}
|
{% endmacro %}
|
||||||
|
|
||||||
{% block body %}
|
{% block body %}
|
||||||
<main>
|
<main>
|
||||||
<nav>
|
<nav>
|
||||||
{{ render_tree(item_list) }}
|
{{ render_tree(item_list) }}
|
||||||
</nav>
|
</nav>
|
||||||
|
|
||||||
<article>
|
<article>
|
||||||
{% if not edit %}
|
{% if not edit %}
|
||||||
{% autoescape false %}
|
{% autoescape false %}
|
||||||
{{ markdown(content, extensions=['extra']) }}
|
{{ markdown(content, extensions=['extra']) }}
|
||||||
{% endautoescape %}
|
{% endautoescape %}
|
||||||
|
|
||||||
{% if path != '/' %}
|
{% if path != '/' %}
|
||||||
<a href="{{ path }}?edit">Edit</a>
|
<a href="{{ path }}?edit">Edit</a>
|
||||||
{% endif %}
|
|
||||||
{% else %}
|
|
||||||
<form method="POST" action="{{ path }}">
|
|
||||||
<textarea id="markdown-textarea" name="text">{{ content }}</textarea>
|
|
||||||
<input type="submit" value="Save"/>
|
|
||||||
<form>
|
|
||||||
<script>
|
|
||||||
const easyMDE = new EasyMDE({
|
|
||||||
element: document.getElementById('markdown-textarea'),
|
|
||||||
autosave: {
|
|
||||||
enabled: false,
|
|
||||||
uniqueId: "whatever-i-guess",
|
|
||||||
delay: 1000,
|
|
||||||
submit_delay: 5000,
|
|
||||||
timeFormat: {
|
|
||||||
locale: 'en-US',
|
|
||||||
format: {
|
|
||||||
//year: 'numeric',
|
|
||||||
//month: 'long',
|
|
||||||
//day: '2-digit',
|
|
||||||
hour: '2-digit',
|
|
||||||
minute: '2-digit',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
text: "Autosaved: "
|
|
||||||
},
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</article>
|
{% else %}
|
||||||
</main>
|
<form method="POST" action="{{ path }}">
|
||||||
|
<textarea id="markdown-textarea" name="text">{{ content }}</textarea>
|
||||||
|
<input type="submit" value="Save"/>
|
||||||
|
<form>
|
||||||
|
<script>
|
||||||
|
const easyMDE = new EasyMDE({
|
||||||
|
element: document.getElementById('markdown-textarea'),
|
||||||
|
autosave: {
|
||||||
|
enabled: false,
|
||||||
|
uniqueId: "whatever-i-guess",
|
||||||
|
delay: 1000,
|
||||||
|
submit_delay: 5000,
|
||||||
|
timeFormat: {
|
||||||
|
locale: 'en-US',
|
||||||
|
format: {
|
||||||
|
//year: 'numeric',
|
||||||
|
//month: 'long',
|
||||||
|
//day: '2-digit',
|
||||||
|
hour: '2-digit',
|
||||||
|
minute: '2-digit',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
text: "Autosaved: "
|
||||||
|
},
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
{% endif %}
|
||||||
|
</article>
|
||||||
|
</main>
|
||||||
|
|
||||||
{% set footer = config.get('foote') %}
|
{% set footer = config.get('foote') %}
|
||||||
{% if footer not in [None, False, ""] %}
|
{% if footer not in [None, False, ""] %}
|
||||||
<footer>{{ config.get('footer') }}</footer>
|
<footer>{{ config.get('footer') }}</footer>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
Loading…
Reference in New Issue