diff --git a/relay/application.py b/relay/application.py index dfa3861..6c59546 100644 --- a/relay/application.py +++ b/relay/application.py @@ -111,6 +111,11 @@ class Application(web.Application): self['signer'] = Signer(value, self.config.keyid) + @property + def template(self) -> Template: + return self['template'] + + @property def uptime(self) -> timedelta: if not self['start_time']: diff --git a/relay/frontend/page/home.haml b/relay/frontend/page/home.haml index 9c3e063..2aa745f 100644 --- a/relay/frontend/page/home.haml +++ b/relay/frontend/page/home.haml @@ -2,9 +2,7 @@ -set page = "Home" -block content .section - -for line in config.note.splitlines() - -if line - %p -> =line + -markdown -> =config.note .section %p diff --git a/relay/template.py b/relay/template.py index 4699d01..e951424 100644 --- a/relay/template.py +++ b/relay/template.py @@ -1,14 +1,22 @@ from __future__ import annotations +import textwrap import typing +from collections.abc import Callable from hamlish_jinja import HamlishExtension from jinja2 import Environment, FileSystemLoader +from jinja2.ext import Extension +from jinja2.nodes import CallBlock +from markdown import Markdown + from . import __version__ from .misc import get_resource if typing.TYPE_CHECKING: + from jinja2.nodes import Node + from jinja2.parser import Parser from typing import Any from .application import Application from .views.base import View @@ -21,7 +29,8 @@ class Template(Environment): trim_blocks = True, lstrip_blocks = True, extensions = [ - HamlishExtension + HamlishExtension, + MarkdownExtension ], loader = FileSystemLoader([ get_resource('frontend'), @@ -48,3 +57,36 @@ class Template(Environment): } return self.get_template(path).render(new_context) + + +class MarkdownExtension(Extension): + tags = {'markdown'} + extensions = { + 'attr_list', + 'smarty', + 'tables' + } + + + def __init__(self, environment: Environment): + Extension.__init__(self, environment) + self._markdown = Markdown(extensions = MarkdownExtension.extensions) + environment.extend( + render_markdown = self._render_markdown + ) + + + def parse(self, parser: Parser) -> Node | list[Node]: + lineno = next(parser.stream).lineno + body = parser.parse_statements( + ['name:endmarkdown'], + drop_needle = True + ) + + output = CallBlock(self.call_method('_render_markdown'), [], [], body) + return output.set_lineno(lineno) + + + def _render_markdown(self, caller: Callable[[], str] | str) -> str: + text = caller() if isinstance(caller, Callable) else caller + return self._markdown.convert(textwrap.dedent(text.strip('\n'))) diff --git a/relay/views/activitypub.py b/relay/views/activitypub.py index df3085f..6e392db 100644 --- a/relay/views/activitypub.py +++ b/relay/views/activitypub.py @@ -36,7 +36,7 @@ class ActorView(View): data = Message.new_actor( host = self.config.domain, pubkey = self.app.signer.pubkey, - description = ''.join(f"
{line}
" for line in config['note'].splitlines()), + description = self.app.template.render_markdown(config['note']), approves = config['approval-required'] ) diff --git a/requirements.txt b/requirements.txt index 4c43b87..5ca0df6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,6 +6,7 @@ barkshark-sql@https://git.barkshark.xyz/barkshark/bsql/archive/0.1.2.tar.gz click>=8.1.2 hamlish-jinja@https://git.barkshark.xyz/barkshark/hamlish-jinja/archive/0.3.5.tar.gz hiredis==2.3.2 +markdown==3.5.2 platformdirs==4.2.0 pyyaml>=6.0 redis==5.0.1