From 317fc07a4599e775764b3abc5d829ddd60c5f842 Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Sun, 19 Apr 2020 14:52:41 -0700 Subject: [PATCH] Add partial citeblock and real links --- amanuensis/lexicon/manage.py | 6 +++--- amanuensis/parser/render.py | 14 ++++++++++++-- amanuensis/server/helpers.py | 5 +++++ amanuensis/server/lexicon.py | 12 +++++++++--- amanuensis/templates/lexicon/article.html | 10 ++++++++-- 5 files changed, 37 insertions(+), 10 deletions(-) diff --git a/amanuensis/lexicon/manage.py b/amanuensis/lexicon/manage.py index 2c551a6..3d756df 100644 --- a/amanuensis/lexicon/manage.py +++ b/amanuensis/lexicon/manage.py @@ -13,7 +13,7 @@ from amanuensis.config import prepend, json_rw, json_ro, logger from amanuensis.config.loader import AttrOrderedDict from amanuensis.errors import ArgumentError from amanuensis.lexicon import LexiconModel -from amanuensis.parser import parse_raw_markdown, GetCitations, HtmlRenderer, filesafe_title +from amanuensis.parser import parse_raw_markdown, GetCitations, HtmlRenderer, filesafe_title, titlesort from amanuensis.resources import get_stream def valid_name(name): @@ -300,7 +300,7 @@ def publish_turn(lexicon, drafts): # Get all citations citations_by_title = {} for title, article in article_renderable_by_title.items(): - citations_by_title[title] = article.render(GetCitations()) + citations_by_title[title] = sorted(set(article.render(GetCitations())), key=titlesort) # Get the written and phantom lists from the citation map written_titles = list(citations_by_title.keys()) @@ -327,7 +327,7 @@ def publish_turn(lexicon, drafts): # Render article HTML and save to article cache rendered_html_by_title = {} for title, article in article_renderable_by_title.items(): - html = article.render(HtmlRenderer(written_titles)) + html = article.render(HtmlRenderer(lexicon.name, written_titles)) filename = filesafe_title(title) with lexicon.ctx.article.edit(filename, create=True) as f: f['title'] = title diff --git a/amanuensis/parser/render.py b/amanuensis/parser/render.py index ed87f4c..81c264a 100644 --- a/amanuensis/parser/render.py +++ b/amanuensis/parser/render.py @@ -3,12 +3,17 @@ Internal module encapsulating visitors that render articles into readable formats. """ +from flask import url_for + +from amanuensis.parser.helpers import filesafe_title + class HtmlRenderer(): """ Renders an article token tree into published article HTML. """ - def __init__(self, written_articles): + def __init__(self, lexicon_name, written_articles): + self.lexicon_name = lexicon_name self.written_articles = written_articles def TextSpan(self, span): @@ -41,7 +46,12 @@ class HtmlRenderer(): link_class = '' else: link_class = ' class="phantom"' - return f'{"".join(span.recurse(self))}' + # link = url_for( + # 'lexicon.article', + # name=self.lexicon_name, + # title=filesafe_title(span.cite_target)) + link = f'/lexicon/{self.lexicon_name}/article/{filesafe_title(span.cite_target)}' + return f'{"".join(span.recurse(self))}' diff --git a/amanuensis/server/helpers.py b/amanuensis/server/helpers.py index 72532f5..c7522b3 100644 --- a/amanuensis/server/helpers.py +++ b/amanuensis/server/helpers.py @@ -8,6 +8,7 @@ from flask_login import current_user # Module imports from amanuensis.lexicon import LexiconModel +from amanuensis.parser import filesafe_title from amanuensis.user import UserModel @@ -27,6 +28,10 @@ def register_custom_filters(app): dt = datetime.fromtimestamp(ts) return dt.strftime(formatstr) + @app.template_filter("articlelink") + def article_link(title): + return url_for('lexicon.article', name=g.lexicon.name, title=filesafe_title(title)) + def lexicon_param(route): """Wrapper for loading a route's lexicon""" diff --git a/amanuensis/server/lexicon.py b/amanuensis/server/lexicon.py index d3d70eb..aeea3cd 100644 --- a/amanuensis/server/lexicon.py +++ b/amanuensis/server/lexicon.py @@ -71,7 +71,9 @@ def get_bp(): with g.lexicon.ctx.article.read(title) as a: article = dict(a) article['html'] = Markup(a['html'] or "") - return render_template('lexicon/article.html', article=article) + with g.lexicon.ctx.read('info') as info: + cites = info[a.title]['citations'] + return render_template('lexicon/article.html', article=article, cites=cites) @bp.route('/rules/', methods=['GET']) @lexicon_param @@ -183,8 +185,12 @@ def get_bp(): return redirect(url_for('lexicon.session', name=name)) parsed_draft = parse_raw_markdown(draft.contents) - rendered_html = parsed_draft.render(PreviewHtmlRenderer( - {'Article':'default','Phantom':None})) + 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)) # If the article is ready and awaiting review if not draft.status.approved: diff --git a/amanuensis/templates/lexicon/article.html b/amanuensis/templates/lexicon/article.html index 8b016b0..b72e244 100644 --- a/amanuensis/templates/lexicon/article.html +++ b/amanuensis/templates/lexicon/article.html @@ -8,8 +8,14 @@ {% endfor %}

{{ article.title }}

- {{ article.html }} {% endblock %} -{% set template_content_blocks = [self.main()] %} \ No newline at end of file + +{% block citations %} +{% for cite in cites %} +{{ cite }} / +{% endfor %} +{% endblock %} + +{% set template_content_blocks = [self.main(), self.citations()] %} \ No newline at end of file