impl: Children footer in article

This commit is contained in:
Dendy 2024-02-24 10:35:36 +01:00
parent 6e4ae37bea
commit a5b93eee90
3 changed files with 49 additions and 13 deletions

View File

@ -2,6 +2,7 @@
import traceback import traceback
import os import os
import posixpath
from flask import Flask, render_template, request from flask import Flask, render_template, request
from pygments import formatters, highlight, lexers from pygments import formatters, highlight, lexers
@ -22,13 +23,17 @@ app = Flask(
static_folder="../static", static_folder="../static",
) )
app.jinja_env.globals.update(
config=config,
path_join=posixpath.join,
)
@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):
data_path = config.get('data_path') data_path = config.get('data_path')
internal_path = os.path.join(data_path, path + '.md') internal_path = os.path.join(data_path, path + '.md')
path = '/' + path
document_tree = files.DocumentTree.get_tree(data_path) document_tree = files.DocumentTree.get_tree(data_path)
document_tree.print_tree() document_tree.print_tree()
@ -40,12 +45,12 @@ def index(path):
# If the user sent input, save it ########################## # If the user sent input, save it ##########################
if path == '/': if path == '':
return ( return (
render_template( render_template(
"main.html", "main.html",
path=path, path=path,
item_list=document_tree, document_tree=document_tree,
markdown=markdown.markdown, markdown=markdown.markdown,
content='Welcome', content='Welcome',
edit=False, edit=False,
@ -60,7 +65,7 @@ def index(path):
# Get the document contents ################################ # 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): if os.path.exists(internal_path):
with open(internal_path) as file: with open(internal_path) as file:
@ -83,7 +88,7 @@ def index(path):
render_template( render_template(
"main.html", "main.html",
path=path, path=path,
item_list=document_tree, document_tree=document_tree,
markdown=markdown.markdown, markdown=markdown.markdown,
content=raw_markdown, content=raw_markdown,
edit=is_edit, edit=is_edit,

View File

@ -14,9 +14,27 @@ class DocumentTree():
for child in self.children: for child in self.children:
child.print_tree(level + 1) 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 @staticmethod
def get_tree(base: str, path: str = '') -> "DocumentTree": def get_tree(base: str, path: str = '') -> "DocumentTree":
print(base, path)
children = list() children = list()
# Split and remerge for Windows # Split and remerge for Windows

View File

@ -6,8 +6,9 @@
<ul> <ul>
{% for entry in tree %} {% for entry in tree %}
<li> <li>
<a href="{{ root }}{{ entry.name }}">{{ entry.name }}</a> {% set url = path_join(root, entry.name) %}
{{ render_tree(entry.children, root + entry.name + '/') }} <a href="{{ url }}">{{ entry.name }}</a>
{{ render_tree(entry.children, url) }}
</li> </li>
{% endfor %} {% endfor %}
</ul> </ul>
@ -16,18 +17,18 @@
{% block body %} {% block body %}
<main> <main>
<nav> <nav>
{{ render_tree(item_list.children) }} {{ render_tree(document_tree.children) }}
</nav> </nav>
<article> <article>
{% if not edit %} {% if not edit %}
{% if path != '' %}
<a href="{{ path }}?edit">Edit</a>
{% endif %}
{% autoescape false %} {% autoescape false %}
{{ markdown(content, extensions=['extra']) }} {{ markdown(content, extensions=['extra']) }}
{% endautoescape %} {% endautoescape %}
{% if path != '/' %}
<a href="{{ path }}?edit">Edit</a>
{% endif %}
{% else %} {% else %}
<form method="POST" action="{{ path }}"> <form method="POST" action="{{ path }}">
<textarea id="markdown-textarea" name="text">{{ content }}</textarea> <textarea id="markdown-textarea" name="text">{{ content }}</textarea>
@ -56,6 +57,18 @@
}); });
</script> </script>
{% endif %} {% endif %}
<hr/>
{% for child in document_tree.get_child(path).children %}
{% if loop.first %}
<h1>Children</h1>
{% endif %}
<a href="{{ path_join(path, child.name) }}">{{child.name}}</a>{% if not loop.last %},{% endif %}
{% endfor %}
</article> </article>
</main> </main>