Rework printable page for new article model

This commit is contained in:
Tim Van Baak 2018-11-10 15:39:11 -08:00
parent 4ab9f1f1ea
commit 9a8ad419a0
1 changed files with 44 additions and 39 deletions

View File

@ -298,49 +298,54 @@ def build_compiled_page(articles, config):
""" """
Builds a page compiling all articles in the Lexicon. Builds a page compiling all articles in the Lexicon.
""" """
# Sort by turn and title articles = sorted(
turn_order = sorted(
articles, articles,
key=lambda a: (a.turn, utils.titlesort(a.title))) key=lambda a: (utils.titlesort(a.title)))
# Build the content of each article # Write the header
css = utils.load_resource("lexicon.css") content = "<html><head><title>{}</title>"\
css += "\n"\ "<style>span.signature {{ text-align: right; }} "\
"body { background: #ffffff; }\n"\ "sup {{ vertical-align: top; font-size: 0.6em; }} "\
"sup { vertical-align: top; font-size: 0.6em; }\n" "u {{ text-decoration-color: #888888; }}</style>"\
content = "<html>\n"\ "</head><body>\n".format(config["LEXICON_TITLE"])
"<head>\n"\
"<title>{lexicon}</title>\n"\ # Write each article
"<style>\n"\ for article in articles:
"{css}\n"\ # Article title
"</style>\n"\ content += "<div style=\"page-break-inside:avoid;\"><h2>{0.title}</h2>".format(article)
"<body>\n"\
"<h1>{lexicon}</h1>".format( # Article content
lexicon=config["LEXICON_TITLE"],
css=css)
for article in turn_order:
# Stitch in superscripts for citations
format_map = { format_map = {
format_id: "{}<sup>{}</sup>".format(cite_tuple[0], format_id[1:]) "c"+str(c.id) : c.format("<u>{text}</u><sup>{id}</sup>")
for format_id, cite_tuple in article.citations.items() for c in article.citations
} }
article_body = article.content.format(**format_map) article_content = article.content.format(**format_map)
# Stitch a page-break-avoid div around the header and first paragraph article_content = article_content.replace("</p>", "</p></div>", 1)
article_body = article_body.replace("</p>", "</p></div>", 1) content += article_content
# Append the citation block
cite_list = "<br>\n".join( # Article citations
"{}. {}\n".format(format_id[1:], cite_tuple[1]) cite_list = "<br>".join(
for format_id, cite_tuple in sorted( c.format("{id}. {target}")
article.citations.items(), for c in article.citations)
key=lambda t:int(t[0][1:]))) cite_block = "<p>{}</p>".format(cite_list)
cite_block = "" if article.player is None else ""\ content += cite_block
"<p><i>Citations:</i><br>\n"\
"{}\n</p>".format(cite_list) # Addendums
article_block = "<div style=\"page-break-inside:avoid;\">\n"\ for addendum in article.addendums:
"<h2>{}</h2>\n"\ # Addendum content
"{}\n"\ format_map = {
"{}\n".format(article.title, article_body, cite_block) "c"+str(c.id) : c.format("<u>{text}</u><sup>{id}</sup>")
content += article_block for c in addendum.citations
}
article_content = addendum.content.format(**format_map)
content += article_content
# Addendum citations
cite_list = "<br>".join(
c.format("{id}. {target}")
for c in addendum.citations)
cite_block = "<p>{}</p>".format(cite_list)
content += cite_block
content += "</body></html>" content += "</body></html>"
return content return content