diff --git a/app.py b/app.py index 17be332..e6f448e 100644 --- a/app.py +++ b/app.py @@ -1,20 +1,31 @@ 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 markdown_render import MarkdownRenderer - +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() -md_renderer = MarkdownRenderer() +# 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 ) +app.add_middleware( HTTPSRedirectMiddleware ) # Templates directory templates = Jinja2Templates(directory="templates") @@ -24,34 +35,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