Implementation:666ghj BettaFish parse template sections
| Knowledge Sources | |
|---|---|
| Domains | Report_Generation, domain:Text_Processing |
| Last Updated | 2026-10-08 00:00 GMT |
Overview
Concrete function that parses a Markdown template into structured sections (TemplateSection dataclasses) with title, slug, order, depth, and outline information.
Description
The parse_template_sections function uses regular expressions and Unicode normalization to parse a Markdown template file into a list of `TemplateSection` dataclasses. Each section captures the heading level, title, auto-generated slug, sequential order, and the content outline beneath it. This structured representation is consumed by downstream nodes (DocumentLayoutNode, WordBudgetNode, ChapterGenerationNode) that need section-level metadata.
Usage
Import this function when you need to decompose a Markdown template into structured sections. It runs after TemplateSelectionNode in the ReportEngine pipeline.
Code Reference
Source Location
- Repository: BettaFish
- File: `ReportEngine/core/template_parser.py:L87-302` (function), `L19-51` (TemplateSection dataclass)
Signature
@dataclass
class TemplateSection:
title: str
slug: str
order: int
depth: int
number: str
chapter_id: str
outline: str
def to_dict(self) -> Dict[str, Any]:
"""Convert to dict for JSON serialization."""
def parse_template_sections(template_md: str) -> List[TemplateSection]:
"""
Parse a Markdown template into structured sections.
Args:
template_md: Full Markdown template text
Returns:
List of TemplateSection with title, slug, order, depth, chapter_id, outline
"""
Import
from ReportEngine.core.template_parser import parse_template_sections, TemplateSection
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| template_md | str | Yes | Full Markdown template text |
Outputs
| Name | Type | Description |
|---|---|---|
| sections | List[TemplateSection] | Parsed sections with metadata |
Usage Examples
Parse Template
from ReportEngine.core.template_parser import parse_template_sections
with open("ReportEngine/report_template/突发事件与危机公关舆情报告模板.md", "r") as f:
template_md = f.read()
sections = parse_template_sections(template_md)
print(f"Parsed {len(sections)} sections:")
for s in sections:
print(f" {s.order}. [{s.depth}] {s.title} (slug: {s.slug})")