From 18b76ebd5e2f11451db04800d426a16b1ef1dd14 Mon Sep 17 00:00:00 2001 From: retoor Date: Fri, 24 Jan 2025 23:33:36 +0100 Subject: [PATCH] Progress. --- src/snek/app.py | 5 ++++- src/snek/system/markdown.py | 30 +++++++++++++++++++++++++++++- src/snek/templates/index.html | 2 +- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/snek/app.py b/src/snek/app.py index 0cca574..78c0e2b 100644 --- a/src/snek/app.py +++ b/src/snek/app.py @@ -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( diff --git a/src/snek/system/markdown.py b/src/snek/system/markdown.py index 489a3e5..fcffe40 100644 --- a/src/snek/system/markdown.py +++ b/src/snek/system/markdown.py @@ -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) \ No newline at end of file + 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()) \ No newline at end of file diff --git a/src/snek/templates/index.html b/src/snek/templates/index.html index ad02a6a..c6d57df 100644 --- a/src/snek/templates/index.html +++ b/src/snek/templates/index.html @@ -16,7 +16,7 @@ Design choices App preview - API docs + API docs