From 9c41c68958d606f6cd25f9563e2e75a5c4d6dedb Mon Sep 17 00:00:00 2001 From: Thomas Sileo Date: Sun, 26 Jun 2022 21:54:07 +0200 Subject: [PATCH] Microformats support --- app/templates.py | 11 +++++++++++ app/templates/header.html | 2 +- app/templates/index.html | 16 ++++++++++++++++ app/templates/layout.html | 1 + app/templates/object.html | 13 +++++++++++++ app/templates/utils.html | 20 ++++++++++---------- poetry.lock | 14 +++++++++++++- pyproject.toml | 1 + 8 files changed, 66 insertions(+), 12 deletions(-) diff --git a/app/templates.py b/app/templates.py index 0b4512c..7d9f529 100644 --- a/app/templates.py +++ b/app/templates.py @@ -6,6 +6,7 @@ from typing import Any from urllib.parse import urlparse import bleach +import html2text import timeago # type: ignore from bs4 import BeautifulSoup # type: ignore from fastapi import Request @@ -32,6 +33,11 @@ from app.utils.highlight import highlight _templates = Jinja2Templates(directory="app/templates") +H2T = html2text.HTML2Text() +H2T.ignore_links = True +H2T.ignore_images = True + + def _filter_domain(text: str) -> str: hostname = urlparse(text).hostname if not hostname: @@ -219,6 +225,10 @@ def _replace_custom_emojis(content: str, note: Object) -> str: return content +def _html2text(content: str) -> str: + return H2T.handle(content) + + _templates.env.filters["domain"] = _filter_domain _templates.env.filters["media_proxy_url"] = _media_proxy_url _templates.env.filters["clean_html"] = _clean_html @@ -226,3 +236,4 @@ _templates.env.filters["timeago"] = _timeago _templates.env.filters["format_date"] = _format_date _templates.env.filters["has_media_type"] = _has_media_type _templates.env.filters["pluralize"] = _pluralize +_templates.env.filters["html2text"] = _html2text diff --git a/app/templates/header.html b/app/templates/header.html index bb6de21..b5be09f 100644 --- a/app/templates/header.html +++ b/app/templates/header.html @@ -4,7 +4,7 @@ {{ local_actor.name }} - {{ local_actor.handle }} + {{ local_actor.handle }}
diff --git a/app/templates/index.html b/app/templates/index.html index 1f45032..6ca13bd 100644 --- a/app/templates/index.html +++ b/app/templates/index.html @@ -1,11 +1,27 @@ {%- import "utils.html" as utils with context -%} {% extends "layout.html" %} + +{% block head %} + + + + + + + + + +{% endblock %} + {% block content %} {% include "header.html" %} +
+ {% for outbox_object in objects %} {{ utils.display_object(outbox_object) }} {% endfor %} +
{% if has_previous_page %} Previous diff --git a/app/templates/layout.html b/app/templates/layout.html index fc60875..90dd568 100644 --- a/app/templates/layout.html +++ b/app/templates/layout.html @@ -8,6 +8,7 @@ +{% block head %}{% endblock %}
diff --git a/app/templates/object.html b/app/templates/object.html index 0ebcbad..3da1392 100644 --- a/app/templates/object.html +++ b/app/templates/object.html @@ -1,5 +1,18 @@ {%- import "utils.html" as utils with context -%} {% extends "layout.html" %} + +{% block head %} + + + + + + + + + +{% endblock %} + {% block content %} {% include "header.html" %} diff --git a/app/templates/utils.html b/app/templates/utils.html index 8f7ff86..b878272 100644 --- a/app/templates/utils.html +++ b/app/templates/utils.html @@ -111,13 +111,13 @@ {% macro display_actor(actor, actors_metadata) %} {% set metadata = actors_metadata.get(actor.ap_id) %} -
+ {% if is_admin and metadata %} @@ -145,15 +145,15 @@ {% macro display_object_expanded(object) %} -
+
{{ display_actor(object.actor, {}) }} -
+
{{ object.content | clean_html(object) | safe }}
-{{ object.ap_published_at | format_date }} + {{ object.visibility.value }} {% if object.is_from_outbox %} {{ object.likes_count }} likes @@ -170,17 +170,17 @@ {% macro display_object(object) %} {% if object.ap_type in ["Note", "Article", "Video"] %} -