From e314b98eaa9a24cd062f8907293ed57cb8333129 Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Sat, 7 Jul 2018 14:21:09 -0700 Subject: [PATCH] Fix sorting in contents page --- src/build.py | 33 +++++++++++++++------------------ src/utils.py | 11 ++++++----- 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/src/build.py b/src/build.py index 314a470..6b5ff43 100644 --- a/src/build.py +++ b/src/build.py @@ -13,7 +13,7 @@ def build_contents_page(articles, config): Builds the full HTML of the contents page. """ 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]) if phantom_count == 0: content = "

There are {0} entries in this lexicon.

\n".format(len(articles)) @@ -23,48 +23,44 @@ def build_contents_page(articles, config): # Prepare article links link_by_title = {article.title : "{0}".format( 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} # Write the articles in alphabetical order content += utils.load_resource("contents.html") content += "
\n\n
\n" # Write the articles in turn order content += "
\n\n
\n" # Fill in the page skeleton 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 #written_titles = [article.title for article in 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( 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] def pathto(*els): return os.path.join(lex_path, *els) diff --git a/src/utils.py b/src/utils.py index d97111d..af15187 100644 --- a/src/utils.py +++ b/src/utils.py @@ -23,13 +23,14 @@ def titleescape(s): s = hex(abs(hash(s)))[2:] # Replace it with a hex hash 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:] - if s.startswith("An "): return s[3:] - if s.startswith("A "): return s[2:] + s = s.lower() + if s.startswith("the "): return s[4:] + if s.startswith("an "): return s[3:] + if s.startswith("a "): return s[2:] return s # Load functions