Fix sorting in contents page

This commit is contained in:
Tim Van Baak 2018-07-07 14:21:09 -07:00
parent 86478e3d87
commit e314b98eaa
2 changed files with 21 additions and 23 deletions

View File

@ -13,7 +13,7 @@ def build_contents_page(articles, config):
Builds the full HTML of the contents page. Builds the full HTML of the contents page.
""" """
content = "" content = ""
# Article counts # Head the contents page with counts of written and phantom articles
phantom_count = len([article for article in articles if article.player is None]) phantom_count = len([article for article in articles if article.player is None])
if phantom_count == 0: if phantom_count == 0:
content = "<p>There are <b>{0}</b> entries in this lexicon.</p>\n".format(len(articles)) content = "<p>There are <b>{0}</b> entries in this lexicon.</p>\n".format(len(articles))
@ -23,48 +23,44 @@ def build_contents_page(articles, config):
# Prepare article links # Prepare article links
link_by_title = {article.title : "<a href=\"../article/{1}.html\"{2}>{0}</a>".format( link_by_title = {article.title : "<a href=\"../article/{1}.html\"{2}>{0}</a>".format(
article.title, article.title_filesafe, article.title, article.title_filesafe,
"" if article.player is not None else " class=\"phantom\"") " class=\"phantom\"" if article.player is None else "")
for article in articles} for article in articles}
# Write the articles in alphabetical order # Write the articles in alphabetical order
content += utils.load_resource("contents.html") content += utils.load_resource("contents.html")
content += "<div id=\"index-order\" style=\"display:block\">\n<ul>\n" content += "<div id=\"index-order\" style=\"display:block\">\n<ul>\n"
indices = config["INDEX_LIST"].split("\n") indices = config["INDEX_LIST"].split("\n")
alphabetical_order = sorted(articles, key=lambda a: utils.titlecase(a.title)) alphabetical_order = sorted(
articles,
key=lambda a: utils.titlesort(a.title))
check_off = list(alphabetical_order) check_off = list(alphabetical_order)
for index_str in indices: for index_str in indices:
content += "<h3>{0}</h3>\n".format(index_str) content += "<h3>{0}</h3>\n".format(index_str)
for article in alphabetical_order: for article in alphabetical_order:
if (utils.titlestrip(article.title)[0].upper() in index_str): if (utils.titlesort(article.title)[0].upper() in index_str):
check_off.remove(article) check_off.remove(article)
content += "<li>" content += "<li>{}</li>\n".format(link_by_title[article.title])
content += link_by_title[article.title]
content += "</li>\n"
if len(check_off) > 0: if len(check_off) > 0:
content += "<h3>&c.</h3>\n" content += "<h3>&c.</h3>\n"
for article in check_off: for article in check_off:
content += "<li>" content += "<li>{}</li>\n".format(link_by_title[article.title])
content += link_by_title[article.title]
content += "</li>\n"
content += "</ul>\n</div>\n" content += "</ul>\n</div>\n"
# Write the articles in turn order # Write the articles in turn order
content += "<div id=\"turn-order\" style=\"display:none\">\n<ul>\n" content += "<div id=\"turn-order\" style=\"display:none\">\n<ul>\n"
latest_turn = max([article.turn for article in articles if article.player is not None]) latest_turn = max([article.turn for article in articles if article.player is not None])
turn_order = sorted(articles, key=lambda a: (a.turn, utils.titlecase(a.title))) turn_order = sorted(
articles,
key=lambda a: (a.turn, utils.titlesort(a.title)))
check_off = list(turn_order) check_off = list(turn_order)
for turn_num in range(1, latest_turn + 1): for turn_num in range(1, latest_turn + 1):
content += "<h3>Turn {0}</h3>\n".format(turn_num) content += "<h3>Turn {0}</h3>\n".format(turn_num)
for article in turn_order: for article in turn_order:
if article.turn == turn_num: if article.turn == turn_num:
check_off.remove(article) check_off.remove(article)
content += "<li>" content += "<li>{}</li>\n".format(link_by_title[article.title])
content += link_by_title[article.title]
content += "</li>\n"
if len(check_off) > 0: if len(check_off) > 0:
content += "<h3>Unwritten</h3>\n" content += "<h3>Unwritten</h3>\n"
for article in check_off: for article in check_off:
content += "<li>" content += "<li>{}</li>\n".format(link_by_title[article.title])
content += link_by_title[article.title]
content += "</li>\n"
content += "</ul>\n</div>\n" content += "</ul>\n</div>\n"
# Fill in the page skeleton # Fill in the page skeleton
entry_skeleton = utils.load_resource("entry-page.html") entry_skeleton = utils.load_resource("entry-page.html")
@ -264,9 +260,10 @@ def build_all(path_prefix, lexicon_name):
# so we can derive the written titles from this list # so we can derive the written titles from this list
#written_titles = [article.title for article in articles] #written_titles = [article.title for article in articles]
# Once they've been populated, the articles list has the titles of all articles # Once they've been populated, the articles list has the titles of all articles
# Sort this by turn before title so prev/next links run in turn order
articles = sorted( articles = sorted(
LexiconArticle.populate(articles), LexiconArticle.populate(articles),
key=lambda a: utils.titlestrip(a.title)) key=lambda a: (a.turn, utils.titlesort(a.title)))
#phantom_titles = [article.title for article in articles if article.title not in written_titles] #phantom_titles = [article.title for article in articles if article.title not in written_titles]
def pathto(*els): def pathto(*els):
return os.path.join(lex_path, *els) return os.path.join(lex_path, *els)

View File

@ -23,13 +23,14 @@ def titleescape(s):
s = hex(abs(hash(s)))[2:] # Replace it with a hex hash s = hex(abs(hash(s)))[2:] # Replace it with a hex hash
return s return s
def titlestrip(s): def titlesort(s):
""" """
Strips articles for title sorting. Reduces titles down for sorting.
""" """
if s.startswith("The "): return s[4:] s = s.lower()
if s.startswith("An "): return s[3:] if s.startswith("the "): return s[4:]
if s.startswith("A "): return s[2:] if s.startswith("an "): return s[3:]
if s.startswith("a "): return s[2:]
return s return s
# Load functions # Load functions