"""M01 「만들기」 저장소 — 원문 절 목록 · 표 조건 값 · 자체 로직 만들기·본뜨기·고치기·지우기
· 복사해서 만들기(정본 로직을 별칭 꼴 식으로 펴 보이고 되돌려 저장).
계약: `resources/master_data/_화면_계약.md` 2장·5장·7장 · 파일 모양: `_틀.md` 7장.
자체 로직은 `로직_자체.json` 한 파일 · 키는 `_키대장.json` 의 `GX` 다음 번호(서버가 줌).
"""
from __future__ import annotations
import re
from pathlib import Path
from common_util.common_util_time import kst_today
from M01_MasterData import M01_MasterData_Machine as machine
from M01_MasterData import M01_MasterData_Store as store
from M01_MasterData import M01_MasterData_Store_Revise as revise
cm, mk, mcp = store.cm, store.mk, store.mcp
OWN = "로직_자체.json" # 자체 로직 한 자리 — 이 파일 줄만 고치고 지울 수 있음
OWNERS = ("현장", "공용")
BODY = mk._BODY # resources/knowledge/original/원가계산
BOOKS = {"산림품셈": "산림_표준품셈", "건설품셈": "건설공사_표준품셈"}
_HEAD = re.compile(r"^#{2,6}\s*(\d+(?:-\d+)*)\.?\s+(\S.*?)\s*$")
_CHAPTER_DIR = re.compile(r"^제(\d\d)장_(.+)$")
# ── 원문 절 목록 ───────────────────────────────────────────────────────
def _chapters(book: str):
"""(부문, 장 번호, 장 제목, 폴더) — 건설품셈은 부문 폴더 아래."""
root = BODY / BOOKS[book] / "본문"
tops = sorted(root.glob("*부문")) if book == "건설품셈" else [root]
for top in tops:
division = top.name.split("_", 1)[1].removesuffix("부문") if book == "건설품셈" else ""
for folder in sorted(top.glob("제*장_*")):
m = _CHAPTER_DIR.match(folder.name)
if m:
yield division, m.group(1), m.group(2).replace("_", " "), folder
_CACHE: dict[str, list[dict]] = {}
def _all_sections(book: str) -> list[dict]:
"""원문 본문 제목 줄에서 모은 절 — 한 번 세워 두고 다시 씀(원문은 안 바뀜)."""
if book in _CACHE:
return _CACHE[book]
out, seen = [], set()
for division, number, title, folder in _chapters(book):
for md in sorted(folder.glob("*.md")):
for line in md.read_text(encoding="utf-8").split("\n"):
m = _HEAD.match(line)
if not m or m.group(1).split("-")[0] != str(int(number)):
continue
if (division, m.group(1)) in seen:
continue
seen.add((division, m.group(1)))
out.append(
{
"book": book,
"division": division,
"chapter": f"{division} {number}장 {title}".strip(),
"chapterNo": number,
"section": m.group(1),
"title": m.group(2),
}
)
_CACHE[book] = out
return out
# ── 로직 원문(출처 글 → 품셈 본문 절) ──────────────────────────────────
_NUMBER = re.compile(r"(?~`])") # 마크다운 이스케이프 `\.` `\-` …
_COMMENT = re.compile(r"") # 글 안 주석(글자층 표시) — 글자만 남김
_CIRCLED = "①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳"
def _plain(text: str) -> str:
"""원문 md 기호 벗김 — 주석 · 이스케이프 · 인용 머리 `> `."""
text = _ESCAPE.sub(r"\1", _COMMENT.sub("", text)).strip()
return text[1:].strip() if text.startswith(">") else text
def _blocks(lines: list[str]) -> list[dict]:
"""md 줄 → 화면 조각 — 제목 {h} · 표 {table} · 글 {p} · 주석 줄은 뺌 ·
빈 줄 · 주석이 표를 끊음(붙은 표 둘을 한 표로 안 합침) · `
` = 줄바꿈."""
out: list[dict] = []
joinable = False
for line in lines:
text = re.sub(r"
", "\n", line.strip())
if not text or text.startswith("