impl: Children footer in article
This commit is contained in:
parent
6e4ae37bea
commit
a5b93eee90
|
@ -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('/<path:path>', 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,
|
||||
|
|
20
src/files.py
20
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
|
||||
|
|
|
@ -6,8 +6,9 @@
|
|||
<ul>
|
||||
{% for entry in tree %}
|
||||
<li>
|
||||
<a href="{{ root }}{{ entry.name }}">{{ entry.name }}</a>
|
||||
{{ render_tree(entry.children, root + entry.name + '/') }}
|
||||
{% set url = path_join(root, entry.name) %}
|
||||
<a href="{{ url }}">{{ entry.name }}</a>
|
||||
{{ render_tree(entry.children, url) }}
|
||||
</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
|
@ -16,18 +17,18 @@
|
|||
{% block body %}
|
||||
<main>
|
||||
<nav>
|
||||
{{ render_tree(item_list.children) }}
|
||||
{{ render_tree(document_tree.children) }}
|
||||
</nav>
|
||||
|
||||
<article>
|
||||
{% if not edit %}
|
||||
{% if path != '' %}
|
||||
<a href="{{ path }}?edit">Edit</a>
|
||||
{% endif %}
|
||||
|
||||
{% autoescape false %}
|
||||
{{ markdown(content, extensions=['extra']) }}
|
||||
{% endautoescape %}
|
||||
|
||||
{% if path != '/' %}
|
||||
<a href="{{ path }}?edit">Edit</a>
|
||||
{% endif %}
|
||||
{% else %}
|
||||
<form method="POST" action="{{ path }}">
|
||||
<textarea id="markdown-textarea" name="text">{{ content }}</textarea>
|
||||
|
@ -56,6 +57,18 @@
|
|||
});
|
||||
</script>
|
||||
{% 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>
|
||||
</main>
|
||||
|
||||
|
|
Loading…
Reference in New Issue