Files
MDHelper/mdhelper/code_structure.py
T
2026-06-14 09:51:16 +08:00

81 lines
3.0 KiB
Python

from __future__ import annotations
import ast
import re
from pathlib import Path
from .document_tree import CPP_EXTENSIONS, PYTHON_EXTENSIONS
from .markdown_utils import Heading
from .text_io import DEFAULT_TEXT_PREVIEW_BYTES, read_text_with_fallback
_CPP_TYPE_RE = re.compile(r"^\s*(class|struct)\s+([A-Za-z_]\w*)\b")
_CPP_FUNCTION_RE = re.compile(
r"^\s*(?:template\s*<[^;{}]+>\s*)?(?:[A-Za-z_~][\w:<>~*&\s,]*\s+)?([A-Za-z_~][\w:]*)\s*\([^;{}]*\)\s*(?:const\s*)?(?:noexcept\s*)?(?:->\s*[^{};]+\s*)?(?:\{|$)"
)
_CPP_CONTROL_WORDS = {"if", "for", "while", "switch", "catch", "return", "sizeof"}
def extract_code_headings(path: Path, source: str | None = None) -> list[Heading]:
suffix = path.suffix.lower()
if source is None:
source = read_text_with_fallback(path, max_bytes=DEFAULT_TEXT_PREVIEW_BYTES)
if suffix in PYTHON_EXTENSIONS:
return _extract_python_headings(source)
if suffix in CPP_EXTENSIONS:
return _extract_cpp_headings(source)
return []
def _extract_python_headings(source: str) -> list[Heading]:
try:
tree = ast.parse(source)
except SyntaxError:
return []
headings: list[Heading] = []
def walk(nodes: list[ast.stmt], level: int) -> None:
for node in nodes:
if isinstance(node, ast.ClassDef):
headings.append(Heading(level=min(level, 6), title=f"class {node.name}", slug=f"line-{node.lineno}"))
walk(node.body, min(level + 1, 6))
elif isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
prefix = "async def" if isinstance(node, ast.AsyncFunctionDef) else "def"
headings.append(Heading(level=min(level, 6), title=f"{prefix} {node.name}", slug=f"line-{node.lineno}"))
walk(node.body, min(level + 1, 6))
walk(list(tree.body), 1)
return headings
def _extract_cpp_headings(source: str) -> list[Heading]:
headings: list[Heading] = []
pending_template = False
for lineno, line in enumerate(source.splitlines(), start=1):
stripped = line.strip()
if not stripped or stripped.startswith(("//", "#")):
continue
if stripped.startswith("template"):
pending_template = True
continue
type_match = _CPP_TYPE_RE.match(line)
if type_match:
kind, name = type_match.groups()
headings.append(Heading(level=1, title=f"{kind} {name}", slug=f"line-{lineno}"))
pending_template = False
continue
function_line = f"template <> {line}" if pending_template else line
func_match = _CPP_FUNCTION_RE.match(function_line)
if not func_match:
pending_template = False
continue
name = func_match.group(1).split("::")[-1]
if name in _CPP_CONTROL_WORDS:
pending_template = False
continue
headings.append(Heading(level=2, title=f"function {func_match.group(1)}", slug=f"line-{lineno}"))
pending_template = False
return headings