Compare commits

..

No commits in common. "2a45ad1a1bca95ed35edb6a5027a74a1128a6dbb" and "6cddcbbc1bcd6b1665b68dcf30f3c492563ca35e" have entirely different histories.

3 changed files with 23 additions and 70 deletions

View File

@ -3,15 +3,7 @@ Logic for managing documents.
""" """
import os import os
from redstring.parser import ( from redstring.parser import load, DocumentTag, DocumentTab, Document, TabOptions, TagOptions
Document,
DocumentTab,
DocumentTag,
DocumentSubtag,
load,
TabOptions,
TagOptions,
)
def generate_index_document(directory: str) -> Document: def generate_index_document(directory: str) -> Document:
@ -20,48 +12,31 @@ def generate_index_document(directory: str) -> Document:
""" """
categories: dict = {} categories: dict = {}
# categories = {
# tab_name: {
# tag_name: {
# 'documents': [ (id, title), ... ]
# 'subtags': {
# subtag_name: [ (id, title), ... ]
# }
# }
# }
# }
for filename in os.listdir(directory): for filename in os.listdir(directory):
with open(os.path.join(directory, filename)) as f: with open(os.path.join(directory, filename)) as f:
document: Document = load(f) document: Document = load(f)
# Check if this document specifies a tab, and create it if necessary. # Check if this document specifies a tab, and create it if necessary.
if category_tag := document.get_tag('category'): if category_tag := document.get_tag('category'):
category_name = category_tag.value category = category_tag.value
else: else:
category_name = 'index' category = 'index'
if category_name not in categories: if category not in categories:
categories[category_name] = {} categories[category] = {}
destination_category = categories[category_name] category_tab = categories[category]
# Check if this document specifies a topic, and create it if necessary. # Check if this document specifies a topic, and create it if necessary.
if topic_tag := document.get_tag('topic'): if topic_tag := document.get_tag('topic'):
topic_name, subtopic_name = topic_tag.value, None topic = topic_tag.value
else: else:
topic_name, subtopic_name = 'uncategorized', None topic = 'uncategorized'
if '.' in topic_name: if '.' in topic:
topic_name, subtopic_name = topic_name.split('.', maxsplit=1) topic, subtopic = topic.split('.', maxsplit=1)
if topic_name not in destination_category:
destination_category[topic_name] = {
'documents': [],
'subtopics': {},
}
if subtopic_name:
if subtopic_name not in destination_category[topic_name]['subtopics']:
destination_category[topic_name]['subtopics'][subtopic_name] = []
destination_topic = destination_category[topic_name]['subtopics'][subtopic_name]
else: else:
destination_topic = destination_category[topic_name]['documents'] subtopic = None
if topic not in category_tab:
category_tab[topic] = []
topic_tag = category_tab[topic]
# Save the title and id. # Save the title and id.
doc_id = document.get_tag('id').value doc_id = document.get_tag('id').value
@ -70,7 +45,7 @@ def generate_index_document(directory: str) -> Document:
else: else:
doc_title = None doc_title = None
destination_topic.append((doc_id, doc_title)) topic_tag.append((doc_id, doc_title))
# Build an index document # Build an index document
def document_link(info): def document_link(info):
@ -82,24 +57,16 @@ def generate_index_document(directory: str) -> Document:
) )
built_tabs: list = [] built_tabs: list = []
for category_name, category in sorted(categories.items()): for category in sorted(categories.keys()):
built_tags: list = [] built_tags: list = []
for topic_name, topic in sorted(category.items()):
docs = sorted(topic['documents'], key=lambda x: x[0]) for topic in sorted(categories[category].keys()):
docs = sorted(categories[category][topic], key=lambda x: x[0])
doc_links = map(document_link, docs) doc_links = map(document_link, docs)
value = '- ' + '<br>- '.join(doc_links) if docs else '' value = '- ' + '<br>- '.join(doc_links)
built_tags.append(DocumentTag(topic, value))
built_subtags: list = [] built_tabs.append(DocumentTab(category, built_tags))
for subtopic_name, subtopic in sorted(topic['subtopics'].items()):
docs = sorted(subtopic, key = lambda x: x[0])
doc_links = map(document_link, docs)
subtag_value = '- ' + '<br>- '.join(doc_links) if docs else ''
built_subtags.append(DocumentSubtag(subtopic_name, subtag_value))
built_tags.append(DocumentTag(topic_name, value, subtags=built_subtags))
built_tabs.append(DocumentTab(category_name, built_tags))
return Document(built_tabs) return Document(built_tabs)

View File

@ -6,7 +6,6 @@ import json
import logging import logging
import os import os
import random import random
import re
import string import string
from flask import ( from flask import (
@ -45,14 +44,6 @@ def inject_edit():
return {'edit': current_app.config['edit']} return {'edit': current_app.config['edit']}
@app.template_filter('interlink')
def interlink_filter(value):
return re.sub(
r'\[\[([^|]+)\|([^|]+)\]\]',
lambda match: f'<a href="{match[2]}">{match[1]}</a>',
value)
@app.route('/', methods=['GET']) @app.route('/', methods=['GET'])
def root(): def root():
return redirect(url_for('index')) return redirect(url_for('index'))

View File

@ -63,11 +63,10 @@ window.onload = function () {
</div> </div>
{% endmacro %} {% endmacro %}
{# TODO: tag.interlink #}
{% macro make_tag_value(tag) -%} {% macro make_tag_value(tag) -%}
{%- if tag.options.hyperlink -%} {%- if tag.options.hyperlink -%}
<a href="{{ tag.value }}">{{ tag.value }}</a> <a href="{{ tag.value }}">{{ tag.value }}</a>
{%- elif tag.options.interlink -%}
{{ tag.value|interlink }}
{%- else -%} {%- else -%}
{{ tag.value }} {{ tag.value }}
{%- endif -%} {%- endif -%}
@ -77,10 +76,6 @@ window.onload = function () {
{% block page_content %} {% block page_content %}
<div id="tabs"> <div id="tabs">
{%- if index and edit -%}
<div id="new-document" class="tab"><a href="/new/">new</a></div>
{%- endif -%}
{%- for tab in document -%} {%- for tab in document -%}
{%- if not tab.options.private or edit-%} {%- if not tab.options.private or edit-%}
{{ make_content_tab(tab, loop.first) }} {{ make_content_tab(tab, loop.first) }}