Add index sorting and phantom styling on contents page

This commit is contained in:
Tim Van Baak 2020-04-30 01:24:31 -07:00
parent 2494d09c40
commit 78477efba6
4 changed files with 69 additions and 16 deletions

View File

@ -8,6 +8,7 @@ from .gameloop import (
get_draft, get_draft,
title_constraint_analysis, title_constraint_analysis,
content_constraint_analysis, content_constraint_analysis,
sort_by_index_spec,
attempt_publish) attempt_publish)
from .setup import ( from .setup import (
player_can_join_lexicon, player_can_join_lexicon,
@ -23,6 +24,7 @@ __all__ = [member.__name__ for member in [
get_draft, get_draft,
title_constraint_analysis, title_constraint_analysis,
content_constraint_analysis, content_constraint_analysis,
sort_by_index_spec,
attempt_publish, attempt_publish,
player_can_join_lexicon, player_can_join_lexicon,
add_player_to_lexicon, add_player_to_lexicon,

View File

@ -2,6 +2,7 @@
Submodule of functions for managing lexicon games during the core game Submodule of functions for managing lexicon games during the core game
loop of writing and publishing articles. loop of writing and publishing articles.
""" """
from collections import OrderedDict
from typing import Iterable, Any, List, Optional, Tuple from typing import Iterable, Any, List, Optional, Tuple
from amanuensis.config import ReadOnlyOrderedDict from amanuensis.config import ReadOnlyOrderedDict
@ -163,6 +164,52 @@ def content_constraint_analysis(
return infos, warnings, errors return infos, warnings, errors
def index_match(index, title) -> bool:
if index.type == 'char':
return titlesort(title)[0].upper() in index.pattern.upper()
if index.type == 'prefix':
return title.startswith(index.pattern)
if index.type == 'etc':
return True
raise ValueError(f'Unknown index type: "{index.type}"')
def sort_by_index_spec(articles, index_specs, key=None):
"""
Sorts a list under the appropriate index in the given index
specification list. If the list is not a list of titles, the key
function should map the contents to the indexable strings.
"""
if key is None:
def key(k):
return k
# Determine the index evaluation order vs list order
index_by_pri = {}
index_list_order = []
for index in index_specs:
if index.pri not in index_by_pri:
index_by_pri[index.pri] = []
index_by_pri[index.pri].append(index)
index_list_order.append(index)
index_eval_order = [
index
for pri in sorted(index_by_pri.keys(), reverse=True)
for index in index_by_pri[pri]]
print(str(articles)[:200])
articles_titlesorted = sorted(
articles,
key=lambda a: titlesort(key(a)))
print(str(articles_titlesorted)[:200])
indexed = OrderedDict()
for index in index_list_order:
indexed[index.pattern] = []
for article in articles_titlesorted:
for index in index_eval_order:
if index_match(index, key(article)):
indexed[index.pattern].append(article)
return indexed
def attempt_publish(lexicon: LexiconModel) -> bool: def attempt_publish(lexicon: LexiconModel) -> bool:
""" """
If the lexicon's publsh policy allows the current set of approved If the lexicon's publsh policy allows the current set of approved

View File

@ -8,9 +8,11 @@ from flask import (
Markup) Markup)
from flask_login import login_required, current_user from flask_login import login_required, current_user
from amanuensis.lexicon import player_can_join_lexicon, add_player_to_lexicon from amanuensis.lexicon import (
player_can_join_lexicon,
add_player_to_lexicon,
sort_by_index_spec)
from amanuensis.models import LexiconModel from amanuensis.models import LexiconModel
from amanuensis.parser import filesafe_title
from amanuensis.server.helpers import ( from amanuensis.server.helpers import (
lexicon_param, lexicon_param,
player_required_if_not_public) player_required_if_not_public)
@ -61,17 +63,14 @@ def join(name):
@lexicon_param @lexicon_param
@player_required_if_not_public @player_required_if_not_public
def contents(name): def contents(name):
articles = [] with g.lexicon.ctx.read('info') as info:
filenames = g.lexicon.ctx.article.ls() indexed = sort_by_index_spec(info, g.lexicon.cfg.article.index.list)
for filename in filenames: for articles in indexed.values():
with g.lexicon.ctx.article.read(filename) as a: for i in range(len(articles)):
articles.append({ articles[i] = {
'title': a.title, 'title': articles[i],
'link': url_for('lexicon.article', **info.get(articles[i])}
name=name, return render_template('lexicon.contents.jinja', indexed=indexed)
title=filesafe_title(a.title)),
})
return render_template('lexicon.contents.jinja', articles=articles)
@bp_lexicon.route('/article/<title>') @bp_lexicon.route('/article/<title>')

View File

@ -8,13 +8,18 @@
<span style="color:#ff0000">{{ message }}</span><br> <span style="color:#ff0000">{{ message }}</span><br>
{% endfor %} {% endfor %}
{% if articles %} {% for index in indexed %}
<b>{{ index }}</b>
{% if indexed[index] %}
<ul> <ul>
{% for article in articles %} {% for article in indexed[index] %}
<li><a href="{{ article.link }}">{{ article.title }}</a></li> <li><a href="{{ article.title|articlelink }}" class="{{ 'phantom' if not article.character else '' }}">
{{ article.title }}
</a></li>
{% endfor %} {% endfor %}
</ul> </ul>
{% endif %} {% endif %}
{% endfor %}
{% endblock %} {% endblock %}
{% set template_content_blocks = [self.main()] %} {% set template_content_blocks = [self.main()] %}