From 2705764a6acb4d7f2500e213cacdb833807f3e45 Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Sun, 19 Apr 2020 19:57:08 -0700 Subject: [PATCH] Refactor info lookup into preview renderer ctor --- amanuensis/parser/render.py | 12 ++++++------ amanuensis/server/lexicon.py | 14 ++------------ 2 files changed, 8 insertions(+), 18 deletions(-) diff --git a/amanuensis/parser/render.py b/amanuensis/parser/render.py index 81c264a..4ec5afd 100644 --- a/amanuensis/parser/render.py +++ b/amanuensis/parser/render.py @@ -56,12 +56,12 @@ class HtmlRenderer(): class PreviewHtmlRenderer(): - def __init__(self, article_map): - """ - article_map maps article titles to character ids. An article - present in the map but mapped to None is a phantom article. - """ - self.article_map = article_map + def __init__(self, lexicon): + with lexicon.ctx.read('info') as info: + self.article_map = { + title: article.character + for title, article in info.items() + } def TextSpan(self, span): return span.innertext diff --git a/amanuensis/server/lexicon.py b/amanuensis/server/lexicon.py index aeea3cd..ca9db80 100644 --- a/amanuensis/server/lexicon.py +++ b/amanuensis/server/lexicon.py @@ -185,12 +185,7 @@ def get_bp(): return redirect(url_for('lexicon.session', name=name)) parsed_draft = parse_raw_markdown(draft.contents) - with g.lexicon.ctx.read('info') as info: - authorship = { - title: article.character - for title, article in info.items() - } - rendered_html = parsed_draft.render(PreviewHtmlRenderer(authorship)) + rendered_html = parsed_draft.render(PreviewHtmlRenderer(g.lexicon)) # If the article is ready and awaiting review if not draft.status.approved: @@ -324,12 +319,7 @@ def get_bp(): # check if article was previously approved # check extrinsic constraints for blocking errors parsed_draft = parse_raw_markdown(article['contents']) - with g.lexicon.ctx.read('info') as info: - authorship = { - title: article.character - for title, article in info.items() - } - rendered_html = parsed_draft.render(PreviewHtmlRenderer(authorship)) + rendered_html = parsed_draft.render(PreviewHtmlRenderer(g.lexicon)) features = parsed_draft.render(FeatureCounter()) filename = f'{article["character"]}.{article["aid"]}'