From b2f8cc3c818ffb2e3909605c01ea7994f7912c8d Mon Sep 17 00:00:00 2001 From: Henrik Jess Nielsen Date: Wed, 11 Dec 2024 20:49:14 +0100 Subject: [PATCH] Lets test --- app.py | 102 +++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 73 insertions(+), 29 deletions(-) diff --git a/app.py b/app.py index 4e26457..41dd508 100644 --- a/app.py +++ b/app.py @@ -1,19 +1,34 @@ from fastapi import FastAPI, Request from fastapi.staticfiles import StaticFiles -from fastapi.responses import HTMLResponse from fastapi.templating import Jinja2Templates import json import os - - - +from fastapi import FastAPI +from fastapi.responses import HTMLResponse +from contextlib import asynccontextmanager +from markdown_render import render_markdown_with_jinja from starlette.middleware.httpsredirect import HTTPSRedirectMiddleware -app = FastAPI() + + + + +# Context manager for app lifespan +@asynccontextmanager +async def lifespan(app: FastAPI): + print("App startup: Processing Markdown files...") + process_markdown_files("./data", "./data") # Process all Markdown files + print("Markdown processing complete!") + yield # Allow the app to start + print("App shutdown: Cleanup complete.") + +app = FastAPI(lifespan=lifespan) +app.mount("/data", StaticFiles(directory="data"), name="data") + # Mount static files app.mount("/static", StaticFiles(directory="static"), name="static") -#app.add_middleware( HTTPSRedirectMiddleware ) + # Templates directory templates = Jinja2Templates(directory="templates") @@ -23,34 +38,22 @@ with open("mock_data.json") as file: data = json.load(file) @app.get("/test", response_class=HTMLResponse) -async def mark_test(): - markdown_content = """ - # Custom Tags Test +async def home_test(): + # Load the Markdown content from a file + with open("templates/example.md", "r") as f: + markdown_content = f.read() - Here is an image overlay: - {img-left-overlay: src=my-cat.png} + # Render Markdown first, then inject Jinja2 components + rendered_html = render_markdown_with_jinja(markdown_content) - Here is a box: - {box: title=Important, content=This is a reusable box.} - - And a note: - {note: content=This is a note for the user.} - - Warning section: - {warning: content=Pay attention to this warning!} - """ - - # Render Markdown content - rendered_html = md_renderer.render( markdown_content ) - - # Wrap in a basic template - template = f""" + # Wrap in a base HTML layout + html_template = f""" - Markdown Tags + Markdown + Jinja2