Files
Aislo/common_util/common_util_help_router.py
T

35 lines
1.3 KiB
Python

"""[?] 설명서 글(54-2) — `resources/help/<페이지>/*.md` 를 부를 때마다 읽어 줌.
사용자가 메모장으로 파일을 고치면 화면 고침 없이 다음에 열 때 바뀐 글이 보임.
파일 이름 순서 = 모달 왼쪽 차례 순서 · 제목 = 첫 `# ` 줄(없으면 파일 이름).
"""
from __future__ import annotations
import re
from pathlib import Path
from fastapi import APIRouter, HTTPException
router = APIRouter(prefix="/api/help", tags=["help"])
HELP_ROOT = Path(__file__).resolve().parent.parent / "resources" / "help"
PAGE_RE = re.compile(r"^(B0[2-9]|D0[1-4]|E0[1-7])$")
def read_help(page: str, root: Path = HELP_ROOT) -> list[dict[str, str]]:
if not PAGE_RE.match(page):
raise HTTPException(status_code=404, detail="없는 페이지")
folder = root / page
items = []
for path in sorted(folder.glob("*.md")) if folder.is_dir() else []:
text = path.read_text(encoding="utf-8-sig")
head = next((line[2:].strip() for line in text.splitlines() if line.startswith("# ")), "")
items.append({"key": path.stem, "title": head or path.stem, "text": text})
return items
@router.get("/{page}")
async def get_help(page: str) -> dict[str, list[dict[str, str]]]:
return {"items": read_help(page)}