Spaces:
Sleeping
Sleeping
File size: 1,180 Bytes
14c7fcf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | from __future__ import annotations
import re
from .models import Section
_HEADING = re.compile(r"^(#{1,6})\s+(.+?)\s*$")
def split_markdown_sections(markdown: str) -> list[Section]:
"""Split Markdown into heading-aware sections while preserving source order."""
lines = markdown.splitlines()
sections: list[Section] = []
current_title = "Document opening"
current_level = 1
current_lines: list[str] = []
def flush() -> None:
nonlocal current_lines
content = "\n".join(current_lines).strip()
if content:
sections.append(
Section(title=current_title, level=current_level, content=content)
)
current_lines = []
for line in lines:
heading = _HEADING.match(line)
if heading:
flush()
current_level = len(heading.group(1))
current_title = heading.group(2).strip()
else:
current_lines.append(line)
flush()
return sections
def section_table(sections: list[Section]) -> list[list[object]]:
return [[index + 1, item.level, item.title, item.word_count] for index, item in enumerate(sections)]
|