Progress.

This commit is contained in:
retoor 2025-01-24 23:33:36 +01:00
parent 2ba55f692d
commit 18b76ebd5e
3 changed files with 34 additions and 3 deletions

View File

@ -2,11 +2,12 @@ import pathlib
from aiohttp import web
from app.app import Application as BaseApplication
from snek.docs.app import Application as DocsApplication
from app.cache import time_cache_async
from jinja_markdown2 import MarkdownExtension
from snek.mapper import get_mappers
from snek.service import get_services
from snek.system import http
from snek.system.markdown import MarkdownExtension
from snek.system.middleware import cors_middleware
from snek.view.about import AboutHTMLView, AboutMDView
from snek.view.docs import DocsHTMLView, DocsMDView
@ -59,6 +60,8 @@ class Application(BaseApplication):
self.router.add_get("/http-get", self.handle_http_get)
self.router.add_get("/http-photo", self.handle_http_photo)
self.add_subapp("/docs", DocsApplication(path=pathlib.Path(__file__).parent.joinpath("docs")))
async def handle_test(self, request):
return await self.render_template(

View File

@ -1,6 +1,7 @@
# Original source: https://brandonjay.dev/posts/2021/render-markdown-html-in-python-with-jinja2
from types import SimpleNamespace
from mistune import escape
from mistune import Markdown
from mistune import HTMLRenderer
@ -12,6 +13,8 @@ import functools
from app.cache import time_cache_async
class MarkdownRenderer(HTMLRenderer):
_allow_harmful_protocols = True
def __init__(self, app, template):
self.template = template
@ -45,4 +48,29 @@ def render_markdown_sync(app, markdown_string):
@time_cache_async(120)
async def render_markdown(app, markdown_string):
return render_markdown_sync(app,markdown_string)
return render_markdown_sync(app,markdown_string)
from jinja2 import nodes, TemplateSyntaxError
from jinja2.ext import Extension
from jinja2.nodes import Const
# Source: https://ron.sh/how-to-write-a-jinja2-extension/
class MarkdownExtension(Extension):
tags = {'markdown'}
def __init__(self, environment):
self.app = SimpleNamespace(jinja2_env=environment)
super(MarkdownExtension, self).__init__(environment)
def parse(self, parser):
line_number = next(parser.stream).lineno
md_file = [Const('')]
body = ''
try:
md_file = [parser.parse_expression()]
except TemplateSyntaxError:
body = parser.parse_statements(['name:endmarkdown'], drop_needle=True)
return nodes.CallBlock(self.call_method('_to_html', md_file), [], [], body).set_lineno(line_number)
def _to_html(self, md_file, caller):
return render_markdown_sync(self.app,caller())

View File

@ -16,7 +16,7 @@
<fancy-button url="/register.html" text="Register"></fancy-button>
<a href="/about.html">Design choices</a>
<a href="/web.html">App preview</a>
<a href="/docs.html">API docs</a>
<a href="/docs/docs/">API docs</a>
</div>
</body>
</html>