Switch to gunicorn , need to add config file and proper password loading
All checks were successful
Datadog Software Composition Analysis / Datadog SBOM Generation and Upload (push) Successful in 16s
Datadog Static Analysis / Datadog Static Analyzer (push) Successful in 28s

This commit is contained in:
2025-02-28 23:23:16 -05:00
parent c5caa0848b
commit 4d79f86df4
7 changed files with 429 additions and 28 deletions

View File

@ -1,10 +1,9 @@
from dataclasses import dataclass
from src.config.config import Configuration
from src.rendering import GENERIC_FILE_MAPPING
from src.rendering.markdown import render_markdown
from src.rendering.markdown import render_markdown, read_raw_markdown, rendered_markdown_to_plain_text
from enum import Enum
from thumbhash import image_to_thumbhash
from PIL import Image
from datetime import datetime
import frontmatter
@ -78,9 +77,9 @@ class TemplateHelpers:
ret.typeMeta = MarkdownMetadata({}, "", "")
ret.typeMeta.fontmatter = frontmatter.load(file_path)
ret.typeMeta.content = render_markdown(file_path)
ret.typeMeta.preview = ret.typeMeta.content[:100]
if "#" in ret.typeMeta.preview:
ret.typeMeta.preview = ret.typeMeta.preview.split("#")[0]
ret.typeMeta.rawContent = read_raw_markdown(file_path)
ret.typeMeta.rawText = rendered_markdown_to_plain_text(ret.typeMeta.content)
ret.typeMeta.preview = ret.typeMeta.rawText[:500] + "..."
return ret
return None

View File

@ -1,6 +1,7 @@
import frontmatter
import mistune
from pathlib import Path
from bs4 import BeautifulSoup
mistune_render = mistune.create_markdown(
@ -10,7 +11,18 @@ mistune_render = mistune.create_markdown(
def render_markdown(path: Path) -> str:
with open(path, "r") as f:
with open(path, "r", encoding="utf-8") as f:
obj = frontmatter.load(f)
res = mistune_render(obj.content)
return res
return res
def read_raw_markdown(path: Path) -> str:
with open(path, "r") as f:
obj = frontmatter.load(f)
return obj.content
def rendered_markdown_to_plain_text(html):
text = "\n\n".join(BeautifulSoup(html, features="html.parser").stripped_strings)
return text

View File

@ -1,6 +1,6 @@
from pathlib import Path
from typing import Optional
from pprint import pprint as pp
from pprint import pprint
from flask import render_template_string, send_file
from src.rendering import GENERIC_FILE_MAPPING
@ -23,15 +23,15 @@ def count_file_extensions(path):
def determine_type(path: Path) -> tuple[str, str, Optional[str]]:
if path.is_file():
# Find extension in GENERIC_FILE_MAPPING, it may match multiple
generic_mapping = None
generic_mapping = []
for file_type, extensions in GENERIC_FILE_MAPPING.items():
if path.suffix[1:] in extensions:
if generic_mapping is None:
if not generic_mapping:
generic_mapping = [file_type]
else:
generic_mapping.append(file_type)
generic_mapping.reverse()
if generic_mapping is None:
if not generic_mapping:
generic_mapping = ["other"]
extension = path.suffix[1:]
return "file", generic_mapping, extension
@ -39,7 +39,7 @@ def determine_type(path: Path) -> tuple[str, str, Optional[str]]:
files_map = count_file_extensions(path)
if files_map:
most_seen_extension = max(files_map, key=files_map.get)
generic_mapping = None
generic_mapping = []
for file_type, extensions in GENERIC_FILE_MAPPING.items():
if most_seen_extension[1:] in extensions:
if generic_mapping is None:
@ -97,10 +97,10 @@ def render_page(
):
if not path.exists():
return render_error_page(
404,
"Not Found",
"The requested resource was not found on this server.",
template_path,
error_code=404,
error_message="Not Found",
error_description="The requested resource was not found on this server.",
template_path=template_path,
)
target_path = path
target_file = path