Improve display of children list

This commit is contained in:
RunasSudo 2020-07-23 22:27:49 +10:00
parent 0196f3f86a
commit 60a60ace18
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
2 changed files with 32 additions and 19 deletions

View File

@ -31,15 +31,29 @@ if os.path.exists('index.pickle'):
print('Error loading index.pickle') print('Error loading index.pickle')
import traceback; traceback.print_exc() import traceback; traceback.print_exc()
def get_children(path):
def child_name(c):
return os.path.splitext(c)[0]
children = []
if os.path.isdir(flask.safe_join('./data/pages', path)):
for child in os.listdir(flask.safe_join('./data/pages', path)):
child_path = flask.safe_join('./data/pages', path, child)
if child.startswith('_'):
continue
if os.path.islink(child_path):
children.append((child_name(child), child_name(os.path.split(os.path.realpath(child_path))[1])))
else:
children.append((child_name(child), None))
children.sort(key=lambda x: x[0][1:] if x[0].startswith('(') else x[0])
return children
@app.route('/') @app.route('/')
def index_page(): def index_page():
children = [] children = get_children('')
for child in os.listdir('./data/pages'):
if child.endswith('.md'):
children.append(child[:-3])
else:
children.append(child)
children.sort()
if not os.path.exists('./data/pages/Home.md'): if not os.path.exists('./data/pages/Home.md'):
return flask.render_template('page_404.html', page={ return flask.render_template('page_404.html', page={
@ -67,16 +81,11 @@ def index_page():
def page_view(path): def page_view(path):
fname = flask.safe_join('./data/pages', path) + '.md' fname = flask.safe_join('./data/pages', path) + '.md'
children = [] if os.path.islink(fname):
if os.path.isdir(flask.safe_join('./data/pages', path)): redir_page = '/'.join(path.split('/')[:-1]) + '/' + os.path.splitext(os.readlink(fname))[0]
for child in os.listdir(flask.safe_join('./data/pages', path)): return flask.redirect(flask.url_for('page_view', path=redir_page))
if child.startswith('_'):
continue children = get_children(path)
if child.endswith('.md'):
children.append(child[:-3])
else:
children.append(child)
children.sort()
if not os.path.exists(fname): if not os.path.exists(fname):
return flask.render_template('page_404.html', page={ return flask.render_template('page_404.html', page={

View File

@ -27,8 +27,12 @@
<h2>Children</h2> <h2>Children</h2>
<ul> <ul>
{% for child in page.children %} {% for child, link in page.children %}
<li><a href="{{ url_for('page_view', path=page.path + '/' + child) }}">{{ child }}</a></li> {% if link %}
<li><a href="{{ url_for('page_view', path=page.path + '/' + child) }}">{{ child }} <i>(→ {{ link }})</i></a></li>
{% else %}
<li><a href="{{ url_for('page_view', path=page.path + '/' + child) }}">{{ child }}</a></li>
{% endif %}
{% endfor %} {% endfor %}
</ul> </ul>
{% endif %} {% endif %}