From 2a45ad1a1bca95ed35edb6a5027a74a1128a6dbb Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Sun, 21 Feb 2021 20:08:57 -0800 Subject: [PATCH] Support subtopics as subtags in index --- redstring/library.py | 77 +++++++++++++++++++++++++++++++------------- 1 file changed, 55 insertions(+), 22 deletions(-) diff --git a/redstring/library.py b/redstring/library.py index d74d325..a3eecbb 100644 --- a/redstring/library.py +++ b/redstring/library.py @@ -3,7 +3,15 @@ Logic for managing documents. """ import os -from redstring.parser import load, DocumentTag, DocumentTab, Document, TabOptions, TagOptions +from redstring.parser import ( + Document, + DocumentTab, + DocumentTag, + DocumentSubtag, + load, + TabOptions, + TagOptions, +) def generate_index_document(directory: str) -> Document: @@ -12,31 +20,48 @@ def generate_index_document(directory: str) -> Document: """ categories: dict = {} + # categories = { + # tab_name: { + # tag_name: { + # 'documents': [ (id, title), ... ] + # 'subtags': { + # subtag_name: [ (id, title), ... ] + # } + # } + # } + # } + for filename in os.listdir(directory): with open(os.path.join(directory, filename)) as f: document: Document = load(f) # Check if this document specifies a tab, and create it if necessary. if category_tag := document.get_tag('category'): - category = category_tag.value + category_name = category_tag.value else: - category = 'index' - if category not in categories: - categories[category] = {} - category_tab = categories[category] + category_name = 'index' + if category_name not in categories: + categories[category_name] = {} + destination_category = categories[category_name] # Check if this document specifies a topic, and create it if necessary. if topic_tag := document.get_tag('topic'): - topic = topic_tag.value + topic_name, subtopic_name = topic_tag.value, None else: - topic = 'uncategorized' - if '.' in topic: - topic, subtopic = topic.split('.', maxsplit=1) + topic_name, subtopic_name = 'uncategorized', None + if '.' in topic_name: + topic_name, subtopic_name = topic_name.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: - subtopic = None - if topic not in category_tab: - category_tab[topic] = [] - topic_tag = category_tab[topic] + destination_topic = destination_category[topic_name]['documents'] # Save the title and id. doc_id = document.get_tag('id').value @@ -45,7 +70,7 @@ def generate_index_document(directory: str) -> Document: else: doc_title = None - topic_tag.append((doc_id, doc_title)) + destination_topic.append((doc_id, doc_title)) # Build an index document def document_link(info): @@ -57,16 +82,24 @@ def generate_index_document(directory: str) -> Document: ) built_tabs: list = [] - for category in sorted(categories.keys()): + for category_name, category in sorted(categories.items()): + built_tags: list = [] - - for topic in sorted(categories[category].keys()): - docs = sorted(categories[category][topic], key=lambda x: x[0]) + for topic_name, topic in sorted(category.items()): + docs = sorted(topic['documents'], key=lambda x: x[0]) doc_links = map(document_link, docs) - value = '- ' + '
- '.join(doc_links) - built_tags.append(DocumentTag(topic, value)) + value = '- ' + '
- '.join(doc_links) if docs else '' - built_tabs.append(DocumentTab(category, built_tags)) + built_subtags: list = [] + 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 = '- ' + '
- '.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)