add markdown support for the note setting

This commit is contained in:
Izalia Mae 2024-03-11 03:17:38 -04:00
parent 5b1f244703
commit 21e0e0a3ec
5 changed files with 51 additions and 5 deletions

View file

@ -111,6 +111,11 @@ class Application(web.Application):
self['signer'] = Signer(value, self.config.keyid) self['signer'] = Signer(value, self.config.keyid)
@property
def template(self) -> Template:
return self['template']
@property @property
def uptime(self) -> timedelta: def uptime(self) -> timedelta:
if not self['start_time']: if not self['start_time']:

View file

@ -2,9 +2,7 @@
-set page = "Home" -set page = "Home"
-block content -block content
.section .section
-for line in config.note.splitlines() -markdown -> =config.note
-if line
%p -> =line
.section .section
%p %p

View file

@ -1,14 +1,22 @@
from __future__ import annotations from __future__ import annotations
import textwrap
import typing import typing
from collections.abc import Callable
from hamlish_jinja import HamlishExtension from hamlish_jinja import HamlishExtension
from jinja2 import Environment, FileSystemLoader from jinja2 import Environment, FileSystemLoader
from jinja2.ext import Extension
from jinja2.nodes import CallBlock
from markdown import Markdown
from . import __version__ from . import __version__
from .misc import get_resource from .misc import get_resource
if typing.TYPE_CHECKING: if typing.TYPE_CHECKING:
from jinja2.nodes import Node
from jinja2.parser import Parser
from typing import Any from typing import Any
from .application import Application from .application import Application
from .views.base import View from .views.base import View
@ -21,7 +29,8 @@ class Template(Environment):
trim_blocks = True, trim_blocks = True,
lstrip_blocks = True, lstrip_blocks = True,
extensions = [ extensions = [
HamlishExtension HamlishExtension,
MarkdownExtension
], ],
loader = FileSystemLoader([ loader = FileSystemLoader([
get_resource('frontend'), get_resource('frontend'),
@ -48,3 +57,36 @@ class Template(Environment):
} }
return self.get_template(path).render(new_context) 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')))

View file

@ -36,7 +36,7 @@ class ActorView(View):
data = Message.new_actor( data = Message.new_actor(
host = self.config.domain, host = self.config.domain,
pubkey = self.app.signer.pubkey, pubkey = self.app.signer.pubkey,
description = ''.join(f"<p>{line}</p>" for line in config['note'].splitlines()), description = self.app.template.render_markdown(config['note']),
approves = config['approval-required'] approves = config['approval-required']
) )

View file

@ -6,6 +6,7 @@ barkshark-sql@https://git.barkshark.xyz/barkshark/bsql/archive/0.1.2.tar.gz
click>=8.1.2 click>=8.1.2
hamlish-jinja@https://git.barkshark.xyz/barkshark/hamlish-jinja/archive/0.3.5.tar.gz hamlish-jinja@https://git.barkshark.xyz/barkshark/hamlish-jinja/archive/0.3.5.tar.gz
hiredis==2.3.2 hiredis==2.3.2
markdown==3.5.2
platformdirs==4.2.0 platformdirs==4.2.0
pyyaml>=6.0 pyyaml>=6.0
redis==5.0.1 redis==5.0.1