generated from hjess/PythonTemplateProject
https enforcement
This commit is contained in:
52
app.py
52
app.py
@@ -4,14 +4,17 @@ from fastapi.responses import HTMLResponse
|
||||
from fastapi.templating import Jinja2Templates
|
||||
import json
|
||||
import os
|
||||
from markdown_render import MarkdownRenderer
|
||||
|
||||
|
||||
from starlette.middleware.httpsredirect import HTTPSRedirectMiddleware
|
||||
|
||||
app = FastAPI()
|
||||
md_renderer = MarkdownRenderer()
|
||||
|
||||
# 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")
|
||||
@@ -20,6 +23,51 @@ templates = Jinja2Templates(directory="templates")
|
||||
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
|
||||
|
||||
Here is an image overlay:
|
||||
{img-left-overlay: src=my-cat.png}
|
||||
|
||||
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"""
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Markdown Tags</title>
|
||||
<style>
|
||||
.img-left-overlay img {{ width: 300px; }}
|
||||
.box {{ border: 1px solid #ccc; padding: 10px; background-color: #f9f9f9; }}
|
||||
.note {{ background-color: #e7f3fe; border-left: 4px solid #2196F3; padding: 10px; }}
|
||||
.warning {{ background-color: #fff3cd; border-left: 4px solid #ffeb3b; padding: 10px; }}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="content">
|
||||
{rendered_html}
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
return HTMLResponse( content = template )
|
||||
|
||||
# Index route
|
||||
@app.get("/", response_class=HTMLResponse)
|
||||
async def get_index(request: Request):
|
||||
@@ -28,6 +76,8 @@ async def get_index(request: Request):
|
||||
{"request": request, "data": data, "page_title": "Forside", "author": "Henrik"}
|
||||
)
|
||||
|
||||
|
||||
|
||||
# Category route
|
||||
@app.get("/category/{category_name}", response_class=HTMLResponse)
|
||||
async def get_category(request: Request, category_name: str):
|
||||
|
||||
Reference in New Issue
Block a user