lexipython/src/build.py

446 lines
16 KiB
Python
Raw Normal View History

2017-08-27 05:14:16 +00:00
import sys # For argv and stderr
import os # For reading directories
import re # For parsing lex content
2017-11-06 07:53:25 +00:00
import io # For writing pages out as UTF-8
2017-08-27 05:14:16 +00:00
import networkx # For pagerank analytics
from collections import defaultdict # For rank inversion in statistics
2018-07-07 19:58:55 +00:00
from src import utils
from src.article import LexiconArticle
2017-08-27 05:14:16 +00:00
2018-10-28 20:50:58 +00:00
class LexiconPage:
"""
An abstraction layer around formatting a Lexicon page skeleton with kwargs
so that kwargs that are constant across pages aren't repeated.
"""
def __init__(self, skeleton=None, page=None):
self.kwargs = {}
self.skeleton = skeleton
if page is not None:
self.skeleton = page.skeleton
self.kwargs = dict(page.kwargs)
def add_kwargs(self, **kwargs):
self.kwargs.update(kwargs)
def format(self, **kwargs):
total_kwargs = {**self.kwargs, **kwargs}
return self.skeleton.format(**total_kwargs)
def build_contents_page(page, articles, index_list):
2017-08-27 05:14:16 +00:00
"""
Builds the full HTML of the contents page.
2017-08-27 05:14:16 +00:00
"""
2018-10-28 20:50:58 +00:00
content = "<div class=\"contentblock\">"
2018-07-07 21:21:09 +00:00
# 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:
2018-10-28 20:50:58 +00:00
content += "<p>There are <b>{0}</b> entries in this lexicon.</p>\n".format(len(articles))
2017-08-27 05:14:16 +00:00
else:
2018-10-28 20:50:58 +00:00
content += "<p>There are <b>{0}</b> entries, <b>{1}</b> written and <b>{2}</b> phantom.</p>\n".format(
len(articles), len(articles) - phantom_count, phantom_count)
# Prepare article links
link_by_title = {article.title : "<a href=\"../article/{1}.html\"{2}>{0}</a>".format(
article.title, article.title_filesafe,
2018-07-07 21:21:09 +00:00
" class=\"phantom\"" if article.player is None else "")
for article in articles}
# Write the articles in alphabetical order
2018-07-01 20:12:57 +00:00
content += utils.load_resource("contents.html")
2018-08-04 05:40:37 +00:00
content += "<div id=\"index-order\" style=\"display:none\">\n<ul>\n"
2018-10-28 20:50:58 +00:00
indices = index_list.split("\n")
2018-07-07 21:21:09 +00:00
alphabetical_order = sorted(
articles,
key=lambda a: utils.titlesort(a.title))
check_off = list(alphabetical_order)
2017-08-27 05:14:16 +00:00
for index_str in indices:
content += "<h3>{0}</h3>\n".format(index_str)
for article in alphabetical_order:
2018-07-07 21:21:09 +00:00
if (utils.titlesort(article.title)[0].upper() in index_str):
check_off.remove(article)
2018-07-07 21:21:09 +00:00
content += "<li>{}</li>\n".format(link_by_title[article.title])
if len(check_off) > 0:
2018-07-07 19:58:55 +00:00
content += "<h3>&c.</h3>\n"
for article in check_off:
2018-07-07 21:21:09 +00:00
content += "<li>{}</li>\n".format(link_by_title[article.title])
content += "</ul>\n</div>\n"
# Write the articles in turn order
content += "<div id=\"turn-order\" style=\"display:none\">\n<ul>\n"
2018-10-27 22:10:24 +00:00
turn_numbers = [article.turn for article in articles if article.player is not None]
first_turn, last_turn = min(turn_numbers), max(turn_numbers)
2018-07-07 21:21:09 +00:00
turn_order = sorted(
articles,
key=lambda a: (a.turn, utils.titlesort(a.title)))
check_off = list(turn_order)
2018-10-27 22:10:24 +00:00
for turn_num in range(first_turn, last_turn + 1):
content += "<h3>Turn {0}</h3>\n".format(turn_num)
for article in turn_order:
if article.turn == turn_num:
check_off.remove(article)
2018-07-07 21:21:09 +00:00
content += "<li>{}</li>\n".format(link_by_title[article.title])
if len(check_off) > 0:
content += "<h3>Unwritten</h3>\n"
for article in check_off:
2018-07-07 21:21:09 +00:00
content += "<li>{}</li>\n".format(link_by_title[article.title])
content += "</ul>\n</div>\n"
# Fill in the page skeleton
2018-10-28 20:50:58 +00:00
return page.format(title="Index", content=content)
2017-08-27 05:14:16 +00:00
2018-10-28 20:50:58 +00:00
def build_rules_page(page):
2017-08-27 05:14:16 +00:00
"""
Builds the full HTML of the rules page.
"""
2018-07-01 20:12:57 +00:00
content = utils.load_resource("rules.html")
2017-08-27 05:14:16 +00:00
# Fill in the entry skeleton
2018-10-28 20:50:58 +00:00
return page.format(title="Rules", content=content)
2017-08-27 05:14:16 +00:00
2018-10-28 20:50:58 +00:00
def build_formatting_page(page):
2017-08-27 05:14:16 +00:00
"""
Builds the full HTML of the formatting page.
"""
2018-07-01 20:12:57 +00:00
content = utils.load_resource("formatting.html")
2017-08-27 05:14:16 +00:00
# Fill in the entry skeleton
2018-10-28 20:50:58 +00:00
return page.format(title="Formatting", content=content)
2017-08-27 05:14:16 +00:00
2018-10-28 20:50:58 +00:00
def build_session_page(page, session_content):
2017-08-27 05:14:16 +00:00
"""
Builds the full HTML of the session page.
"""
# Fill in the entry skeleton
2018-10-28 20:50:58 +00:00
content = "<div class=\"contentblock\">{}</div>".format(session_content)
return page.format(title="Session", content=content)
2017-08-27 05:14:16 +00:00
2018-10-24 19:40:54 +00:00
def reverse_statistics_dict(stats, reverse=True):
"""
Transforms a dictionary mapping titles to a value into a list of values
and lists of titles. The list is sorted by the value, and the titles are
sorted alphabetically.
"""
rev = {}
for key, value in stats.items():
if value not in rev:
rev[value] = []
rev[value].append(key)
for key, value in rev.items():
rev[key] = sorted(value, key=lambda t: utils.titlesort(t))
return sorted(rev.items(), key=lambda x:x[0], reverse=reverse)
def itemize(stats_list):
return map(lambda x: "{0} &ndash; {1}".format(x[0], "; ".join(x[1])), stats_list)
2018-10-28 20:50:58 +00:00
def build_statistics_page(page, articles):
2017-08-27 05:14:16 +00:00
"""
Builds the full HTML of the statistics page.
"""
content = ""
2018-07-08 00:02:10 +00:00
2018-10-24 19:40:54 +00:00
# Top pages by pagerank
# Compute pagerank for each article
2017-08-27 05:14:16 +00:00
G = networkx.Graph()
for article in articles:
for citation in article.citations:
G.add_edge(article.title, citation.target)
2018-10-24 19:40:54 +00:00
rank_by_article = networkx.pagerank(G)
# Get the top ten articles by pagerank
top_pageranks = reverse_statistics_dict(rank_by_article)[:10]
# Replace the pageranks with ordinals
top_ranked = enumerate(map(lambda x: x[1], top_pageranks), start=1)
# Format the ranks into strings
top_ranked_items = itemize(top_ranked)
# Write the statistics to the page
2018-10-28 20:50:58 +00:00
content += "<div class=\"contentblock\">\n"
content += "<u>Top 10 articles by page rank:</u><br>\n"
2018-10-24 19:40:54 +00:00
content += "<br>\n".join(top_ranked_items)
2018-10-28 20:50:58 +00:00
content += "</div>\n"
2018-07-08 00:02:10 +00:00
# Top number of citations made
citations_made = {article.title : len(article.citations) for article in articles}
2018-10-24 19:40:54 +00:00
top_citations = reverse_statistics_dict(citations_made)[:3]
top_citations_items = itemize(top_citations)
2018-10-28 20:50:58 +00:00
content += "<div class=\"contentblock\">\n"
content += "<u>Top articles by citations made:</u><br>\n"
2018-10-24 19:40:54 +00:00
content += "<br>\n".join(top_citations_items)
2018-10-28 20:50:58 +00:00
content += "</div>\n"
2018-07-08 00:02:10 +00:00
# Top number of times cited
citations_to = {article.title : len(article.citedby) for article in articles}
2018-10-24 19:40:54 +00:00
top_cited = reverse_statistics_dict(citations_to)[:3]
top_cited_items = itemize(top_cited)
2018-10-28 20:50:58 +00:00
content += "<div class=\"contentblock\">\n"
content += "<u>Most cited articles:</u><br>\n"
2018-10-24 19:40:54 +00:00
content += "<br>\n".join(top_cited_items)
2018-10-28 20:50:58 +00:00
content += "</div>\n"
2018-07-08 00:02:10 +00:00
# Top article length, roughly by words
article_length = {}
for article in articles:
format_map = {
"c"+str(c.id): c.text
for c in article.citations
2018-07-08 00:02:10 +00:00
}
plain_content = article.content.format(**format_map)
article_length[article.title] = len(plain_content.split())
2018-10-24 19:40:54 +00:00
top_length = reverse_statistics_dict(article_length)[:3]
top_length_items = itemize(top_length)
2018-10-28 20:50:58 +00:00
content += "<div class=\"contentblock\">\n"
content += "<u>Longest articles:</u><br>\n"
2018-10-24 19:40:54 +00:00
content += "<br>\n".join(top_length_items)
2018-10-28 20:50:58 +00:00
content += "</div>\n"
2018-07-08 00:02:10 +00:00
2018-10-24 19:40:54 +00:00
# Total word count
2018-11-04 04:00:39 +00:00
all_articles = []
for article in articles:
all_articles.append(article)
all_articles.extend(article.addendums)
turn_numbers = set([a.turn for a in articles if a.player is not None])
aggregate = {num: 0 for num in turn_numbers}
for turn_num in turn_numbers:
for article in all_articles:
if article.turn <= turn_num:
aggregate[turn_num] += article_length[article.title]
aggr_list = [(str(k), [str(v)]) for k,v in aggregate.items()]
2018-10-28 20:50:58 +00:00
content += "<div class=\"contentblock\">\n"
2018-11-04 04:00:39 +00:00
content += "<u>Aggregate word count by turn:</u><br>\n"
content += "<br>\n".join(itemize(aggr_list))
2018-10-28 20:50:58 +00:00
content += "</div>\n"
2018-10-22 01:08:20 +00:00
2018-07-08 00:02:10 +00:00
# Player pageranks
# Add addendums and recompute pagerank
for article in articles:
for addendum in article.addendums:
for citation in addendum.citations:
addendum_title = "{0.title}-T{0.turn}".format(addendum)
G.add_edge(addendum_title, citation.target)
2018-11-04 07:02:48 +00:00
rank_by_article_all = networkx.pagerank(G)
players = sorted(set([article.player for article in articles if article.player is not None]))
pagerank_by_player = {player: 0 for player in players}
for article in articles:
if article.player is not None:
2018-11-04 07:02:48 +00:00
pagerank_by_player[article.player] += (rank_by_article_all[article.title]
if article.title in rank_by_article_all else 0)
for addendum in article.addendums:
addendum_title = "{0.title}-T{0.turn}".format(addendum)
2018-11-04 07:02:48 +00:00
pagerank_by_player[addendum_title] += (rank_by_article_all[addendum_title]
if addendum_title in rank_by_article_all else 0)
for player in players:
pagerank_by_player[player] = round(pagerank_by_player[player], 3)
2018-10-24 19:40:54 +00:00
player_rank = reverse_statistics_dict(pagerank_by_player)
player_rank_items = itemize(player_rank)
2018-10-28 20:50:58 +00:00
content += "<div class=\"contentblock\">\n"
content += "<u>Player total page rank:</u><br>\n"
2018-10-24 19:40:54 +00:00
content += "<br>\n".join(player_rank_items)
2018-10-28 20:50:58 +00:00
content += "</div>\n"
2018-07-08 00:02:10 +00:00
# Player citations made
cite_count_by_player = {player: 0 for player in players}
for article in articles:
if article.player is not None:
unique_citations = set([a.target for a in article.citations])
cite_count_by_player[article.player] += len(unique_citations)
for addendum in article.addendums:
cite_count_by_player[addendum.player] += len(addendum.citations)
player_cites_made_ranks = reverse_statistics_dict(cite_count_by_player)
2018-10-24 19:40:54 +00:00
player_cites_made_items = itemize(player_cites_made_ranks)
2018-10-28 20:50:58 +00:00
content += "<div class=\"contentblock\">\n"
content += "<u>Citations made by player:</u><br>\n"
2018-10-24 19:40:54 +00:00
content += "<br>\n".join(player_cites_made_items)
2018-10-28 20:50:58 +00:00
content += "</div>\n"
2018-07-08 00:02:10 +00:00
# Player cited count
cited_times = {player : 0 for player in players}
for article in articles:
if article.player is not None:
cited_times[article.player] += len(article.citedby)
2018-10-24 19:40:54 +00:00
cited_times_ranked = reverse_statistics_dict(cited_times)
cited_times_items = itemize(cited_times_ranked)
2018-10-28 20:50:58 +00:00
content += "<div class=\"contentblock\">\n"
content += "<u>Citations made to player:</u><br>\n"
2018-10-24 19:40:54 +00:00
content += "<br>\n".join(cited_times_items)
2018-10-28 20:50:58 +00:00
content += "</div>\n"
2018-07-08 00:02:10 +00:00
2018-11-04 07:02:48 +00:00
# Lowest pagerank
pageranks = reverse_statistics_dict(rank_by_article)
bot_ranked = list(enumerate(map(lambda x: x[1], pageranks), start=1))[-10:]
# Format the ranks into strings
bot_ranked_items = itemize(bot_ranked)
content += "<div class=\"contentblock\">\n"
content += "<u>Bottom 10 articles by pagerank:</u><br>\n"
content += "<br>\n".join(bot_ranked_items)
content += "</div>\n"
2017-08-27 05:14:16 +00:00
# Fill in the entry skeleton
2018-10-28 20:50:58 +00:00
return page.format(title="Statistics", content=content)
2017-08-27 05:14:16 +00:00
def build_graphviz_file(cite_map):
"""
Builds a citation graph in dot format for Graphviz.
"""
result = []
result.append("digraph G {\n")
# Node labeling
written_entries = list(cite_map.keys())
phantom_entries = set([title for cites in cite_map.values() for title in cites if title not in written_entries])
node_labels = [title[:20] for title in written_entries + list(phantom_entries)]
node_names = [hash(i) for i in node_labels]
for i in range(len(node_labels)):
result.append("{} [label=\"{}\"];\n".format(node_names[i], node_labels[i]))
# Edges
for citer in written_entries:
for cited in cite_map[citer]:
result.append("{}->{};\n".format(hash(citer[:20]), hash(cited[:20])))
# Return result
result.append("overlap=false;\n}\n")
return "".join(result)#"…"
2018-07-07 19:58:55 +00:00
def build_compiled_page(articles, config):
"""
Builds a page compiling all articles in the Lexicon.
"""
2018-07-07 22:28:18 +00:00
# Sort by turn and title
turn_order = sorted(
articles,
key=lambda a: (a.turn, utils.titlesort(a.title)))
# Build the content of each article
css = utils.load_resource("lexicon.css")
css += "\n"\
2018-07-07 23:17:55 +00:00
"body { background: #ffffff; }\n"\
"sup { vertical-align: top; font-size: 0.6em; }\n"
2018-07-07 22:28:18 +00:00
content = "<html>\n"\
"<head>\n"\
"<title>{lexicon}</title>\n"\
"<style>\n"\
"{css}\n"\
"</style>\n"\
"<body>\n"\
"<h1>{lexicon}</h1>".format(
lexicon=config["LEXICON_TITLE"],
css=css)
for article in turn_order:
2018-07-07 23:17:55 +00:00
# Stitch in superscripts for citations
2018-07-07 22:28:18 +00:00
format_map = {
2018-07-07 23:17:55 +00:00
format_id: "{}<sup>{}</sup>".format(cite_tuple[0], format_id[1:])
2018-07-07 22:28:18 +00:00
for format_id, cite_tuple in article.citations.items()
}
article_body = article.content.format(**format_map)
# Stitch a page-break-avoid div around the header and first paragraph
article_body = article_body.replace("</p>", "</p></div>", 1)
2018-07-07 23:17:55 +00:00
# Append the citation block
cite_list = "<br>\n".join(
"{}. {}\n".format(format_id[1:], cite_tuple[1])
for format_id, cite_tuple in sorted(
article.citations.items(),
key=lambda t:int(t[0][1:])))
cite_block = "" if article.player is None else ""\
"<p><i>Citations:</i><br>\n"\
"{}\n</p>".format(cite_list)
2018-07-07 22:28:18 +00:00
article_block = "<div style=\"page-break-inside:avoid;\">\n"\
"<h2>{}</h2>\n"\
2018-07-07 23:17:55 +00:00
"{}\n"\
"{}\n".format(article.title, article_body, cite_block)
2018-07-07 22:28:18 +00:00
content += article_block
2018-07-07 23:17:55 +00:00
2018-07-07 22:28:18 +00:00
content += "</body></html>"
return content
2018-07-07 19:58:55 +00:00
def build_all(path_prefix, lexicon_name):
"""
Builds all browsable articles and pages in the Lexicon.
"""
lex_path = os.path.join(path_prefix, lexicon_name)
# Load the Lexicon's peripherals
config = utils.load_config(lexicon_name)
2018-10-28 20:50:58 +00:00
page_skeleton = utils.load_resource("page-skeleton.html")
page = LexiconPage(skeleton=page_skeleton)
page.add_kwargs(
lexicon=config["LEXICON_TITLE"],
logo=config["LOGO_FILENAME"],
prompt=config["PROMPT"],
sort=config["DEFAULT_SORT"])
2018-07-07 19:58:55 +00:00
# Parse the written articles
articles = LexiconArticle.parse_from_directory(os.path.join(lex_path, "src"))
# Once they've been populated, the articles list has the titles of all articles
2018-07-07 21:21:09 +00:00
# Sort this by turn before title so prev/next links run in turn order
2018-07-07 19:58:55 +00:00
articles = sorted(
LexiconArticle.interlink(articles),
2018-07-07 21:21:09 +00:00
key=lambda a: (a.turn, utils.titlesort(a.title)))
2018-10-28 20:50:58 +00:00
2018-07-07 19:58:55 +00:00
def pathto(*els):
return os.path.join(lex_path, *els)
# Write the redirect page
print("Writing redirect page...")
with open(pathto("index.html"), "w", encoding="utf8") as f:
2018-10-28 20:50:58 +00:00
f.write(utils.load_resource("redirect.html").format(
lexicon=config["LEXICON_TITLE"], sort=config["DEFAULT_SORT"]))
2017-08-27 05:14:16 +00:00
2018-07-07 19:58:55 +00:00
# Write the article pages
print("Deleting old article pages...")
for filename in os.listdir(pathto("article")):
if filename[-5:] == ".html":
os.remove(pathto("article", filename))
print("Writing article pages...")
l = len(articles)
for idx in range(l):
article = articles[idx]
2018-08-20 08:25:06 +00:00
with open(pathto("article", article.title_filesafe + ".html"), "w", encoding="utf-8") as f:
content = article.build_default_content()
2018-10-28 20:50:58 +00:00
article_html = page.format(
2018-07-07 19:58:55 +00:00
title = article.title,
content = content)
2018-07-07 19:58:55 +00:00
f.write(article_html)
print(" Wrote " + article.title)
2017-08-27 05:14:16 +00:00
2018-07-07 19:58:55 +00:00
# Write default pages
print("Writing default pages...")
2018-08-20 08:25:06 +00:00
with open(pathto("contents", "index.html"), "w", encoding="utf-8") as f:
2018-10-28 20:50:58 +00:00
f.write(build_contents_page(page, articles, config["INDEX_LIST"]))
2018-07-07 19:58:55 +00:00
print(" Wrote Contents")
2018-08-20 08:25:06 +00:00
with open(pathto("rules", "index.html"), "w", encoding="utf-8") as f:
2018-10-28 20:50:58 +00:00
f.write(build_rules_page(page))
2018-07-07 19:58:55 +00:00
print(" Wrote Rules")
2018-08-20 08:25:06 +00:00
with open(pathto("formatting", "index.html"), "w", encoding="utf-8") as f:
2018-10-28 20:50:58 +00:00
f.write(build_formatting_page(page))
2018-07-07 19:58:55 +00:00
print(" Wrote Formatting")
2018-08-20 08:25:06 +00:00
with open(pathto("session", "index.html"), "w", encoding="utf-8") as f:
2018-10-28 20:50:58 +00:00
f.write(build_session_page(page, config["SESSION_PAGE"]))
2018-07-07 19:58:55 +00:00
print(" Wrote Session")
2018-08-20 08:25:06 +00:00
with open(pathto("statistics", "index.html"), "w", encoding="utf-8") as f:
2018-10-28 20:50:58 +00:00
f.write(build_statistics_page(page, articles))
2018-07-07 22:28:18 +00:00
print(" Wrote Statistics")
# Write auxiliary pages
if "PRINTABLE_FILE" in config and config["PRINTABLE_FILE"]:
with open(pathto(config["PRINTABLE_FILE"]), "w", encoding="utf-8") as f:
f.write(build_compiled_page(articles, config))
2018-08-17 00:03:16 +00:00
print(" Wrote compiled page to " + config["PRINTABLE_FILE"])
2018-11-03 21:54:13 +00:00
with open(pathto("editor.html"), "w", encoding="utf-8") as f:
editor = utils.load_resource("editor.html")
writtenArticles = ""
phantomArticles = ""
for article in articles:
if article.player is None:
phantomArticles += "{{title: \"{0}\"}},".format(article.title.replace("\"", "\\\""))
else:
writtenArticles += "{{title: \"{0}\", author: \"{1.player}\"}},".format(
article.title.replace("\"", "\\\""), article)
nextTurn = 0
if articles:
nextTurn = max([article.turn for article in articles if article.player is not None]) + 1
editor = editor.replace("//writtenArticles", writtenArticles)
editor = editor.replace("//phantomArticles", phantomArticles)
editor = editor.replace("TURNNUMBER", str(nextTurn))
f.write(editor)
2018-08-17 00:03:16 +00:00
# Check that authors aren't citing themselves
print("Running citation checks...")
for parent in articles:
for article in [parent] + parent.addendums:
for citation in article.citations:
if article.player == citation.article.player:
print(" {2}: {0} cites {1}".format(article.title, citation.target, article.player))
2018-08-17 00:03:16 +00:00
print()