feat: slim JSON, EmojiMarkupRenderer, configurable delimiters
- JSONRenderer(slim=True): null·기본값·빈 attributes 필드 제거 - EmojiMarkupRenderer: CTTree → 이모지 마크업 텍스트 왕복 변환 지원 - title_mark / section_marks 커스터마이징 가능 - names_per_line 옵션으로 줄당 이름 수 제한 - EmojiMarkupParser: 구분자를 생성자 파라미터로 주입 가능 - section_marks 길이 제한 없음 (임의 깊이 섹션 지원) - 긴 마커 우선 매칭으로 prefix 충돌 방지 - 테스트 39개 추가 (커스텀 구분자·렌더러·slim 검증) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -3,11 +3,16 @@ ctstruct/parsers/emoji_markup.py
|
||||
이모지 마크업 포맷 → CTTree 파서
|
||||
|
||||
지원 포맷:
|
||||
✅제목 → 문서 제목 (root 속성)
|
||||
🔹1단계 섹션 → SECTION (level=1)
|
||||
🔸2단계 섹션 → SECTION (level=2)
|
||||
(카테고리) 이름 → 카테고리 태그가 붙은 LIST + LIST_ITEM들
|
||||
이름1 이름2 → 카테고리 없는 LIST + LIST_ITEM들 (줄 연속 가능)
|
||||
<title_mark>제목 → 문서 제목 (root 속성)
|
||||
<section_marks[0]>1단계 → SECTION (level=1)
|
||||
<section_marks[1]>2단계 → SECTION (level=2)
|
||||
…
|
||||
(카테고리) 이름 → 카테고리 태그가 붙은 LIST + LIST_ITEM들
|
||||
이름1 이름2 → 카테고리 없는 LIST + LIST_ITEM들 (줄 연속 가능)
|
||||
|
||||
기본 구분자:
|
||||
title_mark = "✅"
|
||||
section_marks = ["🔹", "🔸"]
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -21,36 +26,55 @@ from .base import BaseParser
|
||||
|
||||
class EmojiMarkupParser(BaseParser):
|
||||
"""
|
||||
✅ / 🔹 / 🔸 이모지로 계층을 표현하는 텍스트 포맷 파서.
|
||||
임의 구분자로 계층을 표현하는 텍스트 포맷 파서.
|
||||
|
||||
명단은 공백으로 구분된 이름 목록이며, 빈 줄 또는 새로운 섹션/카테고리
|
||||
마커가 나올 때까지 같은 그룹으로 묶입니다.
|
||||
구분자는 이모지, 특수문자, 문자열 등 어떤 값도 사용할 수 있습니다.
|
||||
|
||||
Args:
|
||||
title_mark: 문서 제목 줄 앞에 붙는 마커 (기본 "✅")
|
||||
section_marks: 섹션 레벨별 마커 목록 (기본 ["🔹", "🔸"])
|
||||
인덱스 0 → level 1, 인덱스 1 → level 2, …
|
||||
더 긴 마커가 먼저 매칭되도록 내부에서 길이 내림차순 정렬됩니다.
|
||||
|
||||
Examples:
|
||||
>>> parser = EmojiMarkupParser()
|
||||
>>> tree = parser.parse(text)
|
||||
>>> tree.pretty()
|
||||
# 기본 이모지 구분자
|
||||
parser = EmojiMarkupParser()
|
||||
|
||||
# 마크다운 스타일 구분자
|
||||
parser = EmojiMarkupParser(title_mark="# ", section_marks=["## ", "### "])
|
||||
|
||||
# 커스텀 구분자
|
||||
parser = EmojiMarkupParser(title_mark="[제목]", section_marks=["[1]", "[2]", "[3]"])
|
||||
"""
|
||||
|
||||
_TITLE = "✅"
|
||||
_SEC1 = "🔹"
|
||||
_SEC2 = "🔸"
|
||||
_RE_CAT = re.compile(r'^\(([^)]+)\)\s*(.*)')
|
||||
_RE_CAT = re.compile(r'^\(([^)]+)\)\s*(.*)', re.DOTALL)
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
title_mark: str = "✅",
|
||||
section_marks: list[str] | None = None,
|
||||
) -> None:
|
||||
self.title_mark = title_mark
|
||||
self.section_marks = section_marks if section_marks is not None else ["🔹", "🔸"]
|
||||
|
||||
# 긴 마커를 먼저 검사해 prefix 충돌 방지 (예: "##" vs "#")
|
||||
self._sorted_marks: list[tuple[str, int]] = sorted(
|
||||
((m, i + 1) for i, m in enumerate(self.section_marks)),
|
||||
key=lambda x: len(x[0]),
|
||||
reverse=True,
|
||||
)
|
||||
|
||||
def parse(self, text: str) -> CTTree:
|
||||
tree = CTTree()
|
||||
|
||||
# 현재 컨텍스트 추적
|
||||
sec1: CTNode | None = None
|
||||
sec2: CTNode | None = None
|
||||
# 각 레벨별 현재 활성 섹션 노드 (인덱스 = level-1)
|
||||
section_stack: list[CTNode | None] = [None] * len(self.section_marks)
|
||||
|
||||
# 아직 트리에 붙이지 않은 이름 버퍼
|
||||
pending_names: list[str] = []
|
||||
pending_category: str | None = None
|
||||
pending_parent: CTNode | None = None # 이름 그룹이 붙을 부모 노드
|
||||
pending_names: list[str] = []
|
||||
pending_category: str | None = None
|
||||
pending_parent: CTNode | None = None
|
||||
|
||||
def flush() -> None:
|
||||
"""버퍼에 쌓인 이름을 LIST로 만들어 부모에 붙인다."""
|
||||
nonlocal pending_names, pending_category, pending_parent
|
||||
if not pending_names:
|
||||
return
|
||||
@@ -60,9 +84,16 @@ class EmojiMarkupParser(BaseParser):
|
||||
lst.append(CTNode(NodeType.LIST_ITEM, name))
|
||||
target = pending_parent if pending_parent is not None else tree.root
|
||||
target.append(lst)
|
||||
pending_names = []
|
||||
pending_names.clear()
|
||||
pending_category = None
|
||||
|
||||
def nearest_parent(level: int) -> CTNode:
|
||||
"""level 보다 얕은 가장 가까운 활성 섹션, 없으면 root."""
|
||||
for i in range(level - 2, -1, -1):
|
||||
if section_stack[i] is not None:
|
||||
return section_stack[i] # type: ignore[return-value]
|
||||
return tree.root
|
||||
|
||||
for raw in text.splitlines():
|
||||
line = raw.strip()
|
||||
|
||||
@@ -70,31 +101,31 @@ class EmojiMarkupParser(BaseParser):
|
||||
flush()
|
||||
continue
|
||||
|
||||
# ✅ 문서 제목
|
||||
if line.startswith(self._TITLE):
|
||||
# 제목 마커
|
||||
if line.startswith(self.title_mark):
|
||||
flush()
|
||||
tree.root.set_attr("title", line[len(self._TITLE):].strip())
|
||||
tree.root.set_attr("title", line[len(self.title_mark):].strip())
|
||||
continue
|
||||
|
||||
# 🔹 1단계 섹션
|
||||
if line.startswith(self._SEC1):
|
||||
flush()
|
||||
sec1 = CTNode(NodeType.SECTION, line[len(self._SEC1):].strip(), {"level": 1})
|
||||
sec2 = None
|
||||
pending_parent = sec1
|
||||
tree.root.append(sec1)
|
||||
# 섹션 마커 (긴 것 우선 매칭)
|
||||
matched = False
|
||||
for mark, level in self._sorted_marks:
|
||||
if line.startswith(mark):
|
||||
flush()
|
||||
content = line[len(mark):].strip()
|
||||
node = CTNode(NodeType.SECTION, content, {"level": level})
|
||||
# 현재 레벨 이하의 스택 초기화
|
||||
for i in range(level - 1, len(section_stack)):
|
||||
section_stack[i] = None
|
||||
section_stack[level - 1] = node
|
||||
pending_parent = node
|
||||
nearest_parent(level).append(node)
|
||||
matched = True
|
||||
break
|
||||
if matched:
|
||||
continue
|
||||
|
||||
# 🔸 2단계 섹션
|
||||
if line.startswith(self._SEC2):
|
||||
flush()
|
||||
sec2 = CTNode(NodeType.SECTION, line[len(self._SEC2):].strip(), {"level": 2})
|
||||
pending_parent = sec2
|
||||
parent = sec1 if sec1 is not None else tree.root
|
||||
parent.append(sec2)
|
||||
continue
|
||||
|
||||
# (카테고리) 이름들 — 새 카테고리이면 기존 버퍼를 먼저 flush
|
||||
# 카테고리 마커 → 새 그룹 시작
|
||||
m = self._RE_CAT.match(line)
|
||||
if m:
|
||||
flush()
|
||||
@@ -104,7 +135,7 @@ class EmojiMarkupParser(BaseParser):
|
||||
pending_names.extend(rest.split())
|
||||
continue
|
||||
|
||||
# 이름 목록 줄 (카테고리 없이 연속)
|
||||
# 이름 목록 (연속 줄은 같은 그룹으로 병합)
|
||||
pending_names.extend(line.split())
|
||||
|
||||
flush()
|
||||
|
||||
Reference in New Issue
Block a user